For functions in the memory, we usually have W^X protection to avoid buffer overflow attacking.
In the patch_function, it disabled the W^X first to be able to inject code to the original function address:
pub(crate) unsafe fn patch_function(func: *mut u8, patch: &[u8]) {
make_memory_writable_and_executable(func); // <--------- HERE
inject_asm_code(patch, func);
}
Should we consider doing it like this:
- Change the memory page to Writable only (or Writable and Executable)
- Inject the code.
- Change the memory page to Executable only.
This idea also applies to the allocate_jit_memory.