Skip to content

Allow finer control of start function on instantiation #4072

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sjamesr
Copy link
Contributor

@sjamesr sjamesr commented Feb 10, 2025

Tries to address #4047.

Before this change, wasm_runtime_instantiate would execute the start function unconditionally, correctly implementing the spec. However, if there is an infinite loop in the start function, there was no way to interrupt execution.

This change introduces a parameter to InstantiationArgs indicating whether the start function should be immediately invoked. If not, wasm_runtime_instantiate returns a module instance that is initialized except for the start function.

This change adds a wasm_runtime_instantiate_run_start_func function which performs this last instantiation step. The user may prepare a timeout in advance of calling this to guard against expensive/infinite loops.

This change also demonstrates a possible technique for doing this in iwasm.c.

Tries to address
bytecodealliance#4047.

Before this change, wasm_runtime_instantiate would execute the start
function unconditionally, correctly implementing the spec. However, if
there is an infinite loop in the start function, there was no way to
interrupt execution.

This change introduces a parameter to InstantiationArgs indicating
whether the start function should be immediately invoked. If not,
wasm_runtime_instantiate returns a module instance that is initialized
except for the start function.

This change adds a wasm_runtime_instantiate_run_start_func function
which performs this last instantiation step. The user may prepare a
timeout in advance of calling this to guard against expensive/infinite
loops.

This change also demonstrates a possible technique for doing this in
iwasm.c.
@sjamesr sjamesr force-pushed the instantiate_timeout branch from cd6cd60 to b8e57c6 Compare February 10, 2025 06:57
@lum1n0us
Copy link
Collaborator

I'm pretty confident that this is a workable patch. However, from my understanding, it seems more like a workaround rather than a general solution.

Ideally, the termination should originate from the runtime itself, whether it's the interpreter, JIT, or AOT, rather than from iwasm. I'm considering a solution that's more intrinsic to the runtime, such as using a counter (which might be less precise) to estimate the remaining fuel or time left after each opcode or block(maybe every round of a loop).

@lum1n0us lum1n0us mentioned this pull request Feb 14, 2025
@yamt
Copy link
Collaborator

yamt commented Apr 23, 2025

Ideally, the termination should originate from the runtime itself, whether it's the interpreter, JIT, or AOT, rather than from iwasm. I'm considering a solution that's more intrinsic to the runtime, such as using a counter (which might be less precise) to estimate the remaining fuel or time left after each opcode or block(maybe every round of a loop).

please note that the timeout thing is NOT the sole motivation of wasm_runtime_terminate.
what we (well, i) wanted to have was a functionality to terminate instances on async events in general. (eg. on user request)
i just added the timeout option as a demo of the api. it was not the main motivation of the api.

fuel-like approaches would work for my use cases only if it allows to resume terminated instance. it likely makes host functions very tricky. (i have implemented a similar approach in the other runitme: https://github.com/yamt/toywasm/blob/master/doc/check_interrupt.md. my gut feeling is that it isn't suitable for wamr.)

@@ -2333,6 +2333,25 @@ wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
return set_running_mode(module_inst, running_mode, false);
}

bool
wasm_runtime_instantiate_run_start_func(wasm_module_inst_t module_inst,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this name is a bit confusing because it doesn't run wasi's entry point, _start.
how about wasm_runtime_execute_instantiate_functions?

is_sub_inst, exec_env)) {
set_error_buf(error_buf, error_buf_size,
((WASMModuleInstance *)module_inst)->cur_exception);
wasm_runtime_deinstantiate(module_inst);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as wasm_runtime_instantiate succeeded at this point,
isn't it simpler to leave wasm_runtime_deinstantiate to api users?

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