History

tccrun: 'selinux' mmap: use only one mapping

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.

grischka committed 9 years ago

Commit ea2c36c1 parent 5420bb81 file · +19 −38Browse files

Unified · Split

tccrun.c+19−38
@@ -60,7 +60,7 @@ static void win64_del_function_table(void *); · 59 unmodified lines
60 60
61LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)61LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
62{62{
63 int size; void *mem;63 int size;
64 64
65 if (TCC_RELOCATE_AUTO != ptr)65 if (TCC_RELOCATE_AUTO != ptr)
66 return tcc_relocate_ex(s1, ptr);66 return tcc_relocate_ex(s1, ptr);
@@ -70,35 +70,17 @@ LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr) · 3 unmodified lines
70 return -1;70 return -1;
71 71
72#ifdef HAVE_SELINUX72#ifdef HAVE_SELINUX
73 { /* Use mmap instead of malloc for Selinux. Ref:73 /* Use mmap instead of malloc for Selinux. */
74 http://www.gnu.org/s/libc/manual/html_node/File-Size.html */74 ptr = mmap (NULL, size, PROT_READ|PROT_WRITE,
75 75 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
76 char tmpfname[] = "/tmp/.tccrunXXXXXX";76 if (ptr == MAP_FAILED)
77 int fd = mkstemp (tmpfname);77 tcc_error("tccrun: could not map memory");
78 void *wr_mem;78 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
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 }
97#else79#else
98 mem = tcc_malloc(size);80 ptr = tcc_malloc(size);
99 tcc_relocate_ex(s1, mem); /* no more errors expected */
100 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
101#endif81#endif
82 tcc_relocate_ex(s1, ptr); /* no more errors expected */
83 dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr);
102 return 0;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 for (i = 0; i < s1->nb_runtime_mem; ++i) {91 for (i = 0; i < s1->nb_runtime_mem; ++i) {
110#ifdef HAVE_SELINUX92#ifdef HAVE_SELINUX
111 int size = (int)(addr_t)s1->runtime_mem[i];93 unsigned size = (unsigned)(addr_t)s1->runtime_mem[i++];
112 munmap(s1->runtime_mem[++i], size);94 munmap(s1->runtime_mem[i], size);
113 munmap(s1->runtime_mem[++i], size);
114#else95#else
115# ifdef _WIN6496#ifdef _WIN64
116 win64_del_function_table(*(void**)s1->runtime_mem[i]);97 win64_del_function_table(*(void**)s1->runtime_mem[i]);
117# endif98#endif
118 tcc_free(s1->runtime_mem[i]);99 tcc_free(s1->runtime_mem[i]);
119#endif100#endif
120 }101 }
@@ -264,19 +245,19 @@ static void set_pages_executable(void *ptr, unsigned long length) · 143 unmodified lines
264 unsigned long old_protect;245 unsigned long old_protect;
265 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);246 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
266#else247#else
248 void __clear_cache(void *beginning, void *end);
249 addr_t start, end;
267#ifndef PAGESIZE250#ifndef PAGESIZE
268# define PAGESIZE 4096251# define PAGESIZE 4096
269#endif252#endif
270 addr_t start, end;
271 start = (addr_t)ptr & ~(PAGESIZE - 1);253 start = (addr_t)ptr & ~(PAGESIZE - 1);
272 end = (addr_t)ptr + length;254 end = (addr_t)ptr + length;
273 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);255 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
274 if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC))256 if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC))
275 tcc_error("mprotect failed: did you mean to configure --with-selinux?");257 tcc_error("mprotect failed: did you mean to configure --with-selinux?");
276#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64258# if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
277 { extern void __clear_cache(void *beginning, void *end);259 __clear_cache(ptr, (char *)ptr + length);
278 __clear_cache(ptr, (char *)ptr + length); }260# endif
279#endif
280#endif261#endif
281}262}
282 263

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t