Skip to content

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Aug 15, 2025

Tighten up safety of FixedSizeAllocator::new.

  1. Panic early on big-endian system. This should not be possible, but adding an assertion before the allocation happens makes absolutely sure the same panic can't happen in Allocator::from_raw_parts. If it did, the allocation would not be freed, which would be a huge memory leak.

  2. Wrap Allocator in ManuallyDrop as soon as it's created. If code later in the function panicked, Allocator would be dropped, which would be UB. ManuallyDrop prevents that happening, so a panic would instead cause a memory leak - still bad, but better than UB.

Neither of these scenarios should be possible, but all the code around FixedSizedAllocator is pretty labyrinthine (bad design on my part). So it's better to code defensively in case of a bug elsewhere. Both changes have 0 performance impact.

@github-actions github-actions bot added the C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior label Aug 15, 2025
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@overlookmotel overlookmotel force-pushed the 08-15-refactor_allocator_harden_safety_of_fixedsizeallocator_new_ branch from 27cd512 to aba0758 Compare August 15, 2025 16:05
@codspeed-hq
Copy link

codspeed-hq bot commented Aug 15, 2025

CodSpeed Instrumentation Performance Report

Merging #13124 will not alter performance

Comparing 08-15-refactor_allocator_harden_safety_of_fixedsizeallocator_new_ (18ad3c0) with main (6c5b8be)1

Summary

✅ 34 untouched benchmarks

Footnotes

  1. No successful run was found on main (18ad3c0) during the generation of this report, so 6c5b8be was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@overlookmotel overlookmotel marked this pull request as ready for review August 15, 2025 16:12
Copilot AI review requested due to automatic review settings August 15, 2025 16:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR hardens the safety of FixedSizeAllocator::new by adding defensive programming measures to prevent undefined behavior and memory leaks in edge cases.

  • Adds early panic on big-endian systems before allocation to prevent memory leaks
  • Wraps Allocator in ManuallyDrop immediately after creation to prevent UB if later code panics
  • Moves the ManuallyDrop::new call earlier in the function for better safety

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@overlookmotel
Copy link
Member Author

I don't think anyone else has foggiest idea about this code. Even I only barely do, and I wrote it! So merging without review.

@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Aug 15, 2025
Copy link
Member Author

overlookmotel commented Aug 15, 2025

Merge activity

Tighten up safety of `FixedSizeAllocator::new`.

1. Panic early on big-endian system. This should not be possible, but adding an assertion before the allocation happens makes absolutely sure the same panic can't happen in `Allocator::from_raw_parts`. If it did, the allocation would not be freed, which would be a huge memory leak.

2. Wrap `Allocator` in `ManuallyDrop` as soon as it's created. If code later in the function panicked, `Allocator` would be dropped, which would be UB. `ManuallyDrop` prevents that happening, so a panic would instead cause a memory leak - still bad, but better than UB.

Neither of these scenarios should be possible, but all the code around `FixedSizedAllocator` is pretty labyrinthine (bad design on my part). So it's better to code defensively in case of a bug elsewhere. Both changes have 0 performance impact.
@graphite-app graphite-app bot force-pushed the 08-15-refactor_allocator_harden_safety_of_fixedsizeallocator_new_ branch from aba0758 to 18ad3c0 Compare August 15, 2025 16:16
@graphite-app graphite-app bot merged commit 18ad3c0 into main Aug 15, 2025
26 checks passed
@graphite-app graphite-app bot deleted the 08-15-refactor_allocator_harden_safety_of_fixedsizeallocator_new_ branch August 15, 2025 16:22
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants