Skip to content
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

wasm_runtime_instantiate calls native function, which needs access to user data pointer #4034

Open
malytomas opened this issue Jan 19, 2025 · 0 comments

Comments

@malytomas
Copy link
Contributor

malytomas commented Jan 19, 2025

Assume this c++ code compiled to wasm.

int compute_hash(const char *str);

const int static_value = compute_hash("hello world");

void print_value()
{
	printf("value: %d", static_value);
}

There is a global variable in wasm static memory that is initialized with a call to function. That function is native function linked from the host. This native function is called during wasm_runtime_instantiate, before it returns. That means, that the function is called before I have access to the instance pointer returned from wasm_runtime_instantiate. Therefore I cannot use wasm_runtime_set_user_data on the execute env in the instance. However, I need the user data pointer inside the native function.

// snippet of host c++ code for the instantiation
String err;
instance = wasm_runtime_instantiate(module->module, 1'000'000, 1'000'000, err.rawData(), err.MaxLength - 100); // the native function is called within wasm_runtime_instantiate
if (!instance)
	throw;
exec = wasm_runtime_get_exec_env_singleton(instance);
if (!exec)
	throw;
wasm_runtime_set_user_data(exec, this); // before I get the chance to call this

I hope the description of the example is clear?

Is there a solution to this problem?

If not, possible solution is to split wasm_runtime_instantiate into two functions, where the first returns the instance pointer, and the second initializes the global memory inside the wasm, while calling the native functions.

Note, that passing user data pointer to wasm_runtime_instantiate would be insufficient, as that would not allow me to initialize the instance pointer.

@malytomas malytomas changed the title wasm_runtime_instantiate calls native function, which needs access to instance pointer wasm_runtime_instantiate calls native function, which needs access to user data pointer Jan 19, 2025
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

No branches or pull requests

1 participant