TOPLLab / WARDuino

📟 A dynamic WebAssembly VM for embedded systems
https://topllab.github.io/WARDuino/
Mozilla Public License 2.0
73 stars 7 forks source link

Program counter in dump points to data section #189

Closed tolauwae closed 1 year ago

tolauwae commented 1 year ago

When debugging the following program in the plugin. The microcontroller returns a pc that points to a datasection, and immediately the microcontroller resets without throwing a clear error.

import {digitalRead,
        digitalWrite,
        PinVoltage,
        pinMode,
        PinMode} from "as-warduino";

const button: u32 = 25;
const led: u32 = 26;

let state: PinVoltage = PinVoltage.LOW;

function init(): void {
    pinMode(led, PinMode.OUTPUT);
    pinMode(button, PinMode.INPUT);
}

export function main(): void {
    state = digitalRead(button);

    if (state === PinVoltage.HIGH) {
        digitalWrite(led, PinVoltage.HIGH);
    } else {
        digitalWrite(led, PinVoltage.LOW);
    }
}

At the if instruction we get:

{"pc":3144,"breakpoints":[],"callstack":[{"type":0,"fidx":"0x13","sp":-1,"fp":-1,"idx":0,"block_key":0,"ra":3538}],"globals":[{"idx":0,"type":"i32","value":0},{"idx":1,"type":"i32","value":0},{"idx":2,"type":"i32","value":1232}],"stack":[],"events": []}

When we step we get END, I don't know what causes this or what it means.

SerialConnection (/dev/ttyUSB0): received interrupt 4
SerialConnection (/dev/ttyUSB0): Interrupt: 4
SerialConnection (/dev/ttyUSB0): STEP!
SerialConnection (/dev/ttyUSB0): END

This happens right after the if instruction:

{"pc":3346,"breakpoints":[],"callstack":[{"type":0,"fidx":"0x15","sp":-1,"fp":-1,"idx":0,"block_key":0,"ra":3538}],"globals":[{"idx":0,"type":"i32","value":0},{"idx":1,"type":"i32","value":0},{"idx":2,"type":"i32","value":1232}],"stack":[],"events": []}

But 3346 is d12 in hex. This address points to a data segment.

0000cec: 73                                        ; data segment data
; data segment header 2
0000ced: 00                                        ; segment flags
@ { line: 16, col_start: 9, col_end: 18 }
0000cee: 41                                        ; i32.const
0000cef: 8c09                                      ; i32 literal
0000cf1: 0b                                        ; end
0000cf2: 31                                        ; data segment size
; data segment data 2
0000cf3: 1e00 0000 0100 0000 0000 0000 0100 0000 
0000d03: 1e00 0000 7e00 6c00 6900 6200 2f00 7200 
0000d13: 7400 2f00 7400 6c00 7300 6600 2e00 7400 
0000d23: 73                                        ; data segment data
; data segment header 3
0000d24: 00                                        ; segment flags
@ { line: 17, col_start: 9, col_end: 18 }
0000d25: 41                                        ; i32.const
0000d26: c009                                      ; i32 literal
0000d28: 0b                                        ; end
0000d29: 15                                        ; data segment size
; data segment data 3
0000d2a: 0300 0000 2000 0000 0000 0000 2000 0000 
0000d3a: 0000 0000 20                              ; data segment data
0000c6f: cb81 8080 00                              ; FIXUP section size

The code and compiled files: code.zip


Screenshot:

image

tolauwae commented 1 year ago

I think I might know what it is. There is a void instruction after the if. Which we don't handle properly.

tolauwae commented 1 year ago

No that's not it.

tolauwae commented 1 year ago

I don't get the issue anymore. Closing.