bytecodealliance / wasmtime

A fast and secure runtime for WebAssembly
https://wasmtime.dev/
Apache License 2.0
15.08k stars 1.26k forks source link

`ref.func`: undeclared function reference #8657

Closed oovm closed 3 months ago

oovm commented 3 months ago

Test Case

(component $App
    (core module $Main
        (type $i32-i32 (func (param i32) (result i32)))

        (func $hof (param $f (ref $i32-i32)) (result i32)
            (i32.add (i32.const 10) (call_ref $i32-i32 (i32.const 42) (local.get $f)))
        )

        (func $inc (param $i i32) (result i32)
            (i32.add (local.get $i) (i32.const 1))
        )

        (func $caller (result i32)
            (call $hof (ref.func $inc))
        )
    )
)

Steps to Reproduce

Expected Results

No error reported

Actual Results

Error: WebAssembly translation error

Caused by:
    Invalid input WebAssembly code at offset 67: undeclared function reference

Stack backtrace:
   0: std::backtrace_rs::backtrace::dbghelp::trace
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c/library\std\src\..\..\backtrace\src\backtrace\dbghelp.rs:131
   1: std::backtrace_rs::backtrace::trace_unsynchronized
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2: std::backtrace::Backtrace::create
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c/library\std\src\backtrace.rs:331
   3: std::backtrace::Backtrace::capture
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c/library\std\src\backtrace.rs:296
   4: anyhow::error::impl$1::from<enum2$<wasmtime_environ::compile::CompileError> >
             at .cargo\anyhow-1.0.85\src\error.rs:565
   5: core::result::impl$27::from_residual<wasmtime::compile::CompileOutput,enum2$<wasmtime_environ::compile::CompileError>,anyhow::Error>
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\result.rs:1963
   6: wasmtime::compile::impl$4::collect_inputs_in_translations::closure$0<core::iter::adapters::map::Map<cranelift_entity::iter::IterMut<wasmtime_types::StaticModuleIndex,wasmtime_environ::compile::module_environ::ModuleTranslation>,wasmtime::compile::build_co
             at C:\Users\Dell\.cargo\git\checkouts\wasmtime-41807828cb3a7a7e\a77e877\crates\wasmtime\src\compile.rs:457
   7: core::ops::function::FnOnce::call_once<wasmtime::compile::impl$4::collect_inputs_in_translations::closure_env$0<core::iter::adapters::map::Map<cranelift_entity::iter::IterMut<wasmtime_types::StaticModuleIndex,wasmtime_environ::compile::module_environ::Mod
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\ops\function.rs:250
   8: alloc::boxed::impl$47::call_once<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::compile::CompileOutpu
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\alloc\src\boxed.rs:2016
   9: wasmtime::compile::impl$4::compile::closure$0
             at C:\Users\Dell\.cargo\git\checkouts\wasmtime-41807828cb3a7a7e\a77e877\crates\wasmtime\src\compile.rs:537
  10: wasmtime::engine::impl$1::run_maybe_parallel::closure$0<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::compile::CompileOutput,anyhow::Error> 
             at C:\Users\Dell\.cargo\git\checkouts\wasmtime-41807828cb3a7a7e\a77e877\crates\wasmtime\src\engine.rs:164
  11: core::ops::function::impls::impl$1::call_mut<tuple$<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::compile::CompileOutput,anyhow::Error> > > 
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\ops\function.rs:272
  12: core::ops::function::impls::impl$4::call_once<tuple$<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::compile::CompileOutput,anyhow::Error> > >
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\ops\function.rs:305
  13: enum2$<core::option::Option<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::compile::CompileOutput,anyhow::Error> > > >,core::marker::Send>,al
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\option.rs:1072
  14: core::iter::adapters::map::impl$2::next<enum2$<core::result::Result<wasmtime::compile::CompileOutput,anyhow::Error> >,rayon::vec::SliceDrain<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\iter\adapters\map.rs:108
  15: core::iter::adapters::map::impl$2::next<enum2$<core::option::Option<wasmtime::compile::CompileOutput> >,core::iter::adapters::map::Map<rayon::vec::SliceDrain<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Co
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\iter\adapters\map.rs:108
  16: core::iter::adapters::take_while::impl$2::next<core::iter::adapters::map::Map<core::iter::adapters::map::Map<rayon::vec::SliceDrain<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\iter\adapters\take_while.rs:47
  17: core::iter::adapters::map::impl$2::next<wasmtime::compile::CompileOutput,core::iter::adapters::take_while::TakeWhile<core::iter::adapters::map::Map<core::iter::adapters::map::Map<rayon::vec::SliceDrain<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tu
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\iter\adapters\map.rs:108
  18: alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>::extend_desugared<wasmtime::compile::CompileOutput,alloc::alloc::Global,core::iter::adapters::map::Map<core::iter::adapters::take_while::TakeWhile<core::iter::adapters::map::Map<core::
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\alloc\src\vec\mod.rs:2930
  19: alloc::vec::spec_extend::impl$0::spec_extend<wasmtime::compile::CompileOutput,core::iter::adapters::map::Map<core::iter::adapters::take_while::TakeWhile<core::iter::adapters::map::Map<core::iter::adapters::map::Map<rayon::vec::SliceDrain<alloc::boxed::Box
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\alloc\src\vec\spec_extend.rs:17
  20: alloc::vec::impl$18::extend<wasmtime::compile::CompileOutput,alloc::alloc::Global,core::iter::adapters::map::Map<core::iter::adapters::take_while::TakeWhile<core::iter::adapters::map::Map<core::iter::adapters::map::Map<rayon::vec::SliceDrain<alloc::boxed:
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\alloc\src\vec\mod.rs:2904
  21: rayon::iter::extend::impl$2::consume_iter<wasmtime::compile::CompileOutput,core::iter::adapters::map::Map<core::iter::adapters::take_while::TakeWhile<core::iter::adapters::map::Map<core::iter::adapters::map::Map<rayon::vec::SliceDrain<alloc::boxed::Box<dy
             at .cargo\rayon-1.10.0\src\iter\extend.rs:130
  22: rayon::iter::while_some::impl$4::consume_iter<wasmtime::compile::CompileOutput,rayon::iter::extend::ListVecFolder<wasmtime::compile::CompileOutput>,core::iter::adapters::map::Map<core::iter::adapters::map::Map<rayon::vec::SliceDrain<alloc::boxed::Box<dyn$
             at .cargo\rayon-1.10.0\src\iter\while_some.rs:139
  23: rayon::iter::map::impl$8::consume_iter<enum2$<core::result::Result<wasmtime::compile::CompileOutput,anyhow::Error> >,enum2$<core::option::Option<wasmtime::compile::CompileOutput> >,rayon::iter::while_some::WhileSomeFolder<rayon::iter::extend::ListVecFolde
             at .cargo\rayon-1.10.0\src\iter\map.rs:248
  24: rayon::iter::map::impl$8::consume_iter<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::compile::CompileOutput,anyhow::Error> > > >,core::marke
             at .cargo\rayon-1.10.0\src\iter\map.rs:248
  25: rayon::iter::plumbing::Producer::fold_with<rayon::vec::DrainProducer<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::compile::CompileOutput,an
             at .cargo\rayon-1.10.0\src\iter\plumbing\mod.rs:109
  26: rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::compile::Com
             at .cargo\rayon-1.10.0\src\iter\plumbing\mod.rs:437
  27: rayon::iter::plumbing::bridge_producer_consumer::helper::closure$1<rayon::vec::DrainProducer<alloc::boxed::Box<dyn$<core::ops::function::FnOnce<tuple$<ref$<dyn$<wasmtime_environ::compile::Compiler> > >,assoc$<Output,enum2$<core::result::Result<wasmtime::c
             at .cargo\rayon-1.10.0\src\iter\plumbing\mod.rs:426
  28: rayon_core::join::join_context::call_b::closure$0<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global>,rayon::iter::plumbing::bridge_producer_consumer::helper::closure_env
             at .cargo\rayon-core-1.12.1\src\join\mod.rs:129
  29: rayon_core::job::impl$9::call::closure$0<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global>,rayon_core::join::join_context::call_b::closure_env$0<alloc::collections::lin
             at .cargo\rayon-core-1.12.1\src\job.rs:218
  30: core::panic::unwind_safe::impl$23::call_once<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global>,rayon_core::job::impl$9::call::closure_env$0<alloc::collections::linked_l
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\panic\unwind_safe.rs:272
  31: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<rayon_core::job::impl$9::call::closure_env$0<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global>,r
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\panicking.rs:554
  32: wasmtime::runtime::vm::arch::x86_64::assert_fp_is_aligned
  33: std::panicking::try<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global>,core::panic::unwind_safe::AssertUnwindSafe<rayon_core::job::impl$9::call::closure_env$0<alloc::col
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\panicking.rs:518
  34: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<rayon_core::job::impl$9::call::closure_env$0<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global>,rayon
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\panic.rs:142
  35: rayon_core::unwind::halt_unwinding<rayon_core::job::impl$9::call::closure_env$0<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global>,rayon_core::join::join_context::call_b
             at .cargo\rayon-core-1.12.1\src\unwind.rs:17
  36: enum2$<rayon_core::job::JobResult<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global> > >::call<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::comp
             at .cargo\rayon-core-1.12.1\src\job.rs:218
  37: rayon_core::job::impl$4::execute<rayon_core::latch::SpinLatch,rayon_core::join::join_context::call_b::closure_env$0<alloc::collections::linked_list::LinkedList<alloc::vec::Vec<wasmtime::compile::CompileOutput,alloc::alloc::Global>,alloc::alloc::Global>,ra
             at .cargo\rayon-core-1.12.1\src\job.rs:120
  38: rayon_core::job::JobRef::execute
             at .cargo\rayon-core-1.12.1\src\job.rs:64
  39: rayon_core::registry::WorkerThread::execute
             at .cargo\rayon-core-1.12.1\src\registry.rs:860
  40: rayon_core::registry::WorkerThread::wait_until_cold
             at .cargo\rayon-core-1.12.1\src\registry.rs:794
  41: rayon_core::registry::WorkerThread::wait_until<rayon_core::latch::OnceLatch>
             at .cargo\rayon-core-1.12.1\src\registry.rs:769
  42: rayon_core::registry::WorkerThread::wait_until_out_of_work
             at .cargo\rayon-core-1.12.1\src\registry.rs:818
  43: rayon_core::registry::main_loop
             at .cargo\rayon-core-1.12.1\src\registry.rs:923
  44: rayon_core::registry::ThreadBuilder::run
             at .cargo\rayon-core-1.12.1\src\registry.rs:53
  45: rayon_core::registry::impl$2::spawn::closure$0
             at .cargo\rayon-core-1.12.1\src\registry.rs:98
  46: core::hint::black_box
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\hint.rs:334
  47: std::sys_common::backtrace::__rust_begin_short_backtrace<rayon_core::registry::impl$2::spawn::closure_env$0,tuple$<> >
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\sys_common\backtrace.rs:155
  48: std::thread::impl$0::spawn_unchecked_::closure$1::closure$0<rayon_core::registry::impl$2::spawn::closure_env$0,tuple$<> >
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\thread\mod.rs:528
  49: core::panic::unwind_safe::impl$23::call_once<tuple$<>,std::thread::impl$0::spawn_unchecked_::closure$1::closure_env$0<rayon_core::registry::impl$2::spawn::closure_env$0,tuple$<> > >
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\panic\unwind_safe.rs:272
  50: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<std::thread::impl$0::spawn_unchecked_::closure$1::closure_env$0<rayon_core::registry::impl$2::spawn::closure_env$0,tuple$<> > >,tuple$<> >
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\panicking.rs:554
  51: rayon_core::sleep::impl$2::default
  52: std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<std::thread::impl$0::spawn_unchecked_::closure$1::closure_env$0<rayon_core::registry::impl$2::spawn::closure_env$0,tuple$<> > > >
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\panicking.rs:518
  53: std::panic::catch_unwind
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\panic.rs:142
  54: std::thread::impl$0::spawn_unchecked_::closure$1<rayon_core::registry::impl$2::spawn::closure_env$0,tuple$<> >
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\std\src\thread\mod.rs:527
  55: core::ops::function::FnOnce::call_once<std::thread::impl$0::spawn_unchecked_::closure_env$1<rayon_core::registry::impl$2::spawn::closure_env$0,tuple$<> >,tuple$<> >
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c\library\core\src\ops\function.rs:250
  56: std::sys::pal::windows::thread::impl$0::new::thread_start
             at /rustc/6672c16afcd4db8acdf08a6984fd4107bf07632c/library\std\src\sys\pal\windows\thread.rs:58
  57: BaseThreadInitThunk
  58: RtlUserThreadStart

Versions and Environment

Wasmtime version or commit: git nightly branch=main , tag=a77e877, features = ["component-model", "async", "gc"]

Operating system: Windows 11

See details on CI: https://github.com/valkyrie-language/valkyrie-runtime/actions/runs/9159552776/job/25180193563#step:6:188

alexcrichton commented 3 months ago

Thanks for the report! The example you're copying in the function-references proposal, and the snippet here, are both invalid and need to be updated (at least in the function-references proposal). The validation of ref.func requires that the referenced function is already registered in an elem segment or defined global. For this you'll want to do something like:

(elem declare funcref (ref.func $inc))

where the complete example is:

(component $App
    (core module $Main
        (type $i32-i32 (func (param i32) (result i32)))

        (elem declare funcref (ref.func $inc)) ;; <- this line was added

        (func $hof (param $f (ref $i32-i32)) (result i32)
            (i32.add (i32.const 10) (call_ref $i32-i32 (i32.const 42) (local.get $f)))
        )

        (func $inc (param $i i32) (result i32)
            (i32.add (local.get $i) (i32.const 1))
        )

        (func $caller (result i32)
            (call $hof (ref.func $inc))
        )
    )
)