From: Klaus Treichel Date: Tue, 22 Aug 2006 18:02:55 +0000 (+0000) Subject: 2006-08-22 Klaus treichel X-Git-Tag: before.move.to.git~209 X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=f9dc32bbdc16546cdd56ded8145b8bc40eb47e61;p=francis%2Flibjit.git 2006-08-22 Klaus treichel * jit/jit-alloc.c: Use mmap and munmap to allocate executable memory where available because memory allocated with malloc is not executable on some archs/distros. --- diff --git a/ChangeLog b/ChangeLog index a50d4d5..44d417f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2006-08-22 Klaus treichel + + * jit/jit-alloc.c: Use mmap and munmap to allocate executable memory + where available because memory allocated with malloc is not executable + on some archs/distros. + 2006-08-23 Aleksey Demakov * jit/jit-reg-alloc.c: improve handling of three-address op codes. diff --git a/jit/jit-alloc.c b/jit/jit-alloc.c index cd3ea47..e279382 100644 --- a/jit/jit-alloc.c +++ b/jit/jit-alloc.c @@ -40,6 +40,21 @@ #include #include #endif +#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MMAP) && defined(HAVE_MUNMAP) +/* + * Make sure that "MAP_ANONYMOUS" is correctly defined, because it + * may not exist on some variants of Unix. + */ +#ifndef MAP_ANONYMOUS + #ifdef MAP_ANON + #define MAP_ANONYMOUS MAP_ANON + #endif +#endif +#ifdef MAP_ANONYMOUS +#define JIT_USE_MMAP +#endif +#endif + /*@ * @section Memory allocation @@ -126,7 +141,18 @@ void jit_free(void *ptr) @*/ void *jit_malloc_exec(unsigned int size) { +#ifdef JIT_USE_MMAP + void *ptr = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if(ptr == (void *)-1) + { + return (void *)0; + } + return ptr; +#else return malloc(size); +#endif } /*@ @@ -141,7 +167,11 @@ void jit_free_exec(void *ptr, unsigned int size) { if(ptr) { + #ifdef JIT_USE_MMAP + munmap(ptr, size); + #else free(ptr); + #endif } }