Skip to content

Increase FuncCastEmulation NUM_PARAMS - #1884

Merged
kripken merged 2 commits into
WebAssembly:masterfrom
willglynn:increase_func_cast_emulation_params
Jan 29, 2019
Merged

Increase FuncCastEmulation NUM_PARAMS#1884
kripken merged 2 commits into
WebAssembly:masterfrom
willglynn:increase_func_cast_emulation_params

Conversation

@willglynn

Copy link
Copy Markdown
Contributor

FuncCastEmulation supports a hardcoded number of parameters:

// This should be enough for everybody. (As described above, we need this
// to match when dynamically linking, and also dynamic linking is why we
// can't just detect this automatically in the module we see.)
static const int NUM_PARAMS = 15;

Turns out 15 is not enough for everybody: Ruby 2.6.0 needs NUM_PARAMS = 16. This patch is necessary to support Ruby 2.6.0 in WebAssembly, and in fact is the only patch needed to make the relevant build process work with an otherwise normal emscripten toolchain.

@kripken

kripken commented Jan 23, 2019

Copy link
Copy Markdown
Member

Thanks! Looks good.

Interesting that Ruby needs more parameters here. Do you happen to know where in the code it depends on FuncCastEmulation? (just curious since it's rare - only other case I know of is python)

Before we merge, have you joined the wasm community group?

@willglynn

Copy link
Copy Markdown
Contributor Author

Sorry, I didn't see the CONTRIBUTING.md. I requested a W3C account but am not yet able to join the group; I'll post again when that changes.

As for why this is happening, I don't quite know what I'm looking at, but I'll tell you what I do know. Calling main() with ASSERTIONS=2 EMULATE_FUNCTION_POINTER_CASTS=0:

Invalid function pointer '1685' called with signature 'iii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)
This pointer might make sense in another type signature: ii: 0  iiii: 0  i: _rb_obj_dummy  iiiii: 0  iiiiii: 0  iiiiiii: 0  iiiiiiii: undefined  iiiiiiiii: undefined  iiiiiiiiii: undefined  iiiiiiiiiii: undefined  iiiiiiiiiiii: undefined  iiiiiiiiiiiii: undefined  iiiiiiiiiiiiii: undefined  iiiiiiiiiiiiiii: undefined  iiiiiiiiiiiiiiii: undefined  iiiiiiiiiiiiiiiii: undefined  viii: 0  vii: 0  vi: 0  viiii: 0  jij: 0  viiiii: 0  v: 0  viiiiii: 0  viiiiiiii: undefined  viiiiiiiii: 0  

$1685 is enumerator_init_copy, which is called correctly from C but is additionally exposed to Ruby. Symbolizing the backtrace, the call did indeed come from the Ruby VM machinery:

b8395
_call_cfunc_1
_vm_call0_cfunc_with_frame
_vm_call0_cfunc
_vm_call0_body
_rb_vm_call0
_rb_call0
_rb_call
_rb_funcall
_rb_class_inherited
_rb_define_class
_InitVM_Object
_Init_Object
_rb_call_inits
dynCall_v
…

call_cfunc_1 looks like it should be calling the function pointer with ii, which… would be correct and isn't the signature in the complaint? How do I hunt down b8395?

@kripken

kripken commented Jan 29, 2019

Copy link
Copy Markdown
Member

Hmm, b8395 is probably just the function that throws the error. I think that data is enough - looks like Ruby does the same as Python, and relies on C undefined behavior with function pointer arguments, making this pass necessary.

@kripken

kripken commented Jan 29, 2019

Copy link
Copy Markdown
Member

As this is a small change to a constant, I think we can merge it, but for future contributions it would be important to finish joining the group.

@kripken
kripken merged commit 5bfb98b into WebAssembly:master Jan 29, 2019
@willglynn
willglynn deleted the increase_func_cast_emulation_params branch January 29, 2019 20:10
@aidanhs

aidanhs commented Feb 9, 2021

Copy link
Copy Markdown

Just stumbled on this PR.

only other case I know of is python

@kripken fyi glib (not glibc) is not great with function pointer casts. Here's a patch I had to apply to avoid function pointer casts back in the asm.js days (emulating function pointer casts increased the output .js size to ~1GB): https://gitlab.com/aidanhs/emwesnoth/-/blob/master/diff-glib.patch

I should try to update that repo for wasm...

@kripken

kripken commented Feb 9, 2021

Copy link
Copy Markdown
Member

Thanks @aidanhs , interesting to know it's not just Python here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants