ruby.wasm icon indicating copy to clipboard operation
ruby.wasm copied to clipboard

MatchData#end returns invalid offset when called within evalAsync

Open palkan opened this issue 1 year ago • 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

palkan avatar Nov 12 '24 20:11 palkan

Good news: the head version works fine.

palkan avatar Nov 12 '24 20:11 palkan

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

kateinoigakukun avatar Nov 12 '24 21:11 kateinoigakukun

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)

palkan avatar Nov 14 '24 20:11 palkan

Okay, I found the fix 😁—https://github.com/ruby/ruby/commit/0d4de0f4b1b9ac90be437bf1bac6851dd1d96fd0:

Author: Yuta Saito <[email protected]>
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.

palkan avatar Nov 15 '24 19:11 palkan

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

  • https://github.com/ruby/ruby/pull/12100
  • https://github.com/ruby/ruby/pull/12101

kateinoigakukun avatar Nov 15 '24 20:11 kateinoigakukun