emscripten-core / emscripten-core/emscripten

Doubts related to dlopen and dlclose

Open
#23,793 16 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 6h
Merged PRs (30d)
109

Description

Hey @sbc100,

I think you might be able to help me out here.

So clang-repl can do the undo operation to surpass redefinitions and stuff. For eg
```
clang-repl> int y = 50;
clang-repl> const char* y = "Hello World";
In file included from <<< inputs >>>:1:
input_line_6:1:13: error: redefinition of 'y' with a different type: 'const char *' vs 'int'
1 | const char* y = "Hello World";
| ^
input_line_5:1:5: note: previous definition is here
1 | int y = 50;
| ^
error: Parsing failed.

clang-repl> extern "C" int printf(const char*, ...);
clang-repl> int x = 50;
clang-repl> %undo
clang-repl> const char* x = "Hello World";
clang-repl> auto r = printf("%s\n", x);
Hello World
```

Now I was trying to extend the same functionality while we're running clang-repl in the browser.

So basically
1) for executing we use AddModule : I think I might have explained the approach here in the past. We use `dlopen` for this. Each cell/code block gives us a side module which should be loaded up top of the main module using dlopen

`code -> PTU -> LLVM IR -> wasm object -> wasm binary -> loaded on top of main module using dlopen`

This is done here https://github.com/llvm/llvm-project/blob/main/clang/lib/Interpreter/Wasm.cpp#L120-L126

2) for [undo](https://github.com/llvm/llvm-project/blob/44b9f5eeab63dbf7d4e4ebc87dfedca5c42708b6/clang/lib/Interpreter/Interpreter.cpp#L695) we use [removeModule](https://github.com/llvm/llvm-project/blob/44b9f5eeab63dbf7d4e4ebc87dfedca5c42708b6/clang/lib/Interpreter/Wasm.cpp#L131) : My approach here was as we load the module, we just need to keep track of the handle and unload it. I thought we can put `dlclose` to use for the same.

**So as of now as removeModule was not implemented**, but I thought of introducing this small diff for implementation

https://github.com/llvm/llvm-project/blob/main/clang/lib/Interpreter/Wasm.cpp#L120-L126 (Has few debug logs too)

i) So I just keep track of the modules
ii) locate the handle to the unloaded.
iii) Unload using dlclose

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.