ruby/ruby.wasm

MatchData#end returns invalid offset when called within evalAsync

Opened this issue · 5 comments

Stackblitz reproduction: https://stackblitz.com/edit/ruby-wasm-demo-78s71k?file=index.html

MatchData#end returns incorrect match offset when used in async context:

vm.eval(/(т)(е)(с)/.match('тест').end(0)) #=> 3
vm.eval(/(т)(е)(с)/.match('тест').end(1)) #=> 1

vm.evalAsync(/(т)(е)(с)/.match('тест').end(0)) #=> 3291
vm.evalAsync(/(т)(е)(с)/.match('тест').end(1)) #=> 3

Good news: the head version works fine.

I'll bisect which nightly head version fixed the issue once I'll get a stable internet connection.

This also crashes Date._parse (within evalAsync):

app.wasm:0x5739f8 Uncaught RuntimeError: memory access out of bounds
    at enc_strlen (app.wasm:0x5739f7)
    at rb_enc_strlen (app.wasm:0x573574)
    at update_char_offset (app.wasm:0x50cc96)
    at match_begin (app.wasm:0x507547)
    at ractor_safe_call_cfunc_1 (app.wasm:0x67a681)
    at vm_call0_body (app.wasm:0x638879)
    at rb_funcallv_scope (app.wasm:0x637cdb)
    at rb_funcall (app.wasm:0x65269f)
    at subx (app.wasm:0x7ed59)
    at date__parse (app.wasm:0x7bd9b)

Okay, I found the fix 😁—ruby/ruby@0d4de0f:

Author: Yuta Saito <kateinoigakukun@gmail.com>
Date:   Mon Jan 29 09:49:15 2024 +0000

    wasm: align fiber stack pointer to 16 bytes

    In WebAssembly C ABI, the linear stack pointer must be always aligned
    to 16 bytes like other archs.
    The misaligned stack pointer causes some weird memory corruption since
    compiler assumes the aligned stack pointer.

I'm sorry about totally forgetting my fix in January... I opened backport PRs for 3.2 and 3.3: