Unsafe array delete destroys uninitialized tail elements and crashes on zero-length arrays
perfloop-oss/cpp2rust · Extent mismatch
https://perfloop.ai/t/oss/case_emqarx06c9
Verdict
VERIFIED · settled 2026-12-05
What happened: The check fails before this change and passes with it.
Hypothesis
In the unsafe output model, translating C++ `delete[]` infers the array length from the allocator's usable bucket size (`malloc_usable_size`) instead of the actual element count allocated by `new T[n]`. Because system allocators round allocation sizes up to size classes and because Rust's zero-length `Box<[T]>` returns a non-null dangling alignment sentinel rather than a heap pointer, `delete[]` silently invokes destructors on uninitialized memory past the end of the array, passes a mismatched slice layout to `Box::from_raw`, and crashes with SIGSEGV on valid `new T[0]` / `delete[]` pairs. Real-world reach is common_config (labelled engineering prior: `--model=unsafe` is one of the two supported CLI output models in cpp2rust, and `new T[n]` / `delete[]` on small or non-bucket-aligned arrays of classes or zero-length dynamic buffers is routine in C++ codebases; production prevalence is not measured).
In cpp2rust/converter/converter.cpp, Converter::VisitCXXDeleteExpr (lines 4150-4174) emits `for __i in 0..libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void) / ::std::mem::size_of::<T>() { T::destructor(&mut *__p.add(__i)); }` for array `delete[]`, and Converter::EmitDeallocation (lines 4176-4195) reconstructs `Box::from_raw(slice::from_raw_parts_mut(__p, malloc_usable_size(__p) / size_of::<T>()))`. Meanwhile Converter::VisitCXXNewExpr (lines 4116-4136) allocates arrays with `Box::leak((0..n).map(|_| ...).collect::<Box<[T]>>()).as_mut_ptr()` without preserving `n`. This causes three distinct memory-safety defects: - Over-destruction and uninitialized reads: whenever `malloc_usable_size(p)` exceeds `n * sizeof(T)` (for example, glibc returns 24 bytes for any allocation up to 24 bytes), destructors run on uninitialized tail slots beyond index `n - 1` (and in forward order instead of C++ reverse order). - Immediate SIGSEGV on zero-length arrays: for `new T[0]` (even trivial `new int[0]`), `Box::leak` on an empty `Box<[T]>` returns `NonNull::dangling()` (e.g. `0x4`), which passes `if !__p.is_null()` and crashes inside `malloc_usable_size`. - Allocator layout mismatch: passing the rounded-up slice length to `Box::from_raw` invokes `GlobalAlloc::dealloc` with a larger `Layout` than was allocated, which is undefined behavior in Rust.
A bounded public-CLI differential check in unsafe mode over element sizes {1, 4, 8, 12, 16} and array lengths {1..8} (40 positive-length cases) plus `new int[0]` / `delete[]` observed: - Refcount control: 40/40 positive-length cases and `new int[0]` matched native C++ (0 mismatches). - Unsafe model: 22/40 positive-length cases ran the wrong number of destructors (`sz=1, n=1` ran 24 destructors instead of 1; `sz=4, n=1` ran 6 destructors instead of 1 and read uninitialized garbage `-717006341` in a 4-byte struct test), and `new int[0]; delete[] p;` terminated with SIGSEGV (exit `-11` vs native `0`).
The independent proof target is the attached differential check (`tests/property/unsafe_delete_array/check.py`, provisional layout), which replays `new S[1]; delete[] p;` first (native exit 1 vs unsafe Rust exit 6) and then runs all 46 bounded `(size, count)` and zero-length cases. Matching native C++ exit status and destructor counts across all cases confirms repair; any over-destruction, crash, or compilation failure refutes it.
Upstream and prior-finding overlap check: open pull requests #679 and #683 do not touch `VisitCXXDeleteExpr` or `EmitDeallocation`; prior findings searches for `VisitCXXDeleteExpr` and `delete` found no overlap.
Change to test: Store the exact allocated element count for unsafe-model array new allocations (for example in an allocation header or allocator metadata) instead of inferring array length from malloc_usable_size in Converter::VisitCXXDeleteExpr and Converter::EmitDeallocation, and destroy elements in reverse order while handling zero-length allocations safely without passing dangling sentinel pointers to malloc_usable_size.
Where it lives
perfloop-oss/cpp2rust · cpp2rust/converter/converter.cpp
Evidence
The check fails before this change and passes with it: `In cpp2rust --model=unsafe output, translating C++ new T[n] and delete[] p preserves exact allocated element counts, reverse-order destruction without touching uninitialized tail slots, safe handling of zero-length arrays across element sizes {1, 4, 8, 12, 16} with lengths {0..8}, new int[0]/delete[], empty structs with destructors, const-pointer delete[], 2D array delete[] and reverse member destruction for structs with user destructors and owning members (including early-return destructors), runtime-bound array new with {}, shorter initializer lists, and exact/padded string literals across all character kinds (char, wchar_t, char8_t, char16_t, char32_t), and std::unique_ptr<T[]> reset()/drop reverse-order C++ destruction including arbitrary-arity function pointers and user methods named destroy/destroy_impl, matching native C++ exit status and destructor counts/order.`
unsafe unique_ptr<T[]> fft and matmul · 45 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
fft_ms |
15.03 |
17.1 |
+14% (+2.102) |
+1.529 to +2.758 |
loss ≤ 0.3 × |baseline median| |
PASSED |
matmul_ms |
15.35 |
15.79 |
+0.368 |
−0.1233 to +1.184 |
loss ≤ 0.08 × |baseline median| |
PASSED |
Checks: 5 of 5 passed. Verification: no defect found.
Timeline
- 2026-11-26 · Case opened
- 2026-12-04 · PR opened