Skip to content

Commit a72a245

Browse files
MaartenS11tolauwae
andauthored
Fix tableidx overflow on call_indirect (#303)
* Fix maxbits being set to 7 and 1 instead of 32 as described in the spec * Add FATAL error message when stack size is exceeded when running in DEBUG/TRACE/WARN/INFO mode This way you get a friendly error when trying to figure out why something is not working instead of a vague segfault. * Change error message to say "WebAssembly stack overflow" --------- Co-authored-by: Tom Lauwaerts <[email protected]>
1 parent 9f6baef commit a72a245

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

src/Interpreter/instructions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ bool i_instr_call(Module *m) {
317317
bool i_instr_call_indirect(Module *m) {
318318
uint32_t tidx = read_LEB_32(&m->pc_ptr); // TODO: use tidx?
319319
(void)tidx;
320-
read_LEB(&m->pc_ptr, 1); // reserved immediate
320+
read_LEB_32(&m->pc_ptr); // reserved immediate
321321
uint32_t val = m->stack[m->sp--].value.uint32;
322322
if (m->options.mangle_table_index) {
323323
// val is the table address + the index (not sized for the

src/Interpreter/interpreter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ void Interpreter::setup_call(Module *m, uint32_t fidx) {
9898
// Push function locals
9999
for (uint32_t lidx = 0; lidx < func->local_count; lidx++) {
100100
m->sp += 1;
101+
#if DEBUG || TRACE || WARN || INFO
102+
if (m->sp >= STACK_SIZE) {
103+
FATAL("WebAssembly stack overflow m->sp = %d, STACK_SIZE = %d\n",
104+
m->sp, STACK_SIZE);
105+
}
106+
#endif
101107
m->stack[m->sp].value_type = func->local_value_type[lidx];
102108
m->stack[m->sp].value = {0}; // Initialize whole union to 0
103109
}

src/WARDuino/WARDuino.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void skip_immediates(uint8_t **pos) {
159159
case 0x11: // call_indirect
160160
// encoding: 0x11 x 0x00
161161
read_LEB_32(pos); // read x
162-
read_LEB(pos, 7); // 0x00 byte
162+
read_LEB_32(pos); // 0x00 byte
163163
break;
164164
// varint64
165165
case 0x42: // i64.const

0 commit comments

Comments
 (0)