Skip to content

Fix alignment error - #126

Merged
alessandropellegrini merged 1 commit into
developfrom
alignment
Jun 12, 2026
Merged

Fix alignment error#126
alessandropellegrini merged 1 commit into
developfrom
alignment

Conversation

@alessandropellegrini

Copy link
Copy Markdown
Member

This is a quick&dirty fix for an alignment error in the buddy system. Fixes #105.

Per the C standard, malloc() returns memory aligned to max_align_t, typically 8 bytes on some LP64 platforms (e.g., many Linux/ARM64 configurations) and 16 bytes on x86-64. The C standard does not guarantee
malloc() satisfies over-aligned types (with
alignment greater than max_align_t).

Possible problems in the previous implementation:

  • Undefined behavior: Accessing a misaligned alignas(16) member is UB per C11 §6.2.8.
  • SIMD crashes: If the compiler auto-vectorizes operations on longest[] or base_mem[] and emits SSE aligned loads/stores (movaps, ld1), a misaligned buffer will cause a hardware fault.
  • Checkpoint corruption: The static_assert in buddy.h enforces contiguity between longest and base_mem. Misalignment of the struct could interact poorly with memcpy-based checkpoint/restore paths (checkpoint.c) if the compiler assumes alignment it doesn't actually have. I wasn't actually able to trigger this latter condition in any environment, but it should be nevertheless possible from my understanding.

On x86-64, malloc() typically returns 16-byte-aligned memory, so this bug is latent on the most common targets. It becomes active on platforms where max_align_t alignment is less than 16.
Since we pretend to be architecture-independent, I classify this as a severe bug.

This is a quick&dirty fix for an alignment error in the
buddy system. Fixes #105.

Per the C standard, malloc() returns memory aligned to
max_align_t, typically 8 bytes on some LP64 platforms
(e.g., many Linux/ARM64 configurations) and 16 bytes
on x86-64. The C standard does not guarantee
malloc() satisfies over-aligned types (with
alignment greater than max_align_t).

Possible problems in the previous implementation:
- Undefined behavior: Accessing a misaligned alignas(16)
member is UB per C11 §6.2.8.
- SIMD crashes: If the compiler auto-vectorizes operations on longest[] or base_mem[] and emits SSE aligned loads/stores
(movaps, ld1), a misaligned buffer will cause a hardware fault.
- Checkpoint corruption: The static_assert in buddy.h enforces
contiguity between longest and base_mem. Misalignment of the
struct could interact poorly with memcpy-based checkpoint/restore
paths (checkpoint.c) if the compiler assumes alignment it
doesn't actually have. I wasn't actually able to trigger this latter
condition in any environment, but it should be nevertheless
possible from my understanding.

On x86-64, malloc() typically returns 16-byte-aligned
memory, so this bug is latent on the most common targets.
It becomes active on platforms where max_align_t alignment is less
than 16.
Since we pretend to be architecture-independent, I classify this
as a severe bug.

Signed-off-by: Alessandro Pellegrini <a.pellegrini@ing.uniroma2.it>
@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.32%. Comparing base (15c55e2) to head (cdd3b5e).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #126      +/-   ##
===========================================
- Coverage    85.81%   85.32%   -0.49%     
===========================================
  Files           46       46              
  Lines         1600     1554      -46     
  Branches        39       39              
===========================================
- Hits          1373     1326      -47     
- Misses         211      212       +1     
  Partials        16       16              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Piccions Piccions left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Quick and dirty does the job here!

@alessandropellegrini
alessandropellegrini merged commit d6568f6 into develop Jun 12, 2026
17 of 20 checks passed
@alessandropellegrini
alessandropellegrini deleted the alignment branch June 12, 2026 04:01
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