History

added computed gotos - better runtime error handling

bellard committed 24 years ago

Commit 804d0c81 parent fde824b1 file · +179 −65Browse files

Unified · Split

tcc.c+179−65
@@ -190,7 +190,7 @@ typedef struct AttributeDef { · 189 unmodified lines
190190 #define MACRO_OBJ 0 /* object like macro */
191191 #define MACRO_FUNC 1 /* function like macro */
192192
193−/* field 'Sym.t' for labels */
193+/* field 'Sym.r' for labels */
194194 #define LABEL_FORWARD 1 /* label is forward defined */
195195
196196 /* type_decl() types */
@@ -305,6 +305,10 @@ int gnu_ext = 1; · 108 unmodified lines
305305 /* use Tiny C extensions */
306306 int tcc_ext = 1;
307307
308+/* max number of callers shown if error */
309+static int num_callers = 6;
310+static const char **rt_bound_error_msg;
311+
308312 /* XXX: suppress that ASAP */
309313 static struct TCCState *tcc_state;
310314
@@ -572,7 +576,7 @@ int parse_btype(int *type_ptr, AttributeDef *ad); · 261 unmodified lines
572576 int type_decl(AttributeDef *ad, int *v, int t, int td);
573577
574578 void error(const char *fmt, ...);
575−void rt_error(unsigned long pc, const char *fmt, ...);
579+void rt_error(struct ucontext *uc, const char *fmt, ...);
576580 void vpushi(int v);
577581 void vset(int t, int r, int v);
578582 void type_to_str(char *buf, int buf_size,
@@ -910,6 +914,7 @@ static void put_extern_sym(Sym *sym, Section *section, · 331 unmodified lines
910914 name = get_tok_str(sym->v, NULL);
911915 #ifdef CONFIG_TCC_BCHECK
912916 if (do_bounds_check) {
917+ /* XXX: avoid doing that for statics ? */
913918 /* if bound checking is activated, we change some function
914919 names by adding the "__bound" prefix */
915920 switch(sym->v) {
@@ -5475,8 +5480,7 @@ static void unary(void) · 4559 unmodified lines
54755480 test_lvalue();
54765481 vtop->t = mk_pointer(vtop->t);
54775482 gaddrof();
5478− } else
5479− if (t == '!') {
5483+ } else if (t == '!') {
54805484 unary();
54815485 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
54825486 vtop->c.i = !vtop->c.i;
@@ -5484,21 +5488,18 @@ static void unary(void) · 1 unmodified line
54845488 vtop->c.i = vtop->c.i ^ 1;
54855489 else
54865490 vset(VT_INT, VT_JMP, gtst(1, 0));
5487− } else
5488− if (t == '~') {
5491+ } else if (t == '~') {
54895492 unary();
54905493 vpushi(-1);
54915494 gen_op('^');
5492− } else
5493− if (t == '+') {
5495+ } else if (t == '+') {
54945496 /* in order to force cast, we add zero */
54955497 unary();
54965498 if ((vtop->t & VT_BTYPE) == VT_PTR)
54975499 error("pointer not accepted for unary plus");
54985500 vpushi(0);
54995501 gen_op('+');
5500− } else
5501− if (t == TOK_SIZEOF || t == TOK_ALIGNOF) {
5502+ } else if (t == TOK_SIZEOF || t == TOK_ALIGNOF) {
55025503 if (tok == '(') {
55035504 ft = parse_expr_type();
55045505 } else {
@@ -5509,16 +5510,28 @@ static void unary(void) · 4 unmodified lines
55095510 vpushi(size);
55105511 else
55115512 vpushi(align);
5512− } else
5513− if (t == TOK_INC || t == TOK_DEC) {
5513+ } else if (t == TOK_INC || t == TOK_DEC) {
55145514 unary();
55155515 inc(0, t);
55165516 } else if (t == '-') {
55175517 vpushi(0);
55185518 unary();
55195519 gen_op('-');
5520− } else
5521− {
5520+ } else if (t == TOK_LAND && gnu_ext) {
5521+ /* allow to take the address of a label */
5522+ if (tok < TOK_UIDENT)
5523+ expect("label identifier");
5524+ s = sym_find1(&label_stack, tok);
5525+ if (!s) {
5526+ s = sym_push1(&label_stack, tok, 0, 0);
5527+ s->r = LABEL_FORWARD;
5528+ }
5529+ if (!s->t)
5530+ s->t = mk_pointer(VT_VOID) | VT_STATIC;
5531+ vset(s->t, VT_CONST | VT_SYM, 0);
5532+ vtop->sym = s;
5533+ next();
5534+ } else {
55225535 if (t < TOK_UIDENT)
55235536 expect("identifier");
55245537 s = sym_find(t);
@@ -6200,16 +6213,29 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 675 unmodified lines
62006213 } else
62016214 if (tok == TOK_GOTO) {
62026215 next();
6203− s = sym_find1(&label_stack, tok);
6204− /* put forward definition if needed */
6205− if (!s)
6206− s = sym_push1(&label_stack, tok, LABEL_FORWARD, 0);
6207− /* label already defined */
6208− if (s->t & LABEL_FORWARD)
6209− s->c = gjmp(s->c);
6210− else
6211− gjmp_addr(s->c);
6212− next();
6216+ if (tok == '*' && gnu_ext) {
6217+ /* computed goto */
6218+ next();
6219+ gexpr();
6220+ if ((vtop->t & VT_BTYPE) != VT_PTR)
6221+ expect("pointer");
6222+ ggoto();
6223+ } else if (tok >= TOK_UIDENT) {
6224+ s = sym_find1(&label_stack, tok);
6225+ /* put forward definition if needed */
6226+ if (!s) {
6227+ s = sym_push1(&label_stack, tok, 0, 0);
6228+ s->r = LABEL_FORWARD;
6229+ }
6230+ /* label already defined */
6231+ if (s->r & LABEL_FORWARD)
6232+ s->next = (void *)gjmp((long)s->next);
6233+ else
6234+ gjmp_addr((long)s->next);
6235+ next();
6236+ } else {
6237+ expect("label identifier");
6238+ }
62136239 skip(';');
62146240 } else {
62156241 b = is_label();
@@ -6217,14 +6243,14 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 1 unmodified line
62176243 /* label case */
62186244 s = sym_find1(&label_stack, b);
62196245 if (s) {
6220− if (!(s->t & LABEL_FORWARD))
6246+ if (!(s->r & LABEL_FORWARD))
62216247 error("multiple defined label");
6222− gsym(s->c);
6223− s->c = ind;
6224− s->t = 0;
6248+ gsym((long)s->next);
62256249 } else {
6226− sym_push1(&label_stack, b, 0, ind);
6250+ s = sym_push1(&label_stack, b, 0, 0);
62276251 }
6252+ s->next = (void *)ind;
6253+ s->r = 0;
62286254 /* we accept this, but it is a mistake */
62296255 if (tok == '}')
62306256 warning("deprecated use of label at end of compound statement");
@@ -6997,6 +7023,22 @@ static void decl(int l) · 766 unmodified lines
69977023 gsym(rsym);
69987024 gfunc_epilog();
69997025 cur_text_section->data_offset = ind;
7026+ /* look if any labels are undefined. Define symbols if
7027+ '&&label' was used. */
7028+ {
7029+ Sym *s;
7030+ for(s = label_stack.top; s != NULL; s = s->prev) {
7031+ if (s->r & LABEL_FORWARD) {
7032+ error("label '%s' used but not defined",
7033+ get_tok_str(s->v, NULL));
7034+ }
7035+ if (s->c) {
7036+ /* define corresponding symbol. A size of
7037+ 1 is put. */
7038+ put_extern_sym(s, cur_text_section, (long)s->next, 1);
7039+ }
7040+ }
7041+ }
70007042 sym_pop(&label_stack, NULL); /* reset label stack */
70017043 sym_pop(&local_stack, NULL); /* reset local stack */
70027044 /* end of function */
@@ -7234,6 +7276,8 @@ static void rt_printline(unsigned long wanted_pc) · 231 unmodified lines
72347276 int incl_index, len, last_line_num, i;
72357277 const char *str, *p;
72367278
7279+ fprintf(stderr, "0x%08lx:", wanted_pc);
7280+
72377281 func_name[0] = '\0';
72387282 func_addr = 0;
72397283 incl_index = 0;
@@ -7247,6 +7291,10 @@ static void rt_printline(unsigned long wanted_pc) · 7 unmodified lines
72477291 /* function start or end */
72487292 case N_FUN:
72497293 if (sym->n_strx == 0) {
7294+ /* we test if between last line and end of function */
7295+ pc = sym->n_value + func_addr;
7296+ if (wanted_pc >= last_pc && wanted_pc < pc)
7297+ goto found;
72507298 func_name[0] = '\0';
72517299 func_addr = 0;
72527300 } else {
@@ -7300,31 +7348,98 @@ static void rt_printline(unsigned long wanted_pc) · 47 unmodified lines
73007348 }
73017349 sym++;
73027350 }
7303− /* did not find line number info: */
7304− fprintf(stderr, "(no debug info, pc=0x%08lx): ", wanted_pc);
7351+
7352+ /* second pass: we try symtab symbols (no line number info) */
7353+ incl_index = 0;
7354+ {
7355+ Elf32_Sym *sym, *sym_end;
7356+ int type;
7357+
7358+ sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
7359+ for(sym = (Elf32_Sym *)symtab_section->data + 1;
7360+ sym < sym_end;
7361+ sym++) {
7362+ type = ELF32_ST_TYPE(sym->st_info);
7363+ if (type == STT_FUNC) {
7364+ if (wanted_pc >= sym->st_value &&
7365+ wanted_pc < sym->st_value + sym->st_size) {
7366+ pstrcpy(last_func_name, sizeof(last_func_name),
7367+ strtab_section->data + sym->st_name);
7368+ goto found;
7369+ }
7370+ }
7371+ }
7372+ }
7373+ /* did not find any info: */
7374+ fprintf(stderr, " ???\n");
73057375 return;
73067376 found:
7307− for(i = 0; i < incl_index - 1; i++)
7308− fprintf(stderr, "In file included from %s\n",
7309− incl_files[i]);
7377+ if (last_func_name[0] != '\0') {
7378+ fprintf(stderr, " %s()", last_func_name);
7379+ }
73107380 if (incl_index > 0) {
7311− fprintf(stderr, "%s:%d: ",
7381+ fprintf(stderr, " (%s:%d",
73127382 incl_files[incl_index - 1], last_line_num);
7383+ for(i = incl_index - 2; i >= 0; i--)
7384+ fprintf(stderr, ", included from %s", incl_files[i]);
7385+ fprintf(stderr, ")");
73137386 }
7314− if (last_func_name[0] != '\0') {
7315− fprintf(stderr, "in function '%s()': ", last_func_name);
7387+ fprintf(stderr, "\n");
7388+}
7389+
7390+#ifdef __i386__
7391+
7392+#ifndef EIP
7393+#define EIP 14
7394+#define EBP 6
7395+#endif
7396+
7397+/* return the PC at frame level 'level'. Return non zero if not found */
7398+static int rt_get_caller_pc(unsigned long *paddr,
7399+ struct ucontext *uc, int level)
7400+{
7401+ unsigned long fp;
7402+ int i;
7403+
7404+ if (level == 0) {
7405+ *paddr = uc->uc_mcontext.gregs[EIP];
7406+ return 0;
7407+ } else {
7408+ fp = uc->uc_mcontext.gregs[EBP];
7409+ for(i=1;i<level;i++) {
7410+ /* XXX: check address validity with program info */
7411+ if (fp <= 0x1000 || fp >= 0xc0000000)
7412+ return -1;
7413+ fp = ((unsigned long *)fp)[0];
7414+ }
7415+ *paddr = ((unsigned long *)fp)[1];
7416+ return 0;
73167417 }
73177418 }
7419+#else
7420+#error add corresponding function
7421+#endif
73187422
73197423 /* emit a run time error at position 'pc' */
7320−void rt_error(unsigned long pc, const char *fmt, ...)
7424+void rt_error(struct ucontext *uc, const char *fmt, ...)
73217425 {
73227426 va_list ap;
7323− va_start(ap, fmt);
7427+ unsigned long pc;
7428+ int i;
73247429
7325− rt_printline(pc);
7430+ va_start(ap, fmt);
7431+ fprintf(stderr, "Runtime error: ");
73267432 vfprintf(stderr, fmt, ap);
73277433 fprintf(stderr, "\n");
7434+ for(i=0;i<num_callers;i++) {
7435+ if (rt_get_caller_pc(&pc, uc, i) < 0)
7436+ break;
7437+ if (i == 0)
7438+ fprintf(stderr, "at ");
7439+ else
7440+ fprintf(stderr, "by ");
7441+ rt_printline(pc);
7442+ }
73287443 exit(255);
73297444 va_end(ap);
73307445 }
@@ -7334,38 +7449,34 @@ void rt_error(unsigned long pc, const char *fmt, ...) · 3 unmodified lines
73347449 static void sig_error(int signum, siginfo_t *siginf, void *puc)
73357450 {
73367451 struct ucontext *uc = puc;
7337− unsigned long pc;
7338−
7339−#ifdef __i386__
7340− pc = uc->uc_mcontext.gregs[14];
7341−#else
7342−#error please put the right sigcontext field
7343−#endif
73447452
73457453 switch(signum) {
73467454 case SIGFPE:
73477455 switch(siginf->si_code) {
73487456 case FPE_INTDIV:
73497457 case FPE_FLTDIV:
7350− rt_error(pc, "division by zero");
7458+ rt_error(uc, "division by zero");
73517459 break;
73527460 default:
7353− rt_error(pc, "floating point exception");
7461+ rt_error(uc, "floating point exception");
73547462 break;
73557463 }
73567464 break;
73577465 case SIGBUS:
73587466 case SIGSEGV:
7359− rt_error(pc, "dereferencing invalid pointer");
7467+ if (rt_bound_error_msg && *rt_bound_error_msg)
7468+ rt_error(uc, *rt_bound_error_msg);
7469+ else
7470+ rt_error(uc, "dereferencing invalid pointer");
73607471 break;
73617472 case SIGILL:
7362− rt_error(pc, "illegal instruction");
7473+ rt_error(uc, "illegal instruction");
73637474 break;
73647475 case SIGABRT:
7365− rt_error(pc, "abort() called");
7476+ rt_error(uc, "abort() called");
73667477 break;
73677478 default:
7368− rt_error(pc, "caught signal %d", signum);
7479+ rt_error(uc, "caught signal %d", signum);
73697480 break;
73707481 }
73717482 exit(255);
@@ -7440,11 +7551,9 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 68 unmodified lines
74407551 #ifdef CONFIG_TCC_BCHECK
74417552 if (do_bounds_check) {
74427553 void (*bound_init)(void);
7443− void **bound_error_func;
74447554
74457555 /* set error function */
7446− bound_error_func = (void **)tcc_get_symbol(s1, "__bound_error_func");
7447− *bound_error_func = rt_error;
7556+ rt_bound_error_msg = (void *)tcc_get_symbol(s1, "__bound_error_msg");
74487557
74497558 /* XXX: use .init section so that it also work in binary ? */
74507559 bound_init = (void *)tcc_get_symbol(s1, "__bound_init");
@@ -7782,9 +7891,9 @@ int tcc_set_output_type(TCCState *s, int output_type) · 331 unmodified lines
77827891
77837892 void help(void)
77847893 {
7785− printf("tcc version 0.9.12 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
7894+ printf("tcc version 0.9.13 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
77867895 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
7787− " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"
7896+ " [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
77887897 " [--] infile1 [infile2... --] [infile_args...]\n"
77897898 "\n"
77907899 "General options:\n"
@@ -7798,17 +7907,18 @@ void help(void) · 7 unmodified lines
77987907 " -Idir add include path 'dir'\n"
77997908 " -Dsym[=val] define 'sym' with value 'val'\n"
78007909 " -Usym undefine 'sym'\n"
7801− "C compiler options:\n"
7802− " -g generate runtime debug info\n"
7803−#ifdef CONFIG_TCC_BCHECK
7804− " -b compile with built-in memory and bounds checker (implies -g)\n"
7805−#endif
78067910 "Linker options:\n"
78077911 " -Ldir add library path 'dir'\n"
78087912 " -llib link with dynamic or static library 'lib'\n"
78097913 " -shared generate a shared library\n"
78107914 " -static static linking\n"
78117915 " -r relocatable output\n"
7916+ "Debugger options:\n"
7917+ " -g generate runtime debug info\n"
7918+#ifdef CONFIG_TCC_BCHECK
7919+ " -b compile with built-in memory and bounds checker (implies -g)\n"
7920+#endif
7921+ " -bt N show N callers in stack traces\n"
78127922 );
78137923 }
78147924
@@ -7884,7 +7994,9 @@ int main(int argc, char **argv) · 69 unmodified lines
78847994 nb_libraries++;
78857995 } else if (!strcmp(r + 1, "bench")) {
78867996 do_bench = 1;
7887− } else
7997+ } else if (!strcmp(r + 1, "bt")) {
7998+ num_callers = atoi(argv[optind++]);
7999+ } else
78888000 #ifdef CONFIG_TCC_BCHECK
78898001 if (r[1] == 'b') {
78908002 do_bounds_check = 1;
@@ -7982,7 +8094,9 @@ int main(int argc, char **argv) · 91 unmodified lines
79828094 ret = tcc_run(s, argc - optind, argv + optind);
79838095 }
79848096 the_end:
7985− tcc_delete(s);
8097+ /* XXX: cannot do it with bound checking because of the malloc hooks */
8098+ if (!do_bounds_check)
8099+ tcc_delete(s);
79868100
79878101 #ifdef MEM_DEBUG
79888102 if (do_bench) {

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