History

suppressed some buffer overflows - moved function generation to cpu specific code (XXX: should have less cpu specific code for that)

bellard committed 24 years ago

Commit 14799da1 parent 8b41bc52 files · +105 −83Browse files

Unified · Split

2 files
  1. i386-gen.c +44 −0
  2. tcc.c +61 −83
i386-gen.c+44−0
@@ -72,6 +72,8 @@ typedef struct GFuncContext { · 71 unmodified lines
7272
7373 /******************************************************/
7474
75+static int *func_sub_sp_ptr;
76+
7577 void g(int c)
7678 {
7779 *(char *)ind++ = c;
@@ -355,6 +357,48 @@ void gfunc_call(GFuncContext *c) · 277 unmodified lines
355357 vtop--;
356358 }
357359
360+/* generate function prolog of type 't' */
361+void gfunc_prolog(int t)
362+{
363+ int addr, align, size, u;
364+ Sym *sym;
365+
366+ sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
367+ addr = 8;
368+ /* if the function returns a structure, then add an
369+ implicit pointer parameter */
370+ func_vt = sym->t;
371+ if ((func_vt & VT_BTYPE) == VT_STRUCT) {
372+ func_vc = addr;
373+ addr += 4;
374+ }
375+ /* define parameters */
376+ while ((sym = sym->next) != NULL) {
377+ u = sym->t;
378+ sym_push(sym->v & ~SYM_FIELD, u,
379+ VT_LOCAL | VT_LVAL, addr);
380+ size = type_size(u, &align);
381+ size = (size + 3) & ~3;
382+#ifdef FUNC_STRUCT_PARAM_AS_PTR
383+ /* structs are passed as pointer */
384+ if ((u & VT_BTYPE) == VT_STRUCT) {
385+ size = 4;
386+ }
387+#endif
388+ addr += size;
389+ }
390+ o(0xe58955); /* push %ebp, mov %esp, %ebp */
391+ func_sub_sp_ptr = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
392+}
393+
394+/* generate function epilog */
395+void gfunc_epilog(void)
396+{
397+ o(0xc3c9); /* leave, ret */
398+ *func_sub_sp_ptr = (-loc + 3) & -4; /* align local size to word &
399+ save local variables */
400+}
401+
358402 int gjmp(int t)
359403 {
360404 return psym(0xe9, t);
tcc.c+61−83
@@ -32,11 +32,6 @@ · 31 unmodified lines
3232 /* preprocessor debug */
3333 //#define PP_DEBUG
3434
35−/* these sizes are dummy for unix, because malloc() does not use
36− memory when the pages are not used */
37−#define TEXT_SIZE (4*1024*1024)
38−#define DATA_SIZE (4*1024*1024)
39−
4035 /* amount of virtual memory associate to a section (currently, we do
4136 not realloc them) */
4237 #define SECTION_VSIZE (1024 * 1024)
@@ -398,6 +393,8 @@ void gen_op(int op); · 355 unmodified lines
398393 void force_charshort_cast(int t);
399394 void gen_cast(int t);
400395 void vstore(void);
396+Sym *sym_find(int v);
397+Sym *sym_push(int v, int t, int r, int c);
401398
402399 /* type handling */
403400 int type_size(int t, int *a);
@@ -546,7 +543,7 @@ unsigned long long __ldtoull(long double a) · 142 unmodified lines
546543
547544 /********************************************************/
548545
549−/* copy a string and trucate it */
546+/* copy a string and truncate it */
550547 char *pstrcpy(char *buf, int buf_size, const char *s)
551548 {
552549 char *q, *q_end;
@@ -564,6 +561,16 @@ char *pstrcpy(char *buf, int buf_size, const char *s) · 11 unmodified lines
564561 return buf;
565562 }
566563
564+/* strcat and truncate */
565+char *pstrcat(char *buf, int buf_size, const char *s)
566+{
567+ int len;
568+ len = strlen(buf);
569+ if (len < buf_size)
570+ pstrcpy(buf + len, buf_size - len, s);
571+ return buf;
572+}
573+
567574 Section *new_section(const char *name)
568575 {
569576 Section *sec, **psec;
@@ -1235,8 +1242,7 @@ void preprocess(void) · 665 unmodified lines
12351242 next();
12361243 if (tok != TOK_STR)
12371244 error("#include syntax error");
1238− /* XXX: buffer overflow */
1239− strcpy(buf, get_tok_str(tok, &tokc));
1245+ pstrcpy(buf, sizeof(buf), get_tok_str(tok, &tokc));
12401246 c = '\"';
12411247 }
12421248 /* eat all spaces and comments after include */
@@ -1248,14 +1254,15 @@ void preprocess(void) · 5 unmodified lines
12481254 error("memory full");
12491255 if (c == '\"') {
12501256 /* first search in current dir if "header.h" */
1251− /* XXX: buffer overflow */
12521257 size = 0;
12531258 p = strrchr(filename, '/');
12541259 if (p)
12551260 size = p + 1 - filename;
1261+ if (size > sizeof(buf1) - 1)
1262+ size = sizeof(buf1) - 1;
12561263 memcpy(buf1, filename, size);
12571264 buf1[size] = '\0';
1258− strcat(buf1, buf);
1265+ pstrcat(buf1, sizeof(buf1), buf);
12591266 f = fopen(buf1, "r");
12601267 if (f)
12611268 goto found;
@@ -1850,13 +1857,12 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 588 unmodified lines
18501857 if (s) {
18511858 token_buf[0] = '\0';
18521859 st = (int *)s->c;
1853− /* XXX: buffer overflow */
18541860 notfirst = 0;
18551861 while (*st) {
18561862 if (notfirst)
1857− strcat(token_buf, " ");
1863+ pstrcat(token_buf, sizeof(token_buf), " ");
18581864 t = tok_get(&st, &cval);
1859− strcat(token_buf, get_tok_str(t, &cval));
1865+ pstrcat(token_buf, sizeof(token_buf), get_tok_str(t, &cval));
18601866 notfirst = 1;
18611867 }
18621868 #ifdef PP_DEBUG
@@ -1918,11 +1924,10 @@ int *macro_twosharps(int *macro_str) · 55 unmodified lines
19181924 ident + ident or ident + number */
19191925 if (tok >= TOK_IDENT &&
19201926 (t >= TOK_IDENT || t == TOK_CINT)) {
1921− /* XXX: buffer overflow */
19221927 p = get_tok_str(tok, &tokc);
1923− strcpy(token_buf, p);
1928+ pstrcpy(token_buf, sizeof(token_buf), p);
19241929 p = get_tok_str(t, &cval);
1925− strcat(token_buf, p);
1930+ pstrcat(token_buf, sizeof(token_buf), p);
19261931 ts = tok_alloc(token_buf, 0);
19271932 tok = ts->tok; /* modify current token */
19281933 } else {
@@ -3241,87 +3246,90 @@ int is_compatible_types(int t1, int t2) · 1312 unmodified lines
32413246 printed in the type */
32423247 /* XXX: union */
32433248 /* XXX: add array and function pointers */
3244−/* XXX: buffer overflows */
32453249 void type_to_str(char *buf, int buf_size,
32463250 int t, const char *varstr)
32473251 {
32483252 int bt, v;
32493253 Sym *s, *sa;
32503254 char buf1[256];
3255+ const char *tstr;
32513256
32523257 t = t & VT_TYPE;
32533258 bt = t & VT_BTYPE;
32543259 buf[0] = '\0';
32553260 if (t & VT_UNSIGNED)
3256− strcat(buf, "unsigned ");
3261+ pstrcat(buf, buf_size, "unsigned ");
32573262 switch(bt) {
32583263 case VT_VOID:
3259− strcat(buf, "void");
3260− break;
3264+ tstr = "void";
3265+ goto add_tstr;
32613266 case VT_BOOL:
3262− strcat(buf, "_Bool");
3263− break;
3267+ tstr = "_Bool";
3268+ goto add_tstr;
32643269 case VT_BYTE:
3265− strcat(buf, "char");
3266− break;
3270+ tstr = "char";
3271+ goto add_tstr;
32673272 case VT_SHORT:
3268− strcat(buf, "short");
3269− break;
3273+ tstr = "short";
3274+ goto add_tstr;
32703275 case VT_INT:
3271− strcat(buf, "int");
3272− break;
3276+ tstr = "int";
3277+ goto add_tstr;
32733278 case VT_LONG:
3274− strcat(buf, "long");
3275− break;
3279+ tstr = "long";
3280+ goto add_tstr;
32763281 case VT_LLONG:
3277− strcat(buf, "long long");
3278− break;
3282+ tstr = "long long";
3283+ goto add_tstr;
32793284 case VT_FLOAT:
3280− strcat(buf, "float");
3281− break;
3285+ tstr = "float";
3286+ goto add_tstr;
32823287 case VT_DOUBLE:
3283− strcat(buf, "double");
3284− break;
3288+ tstr = "double";
3289+ goto add_tstr;
32853290 case VT_LDOUBLE:
3286− strcat(buf, "long double");
3291+ tstr = "long double";
3292+ add_tstr:
3293+ pstrcat(buf, buf_size, tstr);
32873294 break;
32883295 case VT_ENUM:
32893296 case VT_STRUCT:
32903297 if (bt == VT_STRUCT)
3291− strcat(buf, "struct ");
3298+ tstr = "struct ";
32923299 else
3293− strcat(buf, "enum ");
3300+ tstr = "enum ";
3301+ pstrcat(buf, buf_size, tstr);
32943302 v = (unsigned)t >> VT_STRUCT_SHIFT;
32953303 if (v >= SYM_FIRST_ANOM)
3296− strcat(buf, "<anonymous>");
3304+ pstrcat(buf, buf_size, "<anonymous>");
32973305 else
3298− strcat(buf, get_tok_str(v, NULL));
3306+ pstrcat(buf, buf_size, get_tok_str(v, NULL));
32993307 break;
33003308 case VT_FUNC:
33013309 s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
33023310 type_to_str(buf, buf_size, s->t, varstr);
3303− strcat(buf, "(");
3311+ pstrcat(buf, buf_size, "(");
33043312 sa = s->next;
33053313 while (sa != NULL) {
33063314 type_to_str(buf1, sizeof(buf1), sa->t, NULL);
3307− strcat(buf, buf1);
3315+ pstrcat(buf, buf_size, buf1);
33083316 sa = sa->next;
33093317 if (sa)
3310− strcat(buf, ", ");
3318+ pstrcat(buf, buf_size, ", ");
33113319 }
3312− strcat(buf, ")");
3320+ pstrcat(buf, buf_size, ")");
33133321 goto no_var;
33143322 case VT_PTR:
33153323 s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
3316− strcpy(buf1, "*");
3324+ pstrcpy(buf1, sizeof(buf1), "*");
33173325 if (varstr)
3318− strcat(buf1, varstr);
3326+ pstrcat(buf1, sizeof(buf1), varstr);
33193327 type_to_str(buf, buf_size, s->t, buf1);
33203328 goto no_var;
33213329 }
33223330 if (varstr) {
3323− strcat(buf, " ");
3324− strcat(buf, varstr);
3331+ pstrcat(buf, buf_size, " ");
3332+ pstrcat(buf, buf_size, varstr);
33253333 }
33263334 no_var: ;
33273335 }
@@ -5147,7 +5155,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 1819 unmodified lines
51475155 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
51485156 void decl(int l)
51495157 {
5150− int *a, t, b, v, addr, has_init, size, align, r, u;
5158+ int t, b, v, addr, has_init, r;
51515159 Sym *sym;
51525160 AttributeDef ad;
51535161
@@ -5207,41 +5215,12 @@ void decl(int l) · 53 unmodified lines
52075215 funcname = get_tok_str(v, NULL);
52085216 /* push a dummy symbol to enable local sym storage */
52095217 sym_push1(&local_stack, 0, 0, 0);
5210− /* define parameters */
5211− sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
5212− /* XXX: the following is x86 dependant -> move it to
5213− x86 code gen */
5214− addr = 8;
5215− /* if the function returns a structure, then add an
5216− implicit pointer parameter */
5217− func_vt = sym->t;
5218− if ((func_vt & VT_BTYPE) == VT_STRUCT) {
5219− func_vc = addr;
5220− addr += 4;
5221− }
5222− while ((sym = sym->next) != NULL) {
5223− u = sym->t;
5224− sym_push(sym->v & ~SYM_FIELD, u,
5225− VT_LOCAL | VT_LVAL, addr);
5226− size = type_size(u, &align);
5227− size = (size + 3) & ~3;
5228−#ifdef FUNC_STRUCT_PARAM_AS_PTR
5229− /* structs are passed as pointer */
5230− if ((u & VT_BTYPE) == VT_STRUCT) {
5231− size = 4;
5232− }
5233−#endif
5234− addr += size;
5235− }
5218+ gfunc_prolog(t);
52365219 loc = 0;
5237− o(0xe58955); /* push %ebp, mov %esp, %ebp */
5238− a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
52395220 rsym = 0;
52405221 block(NULL, NULL, NULL, NULL, 0);
52415222 gsym(rsym);
5242− o(0xc3c9); /* leave, ret */
5243− *a = (-loc + 3) & -4; /* align local size to word &
5244− save local variables */
5223+ gfunc_epilog();
52455224 cur_text_section->data_ptr = (unsigned char *)ind;
52465225 sym_pop(&label_stack, NULL); /* reset label stack */
52475226 sym_pop(&local_stack, NULL); /* reset local stack */
@@ -5398,8 +5377,7 @@ void define_symbol(const char *sym) · 150 unmodified lines
53985377
53995378 p = strchr(sym, '=');
54005379 if (!p) {
5401− /* XXX: buffer overflow */
5402− strcpy(buf, sym);
5380+ pstrcpy(buf, sizeof(buf), sym);
54035381 p = "1";
54045382 } else {
54055383 len = p - sym;

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