Skip to content

Commit d2b0961

Browse files
authored
Merge pull request #42 from alexcrichton/test-wasm
Run CI tests in wasm as well
2 parents 4bf7569 + 5258920 commit d2b0961

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

.github/workflows/main.yml

+19-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,28 @@ jobs:
1818
rust: stable
1919
- os: windows-latest
2020
rust: stable
21+
- os: ubuntu-latest
22+
rust: stable
23+
target: wasm32-wasip1
2124
steps:
2225
- uses: actions/checkout@v4
23-
- name: Install Rust (
24-
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
26+
- run: rustup update ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }}
2527
shell: bash
28+
29+
# Configure cross-builds by adding the rustup target and configuring future
30+
# cargo invocations.
31+
- run: |
32+
rustup target add ${{ matrix.target }}
33+
echo CARGO_BUILD_TARGET=${{ matrix.target }} >> $GITHUB_ENV
34+
if: matrix.target != ''
35+
36+
# For wasm install wasmtime as a test runner and configure it with Cargo.
37+
- name: Setup `wasmtime`
38+
uses: bytecodealliance/actions/wasmtime/setup@v1
39+
if: matrix.target == 'wasm32-wasip1'
40+
- run: echo CARGO_TARGET_WASM32_WASIP1_RUNNER=wasmtime >> $GITHUB_ENV
41+
if: matrix.target == 'wasm32-wasip1'
42+
2643
- run: cargo test
2744
- run: cargo test --features debug
2845
- run: cargo test --features global

src/global.rs

+22-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::Dlmalloc;
22
use core::alloc::{GlobalAlloc, Layout};
3-
use core::ops::{Deref, DerefMut};
3+
use core::ptr;
44

55
pub use crate::sys::enable_alloc_after_fork;
66

@@ -10,52 +10,47 @@ pub use crate::sys::enable_alloc_after_fork;
1010
/// implements the `GlobalAlloc` trait in the standard library.
1111
pub struct GlobalDlmalloc;
1212

13+
static mut DLMALLOC: Dlmalloc = Dlmalloc::new();
14+
1315
unsafe impl GlobalAlloc for GlobalDlmalloc {
1416
#[inline]
1517
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
16-
<Dlmalloc>::malloc(&mut get(), layout.size(), layout.align())
18+
let _guard = lock();
19+
let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
20+
(*dlmalloc).malloc(layout.size(), layout.align())
1721
}
1822

1923
#[inline]
2024
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
21-
<Dlmalloc>::free(&mut get(), ptr, layout.size(), layout.align())
25+
let _guard = lock();
26+
let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
27+
(*dlmalloc).free(ptr, layout.size(), layout.align())
2228
}
2329

2430
#[inline]
2531
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
26-
<Dlmalloc>::calloc(&mut get(), layout.size(), layout.align())
32+
let _guard = lock();
33+
let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
34+
(*dlmalloc).calloc(layout.size(), layout.align())
2735
}
2836

2937
#[inline]
3038
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
31-
<Dlmalloc>::realloc(&mut get(), ptr, layout.size(), layout.align(), new_size)
39+
let _guard = lock();
40+
let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
41+
(*dlmalloc).realloc(ptr, layout.size(), layout.align(), new_size)
3242
}
3343
}
3444

35-
static mut DLMALLOC: Dlmalloc = Dlmalloc::new();
36-
37-
struct Instance;
38-
39-
unsafe fn get() -> Instance {
45+
unsafe fn lock() -> impl Drop {
4046
crate::sys::acquire_global_lock();
41-
Instance
42-
}
4347

44-
impl Deref for Instance {
45-
type Target = Dlmalloc;
46-
fn deref(&self) -> &Dlmalloc {
47-
unsafe { &DLMALLOC }
48+
struct Guard;
49+
impl Drop for Guard {
50+
fn drop(&mut self) {
51+
crate::sys::release_global_lock()
52+
}
4853
}
49-
}
5054

51-
impl DerefMut for Instance {
52-
fn deref_mut(&mut self) -> &mut Dlmalloc {
53-
unsafe { &mut DLMALLOC }
54-
}
55-
}
56-
57-
impl Drop for Instance {
58-
fn drop(&mut self) {
59-
crate::sys::release_global_lock()
60-
}
55+
Guard
6156
}

src/wasm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub fn release_global_lock() {
6868
assert!(!cfg!(target_feature = "atomics"));
6969
}
7070

71+
#[allow(missing_docs)]
7172
#[cfg(feature = "global")]
7273
pub unsafe fn enable_alloc_after_fork() {
7374
// single threaded, no need!

tests/global.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn strings() {
2626
}
2727

2828
#[test]
29+
#[cfg(not(target_family = "wasm"))]
2930
fn threads() {
3031
assert!(thread::spawn(|| panic!()).join().is_err());
3132
}

0 commit comments

Comments
 (0)