Skip to content

Commit

Permalink
Make protect page aligned
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings committed Apr 23, 2024
1 parent b352346 commit 7a4407e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ namespace photon
assert(state == states::DONE);
// `buf` and `stack_size` will always store on register
// when calling deallocating.
mprotect(buf, PAGE_SIZE, PROT_READ | PROT_WRITE);
char* protect_head = (char*)align_up((uint64_t)buf, PAGE_SIZE);
mprotect(protect_head, PAGE_SIZE, PROT_READ | PROT_WRITE);
photon_thread_dealloc(buf, stack_size);
}
};
Expand Down Expand Up @@ -935,14 +936,16 @@ R"(
if (unlikely(!rq.current))
LOG_ERROR_RETURN(ENOSYS, nullptr, "Photon not initialized in this vCPU (OS thread)");
size_t randomizer = (rand() % 32) * (1024 + 8);
stack_size = align_up(randomizer + stack_size + sizeof(thread) + PAGE_SIZE, PAGE_SIZE);
stack_size = align_up(randomizer + stack_size + sizeof(thread), PAGE_SIZE);
stack_size += PAGE_SIZE * 2; // extra 2 pages for alignment and set guard page
char* ptr = (char*)photon_thread_alloc(stack_size);
mprotect(ptr, PAGE_SIZE, PROT_NONE);
uint64_t p = (uint64_t) ptr + stack_size - sizeof(thread) - randomizer;
char* protect_head = (char*)align_up((uint64_t)ptr, PAGE_SIZE);
mprotect(protect_head, PAGE_SIZE, PROT_NONE);
uint64_t p = (uint64_t)ptr + stack_size - sizeof(thread) - randomizer;
p = align_down(p, 64);
auto th = new((char*) p) thread;
auto th = new ((char*)p) thread;
th->buf = ptr;
th->stackful_alloc_top = ptr + PAGE_SIZE;
th->stackful_alloc_top = protect_head + PAGE_SIZE;
th->start = start;
th->stack_size = stack_size;
th->arg = arg;
Expand Down
2 changes: 1 addition & 1 deletion thread/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace photon
// it has a `stack_size`, and the `on_timer` is invoked within the thread's context.
// The timer object is deleted automatically after it is finished.
Timer(uint64_t default_timeout, Entry on_timer, bool repeating = true,
uint64_t stack_size = 8UL * 1024 * 1024)
uint64_t stack_size = DEFAULT_STACK_SIZE)
{
_on_timer = on_timer;
_default_timeout = default_timeout;
Expand Down

0 comments on commit 7a4407e

Please sign in to comment.