ceifa / wasmoon

A real lua 5.4 VM with JS bindings made with webassembly
MIT License
449 stars 29 forks source link

STACK_SIZE vs. LUAI_MAXCCALLS #75

Closed gottfriedleibniz closed 1 year ago

gottfriedleibniz commented 1 year ago

Following up on https://github.com/ceifa/wasmoon/pull/72, the current build script does not set STACK_SIZE (Emscripten) or reduce LUAI_MAXCCALLS (Lua) to deal with emscripten reducing the default stack size. For example, using a test from the Lua repository (cstack.lua):

local function checkerror (msg, f, ...)
  local s, err = pcall(f, ...)
  assert(not s and string.find(err, msg))
end

do  print("testing stack-overflow in recursive 'gsub'")
  local count = 0
  local function foo ()
    count = count + 1
    string.gsub("a", ".", foo)
  end
  checkerror("stack overflow", foo)
end

Results in:

└> ./bin/wasmoon cstack.lua
testing stack-overflow in recursive 'gsub'
RuntimeError: memory access out of bounds
    at wasm://wasm/000f77c2:wasm-function[517]:0x31e5c
    at wasm://wasm/000f77c2:wasm-function[320]:0x1f7fd
    at wasm://wasm/000f77c2:wasm-function[260]:0x19d59
    ...

Building with -s STACK_SIZE=1MB (value pulled from Windows) or -DLUAI_MAXCCALLS=120 fixes the above.

ceifa commented 1 year ago

Apparently lua limits the stack to 1MB by default. Things should run great if we limit at the same value on emscripten.

gottfriedleibniz commented 1 year ago

In luatests.mjs, if you enable _port, e.g., lua.global.set('_port', true), it will disable all non-portable tests. This allows you to remove main.lua, strings.lua, and files.lua from disabledtests.

literals.lua includes some locale tests for pt_BR and ptb. However, musl treats invalid/unusable locales as aliases to C.UTF-8, causing os.setlocale to always return true (regardless of validity).

I suppose you could patch that by replacing os.setlocale with a function to always return false.