2

I am doing some simple JITing, and use VirtualProtectEx under Windows to mark pages as executable. What would be the equivalent of that under Linux, and preferably, other POSIX/Unix-like OSes too?

uj2
  • 2,255
  • 2
  • 21
  • 32

1 Answers1

3

You are looking for mprotect and probably also mmap. Note that, unlike with Windows, there is no way for process A to change process B's memory map (short of horrible tricks with ptrace).

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 1
    ...and keep in mind that some hardened kernels will prevent you from setting both write and execute permissions at the same time (`PROT_WRITE | PROT_EXEC`). – ssokolow Sep 27 '10 at 02:50
  • Yeah, also processes can change each others' memory by mmap'ing parts of their /proc/pid/mem – MarkR Sep 27 '10 at 11:12
  • MarkR: Processes can *write to each others' memory* with /proc/pid/mem or various other standard APIs (`shm*`, `mmap` with `MAP_SHARED`, etc) but that's not what I meant by "can't change another process's memory map". `VirtualProtectEx` and its friends let you change page permissions or allocate/deallocate memory in the address space of another process; that's what you can't do without dirty tricks on Unix. (Oddly, unless I missed something, Windows doesn't let you do a `MapViewOfFile(Ex)` operation on anyone but yourself.) – zwol Oct 18 '10 at 23:31