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 e222e42
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ namespace photon
#if !defined(_WIN64) && !defined(__aarch64__)
madvise(ptr, size, MADV_DONTNEED);
#endif
mprotect(ptr, PAGE_SIZE, PROT_READ | PROT_WRITE);
free(ptr);
}

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

0 comments on commit e222e42

Please sign in to comment.