Skip to content

Commit 181912b

Browse files
committed
Refactor with separate objects
Try to work around symbol/export trickery
1 parent b38aa77 commit 181912b

11 files changed

Lines changed: 69 additions & 42 deletions

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ jobs:
151151
- name: Install wasm32-wasip1 target
152152
run: rustup target add wasm32-wasip1
153153

154-
# Verify the output of the `./ci/rebuild-libcabi-realloc.sh` script is
154+
# Verify the output of the `./ci/rebuild-libwit-bindgen-cabi.sh` script is
155155
# up-to-date.
156156
- uses: ./.github/actions/install-wasi-sdk
157-
- run: ./ci/rebuild-libcabi-realloc.sh
157+
- run: ./ci/rebuild-libwit-bindgen-cabi.sh
158158
- run: git diff --exit-code
159159

160160
# Test various feature combinations, make sure they all build

ci/rebuild-libwit-bindgen-cabi.sh

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ set -ex
4242
version=$(./ci/print-current-version.sh | sed 's/\./_/g')
4343

4444
realloc=cabi_realloc_wit_bindgen_$version
45-
wasip3_task_set=wasip3_task_set_wit_bindgen_$version
46-
wasip3_task_get=wasip3_task_get_wit_bindgen_$version
4745

48-
cat >./crates/guest-rust/rt/src/wit_bindgen_cabi.rs <<-EOF
46+
rm -f crates/guest-rust/rt/src/wit_bindgen_*.{rs,o,c}
47+
rm -f crates/guest-rust/rt/src/libwit_bindgen_cabi.a
48+
49+
cat >./crates/guest-rust/rt/src/wit_bindgen_cabi_realloc.rs <<-EOF
4950
// This file is generated by $0
5051
5152
#[unsafe(no_mangle)]
@@ -57,20 +58,9 @@ pub unsafe extern "C" fn $realloc(
5758
) -> *mut u8 {
5859
crate::cabi_realloc(old_ptr, old_len, align, new_len)
5960
}
60-
61-
static mut WASIP3_TASK: *mut u8 = core::ptr::null_mut();
62-
63-
#[unsafe(no_mangle)]
64-
pub unsafe extern "C" fn $wasip3_task_set(ptr: *mut u8) -> *mut u8 {
65-
unsafe {
66-
let ret = WASIP3_TASK;
67-
WASIP3_TASK = ptr;
68-
ret
69-
}
70-
}
7161
EOF
7262

73-
cat >./crates/guest-rust/rt/src/wit_bindgen_cabi.c <<-EOF
63+
cat >./crates/guest-rust/rt/src/wit_bindgen_cabi_realloc.c <<-EOF
7464
// This file is generated by $0
7565
7666
#include <stdint.h>
@@ -81,25 +71,37 @@ __attribute__((__weak__, __export_name__("cabi_realloc")))
8171
void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
8272
return $realloc(ptr, old_size, align, new_size);
8373
}
74+
EOF
75+
76+
cat >./crates/guest-rust/rt/src/wit_bindgen_cabi_wasip3.c <<-EOF
77+
// This file is generated by $0
8478
85-
extern void *$wasip3_task_set(void *ptr);
79+
#include <stdlib.h>
80+
81+
static void *WASIP3_TASK = NULL;
8682
8783
__attribute__((__weak__))
8884
void *wasip3_task_set(void *ptr) {
89-
return $wasip3_task_set(ptr);
85+
void *ret = WASIP3_TASK;
86+
WASIP3_TASK = ptr;
87+
return ret;
9088
}
9189
EOF
9290

93-
rm -f crates/guest-rust/rt/src/wit_bindgen_cabi.o
94-
$WASI_SDK_PATH/bin/clang crates/guest-rust/rt/src/wit_bindgen_cabi.c \
95-
-O -c -o crates/guest-rust/rt/src/wit_bindgen_cabi.o
91+
build() {
92+
file=$1
93+
$WASI_SDK_PATH/bin/clang crates/guest-rust/rt/src/$1.c \
94+
-O -c -o crates/guest-rust/rt/src/$1.o
95+
# Remove the `producers` section. This appears to differ whether the host for
96+
# clang is either macOS or Linux. Not needed here anyway, so discard it to help
97+
# either host produce the same object.
98+
wasm-tools strip -d producers ./crates/guest-rust/rt/src/$1.o \
99+
-o ./crates/guest-rust/rt/src/$1.o
100+
}
96101

