Is 4 byte -1 invalid code on most/all architectures?

Joel Dice joel.dice@gmail.com
Thu Dec 9 01:42:00 GMT 2010


On Thu, 9 Dec 2010, Jay K wrote:

>> Most Linux kernel based systems will give you an executable stack if you
>> ask for it by setting the GNU executable stack attribute on your executable.
>
>
> I really don't want to make the entire stack executable just for the
> sake of a relatively small amount of code. It seems like overkill.
>
>
> I don't even like the use I've seen of mprotect as needed that makes
> entire pages executable. Also overkill and slow.
>
>
> I think I will look into what various JITers do (Java, Mono).
> e.g. maybe mmap over file -1 with protection flags that allow execution?
> I believe I have known/nested lifetimes, so I can use a per-thread LIFO discipline,
> rather than, e.g. something garbage collected.

This is how I allocate temporary executable memory for JIT compilation, 
FWIW:

   // POSIX:
   void* p = mmap(0, sizeInBytes, PROT_EXEC | PROT_READ | PROT_WRITE,
                  MAP_PRIVATE | MAP_ANON, -1, 0);
   if (p != MAP_FAILED) {
     doSomething(p);
     munmap(const_cast<void*>(p), sizeInBytes);
   }

   // Windows:
   void* p = VirtualAlloc(0, sizeInBytes, MEM_COMMIT | MEM_RESERVE,
                          PAGE_EXECUTE_READWRITE);
   if (p) {
     doSomething(p);
     VirtualFree(const_cast<void*>(p), 0, MEM_RELEASE);
   }


>
> We also support far more than Linux: Solaris, MacOSX, Open/Net/FreeBSD, NT.
> sort of: Tru64, HP-UX (they were running recently)
> theoretically: Irix, AIX (should be easy given what we have), maybe even VMS (I
>  did some investigation/work on it)
>
>
> Obviously there is a solution involving writing out little temporary .so files,
> since all systems can run executable code out of files, but that I definitely don't want to do.



More information about the Gcc-help mailing list