From 12eadc2b5edaf9e7b575e5499bd4f0005a7cb35b Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sun, 20 Jun 2021 19:54:24 +0300 Subject: [PATCH] Add a guard page for alternative signal handling stack --- src/Common/ThreadStatus.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Common/ThreadStatus.cpp b/src/Common/ThreadStatus.cpp index 18f24553d5e..76b0a6797df 100644 --- a/src/Common/ThreadStatus.cpp +++ b/src/Common/ThreadStatus.cpp @@ -7,6 +7,7 @@ #include #include +#include #include @@ -43,10 +44,15 @@ namespace struct ThreadStack { ThreadStack() - : data(aligned_alloc(4096, size)) - {} + : data(aligned_alloc(getPageSize(), size)) + { + /// Add a guard page + /// (and since the stack grows downward, we need to protect the first page). + mprotect(data, getPageSize(), PROT_NONE); + } ~ThreadStack() { + mprotect(data, getPageSize(), PROT_WRITE|PROT_READ); free(data); }