space-data-module-sdk 0.8.25 → 0.8.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/package.json +1 -1
- package/src/index.d.ts +2 -0
- package/src/licensing/records.js +10 -1
- package/src/testing/buildWasmEdgeRunner.js +46 -9
- package/src/testing/docker/wasmedge-parity.Dockerfile +25 -5
- package/src/testing/native/wasmedge-0.16.4-atomic-memarg-offset.patch +46 -0
- package/src/testing/native/wasmedge-0.16.4-fault-jmp.patch +35 -0
- package/src/testing/native/wasmedge-0.16.4-stop-token.patch +111 -0
- package/src/testing/parityHarness.js +7 -0
package/README.md
CHANGED
|
@@ -145,6 +145,31 @@ Source-built modules can set this on an emitted output frame with
|
|
|
145
145
|
`plugin_set_output_stream_frame(outputIndex, sequence, endOfStream)` or set the
|
|
146
146
|
raw frame id with `plugin_set_output_frame_id(outputIndex, frameId)`.
|
|
147
147
|
|
|
148
|
+
### Size-prefixed records
|
|
149
|
+
|
|
150
|
+
A size-prefixed FlatBuffer is not an unprefixed buffer with a length written in
|
|
151
|
+
front of it. FlatBuffers aligns each field relative to the start of the buffer.
|
|
152
|
+
A size-prefixed finish lays the record out so that the 4-byte prefix comes
|
|
153
|
+
before an aligned root:
|
|
154
|
+
|
|
155
|
+
- C++: `FinishSizePrefixed`;
|
|
156
|
+
- JS: `finishSizePrefixed`;
|
|
157
|
+
- `flatc --binary --size-prefixed`.
|
|
158
|
+
|
|
159
|
+
Writing a length in front of an unprefixed buffer moves every field by 4
|
|
160
|
+
bytes. Eight-byte scalars and vectors, such as `double` and `long`, are then
|
|
161
|
+
misaligned. `VerifySizePrefixedBuffer` with alignment checks, which a module's
|
|
162
|
+
generated readers run, refuses the record.
|
|
163
|
+
|
|
164
|
+
- Build size-prefixed frames with the generated builder's size-prefixed finish,
|
|
165
|
+
or with `flatc --binary --size-prefixed` from JSON.
|
|
166
|
+
- flatc-wasm's `generateBinary(schema, json, { sizePrefix: true })`, the
|
|
167
|
+
default, writes the length in front in JS and does not produce such a record.
|
|
168
|
+
For a size-prefixed record, run flatc with `--size-prefixed` through
|
|
169
|
+
`runCommand`. Otherwise pass `sizePrefix: false` for an unprefixed record.
|
|
170
|
+
- Forward a module's size-prefixed output frames unchanged. Do not strip the
|
|
171
|
+
prefix or add one.
|
|
172
|
+
|
|
148
173
|
For simple single-input / single-output methods, command mode also supports a
|
|
149
174
|
raw shortcut:
|
|
150
175
|
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -3055,6 +3055,8 @@ export function encodeLicensingChallengeRequest(options: {
|
|
|
3055
3055
|
requestedTimeoutMs: number;
|
|
3056
3056
|
requestedAtMs: number;
|
|
3057
3057
|
providerPeerId: string;
|
|
3058
|
+
/** The requester's $EPM FlatBuffer; an allowlisted module requires it. */
|
|
3059
|
+
requesterEpm?: Uint8Array;
|
|
3058
3060
|
}): Uint8Array;
|
|
3059
3061
|
|
|
3060
3062
|
export function decodeLicensingChallengeMessage(
|
package/src/licensing/records.js
CHANGED
|
@@ -60,6 +60,15 @@ export function encodeLicensingChallengeRequest(options) {
|
|
|
60
60
|
const providerPeerIdOffset = builder.createString(
|
|
61
61
|
normalizeRequiredString(options.providerPeerId, "providerPeerId"),
|
|
62
62
|
);
|
|
63
|
+
// The requester's $EPM, re-sent per grant: an allowlisted module needs it to
|
|
64
|
+
// bind the requester's xpub to the signing key it is about to prove.
|
|
65
|
+
const requesterEpmOffset =
|
|
66
|
+
options.requesterEpm == null
|
|
67
|
+
? 0
|
|
68
|
+
: LCH.createRequesterEpmVector(
|
|
69
|
+
builder,
|
|
70
|
+
cloneRequiredBytes(options.requesterEpm, "requesterEpm"),
|
|
71
|
+
);
|
|
63
72
|
const root = LCH.createLCH(
|
|
64
73
|
builder,
|
|
65
74
|
licensingChallengeMessageType.Request,
|
|
@@ -79,7 +88,7 @@ export function encodeLicensingChallengeRequest(options) {
|
|
|
79
88
|
providerPeerIdOffset,
|
|
80
89
|
0,
|
|
81
90
|
0,
|
|
82
|
-
|
|
91
|
+
requesterEpmOffset,
|
|
83
92
|
);
|
|
84
93
|
LCH.finishLCHBuffer(builder, root);
|
|
85
94
|
return builder.asUint8Array();
|
|
@@ -276,15 +276,47 @@ export async function buildWasmEdgeEmscriptenPthreadRunner(options = {}) {
|
|
|
276
276
|
const WASMEDGE_THREADS_REVISION = "be85c2fbba68318f103b4a766728f6946e65abf8";
|
|
277
277
|
const runtimeBuilds = new Map();
|
|
278
278
|
|
|
279
|
-
//
|
|
280
|
-
//
|
|
281
|
-
//
|
|
279
|
+
// The SDN patch series for WasmEdge 0.16.4 (sdn-server/internal/wasmrt/SUBSTRATE.md),
|
|
280
|
+
// applied here byte-identical from SDN's testdata/ (kept local so this build
|
|
281
|
+
// has no cross-repo file dependency). Order matters (02 and 04 both touch
|
|
282
|
+
// lib/llvm/compiler.cpp) and matches SDN's own build-static-wasmedge.sh.
|
|
283
|
+
// 01-atomic-wait compare/register/sleep under one mutex; a notify
|
|
284
|
+
// wakes a waiter without a store. Fixes upstream's
|
|
285
|
+
// lost wakeups — this build's interpreter is the
|
|
286
|
+
// only execution mode, so this is the one patch
|
|
287
|
+
// with an observable effect here.
|
|
288
|
+
// 02-stop-token a stop is sticky, read (never consumed) by the
|
|
289
|
+
// interpreter and by Interruptible AOT code.
|
|
290
|
+
// 03-fault-jmp darwin _setjmp/_longjmp signal-stack-flag fix
|
|
291
|
+
// for the native-fault path AOT execution uses.
|
|
292
|
+
// 04-atomic-memarg-offset AOT memory.atomic.notify/wait honour the
|
|
293
|
+
// instruction's memarg offset (lib/llvm/compiler.cpp).
|
|
294
|
+
//
|
|
295
|
+
// This runner is built with -DWASMEDGE_USE_LLVM=OFF: in 0.16.4 that flag also
|
|
296
|
+
// gates WASMEDGE_BUILD_AOT_RUNTIME (WasmEdge's CMakeLists.txt), so the linked
|
|
297
|
+
// library can neither compile nor LOAD AOT code. Patches 02-04 therefore
|
|
298
|
+
// patch code this runner never executes; only 01 is load-bearing today. They
|
|
299
|
+
// stay applied anyway so this isolated dependency's WasmEdge checkout tracks
|
|
300
|
+
// SDN's full patched baseline (never drifts if AOT is enabled here later),
|
|
301
|
+
// and 04 specifically is why no memarg-offset regression test is added
|
|
302
|
+
// alongside it: there is no AOT lane in this SDK to run SDN's substrate probe
|
|
303
|
+
// against (module-sdk-oracle: tri-runtime parity is browser / native
|
|
304
|
+
// interpreter WasmEdge / Docker interpreter WasmEdge — none compile AOT).
|
|
305
|
+
const WASMEDGE_PATCH_NAMES = Object.freeze([
|
|
306
|
+
"wasmedge-0.16.4-atomic-wait.patch",
|
|
307
|
+
"wasmedge-0.16.4-stop-token.patch",
|
|
308
|
+
"wasmedge-0.16.4-fault-jmp.patch",
|
|
309
|
+
"wasmedge-0.16.4-atomic-memarg-offset.patch",
|
|
310
|
+
]);
|
|
311
|
+
|
|
282
312
|
async function prepareWasmEdgeThreadsRuntime(options) {
|
|
283
313
|
if ((options.wasmedgeIncludeDir ?? process.env.WASMEDGE_INCLUDE_DIR) &&
|
|
284
314
|
(options.wasmedgeLibDir ?? process.env.WASMEDGE_LIB_DIR)) return {};
|
|
285
|
-
const
|
|
286
|
-
const
|
|
287
|
-
|
|
315
|
+
const patchPaths = WASMEDGE_PATCH_NAMES.map((name) => path.join(__dirname, "native", name));
|
|
316
|
+
const patchBytes = await Promise.all(patchPaths.map((p) => readFile(p)));
|
|
317
|
+
const digestHash = createHash("sha256").update(WASMEDGE_THREADS_REVISION);
|
|
318
|
+
for (const bytes of patchBytes) digestHash.update(bytes);
|
|
319
|
+
const digest = digestHash.update(`${process.platform}-${process.arch}`).digest("hex");
|
|
288
320
|
if (!runtimeBuilds.has(digest)) {
|
|
289
321
|
const build = (async () => {
|
|
290
322
|
const root = path.join(os.tmpdir(), "sdm-wasmedge-runtimes", digest);
|
|
@@ -324,8 +356,10 @@ async function prepareWasmEdgeThreadsRuntime(options) {
|
|
|
324
356
|
}
|
|
325
357
|
const revision = (await run("git", ["-C", source, "rev-parse", "HEAD"])).stdout.trim();
|
|
326
358
|
if (revision !== WASMEDGE_THREADS_REVISION) throw new Error(`Unexpected WasmEdge source revision: ${revision}`);
|
|
327
|
-
|
|
328
|
-
|
|
359
|
+
for (const patchPath of patchPaths) {
|
|
360
|
+
try { await run("git", ["-C", source, "apply", "--reverse", "--check", patchPath]); }
|
|
361
|
+
catch { await run("git", ["-C", source, "apply", patchPath]); }
|
|
362
|
+
}
|
|
329
363
|
await run("cmake", ["-S", source, "-B", buildDir, "-G", "Ninja",
|
|
330
364
|
"-DCMAKE_BUILD_TYPE=Release", `-DCMAKE_INSTALL_PREFIX=${prefix}`,
|
|
331
365
|
process.platform === "darwin" ? "-DCMAKE_CXX_FLAGS=-Wno-invalid-specialization" : "-DCMAKE_CXX_FLAGS=-Wno-error=maybe-uninitialized -Wno-error=array-bounds",
|
|
@@ -333,7 +367,10 @@ async function prepareWasmEdgeThreadsRuntime(options) {
|
|
|
333
367
|
"-DWASMEDGE_BUILD_PLUGINS=OFF", "-DWASMEDGE_BUILD_TOOLS=OFF", "-DWASMEDGE_FORCE_DISABLE_LTO=ON"]);
|
|
334
368
|
await run("cmake", ["--build", buildDir, "-j", "4"]);
|
|
335
369
|
await run("cmake", ["--install", buildDir]);
|
|
336
|
-
|
|
370
|
+
const patchesSha256 = Object.fromEntries(
|
|
371
|
+
WASMEDGE_PATCH_NAMES.map((name, i) => [name, createHash("sha256").update(patchBytes[i]).digest("hex")]),
|
|
372
|
+
);
|
|
373
|
+
await writeFile(marker, JSON.stringify({ revision, patchesSha256 }));
|
|
337
374
|
}
|
|
338
375
|
} finally {
|
|
339
376
|
if (held) await rm(lock, { recursive: true, force: true });
|
|
@@ -35,12 +35,31 @@ ENV PATH="/opt/wasmedge/bin:${PATH}" \
|
|
|
35
35
|
LD_LIBRARY_PATH="/opt/wasmedge/lib64:/opt/wasmedge/lib"
|
|
36
36
|
|
|
37
37
|
COPY native/wasmedge_wasi_threads_runner.c /tmp/sdm-wasi-threads-runner.c
|
|
38
|
-
|
|
39
|
-
#
|
|
40
|
-
#
|
|
38
|
+
# The SDN WasmEdge 0.16.4 patch series, byte-identical to SDN's testdata/
|
|
39
|
+
# (sdn-server/internal/wasmrt/SUBSTRATE.md): 01-atomic-wait, 02-stop-token,
|
|
40
|
+
# 03-fault-jmp, 04-atomic-memarg-offset. Order matters (02 and 04 both touch
|
|
41
|
+
# lib/llvm/compiler.cpp) and matches SDN's build-static-wasmedge.sh.
|
|
42
|
+
# The ordinary CLI (above) keeps its release library. Only the SDK thread
|
|
43
|
+
# runner links this patched build, matching the native (non-Docker) build in
|
|
44
|
+
# buildWasmEdgeRunner.js. Configured -DWASMEDGE_USE_LLVM=OFF, which in 0.16.4
|
|
45
|
+
# also gates WASMEDGE_BUILD_AOT_RUNTIME: this library can neither compile nor
|
|
46
|
+
# load AOT code. Patches 02-04 therefore patch code this runner never
|
|
47
|
+
# executes (Interruptible-AOT stop-token traffic, the AOT fault path, and the
|
|
48
|
+
# LLVM AOT compiler); only 01 changes this image's observable behaviour
|
|
49
|
+
# today. They stay applied so this image's WasmEdge checkout never drifts
|
|
50
|
+
# from SDN's patched baseline. This is also why no memarg-offset (04)
|
|
51
|
+
# regression test runs against this image: there is no AOT lane here to run
|
|
52
|
+
# SDN's substrate probe against.
|
|
53
|
+
COPY native/wasmedge-0.16.4-atomic-wait.patch /tmp/01-atomic-wait.patch
|
|
54
|
+
COPY native/wasmedge-0.16.4-stop-token.patch /tmp/02-stop-token.patch
|
|
55
|
+
COPY native/wasmedge-0.16.4-fault-jmp.patch /tmp/03-fault-jmp.patch
|
|
56
|
+
COPY native/wasmedge-0.16.4-atomic-memarg-offset.patch /tmp/04-atomic-memarg-offset.patch
|
|
41
57
|
RUN git clone --depth 1 --branch 0.16.4 https://github.com/WasmEdge/WasmEdge.git /tmp/wasmedge-source \
|
|
42
58
|
&& test "$(git -C /tmp/wasmedge-source rev-parse HEAD)" = be85c2fbba68318f103b4a766728f6946e65abf8 \
|
|
43
|
-
&& git -C /tmp/wasmedge-source apply /tmp/atomic-wait.patch \
|
|
59
|
+
&& git -C /tmp/wasmedge-source apply /tmp/01-atomic-wait.patch \
|
|
60
|
+
&& git -C /tmp/wasmedge-source apply /tmp/02-stop-token.patch \
|
|
61
|
+
&& git -C /tmp/wasmedge-source apply /tmp/03-fault-jmp.patch \
|
|
62
|
+
&& git -C /tmp/wasmedge-source apply /tmp/04-atomic-memarg-offset.patch \
|
|
44
63
|
&& cmake -S /tmp/wasmedge-source -B /tmp/wasmedge-build -G Ninja \
|
|
45
64
|
-DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/sdm-wasmedge \
|
|
46
65
|
"-DCMAKE_CXX_FLAGS=-Wno-error=maybe-uninitialized -Wno-error=array-bounds" \
|
|
@@ -48,7 +67,8 @@ RUN git clone --depth 1 --branch 0.16.4 https://github.com/WasmEdge/WasmEdge.git
|
|
|
48
67
|
-DWASMEDGE_BUILD_TOOLS=OFF -DWASMEDGE_FORCE_DISABLE_LTO=ON \
|
|
49
68
|
&& cmake --build /tmp/wasmedge-build -j 4 \
|
|
50
69
|
&& cmake --install /tmp/wasmedge-build \
|
|
51
|
-
&& rm -rf /tmp/wasmedge-source /tmp/wasmedge-build
|
|
70
|
+
&& rm -rf /tmp/wasmedge-source /tmp/wasmedge-build \
|
|
71
|
+
/tmp/01-atomic-wait.patch /tmp/02-stop-token.patch /tmp/03-fault-jmp.patch /tmp/04-atomic-memarg-offset.patch
|
|
52
72
|
RUN cc /tmp/sdm-wasi-threads-runner.c -std=c11 -O2 -pthread -Wall -Wextra -Werror \
|
|
53
73
|
-I/opt/sdm-wasmedge/include -L/opt/sdm-wasmedge/lib64 -L/opt/sdm-wasmedge/lib -lwasmedge \
|
|
54
74
|
-Wl,--disable-new-dtags,-rpath,/opt/sdm-wasmedge/lib64,-rpath,/opt/sdm-wasmedge/lib \
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
diff --git a/lib/llvm/compiler.cpp b/lib/llvm/compiler.cpp
|
|
2
|
+
index f765db6..5b1bfc3 100644
|
|
3
|
+
--- a/lib/llvm/compiler.cpp
|
|
4
|
+
+++ b/lib/llvm/compiler.cpp
|
|
5
|
+
@@ -3952,6 +3952,21 @@ public:
|
|
6
|
+
Builder.positionAtEnd(OkBB);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
+ // memory.atomic.notify / wait32 / wait64 call the runtime with the
|
|
10
|
+
+ // effective address: the operand plus the memarg offset (as the interpreter
|
|
11
|
+
+ // does, threadInstr.cpp). Passing the bare operand notified and waited on
|
|
12
|
+
+ // the wrong word whenever the offset was not 0 (a global such as wasi-libc's
|
|
13
|
+
+ // __thread_list_lock at `i32.const 0; memory.atomic.notify offset=N`): lost
|
|
14
|
+
+ // wakeups. An address past 4 GiB is out of bounds.
|
|
15
|
+
+ LLVM::Value compileAtomicEffectiveAddress(LLVM::Value Addr) noexcept {
|
|
16
|
+
+ auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "address_in_bounds");
|
|
17
|
+
+ auto InBounds = Builder.createLikely(
|
|
18
|
+
+ Builder.createICmpULE(Addr, LLContext.getInt64(UINT32_MAX)));
|
|
19
|
+
+ Builder.createCondBr(InBounds, OkBB,
|
|
20
|
+
+ getTrapBB(ErrCode::Value::MemoryOutOfBounds));
|
|
21
|
+
+ Builder.positionAtEnd(OkBB);
|
|
22
|
+
+ return Builder.createTrunc(Addr, Context.Int32Ty);
|
|
23
|
+
+ }
|
|
24
|
+
void compileMemoryFence() noexcept {
|
|
25
|
+
Builder.createFence(LLVMAtomicOrderingSequentiallyConsistent);
|
|
26
|
+
}
|
|
27
|
+
@@ -3963,7 +3978,8 @@ public:
|
|
28
|
+
Addr = Builder.createAdd(Addr, LLContext.getInt64(MemoryOffset));
|
|
29
|
+
}
|
|
30
|
+
compileAtomicCheckOffsetAlignment(Addr, Context.Int32Ty);
|
|
31
|
+
- auto Offset = stackPop();
|
|
32
|
+
+ stackPop();
|
|
33
|
+
+ auto Offset = compileAtomicEffectiveAddress(Addr);
|
|
34
|
+
|
|
35
|
+
stackPush(Builder.createCall(
|
|
36
|
+
Context.getIntrinsic(
|
|
37
|
+
@@ -3982,7 +3998,8 @@ public:
|
|
38
|
+
Addr = Builder.createAdd(Addr, LLContext.getInt64(MemoryOffset));
|
|
39
|
+
}
|
|
40
|
+
compileAtomicCheckOffsetAlignment(Addr, TargetType);
|
|
41
|
+
- auto Offset = stackPop();
|
|
42
|
+
+ stackPop();
|
|
43
|
+
+ auto Offset = compileAtomicEffectiveAddress(Addr);
|
|
44
|
+
|
|
45
|
+
stackPush(Builder.createCall(
|
|
46
|
+
Context.getIntrinsic(
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
diff --git a/include/system/fault.h b/include/system/fault.h
|
|
2
|
+
index 1f57d78..71dd6ea 100644
|
|
3
|
+
--- a/include/system/fault.h
|
|
4
|
+
+++ b/include/system/fault.h
|
|
5
|
+
@@ -43,4 +43,14 @@ private:
|
|
6
|
+
|
|
7
|
+
} // namespace WasmEdge
|
|
8
|
+
|
|
9
|
+
+// _setjmp/_longjmp carry no signal state. darwin's longjmp resets or sets the
|
|
10
|
+
+// thread's alternate-signal-stack flag from a jmp_buf word setjmp never
|
|
11
|
+
+// writes, so a trap could leave a thread marked as running on its signal stack
|
|
12
|
+
+// and the embedder's next signal (Go's preemption) lands on the wrong stack.
|
|
13
|
+
+// The fault handler unblocks its own signal before it jumps, so nothing needs
|
|
14
|
+
+// restoring.
|
|
15
|
+
+#if defined(_WIN32)
|
|
16
|
+
#define PREPARE_FAULT(f) (static_cast<uint32_t>(setjmp((f).buffer())))
|
|
17
|
+
+#else
|
|
18
|
+
+#define PREPARE_FAULT(f) (static_cast<uint32_t>(_setjmp((f).buffer())))
|
|
19
|
+
+#endif
|
|
20
|
+
diff --git a/lib/system/fault.cpp b/lib/system/fault.cpp
|
|
21
|
+
index e435d4e..c49f5ae 100644
|
|
22
|
+
--- a/lib/system/fault.cpp
|
|
23
|
+
+++ b/lib/system/fault.cpp
|
|
24
|
+
@@ -118,7 +118,11 @@ Fault::~Fault() noexcept {
|
|
25
|
+
assuming(localHandler != nullptr);
|
|
26
|
+
auto Buffer = stackTrace(localHandler->StackTraceBuffer);
|
|
27
|
+
localHandler->StackTraceSize = Buffer.size();
|
|
28
|
+
+#if defined(_WIN32)
|
|
29
|
+
longjmp(localHandler->Buffer, static_cast<int>(Error.operator uint32_t()));
|
|
30
|
+
+#else
|
|
31
|
+
+ _longjmp(localHandler->Buffer, static_cast<int>(Error.operator uint32_t()));
|
|
32
|
+
+#endif
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
} // namespace WasmEdge
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
diff --git a/include/executor/executor.h b/include/executor/executor.h
|
|
2
|
+
index 6683ee9..6bdccea 100644
|
|
3
|
+
--- a/include/executor/executor.h
|
|
4
|
+
+++ b/include/executor/executor.h
|
|
5
|
+
@@ -201,9 +201,15 @@ public:
|
|
6
|
+
asyncInvoke(const Runtime::Instance::FunctionInstance *FuncInst,
|
|
7
|
+
Span<const ValVariant> Params, Span<const ValType> ParamTypes);
|
|
8
|
+
|
|
9
|
+
- /// Stop execution
|
|
10
|
+
+ /// Stop execution.
|
|
11
|
+
+ ///
|
|
12
|
+
+ /// SDN patch (stop-token): a stop is STICKY for every invocation running on
|
|
13
|
+
+ /// this executor. Upstream consumed the token with exchange(0), so one stop
|
|
14
|
+
+ /// interrupted exactly one thread and every other wasi-thread on the same
|
|
15
|
+
+ /// executor kept running. The token is cleared only when the next
|
|
16
|
+
+ /// invocation starts on an idle executor (see invoke()).
|
|
17
|
+
void stop() noexcept {
|
|
18
|
+
- StopToken.store(1, std::memory_order_relaxed);
|
|
19
|
+
+ StopToken.store(1, std::memory_order_seq_cst);
|
|
20
|
+
atomicNotifyAll();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@@ -1120,6 +1126,8 @@ private:
|
|
24
|
+
Statistics::Statistics *Stat;
|
|
25
|
+
/// Stop Execution
|
|
26
|
+
std::atomic_uint32_t StopToken = 0;
|
|
27
|
+
+ /// Invocations currently running on this executor (SDN stop-token patch).
|
|
28
|
+
+ std::atomic_uint32_t ActiveInvocations = 0;
|
|
29
|
+
/// Executor Host Function Handler
|
|
30
|
+
HostFuncHandler HostFuncHelper = {};
|
|
31
|
+
};
|
|
32
|
+
diff --git a/lib/executor/engine/controlInstr.cpp b/lib/executor/engine/controlInstr.cpp
|
|
33
|
+
index 935992a..55e08d7 100644
|
|
34
|
+
--- a/lib/executor/engine/controlInstr.cpp
|
|
35
|
+
+++ b/lib/executor/engine/controlInstr.cpp
|
|
36
|
+
@@ -134,7 +134,7 @@ Expect<void> Executor::runBrOnCastOp(Runtime::StackManager &StackMgr,
|
|
37
|
+
Expect<void> Executor::runReturnOp(Runtime::StackManager &StackMgr,
|
|
38
|
+
AST::InstrView::iterator &PC) noexcept {
|
|
39
|
+
// Check stop token
|
|
40
|
+
- if (unlikely(StopToken.exchange(0, std::memory_order_relaxed))) {
|
|
41
|
+
+ if (unlikely(StopToken.load(std::memory_order_relaxed))) {
|
|
42
|
+
spdlog::error(ErrCode::Value::Interrupted);
|
|
43
|
+
return Unexpect(ErrCode::Value::Interrupted);
|
|
44
|
+
}
|
|
45
|
+
diff --git a/lib/executor/executor.cpp b/lib/executor/executor.cpp
|
|
46
|
+
index 137131b..68ef4fe 100644
|
|
47
|
+
--- a/lib/executor/executor.cpp
|
|
48
|
+
+++ b/lib/executor/executor.cpp
|
|
49
|
+
@@ -140,6 +140,16 @@ Executor::invoke(const Runtime::Instance::FunctionInstance *FuncInst,
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
+ // SDN patch (stop-token): a stop issued while invocations run stays set
|
|
54
|
+
+ // until all of them have returned; the first invocation on an idle executor
|
|
55
|
+
+ // clears a token left over from an earlier stop.
|
|
56
|
+
+ if (ActiveInvocations.fetch_add(1, std::memory_order_acq_rel) == 0) {
|
|
57
|
+
+ StopToken.store(0, std::memory_order_seq_cst);
|
|
58
|
+
+ }
|
|
59
|
+
+ cxx20::scope_exit ActiveInvocationGuard([this]() noexcept {
|
|
60
|
+
+ ActiveInvocations.fetch_sub(1, std::memory_order_acq_rel);
|
|
61
|
+
+ });
|
|
62
|
+
+
|
|
63
|
+
Runtime::StackManager StackMgr;
|
|
64
|
+
|
|
65
|
+
// Call runFunction.
|
|
66
|
+
diff --git a/lib/executor/helper.cpp b/lib/executor/helper.cpp
|
|
67
|
+
index 96205cc..2ace419 100644
|
|
68
|
+
--- a/lib/executor/helper.cpp
|
|
69
|
+
+++ b/lib/executor/helper.cpp
|
|
70
|
+
@@ -51,7 +51,7 @@ Executor::enterFunction(Runtime::StackManager &StackMgr,
|
|
71
|
+
// RetIt: the return position when the entered function returns.
|
|
72
|
+
|
|
73
|
+
// Check if the interruption occurs.
|
|
74
|
+
- if (unlikely(StopToken.exchange(0, std::memory_order_relaxed))) {
|
|
75
|
+
+ if (unlikely(StopToken.load(std::memory_order_relaxed))) {
|
|
76
|
+
spdlog::error(ErrCode::Value::Interrupted);
|
|
77
|
+
return Unexpect(ErrCode::Value::Interrupted);
|
|
78
|
+
}
|
|
79
|
+
@@ -251,7 +251,7 @@ Executor::branchToLabel(Runtime::StackManager &StackMgr,
|
|
80
|
+
const AST::Instruction::JumpDescriptor &JumpDesc,
|
|
81
|
+
AST::InstrView::iterator &PC) noexcept {
|
|
82
|
+
// Check the stop token.
|
|
83
|
+
- if (unlikely(StopToken.exchange(0, std::memory_order_relaxed))) {
|
|
84
|
+
+ if (unlikely(StopToken.load(std::memory_order_relaxed))) {
|
|
85
|
+
spdlog::error(ErrCode::Value::Interrupted);
|
|
86
|
+
return Unexpect(ErrCode::Value::Interrupted);
|
|
87
|
+
}
|
|
88
|
+
diff --git a/lib/llvm/compiler.cpp b/lib/llvm/compiler.cpp
|
|
89
|
+
index 8db12e3..f765db6 100644
|
|
90
|
+
--- a/lib/llvm/compiler.cpp
|
|
91
|
+
+++ b/lib/llvm/compiler.cpp
|
|
92
|
+
@@ -5670,12 +5670,13 @@ private:
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
auto NotStopBB = LLVM::BasicBlock::create(LLContext, F.Fn, "NotStop");
|
|
96
|
+
- auto StopToken = Builder.createAtomicRMW(
|
|
97
|
+
- LLVMAtomicRMWBinOpXchg, Context.getStopToken(Builder, ExecCtx),
|
|
98
|
+
- LLContext.getInt32(0), LLVMAtomicOrderingMonotonic);
|
|
99
|
+
-#if LLVM_VERSION_MAJOR >= 13
|
|
100
|
+
- StopToken.setAlignment(32);
|
|
101
|
+
-#endif
|
|
102
|
+
+ // SDN patch (stop-token): read the token, never consume it. An exchange
|
|
103
|
+
+ // let one thread swallow a stop meant for all of them, and it made every
|
|
104
|
+
+ // block of every thread an RMW on one shared cache line.
|
|
105
|
+
+ auto StopToken = Builder.createLoad(
|
|
106
|
+
+ LLContext.getInt32Ty(), Context.getStopToken(Builder, ExecCtx), true);
|
|
107
|
+
+ StopToken.setOrdering(LLVMAtomicOrderingMonotonic);
|
|
108
|
+
+ StopToken.setAlignment(4);
|
|
109
|
+
auto NotStop = Builder.createLikely(
|
|
110
|
+
Builder.createICmpEQ(StopToken, LLContext.getInt32(0)));
|
|
111
|
+
Builder.createCondBr(NotStop, NotStopBB,
|
|
@@ -71,9 +71,16 @@ export function loadWasmEdgePin() {
|
|
|
71
71
|
if (!repository) {
|
|
72
72
|
throw new Error("wasmedgePin.json dockerImageRepository is required.");
|
|
73
73
|
}
|
|
74
|
+
// The SDN WasmEdge 0.16.4 patch series (01-atomic-wait, 02-stop-token,
|
|
75
|
+
// 03-fault-jmp, 04-atomic-memarg-offset; sdn-server/internal/wasmrt/SUBSTRATE.md).
|
|
76
|
+
// Any change to the runner source, any patch, or the Dockerfile itself must
|
|
77
|
+
// bump this identity so a stale-patched image is never reused.
|
|
74
78
|
const runnerIdentity = createHash("sha256")
|
|
75
79
|
.update(readFileSync(path.join(__dirname, "native/wasmedge_wasi_threads_runner.c")))
|
|
76
80
|
.update(readFileSync(path.join(__dirname, "native/wasmedge-0.16.4-atomic-wait.patch")))
|
|
81
|
+
.update(readFileSync(path.join(__dirname, "native/wasmedge-0.16.4-stop-token.patch")))
|
|
82
|
+
.update(readFileSync(path.join(__dirname, "native/wasmedge-0.16.4-fault-jmp.patch")))
|
|
83
|
+
.update(readFileSync(path.join(__dirname, "native/wasmedge-0.16.4-atomic-memarg-offset.patch")))
|
|
77
84
|
.update(readFileSync(path.join(__dirname, String(pin.dockerfile))))
|
|
78
85
|
.digest("hex").slice(0, 16);
|
|
79
86
|
return Object.freeze({
|