Skip to content

Commit ff45b86

Browse files
authored
Fix zero initialization of locals causing garbage values (#331)
* Fix zero initialization of locals stackvalues * Add get zero initialized local tests with stack garbage
1 parent b8dbd3d commit ff45b86

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/Interpreter/interpreter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ void Interpreter::setup_call(Module *m, uint32_t fidx) {
104104
m->sp, STACK_SIZE);
105105
}
106106
#endif
107+
memset(&m->stack[m->sp], 0, sizeof(StackValue));
107108
m->stack[m->sp].value_type = func->local_value_type[lidx];
108-
m->stack[m->sp].value = {0}; // Initialize whole union to 0
109109
}
110110

111111
// Set program counter to start of function

tests/latch/core/local_get_0.asserts.wast

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
(assert_return (invoke "type-local-i64") (i64.const 0))
33
(assert_return (invoke "type-local-f32") (f32.const 0))
44
(assert_return (invoke "type-local-f64") (f64.const 0))
5+
(assert_return (invoke "zero-init-f64") (f64.const 0))
6+
(assert_return (invoke "zero-init-i64") (i64.const 0))
57
(assert_return (invoke "type-param-i32" (i32.const 2)) (i32.const 2))
68
(assert_return (invoke "type-param-i64" (i64.const 3)) (i64.const 3))
79
(assert_return (invoke "as-block-value" (i32.const 6)) (i32.const 6))

tests/latch/core/local_get_0.wast

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
(module
33
;; Typing
44

5-
(func (export "type-local-i32") (result i32) (local i32) (local.get 0))
6-
(func (export "type-local-i64") (result i64) (local i64) (local.get 0))
7-
(func (export "type-local-f32") (result f32) (local f32) (local.get 0))
8-
(func (export "type-local-f64") (result f64) (local f64) (local.get 0))
5+
(func $type-local-i32 (export "type-local-i32") (result i32) (local i32) (local.get 0))
6+
(func $type-local-i64 (export "type-local-i64") (result i64) (local i64) (local.get 0))
7+
(func $type-local-f32 (export "type-local-f32") (result f32) (local f32) (local.get 0))
8+
(func $type-local-f64 (export "type-local-f64") (result f64) (local f64) (local.get 0))
99

10-
(func (export "type-param-i32") (param i32) (result i32) (local.get 0))
11-
(func (export "type-param-i64") (param i64) (result i64) (local.get 0))
12-
(func (export "type-param-f32") (param f32) (result f32) (local.get 0))
13-
(func (export "type-param-f64") (param f64) (result f64) (local.get 0))
10+
(func $type-param-i32 (export "type-param-i32") (param i32) (result i32) (local.get 0))
11+
(func $type-param-i64 (export "type-param-i64") (param i64) (result i64) (local.get 0))
12+
(func $type-param-f32 (export "type-param-f32") (param f32) (result f32) (local.get 0))
13+
(func $type-param-f64 (export "type-param-f64") (param f64) (result f64) (local.get 0))
1414

1515
(func (export "type-mixed") (param i64 f32 f64 i32 i32)
1616
(local f32 i64 i64 f64)
@@ -25,6 +25,19 @@
2525
(drop (f64.neg (local.get 8)))
2626
)
2727

28+
;; ensure the full 64-bit local value is 0-initialized
29+
;; (similar to type-local-<x>64 but with added stack garbage)
30+
(func (export "zero-init-f64") (result f64)
31+
(f64.const 0xAAAAAAAAAAAAAAAA)
32+
(drop)
33+
(call $type-local-f64)
34+
)
35+
(func (export "zero-init-i64") (result i64)
36+
(i64.const 0xAAAAAAAAAAAAAAAA)
37+
(drop)
38+
(call $type-local-i64)
39+
)
40+
2841
;; Reading
2942

3043
(func (export "read") (param i64 f32 f64 i32 i32) (result f64)

0 commit comments

Comments
 (0)