Skip to content

Windows: fix #1081 (with_str_from_cstr view double free), the spawn/one-liner defects, growable ownership tables, pcre2test harness link - #1084

Merged
ehartford merged 11 commits into
mainfrom
fix-1081-str-from-cstr
Sep 6, 2026
Merged

ehartford merged 11 commits into
mainfrom
fix-1081-str-from-cstr

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #1081. Every change verified by running on the native Windows CI box; evidence in each commit message.

Root cause of #1081 — with_str_from_cstr returned an owned-typed view of a caller-owned C buffer; win_list_append's path_text drop freed str_to_cstr's copy, then with_fs_list_files freed it again (invalid free: pointer is not an allocated payload start). Only Windows reached it because its walk appended a missing path (out/lib, absent during the .wo bundle build); unix walks return on failed stat. Latent on every platform for a file path (fixture). Trap history + backtraces of both frees in c44634d3.

Commits

  • c44634d3 rt: with_str_from_cstr copies — an owned str owns its bytes (#691 str flip: str buffers free on drop; Copy-with-str sites de-Copy (D28 ruling 1) #747-H)
  • 72839c53 rt/windows: a missing path lists nothing, like the unix walk
  • 05b5ef70 rt: the instruments that found it with no debugger (Windows panic backtrace via unwind tables + dbghelp, invalid-free forensics, 4M-slot ledger, zero-class knob)
  • 2888167c rt/windows: CreateProcessW can't resolve a relative forward-slash program path — every compiler-built binary was launched that way (with test exit -2, with run silent); argv[0] backslashed; fixture
  • 2bf50877 cli: one-liner binary carries .exe on Windows (relative extensionless name) — -e/-p/-n work natively for the first time
  • 266ce65e rt: ownership range tables grow (mmap, doubling) instead of silently disabling the invalid-free check past 8192 regions
  • 597878c3 + 53784475 rt/windows errno/rlimit shims + legacy_stdio_definitions.lib (Link.w separate per the rule): pcre2-wo-drift links and runs pcre2test on Windows
  • docs: native-Windows route in deep-debugging-tools.md; resolution, seed ladder and battery status in fix_windows.md

Verification on Windows (main + branch, built by the af7db8c hop compiler): full build green, :fixpoint green (three runs), pcre2 bundle builds (970,850-byte object), :test: 983/984 behavior files, pcre2-wo-drift green, nm targets green with NM set. Remaining red: behav_project_overflow_modes → #1082 / #1083 (build-driver, masked on unix).

Seed: the v0.15.1.8 Windows seed cannot evaluate main's build.w (comptime str indexing since 0173d08e); the proven ladder is v0.15.1.8 → af7db8c+fix → main, natively, no --emit-c. Publishing the resulting Windows seed and moving seed.lock off v0.15.1.8 is the follow-up.

with_str_from_cstr built the returned str with make_str, a view of the C
buffer it was handed. The type says owned, so every receiver drops it,
and the drop's ownership guard (rt_payload_start_is_owned) only makes
that a no-op while the viewed bytes are NOT a live allocator payload.
Hand it a live payload and the guard passes: the drop frees the caller's
buffer, and the caller frees it again.

That is the native-Windows crash building the pcre2 .wo bundle
(#1081): with_fs_list_files converts the path with str_to_cstr (an
rt_alloc'd copy), the Windows walk's not-a-directory branch appends the
path through win_list_append, whose `path_text =
with_str_from_cstr(path)` is a view of that copy; path_text's scope-exit
drop frees it, then cstr_free(cpath) frees it again:

  invalid free: pointer is not an allocated payload start

Evidence (docs/deep-debugging-tools.md route, stage1 built at af7db8c on
a native Windows box, ASLR off via editbin /DYNAMICBASE:NO):
- WITH_DEBUG_ALLOC_TRAP_FREE on the panicking address: alloc #60240
  origin=0 (str_to_cstr's rt_alloc) -> free #60240
  origin=drop#struct __drop_struct_16 (str glue) -> free #60241 untagged
  -> panic. A double free, 60320 allocs / 60321 frees of that block.
- Backtrace of free #60240: with_str_free_drop_origin <-
  win_list_files_walk (win_list_append inlined) <- rt_list_files <-
  with_fs_list_files <- link_stage_cleanup_owned_temp_archives_in.
- Backtrace of free #60241 (the panic): rt_free <- with_fs_list_files
  +0x85, the cstr_free(cpath) call. Forensics: header word is a slab
  address, i.e. a freelist link -- the block was already free.
- MIR: win_list_append `_3 = move with_str_from_cstr(path)` then
  `drop(_3) @ scope-exit`.
- The compiler reaches the branch because out/lib does not exist during
  the .wo bundle build (the pre-metadata phase of #1075); the unix walks
  return early when stat fails, so only Windows listed the missing path.
  A FILE path takes the same append line on every platform, which the
  fixture exercises.
- WITH_DEBUG_ALLOC on the fixture: DOUBLE FREE size=32
  first_drop=drop#struct __drop_struct_16 second_drop=<untagged>.

Precedent: #747 instance H (session_make_str) -- a `-> str` return
transfers ownership of exactly one allocation. Null stays {null, 0}
for the c_import `*u8 -> str` coercion that relies on it.

Verified on native Windows (stage1 at af7db8c + this runtime): the
fixture prints ok; `with build lib/std/re/bundle.w --emit-obj
--bundle-corpus std/re ...` exits 0 with a 970,850-byte object where it
panicked before; WITH_DEBUG_ALLOC reports leak count=0 on the fixture.
win_list_files_walk had one test, "not a directory", covering both a
file and a path that does not exist, and appended the path in either
case. The unix walks return `out` when lstat fails, so
list_files_text("missing") is "" there and "missing\n" on Windows.
That divergence is how the compiler reached the #1081 double free: its
temp-archive cleanup lists out/lib before the directory exists, and only
the Windows walk took the append branch.

Three cases now, in both Windows backends: missing lists nothing, a
file lists itself, a directory lists its tree. The fixture asserts the
missing case alongside the file case; it passes under the tree runner on
native Windows (stage1 at af7db8c carrying this runtime).
Every one of these was needed to name the exact line, and none existed.

- rt_backtrace_print: with_panic_core prints the call chain. On Windows
  x64 the unwind tables (.pdata/.xdata) let RtlCaptureStackBackTrace walk
  the stack with no frame-pointer chain, and dbghelp resolves each
  return address against the PDB the link wrote. So a
  WITH_DEBUG_ALLOC_TRAP_FREE_HIT stop, or an invalid-free panic, names
  its frames with no lldb/cdb attached -- this box has neither. Both
  #1081 frees were located this way. A no-op on the other backends,
  where lldb remains the site tool.
- Invalid-free forensics: the panic now names the slab or large range
  holding the header, the offset into it, and the header word (a size
  class = live; a slab address or 0 = freelist link of an already-freed
  block; -1 = freed under WITH_ALLOC_NO_REUSE). It is the memory read a
  debugger would be asked for first; for #1081 it said "freelist link",
  i.e. double free, before any trace ran.
- Range-table overflow is announced. Past RT_ALLOC_RANGE_CAP the
  ownership tables are incomplete and rt_payload_start_can_be_owned must
  pass every pointer. A compiler-sized run under WITH_ALLOC_NO_REUSE=1
  fills the slab table, and the #1081 double free -- which reproduces
  every time -- "vanished" under NO_REUSE for that reason alone, which
  the route in docs/deep-debugging-tools.md reads as "stale pointer or
  uninitialized stack". A tool that stays silent while it stands down
  is a bug; it now prints one line the moment it does.
- DBG_CAP 65536 -> 4M slots. The ledger truncated on every compiler
  run ("ledger full, tracking truncated"), voiding the double-free
  verdict where it was needed. A 302 MB reservation, touched as it
  fills.
- WITH_ALLOC_ZERO_CLASS=1 (diagnostic): zero a recycled small block to
  its full class size, not just the request. Separates "stale bytes in
  a recycled block's tail" from stack garbage in one run.

Refs #1081
deep-debugging-tools.md: a "Native Windows, no debugger" recipe -- the
runtime's panic backtrace and invalid-free forensics stand in for lldb;
editbin /DYNAMICBASE:NO makes heap addresses repeat; what moves an
address between a learn and a trap run (the environment block, and any
argument-derived path of a different length). Step 2 of the drop route
now says when a NO_REUSE "vanish" is the ownership table standing down
rather than a verdict, and the ledger paragraph says a truncated ledger
voids the run.

fix_windows.md: a resolution section ahead of the orientation -- the
proven root cause, what landed, and what still keeps the Windows lanes
red (no published Windows seed can evaluate main's build.w since the
comptime str-index sweep; D40's answer is a cross-built Windows seed).

Refs #1081
…esolve a relative forward-slash program path (#1081)

On native Windows `with test` failed every fixture at the run stage with
`exit code -2` and no child output, and `with run` exited silently --
under the published seed AND under the tree's own compiler. The kept
test binary ran fine by hand, which was the tell: the spawner, not the
program.

-2 is win_neg_error() = -ERROR_FILE_NOT_FOUND from CreateProcessW. It
resolves the program from the command line, and a RELATIVE path spelled
with forward slashes does not resolve there. A std.process.run probe of
the same child by three spellings, one run:

    relative fwd  "out/C/src/with/out/tmp/hello.exe"  rc=-2
    relative back "out\C\src\with\out\tmp\hello.exe"  rc=0 (child ran)
    absolute      "C:/src/with/out/C/.../hello.exe"   rc=0 (child ran)

Every compiler-built binary is launched by the first spelling: the
runner's out/test/<dir>/<stem>.test.<pid>.<nanos>, `with run`'s
out/<sanitized source dir>/<stem>.exe, `-e`'s out/tmp/with-cli-one-liner-*.
(The one earlier success -- stage1 `run` in a worktree -- had WITH_OUT_DIR
set to an absolute root, so its path was absolute.)

win_build_command_line now maps '/' to '\' while copying argv[0], at read
time, so the existing quote/backslash escaping stays uniform; other
arguments are untouched. Both Windows backends. A program that parses its
own argv[0] gets the same relief (cmd.exe read a forward-slash one as its
own switches).

Fixture: behav_process_run_relative_path re-launches its own binary once
by args()[0] -- the exact path the runner used. Under main's release
compiler on this box: `with run out/tmp/hello.w` runs the child, and
behav_fs_list_files_file_path, behav_process_run_relative_path and
assoc_type_basic all pass under `with test`, where all three failed
before.

Docs: the earlier notes attributed the -2 to the seed alone; corrected to
the real cause, and fix_windows.md records the native seed ladder
(v0.15.1.8 -> af7db8c+fix -> main, both stages `:fixpoint` green).
`with -e` / `-p` / `-n` exited silently on native Windows. Their binary,
out/tmp/with-cli-one-liner-<pid>-<nanos>, was the only extensionless
RELATIVE program the compiler launches, and CreateProcessW appends
".exe" to an extensionless command-line name and then finds nothing.
The std.process.run probe that isolated it:

    "out/tmp/child_rel.exe"    rc=0 (child ran)
    "out/tmp/child_rel_noext"  rc=-2  (ERROR_FILE_NOT_FOUND)

(an ABSOLUTE extensionless path runs, which is what first sent this
diagnosis the wrong way). The path now takes ".exe" on Windows.

Verified under main's release compiler on this box after the rebuild:
`with -e 'print_i32(6 * 7)'` prints 42, `-p` and `-n` transform stdin,
all exit 0 -- the one-liner surface CLAUDE.md prescribes for tooling
works natively on Windows for the first time.
ehartford added a commit that referenced this pull request Sep 6, 2026
…1081)

597878c added getrlimit and setrlimit to codegen_is_runtime_abi_symbol
in src/FnAbi.w, one of the two ABI-defining sources the battery hashes
(D38; abi-hash-check failed on linux x86_64 for PR #1084). The
convention is unchanged -- two runtime backend exports keep their C
names, as __error and gethostname already do -- so WITH_ABI_VERSION
stays; the key moves and every .wo bundle rebuilds once.
@ehartford
ehartford force-pushed the fix-1081-str-from-cstr branch from 6b6e127 to 4e6666d Compare September 6, 2026 16:19
The slab-region and large-allocation tables that back
rt_payload_start_is_owned were fixed [8192]i64 arrays. Past the cap a
table was marked "incomplete" and rt_payload_start_can_be_owned passed
every pointer -- the invalid-free check silently off for the rest of the
run. A compiler-sized run under WITH_ALLOC_NO_REUSE=1 gets there (no
recycling, one slab per 64 KB), which is exactly how the #1081 double free
"vanished" under the one diagnostic the route reaches for second. The
previous commit made the stand-down loud; a table whose size is a policy
should not exist at all.

Both tables are now (starts, ends) pairs in rt_mmap'd memory that double
when full: map, copy, unmap, all under the allocator lock every record,
forget and lookup already holds, so no new synchronization. The sorted
insert and binary searches are unchanged; the accessor functions are the
only change to the lookup. RT_ALLOC_RANGE_CAP is the initial capacity.
The *_ranges_complete flags and the permissive clause are gone.

Verified on native Windows with main's release compiler: the pcre2 bundle
under WITH_ALLOC_NO_REUSE=1 builds (exit 0, 970,850-byte object) with no
stand-down line, and the fixture under NO_REUSE + WITH_DEBUG_ALLOC reports
leak count=0.
…n Windows)

The pcre2test harness (lib/std/re/pcre2test.w, through std.libc) reads
errno as *__error() and sizes its stack with getrlimit/setrlimit. The
Windows backends had neither, so `pcre2-wo-drift` -- which links the
harness against the stored bundle and runs it -- failed to link on native
Windows with undefined __error, getrlimit, setrlimit (plus vfprintf and
sprintf, which are the link archive's job; next commit).

__error returns UCRT's _errno(). There is no rlimit on Windows -- the main
thread's stack is fixed at link time -- so RLIMIT_STACK reads as unlimited
(rlim_cur = rlim_max = Darwin's RLIM_INFINITY) and a set is accepted as a
no-op; any other resource is refused. Layout is std.libc's rlimit.

The two names join codegen_is_runtime_abi_symbol (src/FnAbi.w), the
allowlist that keeps a runtime backend's C-spelled exports unmangled:
without it they came out as __with_mod_<hash>__getrlimit and the harness
still failed to link (llvm-nm on out/lib/rt_windows_x86_64.o showed it).

Verified: `with build :pcre2-wo-drift` on this box -- "out/wo-drift/pcre2/
pcre2.o is byte-identical to out/wo/pcre2.o; pcre2test links the bundle
and runs" -- where it failed on those five symbols before.

Refs #1081
UCRT exports the printf/scanf family (sprintf, vfprintf, ...) only
through this archive -- the functions are inline in the headers since
VS 2015 -- so C code migrated from Darwin, which calls them by name
(pcre2test.w), fails to link with undefined sprintf / vfprintf. Part of
the pcre2-wo-drift fix; the rt shims for errno and rlimit landed
separately, as the Link.w-and-runtime rule requires.
… and the two build-driver defects left to file
…1081)

597878c added getrlimit and setrlimit to codegen_is_runtime_abi_symbol
in src/FnAbi.w, one of the two ABI-defining sources the battery hashes
(D38; abi-hash-check failed on linux x86_64 for PR #1084). The
convention is unchanged -- two runtime backend exports keep their C
names, as __error and gethostname already do -- so WITH_ABI_VERSION
stays; the key moves and every .wo bundle rebuilds once.
@ehartford
ehartford merged commit d5149f4 into main Sep 6, 2026
3 of 5 checks passed
@ehartford
ehartford deleted the fix-1081-str-from-cstr branch September 24, 2026 16:54
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.

Windows: compiler panics 'invalid free' building the pcre2 .wo bundle (native-Windows only; cross-from-macOS is clean)

1 participant