Skip to content

va_list is modeled as a pointer on every target: a variadic With definition corrupts its stack on linux x86_64 (and linux aarch64) — zlib's gzprintf crashes the drift harness #1104

Closed

Description

@ehartford

Found by the zlib bundle's drift harness (#1103): linux x86_64's zlib-wo-drift runs zlib's own example against the bundle and dies with exit 139 in test_gzio (gzprintf → gzvprintf → vsnprintf(va)); macOS and Windows run it fine, linux aarch64 runs no battery yet.

Mechanism

A migrated variadic definition (#174's scheme) declares var __local_va: *mut i8, calls with_va_start(&raw mut __local_va), and passes __local_va to the C consumer. Codegen lowers with_va_start to llvm.va_start.p0 on that slot. The IR is identical on every target — this is with ir va.w --target=linux_x86_64 for a 10-line repro:

define internal i32 @fmt1(ptr %0, ptr %1, ...) {
  %2 = alloca ptr, align 8
  call void @llvm.va_start.p0(ptr %2)
  %3 = load ptr, ptr %2, align 8
  %4 = call i32 @vsnprintf(ptr %0, i64 64, ptr %1, ptr %3)
  • Darwin arm64 and Windows x64: va_list is char *; va_start writes 8 bytes and vsnprintf takes the pointer by value — correct.
  • linux x86_64 (SysV): va_list is __va_list_tag[1], 24 bytes; va_start writes 24 bytes into an 8-byte alloca (stack corruption), and vsnprintf expects a pointer to the tag (array decay) but receives its first 8 bytes (gp_offset/fp_offset) as the pointer.
  • linux aarch64 (AAPCS64): va_list is a 32-byte struct __va_list passed by value (a composite > 16 bytes goes by reference to a copy); same corruption.

The migrator's aggregate-va_list awareness (ci_canonical_is_aggregate_va_list) only sees the migration host's headers, so a corpus migrated on macOS carries the pointer shape everywhere. Variadic calls and externs are fine (LLVM's variadic call convention handles them); only definitions that read their varargs are affected. pcre2 has none; zlib's gz layer has gzprintf/gzvprintf.

Fix (ABI batch, alone, with move/drop audits)

A compiler-known VaList type with per-target layout (8 / 24 / 32 bytes, align 8) and a single PassMode rule in compute_fn_abi for VaList-typed parameters — pointer to the caller's storage on SysV x86_64 (C's array decay: the callee consumes the caller's list), by-value pointer on Darwin/Windows, by reference to a copy on AAPCS64 — so with_va_start/with_va_end/with_va_copy operate on real storage and vsnprintf(..., va) receives what the platform's C expects. The migrator emits VaList for C va_list in locals, parameters and extern declarations instead of the host's *mut i8 / [24]u8; zlib is re-migrated. A behavior test defines a variadic function that formats through vsnprintf and runs on every lane (the linux x86_64 lane is the one that catches this class).

Until it lands, #1103's linux x86_64 lane is red on the harness and every user of std.zl.gzwrite.gzprintf on Linux crashes.

Activity

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

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