Skip to content

Commit 9d2068c

Browse files
author
grischka
committed
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
1 parent ca061f3 commit 9d2068c

File tree

9 files changed

+27
-24
lines changed

9 files changed

+27
-24
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ test-install: tccdefs_.h
491491
@$(MAKE) -C tests TESTINSTALL=yes #_all
492492

493493
clean:
494-
@rm -f tcc$(EXESUF) tcc_c$(EXESUF) tcc_p$(EXESUF) *-tcc$(EXESUF)
494+
@rm -f tcc *-tcc tcc_p tcc_c
495495
@rm -f tags ETAGS *.o *.a *.so* *.out *.log lib*.def *.exe *.dll
496496
@rm -f a.out *.dylib *_.h *.pod *.tcov
497497
@$(MAKE) -s -C lib $@

libtcc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,9 +2203,10 @@ PUB_FUNC void tcc_print_stats(TCCState *s1, unsigned total_time)
22032203
#ifdef TCC_IS_NATIVE
22042204
if (s1->run_size) {
22052205
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;
22072208
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);
22092210
}
22102211
#endif
22112212
fprintf(stderr, " %d max (bytes)\n", mem_max_size);

tcc.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ extern long double strtold (const char *__nptr, char **__endptr);
135135
/* include file debug */
136136
/* #define INC_DEBUG */
137137
/* memory leak debug (only for single threaded usage) */
138-
/* #define MEM_DEBUG */
138+
/* #define MEM_DEBUG 1,2,3 */
139139
/* assembler debug */
140140
/* #define ASM_DEBUG */
141141

@@ -987,8 +987,7 @@ struct TCCState {
987987

988988
#ifdef TCC_IS_NATIVE
989989
const char *run_main; /* entry for tcc_run() */
990-
void *run_mem; /* runtime_memory */
991-
void *run_ptr; /* ptr to runtime_memory (aligned) */
990+
void *run_ptr; /* runtime_memory */
992991
unsigned run_size; /* size of runtime_memory */
993992
#ifdef _WIN64
994993
void *run_function_table; /* unwind data */

tccpe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ static int pe_write(struct pe_info *pe)
732732

733733
memcpy(psh->Name, sh_name, umin(strlen(sh_name), sizeof psh->Name));
734734
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);
736736

737737
psh->Characteristics = si->pe_flags;
738738
psh->VirtualAddress = addr;

tccpp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ ST_FUNC void expect(const char *msg)
127127
#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
128128
#define TAL_DEBUG_PARAMS
129129
#else
130-
#define TAL_DEBUG 1
130+
#define TAL_DEBUG MEM_DEBUG
131131
//#define TAL_INFO 1 /* collect and dump allocators stats */
132132
#define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
133133
#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
@@ -189,7 +189,7 @@ static void tal_delete(TinyAlloc *al)
189189
al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
190190
(al->peak_p - al->buffer) * 100.0 / al->size);
191191
#endif
192-
#ifdef TAL_DEBUG
192+
#if TAL_DEBUG && TAL_DEBUG != 3 /* do not check TAL leaks with -DMEM_DEBUG=3 */
193193
if (al->nb_allocs > 0) {
194194
uint8_t *p;
195195
fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n",
@@ -203,7 +203,7 @@ static void tal_delete(TinyAlloc *al)
203203
}
204204
p += header->size + sizeof(tal_header_t);
205205
}
206-
#if MEM_DEBUG-0 == 2
206+
#if TAL_DEBUG == 2
207207
exit(2);
208208
#endif
209209
}

tccrun.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ static int rt_mem(TCCState *s1, int size)
128128
return tcc_error_noabort("tccrun: could not map memory");
129129
ptr_diff = (char*)prw - (char*)ptr; /* = size; */
130130
//printf("map %p %p %p\n", ptr, prw, (void*)ptr_diff);
131+
size *= 2;
131132
#else
132-
s1->run_mem = tcc_malloc(size + PAGESIZE); /* one extra page to align malloc memory */
133-
ptr = (void*)PAGEALIGN(s1->run_mem);
133+
ptr = tcc_malloc(size += PAGESIZE); /* one extra page to align malloc memory */
134134
#endif
135135
s1->run_ptr = ptr;
136136
s1->run_size = size;
@@ -188,14 +188,14 @@ ST_FUNC void tcc_run_free(TCCState *s1)
188188
st_unlink(s1);
189189
size = s1->run_size;
190190
#ifdef HAVE_SELINUX
191-
munmap(ptr, size * 2);
191+
munmap(ptr, size);
192192
#else
193193
/* 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*/);
195195
# ifdef _WIN64
196196
win64_del_function_table(s1->run_function_table);
197197
# endif
198-
tcc_free(s1->run_mem);
198+
tcc_free(ptr);
199199
#endif
200200
}
201201

