Skip to content

Commit

Permalink
make sure mmap'd memory is page aligned; allocate an additional page …
Browse files Browse the repository at this point in the history
…for overflow protection
  • Loading branch information
koekeishiya committed Dec 15, 2020
1 parent ead6ced commit bda7d2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/misc/memory_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ struct memory_pool

bool memory_pool_init(struct memory_pool *pool, uint64_t size)
{
uint64_t pa_size = size + (size % PAGE_SIZE);

pool->used = 0;
pool->size = size;
pool->memory = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
return pool->memory != NULL;
pool->size = pa_size;
pool->memory = mmap(0, pa_size + PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);

bool result = pool->memory != MAP_FAILED;
if (result) mprotect(pool->memory + pa_size, PAGE_SIZE, PROT_NONE);

return result;
}

void *memory_pool_push(struct memory_pool *pool, uint64_t size)
Expand Down
12 changes: 9 additions & 3 deletions src/misc/ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ static struct {

bool ts_init(uint64_t size)
{
uint64_t pa_size = size + (size % PAGE_SIZE);

g_temp_storage.used = 0;
g_temp_storage.size = size;
g_temp_storage.memory = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
return g_temp_storage.memory != NULL;
g_temp_storage.size = pa_size;
g_temp_storage.memory = mmap(0, pa_size + PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);

bool result = g_temp_storage.memory != MAP_FAILED;
if (result) mprotect(g_temp_storage.memory + pa_size, PAGE_SIZE, PROT_NONE);

return result;
}

void *ts_alloc(uint64_t size)
Expand Down

0 comments on commit bda7d2b

Please sign in to comment.