97-
# Remove the `producers` section. This appears to differ whether the host for
98-
# clang is either macOS or Linux. Not needed here anyway, so discard it to help
99-
# either host produce the same object.
100-
wasm-tools strip -d producers ./crates/guest-rust/rt/src/wit_bindgen_cabi.o \
101-
-o ./crates/guest-rust/rt/src/wit_bindgen_cabi.o
102+
build wit_bindgen_cabi_realloc
103+
build wit_bindgen_cabi_wasip3
102104

103-
rm -f crates/guest-rust/rt/src/libwit_bindgen_cabi.a
104105
$WASI_SDK_PATH/bin/llvm-ar crus crates/guest-rust/rt/src/libwit_bindgen_cabi.a \
105-
crates/guest-rust/rt/src/wit_bindgen_cabi.o
106+
crates/guest-rust/rt/src/wit_bindgen_cabi_realloc.o \
107+
crates/guest-rust/rt/src/wit_bindgen_cabi_wasip3.o
284 Bytes
Binary file not shown.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This file is generated by ./ci/rebuild-libwit-bindgen-cabi.sh
22

33
#include <stdint.h>
4+
#include <stdlib.h>
45

56
extern void *cabi_realloc_wit_bindgen_0_41_0(void *ptr, size_t old_size, size_t align, size_t new_size);
67

@@ -9,9 +10,11 @@ void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
910
return cabi_realloc_wit_bindgen_0_41_0(ptr, old_size, align, new_size);
1011
}
1112

12-
extern void *wasip3_task_set_wit_bindgen_0_41_0(void *ptr);
13+
static void *WASIP3_TASK = NULL;
1314

1415
__attribute__((__weak__))
1516
void *wasip3_task_set(void *ptr) {
16-
return wasip3_task_set_wit_bindgen_0_41_0(ptr);
17+
void *ret = WASIP3_TASK;
18+
WASIP3_TASK = ptr;
19+
return ret;
1720
}
36 Bytes
Binary file not shown.

crates/guest-rust/rt/src/wit_bindgen_cabi.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,3 @@ pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_41_0(
99
) -> *mut u8 {
1010
crate::cabi_realloc(old_ptr, old_len, align, new_len)
1111
}
12-
13-
static mut WASIP3_TASK: *mut u8 = core::ptr::null_mut();
14-
15-
#[unsafe(no_mangle)]
16-
pub unsafe extern "C" fn wasip3_task_set_wit_bindgen_0_41_0(ptr: *mut u8) -> *mut u8 {
17-
unsafe {
18-
let ret = WASIP3_TASK;
19-
WASIP3_TASK = ptr;
20-
ret
21-
}
22-
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This file is generated by ./ci/rebuild-libwit-bindgen-cabi.sh
2+
3+
#include <stdint.h>
4+
5+
extern void *cabi_realloc_wit_bindgen_0_41_0(void *ptr, size_t old_size, size_t align, size_t new_size);
6+
7+
__attribute__((__weak__, __export_name__("cabi_realloc")))
8+
void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
9+
return cabi_realloc_wit_bindgen_0_41_0(ptr, old_size, align, new_size);
10+
}
294 Bytes
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This file is generated by ./ci/rebuild-libwit-bindgen-cabi.sh
2+
3+
#[unsafe(no_mangle)]
4+
pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_41_0(
5+
old_ptr: *mut u8,
6+
old_len: usize,
7+
align: usize,
8+
new_len: usize,
9+
) -> *mut u8 {
10+
crate::cabi_realloc(old_ptr, old_len, align, new_len)
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This file is generated by ./ci/rebuild-libwit-bindgen-cabi.sh
2+
3+
#include <stdlib.h>
4+
5+
static void *WASIP3_TASK = NULL;
6+
7+
__attribute__((__weak__))
8+
void *wasip3_task_set(void *ptr) {
9+
void *ret = WASIP3_TASK;
10+
WASIP3_TASK = ptr;
11+
return ret;
12+
}

0 commit comments

Comments
 (0)