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

Calling a virtual method from MAIN_MODULE in SIDE_MODULE leads to crash #17907

Closed
nightelf3 opened this issue Sep 21, 2022 · 4 comments
Closed
Assignees

Comments

@nightelf3
Copy link

nightelf3 commented Sep 21, 2022

There is a crash inside the virtual function when the base class is located inside the main module and the derived class is located inside the side module.

Side module is loaded via dlopen.
I found the similar issue here: #17150
IMPORTANT: there is no crash when the side module is pre-loaded on startup

Version of emscripten/emsdk: 3.1.20 (https://github.com/emscripten-core/emscripten/releases/tag/3.1.20)

CBaseFilter.h

#pragma once

#define EXPORTED __attribute__ ((visibility("default")))

class EXPORTED CBaseFilter
{
public:
	virtual int GetValue();
	virtual int MakeValue();
};

filter.cpp

#include "CBaseFilter.h"

class CFilter : public CBaseFilter
{
	int MakeValue() override
	{
		return 123;
	}
};

extern "C"
{
EXPORTED int GetFilterValue()
{
	CFilter flt;
	return flt.GetValue();
}
}

main.cpp

#include <iostream>
#include <dlfcn.h>

#include "CBaseFilter.h"
int CBaseFilter::GetValue()
{
	return MakeValue();
}
int CBaseFilter::MakeValue()
{
	return 0;
}

int main()
{
	std::cout << "Start" << std::endl;
	auto hModule = dlopen("flt.wasm", RTLD_NOW);
	if (!hModule)
		std::cout << dlerror() << std::endl;

	auto sym = reinterpret_cast<int (*)()>(dlsym(hModule, "GetFilterValue"));
	std::cout << sym() << std::endl;

	dlclose(hModule);
	std::cout << "Finished!" << std::endl;
	return 0;
}

Build:

  • mkdir out
  • emcc Filter.cpp -fPIC -fvisibility=hidden -c -o out\Filter.o
  • emcc -sUSE_PTHREADS=1 -sSIDE_MODULE=1 -g -o out\flt.wasm out\Filter.o
  • emcc main.cpp -fPIC -fvisibility=hidden -c -o out\main.o
  • emcc -sMAIN_MODULE=1 -sPROXY_TO_PTHREAD -sUSE_PTHREADS=1 -g -o out\Filter.html out\main.o
    • P.S. no crash when:
      emcc -sMAIN_MODULE=1 -sPROXY_TO_PTHREAD -sUSE_PTHREADS=1 -g -o out\Filter.html out\main.o flt.wasm

Console:

Filter.worker.js:202 Uncaught RuntimeError: null function or function signature mismatch
    at CBaseFilter::GetValue() (Filter.wasm:0x75ff0)
    at GetFilterValue (4a5a1b72:0x38c)
    at __original_main (Filter.wasm:0x76198)
    at _main_thread (Filter.wasm:0x77840)
    at Object.invokeEntryPoint (Filter.js:4166:42)
    at self.onmessage (Filter.worker.js:155:35)
@sbc100 sbc100 self-assigned this Sep 21, 2022
@nightelf3
Copy link
Author

Hello @sbc100, are you able to reproduce the issue?
Do you need some additional info?

@sbc100
Copy link
Collaborator

sbc100 commented Dec 5, 2022

I didn't get change reproduce this yet no, but I think it might the same issue that I'm working on fixing in #18311 .. the fact the passing the module on the command line fixes the issue makes it very likely.

@sbc100
Copy link
Collaborator

sbc100 commented Dec 7, 2022

Should be fixed in #18311

@sbc100 sbc100 closed this as completed Dec 7, 2022
@nightelf3
Copy link
Author

@sbc100, thank you a lot!

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