Skip to content

risc0: determine size of allocator area from the linker script#38

Merged
gballet merged 1 commit intomainfrom
risc0-allocatable-size-from-linker-script
Aug 10, 2025
Merged

risc0: determine size of allocator area from the linker script#38
gballet merged 1 commit intomainfrom
risc0-allocatable-size-from-linker-script

Conversation

@gballet
Copy link
Copy Markdown
Contributor

@gballet gballet commented Aug 9, 2025

This is currently a PR because commit 7e206f2 broke risc0.

Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com>
@gballet gballet force-pushed the risc0-allocatable-size-from-linker-script branch from d61281b to 564d746 Compare August 10, 2025 15:29
@gballet gballet requested a review from Copilot August 10, 2025 15:29
Copy link
Copy Markdown
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 fixes risc0 support by changing how the allocator area is determined from a statically allocated fixed-size buffer to a dynamically sized area determined by the linker script. The change resolves issues introduced by commit 7e206f2.

  • Replaces hardcoded 128MB static allocation with dynamic memory area calculation
  • Uses linker symbols (_end) to determine the start of allocatable memory
  • Calculates memory size based on the difference between the end of program data and a fixed end address

if (!fixed_allocator_initialized) {
fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_mem[0..]);
const mem_start: [*]u8 = @ptrCast(&_end);
const mem_end: [*]u8 = @ptrFromInt(0xC000000);
Copy link

Copilot AI Aug 10, 2025

Choose a reason for hiding this comment

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

The magic number 0xC000000 (201MB) should be replaced with a named constant to improve code maintainability and make the memory layout explicit.

Suggested change
const mem_end: [*]u8 = @ptrFromInt(0xC000000);
const mem_end: [*]u8 = @ptrFromInt(MEMORY_END_ADDRESS);

Copilot uses AI. Check for mistakes.
if (!fixed_allocator_initialized) {
fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_mem[0..]);
const mem_start: [*]u8 = @ptrCast(&_end);
const mem_end: [*]u8 = @ptrFromInt(0xC000000);
Copy link

Copilot AI Aug 10, 2025

Choose a reason for hiding this comment

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

This calculation could result in integer underflow if _end is located at an address higher than 0xC000000. Consider adding a check to ensure mem_start < mem_end before performing the subtraction.

Suggested change
const mem_end: [*]u8 = @ptrFromInt(0xC000000);
const mem_end: [*]u8 = @ptrFromInt(0xC000000);
if (@intFromPtr(mem_start) > @intFromPtr(mem_end)) {
@panic("get_allocator: mem_start is greater than mem_end, possible integer underflow");
}

Copilot uses AI. Check for mistakes.
@gballet gballet merged commit 817aa24 into main Aug 10, 2025
5 checks passed
@g11tech g11tech deleted the risc0-allocatable-size-from-linker-script branch August 16, 2025 18:51
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