Skip to content

Linux: glibc's ULLONG_MAX and other mixed-sign macros translate with C's usual conversions; the runtime's sigprocmask gets a whole sigset_t; the migrator never drops discarded effects (#1911) - #1934

Merged
ehartford merged 4 commits into
mainfrom
fix-1911
Sep 30, 2026

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1911 (root causes found with gdb on eric-5090):

  • Main did not build on Linux: rt/linux_x86_64.w's /dev/urandom fallback passed a u64 remainder to rt_read's i64 (§4.2.6).
  • The facade overflow panics: glibc defines ULLONG_MAX as (LLONG_MAX * 2ULL + 1); c_import translated it as i64 multiplication. The translator now applies C's usual arithmetic conversions to mixed-signedness operands in translated macros (ULLONG_MAX → ((LLONG_MAX as u64) * 2u64) + 1); a comparison macro returns c_int.
  • behav_migrate_assert_macro: (a) the Linux runtimes kept the saved sigprocmask in 4 bytes (Darwin's size) while glibc writes 128 — it overwrote the saved cwd pointer and children exited 127; plus Darwin values for SIG_BLOCK/SIG_SETMASK, a 16-byte sigaction buffer (Linux reads 152), and RLIM_INFINITY; (b) the migrator merged a failed comma operand as an empty statement (CImport.w merge_ir with rhs 0), silently dropping glibc's assert body — a failed operand now fails the comma, extension passes through, a discarded ({…}) keeps its statements, a discarded operator keeps its operands' effects.

Tests red → green: behav_c_import_macro_usual_conversions, behav_migrate_discarded_effects; the four facade tests and behav_migrate_assert_macro green on Linux. Mac :gate green. Draft: #1933 fixes and the Linux zero-dependency work (#1915) land on this branch / above it next; one battery for the batch.

@ehartford
ehartford added this pull request to stack #1936 September 30, 2026 11:09
Eric Hartford added 3 commits September 30, 2026 12:23
The Linux runtimes carried Darwin's signal layouts. posix_run_argv kept the
old signal mask in a u32 while glibc's sigprocmask writes all 128 bytes of
sigset_t: the write ran over posix_run_argv's frame and replaced the saved
cwd pointer, so every child of with_exec_argv_capture_cwd failed chdir
(EFAULT, exit 127). gdb on behav_migrate_assert_macro's kept binary:
posix_run_argv saves cwd at 0x10(%rsp), prev_mask lives at 0xc(%rsp), and
the child called chdir(0x7df400000000).

Also Darwin's: SIG_BLOCK/SIG_SETMASK are 0/2 on Linux (1 was SIG_UNBLOCK,
3 EINVAL), struct sigaction is 152 bytes (sigaction read 136 bytes past a
16-byte buffer for sa_mask and sa_flags), and RLIM_INFINITY is ~0.
…arded effects are never dropped (#1911)

glibc's <limits.h> defines ULLONG_MAX as (LLONG_MAX * 2ULL + 1). The
translator rendered it LLONG_MAX * 2u64: i64 arithmetic by With's rules,
so __with_init_const_ULLONG_MAX called llvm.smul.with.overflow.i64(
9223372036854775807, 2) and every program importing it (zlib, libcurl
facades) panicked "i64 multiplication out of range" at start (9ee1234);
after §4.2.6 the same text is a refused sign change. ci_c_binary now types
each operand (suffixed literal, cast, emitted macro constant, translated
operator) and spells C's common type when signedness differs; a
function-like macro returns that type, and a comparison-valued one returns
c_int (it was `-> i32` over a bool).

glibc's assert is ((void) sizeof ((e) ? 1 : 0), __extension__ ({ if (e) ;
else __assert_fail (...); })). lower_effect_expr_ir merged the comma's
failed right operand (0) as an empty one, so a failed assert ran on
(behav_migrate_assert_macro line 19). A failed operand now fails the
comma; __extension__ is transparent (the bridge maps
CXUnaryOperator_Extension); a discarded statement expression lowers as its
statements; a discarded operator keeps its operands' side effects.
…never peeled; discarded arms keep their effects; hygienic helper names (#1933)

An UnexposedExpr with several expression operands is not a wrapper, but
every lowering peeled it to its last child: `__builtin_choose_expr(1, f(),
7)` returned 7, `f() ?: 1` returned 1 without calling f, and
`__atomic_load_n(&g, 0)` returned its memory order, 0. ci_unexposed_expr_form
names the forms: choose_expr (spelled in the source) lowers its selected
operand, `a ?: b` (libclang visits the common operand, its two opaque uses
as that same cursor, then the fallback) lowers through a temporary
evaluated once, and anything else fails the function with "C expression
`...` has no translation".

lower_discard_expr_side_effects_ir ended by turning a failed value lowering
into an empty statement and dropping effects inside a valid one:
`c ? arr[f() & 3] : 0` lost the call, and a refused union initializer in
that arm passed with its bail recorded and ignored. The tail is now the
expression statement lower_effect_expr_ir makes (zero on failure).

The migrator's helpers share a module with the C program's globals, so a
global named a, b, c, out or result made each helper parameter a refused
shadow. Helper parameters and locals take the __with_ prefix.
…arameters (#1933)

The migrator's helpers name their parameters `__with_a`/`__with_b` since
fe65171 so a C global `a` or `b` cannot collide; the builtin-overflow
case still looked for `u128_mul_would_overflow(a, b)`. The battery on
2152374 was red on this assertion alone.
@ehartford
ehartford removed this pull request from stack #1936 September 30, 2026 16:57
@ehartford
ehartford added this pull request to stack #1943 September 30, 2026 16:57
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on stack top 3743c81 (seed-driven, with run tools/battery.w; stack #1943 = #1934 → #1942): gate rc=0, battery-checks rc=0 wall=198s. Prior run on 2152374 was RED on c-migrator-core-tests alone (builtin_overflow_u128_limb_helper expected pre-#1933 helper parameter names); fixed in de9d19f. The rebase onto main resolved a CImport.w conflict (discarded && / ||): main's guarded-if kept, #1911's fail-loud assignment and operand-effects merge kept; conflict-area tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

linux-x86_64: c facade tests panic 'i64 multiplication out of range'; behav_migrate_assert_macro asserts

1 participant