Skip to content

Commit 5117977

Browse files
committed
Import a data symbol for libdl.so
This commit replaces the `__wasm_set_libraries` protocol with a direct data symbol import of the structure necessary for `libdl.so`. This is synthesized on-the-fly as a global which is imported into the final module. The build is additionally updated to wasi-sdk-33
1 parent 6adf3df commit 5117977

12 files changed

Lines changed: 756 additions & 780 deletions

File tree

.github/actions/install-rust/action.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ runs:
4747
shell: bash
4848

4949
- run: |
50-
curl -L -o wasi-sdk.tar.gz https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-27/wasi-sdk-27.0-x86_64-linux.tar.gz
50+
curl -L -o wasi-sdk.tar.gz https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-33/wasi-sdk-33.0-x86_64-linux.tar.gz
5151
tar xf wasi-sdk.tar.gz
52-
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-27.0-x86_64-linux" >> $GITHUB_ENV
52+
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-33.0-x86_64-linux" >> $GITHUB_ENV
5353
if: runner.os == 'Linux'
5454
shell: bash
5555
working-directory: ${{ runner.tool_cache }}
5656
- run: |
57-
curl -L -o wasi-sdk.tar.gz https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-27/wasi-sdk-27.0-x86_64-macos.tar.gz
57+
curl -L -o wasi-sdk.tar.gz https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-33/wasi-sdk-33.0-x86_64-macos.tar.gz
5858
tar xf wasi-sdk.tar.gz
59-
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-27.0-x86_64-macos" >> $GITHUB_ENV
59+
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-33.0-x86_64-macos" >> $GITHUB_ENV
6060
if: runner.os == 'macOS'
6161
shell: bash
6262
working-directory: ${{ runner.tool_cache }}
6363
- run: |
64-
curl -L -o wasi-sdk.tar.gz https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-27/wasi-sdk-27.0-x86_64-windows.tar.gz
64+
curl -L -o wasi-sdk.tar.gz https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-33/wasi-sdk-33.0-x86_64-windows.tar.gz
6565
tar xf wasi-sdk.tar.gz
66-
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-27.0-x86_64-windows" >> $GITHUB_ENV
66+
echo "WASI_SDK_PATH=`pwd`/wasi-sdk-33.0-x86_64-windows" >> $GITHUB_ENV
6767
if: runner.os == 'Windows'
6868
shell: bash
6969
working-directory: ${{ runner.tool_cache }}

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ jobs:
194194
submodules: true
195195
- uses: ./.github/actions/install-rust
196196
with:
197-
toolchain: 1.89.0
197+
toolchain: 1.97.0
198198
- run: rustup target add wasm32-wasip1
199199
- run: cd crates/wit-component/dl && bash check.sh
200200

crates/wit-component/dl/build.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
set -ex
99

10-
CARGO_PROFILE_RELEASE_LTO=true RUSTFLAGS="-C relocation-model=pic" cargo build --release --target=wasm32-wasip1
11-
$WASI_SDK_PATH/bin/clang -shared -o $1 -Wl,--whole-archive ../../../target/wasm32-wasip1/release/libdl.a -Wl,--no-whole-archive
10+
CARGO_PROFILE_RELEASE_LTO=true cargo build --release --target=wasm32-wasip2
11+
$WASI_SDK_PATH/bin/wasm32-wasip2-clang -shared \
12+
-o $1 \
13+
-Wl,--whole-archive \
14+
../../../target/wasm32-wasip2/release/libdl.a \
15+
-Wl,--no-whole-archive \
16+
-Wl,--allow-undefined
1217
cargo run --manifest-path ../../../Cargo.toml -- strip $1 -o $1
1318
cargo run --manifest-path ../../../Cargo.toml -- strip --delete name $1 -o $1

crates/wit-component/dl/check.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set -ex
22

3-
bash ./build.sh ../../../target/wasm32-wasip1/release/tmp.so
4-
if diff ../../../target/wasm32-wasip1/release/tmp.so ../libdl.so; then
3+
bash ./build.sh ../../../target/wasm32-wasip2/release/tmp.so
4+
if diff ../../../target/wasm32-wasip2/release/tmp.so ../libdl.so; then
55
exit 0
66
else
77
echo "libdl.so is out-of-date; please run crates/wit-component/dl/build.sh to update it">&2

