diff --git a/src/Patcher.zig b/src/Patcher.zig index f40f6e7..1a2995a 100644 --- a/src/Patcher.zig +++ b/src/Patcher.zig @@ -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), diff --git a/src/main.zig b/src/main.zig index 573e9e5..d7f8355 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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)); } }