Skip to content

c_import: a DEFINE_GUID var's mixed macro spelling range plus '>=' taken as '=' feeds 373 KB of winnt.h to the C expression parser; bzip2/libcurl UATs OOM (exit 99) or time out on Windows #1387

Closed

Description

@ehartford

Symptom

Release UAT release-bzip2-uat and release-libcurl-uat time out (with run ... failed with exit code 124, 180 s) on both Windows legs (nightly 35616828206 windows-aarch64, 35512407331 windows-x86_64 and windows-aarch64). zlib, sqlite3 and the raylib spiral pass in ~10 s on the same legs; bzlib.h and curl.h are the two fixtures whose headers include windows.h.

On a native Windows 10 x64 host (32 GB, compiler built today from main-era branch windows-link-crt-and-debug-sections, with v0.15.2.1-g7d6082598), in a fresh with init + with get c.bzip2 project with the fixture as src/main.w:

step result
with get c.bzip2 ok, 4 s
with check src/main.w exit 99 after 338 s, no output
with run (twice) exit 99 after 271 s / 275 s, no output
libcurl: check / run / run exit 99 after 277 / 274 / 275 s

Working set grows ~150 MB/s to ~29 GB; exit 99 is rt_exit(99) from the allocator's mmap-failure paths in rt/rt_core.w (out of memory, silently). On a 16 GB CI runner the same growth pages until the 180 s UAT timeout: that is the exit 124. WITH_PROFILE=1 stops after frontend.interface: all the time is in frontend.c_import.

Exact path (cdb stack samples, all 8 in the same ci_translate_var frame for minutes)

process_c_import_with_defines -> ci_translate_var -> ci_try_eval_var_init_for_type
 -> ci_var_init_expr_for_type -> ci_var_init_expr_from_decl_source_for_type
 -> ci_translate_c_initializer_for_cursor_type -> ci_translate_c_expr
 -> ci_parse_cond_expr ... ci_parse_rel_expr x7 ... ci_parse_add_expr x7 ... ci_parse_mul_expr x22
 leaves: ci_is_c_type_name -> ci_str_contains(g_macro_type_names, ...)  (g_macro_type_names is 192,408 bytes)
         ci_trim / with_str_slice_ref -> alloc_str -> rt_memcpy

The string being parsed as the "initializer" (read from the stack {ptr,len} in the ci_translate_c_expr frame) is 373,670 bytes of raw winnt.h source, starting 1200\r\n#pragma warning(push)\r\n#endif\r\n#pragma warning(disable:4035 4793)\r\n__inline ULONGLONG NTAPI Int64ShllMod32 (...) { __asm { ... } } — i.e. the text after the = of #if _MSC_VER >= 1200.

Three defects compose:

  1. cursor_spelling_text_from_cursor (src/compiler/ClangBridge.w, used by with_ci_cursor_spelling_text, reached from ci_var_initializer_text_from_cursor src/CImport.w:14553) returns a mixed macro-body + macro-argument range. For DEFINE_GUID(name, ...) (-> EXTERN_C const GUID name) the spelling start is the extern token in the macro body (winnt.h:767:28) and the end is name in the invocation (e.g. winnt.h:16387:14), both in winnt.h, so the same-file check passes and ~15,600 lines come back. source_range_preferred_text_offsets already rejects exactly this shape ("avoid mixed body+argument ranges because those span unrelated text"); the spelling-text accessor does not. The Windows TU has 254 such GUID vars (clang -ast-dump of bzlib.h: 938 VarDecls, 254 with the winnt.h:767:28 start).
  2. ci_extract_var_initializer_text (src/CImport.w:13956) takes the first depth-0 = whose neighbours are not =, so the = of >= / <= / != counts as an initializer.
  3. The string-splitting expression parser (ci_parse_*_expr) re-scans and slices the whole input at every level, and on a failed split falls through to the next level with the same string; each step also calls ci_is_c_type_name, a linear scan of g_macro_type_names. On garbage input the cost is super-polynomial in input length and every slice is a fresh allocation.

Repro on darwin (no Windows needed)

// gen.w — with run gen.w <filler>
use std.fs
use std.process
use std.string.StringBuilder
use std.string
let argv = args()
let filler = parse(argv.get(1))
var h = StringBuilder.new()
h.push_str("typedef struct _GUID { unsigned long d1; unsigned short d2; } GUID;\n")
h.push_str("#define EXTERN_C extern\n")
h.push_str("#if 1 >= 0\n#endif\n")
for i in 0..filler:
    h.push_str(f"#define M{i}(a) ((a) * {i} + 1) - ((a) << 2) * 3 / 4\n")
h.push_str("#define DEFINE_GUID(name, l, w) EXTERN_C const GUID name\n")
h.push_str("DEFINE_GUID(G0, 0x1, 0x1);\n")
write_file(f"src/h2_{filler}.h", h.to_str())
write_file(f"src/u2_{filler}.w", f"use c_import(\"h2_{filler}.h\")\nfn main: print(\"ok\")\n")

mkdir src; with run gen.w N; WITH_PROFILE=1 with check src/u2_N.w (installed v0.15.2.1-g478b26481), one GUID var:

filler lines frontend.c_import
100 0.39 s
200 1.41 s
400 9.70 s
800 82.1 s

Control: the same 400-line header without the #if 1 >= 0 line: 0.23 s.

Expected

c_import of bzlib.h / curl.h on Windows completes in seconds, the GUID vars import as extern let (they have no initializer), and the UAT passes under its unchanged 180 s timeout. Fixes belong at 1 (never read a mixed body+argument spelling range) and 2 (>=, <=, !=, == are not initializers); 3 needs a bound so no header text can make the translator super-polynomial. Also: the allocator's OOM rt_exit(99) prints nothing, which is why the UAT logs show empty stdout/stderr — it should say it ran out of memory.

Related: #1163 (quadratic macro value table; that site is fixed on main — ci_collect_object_macro_values now builds a HashMap — and the timeout remained). The emitted-name linear scan (#1386) is not on this path: is_name_emitted does not appear in any Windows sample.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions