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
2010-09-27 02:48:42
...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
2010-09-27 02:50:53
Yeah, also processes can change each others' memory by mmap'ing parts of their /proc/pid/mem
MarkR
2010-09-27 11:12:51
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
2010-10-18 23:31:02