Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ jobs:
- name: Run Ruby tests with the address sanitizer
run: bundle exec rake test:asan

ubsan:
ubsan-x86-64:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
Expand All @@ -335,6 +335,16 @@ jobs:
- name: Run Ruby tests with the undefined behavior sanitizer
run: bundle exec rake test:ubsan

ubsan-armv7:
runs-on: ubuntu-24.04-arm
steps:
- uses: actions/checkout@v7
- name: Run Ruby tests with the undefined behavior sanitizer on 32-bit ARM
run: |
docker run --rm --platform linux/arm/v7 -v "$PWD:/work" -w /work \
-e BUNDLE_GEMFILE=/work/gemfiles/4.0/Gemfile ruby:4.0 \
sh -ec 'bundle install && bundle exec rake test:ubsan'

gcc-analyzer:
runs-on: ubuntu-latest
steps:
Expand Down
43 changes: 23 additions & 20 deletions include/prism/internal/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ void pm_arena_reserve(pm_arena_t *arena, size_t capacity);

/*
* Slow path for pm_arena_alloc: allocate a new block, either the next current
* block or a block of its own for a large allocation, and return a pointer to
* the first `size` bytes. Do not call directly — use pm_arena_alloc instead.
* block or a block of its own for a large allocation, and return an aligned
* allocation from it. Do not call directly — use pm_arena_alloc instead.
*/
void * pm_arena_alloc_slow(pm_arena_t *arena, size_t size);
void * pm_arena_alloc_slow(pm_arena_t *arena, size_t size, size_t alignment);

/*
* Report an allocation whose total size does not fit in a size_t. This
Expand All @@ -81,34 +81,37 @@ void * pm_arena_alloc_slow(pm_arena_t *arena, size_t size);
PRISM_NORETURN void pm_arena_alloc_overflow(void);

/*
* Allocate memory from the arena. The returned memory is NOT zeroed. This
* function is infallible — it aborts on allocation failure.
* Bytes to skip from an address to meet a power-of-two alignment.
*/
static PRISM_INLINE size_t
pm_arena_padding(const char *address, size_t alignment) {
return (size_t) (-(uintptr_t) address & (alignment - 1));
}