@@ -286,8 +286,9 @@ static void cleanup_sections(TCCState *s1)
286286
}
287287

288288
/* ------------------------------------------------------------- */
289-
/* 0 = .text rwx other rw */
290-
/* 1 = .text rx .rdata r .data/.bss rw */
289+
/* 0 = .text rwx other rw (memory >= 2 pages a 4096 bytes) */
290+
/* 1 = .text rx other rw (memory >= 3 pages) */
291+
/* 2 = .text rx .rdata ro .data/.bss rw (memory >= 4 pages) */
291292

292293
/* Some targets implement secutiry options that do not allow write in
293294
executable code. These targets need CONFIG_RUNMEM_RO=1.
@@ -370,7 +371,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr, unsigned ptr_diff)
370371
align = 64;
371372
#endif
372373
/* start new page for different permissions */
373-
if (CONFIG_RUNMEM_RO || k == 0)
374+
if (k <= CONFIG_RUNMEM_RO)
374375
align = PAGESIZE;
375376
}
376377
s->sh_addralign = align;
@@ -387,7 +388,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr, unsigned ptr_diff)
387388
continue;
388389
#endif
389390
f = k;
390-
if (CONFIG_RUNMEM_RO == 0) {
391+
if (f >= CONFIG_RUNMEM_RO) {
391392
if (f != 0)
392393
continue;
393394
f = 3; /* change only SHF_EXECINSTR to rwx */

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ cache: tcc_g
342342
clean:
343343
rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc
344344
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
346346
rm -f ex? tcc_g weaktest.*.txt *.def *.pdb *.obj libtcc_test_mt
347347
@$(MAKE) -C tests2 $@
348348
@$(MAKE) -C pp $@

tests/tcctest.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3760,12 +3760,14 @@ void asm_dot_test(void)
37603760

37613761
void asm_pcrel_test(void)
37623762
{
3763+
#if defined(__i386__)
37633764
unsigned o1, o2;
37643765
/* subtract text-section label from forward or other-section label */
37653766
asm("1: mov $2f-1b,%%eax; mov %%eax,%0" : "=m"(o1));
37663767
/* verify ... */
37673768
asm("2: mov $2b,%%eax; sub $1b,%%eax; mov %%eax,%0" : "=m"(o2));
37683769
printf("%s : %x\n", __FUNCTION__, o1 - o2); /* should be zero */
3770+
#endif
37693771
}
37703772

37713773
void asm_test(void)

tests/tests2/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ endif
5555

5656
# Some tests might need arguments
5757
ARGS =
58-
ifneq (-$(CONFIG_WIN32)-,-yes-)
59-
22_floating_point.test: FLAGS += -lm
60-
24_math_library.test: FLAGS += -lm
61-
endif
6258
31_args.test : ARGS = arg1 arg2 arg3 arg4 arg5
6359
46_grep.test : ARGS = '[^* ]*[:a:d: ]+\:\*-/: $$' $(SRC)/46_grep.c
6460

@@ -69,6 +65,10 @@ NORUN =
6965
# Some tests might need different flags
7066
FLAGS =
7167
76_dollars_in_identifiers.test : FLAGS += -fdollars-in-identifiers
68+
ifneq (-$(CONFIG_WIN32)-,-yes-)
69+
22_floating_point.test: FLAGS += -lm
70+
24_math_library.test: FLAGS += -lm
71+
endif
7272

7373
# These tests run several snippets from the same file one by one
7474
60_errors_and_warnings.test : FLAGS += -dt

0 commit comments

Comments
 (0)