project-mahiwa / mahiwa-frontend-go

mahiwaのGo言語向けフロントエンド
https://pkg.go.dev/github.com/project-mahiwa/mahiwa-frontend-go
MIT License
0 stars 0 forks source link

[🐛Bug]:printLongでパニックになる #31

Closed usuyuki closed 10 months ago

usuyuki commented 10 months ago

🐛 概要

https://github.com/project-mahiwa/mahiwa-backend/issues/91 で見つかった問題

原因

備考

usuyuki commented 10 months ago
m3ApiRawFunction(m3_printFloat2)
{
    m3ApiGetArg(float, num);
    m3ApiGetArg(int, format);
    Serial.print(num, format);
    m3ApiSuccess();
}

型の不一致の可能性も加味して、WebAssemblyにあう方にしてみたが、変わらず

printFloat(1.1111):1.1111Guru Meditation Error: Core  1 panic'ed (Double exception). 

Core  1 register dump:
PC      : 0x40092012  PS      : 0x00040136  A0      : 0x8008ee8d  A1      : 0x3ffdaaa0  
A2      : 0x4008cee8  A3      : 0x3ffbb9e4  A4      : 0x3f801b68  A5      : 0x3ffdac40  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x40080080  A9      : 0x3ffdabf0  
A10     : 0x00060336  A11     : 0x00040026  A12     : 0x3f801b68  A13     : 0x3ffd7684  
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000001e  EXCCAUSE: 0x00000002  
EXCVADDR: 0xffffffe0  LBEG    : 0x400900ad  LEND    : 0x400900bd  LCOUNT  : 0xffffffff  
usuyuki commented 10 months ago

https://github.com/project-mahiwa/mahiwa-backend/issues/91#issuecomment-1857391497

だったが、target.jsonのstack-sizeを増やしても効果なし

usuyuki commented 10 months ago
serial.print(num,format);
serial.print("print end"

ではprint endまで行ったので、serial.print自体では問題なし

m3ApiSuccess();

が問題

usuyuki commented 10 months ago

引数多いが問題な気がする

usuyuki commented 10 months ago
    serial.Print("cos(1.0):")
    cos1 := arduino.Max(1, 0)

でもcosのprintした直後に出たので、引数2つだと壊れるで間違いなさそう。

      if  ;; label = @2
        i64.const 1
        i64.const 0
        call $max

ただ、wasm内での呼び出しは適切に2つ引数渡せていて謎。

usuyuki commented 10 months ago

RustのprintDoubleの箇所

  (func (;10;) (type 4) (param f64 i32)
    local.get 0
    local.get 1
    i32.const 255
    i32.and
    call 4)

WebAssemblyの表現では同じなのに、Goだけエラーになる。

usuyuki commented 10 months ago

wasm-ldのオプションで-sつけるとエラーになるのも気になる。 gcのせい?

usuyuki commented 10 months ago

tinygoのビルドオプションはデフォルトがzなので、サイズは一番小さいはずなのに、rustと比べるとあまりにも大きい。

usuyuki commented 10 months ago

gc=noneにしてコンパイル

tinygo build -target ./target.json -o main.wasm main.go
tinygo:wasm-ld: error: /tmp/tinygo2970132202/main.o: undef
ined symbol: runtime.alloc  
usuyuki commented 10 months ago

"no-debug": true, も効果なし

https://tinygo.org/docs/reference/usage/important-options/

usuyuki commented 10 months ago

"panic": "trap", やheap-sizeの変更も意味なし

usuyuki commented 10 months ago

最適化かけるときに出てくる

unexpected false: Bu
mory operation (bulk memory is disabled),

が気になる。そもそもちゃんとしたwasmになっていない説がある。

ただしこれは単純なprintだけするプロジェクトでも同じエラーが出るので、謎。

usuyuki commented 10 months ago

wasm3公式のexampleとか見たが、やはり問題なさそう。違うのはTinyGoのバージョンだけ。

usuyuki commented 10 months ago

core2だけでなくstampc3uとかでやってみたが変わらず。

usuyuki commented 10 months ago

println("")でもエラーが出ていた。

usuyuki commented 10 months ago

printlnなしにしてのエラー

Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (wasm3)
usuyuki commented 10 months ago

いやこれおかしい。printlnでエラーになるときとならないときがある。 でもずっと動かなかった2引数が動くようになった。

    serial.Print("max(1,0)")
    cos1 := arduino.Max(1, 0)
    serial.PrintLong(cos1)
max(1,0)0Project Mahiwa : WebAssembly done
usuyuki commented 10 months ago

これにserial.Println("")をつけるとパニックする。

    serial.Print("max(1,0)")
    cos1 := arduino.Max(1, 0)
    serial.PrintLong(cos1)
    serial.Println("")

serial.Println("")はそれより前で何度も呼べてるのに……

max(1,0)0Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (wasm3) 

↓ このエラーは https://www.1ft-seabass.jp/memo/2017/07/31/esp32-developer-aws-iot-tips/ より、FreeRTOSでタスクに割り当てるメモリ不足らしい。

ただ

  xTaskCreatePinnedToCore(&wasm_task, "wasm3", NATIVE_STACK_SIZE, NULL, 5, NULL,
                          1);

のNATIVE_STACK_SIZEを増やすと、今度はメモリ確保できずエラーになるので難しい。

usuyuki commented 10 months ago

とりあえず

#define NATIVE_STACK_SIZE (16 * 1024)

#define NATIVE_STACK_SIZE (16 * 2024)

にしてみる

usuyuki commented 10 months ago

実行できてしまった……・

usuyuki commented 10 months ago

https://docs.espressif.com/projects/esp-idf/en/v4.3/esp32/api-reference/system/freertos.html ESP32のやつはFreeRTOSと実は違うらしい。 タスクスタックのサイズであることに間違いはないらしい。

usuyuki commented 10 months ago

いやでもtarget.jsonで指定した、スタックは2048byteで、もともと1024*16byteと比べると小さいので、wasm3のインタプリタが想像以上にスタックを消費している可能性もある

usuyuki commented 10 months ago

ひとまず解決