Repository navigation
Store exact element count for unsafe-model array new[] and delete[] - #703
Open
perfloop-agent wants to merge 1 commit into
Open
perfloop-agent wants to merge 1 commit into
perfloop-agent wants to merge 1 commit into
Conversation
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
lucic71
reviewed
Dec 4, 2026
| targs[0].getAsType()->isArrayType(); | ||
| }; | ||
|
|
||
| auto get_unique_ptr_array_ptr_arg_idx = [&]() -> std::optional<size_t> { |
Contributor
There was a problem hiding this comment.
This is not ok. It checks model != kUnsafe and hardcodes the name of the unique_ptr::reset method
We should stop embedding rule-specific information in the converter
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In
--model=unsafe, translating C++delete[]infers the array length frommalloc_usable_size(p) / sizeof(T)instead of the allocated element count. Because allocators round up to size classes andBox::leakon an emptyBox<[T]>returns a non-null dangling sentinel,delete[]runs destructors on uninitialized tail slots, passes a mismatched sliceLayouttoBox::from_raw, and segfaults onnew T[0]/delete[]. For a 4-bytestruct Swith a destructor,new S[1]; delete[] p;runs 6 destructors instead of 1 on glibc, andnew int[0]; delete[] p;crashes withSIGSEGV.libcc2rs/src/alloc.rsaddsalloc_array_unsafe,alloc_array_from_unsafe,array_len_unsafe, anddealloc_array_unsafeto store the exact element count at the allocation base in an alignment-padded header preceding the payload pointer (including zero-length allocations, sonew T[0]returns a valid distinct non-null pointer) and free that exact layout after droppinglenelements in reverse order.Converteremits these helpers for unsafe-model raw arraynew[]/delete[]while keepingstd::unique_ptr<T[]>construction andreset()on leakedBox<[T]>.This fixes paired
new[]/delete[]allocations produced by the unsafe model; callingdelete[]on an unheaded pointer (such as frommallocor C FFI) remains unsupported.Generated output changes only at unsafe-model raw array
new[]/delete[]sites (25 regeneratedout/unsafe/*.rsexpectations; 0 existingout/refcount/*.rschanges). The newtests/unit/delete_array_exact_len.cppfails on base in unsafe output and passes in both models with this change; scoped Release/Debug lit runs (51/51 each),libcc2rs(86/86) andlibcc2rs-macros(24/24) tests, formatting, and clippy pass.cc @nunoplopes @lucic71
Implementation and testing details
libcc2rs/src/alloc.rs): allocates a single block of sizemax(size_of::<usize>(), align_of::<T>()) + len * size_of::<T>()with alignmentmax(align_of::<usize>(), align_of::<T>()), writeslenatbase.cast::<usize>(), and returnsbase.add(header_size).array_len_unsafeanddealloc_array_unsaferecover the header by subtracting the full header size;ArrayInitGuardandDeallocGuardhandle panic unwinding. Includes 6 new unit tests covering reverse drop order, null deallocation, distinct non-null zero-length pointers, panic cleanup during initialization,alloc_array_from_unsafe, over-aligned types, and ZSTDroplayouts.cpp2rust/converter/converter.h,cpp2rust/converter/converter.cpp): updatesVisitCXXNewExpr,VisitCXXDeleteExpr, andEmitDeallocationto emit the new helpers in unsafe mode and propagatesarray_new_as_box_across value-forwarding expressions intostd::unique_ptr<T[]>constructors andreset().tests/unit/delete_array_exact_len.cppandtests/unit/out/{refcount,unsafe}/delete_array_exact_len.rs): covers small non-bucket-aligned structs, zero-length arrays, null/const/2Ddelete[], RustDropfields, andstd::unique_ptr<T[]>forwarding forms (emplace_back,std::pair,std::move,std::forward, reference casts, compound literals, conditionals, and nested rawnew[]). Regenerated 25 existingout/unsafe/*.rsexpectations (15 intests/unit, 9 intests/ub, 1 intests/std).cargo fmt --check,cargo clippy --all-targets -- -D warnings,clang-format-23, and full Release lit (670/704 passed, 32 expected failures;unit/socket_ip_addrlen.candunit/socket_recvfrom_name.cunvalidated here and left to CI).Case and evidence
Made by Perfloop. Human sponsor: Tomás Senart.