/*
* Allocate memory from the arena with the given power-of-two alignment. The
* returned memory is NOT zeroed. This function aborts on allocation failure.
*
* The fast path (bump pointer within the current block) is inlined at each
* call site. The slow path (new block allocation) is out-of-line.
*/
static PRISM_FORCE_INLINE void *
pm_arena_alloc(pm_arena_t *arena, size_t size, size_t alignment) {
if (arena->current != NULL) {
size_t used_aligned = (arena->current->used + alignment - 1) & ~(alignment - 1);
size_t needed = used_aligned + size;

if (used_aligned >= arena->current->used && needed >= used_aligned && needed <= arena->current->capacity) {
arena->current->used = needed;
return arena->current->data + used_aligned;
pm_arena_block_t *block = arena->current;
if (block != NULL) {
char *next = block->data + block->used;
size_t padding = pm_arena_padding(next, alignment);
size_t available = block->capacity - block->used;

if (padding <= available && size <= available - padding) {
block->used += padding + size;
return next + padding;
}
}

return pm_arena_alloc_slow(arena, size);
return pm_arena_alloc_slow(arena, size, alignment);
}

/*
* Allocate memory from the arena whose address is aligned to the given power of
* two. Unlike pm_arena_alloc, the alignment may exceed the alignment of the
* arena's blocks, at the cost of a slower path that is not inlined.
*/
void * pm_arena_alloc_aligned(pm_arena_t *arena, size_t size, size_t alignment);

/*
* Allocate memory for a single element of type T_ from the arena.
*/
Expand Down
75 changes: 22 additions & 53 deletions src/arena.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "prism/internal/arena.h"

#include "prism/internal/allocator.h"
#include "prism/internal/assert.h"
#include "prism/internal/oom.h"

#include <stdint.h>
Expand Down Expand Up @@ -50,12 +49,11 @@ pm_arena_next_block_size(const pm_arena_t *arena) {
}

/**
* Allocate a block with the given data capacity and initial usage, linked to
* Allocate an empty block with the given data capacity, linked to
* the given previous block. Aborts on allocation failure.
*/
static pm_arena_block_t *
pm_arena_block_alloc(pm_arena_block_t *prev, size_t data_size, size_t initial_used) {
PRISM_ASSERT(initial_used <= data_size);
pm_arena_block_alloc(pm_arena_block_t *prev, size_t data_size) {
if (PRISM_UNLIKELY(data_size > SIZE_MAX - PM_ARENA_BLOCK_SIZE(0))) {
pm_arena_alloc_overflow();
}
Expand All @@ -64,19 +62,19 @@ pm_arena_block_alloc(pm_arena_block_t *prev, size_t data_size, size_t initial_us
if (block == NULL) PRISM_OOM();

block->capacity = data_size;
block->used = initial_used;
block->used = 0;
block->prev = prev;

return block;
}

/**
* Allocate a new block with the given data capacity and initial usage, make it
* Allocate an empty block with the given data capacity, make it
* the current block, and return it.
*/
static pm_arena_block_t *
pm_arena_block_new(pm_arena_t *arena, size_t data_size, size_t initial_used) {
pm_arena_block_t *block = pm_arena_block_alloc(arena->current, data_size, initial_used);
pm_arena_block_new(pm_arena_t *arena, size_t data_size) {
pm_arena_block_t *block = pm_arena_block_alloc(arena->current, data_size);
arena->current = block;
arena->block_count++;
return block;
Expand All @@ -103,60 +101,31 @@ void
pm_arena_reserve(pm_arena_t *arena, size_t capacity) {
if (capacity <= PM_ARENA_INITIAL_SIZE) return;
if (arena->current != NULL && (arena->current->capacity - arena->current->used) >= capacity) return;
pm_arena_block_new(arena, capacity, 0);
pm_arena_block_new(arena, capacity);
}

/**
* Slow path for pm_arena_alloc: allocate a new block and return a pointer to
* the first `size` bytes. Called when the current block has insufficient space.
* A large allocation goes in a block of exactly its size on the large list,
* which leaves the current block in place.
* A new block's address is unknown until it is allocated. Allow for the
* maximum padding when choosing its capacity, then consume the actual padding.
*/
void *
pm_arena_alloc_slow(pm_arena_t *arena, size_t size) {
pm_arena_alloc_slow(pm_arena_t *arena, size_t size, size_t alignment) {
size_t max_padding = alignment - 1;
if (PRISM_UNLIKELY(size > SIZE_MAX - max_padding)) pm_arena_alloc_overflow();
size_t needed = size + max_padding;
size_t block_data_size = pm_arena_next_block_size(arena);

if (size > block_data_size / PM_ARENA_LARGE_DIVISOR) {
arena->large = pm_arena_block_alloc(arena->large, size, size);
return arena->large->data;
pm_arena_block_t *block;
if (needed > block_data_size / PM_ARENA_LARGE_DIVISOR) {
block = pm_arena_block_alloc(arena->large, needed);
arena->large = block;
} else {
block = pm_arena_block_new(arena, block_data_size);
}

pm_arena_block_t *block = pm_arena_block_new(arena, block_data_size, size);
return block->data;
}

/**
* Allocate memory whose address is aligned to the given power of two. In the
* current block the address is known ahead of time, so this pads by exactly
* what it needs. A new block's address is only known once it exists, so that
* case asks for the worst case and gives the unused tail back to the block.
*/
void *
pm_arena_alloc_aligned(pm_arena_t *arena, size_t size, size_t alignment) {
pm_arena_block_t *block = arena->current;

if (block != NULL) {
uintptr_t next = (uintptr_t) (block->data + block->used);
size_t padding = (size_t) (-next & (alignment - 1));
size_t available = block->capacity - block->used;

if (padding <= available && size <= available - padding) {
block->used += padding + size;
return (void *) (next + padding);
}
}

size_t mask = alignment - 1;
if (PRISM_UNLIKELY(size > SIZE_MAX - mask)) pm_arena_alloc_overflow();
uintptr_t start = (uintptr_t) pm_arena_alloc_slow(arena, size + mask);
uintptr_t aligned = (start + mask) & ~(uintptr_t) mask;

block = arena->current;
if (block != NULL && (uintptr_t) block->data == start) {
block->used = (size_t) (aligned - start) + size;
}

return (void *) aligned;
size_t padding = pm_arena_padding(block->data, alignment);
block->used = padding + size;
return block->data + padding;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions templates/src/node.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ pm_node_list_append(pm_arena_t *arena, pm_node_list_t *list, pm_node_t *node) {

/**
* Carve a new batch of chunks out of the arena and hand out its first one.
* Request alignment to the chunk size through pm_arena_alloc_aligned; neither
* the chunk type nor the arena's blocks guarantee that alignment on their own.
* Request alignment to the chunk size so a slot's address identifies its chunk.
* The chunk-sized stride preserves this alignment for every chunk in the batch.
*/
static pm_node_chunk_t *
pm_node_chunks_carve(pm_node_chunks_t *chunks) {
pm_node_chunk_t *batch = (pm_node_chunk_t *) pm_arena_alloc_aligned(chunks->arena, PM_NODE_CHUNK_BATCH * PM_NODE_CHUNK_SIZE, PM_NODE_CHUNK_SIZE);
pm_node_chunk_t *batch = (pm_node_chunk_t *) pm_arena_alloc(chunks->arena, PM_NODE_CHUNK_BATCH * PM_NODE_CHUNK_SIZE, PM_NODE_CHUNK_SIZE);

chunks->next = batch + 1;
chunks->end = batch + PM_NODE_CHUNK_BATCH;
Expand Down
6 changes: 3 additions & 3 deletions test/prism/library_symbols_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def objdump(path)
end

def global_objdump_symbols(path)
objdump(path).select { |line| line[17] == "g" }
objdump(path).grep(/\A\h+ g/)
end

def hidden_global_objdump_symbols(path)
Expand All @@ -44,11 +44,11 @@ def nm(path)
end

def global_nm_symbols(path)
nm(path).select { |line| line[17] == "T" }
nm(path).grep(/\A\h+ T /)
end

def local_nm_symbols(path)
nm(path).select { |line| line[17] == "t" }
nm(path).grep(/\A\h+ t /)
end

# dig the symbol name out of each line. works for both `objdump` and `nm` output.
Expand Down
Loading