History

Fix a VLA problem

see testcase, reduced example of a situation reported by Kyryl Melekhin in https://github.com/kyx0r/neatvi/ . Problem is that setting up the VLA sp-save in a scope that isn't entered at runtime leaves traces of it in outer scopes that then try to restore the stack pointer from uninitialized slots.

Michael Matz committed 5 years ago

Commit fbef90a1 parent d6f2d584 files · +69 −12Browse files

Unified · Split

4 files
  1. tccgen.c +18 −9
  2. tests/tests2/123_vla_bug.c +40 −0
  3. tests/tests2/123_vla_bug.expect +5 −0
  4. tests/tests2/79_vla_continue.c +6 −3
tccgen.c+18−9
@@ -120,7 +120,7 @@ short nb_temp_local_vars; · 119 unmodified lines
120 120
121static struct scope {121static struct scope {
122 struct scope *prev;122 struct scope *prev;
123 struct { int loc, num; } vla;123 struct { int loc, locorig, num; } vla;
124 struct { Sym *s; int n; } cl;124 struct { Sym *s; int n; } cl;
125 int *bsym, *csym;125 int *bsym, *csym;
126 Sym *lstk, *llstk;126 Sym *lstk, *llstk;
@@ -7268,8 +7268,12 @@ static void vla_restore(int loc) · 7141 unmodified lines
7268 7268
7269static void vla_leave(struct scope *o)7269static void vla_leave(struct scope *o)
7270{7270{
7271 if (o->vla.num < cur_scope->vla.num)7271 struct scope *c = cur_scope, *v = NULL;
7272 vla_restore(o->vla.loc);7272 for (; c != o && c; c = c->prev)
7273 if (c->vla.num)
7274 v = c;
7275 if (v)
7276 vla_restore(v->vla.locorig);
7273}7277}
7274 7278
7275/* ------------------------------------------------------------------------- */7279/* ------------------------------------------------------------------------- */
@@ -7281,6 +7285,7 @@ void new_scope(struct scope *o) · 5 unmodified lines
7281 *o = *cur_scope;7285 *o = *cur_scope;
7282 o->prev = cur_scope;7286 o->prev = cur_scope;
7283 cur_scope = o;7287 cur_scope = o;
7288 cur_scope->vla.num = 0;
7284 7289
7285 /* record local declaration stack position */7290 /* record local declaration stack position */
7286 o->lstk = local_stack;7291 o->lstk = local_stack;
@@ -7600,7 +7605,8 @@ again: · 313 unmodified lines
7600 goto block_after_label;7605 goto block_after_label;
7601 7606
7602 } else if (t == TOK_GOTO) {7607 } else if (t == TOK_GOTO) {
7603 vla_restore(root_scope->vla.loc);7608 if (cur_scope->vla.num)
7609 vla_restore(cur_scope->vla.locorig);
7604 if (tok == '*' && gnu_ext) {7610 if (tok == '*' && gnu_ext) {
7605 /* computed goto */7611 /* computed goto */
7606 next();7612 next();
@@ -8566,11 +8572,14 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 959 unmodified lines
8566 if (NODATA_WANTED)8572 if (NODATA_WANTED)
8567 goto no_alloc;8573 goto no_alloc;
8568 8574
8569 /* save current stack pointer */8575 /* save before-VLA stack pointer if needed */
8570 if (root_scope->vla.loc == 0) {8576 if (cur_scope->vla.num == 0) {
8571 struct scope *v = cur_scope;8577 if (cur_scope->prev && cur_scope->prev->vla.num) {
8572 gen_vla_sp_save(loc -= PTR_SIZE);8578 cur_scope->vla.locorig = cur_scope->prev->vla.loc;
8573 do v->vla.loc = loc; while ((v = v->prev));8579 } else {
8580 gen_vla_sp_save(loc -= PTR_SIZE);
8581 cur_scope->vla.locorig = loc;
8582 }
8574 }8583 }
8575 8584
8576 vla_runtime_type_size(type, &a);8585 vla_runtime_type_size(type, &a);
tests/tests2/123_vla_bug.c+40−0
@@ -0,0 +1,40 @@
1typedef __SIZE_TYPE__ size_t;
2extern int printf(const char*, ...);
3extern size_t strlen(const char*);
4char str[] = "blabla";
5int g;
6int main()
7{
8 //char helpme[strlen(str) + 1];
9 int i = 0;
10#if 0
11 if (g) {
12 char buf[strlen(str) + 10];
13 buf[0] = 0;
14 }
15alabel:
16 printf("default: i = %d\n", i);
17#else
18 for (i = 0; i < 5; i++) {
19 switch (i) {
20 case 10:
21 if (g) {
22 char buf[strlen(str) + 10];
23 buf[0] = 0;
24 goto do_cmd;
25 }
26 break;
27 case 1:
28 printf("reached 3\n");
29 do_cmd:
30 printf("after do_cmd");
31 break;
32 default:
33 g++;
34 printf("default: i = %d\n", i);
35 break;
36 }
37 }
38#endif
39 return 0;
40}
tests/tests2/123_vla_bug.expect+5−0
@@ -0,0 +1,5 @@
1default: i = 0
2reached 3
3after do_cmddefault: i = 2
4default: i = 3
5default: i = 4
tests/tests2/79_vla_continue.c+6−3
@@ -48,11 +48,14 @@ void test3() · 47 unmodified lines
48 int count = 10;48 int count = 10;
49 void *addr[count];49 void *addr[count];
50 while(count--) {50 while(count--) {
51 int a[f()];51 int b[f()];
52 if (count >= 0) {
53 int a[f()];
52 54
53 addr[count] = a;55 addr[count] = a;
54 56
55 continue;57 continue;
58 }
56 }59 }
57 60
58 if(addr[9] == addr[0]) {61 if(addr[9] == addr[0]) {

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