mprotect last page of CodeBuffer#4540
Conversation
124f3f5 to
0622184
Compare
|
|
||
| // Protect the last page of the allocated buffer to trigger SIGSEGV on write access | ||
| #ifndef _WIN32 | ||
| const long pageSize = sysconf(_SC_PAGESIZE); |
There was a problem hiding this comment.
FEXCore::Utils::FEX_PAGE_MASK can be used here instead. We already hardcode the assumption of 4K pages in other places.
|
|
||
| // Resize the code buffer and reallocate our code size | ||
| CurrentCodeBuffer->Size *= 1.5; | ||
| CurrentCodeBuffer->Size = (CurrentCodeBuffer->Size >> 1) * 3; // * 1.5 |
There was a problem hiding this comment.
I'm not a fan of replacing readable code with less obvious bit-shifting tricks just to save hypothetical CPU cycles. #4479 changes this to a flat 2.0 anyway, so could we drop this for now?
There was a problem hiding this comment.
me neither and you're right. I made this less readable to silence a clang-tidy warning about multiplying an integer by a float.
| uintptr_t lastPageAddr = reinterpret_cast<uintptr_t>(Buffer.Ptr) + Buffer.Size - pageSize; | ||
| lastPageAddr = lastPageAddr & ~(pageSize - 1); | ||
|
|
||
| if (mprotect(reinterpret_cast<void*>(lastPageAddr), pageSize, PROT_NONE) != 0) { |
There was a problem hiding this comment.
Use VirtualProtect here which is generic - and you'll probably want to adjust the CodeSize check to trigger a resize a page earlier
| const long pageSize = sysconf(_SC_PAGESIZE); | ||
| if (pageSize > 0 && Buffer.Size >= (size_t)pageSize) { | ||
| uintptr_t lastPageAddr = reinterpret_cast<uintptr_t>(Buffer.Ptr) + Buffer.Size - pageSize; | ||
| lastPageAddr = lastPageAddr & ~(pageSize - 1); |
There was a problem hiding this comment.
AlignDown(reinterpret_cast<uintptr_t>(Buffer.Ptr) + Buffer.Size - 1, FEXCore::Utils::FEX_PAGE_SIZE)
Might be cleaner
0622184 to
39e2a83
Compare
bylaws
left a comment
There was a problem hiding this comment.
FEX/FEXCore/Source/Interface/Core/JIT/JIT.cpp
Line 757 in 39e2a83
protects last page of codebuffer. This should cause a SIGSEGV if we try to access it. Until now it was possible to go over and access out of bounds. In addition, there a couple of clang-tidy fixes which should be NFC.
39e2a83 to
791502a
Compare
Done. |
protects last page of codebuffer. This should cause a SIGSEGV if we try to access it. Until now it was possible to go over and access out of bounds.
In addition, there a couple of clang-tidy fixes which should be NFC.