Skip to content

Commit

Permalink
Add sharedMemory option to allow threads with shared memory (#247)
Browse files Browse the repository at this point in the history
* Add `sharedMemory` option to allow threads with shared memory

* Enable thread enabled Swift SDK testing in CI

* Support shared memory in test harness

* Don't use symlink directory in the path of the program name

This is a workaround for the issue that the argv0 program path
containing a symlink directory in the path causes `Bundle.main` to
crash.
  • Loading branch information
kateinoigakukun authored May 27, 2024
1 parent 8449f87 commit ea06982
Show file tree
Hide file tree
Showing 6 changed files with 551 additions and 475 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ jobs:
id: DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasi
download-url: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasi.artifactbundle.zip"
wasi-backend: Node
# TODO: Enable this once we support threads in JavaScriptKit
# - os: ubuntu-22.04
# toolchain: DEVELOPMENT-SNAPSHOT-2024-05-01-a
# swift-sdk:
# id: DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasip1-threads
# download-url: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasip1-threads.artifactbundle.zip"
# wasi-backend: Node
- os: ubuntu-22.04
toolchain: DEVELOPMENT-SNAPSHOT-2024-05-01-a
swift-sdk:
id: DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasip1-threads
download-url: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasip1-threads.artifactbundle.zip"
wasi-backend: Node

runs-on: ${{ matrix.entry.os }}
env:
Expand Down
39 changes: 34 additions & 5 deletions IntegrationTests/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { SwiftRuntime } from "javascript-kit-swift"
import { WASI as NodeWASI } from "wasi"
import { WASI as MicroWASI, useAll } from "uwasi"
import * as fs from "fs/promises"
import path from "path";

const WASI = {
MicroWASI: ({ programName }) => {
const wasi = new MicroWASI({
args: [programName],
args: [path.basename(programName)],
env: {},
features: [useAll()],
})
Expand All @@ -21,7 +22,7 @@ const WASI = {
},
Node: ({ programName }) => {
const wasi = new NodeWASI({
args: [programName],
args: [path.basename(programName)],
env: {},
preopens: {
"/": "./",
Expand Down Expand Up @@ -51,21 +52,49 @@ const selectWASIBackend = () => {
return WASI.Node;
};

function isUsingSharedMemory(module) {
const imports = WebAssembly.Module.imports(module);
for (const entry of imports) {
if (entry.module === "env" && entry.name === "memory" && entry.kind == "memory") {
return true;
}
}
return false;
}

export const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) => {
const swift = new SwiftRuntime();
// Fetch our Wasm File
const wasmBinary = await fs.readFile(wasmPath);
const wasi = wasiConstructor({ programName: wasmPath });

// Instantiate the WebAssembly file
let { instance } = await WebAssembly.instantiate(wasmBinary, {
const module = await WebAssembly.compile(wasmBinary);

const importObject = {
wasi_snapshot_preview1: wasi.wasiImport,
javascript_kit: swift.importObjects(),
benchmark_helper: {
noop: () => {},
noop_with_int: (_) => {},
}
});
};

if (isUsingSharedMemory(module)) {
importObject["env"] = {
// We don't have JS API to get memory descriptor of imported memory
// at this moment, so we assume 256 pages (16MB) memory is enough
// large for initial memory size.
memory: new WebAssembly.Memory({ initial: 256, maximum: 16384, shared: true }),
};
importObject["wasi"] = {
"thread-spawn": () => {
throw new Error("thread-spawn not implemented");
}
}
}

// Instantiate the WebAssembly file
const instance = await WebAssembly.instantiate(module, importObject);

swift.setInstance(instance);
// Start the WebAssembly WASI instance!
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test:
unittest:
@echo Running unit tests
swift build --build-tests -Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor -Xlinker --export-if-defined=main -Xlinker --export-if-defined=__main_argc_argv --static-swift-stdlib -Xswiftc -static-stdlib $(SWIFT_BUILD_FLAGS)
node --experimental-wasi-unstable-preview1 scripts/test-harness.mjs ./.build/wasm32-unknown-wasi/debug/JavaScriptKitPackageTests.wasm
node --experimental-wasi-unstable-preview1 scripts/test-harness.mjs ./.build/debug/JavaScriptKitPackageTests.wasm

.PHONY: benchmark_setup
benchmark_setup:
Expand Down
Loading

0 comments on commit ea06982

Please sign in to comment.