Skip to content

kernel/tlsf: Add double-free detection in tlsf_freevec#777

Open
metaneutrons wants to merge 2 commits into
aros-development-team:masterfrom
metaneutrons:fix/tlsf-64bit
Open

kernel/tlsf: Add double-free detection in tlsf_freevec#777
metaneutrons wants to merge 2 commits into
aros-development-team:masterfrom
metaneutrons:fix/tlsf-64bit

Conversation

@metaneutrons
Copy link
Copy Markdown
Contributor

  • Check after ObtainSemaphore (SMP TOCTOU safe)
  • !! for BOOL normalization (16-bit safe)
  • Local bool avoids duplicated sem-protection condition
  • nbug with [Kernel:TLSF] prefix, %lu format
  • Allman-style braces matching file convention

Prevents TLSF free-list corruption from double-frees.

Check if a block is already marked free before freeing it again.
Double-frees corrupt the TLSF free-list and cause unpredictable
crashes later.

SMP-safe: check is after ObtainSemaphore (TOCTOU prevention).
Uses local BOOL for semaphore-protection check to avoid
duplicating the condition. Format string uses %lu with
(unsigned long) cast for portability on 64-bit.
Copilot AI review requested due to automatic review settings May 7, 2026 20:44
Copy link
Copy Markdown

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

Adds a guard in the TLSF allocator’s tlsf_freevec() path to detect and short-circuit double-frees (after taking the optional pool semaphore), preventing free-list corruption in the kernel memory pool implementation.

Changes:

  • Cache the pool’s semaphore-protection flag (MEMF_SEM_PROTECTED) in a local BOOL to avoid repeated checks.
  • Add an early-return double-free check (FREE_BLOCK(fb)) after semaphore acquisition, with a debug warning.
  • Use the cached semaphore flag for consistent release on all exit paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rom/kernel/tlsf.c Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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