Skip to content

Commit

Permalink
Add mprotect for the top-end of photon stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings committed Apr 22, 2024
1 parent 83a3f39 commit b352346
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ 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);
photon_thread_dealloc(buf, stack_size);
}
};
Expand Down Expand Up @@ -934,13 +935,14 @@ 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);
stack_size = align_up(randomizer + stack_size + sizeof(thread) + PAGE_SIZE, PAGE_SIZE);
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;
p = align_down(p, 64);
auto th = new((char*) p) thread;
th->buf = ptr;
th->stackful_alloc_top = ptr;
th->stackful_alloc_top = ptr + 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 = 1024 * 64)
uint64_t stack_size = 8UL * 1024 * 1024)
{
_on_timer = on_timer;
_default_timeout = default_timeout;
Expand Down

0 comments on commit b352346

Please sign in to comment.