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
120120
121121 static struct scope {
122122 struct scope *prev;
123− struct { int loc, num; } vla;
123+ struct { int loc, locorig, num; } vla;
124124 struct { Sym *s; int n; } cl;
125125 int *bsym, *csym;
126126 Sym *lstk, *llstk;
@@ -7268,8 +7268,12 @@ static void vla_restore(int loc) · 7141 unmodified lines
72687268
72697269 static void vla_leave(struct scope *o)
72707270 {
7271− if (o->vla.num < cur_scope->vla.num)
7272− vla_restore(o->vla.loc);
7271+ struct scope *c = cur_scope, *v = NULL;
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);
72737277 }
72747278
72757279 /* ------------------------------------------------------------------------- */
@@ -7281,6 +7285,7 @@ void new_scope(struct scope *o) · 5 unmodified lines
72817285 *o = *cur_scope;
72827286 o->prev = cur_scope;
72837287 cur_scope = o;
7288+ cur_scope->vla.num = 0;
72847289
72857290 /* record local declaration stack position */
72867291 o->lstk = local_stack;
@@ -7600,7 +7605,8 @@ again: · 313 unmodified lines
76007605 goto block_after_label;
76017606
76027607 } 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);
76047610 if (tok == '*' && gnu_ext) {
76057611 /* computed goto */
76067612 next();
@@ -8566,11 +8572,14 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 959 unmodified lines
85668572 if (NODATA_WANTED)
85678573 goto no_alloc;
85688574
8569− /* save current stack pointer */
8570− if (root_scope->vla.loc == 0) {
8571− struct scope *v = cur_scope;
8572− gen_vla_sp_save(loc -= PTR_SIZE);
8573− do v->vla.loc = loc; while ((v = v->prev));
8575+ /* save before-VLA stack pointer if needed */
8576+ if (cur_scope->vla.num == 0) {
8577+ if (cur_scope->prev && cur_scope->prev->vla.num) {
8578+ cur_scope->vla.locorig = cur_scope->prev->vla.loc;
8579+ } else {
8580+ gen_vla_sp_save(loc -= PTR_SIZE);
8581+ cur_scope->vla.locorig = loc;
8582+ }
85748583 }
85758584
85768585 vla_runtime_type_size(type, &a);
tests/tests2/123_vla_bug.c+40−0
@@ -0,0 +1,40 @@
1+typedef __SIZE_TYPE__ size_t;
2+extern int printf(const char*, ...);
3+extern size_t strlen(const char*);
4+char str[] = "blabla";
5+int g;
6+int 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+ }
15+alabel:
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 @@
1+default: i = 0
2+reached 3
3+after do_cmddefault: i = 2
4+default: i = 3
5+default: i = 4
tests/tests2/79_vla_continue.c+6−3
@@ -48,11 +48,14 @@ void test3() · 47 unmodified lines
4848 int count = 10;
4949 void *addr[count];
5050 while(count--) {
51− int a[f()];
51+ int b[f()];
52+ if (count >= 0) {
53+ int a[f()];
5254
53− addr[count] = a;
55+ addr[count] = a;
5456
55− continue;
57+ continue;
58+ }
5659 }
5760
5861 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