In the previous implementation, the rx mapping was never used. Therefor it is assumed that it is not needed. With only one mapping there is no reason to use a real /tmp/.xxxx file either as we can use an anonymous mapping.
Unified · Split
| @@ -60,7 +60,7 @@ static void win64_del_function_table(void *); · 59 unmodified lines | ||
| 60 | 60 | |
| 61 | 61 | LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr) |
| 62 | 62 | { |
| 63 | − int size; void *mem; | |
| 63 | + int size; | |
| 64 | 64 | |
| 65 | 65 | if (TCC_RELOCATE_AUTO != ptr) |
| 66 | 66 | return tcc_relocate_ex(s1, ptr); |
| @@ -70,35 +70,17 @@ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr) · 3 unmodified lines | ||
| 70 | 70 | return -1; |
| 71 | 71 | |
| 72 | 72 | #ifdef HAVE_SELINUX |
| 73 | − { /* Use mmap instead of malloc for Selinux. Ref: | |
| 74 | − http://www.gnu.org/s/libc/manual/html_node/File-Size.html */ | |
| 75 | − | |
| 76 | − char tmpfname[] = "/tmp/.tccrunXXXXXX"; | |
| 77 | − int fd = mkstemp (tmpfname); | |
| 78 | − void *wr_mem; | |
| 79 | − | |
| 80 | − unlink (tmpfname); | |
| 81 | − ftruncate (fd, size); | |
| 82 | − | |
| 83 | − wr_mem = mmap (NULL, size, PROT_READ|PROT_WRITE, | |
| 84 | − MAP_SHARED, fd, 0); | |
| 85 | − if (wr_mem == MAP_FAILED) | |
| 86 | − tcc_error("/tmp not writeable"); | |
| 87 | − mem = mmap (NULL, size, PROT_READ|PROT_EXEC, | |
| 88 | − MAP_SHARED, fd, 0); | |
| 89 | − if (mem == MAP_FAILED) | |
| 90 | − tcc_error("/tmp not executable"); | |
| 91 | − | |
| 92 | − tcc_relocate_ex(s1, wr_mem); | |
| 93 | − dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size); | |
| 94 | − dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, wr_mem); | |
| 95 | − dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem); | |
| 96 | − } | |
| 73 | + /* Use mmap instead of malloc for Selinux. */ | |
| 74 | + ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, | |
| 75 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| 76 | + if (ptr == MAP_FAILED) | |
| 77 | + tcc_error("tccrun: could not map memory"); | |
| 78 | + dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size); | |
| 97 | 79 | #else |
| 98 | − mem = tcc_malloc(size); | |
| 99 | − tcc_relocate_ex(s1, mem); /* no more errors expected */ | |
| 100 | − dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem); | |
| 80 | + ptr = tcc_malloc(size); | |
| 101 | 81 | #endif |
| 82 | + tcc_relocate_ex(s1, ptr); /* no more errors expected */ | |
| 83 | + dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr); | |
| 102 | 84 | return 0; |
| 103 | 85 | } |
| 104 | 86 | |
| @@ -108,13 +90,12 @@ ST_FUNC void tcc_run_free(TCCState *s1) · 3 unmodified lines | ||
| 108 | 90 | |
| 109 | 91 | for (i = 0; i < s1->nb_runtime_mem; ++i) { |
| 110 | 92 | #ifdef HAVE_SELINUX |
| 111 | − int size = (int)(addr_t)s1->runtime_mem[i]; | |
| 112 | − munmap(s1->runtime_mem[++i], size); | |
| 113 | − munmap(s1->runtime_mem[++i], size); | |
| 93 | + unsigned size = (unsigned)(addr_t)s1->runtime_mem[i++]; | |
| 94 | + munmap(s1->runtime_mem[i], size); | |
| 114 | 95 | #else |
| 115 | −# ifdef _WIN64 | |
| 96 | +#ifdef _WIN64 | |
| 116 | 97 | win64_del_function_table(*(void**)s1->runtime_mem[i]); |
| 117 | −# endif | |
| 98 | +#endif | |
| 118 | 99 | tcc_free(s1->runtime_mem[i]); |
| 119 | 100 | #endif |
| 120 | 101 | } |
| @@ -264,19 +245,19 @@ static void set_pages_executable(void *ptr, unsigned long length) · 143 unmodified lines | ||
| 264 | 245 | unsigned long old_protect; |
| 265 | 246 | VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect); |
| 266 | 247 | #else |
| 248 | + void __clear_cache(void *beginning, void *end); | |
| 249 | + addr_t start, end; | |
| 267 | 250 | #ifndef PAGESIZE |
| 268 | 251 | # define PAGESIZE 4096 |
| 269 | 252 | #endif |
| 270 | − addr_t start, end; | |
| 271 | 253 | start = (addr_t)ptr & ~(PAGESIZE - 1); |
| 272 | 254 | end = (addr_t)ptr + length; |
| 273 | 255 | end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1); |
| 274 | 256 | if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC)) |
| 275 | 257 | tcc_error("mprotect failed: did you mean to configure --with-selinux?"); |
| 276 | −#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 | |
| 277 | − { extern void __clear_cache(void *beginning, void *end); | |
| 278 | − __clear_cache(ptr, (char *)ptr + length); } | |
| 279 | −#endif | |
| 258 | +# if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 | |
| 259 | + __clear_cache(ptr, (char *)ptr + length); | |
| 260 | +# endif | |
| 280 | 261 | #endif |
| 281 | 262 | } |
| 282 | 263 | |