Skip to content

c_import: a DEFINE_GUID var no longer feeds winnt.h to the C expression parser; parser bounded; OOM is loud (#1387) - #1399

Merged
ehartford merged 5 commits into
mainfrom
fix-1387-cimport-var-extent
Sep 23, 2026
Merged

ehartford merged 5 commits into
mainfrom
fix-1387-cimport-var-extent

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1387.

Root cause (exact lines, origin/main)

  1. src/CImport.w:14709-14710 — ci_var_init_expr_for_type fell through to text mining (ci_var_init_expr_from_decl_source_for_type) when clang_Cursor_getVarDeclInitializer returned null, i.e. for a var clang says has no initializer (every DEFINE_GUID).
  2. src/compiler/ClangBridge.w:3883 — cursor_spelling_text_from_cursor accepted a spelling range that starts in the macro body (extern on the #define line) and ends in the invocation argument (name): both in winnt.h, so it returned the ~15.6k lines between them. source_range_preferred_text_offsets already refused this shape; the spelling reader did not.
  3. src/CImport.w:13988 — ci_extract_var_initializer_text took the = of >= as the declarator's.
  4. ci_parse_*_expr hands the whole text to the next level when a split fails, so non-expression text costs grow with the product of split points.

Observed in lldb on darwin with the installed v0.15.2.1: ci_translate_c_initializer_for_cursor_type <- ci_var_init_expr_from_decl_source_for_type <- ci_var_init_expr_for_type+1396 with a 7,863-byte init_src starting 0\n#endif\n#define M0(a) ... (150-line repro).

Commits

  1. Var initializer text only when clang parsed an initializer; source_range_spelling_is_mixed is the single body+argument rule used by both text readers; ==/!=/<=/>=/compound ops are not the declarator =. Test: behav_c_import_macro_var_extent.w (ratio 200 vs 800 filler lines; macro-built and comparison-holding initializers keep their values).
  2. Scan budget for the string expression parser (64 MiB of scanned text per top-level expression; past it every level fails at once and a warning names the expression's size). Test: behav_c_import_expr_scan_budget.w.
  3. Loud OOM: the allocator's four refusal paths print with: out of memory: requested=N committed=M bytes to fd 2 (allocation-free) before exit 99. Verified by forcing rt_mmap to return 0 in lldb: with: out of memory: requested=65536 committed=0 bytes, exit 99. No compile phase in the message: the runtime doesn't know phases, and adding a hook from the compiler would need a new runtime export plus a seed bump.
  4. ci_detect_member_functions uses hash maps for the struct list and for emitted methods (5 of 6 cdb samples of the fixed Windows c_import were in its ci_str_contains scan). The translated_structs string itself still gets passed as known_structs: &str to about 15 functions. Changing it to a set would touch all of them, so I left it. With the member-function scan gone, is_name_emitted is now the top hot spot, and c_import is linear in a header's declarations: emitted names are a hash set, decl locations an index #1386 fixes that.

Measurements

Darwin (frontend.c_import), issue repro with one GUID var:

filler lines installed v0.15.2.1 this branch (stage1)
400 8.3 s wall, 2.0 GB RSS 0.33 s
800 119.6 s 0.31 s
1600 — 0.31 s

Darwin, bogus 4.6–148 KB macro body (budget commit): 100 fragments 3.9 s → 0.47 s; 200 fragments 104 s → 0.47 s; 400 fragments >300 s → 0.59 s; 3000 fragments → 1.7 s, 700 MB, warning.

Windows 10 x64 (win10, 32 GB), with check src/main.w in the bzip2 UAT project, uncached:

rc wall peak working set stderr
before (with v0.15.2.1-g7d6082598) 99 264.6 s 29,079 MB empty
this branch (stage2) 1 18.8 s (c_import 18.3 s) 201 MB parse errors in generated bindings, below
libcurl, this branch 1 20.7 s (c_import 20.7 s) 211 MB same

No budget warnings fire on the real windows.h closure.

The bzip2/libcurl UATs still fail on Windows. c_import now completes, but the bindings it generates don't parse. winnt.h's C_ASSERT(TYPE_ALIGNMENT(LARGE_INTEGER) == 8) declares an untagged struct inside an expression at file scope, and c_import emits it as type struct (unnamed at ...winnt.h:2602:1) {...}. This has a different root cause and reproduces on darwin with struct { int b; } g;. Filed as #1396.

Evidence it changes nothing else

  • 63 c_import behavior fixtures: each file run separately with this branch's stage1 and origin/main's stage1 gives the same pass/fail set. 58 pass on both. The 5 that fail on both are stage1 limits: p7 tests need stage2, and std.re isn't found from outside the repo. Those 5, plus the 2 new tests, all pass with this branch's stage2.
  • The generated c_import text for stdio.h, stdlib.h, string.h, time.h, pthread.h, sys/socket.h, zlib.h, sqlite3.h (162 method lines) and curses.h is byte-identical to origin/main's stage1.
  • The raw migrate output for pcre2 (36 files) and zlib (23 files) is byte-identical between the two stage1 builds.

Iterate tier only (:dev, :stage2, targeted tests). The seed-driven battery has not run, so this stays a draft.

Eric Hartford added 5 commits September 22, 2026 21:54
…sh set, decl locations an index

Two lookups scanned every entry on every query, so c_import cost grew with
the square of a header's declarations:

- ClangBridge `is_name_emitted` walked the list of emitted names with
  c_strcmp on every lookup and every mark. It is now an open-addressing
  hash set. ClangBridge is compiled without the prelude, so it cannot use
  std HashMap.
- CImport `ci_get_decl_location` walked every top-level cursor, making a
  spelling string for each, once per translated struct. It is now a
  name -> first cursor index, rebuilt when the session or parse generation
  changes and cleared at migration teardown, the same pattern as the #744
  indexes.

This is why behav_c_import_scales_linearly.w failed on the darwin,
linux-aarch64 and windows-x86_64 nightly legs (runs 35512407331 and
35616828206), and it is a cause of the bzip2/libcurl UAT timeouts over
windows.h on Windows. The installed compiler measured 0.72s / 5.88s /
23.3s at 750 / 3000 / 6000 declarations (8.2x for 4x the input); the test
allows 8x+1s, so a fast Mac passes and a CI runner does not.

Evidence: a `sample` profile at 6000 declarations had c_strcmp inside
is_name_emitted on top. Once that was fixed, lldb stack samples put 10 of
14 samples in ci_translate_struct -> ci_get_decl_location. A/B of seed-built
stage1 against the base commit, user CPU with fresh headers and about 3.25s
of fixed startup: 3000 declarations went from 15.0-17.3s to 8.4-11.0s,
750 declarations from 5.1-7.1s to 5.0-6.5s. The 40 c_import behavior
fixtures run the same on both.

(cherry picked from commit 816b15f)
…g parsed one for; mixed macro spelling ranges and `>=` are not initializers (#1387)

A DEFINE_GUID var (`EXTERN_C const GUID name`) has no initializer, yet
ci_var_init_expr_for_type fell through to text mining whenever libclang
returned no initializer cursor (CImport.w:14709-14710). The text came from
cursor_spelling_text_from_cursor, whose spelling range starts at `extern` in
the macro body and ends at `name` in the invocation: both in winnt.h, so it
returned the 15.6k lines between them (ClangBridge.w:3883, the shape
source_range_preferred_text_offsets already refused). ci_extract_var_initializer_text
then took the `=` of `#if _MSC_VER >= 1200` as the declarator's
(CImport.w:13988), and 373 KB of header went to the C expression parser:
29 GB and exit 99 on Windows.

- ci_var_init_expr_from_decl_source_for_type reads the declaration's text
  only when clang_Cursor_getVarDeclInitializer has an initializer; otherwise
  only migrate's raw-source lookup by name remains.
- source_range_spelling_is_mixed is the one rule for a body+argument range;
  the preferred-text and spelling-text readers both refuse it.
- `==`, `!=`, `<=`, `>=` and compound assignments are not the declarator `=`.

Observed in lldb on darwin (installed v0.15.2.1): ci_translate_c_initializer_for_cursor_type
called from ci_var_init_expr_from_decl_source_for_type <- ci_var_init_expr_for_type+1396
with a 7863-byte init_src starting "0\n#endif\n#define M0(a) ..." for the
150-line repro. Issue repro, frontend.c_import: 800 filler lines 119.6 s ->
0.31 s; 1600 lines 0.31 s.

(cherry picked from commit 36c79a8)
…ression; past it the text is reported untranslated (#1387)

Each ci_parse_* level splits at its operator and, when the split fails,
hands the whole text to the next level (`(T)-1` parses because add_expr's
failed split falls through to cast_expr). On text that is not an
expression the work multiplies across the split points: a 4.6 KB macro
body of `M{i}(a) ((a) * {i} + 1) - ((a) << 2) * 3 / 4 >= ` fragments took
3.9 s at 100 fragments, 104 s at 200 and did not finish in 300 s at 400
(installed v0.15.2.1, frontend.c_import). With the budget: 0.47 s, 0.47 s,
0.59 s, and 1.7 s at 3000 fragments (148 KB), each with a warning naming
the expression's size. Well-formed expressions scan a few times their
length and never approach 64 MiB.

(cherry picked from commit 4e0cc66)
…fore exit 99 (#1387)

The allocator's four refusal paths (system malloc, large mmap, slab mmap,
range-table growth) called rt_exit(99) with nothing on stderr, so the
Windows bzip2/libcurl UATs that grew to 29 GB left empty logs. They now
write `with: out of memory: requested=N committed=M bytes` to fd 2 with the
allocation-free dbg_puts/dbg_put_i64 path the memory-limit report already
uses, then exit 99 as before.

(cherry picked from commit 023cf0c)
…ot a scan of the pipe-joined struct list (#1387)

With the var-extent fix, c_import of bzlib.h on Windows finishes, but
still takes 19.6 s (curl.h 22.1 s). Five of six cdb stack samples of the
uncached check sit in ci_str_matches_at <- ci_str_contains <-
ci_detect_member_functions: for every function, whose first parameter or
return type names a struct, `ci_str_contains(known_structs, "|S|")` scans
the pipe-joined list of every translated struct (thousands under
windows.h), then a linear walk finds the struct's snake prefix, and
emitted_methods grows by `++` and is scanned the same way.

The struct list is parsed once into a name -> snake-prefix map (first
entry wins, as the linear walk did), emitted methods are a set, and the
output is a StringBuilder. Generated bindings are byte-identical to
origin/main's stage1 for stdio.h, stdlib.h, string.h, time.h, pthread.h,
sys/socket.h, zlib.h, sqlite3.h and curses.h (sqlite3.h: 162 method lines).

(cherry picked from commit 041b325)
@ehartford

Copy link
Copy Markdown
Contributor Author

Batch battery (seed-driven) on main 00408eb + #1386 → #1399 as one chain: build ok, :fixpoint ok, :test ok (no reds, incl. behav_c_import_scales_linearly), :test-green ok, :last-green ok. Branches now hold exactly the tested commits; merge #1386 then #1399.

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

Labels

None yet

Projects

None yet

1 participant