Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions runtime/executor/platform_memory_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cinttypes>
#include <cstdint>

#include <c10/util/safe_numerics.h>
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

<c10/util/safe_numerics.h> is already included by executorch/runtime/core/memory_allocator.h, so this direct include is redundant here. Consider removing it to keep header dependencies minimal and reduce rebuild churn.

Suggested change
#include <c10/util/safe_numerics.h>

Copilot uses AI. Check for mistakes.
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/platform/log.h>
#include <executorch/runtime/platform/platform.h>
Expand Down Expand Up @@ -46,8 +47,19 @@ class PlatformMemoryAllocator final : public MemoryAllocator {
return nullptr;
}

// Allocate enough memory for the node, the data and the alignment bump.
size_t alloc_size = sizeof(AllocationNode) + size + alignment;
// Check for overflow before computing total allocation size.
// Then allocate enough memory for node, data and the alignment bump.
size_t alloc_size = 0;
if (c10::add_overflows(sizeof(AllocationNode), size, &alloc_size) ||
c10::add_overflows(alloc_size, alignment, &alloc_size)) {
Comment on lines +51 to +54
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

The allocation “alignment bump” only needs to reserve up to alignment - 1 bytes. Using + alignment slightly over-allocates and can also make the overflow check reject an allocation that would otherwise fit by 1 byte. Consider adding alignment - 1 (after validating alignment > 0, which isPowerOf2 already implies) instead of alignment.

Suggested change
// Then allocate enough memory for node, data and the alignment bump.
size_t alloc_size = 0;
if (c10::add_overflows(sizeof(AllocationNode), size, &alloc_size) ||
c10::add_overflows(alloc_size, alignment, &alloc_size)) {
// Then allocate enough memory for node, data and the maximum alignment
// bump, which is at most alignment - 1 bytes.
size_t alloc_size = 0;
if (c10::add_overflows(sizeof(AllocationNode), size, &alloc_size) ||
c10::add_overflows(alloc_size, alignment - 1, &alloc_size)) {

Copilot uses AI. Check for mistakes.
ET_LOG(
Error,
"Allocation size overflow: size %zu, alignment %zu",
size,
alignment);
return nullptr;
}

void* node_memory = runtime::pal_allocate(alloc_size);

// If allocation failed, log message and return nullptr.
Expand Down
Loading