Skip to content

Free over-aligned array new storage correctly on exception (non-IA-64) [GH #552] - #565

Open
lexx42xxel wants to merge 2 commits into
edgcpp:mainfrom
lexx42xxel:fix/noia64-aligned-array-new-eh-prefix
Open

lexx42xxel wants to merge 2 commits into
edgcpp:mainfrom
lexx42xxel:fix/noia64-aligned-array-new-eh-prefix

Conversation

@lexx42xxel

@lexx42xxel lexx42xxel commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes the problem reported in triage discussion #552.

Problem and fix

With the non-IA-64 ABI, new[] of an over-aligned type uses the alignment
as the size of the prefix, but the cleanup for a throwing brace or list
initialization freed p - __array_new_prefix_size: a bad free and a leak.
Pass the actual prefix size to the cleanup.

Alternatives considered

Compute the prefix inside the runtime cleanup. Rejected: the runtime does not know the alignment; passing the prefix size the front end already used keeps allocation and deallocation consistent.

Tests

New: tests/tests/regressions/array-new-aligned-throw-delete.sft.cpp.
Verified failing on main and passing with the fix (IA-64 and non-IA-64 builds); reproducer in the discussion.
AddressSanitizer: the discussion's reproducer (ASan variant) reports bad-free on main; no report with the fix or with g++.

Checklist

  • Open issue: not yet; reported in triage discussion Non-IA-64 ABI: wrong pointer freed when initialization of an over-aligned array new throws #552, per CONTRIBUTING.
    Commits are tagged [GH #552] with the discussion number; happy to retag once an issue is opened.
  • Commits are tagged [GH #552].
  • Design and alternatives are described above.
  • No measurable compile-time/memory impact expected (localized lowering change); see the bench job.
  • Changes are localized (src/lower_init.c).
  • EDG style; comments updated.
  • Changes entry: src/pending_changes/gh-552-noia64-aligned-array-new-eh.txt.
  • Tests fail before and pass after: array-new-aligned-throw-delete.
  • edg-docker-test --posture=extensive (edg_x86_64, edg_x86_64_cp, edg_x86_32, edg_x86_32_cp): no regressions; recordings updated with -W.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed cleanup after exceptions during initialization of over-aligned arrays created with new. Storage is now released using the correct allocation pointer, including for brace- and list-initialized arrays.
    • Added regression coverage for throwing and successful initialization across single- and multidimensional arrays, including class-specific allocation and deallocation. Tests verify storage is released exactly once and, when sized deletion is used, with the allocated size.

…) [GH edgcpp#552]

With the non-IA-64 ABI, new[] of an over-aligned type uses the alignment
as the size of the prefix, but the cleanup for a throwing brace or list
initialization freed p - __array_new_prefix_size: a bad free and a leak.
Pass the actual prefix size to the cleanup.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lexx42xxel
lexx42xxel requested a review from a team November 20, 2026 07:47
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Array allocation cleanup now receives the alignment-derived prefix size when available. A regression test checks deallocation after throwing and non-throwing construction for aligned arrays with different initialization forms and dimensions.

Changes: Aligned array cleanup

Layer / File(s) Summary
Preserve the allocation prefix size
src/lower_init.c
The aligned array allocation path saves and passes its prefix size to exception cleanup. Cleanup uses that size when provided and otherwise derives it from the element type and allocation routine. Scalar allocation passes NULL.
Exercise aligned array cleanup
tests/tests/regressions/array-new-aligned-throw-delete.sft.cpp, tests/tests/regressions/.array-new-aligned-throw-delete.rto/*, src/pending_changes/gh-552-noia64-aligned-array-new-eh.txt
The regression test covers global and class-specific aligned allocation, one- and two-dimensional arrays, and brace and list initialization. It checks deallocation count, pointer, and sized-delete size for throwing and non-throwing construction. The pending-change note describes the reported case.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • edgcpp/compiler#564: Changes the size argument passed to sized deallocation after an array constructor throws. This PR changes the prefix-size argument in the same cleanup path.

Suggested reviewers: daveedvdv-nvidia, darkarc

Merge Risk: Minimal · up to aa88a

This fixes a bad free and leak when constructing an over-aligned array with new[] throws during brace or list initialization under the non-IA-64 ABI. A regression test covers the fix. No outstanding merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: over-aligned array new storage is now freed correctly when initialization throws on non-IA-64 ABI. The raw summary confirms the lower_init.c change passes the actual p…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: over-aligned array new storage is now freed correctly when initialization throws on non-IA-64 ABI. The raw summary confirms the lower_init.c change passes the actual prefix size to the cleanup routine, and the regression test and pending-change note match. The title references the issue number GH #552, which matches the linked triage discussion. The title is 80 characters, a single sentence, and specific enough for a teammate scanning history.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 54f5f3d and aa88a44.

Files selected for processing: 1

  • src/lower_init.c
Files skipped from review as they are similar to previous changes: 1

  • src/lower_init.c

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 54f5f3d969e7:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 54f5f3d969e7:

CI success.

Following review feedback on a related pull request, do not start the
argument list of the make_reusable_copy call on a new line; break the
assignment after the "=" instead, as elsewhere in lower_init.c.  No
functional change.
@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for aa88a44d459c:

CI success.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants