patchRegion remove mmap to R|X, caller is responsible now

This commit is contained in:
2025-12-17 11:12:44 +01:00
parent de10ce58e2
commit 403fd6031b
2 changed files with 3 additions and 4 deletions

View File

@@ -209,8 +209,8 @@ pub const Statistics = struct {
/// Scans a memory region for instructions that require patching and applies the patches
/// using a hierarchy of tactics (Direct/Punning -> Successor Eviction -> Neighbor Eviction).
///
/// The region is processed Back-to-Front to ensure that modifications (punning) only
/// constrain instructions that have already been processed or are locked.
/// NOTE: This function leaves the region as R|W and the caller is responsible for changing it to
/// the desired protection
pub fn patchRegion(region: []align(page_size) u8) !void {
// For now just do a coarse lock.
// TODO: should we make this more fine grained?
@@ -296,8 +296,6 @@ pub fn patchRegion(region: []align(page_size) u8) !void {
{
// Apply patches.
try posix.mprotect(region, posix.PROT.READ | posix.PROT.WRITE);
defer posix.mprotect(region, posix.PROT.READ | posix.PROT.EXEC) catch
@panic("patchRegion: mprotect back to R|X failed. Can't continue");
var stats = Statistics.empty;
// Used to track which bytes have been modified or used for constraints (punning),

View File

@@ -261,6 +261,7 @@ fn patchLoadedElf(base: usize) !void {
log.info("Patching segment: 0x{x} - 0x{x}", .{ page_start, page_end });
try Patcher.patchRegion(region);
try posix.mprotect(region, elfToMmapProt(phdr.p_flags));
}
}