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
72 72
73/******************************************************/73/******************************************************/
74 74
75static int *func_sub_sp_ptr;
76
75void g(int c)77void g(int c)
76{78{
77 *(char *)ind++ = c;79 *(char *)ind++ = c;
@@ -355,6 +357,48 @@ void gfunc_call(GFuncContext *c) · 277 unmodified lines
355 vtop--;357 vtop--;
356}358}
357 359
360/* generate function prolog of type 't' */
361void 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 */
395void 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
358int gjmp(int t)402int gjmp(int t)
359{403{
360 return psym(0xe9, t);404 return psym(0xe9, t);
tcc.c+61−83
@@ -32,11 +32,6 @@ · 31 unmodified lines
32/* preprocessor debug */32/* preprocessor debug */
33//#define PP_DEBUG33//#define PP_DEBUG
34 34
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
40/* amount of virtual memory associate to a section (currently, we do35/* amount of virtual memory associate to a section (currently, we do
41 not realloc them) */36 not realloc them) */
42#define SECTION_VSIZE (1024 * 1024)37#define SECTION_VSIZE (1024 * 1024)
@@ -398,6 +393,8 @@ void gen_op(int op); · 355 unmodified lines
398void force_charshort_cast(int t);393void force_charshort_cast(int t);
399void gen_cast(int t);394void gen_cast(int t);
400void vstore(void);395void vstore(void);
396Sym *sym_find(int v);
397Sym *sym_push(int v, int t, int r, int c);
401 398
402/* type handling */399/* type handling */
403int type_size(int t, int *a);400int type_size(int t, int *a);
@@ -546,7 +543,7 @@ unsigned long long __ldtoull(long double a) · 142 unmodified lines
546 543
547/********************************************************/544/********************************************************/
548 545
549/* copy a string and trucate it */546/* copy a string and truncate it */
550char *pstrcpy(char *buf, int buf_size, const char *s)547char *pstrcpy(char *buf, int buf_size, const char *s)
551{548{
552 char *q, *q_end;549 char *q, *q_end;
@@ -564,6 +561,16 @@ char *pstrcpy(char *buf, int buf_size, const char *s) · 11 unmodified lines
564 return buf;561 return buf;
565}562}
566 563
564/* strcat and truncate */
565char *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
567Section *new_section(const char *name)574Section *new_section(const char *name)
568{575{
569 Section *sec, **psec;576 Section *sec, **psec;
@@ -1235,8 +1242,7 @@ void preprocess(void) · 665 unmodified lines
1235 next();1242 next();
1236 if (tok != TOK_STR)1243 if (tok != TOK_STR)
1237 error("#include syntax error");1244 error("#include syntax error");
1238 /* XXX: buffer overflow */1245 pstrcpy(buf, sizeof(buf), get_tok_str(tok, &tokc));
1239 strcpy(buf, get_tok_str(tok, &tokc));
1240 c = '\"';1246 c = '\"';
1241 }1247 }
1242 /* eat all spaces and comments after include */1248 /* eat all spaces and comments after include */
@@ -1248,14 +1254,15 @@ void preprocess(void) · 5 unmodified lines
1248 error("memory full");1254 error("memory full");
1249 if (c == '\"') {1255 if (c == '\"') {
1250 /* first search in current dir if "header.h" */1256 /* first search in current dir if "header.h" */
1251 /* XXX: buffer overflow */
1252 size = 0;1257 size = 0;
1253 p = strrchr(filename, '/');1258 p = strrchr(filename, '/');
1254 if (p) 1259 if (p)
1255 size = p + 1 - filename;1260 size = p + 1 - filename;
1261 if (size > sizeof(buf1) - 1)
1262 size = sizeof(buf1) - 1;
1256 memcpy(buf1, filename, size);1263 memcpy(buf1, filename, size);
1257 buf1[size] = '\0';1264 buf1[size] = '\0';
1258 strcat(buf1, buf);1265 pstrcat(buf1, sizeof(buf1), buf);
1259 f = fopen(buf1, "r");1266 f = fopen(buf1, "r");
1260 if (f)1267 if (f)
1261 goto found;1268 goto found;
@@ -1850,13 +1857,12 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 588 unmodified lines
1850 if (s) {1857 if (s) {
1851 token_buf[0] = '\0';1858 token_buf[0] = '\0';
1852 st = (int *)s->c;1859 st = (int *)s->c;
1853 /* XXX: buffer overflow */
1854 notfirst = 0;1860 notfirst = 0;
1855 while (*st) {1861 while (*st) {
1856 if (notfirst)1862 if (notfirst)
1857 strcat(token_buf, " ");1863 pstrcat(token_buf, sizeof(token_buf), " ");
1858 t = tok_get(&st, &cval);1864 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));
1860 notfirst = 1;1866 notfirst = 1;
1861 }1867 }
1862#ifdef PP_DEBUG1868#ifdef PP_DEBUG
@@ -1918,11 +1924,10 @@ int *macro_twosharps(int *macro_str) · 55 unmodified lines
1918 ident + ident or ident + number */1924 ident + ident or ident + number */
1919 if (tok >= TOK_IDENT && 1925 if (tok >= TOK_IDENT &&
1920 (t >= TOK_IDENT || t == TOK_CINT)) {1926 (t >= TOK_IDENT || t == TOK_CINT)) {
1921 /* XXX: buffer overflow */
1922 p = get_tok_str(tok, &tokc);1927 p = get_tok_str(tok, &tokc);
1923 strcpy(token_buf, p);1928 pstrcpy(token_buf, sizeof(token_buf), p);
1924 p = get_tok_str(t, &cval);1929 p = get_tok_str(t, &cval);
1925 strcat(token_buf, p);1930 pstrcat(token_buf, sizeof(token_buf), p);
1926 ts = tok_alloc(token_buf, 0);1931 ts = tok_alloc(token_buf, 0);
1927 tok = ts->tok; /* modify current token */1932 tok = ts->tok; /* modify current token */
1928 } else {1933 } else {
@@ -3241,87 +3246,90 @@ int is_compatible_types(int t1, int t2) · 1312 unmodified lines
3241 printed in the type */3246 printed in the type */
3242/* XXX: union */3247/* XXX: union */
3243/* XXX: add array and function pointers */3248/* XXX: add array and function pointers */
3244/* XXX: buffer overflows */
3245void type_to_str(char *buf, int buf_size, 3249void type_to_str(char *buf, int buf_size,
3246 int t, const char *varstr)3250 int t, const char *varstr)
3247{3251{
3248 int bt, v;3252 int bt, v;
3249 Sym *s, *sa;3253 Sym *s, *sa;
3250 char buf1[256];3254 char buf1[256];
3255 const char *tstr;
3251 3256
3252 t = t & VT_TYPE;3257 t = t & VT_TYPE;
3253 bt = t & VT_BTYPE;3258 bt = t & VT_BTYPE;
3254 buf[0] = '\0';3259 buf[0] = '\0';
3255 if (t & VT_UNSIGNED)3260 if (t & VT_UNSIGNED)
3256 strcat(buf, "unsigned ");3261 pstrcat(buf, buf_size, "unsigned ");
3257 switch(bt) {3262 switch(bt) {
3258 case VT_VOID:3263 case VT_VOID:
3259 strcat(buf, "void");3264 tstr = "void";
3260 break;3265 goto add_tstr;
3261 case VT_BOOL:3266 case VT_BOOL:
3262 strcat(buf, "_Bool");3267 tstr = "_Bool";
3263 break;3268 goto add_tstr;
3264 case VT_BYTE:3269 case VT_BYTE:
3265 strcat(buf, "char");3270 tstr = "char";
3266 break;3271 goto add_tstr;
3267 case VT_SHORT:3272 case VT_SHORT:
3268 strcat(buf, "short");3273 tstr = "short";
3269 break;3274 goto add_tstr;
3270 case VT_INT:3275 case VT_INT:
3271 strcat(buf, "int");3276 tstr = "int";
3272 break;3277 goto add_tstr;
3273 case VT_LONG:3278 case VT_LONG:
3274 strcat(buf, "long");3279 tstr = "long";
3275 break;3280 goto add_tstr;
3276 case VT_LLONG:3281 case VT_LLONG:
3277 strcat(buf, "long long");3282 tstr = "long long";
3278 break;3283 goto add_tstr;
3279 case VT_FLOAT:3284 case VT_FLOAT:
3280 strcat(buf, "float");3285 tstr = "float";
3281 break;3286 goto add_tstr;
3282 case VT_DOUBLE:3287 case VT_DOUBLE:
3283 strcat(buf, "double");3288 tstr = "double";
3284 break;3289 goto add_tstr;
3285 case VT_LDOUBLE:3290 case VT_LDOUBLE:
3286 strcat(buf, "long double");3291 tstr = "long double";
3292 add_tstr:
3293 pstrcat(buf, buf_size, tstr);
3287 break;3294 break;
3288 case VT_ENUM:3295 case VT_ENUM:
3289 case VT_STRUCT:3296 case VT_STRUCT:
3290 if (bt == VT_STRUCT)3297 if (bt == VT_STRUCT)
3291 strcat(buf, "struct ");3298 tstr = "struct ";
3292 else3299 else
3293 strcat(buf, "enum ");3300 tstr = "enum ";
3301 pstrcat(buf, buf_size, tstr);
3294 v = (unsigned)t >> VT_STRUCT_SHIFT;3302 v = (unsigned)t >> VT_STRUCT_SHIFT;
3295 if (v >= SYM_FIRST_ANOM)3303 if (v >= SYM_FIRST_ANOM)
3296 strcat(buf, "<anonymous>");3304 pstrcat(buf, buf_size, "<anonymous>");
3297 else3305 else
3298 strcat(buf, get_tok_str(v, NULL));3306 pstrcat(buf, buf_size, get_tok_str(v, NULL));
3299 break;3307 break;
3300 case VT_FUNC:3308 case VT_FUNC:
3301 s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);3309 s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
3302 type_to_str(buf, buf_size, s->t, varstr);3310 type_to_str(buf, buf_size, s->t, varstr);
3303 strcat(buf, "(");3311 pstrcat(buf, buf_size, "(");
3304 sa = s->next;3312 sa = s->next;
3305 while (sa != NULL) {3313 while (sa != NULL) {
3306 type_to_str(buf1, sizeof(buf1), sa->t, NULL);3314 type_to_str(buf1, sizeof(buf1), sa->t, NULL);
3307 strcat(buf, buf1);3315 pstrcat(buf, buf_size, buf1);
3308 sa = sa->next;3316 sa = sa->next;
3309 if (sa)3317 if (sa)
3310 strcat(buf, ", ");3318 pstrcat(buf, buf_size, ", ");
3311 }3319 }
3312 strcat(buf, ")");3320 pstrcat(buf, buf_size, ")");
3313 goto no_var;3321 goto no_var;
3314 case VT_PTR:3322 case VT_PTR:
3315 s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);3323 s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
3316 strcpy(buf1, "*");3324 pstrcpy(buf1, sizeof(buf1), "*");
3317 if (varstr)3325 if (varstr)
3318 strcat(buf1, varstr);3326 pstrcat(buf1, sizeof(buf1), varstr);
3319 type_to_str(buf, buf_size, s->t, buf1);3327 type_to_str(buf, buf_size, s->t, buf1);
3320 goto no_var;3328 goto no_var;
3321 }3329 }
3322 if (varstr) {3330 if (varstr) {
3323 strcat(buf, " ");3331 pstrcat(buf, buf_size, " ");
3324 strcat(buf, varstr);3332 pstrcat(buf, buf_size, varstr);
3325 }3333 }
3326 no_var: ;3334 no_var: ;
3327}3335}
@@ -5147,7 +5155,7 @@ int decl_initializer_alloc(int t, AttributeDef *ad, int r, int has_init) · 1819 unmodified lines
5147/* 'l' is VT_LOCAL or VT_CONST to define default storage type */5155/* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5148void decl(int l)5156void decl(int l)
5149{5157{
5150 int *a, t, b, v, addr, has_init, size, align, r, u;5158 int t, b, v, addr, has_init, r;
5151 Sym *sym;5159 Sym *sym;
5152 AttributeDef ad;5160 AttributeDef ad;
5153 5161
@@ -5207,41 +5215,12 @@ void decl(int l) · 53 unmodified lines
5207 funcname = get_tok_str(v, NULL);5215 funcname = get_tok_str(v, NULL);
5208 /* push a dummy symbol to enable local sym storage */5216 /* push a dummy symbol to enable local sym storage */
5209 sym_push1(&local_stack, 0, 0, 0);5217 sym_push1(&local_stack, 0, 0, 0);
5210 /* define parameters */5218 gfunc_prolog(t);
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 }
5236 loc = 0;5219 loc = 0;
5237 o(0xe58955); /* push %ebp, mov %esp, %ebp */
5238 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
5239 rsym = 0;5220 rsym = 0;
5240 block(NULL, NULL, NULL, NULL, 0);5221 block(NULL, NULL, NULL, NULL, 0);
5241 gsym(rsym);5222 gsym(rsym);
5242 o(0xc3c9); /* leave, ret */5223 gfunc_epilog();
5243 *a = (-loc + 3) & -4; /* align local size to word &
5244 save local variables */
5245 cur_text_section->data_ptr = (unsigned char *)ind;5224 cur_text_section->data_ptr = (unsigned char *)ind;
5246 sym_pop(&label_stack, NULL); /* reset label stack */5225 sym_pop(&label_stack, NULL); /* reset label stack */
5247 sym_pop(&local_stack, NULL); /* reset local stack */5226 sym_pop(&local_stack, NULL); /* reset local stack */
@@ -5398,8 +5377,7 @@ void define_symbol(const char *sym) · 150 unmodified lines
5398 5377
5399 p = strchr(sym, '=');5378 p = strchr(sym, '=');
5400 if (!p) {5379 if (!p) {
5401 /* XXX: buffer overflow */5380 pstrcpy(buf, sizeof(buf), sym);
5402 strcpy(buf, sym);
5403 p = "1";5381 p = "1";
5404 } else {5382 } else {
5405 len = p - sym;5383 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