History

tccrun: add option CONFIG_RUNMEM_RO=2

/* 0 = .text rwx other rw (memory: min 2 pages) */ /* 1 = .text rx other rw (memory: min 3 pages) */ /* 2 = .text rx .rdata ro .data/.bss rw (memory: min 4 pages) */ tcc -vv -run ... shows some info. Also when compiled with -DMEM_DEBUG: tcc -bench -run ... shows some memory usage

grischka committed 2 years ago

Commit 9d2068c1 parent ca061f39 files · +27 −24Browse files

Unified · Split

9 files
  1. Makefile +1 −1
  2. libtcc.c +3 −2
  3. tcc.h +2 −3
  4. tccpe.c +1 −1
  5. tccpp.c +3 −3
  6. tccrun.c +10 −9
  7. tests/Makefile +1 −1
  8. tests/tcctest.c +2 −0
  9. tests/tests2/Makefile +4 −4
Makefile+1−1
@@ -491,7 +491,7 @@ test-install: tccdefs_.h · 490 unmodified lines
491 @$(MAKE) -C tests TESTINSTALL=yes #_all491 @$(MAKE) -C tests TESTINSTALL=yes #_all
492 492
493clean:493clean:
494 @rm -f tcc$(EXESUF) tcc_c$(EXESUF) tcc_p$(EXESUF) *-tcc$(EXESUF)494 @rm -f tcc *-tcc tcc_p tcc_c
495 @rm -f tags ETAGS *.o *.a *.so* *.out *.log lib*.def *.exe *.dll495 @rm -f tags ETAGS *.o *.a *.so* *.out *.log lib*.def *.exe *.dll
496 @rm -f a.out *.dylib *_.h *.pod *.tcov496 @rm -f a.out *.dylib *_.h *.pod *.tcov
497 @$(MAKE) -s -C lib $@497 @$(MAKE) -s -C lib $@
libtcc.c+3−2
@@ -2203,9 +2203,10 @@ PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time) · 2202 unmodified lines
2203#ifdef TCC_IS_NATIVE2203#ifdef TCC_IS_NATIVE
2204 if (s1->run_size) {2204 if (s1->run_size) {
2205 Section *s = s1->symtab;2205 Section *s = s1->symtab;
2206 int ms = s->data_offset + s->link->data_offset + s->hash->data_offset;2206 unsigned ms = s->data_offset + s->link->data_offset + s->hash->data_offset;
2207 unsigned rs = s1->run_size;
2207 fprintf(stderr, ": %d to run, %d symbols, %d other,",2208 fprintf(stderr, ": %d to run, %d symbols, %d other,",
2208 s1->run_size, ms, mem_cur_size - s1->run_size - ms);2209 rs, ms, mem_cur_size - rs - ms);
2209 }2210 }
2210#endif2211#endif
2211 fprintf(stderr, " %d max (bytes)\n", mem_max_size);2212 fprintf(stderr, " %d max (bytes)\n", mem_max_size);
tcc.h+2−3
@@ -135,7 +135,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 134 unmodified lines
135/* include file debug */135/* include file debug */
136/* #define INC_DEBUG */136/* #define INC_DEBUG */
137/* memory leak debug (only for single threaded usage) */137/* memory leak debug (only for single threaded usage) */
138/* #define MEM_DEBUG */138/* #define MEM_DEBUG 1,2,3 */
139/* assembler debug */139/* assembler debug */
140/* #define ASM_DEBUG */140/* #define ASM_DEBUG */
141 141
@@ -987,8 +987,7 @@ struct TCCState { · 845 unmodified lines
987 987
988#ifdef TCC_IS_NATIVE988#ifdef TCC_IS_NATIVE
989 const char *run_main; /* entry for tcc_run() */989 const char *run_main; /* entry for tcc_run() */
990 void *run_mem; /* runtime_memory */990 void *run_ptr; /* runtime_memory */
991 void *run_ptr; /* ptr to runtime_memory (aligned) */
992 unsigned run_size; /* size of runtime_memory */991 unsigned run_size; /* size of runtime_memory */
993#ifdef _WIN64992#ifdef _WIN64
994 void *run_function_table; /* unwind data */993 void *run_function_table; /* unwind data */
tccpe.c+1−1
@@ -732,7 +732,7 @@ static int pe_write(struct pe_info *pe) · 731 unmodified lines
732 732
733 memcpy(psh->Name, sh_name, umin(strlen(sh_name), sizeof psh->Name));733 memcpy(psh->Name, sh_name, umin(strlen(sh_name), sizeof psh->Name));
734 if (si->cls == sec_debug)734 if (si->cls == sec_debug)
735 need_strtab += pe_put_long_secname(psh->Name, sh_name);735 need_strtab += pe_put_long_secname((char*)psh->Name, sh_name);
736 736
737 psh->Characteristics = si->pe_flags;737 psh->Characteristics = si->pe_flags;
738 psh->VirtualAddress = addr;738 psh->VirtualAddress = addr;
tccpp.c+3−3
@@ -127,7 +127,7 @@ ST_FUNC void expect(const char *msg) · 126 unmodified lines
127#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)127#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
128#define TAL_DEBUG_PARAMS128#define TAL_DEBUG_PARAMS
129#else129#else
130#define TAL_DEBUG 1130#define TAL_DEBUG MEM_DEBUG
131//#define TAL_INFO 1 /* collect and dump allocators stats */131//#define TAL_INFO 1 /* collect and dump allocators stats */
132#define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)132#define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
133#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)133#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
@@ -189,7 +189,7 @@ tail_call: · 55 unmodified lines
189 al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,189 al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
190 (al->peak_p - al->buffer) * 100.0 / al->size);190 (al->peak_p - al->buffer) * 100.0 / al->size);
191#endif191#endif
192#ifdef TAL_DEBUG192#if TAL_DEBUG && TAL_DEBUG != 3 /* do not check TAL leaks with -DMEM_DEBUG=3 */
193 if (al->nb_allocs > 0) {193 if (al->nb_allocs > 0) {
194 uint8_t *p;194 uint8_t *p;
195 fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n",195 fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n",
@@ -203,7 +203,7 @@ tail_call: · 7 unmodified lines
203 }203 }
204 p += header->size + sizeof(tal_header_t);204 p += header->size + sizeof(tal_header_t);
205 }205 }
206#if MEM_DEBUG-0 == 2206#if TAL_DEBUG == 2
207 exit(2);207 exit(2);
208#endif208#endif
209 }209 }
tccrun.c+10−9
@@ -128,9 +128,9 @@ static int rt_mem(TCCState *s1, int size) · 127 unmodified lines
128 return tcc_error_noabort("tccrun: could not map memory");128 return tcc_error_noabort("tccrun: could not map memory");
129 ptr_diff = (char*)prw - (char*)ptr; /* = size; */129 ptr_diff = (char*)prw - (char*)ptr; /* = size; */
130 //printf("map %p %p %p\n", ptr, prw, (void*)ptr_diff);130 //printf("map %p %p %p\n", ptr, prw, (void*)ptr_diff);
131 size *= 2;
131#else132#else
132 s1->run_mem = tcc_malloc(size + PAGESIZE); /* one extra page to align malloc memory */133 ptr = tcc_malloc(size += PAGESIZE); /* one extra page to align malloc memory */
133 ptr = (void*)PAGEALIGN(s1->run_mem);
134#endif134#endif
135 s1->run_ptr = ptr;135 s1->run_ptr = ptr;
136 s1->run_size = size;136 s1->run_size = size;
@@ -188,14 +188,14 @@ ST_FUNC void tcc_run_free(TCCState *s1) · 51 unmodified lines
188 st_unlink(s1);188 st_unlink(s1);
189 size = s1->run_size;189 size = s1->run_size;
190#ifdef HAVE_SELINUX190#ifdef HAVE_SELINUX
191 munmap(ptr, size * 2);191 munmap(ptr, size);
192#else192#else
193 /* unprotect memory to make it usable for malloc again */193 /* unprotect memory to make it usable for malloc again */
194 protect_pages(ptr, size, 2 /*rw*/);194 protect_pages((void*)PAGEALIGN(ptr), size - PAGESIZE, 2 /*rw*/);
195# ifdef _WIN64195# ifdef _WIN64
196 win64_del_function_table(s1->run_function_table);196 win64_del_function_table(s1->run_function_table);
197# endif197# endif
198 tcc_free(s1->run_mem);198 tcc_free(ptr);
199#endif199#endif
200}200}
201 201
@@ -286,8 +286,9 @@ static void cleanup_sections(TCCState *s1) · 84 unmodified lines
286}286}
287 287
288/* ------------------------------------------------------------- */288/* ------------------------------------------------------------- */
289/* 0 = .text rwx other rw */289/* 0 = .text rwx other rw (memory >= 2 pages a 4096 bytes) */
290/* 1 = .text rx .rdata r .data/.bss rw */290/* 1 = .text rx other rw (memory >= 3 pages) */
291/* 2 = .text rx .rdata ro .data/.bss rw (memory >= 4 pages) */
291 292
292/* Some targets implement secutiry options that do not allow write in293/* Some targets implement secutiry options that do not allow write in
293 executable code. These targets need CONFIG_RUNMEM_RO=1.294 executable code. These targets need CONFIG_RUNMEM_RO=1.
@@ -370,7 +371,7 @@ redo: · 76 unmodified lines
370 align = 64;371 align = 64;
371#endif372#endif
372 /* start new page for different permissions */373 /* start new page for different permissions */
373 if (CONFIG_RUNMEM_RO || k == 0)374 if (k <= CONFIG_RUNMEM_RO)
374 align = PAGESIZE;375 align = PAGESIZE;
375 }376 }
376 s->sh_addralign = align;377 s->sh_addralign = align;
@@ -387,7 +388,7 @@ redo: · 10 unmodified lines
387 continue;388 continue;
388#endif389#endif
389 f = k;390 f = k;
390 if (CONFIG_RUNMEM_RO == 0) {391 if (f >= CONFIG_RUNMEM_RO) {
391 if (f != 0)392 if (f != 0)
392 continue;393 continue;
393 f = 3; /* change only SHF_EXECINSTR to rwx */394 f = 3; /* change only SHF_EXECINSTR to rwx */
tests/Makefile+1−1
@@ -342,7 +342,7 @@ cache: tcc_g · 341 unmodified lines
342clean:342clean:
343 rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc343 rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc
344 rm -f *-cc *-gcc *-tcc *.exe hello libtcc_test vla_test tcctest[1234]344 rm -f *-cc *-gcc *-tcc *.exe hello libtcc_test vla_test tcctest[1234]
345 rm -f asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF)345 rm -f asm-c-connect asm-c-connect-sep
346 rm -f ex? tcc_g weaktest.*.txt *.def *.pdb *.obj libtcc_test_mt346 rm -f ex? tcc_g weaktest.*.txt *.def *.pdb *.obj libtcc_test_mt
347 @$(MAKE) -C tests2 $@347 @$(MAKE) -C tests2 $@
348 @$(MAKE) -C pp $@348 @$(MAKE) -C pp $@
tests/tcctest.c+2−0
@@ -3760,12 +3760,14 @@ void asm_dot_test(void) · 3759 unmodified lines
3760 3760
3761void asm_pcrel_test(void)3761void asm_pcrel_test(void)
3762{3762{
3763#if defined(__i386__)
3763 unsigned o1, o2;3764 unsigned o1, o2;
3764 /* subtract text-section label from forward or other-section label */3765 /* subtract text-section label from forward or other-section label */
3765 asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1));3766 asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1));
3766 /* verify ... */3767 /* verify ... */
3767 asm("2: mov $2b,%%eax; sub $1b,%%eax; mov %%eax,%0" : "=m"(o2));3768 asm("2: mov $2b,%%eax; sub $1b,%%eax; mov %%eax,%0" : "=m"(o2));
3768 printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */3769 printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */
3770#endif
3769}3771}
3770 3772
3771void asm_test(void)3773void asm_test(void)
tests/tests2/Makefile+4−4
@@ -55,10 +55,6 @@ endif · 54 unmodified lines
55 55
56# Some tests might need arguments56# Some tests might need arguments
57ARGS =57ARGS =
58ifneq (-$(CONFIG_WIN32)-,-yes-)
5922_floating_point.test: FLAGS += -lm
6024_math_library.test: FLAGS += -lm
61endif
6231_args.test : ARGS = arg1 arg2 arg3 arg4 arg55831_args.test : ARGS = arg1 arg2 arg3 arg4 arg5
6346_grep.test : ARGS = '[^* ]*[:a:d: ]+\:\*-/: $$' $(SRC)/46_grep.c5946_grep.test : ARGS = '[^* ]*[:a:d: ]+\:\*-/: $$' $(SRC)/46_grep.c
64 60
@@ -69,6 +65,10 @@ NORUN = · 4 unmodified lines
69# Some tests might need different flags65# Some tests might need different flags
70FLAGS =66FLAGS =
7176_dollars_in_identifiers.test : FLAGS += -fdollars-in-identifiers6776_dollars_in_identifiers.test : FLAGS += -fdollars-in-identifiers
68ifneq (-$(CONFIG_WIN32)-,-yes-)
6922_floating_point.test: FLAGS += -lm
7024_math_library.test: FLAGS += -lm
71endif
72 72
73# These tests run several snippets from the same file one by one73# These tests run several snippets from the same file one by one
7460_errors_and_warnings.test : FLAGS += -dt7460_errors_and_warnings.test : FLAGS += -dt

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