crates/wit-component/dl/src/lib.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,17 @@ pub struct Libraries {
4848
}
4949

5050
static mut ERROR: *const c_char = ptr::null();
51-
static mut LIBRARIES: *const Libraries = ptr::null();
51+
52+
unsafe extern "C" {
53+
#[link_name = "__wasm_libdl_libraries"]
54+
static LIBRARIES: Libraries;
55+
}
5256

5357
unsafe fn invalid_handle(library: *const c_void) -> bool {
5458
unsafe {
55-
if LIBRARIES.is_null() {
56-
panic!(
57-
"`__wasm_set_libraries` should have been called during \
58-
instantiation with a non-NULL value"
59-
);
60-
}
61-
6259
let library = library as *const Library;
63-
if (0..(*LIBRARIES).count)
64-
.any(|index| (*LIBRARIES).libraries.add(usize::try_from(index).unwrap()) == library)
60+
if (0..LIBRARIES.count)
61+
.any(|index| LIBRARIES.libraries.add(usize::try_from(index).unwrap()) == library)
6562
{
6663
false
6764
} else {
@@ -95,13 +92,6 @@ pub extern "C" fn dlerror() -> *const c_char {
9592
#[unsafe(no_mangle)]
9693
pub unsafe extern "C" fn dlopen(name: *const c_char, flags: c_int) -> *const c_void {
9794
unsafe {
98-
if LIBRARIES.is_null() {
99-
panic!(
100-
"`__wasm_set_libraries` should have been called during \
101-
instantiation with a non-NULL value"
102-
);
103-
}
104-
10595
if (flags & !(RTLD_LAZY | RTLD_NOW | RTLD_GLOBAL)) != 0 {
10696
// TODO
10797
ERROR = c"dlopen flags not yet supported".as_ptr();
@@ -111,8 +101,8 @@ pub unsafe extern "C" fn dlopen(name: *const c_char, flags: c_int) -> *const c_v
111101
let name = CStr::from_ptr(name);
112102
let name = name.to_bytes();
113103
let libraries = slice::from_raw_parts(
114-
(*LIBRARIES).libraries,
115-
usize::try_from((*LIBRARIES).count).unwrap(),
104+
LIBRARIES.libraries,
105+
usize::try_from(LIBRARIES.count).unwrap(),
116106
);
117107
if let Ok(index) = libraries.binary_search_by(|library| {
118108
slice::from_raw_parts(
@@ -166,17 +156,6 @@ pub unsafe extern "C" fn dlsym(library: *const c_void, name: *const c_char) -> *
166156
}
167157
}
168158

169-
/// # Safety
170-
///
171-
/// `libraries` must be a valid pointer to a `Libraries` object, and this
172-
/// pointer must remain valid for the lifetime of the process.
173-
#[unsafe(no_mangle)]
174-
pub unsafe extern "C" fn __wasm_set_libraries(libraries: *const Libraries) {
175-
unsafe {
176-
LIBRARIES = libraries;
177-
}
178-
}
179-
180159
#[cfg(target_arch = "wasm32")]
181160
#[panic_handler]
182161
fn panic(_info: &core::panic::PanicInfo) -> ! {

crates/wit-component/libdl.so

-343 Bytes
Binary file not shown.

crates/wit-component/src/linking.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -354,25 +354,33 @@ fn make_env_module<'a>(
354354
exports.export(CABI_REALLOC, ExportKind::Func, index);
355355
}
356356

357+
let mut add_global_export = |name: &str, value, mutable| {
358+
let index = globals.len();
359+
globals.global(
360+
wasm_encoder::GlobalType {
361+
val_type: ValType::I32,
362+
mutable,
363+
shared: false,
364+
},
365+
&const_u32(value),
366+
);
367+
exports.export(name, ExportKind::Global, index);
368+
};
369+
357370
let dl_openables = DlOpenables::new(table_offset, memory_offset, metadata);
358371

372+
if metadata.iter().any(|m| m.needs_libdl_libraries) {
373+
add_global_export(
374+
metadata::LIBDL_LIBRARIES,
375+
dl_openables.libraries_address,
376+
true,
377+
);
378+
}
379+
359380
table_offset += dl_openables.function_count;
360381
memory_offset += u32::try_from(dl_openables.buffer.len()).unwrap();
361382

362383
let memory_size = {
363-
let mut add_global_export = |name: &str, value, mutable| {
364-
let index = globals.len();
365-
globals.global(
366-
wasm_encoder::GlobalType {
367-
val_type: ValType::I32,
368-
mutable,
369-
shared: false,
370-
},
371-
&const_u32(value),
372-
);
373-
exports.export(name, ExportKind::Global, index);
374-
};
375-
376384
if metadata.iter().any(|m| m.needs_stack_pointer) {
377385
add_global_export(metadata::STACK_POINTER, stack_size_bytes, true);
378386
}
@@ -566,7 +574,6 @@ fn make_init_module(
566574
types.ty().function([], []);
567575
let thunk_ty = 0;
568576
types.ty().function([ValType::I32], []);
569-
let one_i32_param_ty = 1;
570577
let mut type_offset = 2;
571578

572579
for metadata in metadata {
@@ -736,18 +743,6 @@ fn make_init_module(
736743
)));
737744
}
738745

739-
if metadata.has_set_libraries {
740-
ctor_calls.push(Ins::I32Const(
741-
i32::try_from(dl_openables.libraries_address).unwrap(),
742-
));
743-
ctor_calls.push(Ins::Call(add_function_import(
744-
&mut imports,
745-
metadata.name,
746-
metadata::SET_LIBRARIES,
747-
one_i32_param_ty,
748-
)));
749-
}
750-
751746
for import in &metadata.memory_address_imports {
752747
let (exporter, _) = find_offset_exporter(import, exporters)?;
753748

@@ -1756,6 +1751,7 @@ impl Linker {
17561751
metadata::HEAP_END,
17571752
metadata::STACK_HIGH,
17581753
metadata::STACK_LOW,
1754+
metadata::LIBDL_LIBRARIES,
17591755
]
17601756
.into_iter()
17611757
.map(|name| Item {

crates/wit-component/src/linking/metadata.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub const APPLY_DATA_RELOCS: &str = "__wasm_apply_data_relocs";
3232
pub const CALL_CTORS: &str = "__wasm_call_ctors";
3333
pub const INITIALIZE: &str = "_initialize";
3434
pub const START: &str = "_start";
35-
pub const SET_LIBRARIES: &str = "__wasm_set_libraries";
35+
pub const LIBDL_LIBRARIES: &str = "__wasm_libdl_libraries";
3636
pub const INIT_TASK: &str = "__wasm_init_task";
3737
pub const INIT_ASYNC_TASK: &str = "__wasm_init_async_task";
3838

@@ -210,8 +210,8 @@ pub struct Metadata<'a> {
210210
/// Whether this module exports `_start`
211211
pub has_wasi_start: bool,
212212

213-
/// Whether this module exports `__wasm_set_libraries`
214-
pub has_set_libraries: bool,
213+
/// Whether this module imports `__wasm_libdl_libraries`
214+
pub needs_libdl_libraries: bool,
215215

216216
/// Whether this module exports `__wasm_init_task`
217217
pub has_init_task: bool,
@@ -286,7 +286,7 @@ impl<'a> Metadata<'a> {
286286
has_ctors: false,
287287
has_initialize: false,
288288
has_wasi_start: false,
289-
has_set_libraries: false,
289+
needs_libdl_libraries: false,
290290
has_init_task: false,
291291
has_component_exports,
292292
is_asyncified: false,
@@ -463,6 +463,10 @@ impl<'a> Metadata<'a> {
463463
self::HEAP_END => result.needs_heap_end = true,
464464
self::STACK_HIGH => result.needs_stack_high = true,
465465
self::STACK_LOW => result.needs_stack_low = true,
466+
self::LIBDL_LIBRARIES => {
467+
result.needs_libdl_libraries = true;
468+
}
469+
466470
_ => {
467471
result.memory_address_imports.insert(name);
468472
}
@@ -547,7 +551,6 @@ impl<'a> Metadata<'a> {
547551
self::CALL_CTORS => result.has_ctors = true,
548552
self::INITIALIZE => result.has_initialize = true,
549553
self::START => result.has_wasi_start = true,
550-
self::SET_LIBRARIES => result.has_set_libraries = true,
551554
_ => {
552555
if export.name == self::INIT_TASK {
553556
result.has_init_task = true;

0 commit comments

Comments
 (0)