Skip to content

Bug: string literals over 4095 bytes are silently corrupted at LLVM emission #557

Closed

Description

@ehartford

Problem

wl_build_global_string_ptr (src/compiler/LlvmBridge.w:853) routes string constant data through to_cstr, which copies into a fixed thread-local 4096-byte slot and truncates at 4095 (LlvmBridge.w:425). Any single string literal longer than 4095 bytes is silently corrupted: the emitted str keeps the correct length but the constant data past 4095 bytes is whatever lives in adjacent cstr slots.

Repro (current shipped compiler)

fn main:
    let s = "AAAA...5000 A's..."
    print(f"{s.len()}")            // 5000 — length is right
    if s.byte_at(4500) == 65: print("content ok")
    else: print("content CORRUPT")  // prints content CORRUPT

Embedded-null literals have the same shape of problem: LLVMBuildGlobalStringPtr takes a C string, so a literal containing \x00 truncates at the null.

Why it went unnoticed

src/InitTemplates.w (the only >4KB string payload in the tree) is generated pre-chunked into <4096-byte ++ pieces — the generator was implicitly working around this. Discovered while implementing #556, whose literal-chain fold merged those chunks back into one constant and produced a corrupted with init AGENTS.md (caught by cli-selfhost-project-tests).

Fix

Emit string constant data length-aware: LLVMConstStringInContext + a private constant global, bypassing to_cstr for the data (names still go through to_cstr; they are short). Blocks #556.

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Fixed in 1a42dbf. wl_build_global_string_ptr now emits string constant data via LLVMConstStringInContext with an explicit length and a private constant global — data never touches to_cstr's 4096-byte slot, and embedded nulls survive. Regression test: test/behavior/behav_str_large_literal.w (5000-byte single literal, content verified past the old truncation point). Verified: the previously-corrupted 26KB folded init template now emits byte-identical to docs/with_for_ai.md.

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