Skip to content

Fix indirect call type mismatch #30

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
kateinoigakukun opened this issue Dec 24, 2019 · 1 comment · Fixed by #186
Closed

Fix indirect call type mismatch #30

kateinoigakukun opened this issue Dec 24, 2019 · 1 comment · Fixed by #186
Assignees
Labels
in progress The issue is currently being worked on

Comments

@kateinoigakukun
Copy link
Member

kateinoigakukun commented Dec 24, 2019

Now, most of test cases that depends on StdlibUnittest fails due to same error.

Environment: wasmtime 0.8.0

Caused by:
    0: Instantiation failed during setup
    1: Trap occurred while invoking start function: wasm trap: indirect call type mismatch, source location: @c0925

To debug this problem, I'm developing a debugger of wasm at first.
This crash may be related with #6 🤔

@kateinoigakukun kateinoigakukun added the in progress The issue is currently being worked on label Dec 24, 2019
@kateinoigakukun kateinoigakukun self-assigned this Dec 24, 2019
MaxDesiatov pushed a commit that referenced this issue Jan 29, 2020
[pull] swiftwasm from apple:master
@kateinoigakukun
Copy link
Member Author

kateinoigakukun commented Feb 13, 2020

Progress report

I developed a WebAssembly debugger kateinoigakukun/wasminspect. If you are interested in fixing test cases, this tool is very useful to investigate.

As far as I investigated, CapturePropagation pass in SILOptimizer omits partial_apply which is emitted for thunk function call.

For example,

sil @main : $@convention(c) (Builtin.Int32, Builtin.RawPointer) -> Builtin.Int32 {
bb0(%c : $Builtin.Int32, %v : $Builtin.RawPointer):
  // function_ref createInstance()
  %1 = function_ref @$s5Katei14createInstanceAA1SVSgyF : $@convention(thin) () -> Optional<S> // user: %3
  // function_ref thunk for @escaping @convention(thin) () -> (@unowned S?)
  %2 = function_ref @$s5Katei1SVSgIetd_ADIegd_TR : $@convention(thin) (@convention(thin) () -> Optional<S>) -> Optional<S> // user: %3
  %3 = partial_apply [callee_guaranteed] [on_stack] %2(%1) : $@convention(thin) (@convention(thin) () -> Optional<S>) -> Optional<S> // users: %5, %9
}

this SIL is optimized to be that.

sil @main : $@convention(c) (Builtin.Int32, Builtin.RawPointer) -> Builtin.Int32 {
bb0(%c : $Builtin.Int32, %v : $Builtin.RawPointer):
  // function_ref specialized thunk for @callee_guaranteed () -> (@unowned S?)
  %1 = function_ref @$s5Katei1SVSgIgd_ADIegr_TR69$s5Katei1SVSgIetd_ADIegd_TR33$s5Katei14createInstanceAA1SVSgyFTf3pf_nTf3npf_n : $@convention(thin) @noescape () -> @out Optional<S> // user: %2
  %2 = thin_to_thick_function %3 : $@convention(thin) @noescape () -> @out Optional<S> to $@noescape @callee_guaranteed () -> @out Optional<S>
}


// specialized thunk for @callee_guaranteed () -> (@unowned S?)
sil shared [transparent] [reabstraction_thunk] @$s5Katei1SVSgIgd_ADIegr_TR69$s5Katei1SVSgIetd_ADIegd_TR33$s5Katei14createInstanceAA1SVSgyFTf3pf_nTf3npf_n : $@convention(thin) @noescape () -> @out Optional<S> {
// %0                                             // user: %5
bb0(%0 : $*Optional<S>):
  // function_ref createInstance()
  %1 =  function_ref @$s5Katei14createInstanceAA1SVSgyF : $@convention(thin) () -> Optional<S> // user: %3
  %2 = apply %1() : $@convention(method) () -> S // user: %3
  %3 = enum $Optional<S>, #Optional.some!enumelt.1, %2 : $S // user: %4
  store %3 to %0 : $*Optional<S>                  // id: %4
  %5 = tuple ()                                   // user: %6
  return %5 : $()                                 // id: %6
} // end sil function '$s5Katei1SVSgIgd_ADIegr_TR69$s5Katei1SVSgIetd_ADIegd_TR33$s5Katei14createInstanceAA1SVSgyFTf3pf_nTf3npf_n'

This means thin_to_thick_function should emit LLVM IR level thunk function through IRGen module.

If -Onone is set, most of stdlib test cases are passed 😜

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
in progress The issue is currently being worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant