Skip to content

[codegen] array copy of [T; N>64] copies element-count bytes, truncating to N/sizeof(T) #1050

Closed

Description

@eas4ai

Summary

Copying a large array (var a = b where b: [T; N], N > 64) copies only the first N / sizeof(T) elements. The emitted llvm.memcpy receives the element count as its byte length.

Repro

use std.builtins.print_i32
fn main -> i32:
    var b: [u32; 100] = [0u32; 100]
    var i = 0
    while i < 100:
        b[i] = i as u32
        i = i + 1
    var a = b
    // scan a for a[j] != j: prints first=25 bad=75, expect -1 / 0
    0

(The source array is built with explicit per-element stores, which are exact, so the loss is in the copy itself. Stable across runs.)

Localization (verified by execution)

Pre-cleanup IR (WITH_DUMP_MIR_CLEANUP_FN='main') for a [u32; 100] copy contains:

llvm.memcpy.p0.p0.i64(ptr %9, ptr %10, i64 100, i1 false)

The array is 400 bytes; the length operand is 100 — the element count. Source: the RK_USE-of-large-array store path in src/CodegenDispatch.w (~mc_len), which passes wl_get_array_length(use_arr_ty) directly as the memcpy byte length:

let mc_len = wl_const_int(wl_i64_type(self.context), wl_get_array_length(use_arr_ty), 0)

Same elem-count-as-bytes shape as the u8 fill memset path (masked there because sizeof(u8) == 1); here it is live-broken for every T with sizeof(T) > 1.

Notes for the fix (not applied; audit is read-only)

mc_len should be array_length * ABI size of the element type (cf. wl_abi_size_of, already used for the struct-zero memset at the same site). Sibling C-backend lowering should be checked for the same shape.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced (root cause of #1049; same defect, direct form)

    Verdict. Fully reproduced and correctly localized. A value copy of [T; N] with N > 64 and sizeof(T) > 1 copies only the first N bytes = floor(N / sizeof(T)) elements, because the emitted llvm.memcpy receives the element count as its byte length. The cited source line is exactly right. This is the same defect as #1049 (repeat-init tail truncation): there the fill result is carried into the local by this same array-copy path, so both issues have one root cause and one fix. I confirmed that independently in my #1049 review before seeing this issue.

    Tested.

    Source main @ 5809ac1 (2026-09-14)
    Compilers with v0.15.2.1 built from 5809ac1 (staging worktree); a compiler built from the reported revision 450733e with the nightly-20260903 seed. Same results
    Platform macOS Darwin 25.6.0 arm64

    Copy repro (var a = b after per-element b[i] = i), reporting first wrong index / count wrong:

    case first bad copied elems note
    [u32; 64] -1 0 64 not truncated (path gated > 64)
    [u32; 100] 25 75 25 100 bytes / 4
    [u16; 100] 50 50 50 100 bytes / 2
    [u64; 100] 12 88 12 100 bytes / 8
    [u8; 100] -1 0 100 100 bytes / 1 (accidentally whole)

    [u32; 100] gives 25 / 75 on both compilers.

    Root-cause evidence. Pre-run_mir_cleanup_passes IR (WITH_DUMP_MIR_CLEANUP_FN=main) for the [u32; 100] copy contains:

    call void @llvm.memcpy.p0.p0.i64(ptr %8, ptr %22, i64 100, i1 false)
    

    [100 x i32] is 400 bytes; the length operand is 100, the element count. Source: src/CodegenDispatch.w:5227:

    let mc_len = wl_const_int(wl_i64_type(self.context), wl_get_array_length(use_arr_ty), 0)
    

    wl_get_array_length is LLVMGetArrayLength2 (LlvmBridge.w:644) — element count — handed to llvm.memcpy as a byte length. The path is gated on wl_get_array_length(use_arr_ty) > 64 at :5218, which is why N <= 64 is unaffected (those copies go through element-wise/SSA lowering), and u8 is accidentally correct because element count equals byte count. The report's analysis is accurate on every point.

    Severity. High: a silent miscompile of an ordinary value copy of any [T; N>64] with a multi-byte element, passing with check, producing stack garbage in the tail. Present on 450733e and older per #1049.

    Validator gap. Same as #1049: the truncated memcpy is valid IR, so the module verifier is silent, and no codegen audit checks that an array OK_COPY memcpy length equals ABISizeOf(array_ty).

    Related. #1049 is the repeat-init manifestation of this exact line; fixing :5227 fixes both. No open PR addresses it.

    Recommendation — champion. As the report says: mc_len = wl_abi_size_of(wl_get_module_data_layout(self.llmod), use_arr_ty) (helper at LlvmBridge.w:1220, already used for the struct-zero memset nearby). Acceptance criteria:

    Untested / to check per the report. The C backend (--emit-c) sibling copy lowering was not inspected here; the report flags it and it should be checked for the same element-count-as-bytes shape. Struct-element arrays and Linux/Windows not exercised.

  2. added 5 commits that reference this issue on Sep 28, 2026
    87f5833
    8ca2d75
    debc582
    43ec250
    51c9a73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions