You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[codegen] array copy of [T; N>64] copies element-count bytes, truncating to N/sizeof(T) #1050
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:
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.
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.
[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:
Every row above reports first = -1, bad = 0, and the pre-cleanup memcpy length is 400 for [u32; 100].
A codegen audit asserts array OK_COPY memcpy length equals ABISizeOf(array_ty).
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.
Activity
ehartford commentedon Sep 16, 2026
Review: reproduced (root cause of #1049; same defect, direct form)
Verdict. Fully reproduced and correctly localized. A value copy of
[T; N]withN > 64andsizeof(T) > 1copies only the firstNbytes =floor(N / sizeof(T))elements, because the emittedllvm.memcpyreceives 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.
main@ 5809ac1 (2026-09-14)with v0.15.2.1built from 5809ac1 (staging worktree); a compiler built from the reported revision 450733e with the nightly-20260903 seed. Same resultsCopy repro (
var a = bafter per-elementb[i] = i), reporting first wrong index / count wrong:[u32; 64]> 64)[u32; 100][u16; 100][u64; 100][u8; 100][u32; 100]gives25 / 75on both compilers.Root-cause evidence. Pre-
run_mir_cleanup_passesIR (WITH_DUMP_MIR_CLEANUP_FN=main) for the[u32; 100]copy contains:[100 x i32]is 400 bytes; the length operand is100, the element count. Source:src/CodegenDispatch.w:5227:wl_get_array_lengthisLLVMGetArrayLength2(LlvmBridge.w:644) — element count — handed tollvm.memcpyas a byte length. The path is gated onwl_get_array_length(use_arr_ty) > 64at:5218, which is whyN <= 64is unaffected (those copies go through element-wise/SSA lowering), andu8is 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, passingwith check, producing stack garbage in the tail. Present on 450733e and older per #1049.Validator gap. Same as #1049: the truncated
memcpyis valid IR, so the module verifier is silent, and no codegen audit checks that an arrayOK_COPYmemcpy length equalsABISizeOf(array_ty).Related. #1049 is the repeat-init manifestation of this exact line; fixing
:5227fixes 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 atLlvmBridge.w:1220, already used for the struct-zero memset nearby). Acceptance criteria:first = -1,bad = 0, and the pre-cleanupmemcpylength is 400 for[u32; 100].test/behaviorcase copies[u32; 100],[u16; 100],[u64; 100], and a struct array> 64, asserting every element; shared with the [codegen] repeat-init [v; N>64] initializes only N/sizeof(T) elements; tail left uninitialized #1049 repeat-init cases.OK_COPYmemcpy length equalsABISizeOf(array_ty).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.Codegen: a value copy of `[T; N > 64]` copies the array's byte size, …
Codegen: a value copy of `[T; N > 64]` copies the array's byte size, …
Codegen: a value copy of `[T; N > 64]` copies the array's byte size, …
Codegen: a value copy of `[T; N > 64]` copies the array's byte size, …
Codegen: a value copy of `[T; N > 64]` copies the array's byte size, …