views:

28

answers:

1

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?

+1  A: 

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).

Zack
...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
Yeah, also processes can change each others' memory by mmap'ing parts of their /proc/pid/mem
MarkR
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.)
Zack