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

Multi-threaded test application fails with Runtime exception #302

Closed
dbaeumer opened this issue Mar 8, 2023 · 3 comments
Closed

Multi-threaded test application fails with Runtime exception #302

dbaeumer opened this issue Mar 8, 2023 · 3 comments

Comments

@dbaeumer
Copy link

dbaeumer commented Mar 8, 2023

I maintain a custom implementation of a WASI Host (https://github.com/microsoft/vscode-wasm) on top of the VS Code API. This enables running wasm-wasi binaries and provide them transparent access to the VS Code file system, the console or a terminal.

I was very excited when I saw that there is a pre-release for thread support and started to add thread support to VS Code's host implementation. I implemented the necessary wasi::thread-spawn import however starting the thread fails with a runtime exception that looks like:

RuntimeError: null function or function signature mismatch
    at __wasi_thread_start_C (0002bda6:0x67db)
    at wasi_thread_start (0002bda6:0x7301)
    at port.onmessage (threadWorker.js:1921:28)

The C program I compile looks like this (slightly modified version from (https://www.geeksforgeeks.org/multithreading-in-c/):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>

// A normal C function that is executed as a thread
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp)
{
	sleep(1);
	printf("Printing GeeksQuiz from Thread \n");
	return NULL;
}

int main()
{
	pthread_t thread_id;
	printf("Before Thread\n");
	int result = pthread_create(&thread_id, NULL, myThreadFun, NULL);
	printf("Thread created with result: %i\n", result);
	result = pthread_join(thread_id, NULL);
	printf("Thread joined with result: %i\n", result);
	printf("After Thread\n");
	exit(0);
}

I compile the program to WASM using the following command:

~/bin/wasi-sdk-20.0+threads/bin/clang -Wl,--initial-memory=10485760 -matomics -mbulk-memory -pthread main.c -o ./out/main.wasm -L /home/dirkb/bin/wasi-sdk-20.0+threads/share/wasi-sysroot/lib/wasm32-wasi-threads

The exception happens on this WASI statement

call_indirect (param i32) (result i32)

A more detailed view in the debugger is:

image

Any idea what I do wrong?

From debugging it I am pretty sure that the tid and the start_arg pointer are correct.

@dbaeumer
Copy link
Author

dbaeumer commented Mar 8, 2023

I think I found the problem. Will close the issue for now and re-open if I still can't fix it on my end.

@dbaeumer dbaeumer closed this as completed Mar 8, 2023
@sbc100
Copy link
Member

sbc100 commented Mar 8, 2023

Was the problem related to signature of the myThreadFun entry point?

In emscripten we had an issue where some code was using the wrong signature for the thread entry point (e.g. not taking any arguments at all). In C apparently this is OK but when compiled down to wasm things break.

See emscripten-core/posixtestsuite#2 for some examples.

In emscripten we currently call out of JS and then back in again to invoke the thread entry point, which avoid the signature mismatch issue.

@dbaeumer
Copy link
Author

dbaeumer commented Mar 8, 2023

No, more beginner mistake. I need to import the memory otherwise the second instance creates a new copy of the shared array buffer :-)

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

2 participants