xbintsc 0.6.23 → 0.6.28
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/AGENTS.md +1 -1
- package/dist/src/cli/extensions.js +11 -3
- package/dist/src/cli/extensions.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/help.js +1 -1
- package/dist/src/codegen/generator/state.js +7 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +19 -1
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/concatenate.d.ts +23 -0
- package/dist/src/driver/bundler/concatenate.js +210 -0
- package/dist/src/driver/bundler/concatenate.js.map +1 -0
- package/dist/src/driver/bundler/graph.js +9 -5
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/bundler/merge.d.ts +4 -4
- package/dist/src/driver/bundler/merge.js +24 -207
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
- package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
- package/dist/src/driver/bundler/namespace-object.js.map +1 -0
- package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
- package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
- package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
- package/dist/src/driver/fingerprint.d.ts +2 -2
- package/dist/src/driver/fingerprint.js +3 -3
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/link-features.js +1 -1
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/modules.d.ts +12 -5
- package/dist/src/driver/modules.js +12 -5
- package/dist/src/driver/modules.js.map +1 -1
- package/dist/src/driver/paths.js +8 -4
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +13 -1
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/catalog.js +5 -1
- package/dist/src/extensions/catalog.js.map +1 -1
- package/dist/src/extensions/gui/index.js +0 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/link-feature.d.ts +8 -0
- package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
- package/dist/src/extensions/node/stream-web/index.js +59 -4
- package/dist/src/extensions/node/stream-web/index.js.map +1 -1
- package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
- package/dist/src/extensions/node/worker_threads/index.js +24 -11
- package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +9 -1
- package/dist/src/extensions/registry.js +4 -1
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/wasm-asset.d.ts +16 -0
- package/dist/src/extensions/wasm-asset.js +28 -0
- package/dist/src/extensions/wasm-asset.js.map +1 -0
- package/dist/src/extensions/wasm.d.ts +17 -0
- package/dist/src/extensions/wasm.js +68 -0
- package/dist/src/extensions/wasm.js.map +1 -0
- package/dist/src/extensions/webworker/index.d.ts +24 -0
- package/dist/src/extensions/webworker/index.js +54 -0
- package/dist/src/extensions/webworker/index.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/tests/driver/deferred-reexport.test.js +25 -0
- package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +9 -0
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/e2e/async-fs.test.js +44 -0
- package/dist/tests/e2e/async-fs.test.js.map +1 -1
- package/dist/tests/e2e/harness.d.ts +6 -0
- package/dist/tests/e2e/harness.js +13 -1
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
- package/dist/tests/e2e/node-crypto-random.test.js +148 -0
- package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +1 -1
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/web-streams.test.d.ts +8 -0
- package/dist/tests/e2e/web-streams.test.js +274 -0
- package/dist/tests/e2e/web-streams.test.js.map +1 -0
- package/dist/tests/e2e/web-worker.test.d.ts +7 -0
- package/dist/tests/e2e/web-worker.test.js +74 -0
- package/dist/tests/e2e/web-worker.test.js.map +1 -0
- package/dist/tests/e2e/webassembly.test.d.ts +8 -0
- package/dist/tests/e2e/webassembly.test.js +113 -0
- package/dist/tests/e2e/webassembly.test.js.map +1 -0
- package/dist/tests/e2e/worker-api.test.d.ts +14 -0
- package/dist/tests/e2e/worker-api.test.js +460 -0
- package/dist/tests/e2e/worker-api.test.js.map +1 -0
- package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
- package/dist/tests/e2e/worker-threads.test.js +160 -0
- package/dist/tests/e2e/worker-threads.test.js.map +1 -0
- package/dist/tests/extensions/wasm.test.d.ts +1 -0
- package/dist/tests/extensions/wasm.test.js +53 -0
- package/dist/tests/extensions/wasm.test.js.map +1 -0
- package/doc/ai/cli.md +1 -1
- package/doc/ai/contributing.md +11 -4
- package/doc/ai/extensions.md +25 -2
- package/doc/ai/language-support.md +14 -2
- package/doc/ai/zh-CN/cli.md +1 -1
- package/doc/ai/zh-CN/contributing.md +8 -3
- package/doc/ai/zh-CN/extensions.md +23 -2
- package/doc/ai/zh-CN/language-support.md +13 -2
- package/doc/gui/api.md +1 -1
- package/doc/implemented/cli.md +1 -1
- package/doc/implemented/extensions.md +6 -5
- package/doc/implemented/node/stream-web.md +31 -0
- package/doc/implemented/node/worker-threads.md +88 -5
- package/doc/implemented/node.md +2 -1
- package/doc/implemented/streams.md +82 -0
- package/doc/implemented/wasm.md +80 -18
- package/doc/implemented/web-worker.md +84 -0
- package/doc/implemented.md +6 -2
- package/doc/unimplemented/deviations.md +1 -0
- package/doc/unimplemented/node/worker-threads.md +23 -0
- package/doc/unimplemented/node.md +1 -0
- package/doc/unimplemented/stdlib.md +2 -1
- package/doc/unimplemented/wasm.md +21 -9
- package/doc/unimplemented.md +1 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/implemented/cli.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +7 -6
- package/doc/zh-CN/implemented/node/stream-web.md +24 -0
- package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
- package/doc/zh-CN/implemented/node.md +2 -1
- package/doc/zh-CN/implemented/streams.md +60 -0
- package/doc/zh-CN/implemented/wasm.md +73 -16
- package/doc/zh-CN/implemented/web-worker.md +79 -0
- package/doc/zh-CN/implemented.md +6 -2
- package/doc/zh-CN/unimplemented/deviations.md +1 -0
- package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
- package/doc/zh-CN/unimplemented/node.md +1 -0
- package/doc/zh-CN/unimplemented/stdlib.md +2 -1
- package/doc/zh-CN/unimplemented/wasm.md +16 -8
- package/doc/zh-CN/unimplemented.md +1 -1
- package/llms.txt +2 -0
- package/package.json +8 -7
- package/runtime/ext_gui/browser.cpp +1 -1
- package/runtime/ext_node/stream_web/stream_web.c +20 -6
- package/runtime/worker/worker_api.c +422 -0
- package/runtime/worker/worker_codec.c +489 -0
- package/runtime/worker/worker_core.c +445 -0
- package/runtime/worker/worker_internal.h +223 -0
- package/runtime/worker/worker_platform.c +344 -0
- package/runtime/worker/worker_ports.c +203 -0
- package/runtime/worker/worker_transport.c +464 -0
- package/runtime/worker/worker_wire.c +150 -0
- package/runtime/xt_builtins.c +15 -0
- package/runtime/xt_constructors.c +49 -2
- package/runtime/xt_stdlib2/error.inc +4 -0
- package/runtime/xt_streams/common.inc +232 -0
- package/runtime/xt_streams/constructors.inc +232 -0
- package/runtime/xt_streams/encoding.inc +124 -0
- package/runtime/xt_streams/readable-extra.inc +372 -0
- package/runtime/xt_streams/readable.inc +415 -0
- package/runtime/xt_streams/transform.inc +195 -0
- package/runtime/xt_streams/writable.inc +542 -0
- package/runtime/xt_streams.c +30 -0
- package/runtime/xt_wasm/wasm.h +148 -0
- package/runtime/xt_wasm/wasm_api.c +357 -0
- package/runtime/xt_wasm/wasm_api.h +37 -0
- package/runtime/xt_wasm/wasm_bridge.c +30 -0
- package/runtime/xt_wasm/wasm_instance.c +591 -0
- package/runtime/xt_wasm/wasm_module.c +270 -0
- package/runtime/xt_wasm/wasm_values.c +177 -0
- package/runtime/xt_webassembly.c +15 -4
- package/scripts/fetch-vendored.ts +187 -20
- package/scripts/package-release.ts +3 -2
- package/src/cli/extensions.ts +11 -3
- package/src/cli/help.ts +1 -1
- package/src/codegen/generator/state.ts +7 -0
- package/src/codegen/generator/tables.ts +19 -1
- package/src/driver/bundler/README.md +55 -0
- package/src/driver/bundler/concatenate.ts +244 -0
- package/src/driver/bundler/graph.ts +9 -5
- package/src/driver/bundler/merge.ts +24 -215
- package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
- package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
- package/src/driver/fingerprint.ts +3 -3
- package/src/driver/link-features.ts +1 -1
- package/src/driver/modules.ts +12 -5
- package/src/driver/paths.ts +8 -4
- package/src/driver/runtime-objects.ts +13 -1
- package/src/driver/version.ts +1 -1
- package/src/extensions/catalog.ts +5 -1
- package/src/extensions/gui/index.ts +0 -1
- package/src/extensions/link-feature.ts +8 -0
- package/src/extensions/node/stream-web/index.ts +59 -4
- package/src/extensions/node/worker_threads/index.ts +24 -11
- package/src/extensions/registry.ts +13 -2
- package/src/extensions/wasm-asset.ts +32 -0
- package/src/extensions/wasm.ts +74 -0
- package/src/extensions/webworker/index.ts +58 -0
- package/src/index.ts +1 -0
- package/vendor/wasm3/.pin +1 -0
- package/vendor/wasm3/LICENSE +21 -0
- package/vendor/wasm3/m3_api_libc.c +265 -0
- package/vendor/wasm3/m3_api_libc.h +19 -0
- package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
- package/vendor/wasm3/m3_api_tracer.c +173 -0
- package/vendor/wasm3/m3_api_tracer.h +19 -0
- package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
- package/vendor/wasm3/m3_api_wasi.c +1207 -0
- package/vendor/wasm3/m3_api_wasi.h +74 -0
- package/vendor/wasm3/m3_api_wasi_host.h +137 -0
- package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
- package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
- package/vendor/wasm3/m3_atomic.h +114 -0
- package/vendor/wasm3/m3_bind.c +174 -0
- package/vendor/wasm3/m3_bind.h +20 -0
- package/vendor/wasm3/m3_code.c +232 -0
- package/vendor/wasm3/m3_code.h +74 -0
- package/vendor/wasm3/m3_compile.c +6603 -0
- package/vendor/wasm3/m3_compile.h +354 -0
- package/vendor/wasm3/m3_config.h +448 -0
- package/vendor/wasm3/m3_config_platforms.h +345 -0
- package/vendor/wasm3/m3_core.c +1034 -0
- package/vendor/wasm3/m3_core.h +526 -0
- package/vendor/wasm3/m3_deterministic.c +65 -0
- package/vendor/wasm3/m3_deterministic.h +59 -0
- package/vendor/wasm3/m3_env.c +3146 -0
- package/vendor/wasm3/m3_env.h +748 -0
- package/vendor/wasm3/m3_exception.h +33 -0
- package/vendor/wasm3/m3_exec.c +8 -0
- package/vendor/wasm3/m3_exec.h +4110 -0
- package/vendor/wasm3/m3_exec_atomic.h +146 -0
- package/vendor/wasm3/m3_exec_defs.h +76 -0
- package/vendor/wasm3/m3_function.c +278 -0
- package/vendor/wasm3/m3_function.h +244 -0
- package/vendor/wasm3/m3_host.h +226 -0
- package/vendor/wasm3/m3_host_none.h +76 -0
- package/vendor/wasm3/m3_host_posix.h +543 -0
- package/vendor/wasm3/m3_host_win32.h +253 -0
- package/vendor/wasm3/m3_info.c +616 -0
- package/vendor/wasm3/m3_info.h +38 -0
- package/vendor/wasm3/m3_math_utils.h +560 -0
- package/vendor/wasm3/m3_module.c +431 -0
- package/vendor/wasm3/m3_parse.c +1549 -0
- package/vendor/wasm3/m3_snapshot.c +3825 -0
- package/vendor/wasm3/m3_validate.c +1770 -0
- package/vendor/wasm3/m3_validate.h +25 -0
- package/vendor/wasm3/m3_wasi.h +10 -0
- package/vendor/wasm3/m3_xxh64.c +211 -0
- package/vendor/wasm3/wasm3.h +684 -0
- package/vendor/wasm3/wasm3_defs.h +350 -0
- package/dist/src/driver/bundler/namespace-import.js.map +0 -1
- package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
- package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
|
@@ -0,0 +1,3825 @@
|
|
|
1
|
+
//
|
|
2
|
+
// m3_snapshot.c
|
|
3
|
+
//
|
|
4
|
+
// Copyright © 2026 Volodymyr Shymanskyy. All rights reserved.
|
|
5
|
+
//
|
|
6
|
+
// Saving a runtime's execution state to a stream and bringing it back.
|
|
7
|
+
//
|
|
8
|
+
// A snapshot is written in Wasm's terms rather than this build's, so it can be
|
|
9
|
+
// resumed by a build on another architecture, with another slot width, another
|
|
10
|
+
// revision, or with gas metering set differently. Every number goes out little
|
|
11
|
+
// endian. A place in a function is the offset of a Wasm instruction; a frame is
|
|
12
|
+
// the function's locals and live operand stack as typed values; a reference is
|
|
13
|
+
// a function index, or the id of a continuation or exception the snapshot
|
|
14
|
+
// carries whole. Nothing in the file is an address, a slot or a metacode word.
|
|
15
|
+
//
|
|
16
|
+
// The compiler's snapshot maps (see M3SnapshotMap) are what make this work.
|
|
17
|
+
// Saving reads each value out of wherever this build kept it; loading compiles
|
|
18
|
+
// the same functions, finds the same safepoints in the result, and puts each
|
|
19
|
+
// value wherever that build keeps it - and then stands up the native frames the
|
|
20
|
+
// interpreter needs from what its own code says they hold.
|
|
21
|
+
//
|
|
22
|
+
|
|
23
|
+
#include "m3_env.h"
|
|
24
|
+
#include "m3_compile.h"
|
|
25
|
+
#include "m3_exception.h"
|
|
26
|
+
#include "m3_host.h"
|
|
27
|
+
|
|
28
|
+
#include <limits.h>
|
|
29
|
+
|
|
30
|
+
#if d_m3HasSnapshots
|
|
31
|
+
|
|
32
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
33
|
+
// format
|
|
34
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
// The container opens the way a Wasm binary does: four bytes of magic and a
|
|
37
|
+
// fixed 32-bit version, little endian. Eight bytes, at a fixed place, whatever
|
|
38
|
+
// the version turns out to say.
|
|
39
|
+
static const u8 c_snapshotMagic[4] = { 0x00, 'd', 'm', 'p' };
|
|
40
|
+
static const u32 c_snapshotVersion = 1;
|
|
41
|
+
|
|
42
|
+
enum {
|
|
43
|
+
d_m3SnapshotSection_Meta = 0,
|
|
44
|
+
d_m3SnapshotSection_Memory = 1,
|
|
45
|
+
d_m3SnapshotSection_Table = 2,
|
|
46
|
+
d_m3SnapshotSection_Global = 3,
|
|
47
|
+
d_m3SnapshotSection_Segment = 4,
|
|
48
|
+
d_m3SnapshotSection_Exception = 5,
|
|
49
|
+
d_m3SnapshotSection_Continuation = 6,
|
|
50
|
+
d_m3SnapshotSection_HostState = 7,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
# define d_m3SnapshotFlagPostmortem 0x1
|
|
54
|
+
|
|
55
|
+
// The longest <name> in a "snapshot.<name>" custom section. A name is a label
|
|
56
|
+
// the embedder picks, not something the format needs room to grow.
|
|
57
|
+
# define d_m3MaxSnapshotNameLength 200
|
|
58
|
+
|
|
59
|
+
// no index, and no reference
|
|
60
|
+
# define d_m3SnapshotNone UINT32_MAX
|
|
61
|
+
# define d_m3SnapshotNullRef UINT64_MAX
|
|
62
|
+
|
|
63
|
+
// A continuation's state, as the file spells it. A running continuation can
|
|
64
|
+
// only be written into a postmortem, where it is as finished as the rest.
|
|
65
|
+
enum {
|
|
66
|
+
snapshot_contAllocated = 0,
|
|
67
|
+
snapshot_contSuspended = 1,
|
|
68
|
+
snapshot_contFinished = 2
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// Memory contents go out as runs, so the long stretches of zero or 0xFF that
|
|
72
|
+
// most memories are made of cost a few bytes rather than their length
|
|
73
|
+
# define d_m3ChunkEnd 0x00
|
|
74
|
+
# define d_m3ChunkRaw 0x01
|
|
75
|
+
# define d_m3ChunkFillFF 0x02
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
79
|
+
// identity
|
|
80
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
// Only standard sections affect the module's state. Custom sections (including
|
|
83
|
+
// names, debug information and snapshots) can change without changing its identity.
|
|
84
|
+
// A module whose bytes are gone has no identity to check, and a snapshot is
|
|
85
|
+
// neither saved from one nor loaded into one.
|
|
86
|
+
static
|
|
87
|
+
M3Result ModuleFingerprint (IM3Module i_module, u64* o_hash)
|
|
88
|
+
{
|
|
89
|
+
M3Result result = m3Err_none;
|
|
90
|
+
Xxh64 hash;
|
|
91
|
+
|
|
92
|
+
*o_hash = 0;
|
|
93
|
+
|
|
94
|
+
_throwif("the module's bytes are gone, so a snapshot cannot tell it from another",
|
|
95
|
+
not i_module->wasmStart or i_module->wasmEnd - i_module->wasmStart < 8);
|
|
96
|
+
|
|
97
|
+
{
|
|
98
|
+
bytes_t pos = i_module->wasmStart + 8;
|
|
99
|
+
cbytes_t end = i_module->wasmEnd;
|
|
100
|
+
|
|
101
|
+
Xxh64_Init(&hash, 0);
|
|
102
|
+
|
|
103
|
+
while (pos < end) {
|
|
104
|
+
bytes_t start = pos;
|
|
105
|
+
u8 sectionId = *pos++;
|
|
106
|
+
u32 size = 0;
|
|
107
|
+
|
|
108
|
+
_ (ReadLEB_u32(&size, &pos, end));
|
|
109
|
+
_throwif(m3Err_wasmUnderrun, size > (size_t)(end - pos));
|
|
110
|
+
|
|
111
|
+
pos += size;
|
|
112
|
+
if (sectionId != 0) {
|
|
113
|
+
Xxh64_Update(&hash, start, (size_t)(pos - start));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
*o_hash = Xxh64_Digest(&hash);
|
|
119
|
+
|
|
120
|
+
_catch:
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
126
|
+
// safepoints
|
|
127
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
128
|
+
|
|
129
|
+
static
|
|
130
|
+
const M3SafePoint* FindSafePoint (IM3Function i_function, pc_t i_pc, u8 i_kind)
|
|
131
|
+
{
|
|
132
|
+
const M3SnapshotMap* map = i_function->snapshotMap;
|
|
133
|
+
|
|
134
|
+
if (map) {
|
|
135
|
+
for (u32 i = 0; i < map->numSafePoints; ++i) {
|
|
136
|
+
if (map->safePoints[i].pc == i_pc and map->safePoints[i].kind == i_kind) {
|
|
137
|
+
return &map->safePoints[i];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return NULL;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// The safepoint of i_kind at the instruction at i_wasmOffset, and for a back
|
|
146
|
+
// edge, the one that goes to the loop at i_targetLoop. Two clauses of one
|
|
147
|
+
// instruction can go to the same loop; they leave the same frame, so either of
|
|
148
|
+
// their safepoints does.
|
|
149
|
+
static
|
|
150
|
+
const M3SafePoint* FindSafePointAt (const M3SnapshotMap* i_map, u32 i_wasmOffset, u8 i_kind, u32 i_targetLoop)
|
|
151
|
+
{
|
|
152
|
+
u32 low = 0;
|
|
153
|
+
u32 high = i_map->numSafePoints;
|
|
154
|
+
|
|
155
|
+
while (low < high) {
|
|
156
|
+
u32 middle = low + (high - low) / 2;
|
|
157
|
+
|
|
158
|
+
if (i_map->safePoints[middle].wasmOffset < i_wasmOffset) {
|
|
159
|
+
low = middle + 1;
|
|
160
|
+
} else {
|
|
161
|
+
high = middle;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
for (u32 i = low; i < i_map->numSafePoints and i_map->safePoints[i].wasmOffset == i_wasmOffset; ++i) {
|
|
166
|
+
const M3SafePoint* point = &i_map->safePoints[i];
|
|
167
|
+
|
|
168
|
+
if (point->kind != i_kind or (point->flags & d_m3SafePointTailCall)) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (i_kind != safepoint_op or i_map->blocks[point->aux].wasmOffset == i_targetLoop) {
|
|
172
|
+
return point;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return NULL;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static
|
|
180
|
+
const M3BlockStart* FindBlockAt (const M3SnapshotMap* i_map, u8 i_opcode, u32 i_wasmOffset)
|
|
181
|
+
{
|
|
182
|
+
for (u32 i = 0; i < i_map->numBlocks; ++i) {
|
|
183
|
+
if (i_map->blocks[i].opcode == i_opcode and i_map->blocks[i].wasmOffset == i_wasmOffset) {
|
|
184
|
+
return &i_map->blocks[i];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return NULL;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Whether a loop or try_table encloses the instruction at i_wasmOffset (see
|
|
192
|
+
// M3BlockStart)
|
|
193
|
+
static inline
|
|
194
|
+
bool EnclosesOffset (const M3BlockStart* i_block, u32 i_wasmOffset)
|
|
195
|
+
{
|
|
196
|
+
return i_block->wasmOffset < i_wasmOffset and i_wasmOffset < i_block->wasmEnd;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Whether the handlers of a try_table around a safepoint are in force there.
|
|
200
|
+
// A back edge has retired those of the try_tables it leaves: the ones inside
|
|
201
|
+
// the loop it goes to.
|
|
202
|
+
static inline
|
|
203
|
+
bool HandlersLiveAt (const M3SnapshotMap* i_map, const M3SafePoint* i_point, const M3BlockStart* i_try)
|
|
204
|
+
{
|
|
205
|
+
return not (i_point->kind == safepoint_op and i_try->wasmOffset > i_map->blocks[i_point->aux].wasmOffset);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// The type a call safepoint's instruction calls through, read back out of the
|
|
209
|
+
// caller's body: what its callee has to return
|
|
210
|
+
static
|
|
211
|
+
M3Result CallSiteType (IM3Function i_caller, const M3SafePoint* i_point, IM3FuncType* o_type)
|
|
212
|
+
{
|
|
213
|
+
M3Result result = m3Err_none;
|
|
214
|
+
IM3Module module = i_caller->module;
|
|
215
|
+
bytes_t pos = i_caller->wasm;
|
|
216
|
+
cbytes_t end = i_caller->wasmEnd;
|
|
217
|
+
u32 size = 0;
|
|
218
|
+
u32 index = 0;
|
|
219
|
+
m3opcode_t opcode = 0;
|
|
220
|
+
|
|
221
|
+
*o_type = NULL;
|
|
222
|
+
|
|
223
|
+
_ (ReadLEB_u32(&size, &pos, end));
|
|
224
|
+
_throwif(m3Err_wasmUnderrun, i_point->wasmOffset >= (size_t)(end - pos));
|
|
225
|
+
pos += i_point->wasmOffset;
|
|
226
|
+
|
|
227
|
+
_ (Read_opcode(&opcode, &pos, end));
|
|
228
|
+
_ (ReadLEB_u32(&index, &pos, end));
|
|
229
|
+
|
|
230
|
+
if (opcode == c_waOp_call) {
|
|
231
|
+
_throwif(m3Err_wasmMalformed, index >= module->numFunctions);
|
|
232
|
+
*o_type = module->functions[index].funcType;
|
|
233
|
+
} else {
|
|
234
|
+
_throwif(m3Err_wasmMalformed, opcode != c_waOp_callIndirect and opcode != c_waOp_callRef);
|
|
235
|
+
_throwif(m3Err_wasmMalformed, index >= module->numFuncTypes);
|
|
236
|
+
*o_type = module->funcTypes[index];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
_catch:
|
|
240
|
+
return result;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Whether a function can stand where i_expected was called: it returns as
|
|
244
|
+
// many values, each a subtype of the one expected - which is what a
|
|
245
|
+
// return_call asks of the function it hands its frame to
|
|
246
|
+
static
|
|
247
|
+
bool ReturnsWhat (IM3FuncType i_type, IM3FuncType i_expected)
|
|
248
|
+
{
|
|
249
|
+
u16 numResults = GetFuncTypeNumResults(i_expected);
|
|
250
|
+
|
|
251
|
+
if (not i_type or GetFuncTypeNumResults(i_type) != numResults) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
for (u16 i = 0; i < numResults; ++i) {
|
|
256
|
+
if (not IsSubTypeOf(GetFuncTypeResultType(i_type, i), GetFuncTypeResultType(i_expected, i))) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
266
|
+
// values in a frame
|
|
267
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
268
|
+
|
|
269
|
+
// The registers a frame goes on with. A frame that goes on with clear ones - a
|
|
270
|
+
// resume - has none, and a value the map puts in one cannot be there.
|
|
271
|
+
typedef struct M3FrameRegisters {
|
|
272
|
+
m3reg_t* r0;
|
|
273
|
+
# if d_m3HasFloat
|
|
274
|
+
f64* fp0;
|
|
275
|
+
# endif
|
|
276
|
+
} M3FrameRegisters;
|
|
277
|
+
|
|
278
|
+
// The stack a continuation's frames are on: the runtime's own for the root
|
|
279
|
+
static
|
|
280
|
+
M3Result ContinuationStack (IM3Runtime i_runtime, IM3Continuation i_cont, m3slot_t** o_base, u32* o_numSlots)
|
|
281
|
+
{
|
|
282
|
+
if (i_cont == i_runtime->rootContinuation) {
|
|
283
|
+
*o_base = (m3slot_t*)i_runtime->originStack;
|
|
284
|
+
*o_numSlots = i_runtime->numStackSlots;
|
|
285
|
+
} else {
|
|
286
|
+
*o_base = i_cont->valStack;
|
|
287
|
+
*o_numSlots = i_cont->numStackSlots;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return (*o_base) ? m3Err_none : "a suspended continuation has no stack";
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Where a value is kept, or NULL for a register; o_bytes is how much of the
|
|
294
|
+
// frame it takes, which has to be inside the stack the frame is on
|
|
295
|
+
static
|
|
296
|
+
M3Result LocateValue (m3slot_t* i_base, u32 i_numSlots, m3stack_t i_sp, const M3SlotValue* i_value, u8** o_where)
|
|
297
|
+
{
|
|
298
|
+
M3Result result = m3Err_none;
|
|
299
|
+
u8 type = BaseTypeOf(i_value->type);
|
|
300
|
+
|
|
301
|
+
*o_where = NULL;
|
|
302
|
+
|
|
303
|
+
_throwif(m3Err_wasmMalformed, type == c_m3Type_none or type > c_m3Type_contref);
|
|
304
|
+
|
|
305
|
+
// no operation reads or writes one, so a v128 only ever sits in slots
|
|
306
|
+
_throwif("a v128 value is kept in a register", type == c_m3Type_v128 and IsRegisterSlotAlias(i_value->slot));
|
|
307
|
+
|
|
308
|
+
if (not IsRegisterSlotAlias(i_value->slot)) {
|
|
309
|
+
size_t size = (type == c_m3Type_v128) ? 16
|
|
310
|
+
: (IsRefType(type) ? sizeof(void*)
|
|
311
|
+
: (Is64BitType(type) ? sizeof(u64) : sizeof(u32)));
|
|
312
|
+
size_t offset = (size_t)((m3slot_t*)i_sp - i_base + i_value->slot) * sizeof(m3slot_t);
|
|
313
|
+
|
|
314
|
+
_throwif("a value is outside the stack its frame is on", offset + size > (size_t)i_numSlots * sizeof(m3slot_t));
|
|
315
|
+
|
|
316
|
+
*o_where = (u8*)i_base + offset;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
_catch:
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// A value as the file holds it before a reference is named: a number's bits,
|
|
324
|
+
// zero-extended to 64, or the reference itself
|
|
325
|
+
static
|
|
326
|
+
M3Result ReadValue (u8* i_where, const M3FrameRegisters* i_registers, const M3SlotValue* i_value, u64* o_bits,
|
|
327
|
+
void** o_reference)
|
|
328
|
+
{
|
|
329
|
+
M3Result result = m3Err_none;
|
|
330
|
+
u8 type = BaseTypeOf(i_value->type);
|
|
331
|
+
|
|
332
|
+
*o_bits = 0;
|
|
333
|
+
*o_reference = NULL;
|
|
334
|
+
|
|
335
|
+
if (i_where) {
|
|
336
|
+
if (IsRefType(type)) {
|
|
337
|
+
memcpy(o_reference, i_where, sizeof(void*));
|
|
338
|
+
} else if (Is64BitType(type)) {
|
|
339
|
+
memcpy(o_bits, i_where, sizeof(u64));
|
|
340
|
+
} else {
|
|
341
|
+
u32 narrow;
|
|
342
|
+
memcpy(&narrow, i_where, sizeof(narrow));
|
|
343
|
+
*o_bits = narrow;
|
|
344
|
+
}
|
|
345
|
+
} else if (IsFpRegisterSlotAlias(i_value->slot)) {
|
|
346
|
+
# if d_m3HasFloat
|
|
347
|
+
_throwif("a value is kept in a register the frame does not hold", not i_registers or not i_registers->fp0);
|
|
348
|
+
|
|
349
|
+
if (type == c_m3Type_f32) {
|
|
350
|
+
f32 narrow = (f32)*i_registers->fp0;
|
|
351
|
+
u32 bits;
|
|
352
|
+
memcpy(&bits, &narrow, sizeof(bits));
|
|
353
|
+
*o_bits = bits;
|
|
354
|
+
} else {
|
|
355
|
+
_throwif(m3Err_wasmMalformed, type != c_m3Type_f64);
|
|
356
|
+
memcpy(o_bits, i_registers->fp0, sizeof(u64));
|
|
357
|
+
}
|
|
358
|
+
# else
|
|
359
|
+
_throw(m3Err_wasmMalformed);
|
|
360
|
+
# endif
|
|
361
|
+
} else {
|
|
362
|
+
_throwif("a value is kept in a register the frame does not hold", not i_registers or not i_registers->r0);
|
|
363
|
+
|
|
364
|
+
m3reg_t r0 = *i_registers->r0;
|
|
365
|
+
|
|
366
|
+
if (IsRefType(type)) {
|
|
367
|
+
*o_reference = (void*)(uintptr_t)r0;
|
|
368
|
+
} else if (Is64BitType(type)) {
|
|
369
|
+
*o_bits = (u64)r0;
|
|
370
|
+
} else {
|
|
371
|
+
*o_bits = (u32)r0;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
_catch:
|
|
376
|
+
return result;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
static
|
|
380
|
+
M3Result WriteValue (u8* o_where, const M3FrameRegisters* i_registers, const M3SlotValue* i_value, u64 i_bits,
|
|
381
|
+
void* i_reference)
|
|
382
|
+
{
|
|
383
|
+
M3Result result = m3Err_none;
|
|
384
|
+
u8 type = BaseTypeOf(i_value->type);
|
|
385
|
+
|
|
386
|
+
if (o_where) {
|
|
387
|
+
if (IsRefType(type)) {
|
|
388
|
+
memcpy(o_where, &i_reference, sizeof(void*));
|
|
389
|
+
} else if (Is64BitType(type)) {
|
|
390
|
+
memcpy(o_where, &i_bits, sizeof(u64));
|
|
391
|
+
} else {
|
|
392
|
+
u32 narrow = (u32)i_bits;
|
|
393
|
+
memcpy(o_where, &narrow, sizeof(narrow));
|
|
394
|
+
}
|
|
395
|
+
} else if (IsFpRegisterSlotAlias(i_value->slot)) {
|
|
396
|
+
# if d_m3HasFloat
|
|
397
|
+
_throwif("a value is kept in a register the frame does not hold", not i_registers or not i_registers->fp0);
|
|
398
|
+
|
|
399
|
+
if (type == c_m3Type_f32) {
|
|
400
|
+
u32 bits = (u32)i_bits;
|
|
401
|
+
f32 narrow;
|
|
402
|
+
memcpy(&narrow, &bits, sizeof(narrow));
|
|
403
|
+
*i_registers->fp0 = narrow;
|
|
404
|
+
} else {
|
|
405
|
+
_throwif(m3Err_wasmMalformed, type != c_m3Type_f64);
|
|
406
|
+
memcpy(i_registers->fp0, &i_bits, sizeof(u64));
|
|
407
|
+
}
|
|
408
|
+
# else
|
|
409
|
+
_throw(m3Err_wasmMalformed);
|
|
410
|
+
# endif
|
|
411
|
+
} else {
|
|
412
|
+
_throwif("a value is kept in a register the frame does not hold", not i_registers or not i_registers->r0);
|
|
413
|
+
|
|
414
|
+
if (IsRefType(type)) {
|
|
415
|
+
*i_registers->r0 = (m3reg_t)(uintptr_t)i_reference;
|
|
416
|
+
} else {
|
|
417
|
+
*i_registers->r0 = (m3reg_t)i_bits;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
_catch:
|
|
422
|
+
return result;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// The value a safepoint lists at i_index: the function's locals first, then
|
|
426
|
+
// the operand stack
|
|
427
|
+
static
|
|
428
|
+
const M3SlotValue* FrameValue (const M3SnapshotMap* i_map, const M3SafePoint* i_point, u32 i_index)
|
|
429
|
+
{
|
|
430
|
+
if (i_index < i_map->numLocals) {
|
|
431
|
+
return i_point->firstLocalValue == UINT32_MAX ? &i_map->locals[i_index]
|
|
432
|
+
: &i_map->values[i_point->firstLocalValue + i_index];
|
|
433
|
+
}
|
|
434
|
+
return &i_map->values[i_point->firstValue + i_index - i_map->numLocals];
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// A frame's constants are copied in by op_Entry, and op_Entry already ran
|
|
438
|
+
static
|
|
439
|
+
void RestoreConstants (IM3Function i_function, m3stack_t i_sp)
|
|
440
|
+
{
|
|
441
|
+
if (i_function->constants) {
|
|
442
|
+
u8* where = (u8*)((m3slot_t*)i_sp + i_function->numRetAndArgSlots) + i_function->numLocalBytes;
|
|
443
|
+
|
|
444
|
+
memcpy(where, i_function->constants, i_function->numConstantBytes);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
450
|
+
// pointer ids
|
|
451
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
452
|
+
|
|
453
|
+
// Pointers numbered in the order they were first added, with a hash index so
|
|
454
|
+
// the lookup does not have to walk them
|
|
455
|
+
typedef struct M3PointerIds {
|
|
456
|
+
void** items;
|
|
457
|
+
u32 count;
|
|
458
|
+
u32 capacity;
|
|
459
|
+
|
|
460
|
+
u32* index; // id + 1, or 0 for an empty bucket
|
|
461
|
+
u32 indexCapacity; // a power of two, kept at least twice count
|
|
462
|
+
} M3PointerIds;
|
|
463
|
+
|
|
464
|
+
static
|
|
465
|
+
u32 PointerBucket (void* i_pointer, u32 i_capacity)
|
|
466
|
+
{
|
|
467
|
+
u64 bits = (u64)(uintptr_t)i_pointer;
|
|
468
|
+
|
|
469
|
+
return (u32)((bits * 0x9E3779B97F4A7C15ULL) >> 32) & (i_capacity - 1);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
static
|
|
473
|
+
u32 PointerIds_Find (const M3PointerIds* i_ids, void* i_pointer)
|
|
474
|
+
{
|
|
475
|
+
if (i_ids->indexCapacity) {
|
|
476
|
+
u32 bucket = PointerBucket(i_pointer, i_ids->indexCapacity);
|
|
477
|
+
|
|
478
|
+
while (i_ids->index[bucket]) {
|
|
479
|
+
u32 id = i_ids->index[bucket] - 1;
|
|
480
|
+
|
|
481
|
+
if (i_ids->items[id] == i_pointer) {
|
|
482
|
+
return id;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
bucket = (bucket + 1) & (i_ids->indexCapacity - 1);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
return d_m3SnapshotNone;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
static
|
|
493
|
+
M3Result PointerIds_Add (M3PointerIds* io_ids, void* i_pointer, u32* o_id)
|
|
494
|
+
{
|
|
495
|
+
u32 id = PointerIds_Find(io_ids, i_pointer);
|
|
496
|
+
|
|
497
|
+
if (id != d_m3SnapshotNone) {
|
|
498
|
+
*o_id = id;
|
|
499
|
+
return m3Err_none;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (io_ids->count == io_ids->capacity) {
|
|
503
|
+
u32 capacity = io_ids->capacity ? io_ids->capacity * 2 : 16;
|
|
504
|
+
void** items = m3_ReallocArray(void*, io_ids->items, capacity, io_ids->capacity);
|
|
505
|
+
|
|
506
|
+
if (not items) {
|
|
507
|
+
return m3Err_mallocFailed;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
io_ids->items = items;
|
|
511
|
+
io_ids->capacity = capacity;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if ((io_ids->count + 1) * 2 > io_ids->indexCapacity) {
|
|
515
|
+
u32 capacity = io_ids->indexCapacity ? io_ids->indexCapacity * 2 : 32;
|
|
516
|
+
u32* index = m3_AllocArray(u32, capacity);
|
|
517
|
+
|
|
518
|
+
if (not index) {
|
|
519
|
+
return m3Err_mallocFailed;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
for (u32 i = 0; i < io_ids->count; ++i) {
|
|
523
|
+
u32 bucket = PointerBucket(io_ids->items[i], capacity);
|
|
524
|
+
|
|
525
|
+
while (index[bucket]) {
|
|
526
|
+
bucket = (bucket + 1) & (capacity - 1);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
index[bucket] = i + 1;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
m3_Free(io_ids->index);
|
|
533
|
+
io_ids->index = index;
|
|
534
|
+
io_ids->indexCapacity = capacity;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
id = io_ids->count++;
|
|
538
|
+
io_ids->items[id] = i_pointer;
|
|
539
|
+
|
|
540
|
+
u32 bucket = PointerBucket(i_pointer, io_ids->indexCapacity);
|
|
541
|
+
|
|
542
|
+
while (io_ids->index[bucket]) {
|
|
543
|
+
bucket = (bucket + 1) & (io_ids->indexCapacity - 1);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
io_ids->index[bucket] = id + 1;
|
|
547
|
+
|
|
548
|
+
*o_id = id;
|
|
549
|
+
|
|
550
|
+
return m3Err_none;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
static
|
|
554
|
+
void PointerIds_Free (M3PointerIds* io_ids)
|
|
555
|
+
{
|
|
556
|
+
m3_Free(io_ids->items);
|
|
557
|
+
m3_Free(io_ids->index);
|
|
558
|
+
memset(io_ids, 0, sizeof(*io_ids));
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
563
|
+
// buffers
|
|
564
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
565
|
+
|
|
566
|
+
typedef struct M3BufferWriter {
|
|
567
|
+
u8* buffer;
|
|
568
|
+
size_t size;
|
|
569
|
+
size_t capacity;
|
|
570
|
+
} M3BufferWriter;
|
|
571
|
+
|
|
572
|
+
static
|
|
573
|
+
M3Result BufferWriter_Write (const void* i_data, size_t i_size, void* i_userdata)
|
|
574
|
+
{
|
|
575
|
+
M3BufferWriter* bw = (M3BufferWriter*)i_userdata;
|
|
576
|
+
|
|
577
|
+
if (bw->size + i_size > bw->capacity) {
|
|
578
|
+
size_t newCap = bw->capacity ? bw->capacity * 2 : 4096;
|
|
579
|
+
while (bw->size + i_size > newCap) {
|
|
580
|
+
newCap *= 2;
|
|
581
|
+
}
|
|
582
|
+
u8* newBuf = m3_ReallocArray(u8, bw->buffer, newCap, bw->capacity);
|
|
583
|
+
if (!newBuf) {
|
|
584
|
+
return m3Err_mallocFailed;
|
|
585
|
+
}
|
|
586
|
+
bw->buffer = newBuf;
|
|
587
|
+
bw->capacity = newCap;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
if (i_size) {
|
|
591
|
+
memcpy(bw->buffer + bw->size, i_data, i_size);
|
|
592
|
+
bw->size += i_size;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
return m3Err_none;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
typedef struct M3BufferReader {
|
|
599
|
+
const u8* buffer;
|
|
600
|
+
size_t size;
|
|
601
|
+
size_t cursor;
|
|
602
|
+
} M3BufferReader;
|
|
603
|
+
|
|
604
|
+
static
|
|
605
|
+
M3Result BufferReader_Read (void* o_buffer, size_t i_size, void* i_userdata)
|
|
606
|
+
{
|
|
607
|
+
M3BufferReader* br = (M3BufferReader*)i_userdata;
|
|
608
|
+
|
|
609
|
+
if (i_size > br->size - br->cursor) {
|
|
610
|
+
return m3Err_wasmMalformed;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (i_size) {
|
|
614
|
+
memcpy(o_buffer, br->buffer + br->cursor, i_size);
|
|
615
|
+
br->cursor += i_size;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return m3Err_none;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
623
|
+
// saving
|
|
624
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
625
|
+
|
|
626
|
+
// The snapshot is written twice. The first pass writes nothing and only
|
|
627
|
+
// follows references: every continuation and exception it meets is numbered,
|
|
628
|
+
// and the ones it numbers are followed in turn until nothing new turns up. The
|
|
629
|
+
// second pass writes, and by then every reference has the id it will be
|
|
630
|
+
// written as - which the file needs up front, since what a reference names can
|
|
631
|
+
// come later in it than the reference does.
|
|
632
|
+
typedef struct M3SnapshotSave {
|
|
633
|
+
IM3Runtime runtime;
|
|
634
|
+
IM3Module module;
|
|
635
|
+
M3SnapshotWriter writer;
|
|
636
|
+
void* userdata;
|
|
637
|
+
|
|
638
|
+
bool postmortem;
|
|
639
|
+
bool embedded;
|
|
640
|
+
bool writing;
|
|
641
|
+
|
|
642
|
+
M3PointerIds liveContinuations; // everything the runtime still holds, to check
|
|
643
|
+
M3PointerIds liveExceptions; // a reference against before following it
|
|
644
|
+
M3PointerIds continuations; // what the snapshot carries, by id
|
|
645
|
+
M3PointerIds exceptions;
|
|
646
|
+
} M3SnapshotSave;
|
|
647
|
+
|
|
648
|
+
static inline
|
|
649
|
+
u8 M3TypeToValType (m3type_t i_type)
|
|
650
|
+
{
|
|
651
|
+
switch (BaseTypeOf(i_type)) {
|
|
652
|
+
case c_m3Type_i32: return 0x7F;
|
|
653
|
+
case c_m3Type_i64: return 0x7E;
|
|
654
|
+
case c_m3Type_f32: return 0x7D;
|
|
655
|
+
case c_m3Type_f64: return 0x7C;
|
|
656
|
+
case c_m3Type_v128: return 0x7B;
|
|
657
|
+
case c_m3Type_funcref: return 0x70;
|
|
658
|
+
case c_m3Type_externref: return 0x6F;
|
|
659
|
+
case c_m3Type_exnref: return 0x69;
|
|
660
|
+
case c_m3Type_contref: return 0x68;
|
|
661
|
+
default: return 0x00;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
static inline
|
|
666
|
+
m3type_t ValTypeToM3Type (u8 i_valtype)
|
|
667
|
+
{
|
|
668
|
+
switch (i_valtype) {
|
|
669
|
+
case 0x7F: return c_m3Type_i32;
|
|
670
|
+
case 0x7E: return c_m3Type_i64;
|
|
671
|
+
case 0x7D: return c_m3Type_f32;
|
|
672
|
+
case 0x7C: return c_m3Type_f64;
|
|
673
|
+
case 0x7B: return c_m3Type_v128;
|
|
674
|
+
case 0x70: return c_m3Type_funcref;
|
|
675
|
+
case 0x6F: return c_m3Type_externref;
|
|
676
|
+
case 0x69: return c_m3Type_exnref;
|
|
677
|
+
case 0x68: return c_m3Type_contref;
|
|
678
|
+
default: return c_m3Type_none;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
static
|
|
683
|
+
M3Result Put (M3SnapshotSave* s, const void* i_data, size_t i_size)
|
|
684
|
+
{
|
|
685
|
+
return s->writing ? s->writer(i_data, i_size, s->userdata) : m3Err_none;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
static
|
|
689
|
+
M3Result PutU8 (M3SnapshotSave* s, u8 i_value)
|
|
690
|
+
{
|
|
691
|
+
return Put(s, &i_value, sizeof(i_value));
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
static
|
|
696
|
+
M3Result PutLEB_u32 (M3SnapshotSave* s, u32 i_value)
|
|
697
|
+
{
|
|
698
|
+
u8 bytes[5];
|
|
699
|
+
u32 len = 0;
|
|
700
|
+
|
|
701
|
+
do {
|
|
702
|
+
u8 byte = i_value & 0x7F;
|
|
703
|
+
i_value >>= 7;
|
|
704
|
+
if (i_value != 0) {
|
|
705
|
+
byte |= 0x80;
|
|
706
|
+
}
|
|
707
|
+
bytes[len++] = byte;
|
|
708
|
+
} while (i_value != 0);
|
|
709
|
+
|
|
710
|
+
return Put(s, bytes, len);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
static
|
|
714
|
+
M3Result PutLEB_u64 (M3SnapshotSave* s, u64 i_value)
|
|
715
|
+
{
|
|
716
|
+
u8 bytes[10];
|
|
717
|
+
u32 len = 0;
|
|
718
|
+
|
|
719
|
+
do {
|
|
720
|
+
u8 byte = i_value & 0x7F;
|
|
721
|
+
i_value >>= 7;
|
|
722
|
+
if (i_value != 0) {
|
|
723
|
+
byte |= 0x80;
|
|
724
|
+
}
|
|
725
|
+
bytes[len++] = byte;
|
|
726
|
+
} while (i_value != 0);
|
|
727
|
+
|
|
728
|
+
return Put(s, bytes, len);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
static
|
|
732
|
+
M3Result PutLEB_i64 (M3SnapshotSave* s, i64 i_value)
|
|
733
|
+
{
|
|
734
|
+
u8 bytes[10];
|
|
735
|
+
u32 len = 0;
|
|
736
|
+
bool more = true;
|
|
737
|
+
|
|
738
|
+
while (more) {
|
|
739
|
+
u8 byte = i_value & 0x7F;
|
|
740
|
+
i_value >>= 7;
|
|
741
|
+
if ((i_value == 0 && !(byte & 0x40)) || (i_value == -1 && (byte & 0x40))) {
|
|
742
|
+
more = false;
|
|
743
|
+
} else {
|
|
744
|
+
byte |= 0x80;
|
|
745
|
+
}
|
|
746
|
+
bytes[len++] = byte;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
return Put(s, bytes, len);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// What one section writes. Every section body takes the save state and nothing
|
|
753
|
+
// else, so SaveSection can drive them all the same way.
|
|
754
|
+
typedef M3Result (*M3SnapshotSectionFn)(M3SnapshotSave* s);
|
|
755
|
+
|
|
756
|
+
// Writes one section: the body into a buffer first, so its length can go in
|
|
757
|
+
// front of it, then the whole thing to the real writer. The buffer writer is
|
|
758
|
+
// swapped onto s itself rather than onto a copy of it - a body may name a
|
|
759
|
+
// reference, and the ids that name one belong to s, not to a copy that is
|
|
760
|
+
// about to be thrown away.
|
|
761
|
+
static
|
|
762
|
+
M3Result SaveSection (M3SnapshotSave* s, u8 i_sectionId, M3SnapshotSectionFn i_body)
|
|
763
|
+
{
|
|
764
|
+
M3Result result = m3Err_none;
|
|
765
|
+
M3BufferWriter bw = { NULL, 0, 0 };
|
|
766
|
+
M3SnapshotWriter writer = s->writer;
|
|
767
|
+
void* userdata = s->userdata;
|
|
768
|
+
|
|
769
|
+
s->writer = BufferWriter_Write;
|
|
770
|
+
s->userdata = &bw;
|
|
771
|
+
|
|
772
|
+
result = i_body(s);
|
|
773
|
+
|
|
774
|
+
s->writer = writer;
|
|
775
|
+
s->userdata = userdata;
|
|
776
|
+
|
|
777
|
+
if (result) {
|
|
778
|
+
goto _catch;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// a section with nothing in it is left out
|
|
782
|
+
if (bw.size) {
|
|
783
|
+
_throwif("snapshot section is too large", bw.size > UINT32_MAX);
|
|
784
|
+
_ (PutU8(s, i_sectionId));
|
|
785
|
+
_ (PutLEB_u32(s, (u32)bw.size));
|
|
786
|
+
_ (Put(s, bw.buffer, bw.size));
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
_catch:
|
|
790
|
+
m3_Free(bw.buffer);
|
|
791
|
+
|
|
792
|
+
return result;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
static
|
|
796
|
+
u32 FunctionIndex (IM3Module i_module, IM3Function i_function)
|
|
797
|
+
{
|
|
798
|
+
if (i_function and i_function >= i_module->functions and i_function < i_module->functions + i_module->numFunctions) {
|
|
799
|
+
return (u32)(i_function - i_module->functions);
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
return d_m3SnapshotNone;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
static
|
|
806
|
+
u32 FuncTypeIndex (IM3Module i_module, IM3FuncType i_type)
|
|
807
|
+
{
|
|
808
|
+
for (u32 i = 0; i < i_module->numFuncTypes; ++i) {
|
|
809
|
+
if (i_module->funcTypes[i] == i_type) {
|
|
810
|
+
return i;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
return d_m3SnapshotNone;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
# if d_m3HasExceptionHandling
|
|
818
|
+
|
|
819
|
+
// Code refers to a tag as whatever it resolved to, so that is what an
|
|
820
|
+
// exception holds
|
|
821
|
+
static
|
|
822
|
+
IM3Tag TagOfIndex (IM3Module i_module, u32 i_index)
|
|
823
|
+
{
|
|
824
|
+
IM3Tag tag = &i_module->tags[i_index];
|
|
825
|
+
|
|
826
|
+
return tag->resolved ? tag->resolved : tag;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
static
|
|
830
|
+
u32 TagIndex (IM3Module i_module, IM3Tag i_tag)
|
|
831
|
+
{
|
|
832
|
+
for (u32 i = 0; i < i_module->numTags; ++i) {
|
|
833
|
+
if (TagOfIndex(i_module, i) == i_tag) {
|
|
834
|
+
return i;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
return d_m3SnapshotNone;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
# endif
|
|
842
|
+
|
|
843
|
+
// The word a reference is written as: a function index, an object id, the
|
|
844
|
+
// embedder's name for an externref, or d_m3SnapshotNullRef
|
|
845
|
+
static
|
|
846
|
+
M3Result NameReference (M3SnapshotSave* s, u8 i_type, void* i_reference, u64* o_word)
|
|
847
|
+
{
|
|
848
|
+
M3Result result = m3Err_none;
|
|
849
|
+
u32 id = d_m3SnapshotNone;
|
|
850
|
+
|
|
851
|
+
*o_word = d_m3SnapshotNullRef;
|
|
852
|
+
|
|
853
|
+
if (not i_reference) {
|
|
854
|
+
return m3Err_none;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
if (i_type == c_m3Type_funcref) {
|
|
858
|
+
id = FunctionIndex(s->module, (IM3Function)i_reference);
|
|
859
|
+
_throwif("a funcref names a function of another module", id == d_m3SnapshotNone);
|
|
860
|
+
} else if (i_type == c_m3Type_externref) {
|
|
861
|
+
const M3SnapshotHooks* hooks = &s->runtime->snapshotHooks;
|
|
862
|
+
|
|
863
|
+
_throwif("an externref belongs to the host, and cannot be saved", not hooks->nameExternRef);
|
|
864
|
+
|
|
865
|
+
u64 name = d_m3SnapshotNullRef;
|
|
866
|
+
_ (hooks->nameExternRef(hooks->userdata, i_reference, &name));
|
|
867
|
+
_throwif("the host named an externref as null", name == d_m3SnapshotNullRef);
|
|
868
|
+
|
|
869
|
+
*o_word = name;
|
|
870
|
+
goto _catch;
|
|
871
|
+
}
|
|
872
|
+
# if d_m3HasExceptionHandling
|
|
873
|
+
else if (i_type == c_m3Type_exnref) {
|
|
874
|
+
_throwif("an exnref names an exception that no longer exists",
|
|
875
|
+
PointerIds_Find(&s->liveExceptions, i_reference) == d_m3SnapshotNone);
|
|
876
|
+
|
|
877
|
+
u32 count = s->exceptions.count;
|
|
878
|
+
_ (PointerIds_Add(&s->exceptions, i_reference, &id));
|
|
879
|
+
_throwif("internal: an exception was found only while writing", s->writing and s->exceptions.count != count);
|
|
880
|
+
}
|
|
881
|
+
# endif
|
|
882
|
+
else if (i_type == c_m3Type_contref) {
|
|
883
|
+
_throwif("a contref names a continuation that no longer exists",
|
|
884
|
+
PointerIds_Find(&s->liveContinuations, i_reference) == d_m3SnapshotNone);
|
|
885
|
+
|
|
886
|
+
u32 count = s->continuations.count;
|
|
887
|
+
_ (PointerIds_Add(&s->continuations, i_reference, &id));
|
|
888
|
+
_throwif("internal: a continuation was found only while writing", s->writing and s->continuations.count != count);
|
|
889
|
+
} else {
|
|
890
|
+
_throw("a reference of an unknown type");
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
*o_word = id;
|
|
894
|
+
|
|
895
|
+
_catch:
|
|
896
|
+
return result;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
//// A value held in memory - a global's cell, a bound argument - as the file
|
|
900
|
+
// holds it: a reference by name, a 64-bit value whole, anything narrower
|
|
901
|
+
// zero-extended
|
|
902
|
+
static
|
|
903
|
+
M3Result PutRefValue (M3SnapshotSave* s, u8 i_type, void* i_reference)
|
|
904
|
+
{
|
|
905
|
+
M3Result result = m3Err_none;
|
|
906
|
+
u64 word = 0;
|
|
907
|
+
|
|
908
|
+
_ (NameReference(s, i_type, i_reference, &word));
|
|
909
|
+
|
|
910
|
+
if (not s->writing) {
|
|
911
|
+
return m3Err_none;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
if (word == d_m3SnapshotNullRef) {
|
|
915
|
+
_ (PutU8(s, 0x00));
|
|
916
|
+
} else {
|
|
917
|
+
u8 kind = 0;
|
|
918
|
+
if (i_type == c_m3Type_funcref) {
|
|
919
|
+
kind = 0x01;
|
|
920
|
+
} else if (i_type == c_m3Type_externref) {
|
|
921
|
+
kind = 0x02;
|
|
922
|
+
}
|
|
923
|
+
# if d_m3HasExceptionHandling
|
|
924
|
+
else if (i_type == c_m3Type_exnref) {
|
|
925
|
+
kind = 0x03;
|
|
926
|
+
}
|
|
927
|
+
# endif
|
|
928
|
+
else if (i_type == c_m3Type_contref) {
|
|
929
|
+
kind = 0x04;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
_ (PutU8(s, kind));
|
|
933
|
+
_ (PutLEB_u64(s, word));
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
_catch:
|
|
937
|
+
return result;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// The fixed-width quantities: the container's version, and a float's bit
|
|
941
|
+
// pattern. Little endian whatever the host is. Everything else in the file is
|
|
942
|
+
// a LEB128, but a float has to survive as an exact 32- or 64-bit pattern, NaN
|
|
943
|
+
// payloads and all, and the version has to sit where a reader can find it
|
|
944
|
+
// before it knows anything else about the file.
|
|
945
|
+
static
|
|
946
|
+
M3Result PutLE32 (M3SnapshotSave* s, u32 i_value)
|
|
947
|
+
{
|
|
948
|
+
u8 bytes[4];
|
|
949
|
+
|
|
950
|
+
for (u32 i = 0; i < sizeof(bytes); ++i) {
|
|
951
|
+
bytes[i] = (u8)(i_value >> (8 * i));
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
return Put(s, bytes, sizeof(bytes));
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
static
|
|
958
|
+
M3Result PutLE64 (M3SnapshotSave* s, u64 i_value)
|
|
959
|
+
{
|
|
960
|
+
u8 bytes[8];
|
|
961
|
+
|
|
962
|
+
for (u32 i = 0; i < sizeof(bytes); ++i) {
|
|
963
|
+
bytes[i] = (u8)(i_value >> (8 * i));
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
return Put(s, bytes, sizeof(bytes));
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
// The body of a value the caller already holds as bits, with a narrow type in
|
|
970
|
+
// the low bits - what ReadValue hands back for a frame slot, and what an
|
|
971
|
+
// exception payload is. Bits rather than a location: on a big-endian host the
|
|
972
|
+
// low half of a u64 is not the half its address points at.
|
|
973
|
+
static
|
|
974
|
+
M3Result PutValueBody (M3SnapshotSave* s, m3type_t i_type, u64 i_bits)
|
|
975
|
+
{
|
|
976
|
+
if (IsRefType(i_type)) {
|
|
977
|
+
return PutRefValue(s, BaseTypeOf(i_type), (void*)(uintptr_t)i_bits);
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
switch (BaseTypeOf(i_type)) {
|
|
981
|
+
case c_m3Type_i32: return PutLEB_i64(s, (i32)(u32)i_bits);
|
|
982
|
+
case c_m3Type_i64: return PutLEB_i64(s, (i64)i_bits);
|
|
983
|
+
case c_m3Type_f32: return PutLE32(s, (u32)i_bits);
|
|
984
|
+
case c_m3Type_f64: return PutLE64(s, i_bits);
|
|
985
|
+
default: return "a snapshot cannot hold a value of this type as bits";
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
static
|
|
990
|
+
M3Result PutValueTag (M3SnapshotSave* s, m3type_t i_type)
|
|
991
|
+
{
|
|
992
|
+
u8 valtype = M3TypeToValType(i_type);
|
|
993
|
+
|
|
994
|
+
if (not valtype) {
|
|
995
|
+
return "a snapshot has no tag for a value of this type";
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
return PutU8(s, valtype);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
static
|
|
1002
|
+
M3Result PutValueBits (M3SnapshotSave* s, m3type_t i_type, u64 i_bits)
|
|
1003
|
+
{
|
|
1004
|
+
M3Result result = m3Err_none;
|
|
1005
|
+
|
|
1006
|
+
_ (PutValueTag(s, i_type));
|
|
1007
|
+
_ (PutValueBody(s, i_type, i_bits));
|
|
1008
|
+
|
|
1009
|
+
_catch:
|
|
1010
|
+
return result;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
// A value as a memory location holds it: a global's cell, a bound argument, a
|
|
1014
|
+
// table element. Narrow types are read at their own width first, so what goes
|
|
1015
|
+
// on the wire is the value and not the bytes around it.
|
|
1016
|
+
static
|
|
1017
|
+
M3Result PutValue (M3SnapshotSave* s, m3type_t i_type, const void* i_where)
|
|
1018
|
+
{
|
|
1019
|
+
M3Result result = m3Err_none;
|
|
1020
|
+
|
|
1021
|
+
_ (PutValueTag(s, i_type));
|
|
1022
|
+
|
|
1023
|
+
if (IsRefType(i_type)) {
|
|
1024
|
+
void* reference;
|
|
1025
|
+
memcpy(&reference, i_where, sizeof(reference));
|
|
1026
|
+
_ (PutValueBody(s, i_type, (u64)(uintptr_t)reference));
|
|
1027
|
+
} else if (BaseTypeOf(i_type) == c_m3Type_v128) {
|
|
1028
|
+
// a v128 is a vector of bytes, and has no byte order of its own
|
|
1029
|
+
u8 bytes[16];
|
|
1030
|
+
memcpy(bytes, i_where, sizeof(bytes));
|
|
1031
|
+
_ (Put(s, bytes, sizeof(bytes)));
|
|
1032
|
+
} else if (Is64BitType(i_type)) {
|
|
1033
|
+
u64 bits;
|
|
1034
|
+
memcpy(&bits, i_where, sizeof(bits));
|
|
1035
|
+
_ (PutValueBody(s, i_type, bits));
|
|
1036
|
+
} else {
|
|
1037
|
+
u32 narrow;
|
|
1038
|
+
memcpy(&narrow, i_where, sizeof(narrow));
|
|
1039
|
+
_ (PutValueBody(s, i_type, narrow));
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
_catch:
|
|
1043
|
+
return result;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
static
|
|
1047
|
+
M3Result PutMemoryChunks (M3SnapshotSave* s, const u8* i_bytes, size_t i_size)
|
|
1048
|
+
{
|
|
1049
|
+
M3Result result = m3Err_none;
|
|
1050
|
+
size_t cursor = 0;
|
|
1051
|
+
|
|
1052
|
+
while (cursor < i_size) {
|
|
1053
|
+
u8 byte = i_bytes[cursor];
|
|
1054
|
+
size_t run = 1;
|
|
1055
|
+
|
|
1056
|
+
while (cursor + run < i_size and i_bytes[cursor + run] == byte) {
|
|
1057
|
+
run++;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
if ((byte == 0x00 or byte == 0xFF) and run >= d_m3SnapshotRunThreshold) {
|
|
1061
|
+
// the memory is zeroed before it is filled in, so a run of zeros
|
|
1062
|
+
// is written as nothing at all
|
|
1063
|
+
if (byte == 0xFF) {
|
|
1064
|
+
_ (PutU8(s, d_m3ChunkFillFF));
|
|
1065
|
+
_ (PutLEB_u32(s, (u32)cursor));
|
|
1066
|
+
_ (PutLEB_u32(s, (u32)run));
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
cursor += run;
|
|
1070
|
+
continue;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
size_t rawStart = cursor;
|
|
1074
|
+
|
|
1075
|
+
while (cursor < i_size) {
|
|
1076
|
+
u8 b = i_bytes[cursor];
|
|
1077
|
+
size_t length = 1;
|
|
1078
|
+
|
|
1079
|
+
while (cursor + length < i_size and i_bytes[cursor + length] == b) {
|
|
1080
|
+
length++;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
if ((b == 0x00 or b == 0xFF) and length >= d_m3SnapshotRunThreshold) {
|
|
1084
|
+
break;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
cursor += length;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
_ (PutU8(s, d_m3ChunkRaw));
|
|
1091
|
+
_ (PutLEB_u32(s, (u32)rawStart));
|
|
1092
|
+
_ (PutLEB_u32(s, (u32)(cursor - rawStart)));
|
|
1093
|
+
_ (Put(s, i_bytes + rawStart, cursor - rawStart));
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
_ (PutU8(s, d_m3ChunkEnd));
|
|
1097
|
+
|
|
1098
|
+
_catch:
|
|
1099
|
+
return result;
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
// Two imports can name the same memory or table, so one can stand at several
|
|
1103
|
+
// indices. The file stores it once, at the first of them, and the record at
|
|
1104
|
+
// any other index names that first one instead.
|
|
1105
|
+
static
|
|
1106
|
+
u32 Memory_FirstIndex (IM3Module module, u32 index)
|
|
1107
|
+
{
|
|
1108
|
+
u32 j = 0;
|
|
1109
|
+
while (module->memories[j] != module->memories[index]) {
|
|
1110
|
+
++j;
|
|
1111
|
+
}
|
|
1112
|
+
return j;
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
static
|
|
1116
|
+
u32 Table_FirstIndex (IM3Module module, u32 index)
|
|
1117
|
+
{
|
|
1118
|
+
u32 j = 0;
|
|
1119
|
+
while (module->tables[j] != module->tables[index]) {
|
|
1120
|
+
++j;
|
|
1121
|
+
}
|
|
1122
|
+
return j;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
static
|
|
1126
|
+
M3Result SaveMemories (M3SnapshotSave* s)
|
|
1127
|
+
{
|
|
1128
|
+
M3Result result = m3Err_none;
|
|
1129
|
+
IM3Module module = s->module;
|
|
1130
|
+
|
|
1131
|
+
_ (PutLEB_u32(s, module->numMemories));
|
|
1132
|
+
|
|
1133
|
+
for (u32 m = 0; m < module->numMemories; ++m) {
|
|
1134
|
+
IM3Memory memory = module->memories[m];
|
|
1135
|
+
u32 first = Memory_FirstIndex(module, m);
|
|
1136
|
+
bool hasData = memory and memory->mallocated;
|
|
1137
|
+
u32 pageSize = memory ? Memory_PageSize(memory) : d_m3DefaultMemPageSize;
|
|
1138
|
+
u64 bytes = memory ? memory->numPages * (u64)pageSize : 0;
|
|
1139
|
+
u32 pageBits = 0;
|
|
1140
|
+
|
|
1141
|
+
_ (PutLEB_u32(s, m));
|
|
1142
|
+
_ (PutLEB_u32(s, first));
|
|
1143
|
+
|
|
1144
|
+
if (first != m) {
|
|
1145
|
+
continue;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
// a shared memory belongs to every runtime that maps it, and the file
|
|
1149
|
+
// holds the state of one
|
|
1150
|
+
_throwif("a shared memory cannot be saved in a snapshot", memory and memory->isShared);
|
|
1151
|
+
|
|
1152
|
+
// the file counts pages and addresses bytes in 32 bits, whatever size
|
|
1153
|
+
// the memory's pages are, so one this large has no encoding here
|
|
1154
|
+
_throwif("the memory is too large for a snapshot", bytes > UINT32_MAX);
|
|
1155
|
+
_throwif("shrunken memory cannot be saved in a snapshot",
|
|
1156
|
+
memory and memory->mallocated and memory->mallocated->length < bytes);
|
|
1157
|
+
|
|
1158
|
+
// a page size is a power of two, written the way custom page sizes
|
|
1159
|
+
// writes one: as that power
|
|
1160
|
+
while ((1u << pageBits) < pageSize) {
|
|
1161
|
+
++pageBits;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
_ (PutLEB_u32(s, pageBits));
|
|
1165
|
+
_ (PutLEB_u32(s, memory ? (u32)memory->numPages : 0));
|
|
1166
|
+
|
|
1167
|
+
// linear memory is little endian whatever the host is, and nothing in
|
|
1168
|
+
// it is a reference, so the discovery pass has no reason to read it
|
|
1169
|
+
if (not s->writing) {
|
|
1170
|
+
continue;
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
if (hasData) {
|
|
1174
|
+
_ (PutMemoryChunks(s, m3MemData(memory->mallocated), (size_t)bytes));
|
|
1175
|
+
} else {
|
|
1176
|
+
_ (PutU8(s, d_m3ChunkEnd));
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
_catch:
|
|
1181
|
+
return result;
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
static
|
|
1185
|
+
M3Result SaveGlobals (M3SnapshotSave* s)
|
|
1186
|
+
{
|
|
1187
|
+
M3Result result = m3Err_none;
|
|
1188
|
+
IM3Module module = s->module;
|
|
1189
|
+
|
|
1190
|
+
_ (PutLEB_u32(s, module->numGlobals));
|
|
1191
|
+
|
|
1192
|
+
for (u32 g = 0; g < module->numGlobals; ++g) {
|
|
1193
|
+
M3Global* global = &module->globals[g];
|
|
1194
|
+
M3Global* cell = global->resolved ? global->resolved : global;
|
|
1195
|
+
|
|
1196
|
+
_ (PutLEB_u32(s, g));
|
|
1197
|
+
_ (PutValue(s, global->type, &cell->i64Value));
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
_catch:
|
|
1201
|
+
return result;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
static
|
|
1205
|
+
M3Result SaveTables (M3SnapshotSave* s)
|
|
1206
|
+
{
|
|
1207
|
+
M3Result result = m3Err_none;
|
|
1208
|
+
IM3Module module = s->module;
|
|
1209
|
+
|
|
1210
|
+
_ (PutLEB_u32(s, module->numTables));
|
|
1211
|
+
|
|
1212
|
+
for (u32 t = 0; t < module->numTables; ++t) {
|
|
1213
|
+
IM3Table table = module->tables[t];
|
|
1214
|
+
u32 first = Table_FirstIndex(module, t);
|
|
1215
|
+
|
|
1216
|
+
_ (PutLEB_u32(s, t));
|
|
1217
|
+
_ (PutLEB_u32(s, first));
|
|
1218
|
+
|
|
1219
|
+
// the first record holds the elements, and the discovery pass has
|
|
1220
|
+
// followed their references there
|
|
1221
|
+
if (first != t) {
|
|
1222
|
+
continue;
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
_ (PutU8(s, M3TypeToValType(table->type)));
|
|
1226
|
+
_ (PutLEB_u32(s, table->size));
|
|
1227
|
+
|
|
1228
|
+
for (u32 e = 0; e < table->size; ++e) {
|
|
1229
|
+
_ (PutRefValue(s, BaseTypeOf(table->type), table->elements[e]));
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
_catch:
|
|
1234
|
+
return result;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
static
|
|
1238
|
+
M3Result SaveSegments (M3SnapshotSave* s)
|
|
1239
|
+
{
|
|
1240
|
+
M3Result result = m3Err_none;
|
|
1241
|
+
IM3Module module = s->module;
|
|
1242
|
+
|
|
1243
|
+
_ (PutLEB_u32(s, module->numDataSegments));
|
|
1244
|
+
|
|
1245
|
+
for (u32 i = 0; i < module->numDataSegments; ++i) {
|
|
1246
|
+
_ (PutU8(s, module->dataSegments[i].dropped));
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
_ (PutLEB_u32(s, module->numElementSegments));
|
|
1250
|
+
|
|
1251
|
+
for (u32 i = 0; i < module->numElementSegments; ++i) {
|
|
1252
|
+
_ (PutU8(s, module->elementSegments[i].dropped));
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
_catch:
|
|
1256
|
+
return result;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
# if d_m3HasExceptionHandling
|
|
1260
|
+
|
|
1261
|
+
static
|
|
1262
|
+
M3Result SaveExceptions (M3SnapshotSave* s)
|
|
1263
|
+
{
|
|
1264
|
+
M3Result result = m3Err_none;
|
|
1265
|
+
|
|
1266
|
+
_ (PutLEB_u32(s, s->exceptions.count));
|
|
1267
|
+
|
|
1268
|
+
for (u32 i = 0; i < s->exceptions.count; ++i) {
|
|
1269
|
+
M3Exception* exn = (M3Exception*)s->exceptions.items[i];
|
|
1270
|
+
IM3FuncType type = exn->tag->type;
|
|
1271
|
+
|
|
1272
|
+
_throwif(m3Err_wasmMalformed, exn->numArgs != GetFuncTypeNumParams(type));
|
|
1273
|
+
|
|
1274
|
+
_ (PutLEB_u32(s, i));
|
|
1275
|
+
_ (PutLEB_u32(s, exn->numArgs));
|
|
1276
|
+
|
|
1277
|
+
for (u32 a = 0; a < exn->numArgs; ++a) {
|
|
1278
|
+
m3type_t argType = GetFuncTypeParamType(type, (u16)a);
|
|
1279
|
+
_ (PutValueBits(s, argType, exn->args[a]));
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
_catch:
|
|
1284
|
+
return result;
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
# endif
|
|
1288
|
+
|
|
1289
|
+
// One function's frame within a suspended continuation, as the native frames
|
|
1290
|
+
// the interpreter recorded describe it
|
|
1291
|
+
typedef struct M3Activation {
|
|
1292
|
+
IM3Function function;
|
|
1293
|
+
const M3SafePoint* point; // the safepoint it stands at
|
|
1294
|
+
m3stack_t sp;
|
|
1295
|
+
pc_t pc;
|
|
1296
|
+
u8 kind; // M3SafePointKind
|
|
1297
|
+
M3FrameRegisters registers; // the ones it goes on with
|
|
1298
|
+
const M3Frame* resume; // the resume it waits in, for safepoint_resume
|
|
1299
|
+
i32 firstFrame; // its frames, outermost first: firstFrame down to lastFrame
|
|
1300
|
+
i32 lastFrame;
|
|
1301
|
+
} M3Activation;
|
|
1302
|
+
|
|
1303
|
+
// The safepoint a frame stands at, and so the function it stands in. That is
|
|
1304
|
+
// the function the call into it named, io_function, unless a tail call has
|
|
1305
|
+
// since replaced it; then it is whichever function has a safepoint at i_pc.
|
|
1306
|
+
static
|
|
1307
|
+
const M3SafePoint* FindFrameSafePoint (IM3Module i_module, IM3Function* io_function, pc_t i_pc, u8 i_kind)
|
|
1308
|
+
{
|
|
1309
|
+
const M3SafePoint* point = *io_function ? FindSafePoint(*io_function, i_pc, i_kind) : NULL;
|
|
1310
|
+
|
|
1311
|
+
for (u32 i = 0; not point and i < i_module->numFunctions; ++i) {
|
|
1312
|
+
point = FindSafePoint(&i_module->functions[i], i_pc, i_kind);
|
|
1313
|
+
if (point) {
|
|
1314
|
+
*io_function = &i_module->functions[i];
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
return point;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
// The activation that starts at frame i_first, walking inward: the loops, try
|
|
1322
|
+
// regions and entry frame standing in the function, up to the call or resume it
|
|
1323
|
+
// waits on - or, for the innermost, up to the continuation's own suspension
|
|
1324
|
+
// point. i_function is the function the call into it named.
|
|
1325
|
+
static
|
|
1326
|
+
M3Result NextActivation (IM3Module i_module, IM3Continuation i_cont, IM3Function i_function, m3stack_t i_sp,
|
|
1327
|
+
i32 i_first, M3Activation* o_act)
|
|
1328
|
+
{
|
|
1329
|
+
M3Result result = m3Err_none;
|
|
1330
|
+
i32 i = i_first;
|
|
1331
|
+
|
|
1332
|
+
memset(o_act, 0, sizeof(*o_act));
|
|
1333
|
+
|
|
1334
|
+
o_act->function = i_function;
|
|
1335
|
+
o_act->sp = i_sp;
|
|
1336
|
+
o_act->firstFrame = i_first;
|
|
1337
|
+
|
|
1338
|
+
_throwif("a suspended call does not say what it called", not i_function);
|
|
1339
|
+
_throwif("a function in the snapshot was compiled before the runtime was made suspendable", not i_function->snapshotMap);
|
|
1340
|
+
|
|
1341
|
+
for (; i >= 0; --i) {
|
|
1342
|
+
const M3Frame* frame = &i_cont->frames[i];
|
|
1343
|
+
|
|
1344
|
+
_throwif("a suspended frame runs against another module's memory", frame->memory != Module_Memory0(i_module));
|
|
1345
|
+
_throwif("a suspended frame is not where its function's frame is", frame->sp != i_sp);
|
|
1346
|
+
|
|
1347
|
+
if (frame->kind == frame_call) {
|
|
1348
|
+
o_act->pc = frame->pc;
|
|
1349
|
+
o_act->kind = safepoint_call;
|
|
1350
|
+
o_act->registers.r0 = (m3reg_t*)&frame->call.r0;
|
|
1351
|
+
# if d_m3HasFloat
|
|
1352
|
+
o_act->registers.fp0 = (f64*)&frame->call.fp0;
|
|
1353
|
+
# endif
|
|
1354
|
+
break;
|
|
1355
|
+
} else if (frame->kind == frame_resume) {
|
|
1356
|
+
_throwif("a suspended resume is not the innermost frame", i != 0);
|
|
1357
|
+
|
|
1358
|
+
o_act->pc = frame->pc;
|
|
1359
|
+
o_act->kind = safepoint_resume;
|
|
1360
|
+
o_act->resume = frame;
|
|
1361
|
+
break;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
if (i < 0) {
|
|
1366
|
+
_throwif("a suspended frame is not where its function's frame is", i_cont->sp != i_sp);
|
|
1367
|
+
|
|
1368
|
+
o_act->pc = i_cont->pc;
|
|
1369
|
+
o_act->kind = i_cont->suspendPoint;
|
|
1370
|
+
o_act->registers.r0 = &i_cont->r0;
|
|
1371
|
+
# if d_m3HasFloat
|
|
1372
|
+
o_act->registers.fp0 = &i_cont->fp0;
|
|
1373
|
+
# endif
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
o_act->lastFrame = i;
|
|
1377
|
+
o_act->point = FindFrameSafePoint(i_module, &o_act->function, o_act->pc, o_act->kind);
|
|
1378
|
+
|
|
1379
|
+
_throwif("a suspended frame is not at a safepoint", not o_act->point);
|
|
1380
|
+
_throwif("a function in the snapshot was compiled before the runtime was made suspendable",
|
|
1381
|
+
not o_act->function->snapshotMap);
|
|
1382
|
+
|
|
1383
|
+
# if d_m3EntryKeepsFrame
|
|
1384
|
+
for (i32 k = i_first; k > o_act->lastFrame; --k) {
|
|
1385
|
+
const M3Frame* frame = &i_cont->frames[k];
|
|
1386
|
+
_throwif("a suspended frame is not the function it is entered with",
|
|
1387
|
+
frame->kind == frame_entry and frame->entry.function != o_act->function);
|
|
1388
|
+
}
|
|
1389
|
+
# endif
|
|
1390
|
+
|
|
1391
|
+
_catch:
|
|
1392
|
+
return result;
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
// A function's frame: which function, the safepoint it waits at, and every
|
|
1396
|
+
// value it holds there
|
|
1397
|
+
static
|
|
1398
|
+
M3Result SaveActivation (M3SnapshotSave* s, m3slot_t* i_base, u32 i_numSlots, const M3Activation* i_act)
|
|
1399
|
+
{
|
|
1400
|
+
M3Result result = m3Err_none;
|
|
1401
|
+
IM3Function function = i_act->function;
|
|
1402
|
+
const M3SnapshotMap* map = function->snapshotMap;
|
|
1403
|
+
u32 functionIndex = FunctionIndex(s->module, function);
|
|
1404
|
+
const M3SafePoint* point = i_act->point;
|
|
1405
|
+
u32 numValues;
|
|
1406
|
+
|
|
1407
|
+
_throwif("a suspended frame belongs to another module", functionIndex == d_m3SnapshotNone);
|
|
1408
|
+
|
|
1409
|
+
_ (PutLEB_u32(s, functionIndex));
|
|
1410
|
+
_ (PutU8(s, point->kind));
|
|
1411
|
+
_ (PutLEB_u32(s, point->wasmOffset));
|
|
1412
|
+
|
|
1413
|
+
if (point->kind == safepoint_op) {
|
|
1414
|
+
_ (PutLEB_u32(s, map->blocks[point->aux].wasmOffset));
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
numValues = map->numLocals + point->numValues;
|
|
1418
|
+
|
|
1419
|
+
_ (PutLEB_u32(s, numValues));
|
|
1420
|
+
|
|
1421
|
+
for (u32 v = 0; v < numValues; ++v) {
|
|
1422
|
+
const M3SlotValue* value = FrameValue(map, point, v);
|
|
1423
|
+
u8* where;
|
|
1424
|
+
u64 word;
|
|
1425
|
+
void* reference;
|
|
1426
|
+
|
|
1427
|
+
_ (LocateValue(i_base, i_numSlots, i_act->sp, value, &where));
|
|
1428
|
+
|
|
1429
|
+
// a v128 has no byte order of its own, and no bits path can carry it
|
|
1430
|
+
if (BaseTypeOf(value->type) == c_m3Type_v128) {
|
|
1431
|
+
_ (PutValueTag(s, value->type));
|
|
1432
|
+
_ (Put(s, where, 16));
|
|
1433
|
+
continue;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
_ (ReadValue(where, &i_act->registers, value, &word, &reference));
|
|
1437
|
+
|
|
1438
|
+
_ (PutValueBits(s, value->type, IsRefType(value->type) ? (u64)(uintptr_t)reference : word));
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
if (point->kind == safepoint_resume) {
|
|
1442
|
+
_ (PutRefValue(s, c_m3Type_contref, i_act->resume->resume.cont));
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
_catch:
|
|
1446
|
+
return result;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
// A suspended continuation, as the functions it is in the middle of, outermost
|
|
1450
|
+
// first. A call leaves the caller waiting and the callee next in; a resume the
|
|
1451
|
+
// suspension passed through ends the list, since the rest of it belongs to the
|
|
1452
|
+
// continuation that resume was running.
|
|
1453
|
+
//
|
|
1454
|
+
// A caller left waiting at a return_call compiled as a call is walked past and
|
|
1455
|
+
// not written: in Wasm its callee has taken its place, and a build that reuses
|
|
1456
|
+
// the frame has nothing there to write.
|
|
1457
|
+
static
|
|
1458
|
+
M3Result SaveSuspendedBody (M3SnapshotSave* s, IM3Continuation i_cont)
|
|
1459
|
+
{
|
|
1460
|
+
M3Result result = m3Err_none;
|
|
1461
|
+
M3Activation* acts = NULL;
|
|
1462
|
+
u32 numActs = 0;
|
|
1463
|
+
IM3Function function = i_cont->entryFunction;
|
|
1464
|
+
i32 first = (i32)i_cont->numFrames - 1;
|
|
1465
|
+
m3slot_t* base;
|
|
1466
|
+
u32 numSlots;
|
|
1467
|
+
m3stack_t sp;
|
|
1468
|
+
|
|
1469
|
+
_ (ContinuationStack(s->runtime, i_cont, &base, &numSlots));
|
|
1470
|
+
|
|
1471
|
+
// one activation for each call frame at most, and the innermost
|
|
1472
|
+
acts = m3_AllocArray(M3Activation, i_cont->numFrames + 1);
|
|
1473
|
+
_throwifnull(acts);
|
|
1474
|
+
|
|
1475
|
+
sp = base;
|
|
1476
|
+
|
|
1477
|
+
while (true) {
|
|
1478
|
+
M3Activation* act = &acts[numActs];
|
|
1479
|
+
|
|
1480
|
+
_ (NextActivation(s->module, i_cont, function, sp, first, act));
|
|
1481
|
+
|
|
1482
|
+
if (act->kind != safepoint_call) {
|
|
1483
|
+
numActs++;
|
|
1484
|
+
break;
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
_throwif("a suspended continuation stops at a call", act->lastFrame < 0);
|
|
1488
|
+
|
|
1489
|
+
if (not (act->point->flags & d_m3SafePointTailCall)) {
|
|
1490
|
+
numActs++;
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
function = i_cont->frames[act->lastFrame].call.function;
|
|
1494
|
+
sp = act->sp + act->point->aux;
|
|
1495
|
+
first = act->lastFrame - 1;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
_ (PutLEB_u32(s, numActs));
|
|
1499
|
+
|
|
1500
|
+
for (u32 a = 0; a < numActs; ++a) {
|
|
1501
|
+
_ (SaveActivation(s, base, numSlots, &acts[a]));
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
_catch:
|
|
1505
|
+
m3_Free(acts);
|
|
1506
|
+
return result;
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
static
|
|
1510
|
+
M3Result SaveContinuation (M3SnapshotSave* s, u32 i_id, IM3Continuation i_cont)
|
|
1511
|
+
{
|
|
1512
|
+
M3Result result = m3Err_none;
|
|
1513
|
+
IM3Module module = s->module;
|
|
1514
|
+
bool isRoot = (i_cont == s->runtime->rootContinuation);
|
|
1515
|
+
u8 state = snapshot_contFinished;
|
|
1516
|
+
u32 typeIndex = isRoot ? d_m3SnapshotNone : FuncTypeIndex(module, i_cont->type);
|
|
1517
|
+
|
|
1518
|
+
if (i_cont->state == cont_allocated) {
|
|
1519
|
+
state = snapshot_contAllocated;
|
|
1520
|
+
} else if (i_cont->state == cont_suspended) {
|
|
1521
|
+
state = snapshot_contSuspended;
|
|
1522
|
+
} else if (i_cont->state == cont_running) {
|
|
1523
|
+
_throwif("a continuation is running", not s->postmortem);
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
_throwif("a continuation was made with no function", not i_cont->entryFunction);
|
|
1527
|
+
_throwif("a continuation belongs to another module", FunctionIndex(module, i_cont->entryFunction) == d_m3SnapshotNone);
|
|
1528
|
+
|
|
1529
|
+
_throwif("a continuation has a type of another module", not isRoot and typeIndex == d_m3SnapshotNone);
|
|
1530
|
+
|
|
1531
|
+
_ (PutLEB_u32(s, i_id));
|
|
1532
|
+
_ (PutU8(s, state));
|
|
1533
|
+
_ (PutU8(s, isRoot));
|
|
1534
|
+
_ (PutLEB_u32(s, typeIndex));
|
|
1535
|
+
_ (PutLEB_u32(s, FunctionIndex(module, i_cont->entryFunction)));
|
|
1536
|
+
# if d_m3HasExceptionHandling
|
|
1537
|
+
_ (PutRefValue(s, c_m3Type_exnref, i_cont->resumeThrow));
|
|
1538
|
+
# else
|
|
1539
|
+
_ (PutRefValue(s, c_m3Type_exnref, NULL));
|
|
1540
|
+
# endif
|
|
1541
|
+
|
|
1542
|
+
// a postmortem has nothing to resume, so it does not say how
|
|
1543
|
+
if (s->postmortem) {
|
|
1544
|
+
goto _catch;
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
if (state == snapshot_contAllocated) {
|
|
1548
|
+
IM3FuncType inner = i_cont->entryFunction ? i_cont->entryFunction->funcType : NULL;
|
|
1549
|
+
u16 numRets = inner ? GetFuncTypeNumResults(inner) : 0;
|
|
1550
|
+
|
|
1551
|
+
_throwif(m3Err_wasmMalformed, not inner or i_cont->boundArgsCount > GetFuncTypeNumParams(inner));
|
|
1552
|
+
|
|
1553
|
+
_ (PutLEB_u32(s, i_cont->boundArgsCount));
|
|
1554
|
+
|
|
1555
|
+
for (u32 i = 0; i < i_cont->boundArgsCount; ++i) {
|
|
1556
|
+
m3type_t type = GetFuncTypeParamType(inner, (u16)i);
|
|
1557
|
+
|
|
1558
|
+
_ (PutValue(s, type, i_cont->valStack + (numRets + i) * c_ioSlotCount));
|
|
1559
|
+
}
|
|
1560
|
+
} else if (state == snapshot_contSuspended) {
|
|
1561
|
+
_ (PutLEB_u32(s, i_cont->boundArgsCount));
|
|
1562
|
+
_ (SaveSuspendedBody(s, i_cont));
|
|
1563
|
+
} else {
|
|
1564
|
+
_ (PutLEB_u32(s, 0));
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
_catch:
|
|
1568
|
+
return result;
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
static
|
|
1572
|
+
M3Result SaveContinuations (M3SnapshotSave* s)
|
|
1573
|
+
{
|
|
1574
|
+
M3Result result = m3Err_none;
|
|
1575
|
+
|
|
1576
|
+
_ (PutLEB_u32(s, s->continuations.count));
|
|
1577
|
+
|
|
1578
|
+
for (u32 i = 0; i < s->continuations.count; ++i) {
|
|
1579
|
+
_ (SaveContinuation(s, i, (IM3Continuation)s->continuations.items[i]));
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
_catch:
|
|
1583
|
+
return result;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
// The embedder's own state: the whole section, whose length is where it ends.
|
|
1587
|
+
// Nothing saved is no section at all. Only the writing pass asks for it: it
|
|
1588
|
+
// holds no references to follow.
|
|
1589
|
+
static
|
|
1590
|
+
M3Result SaveHostState (M3SnapshotSave* s)
|
|
1591
|
+
{
|
|
1592
|
+
M3Result result = m3Err_none;
|
|
1593
|
+
const M3SnapshotHooks* hooks = &s->runtime->snapshotHooks;
|
|
1594
|
+
M3BufferWriter bw = { NULL, 0, 0 };
|
|
1595
|
+
|
|
1596
|
+
if (s->writing and hooks->saveHostState and not s->postmortem) {
|
|
1597
|
+
_ (hooks->saveHostState(hooks->userdata, BufferWriter_Write, &bw));
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
if (bw.size > 0) {
|
|
1601
|
+
_ (Put(s, bw.buffer, bw.size));
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
_catch:
|
|
1605
|
+
m3_Free(bw.buffer);
|
|
1606
|
+
|
|
1607
|
+
return result;
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
static
|
|
1611
|
+
void SnapshotResourceTotals (IM3Module module, u64* memoryBytes, u64* tableElements)
|
|
1612
|
+
{
|
|
1613
|
+
*memoryBytes = *tableElements = 0;
|
|
1614
|
+
for (u32 i = 0; i < module->numMemories; ++i) {
|
|
1615
|
+
IM3Memory memory = module->memories[i];
|
|
1616
|
+
if (Memory_FirstIndex(module, i) == i and memory) {
|
|
1617
|
+
*memoryBytes += memory->numPages * (u64)Memory_PageSize(memory);
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
for (u32 i = 0; i < module->numTables; ++i) {
|
|
1621
|
+
IM3Table table = module->tables[i];
|
|
1622
|
+
if (Table_FirstIndex(module, i) == i and table) {
|
|
1623
|
+
*tableElements += table->size;
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1628
|
+
static
|
|
1629
|
+
M3Result SaveMeta (M3SnapshotSave* s)
|
|
1630
|
+
{
|
|
1631
|
+
M3Result result = m3Err_none;
|
|
1632
|
+
u64 hash = 0;
|
|
1633
|
+
u64 memoryBytes, tableElements;
|
|
1634
|
+
u32 activeStacks = 0;
|
|
1635
|
+
for (u32 i = 0; i < s->continuations.count; ++i) {
|
|
1636
|
+
IM3Continuation cont = (IM3Continuation)s->continuations.items[i];
|
|
1637
|
+
if (cont != s->runtime->rootContinuation and
|
|
1638
|
+
(cont->state == cont_allocated or cont->state == cont_suspended)) {
|
|
1639
|
+
activeStacks++;
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
SnapshotResourceTotals(s->module, &memoryBytes, &tableElements);
|
|
1643
|
+
|
|
1644
|
+
_ (ModuleFingerprint(s->module, &hash));
|
|
1645
|
+
|
|
1646
|
+
_ (PutLEB_u32(s, s->postmortem ? d_m3SnapshotFlagPostmortem : 0));
|
|
1647
|
+
_ (PutLEB_u64(s, m3_HostTimeMs()));
|
|
1648
|
+
_ (PutLEB_u64(s, hash));
|
|
1649
|
+
_ (PutLEB_u32(s, s->continuations.count));
|
|
1650
|
+
_ (PutLEB_u32(s, s->exceptions.count));
|
|
1651
|
+
_ (PutLEB_u64(s, memoryBytes));
|
|
1652
|
+
_ (PutLEB_u64(s, tableElements));
|
|
1653
|
+
_ (PutLEB_u32(s, activeStacks));
|
|
1654
|
+
|
|
1655
|
+
# if d_m3HasExceptionHandling
|
|
1656
|
+
// each exception's tag before any of their payloads, so a reader can make
|
|
1657
|
+
// all of them before resolving a reference to one
|
|
1658
|
+
for (u32 i = 0; i < s->exceptions.count; ++i) {
|
|
1659
|
+
M3Exception* exception = (M3Exception*)s->exceptions.items[i];
|
|
1660
|
+
u32 tagIndex = TagIndex(s->module, exception->tag);
|
|
1661
|
+
|
|
1662
|
+
_throwif("an exception was thrown with another module's tag", tagIndex == d_m3SnapshotNone);
|
|
1663
|
+
|
|
1664
|
+
_ (PutLEB_u32(s, tagIndex));
|
|
1665
|
+
}
|
|
1666
|
+
# endif
|
|
1667
|
+
|
|
1668
|
+
_catch:
|
|
1669
|
+
return result;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
// The discovery pass. Reaching a continuation or an exception can find more of
|
|
1673
|
+
// either, so it goes round until neither list grows; the writing pass then
|
|
1674
|
+
// finds nothing new and can lay the sections out in order.
|
|
1675
|
+
static
|
|
1676
|
+
M3Result DiscoverReferences (M3SnapshotSave* s)
|
|
1677
|
+
{
|
|
1678
|
+
M3Result result = m3Err_none;
|
|
1679
|
+
u32 doneContinuations = 0;
|
|
1680
|
+
u32 doneExceptions = 0;
|
|
1681
|
+
|
|
1682
|
+
_ (SaveMemories(s));
|
|
1683
|
+
_ (SaveGlobals(s));
|
|
1684
|
+
_ (SaveTables(s));
|
|
1685
|
+
_ (SaveSegments(s));
|
|
1686
|
+
|
|
1687
|
+
while (doneContinuations < s->continuations.count or doneExceptions < s->exceptions.count) {
|
|
1688
|
+
# if d_m3HasExceptionHandling
|
|
1689
|
+
while (doneExceptions < s->exceptions.count) {
|
|
1690
|
+
M3Exception* exception = (M3Exception*)s->exceptions.items[doneExceptions++];
|
|
1691
|
+
IM3FuncType type = exception->tag->type;
|
|
1692
|
+
|
|
1693
|
+
_throwif(m3Err_wasmMalformed, exception->numArgs != GetFuncTypeNumParams(type));
|
|
1694
|
+
|
|
1695
|
+
for (u32 a = 0; a < exception->numArgs; ++a) {
|
|
1696
|
+
_ (PutValueBits(s, GetFuncTypeParamType(type, (u16)a), exception->args[a]));
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
# endif
|
|
1700
|
+
while (doneContinuations < s->continuations.count and doneExceptions == s->exceptions.count) {
|
|
1701
|
+
u32 id = doneContinuations++;
|
|
1702
|
+
_ (SaveContinuation(s, id, (IM3Continuation)s->continuations.items[id]));
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
_catch:
|
|
1707
|
+
return result;
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
static
|
|
1711
|
+
M3Result SaveSections (M3SnapshotSave* s)
|
|
1712
|
+
{
|
|
1713
|
+
M3Result result = m3Err_none;
|
|
1714
|
+
|
|
1715
|
+
if (not s->writing) {
|
|
1716
|
+
return DiscoverReferences(s);
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
if (not s->embedded) {
|
|
1720
|
+
_ (Put(s, c_snapshotMagic, sizeof(c_snapshotMagic)));
|
|
1721
|
+
_ (PutLE32(s, c_snapshotVersion));
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
// a section with nothing to say is left out: a module with no memory has
|
|
1725
|
+
// no Memory section, rather than one that lists none
|
|
1726
|
+
_ (SaveSection(s, d_m3SnapshotSection_Meta, SaveMeta));
|
|
1727
|
+
|
|
1728
|
+
if (s->module->numMemories) {
|
|
1729
|
+
_ (SaveSection(s, d_m3SnapshotSection_Memory, SaveMemories));
|
|
1730
|
+
}
|
|
1731
|
+
if (s->module->numTables) {
|
|
1732
|
+
_ (SaveSection(s, d_m3SnapshotSection_Table, SaveTables));
|
|
1733
|
+
}
|
|
1734
|
+
if (s->module->numGlobals) {
|
|
1735
|
+
_ (SaveSection(s, d_m3SnapshotSection_Global, SaveGlobals));
|
|
1736
|
+
}
|
|
1737
|
+
if (s->module->numDataSegments or s->module->numElementSegments) {
|
|
1738
|
+
_ (SaveSection(s, d_m3SnapshotSection_Segment, SaveSegments));
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
# if d_m3HasExceptionHandling
|
|
1742
|
+
if (s->exceptions.count) {
|
|
1743
|
+
_ (SaveSection(s, d_m3SnapshotSection_Exception, SaveExceptions));
|
|
1744
|
+
}
|
|
1745
|
+
# endif
|
|
1746
|
+
|
|
1747
|
+
if (s->continuations.count) {
|
|
1748
|
+
_ (SaveSection(s, d_m3SnapshotSection_Continuation, SaveContinuations));
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
// left out by SaveSection when the embedder has nothing to add
|
|
1752
|
+
_ (SaveSection(s, d_m3SnapshotSection_HostState, SaveHostState));
|
|
1753
|
+
|
|
1754
|
+
_catch:
|
|
1755
|
+
return result;
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
1759
|
+
// loading
|
|
1760
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
1761
|
+
|
|
1762
|
+
typedef struct M3SnapshotSuspendSite {
|
|
1763
|
+
const M3SnapshotMap* map;
|
|
1764
|
+
const M3SafePoint* point;
|
|
1765
|
+
u32 target; // child at a resume site, or zero
|
|
1766
|
+
bool resumed;
|
|
1767
|
+
m3type_t expectedType; // concrete heap type required by references to this record
|
|
1768
|
+
} M3SnapshotSuspendSite;
|
|
1769
|
+
|
|
1770
|
+
typedef struct M3SnapshotLoad {
|
|
1771
|
+
IM3Runtime runtime;
|
|
1772
|
+
IM3Module module;
|
|
1773
|
+
M3SnapshotReader reader;
|
|
1774
|
+
void* userdata;
|
|
1775
|
+
|
|
1776
|
+
bool started; // past the header, and so changing the runtime
|
|
1777
|
+
m3slot_t* checkStack;
|
|
1778
|
+
bool changed; // past Meta: a section has been applied to the module
|
|
1779
|
+
|
|
1780
|
+
IM3Continuation* continuations;
|
|
1781
|
+
u32 numContinuations;
|
|
1782
|
+
u64 requiredMemoryBytes;
|
|
1783
|
+
u64 requiredTableElements;
|
|
1784
|
+
u64 loadedMemoryBytes;
|
|
1785
|
+
u64 loadedTableElements;
|
|
1786
|
+
u32 requiredStacks;
|
|
1787
|
+
u32 loadedStacks;
|
|
1788
|
+
M3SnapshotSuspendSite* suspendSites;
|
|
1789
|
+
|
|
1790
|
+
# if d_m3HasExceptionHandling
|
|
1791
|
+
M3Exception** exceptions;
|
|
1792
|
+
# endif
|
|
1793
|
+
u32 numExceptions;
|
|
1794
|
+
|
|
1795
|
+
M3Frame* frames; // a suspended continuation's frames as they are put together, outermost first
|
|
1796
|
+
|
|
1797
|
+
bytes_t pos; // bounded section payload, read using the Wasm parser's helpers
|
|
1798
|
+
bytes_t end;
|
|
1799
|
+
bool inSection;
|
|
1800
|
+
bool checking;
|
|
1801
|
+
bool allowUnfilledRef; // a suspend's not-yet-bound result slots can contain null
|
|
1802
|
+
bool seenSections[256];
|
|
1803
|
+
} M3SnapshotLoad;
|
|
1804
|
+
|
|
1805
|
+
static
|
|
1806
|
+
M3Result Get (M3SnapshotLoad* l, void* o_data, size_t i_size)
|
|
1807
|
+
{
|
|
1808
|
+
if (not l->inSection) {
|
|
1809
|
+
return l->reader(o_data, i_size, l->userdata);
|
|
1810
|
+
}
|
|
1811
|
+
if (i_size > (size_t)(l->end - l->pos)) {
|
|
1812
|
+
return m3Err_wasmUnderrun;
|
|
1813
|
+
}
|
|
1814
|
+
if (i_size) {
|
|
1815
|
+
memcpy(o_data, l->pos, i_size);
|
|
1816
|
+
l->pos += i_size;
|
|
1817
|
+
}
|
|
1818
|
+
return m3Err_none;
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
static
|
|
1822
|
+
M3Result GetU8 (M3SnapshotLoad* l, u8* o_value)
|
|
1823
|
+
{
|
|
1824
|
+
return Get(l, o_value, sizeof(*o_value));
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
static
|
|
1828
|
+
M3Result GetBool (M3SnapshotLoad* l, u8* o_value)
|
|
1829
|
+
{
|
|
1830
|
+
M3Result result = GetU8(l, o_value);
|
|
1831
|
+
return result ? result : (*o_value <= 1 ? m3Err_none : m3Err_wasmMalformed);
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
static
|
|
1835
|
+
M3Result GetLEB_u32 (M3SnapshotLoad* l, u32* o_value)
|
|
1836
|
+
{
|
|
1837
|
+
if (l->inSection) {
|
|
1838
|
+
return ReadLEB_u32(o_value, &l->pos, l->end);
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
// Only the section envelope is streamed. Collect its length and let the
|
|
1842
|
+
// same decoder used for Wasm modules check its width and terminal bits.
|
|
1843
|
+
u8 encoded[5];
|
|
1844
|
+
for (u32 i = 0; i < sizeof(encoded); ++i) {
|
|
1845
|
+
M3Result result = GetU8(l, &encoded[i]);
|
|
1846
|
+
if (result) {
|
|
1847
|
+
return result;
|
|
1848
|
+
}
|
|
1849
|
+
if (not (encoded[i] & 0x80)) {
|
|
1850
|
+
bytes_t pos = encoded;
|
|
1851
|
+
return ReadLEB_u32(o_value, &pos, encoded + i + 1);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
return m3Err_lebOverflow;
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
static
|
|
1858
|
+
M3Result GetLEB_u64 (M3SnapshotLoad* l, u64* o_value)
|
|
1859
|
+
{
|
|
1860
|
+
return ReadLebUnsigned(o_value, 64, &l->pos, l->end);
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
static
|
|
1864
|
+
M3Result GetLEB_i64 (M3SnapshotLoad* l, i64* o_value)
|
|
1865
|
+
{
|
|
1866
|
+
return ReadLEB_i64(o_value, &l->pos, l->end);
|
|
1867
|
+
}
|
|
1868
|
+
|
|
1869
|
+
static
|
|
1870
|
+
M3Result SkipBytes (M3SnapshotLoad* l, size_t i_size)
|
|
1871
|
+
{
|
|
1872
|
+
if (i_size > (size_t)(l->end - l->pos)) {
|
|
1873
|
+
return m3Err_wasmUnderrun;
|
|
1874
|
+
}
|
|
1875
|
+
l->pos += i_size;
|
|
1876
|
+
return m3Err_none;
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
|
|
1880
|
+
static
|
|
1881
|
+
M3Result GetRefValue (M3SnapshotLoad* l, m3type_t i_type, void** o_reference)
|
|
1882
|
+
{
|
|
1883
|
+
M3Result result = m3Err_none;
|
|
1884
|
+
u8 kind = 0;
|
|
1885
|
+
u64 id = 0;
|
|
1886
|
+
|
|
1887
|
+
*o_reference = NULL;
|
|
1888
|
+
|
|
1889
|
+
_ (GetU8(l, &kind));
|
|
1890
|
+
if (kind == 0x00) {
|
|
1891
|
+
return (IsNullableRef(i_type) or l->allowUnfilledRef) ? m3Err_none : "null in a non-nullable snapshot reference";
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
_ (GetLEB_u64(l, &id));
|
|
1895
|
+
|
|
1896
|
+
if (kind == 0x01) {
|
|
1897
|
+
_throwif(m3Err_wasmMalformed, BaseTypeOf(i_type) != c_m3Type_funcref);
|
|
1898
|
+
_throwif(m3Err_wasmMalformed, id >= l->module->numFunctions);
|
|
1899
|
+
# if d_m3HasTypedRefs
|
|
1900
|
+
_throwif("a snapshot function reference has the wrong type", not IsSubTypeOf(RefTypeOfFuncType(l->module->functions[id].funcType, true), i_type));
|
|
1901
|
+
# endif
|
|
1902
|
+
*o_reference = &l->module->functions[id];
|
|
1903
|
+
} else if (kind == 0x02) {
|
|
1904
|
+
_throwif(m3Err_wasmMalformed, BaseTypeOf(i_type) != c_m3Type_externref);
|
|
1905
|
+
const M3SnapshotHooks* hooks = &l->runtime->snapshotHooks;
|
|
1906
|
+
_throwif("the snapshot holds an externref, and nothing here can bind one", not hooks->bindExternRef);
|
|
1907
|
+
if (not l->checking) {
|
|
1908
|
+
_ (hooks->bindExternRef(hooks->userdata, id, o_reference));
|
|
1909
|
+
_throwif(m3Err_wasmMalformed, not *o_reference and not IsNullableRef(i_type));
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
# if d_m3HasExceptionHandling
|
|
1913
|
+
else if (kind == 0x03) {
|
|
1914
|
+
_throwif(m3Err_wasmMalformed, BaseTypeOf(i_type) != c_m3Type_exnref);
|
|
1915
|
+
_throwif(m3Err_wasmMalformed, id >= l->numExceptions);
|
|
1916
|
+
*o_reference = l->exceptions[id];
|
|
1917
|
+
}
|
|
1918
|
+
# endif
|
|
1919
|
+
else if (kind == 0x04) {
|
|
1920
|
+
_throwif(m3Err_wasmMalformed, BaseTypeOf(i_type) != c_m3Type_contref);
|
|
1921
|
+
_throwif(m3Err_wasmMalformed, id == 0 or id >= l->numContinuations);
|
|
1922
|
+
*o_reference = l->continuations[id];
|
|
1923
|
+
# if d_m3HasTypedRefs
|
|
1924
|
+
// The record may appear later, even in another section. Check its
|
|
1925
|
+
// concrete heap type once all continuation records have been read.
|
|
1926
|
+
if (not l->checking and HeapTypeOf(i_type) != d_m3Type_heapAbstract) {
|
|
1927
|
+
m3type_t expected = i_type | d_m3Type_refNonNull;
|
|
1928
|
+
_throwif(m3Err_wasmMalformed, l->suspendSites[id].expectedType and
|
|
1929
|
+
l->suspendSites[id].expectedType != expected);
|
|
1930
|
+
l->suspendSites[id].expectedType = expected;
|
|
1931
|
+
}
|
|
1932
|
+
# endif
|
|
1933
|
+
} else {
|
|
1934
|
+
_throw(m3Err_wasmMalformed);
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
_catch:
|
|
1938
|
+
return result;
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
static
|
|
1942
|
+
M3Result GetLE32 (M3SnapshotLoad* l, u32* o_value)
|
|
1943
|
+
{
|
|
1944
|
+
u8 bytes[4];
|
|
1945
|
+
M3Result result = Get(l, bytes, sizeof(bytes));
|
|
1946
|
+
|
|
1947
|
+
*o_value = 0;
|
|
1948
|
+
|
|
1949
|
+
for (u32 i = 0; i < sizeof(bytes); ++i) {
|
|
1950
|
+
*o_value |= (u32)bytes[i] << (8 * i);
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
return result;
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
static
|
|
1957
|
+
M3Result GetLE64 (M3SnapshotLoad* l, u64* o_value)
|
|
1958
|
+
{
|
|
1959
|
+
u8 bytes[8];
|
|
1960
|
+
M3Result result = Get(l, bytes, sizeof(bytes));
|
|
1961
|
+
|
|
1962
|
+
*o_value = 0;
|
|
1963
|
+
|
|
1964
|
+
for (u32 i = 0; i < sizeof(bytes); ++i) {
|
|
1965
|
+
*o_value |= (u64)bytes[i] << (8 * i);
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
return result;
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
// Reads the tag of a value and says what it holds, refusing one the file's
|
|
1972
|
+
// type does not agree with. c_m3Type_none expects anything.
|
|
1973
|
+
static
|
|
1974
|
+
M3Result GetValueTag (M3SnapshotLoad* l, m3type_t i_expectedType, m3type_t* o_type)
|
|
1975
|
+
{
|
|
1976
|
+
M3Result result = m3Err_none;
|
|
1977
|
+
u8 valtype = 0;
|
|
1978
|
+
|
|
1979
|
+
_ (GetU8(l, &valtype));
|
|
1980
|
+
|
|
1981
|
+
*o_type = ValTypeToM3Type(valtype);
|
|
1982
|
+
|
|
1983
|
+
_throwif(m3Err_wasmMalformed, *o_type == c_m3Type_none);
|
|
1984
|
+
_throwif(m3Err_wasmMalformed,
|
|
1985
|
+
i_expectedType != c_m3Type_none and BaseTypeOf(*o_type) != BaseTypeOf(i_expectedType));
|
|
1986
|
+
if (i_expectedType != c_m3Type_none) {
|
|
1987
|
+
*o_type = i_expectedType;
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
_catch:
|
|
1991
|
+
return result;
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
// The mirror of PutValueBody: narrow types come back in the low bits
|
|
1995
|
+
static
|
|
1996
|
+
M3Result GetValueBody (M3SnapshotLoad* l, m3type_t i_type, u64* o_bits)
|
|
1997
|
+
{
|
|
1998
|
+
M3Result result = m3Err_none;
|
|
1999
|
+
|
|
2000
|
+
*o_bits = 0;
|
|
2001
|
+
|
|
2002
|
+
if (IsRefType(i_type)) {
|
|
2003
|
+
void* reference = NULL;
|
|
2004
|
+
_ (GetRefValue(l, i_type, &reference));
|
|
2005
|
+
*o_bits = (u64)(uintptr_t)reference;
|
|
2006
|
+
} else if (BaseTypeOf(i_type) == c_m3Type_i32) {
|
|
2007
|
+
i32 value = 0;
|
|
2008
|
+
_ (ReadLEB_i32(&value, &l->pos, l->end));
|
|
2009
|
+
*o_bits = (u32)value;
|
|
2010
|
+
} else if (BaseTypeOf(i_type) == c_m3Type_i64) {
|
|
2011
|
+
i64 value = 0;
|
|
2012
|
+
_ (GetLEB_i64(l, &value));
|
|
2013
|
+
*o_bits = (u64)value;
|
|
2014
|
+
} else if (BaseTypeOf(i_type) == c_m3Type_f32) {
|
|
2015
|
+
u32 bits = 0;
|
|
2016
|
+
_ (GetLE32(l, &bits));
|
|
2017
|
+
*o_bits = bits;
|
|
2018
|
+
} else if (BaseTypeOf(i_type) == c_m3Type_f64) {
|
|
2019
|
+
_ (GetLE64(l, o_bits));
|
|
2020
|
+
} else {
|
|
2021
|
+
_throw(m3Err_wasmMalformed);
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
_catch:
|
|
2025
|
+
return result;
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
static
|
|
2029
|
+
M3Result GetValueBits (M3SnapshotLoad* l, m3type_t i_expectedType, u64* o_bits)
|
|
2030
|
+
{
|
|
2031
|
+
M3Result result = m3Err_none;
|
|
2032
|
+
m3type_t type;
|
|
2033
|
+
|
|
2034
|
+
_ (GetValueTag(l, i_expectedType, &type));
|
|
2035
|
+
_ (GetValueBody(l, type, o_bits));
|
|
2036
|
+
|
|
2037
|
+
_catch:
|
|
2038
|
+
return result;
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
static
|
|
2042
|
+
M3Result GetValue (M3SnapshotLoad* l, m3type_t i_expectedType, void* o_where)
|
|
2043
|
+
{
|
|
2044
|
+
M3Result result = m3Err_none;
|
|
2045
|
+
m3type_t type;
|
|
2046
|
+
u8 scratch[16];
|
|
2047
|
+
if (l->checking) {
|
|
2048
|
+
o_where = scratch;
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
_ (GetValueTag(l, i_expectedType, &type));
|
|
2052
|
+
|
|
2053
|
+
if (IsRefType(type)) {
|
|
2054
|
+
void* reference = NULL;
|
|
2055
|
+
u64 bits = 0;
|
|
2056
|
+
_ (GetValueBody(l, type, &bits));
|
|
2057
|
+
reference = (void*)(uintptr_t)bits;
|
|
2058
|
+
memcpy(o_where, &reference, sizeof(reference));
|
|
2059
|
+
} else if (BaseTypeOf(type) == c_m3Type_v128) {
|
|
2060
|
+
_ (Get(l, o_where, 16));
|
|
2061
|
+
} else {
|
|
2062
|
+
u64 bits = 0;
|
|
2063
|
+
_ (GetValueBody(l, type, &bits));
|
|
2064
|
+
|
|
2065
|
+
if (Is64BitType(type)) {
|
|
2066
|
+
memcpy(o_where, &bits, sizeof(bits));
|
|
2067
|
+
} else {
|
|
2068
|
+
u32 narrow = (u32)bits;
|
|
2069
|
+
memcpy(o_where, &narrow, sizeof(narrow));
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
|
|
2073
|
+
_catch:
|
|
2074
|
+
return result;
|
|
2075
|
+
}
|
|
2076
|
+
|
|
2077
|
+
static
|
|
2078
|
+
M3Result GetFunction (M3SnapshotLoad* l, IM3Function* o_function)
|
|
2079
|
+
{
|
|
2080
|
+
M3Result result = m3Err_none;
|
|
2081
|
+
u32 index = 0;
|
|
2082
|
+
|
|
2083
|
+
_ (GetLEB_u32(l, &index));
|
|
2084
|
+
|
|
2085
|
+
_throwif(m3Err_wasmMalformed, index >= l->module->numFunctions);
|
|
2086
|
+
*o_function = &l->module->functions[index];
|
|
2087
|
+
|
|
2088
|
+
_catch:
|
|
2089
|
+
return result;
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
static
|
|
2093
|
+
M3Result LoadMemoryChunks (M3SnapshotLoad* l, u8* o_bytes, size_t i_size)
|
|
2094
|
+
{
|
|
2095
|
+
M3Result result = m3Err_none;
|
|
2096
|
+
|
|
2097
|
+
for (;;) {
|
|
2098
|
+
u8 chunkType = 0;
|
|
2099
|
+
u32 offset = 0, length = 0;
|
|
2100
|
+
|
|
2101
|
+
_ (GetU8(l, &chunkType));
|
|
2102
|
+
|
|
2103
|
+
if (chunkType == d_m3ChunkEnd) {
|
|
2104
|
+
break;
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
_ (GetLEB_u32(l, &offset));
|
|
2108
|
+
_ (GetLEB_u32(l, &length));
|
|
2109
|
+
|
|
2110
|
+
_throwif(m3Err_wasmMalformed, (size_t)offset > i_size or length > i_size - (size_t)offset);
|
|
2111
|
+
|
|
2112
|
+
if (chunkType == d_m3ChunkRaw) {
|
|
2113
|
+
if (l->checking or length == 0) {
|
|
2114
|
+
_ (SkipBytes(l, length));
|
|
2115
|
+
} else {
|
|
2116
|
+
_ (Get(l, o_bytes + offset, length));
|
|
2117
|
+
}
|
|
2118
|
+
} else if (chunkType == d_m3ChunkFillFF) {
|
|
2119
|
+
if (not l->checking and length) {
|
|
2120
|
+
memset(o_bytes + offset, 0xFF, length);
|
|
2121
|
+
}
|
|
2122
|
+
} else {
|
|
2123
|
+
_throw(m3Err_wasmMalformed);
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
|
|
2127
|
+
_catch:
|
|
2128
|
+
return result;
|
|
2129
|
+
}
|
|
2130
|
+
|
|
2131
|
+
static
|
|
2132
|
+
M3Result LoadMemories (M3SnapshotLoad* l)
|
|
2133
|
+
{
|
|
2134
|
+
M3Result result = m3Err_none;
|
|
2135
|
+
IM3Module module = l->module;
|
|
2136
|
+
u32 numMemories = 0;
|
|
2137
|
+
l->loadedMemoryBytes = 0;
|
|
2138
|
+
|
|
2139
|
+
_ (GetLEB_u32(l, &numMemories));
|
|
2140
|
+
// a section is present only when it has something to say
|
|
2141
|
+
_throwif(m3Err_wasmMalformed, numMemories == 0 or numMemories != module->numMemories);
|
|
2142
|
+
|
|
2143
|
+
for (u32 m = 0; m < numMemories; ++m) {
|
|
2144
|
+
u32 memIndex = 0, firstIndex = 0, pageBits = 0, numPages = 0;
|
|
2145
|
+
IM3Memory memory;
|
|
2146
|
+
|
|
2147
|
+
_ (GetLEB_u32(l, &memIndex));
|
|
2148
|
+
_ (GetLEB_u32(l, &firstIndex));
|
|
2149
|
+
|
|
2150
|
+
// every memory is listed, in index order, so the index is a check
|
|
2151
|
+
_throwif(m3Err_wasmMalformed, memIndex != m);
|
|
2152
|
+
|
|
2153
|
+
// which imports share a memory is settled by linking, not by the
|
|
2154
|
+
// module, so the instance restored into has to share the same ones
|
|
2155
|
+
_throwif("the snapshot shares memories between imports differently",
|
|
2156
|
+
firstIndex != Memory_FirstIndex(module, m));
|
|
2157
|
+
if (firstIndex != m) {
|
|
2158
|
+
continue;
|
|
2159
|
+
}
|
|
2160
|
+
|
|
2161
|
+
_ (GetLEB_u32(l, &pageBits));
|
|
2162
|
+
_ (GetLEB_u32(l, &numPages));
|
|
2163
|
+
|
|
2164
|
+
memory = module->memories[memIndex];
|
|
2165
|
+
_throwif("a shared memory cannot be restored from a snapshot", memory and memory->isShared);
|
|
2166
|
+
|
|
2167
|
+
size_t bytes = 0;
|
|
2168
|
+
u8* data = NULL;
|
|
2169
|
+
|
|
2170
|
+
if (memory) {
|
|
2171
|
+
_throwif(m3Err_wasmMalformed, pageBits >= 32 or (1u << pageBits) != Memory_PageSize(memory));
|
|
2172
|
+
_throwif(m3Err_wasmMalformed, numPages < memory->numPages or numPages > memory->maxPages);
|
|
2173
|
+
_throwif(m3Err_wasmMalformed, numPages > SIZE_MAX / Memory_PageSize(memory));
|
|
2174
|
+
bytes = (size_t)numPages * Memory_PageSize(memory);
|
|
2175
|
+
|
|
2176
|
+
_throwif("shrunken memory cannot be restored from a snapshot",
|
|
2177
|
+
l->runtime->memoryLimit and bytes > (size_t)l->runtime->memoryLimit);
|
|
2178
|
+
|
|
2179
|
+
if (not l->checking) {
|
|
2180
|
+
if (memory->numPages < numPages) {
|
|
2181
|
+
_ (ResizeMemory(l->runtime, memory, numPages));
|
|
2182
|
+
}
|
|
2183
|
+
_throwif("shrunken memory cannot be restored from a snapshot",
|
|
2184
|
+
memory->mallocated and memory->mallocated->length < bytes);
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
l->loadedMemoryBytes += bytes;
|
|
2188
|
+
|
|
2189
|
+
if (bytes and memory->mallocated) {
|
|
2190
|
+
data = m3MemData(memory->mallocated);
|
|
2191
|
+
}
|
|
2192
|
+
} else {
|
|
2193
|
+
_throwif(m3Err_wasmMalformed, numPages != 0);
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
// every memory carries a chunk stream, an unbacked one an empty stream,
|
|
2197
|
+
// so a memory this runtime does not hold still has to be read past
|
|
2198
|
+
if (data and not l->checking) {
|
|
2199
|
+
memset(data, 0, bytes);
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
_ (LoadMemoryChunks(l, data, bytes));
|
|
2203
|
+
}
|
|
2204
|
+
_throwif(m3Err_wasmMalformed, l->loadedMemoryBytes != l->requiredMemoryBytes);
|
|
2205
|
+
|
|
2206
|
+
_catch:
|
|
2207
|
+
return result;
|
|
2208
|
+
}
|
|
2209
|
+
|
|
2210
|
+
static
|
|
2211
|
+
M3Result LoadGlobals (M3SnapshotLoad* l)
|
|
2212
|
+
{
|
|
2213
|
+
M3Result result = m3Err_none;
|
|
2214
|
+
IM3Module module = l->module;
|
|
2215
|
+
u32 numGlobals = 0;
|
|
2216
|
+
|
|
2217
|
+
_ (GetLEB_u32(l, &numGlobals));
|
|
2218
|
+
_throwif(m3Err_wasmMalformed, numGlobals == 0 or numGlobals != module->numGlobals);
|
|
2219
|
+
|
|
2220
|
+
for (u32 g = 0; g < numGlobals; ++g) {
|
|
2221
|
+
u32 globalIndex = 0;
|
|
2222
|
+
M3Global* global;
|
|
2223
|
+
M3Global* cell;
|
|
2224
|
+
|
|
2225
|
+
_ (GetLEB_u32(l, &globalIndex));
|
|
2226
|
+
_throwif(m3Err_wasmMalformed, globalIndex != g);
|
|
2227
|
+
|
|
2228
|
+
global = &module->globals[globalIndex];
|
|
2229
|
+
cell = global->resolved ? global->resolved : global;
|
|
2230
|
+
|
|
2231
|
+
_ (GetValue(l, global->type, &cell->i64Value));
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
_catch:
|
|
2235
|
+
return result;
|
|
2236
|
+
}
|
|
2237
|
+
|
|
2238
|
+
static
|
|
2239
|
+
M3Result LoadTables (M3SnapshotLoad* l)
|
|
2240
|
+
{
|
|
2241
|
+
M3Result result = m3Err_none;
|
|
2242
|
+
IM3Module module = l->module;
|
|
2243
|
+
u32 numTables = 0;
|
|
2244
|
+
l->loadedTableElements = 0;
|
|
2245
|
+
|
|
2246
|
+
_ (GetLEB_u32(l, &numTables));
|
|
2247
|
+
_throwif(m3Err_wasmMalformed, numTables == 0 or numTables != module->numTables);
|
|
2248
|
+
|
|
2249
|
+
for (u32 t = 0; t < numTables; ++t) {
|
|
2250
|
+
u32 tableIndex = 0, firstIndex = 0, size = 0;
|
|
2251
|
+
u8 valtype = 0;
|
|
2252
|
+
IM3Table table;
|
|
2253
|
+
|
|
2254
|
+
_ (GetLEB_u32(l, &tableIndex));
|
|
2255
|
+
_ (GetLEB_u32(l, &firstIndex));
|
|
2256
|
+
|
|
2257
|
+
_throwif(m3Err_wasmMalformed, tableIndex != t);
|
|
2258
|
+
_throwif("the snapshot shares tables between imports differently",
|
|
2259
|
+
firstIndex != Table_FirstIndex(module, t));
|
|
2260
|
+
if (firstIndex != t) {
|
|
2261
|
+
continue;
|
|
2262
|
+
}
|
|
2263
|
+
|
|
2264
|
+
_ (GetU8(l, &valtype));
|
|
2265
|
+
_ (GetLEB_u32(l, &size));
|
|
2266
|
+
|
|
2267
|
+
table = module->tables[tableIndex];
|
|
2268
|
+
|
|
2269
|
+
_throwif(m3Err_wasmMalformed, valtype != M3TypeToValType(table->type));
|
|
2270
|
+
_throwif(m3Err_wasmMalformed, size > (table->hasMax ? table->maxSize : d_m3MaxSaneTableSize));
|
|
2271
|
+
|
|
2272
|
+
// the table can only have grown since it was instantiated
|
|
2273
|
+
_throwif(m3Err_wasmMalformed, size < table->size);
|
|
2274
|
+
|
|
2275
|
+
l->loadedTableElements += size;
|
|
2276
|
+
|
|
2277
|
+
if (not l->checking and size != table->size) {
|
|
2278
|
+
void** elements;
|
|
2279
|
+
|
|
2280
|
+
_throwif(m3Err_tableLimitExceeded, l->runtime->tableElementsLimit and
|
|
2281
|
+
size - table->size > l->runtime->tableElementsLimit - l->runtime->tableElementsUsed);
|
|
2282
|
+
elements = m3_ReallocArray(void*, table->elements, size, table->size);
|
|
2283
|
+
_throwifnull(elements);
|
|
2284
|
+
|
|
2285
|
+
l->runtime->tableElementsUsed += size - table->size;
|
|
2286
|
+
table->elements = elements;
|
|
2287
|
+
table->size = size;
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
for (u32 e = 0; e < size; ++e) {
|
|
2291
|
+
void* reference = NULL;
|
|
2292
|
+
_ (GetRefValue(l, table->type, &reference));
|
|
2293
|
+
if (not l->checking) {
|
|
2294
|
+
table->elements[e] = reference;
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
_throwif(m3Err_wasmMalformed, l->loadedTableElements != l->requiredTableElements);
|
|
2299
|
+
|
|
2300
|
+
_catch:
|
|
2301
|
+
return result;
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2304
|
+
static
|
|
2305
|
+
M3Result LoadSegments (M3SnapshotLoad* l)
|
|
2306
|
+
{
|
|
2307
|
+
M3Result result = m3Err_none;
|
|
2308
|
+
IM3Module module = l->module;
|
|
2309
|
+
u32 count = 0;
|
|
2310
|
+
|
|
2311
|
+
_ (GetLEB_u32(l, &count));
|
|
2312
|
+
_throwif(m3Err_wasmMalformed, count != module->numDataSegments);
|
|
2313
|
+
_throwif(m3Err_wasmMalformed, module->numDataSegments == 0 and module->numElementSegments == 0);
|
|
2314
|
+
|
|
2315
|
+
for (u32 i = 0; i < count; ++i) {
|
|
2316
|
+
u8 dropped = 0;
|
|
2317
|
+
_ (GetBool(l, &dropped));
|
|
2318
|
+
if (not l->checking) {
|
|
2319
|
+
module->dataSegments[i].dropped = dropped;
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2323
|
+
_ (GetLEB_u32(l, &count));
|
|
2324
|
+
_throwif(m3Err_wasmMalformed, count != module->numElementSegments);
|
|
2325
|
+
|
|
2326
|
+
for (u32 i = 0; i < count; ++i) {
|
|
2327
|
+
u8 dropped = 0;
|
|
2328
|
+
_ (GetBool(l, &dropped));
|
|
2329
|
+
if (not l->checking) {
|
|
2330
|
+
module->elementSegments[i].dropped = dropped;
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
_catch:
|
|
2335
|
+
return result;
|
|
2336
|
+
}
|
|
2337
|
+
|
|
2338
|
+
// A frame for the continuation being put together, outermost first
|
|
2339
|
+
static
|
|
2340
|
+
M3Result AddFrame (M3SnapshotLoad* l, u32* io_numFrames, M3FrameKind i_kind, m3stack_t i_sp, M3Frame** o_frame)
|
|
2341
|
+
{
|
|
2342
|
+
if (*io_numFrames >= d_m3ContinuationMaxFrames) {
|
|
2343
|
+
return "the snapshot's frames do not fit this build's";
|
|
2344
|
+
}
|
|
2345
|
+
|
|
2346
|
+
M3Frame* frame = &l->frames[(*io_numFrames)++];
|
|
2347
|
+
|
|
2348
|
+
memset(frame, 0, sizeof(*frame));
|
|
2349
|
+
frame->kind = (u8)i_kind;
|
|
2350
|
+
frame->sp = i_sp;
|
|
2351
|
+
frame->memory = Module_Memory0(l->module);
|
|
2352
|
+
|
|
2353
|
+
*o_frame = frame;
|
|
2354
|
+
|
|
2355
|
+
return m3Err_none;
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
// Compiled, and with a snapshot map: where a function's frame in a snapshot is
|
|
2359
|
+
// found in this build's code
|
|
2360
|
+
static
|
|
2361
|
+
M3Result PrepareFunction (IM3Function i_function)
|
|
2362
|
+
{
|
|
2363
|
+
M3Result result = m3Err_none;
|
|
2364
|
+
|
|
2365
|
+
if (not i_function->compiled) {
|
|
2366
|
+
_ (CompileFunction(i_function));
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
_throwif("a function in the snapshot was compiled before the runtime was made suspendable", not i_function->snapshotMap);
|
|
2370
|
+
|
|
2371
|
+
_catch:
|
|
2372
|
+
return result;
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
// The native frames the interpreter keeps for the loops and try_tables around
|
|
2376
|
+
// a safepoint, outermost first. They are fixed by where the frame stands, so
|
|
2377
|
+
// the snapshot does not list them: they come from this build's own map.
|
|
2378
|
+
static
|
|
2379
|
+
M3Result AddBlockFrames (M3SnapshotLoad* l, const M3SnapshotMap* i_map, const M3SafePoint* i_point, m3stack_t i_sp,
|
|
2380
|
+
u32* io_numFrames)
|
|
2381
|
+
{
|
|
2382
|
+
M3Result result = m3Err_none;
|
|
2383
|
+
|
|
2384
|
+
for (u32 b = 0; b < i_map->numBlocks; ++b) {
|
|
2385
|
+
const M3BlockStart* block = &i_map->blocks[b];
|
|
2386
|
+
M3Frame* frame;
|
|
2387
|
+
|
|
2388
|
+
if (not EnclosesOffset(block, i_point->wasmOffset)) {
|
|
2389
|
+
continue;
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
if (block->opcode == c_waOp_loop) {
|
|
2393
|
+
_ (AddFrame(l, io_numFrames, frame_loop, i_sp, &frame));
|
|
2394
|
+
frame->pc = block->pc;
|
|
2395
|
+
}
|
|
2396
|
+
# if d_m3HasExceptionHandling
|
|
2397
|
+
else {
|
|
2398
|
+
_ (AddFrame(l, io_numFrames, frame_try, i_sp, &frame));
|
|
2399
|
+
frame->pc = block->pc;
|
|
2400
|
+
frame->try_.numClauses = block->numClauses;
|
|
2401
|
+
frame->try_.handlersLive = HandlersLiveAt(i_map, i_point, block);
|
|
2402
|
+
}
|
|
2403
|
+
# endif
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
_catch:
|
|
2407
|
+
return result;
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
// Puts a function's values where this build keeps them at the safepoint
|
|
2411
|
+
static
|
|
2412
|
+
M3Result LoadValues (M3SnapshotLoad* l, m3slot_t* i_base, u32 i_numSlots, m3stack_t i_sp,
|
|
2413
|
+
const M3SnapshotMap* i_map, const M3SafePoint* i_point, const M3FrameRegisters* i_registers)
|
|
2414
|
+
{
|
|
2415
|
+
M3Result result = m3Err_none;
|
|
2416
|
+
u32 numValues = 0;
|
|
2417
|
+
|
|
2418
|
+
_ (GetLEB_u32(l, &numValues));
|
|
2419
|
+
_throwif("the snapshot's frame does not match what this build compiled", numValues != i_map->numLocals + i_point->numValues);
|
|
2420
|
+
|
|
2421
|
+
for (u32 v = 0; v < numValues; ++v) {
|
|
2422
|
+
const M3SlotValue* value = FrameValue(i_map, i_point, v);
|
|
2423
|
+
u64 word = 0;
|
|
2424
|
+
void* reference = NULL;
|
|
2425
|
+
u8* where;
|
|
2426
|
+
|
|
2427
|
+
_ (LocateValue(i_base, i_numSlots, i_sp, value, &where));
|
|
2428
|
+
|
|
2429
|
+
// a v128 has no byte order of its own, and no bits path can carry it
|
|
2430
|
+
if (BaseTypeOf(value->type) == c_m3Type_v128) {
|
|
2431
|
+
m3type_t type;
|
|
2432
|
+
_ (GetValueTag(l, value->type, &type));
|
|
2433
|
+
if (l->checking) {
|
|
2434
|
+
_ (SkipBytes(l, 16));
|
|
2435
|
+
} else {
|
|
2436
|
+
_ (Get(l, where, 16));
|
|
2437
|
+
}
|
|
2438
|
+
continue;
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
l->allowUnfilledRef = i_point->kind == safepoint_suspend and
|
|
2442
|
+
v >= numValues - i_point->numResults;
|
|
2443
|
+
_ (GetValueBits(l, value->type, &word));
|
|
2444
|
+
l->allowUnfilledRef = false;
|
|
2445
|
+
|
|
2446
|
+
if (IsRefType(value->type)) {
|
|
2447
|
+
reference = (void*)(uintptr_t)word;
|
|
2448
|
+
}
|
|
2449
|
+
|
|
2450
|
+
if (not l->checking) {
|
|
2451
|
+
_ (WriteValue(where, i_registers, value, word, reference));
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
|
|
2455
|
+
_catch:
|
|
2456
|
+
return result;
|
|
2457
|
+
}
|
|
2458
|
+
|
|
2459
|
+
// The functions a suspended continuation is in the middle of, outermost first.
|
|
2460
|
+
// Each one's frame is found in this build's code, its values put where this
|
|
2461
|
+
// build keeps them, and the native frames the interpreter needs to stand it
|
|
2462
|
+
// back up are made from what its own maps say about them.
|
|
2463
|
+
static
|
|
2464
|
+
M3Result LoadSuspendedBody (M3SnapshotLoad* l, IM3Continuation io_cont, u32 i_id)
|
|
2465
|
+
{
|
|
2466
|
+
M3Result result = m3Err_none;
|
|
2467
|
+
u32 numFrames = 0;
|
|
2468
|
+
M3Frame* callFrame = NULL;
|
|
2469
|
+
IM3Function function = NULL;
|
|
2470
|
+
m3slot_t* base;
|
|
2471
|
+
u32 numSlots;
|
|
2472
|
+
u32 numActivations = 0;
|
|
2473
|
+
IM3FuncType expected = io_cont->entryFunction->funcType;
|
|
2474
|
+
m3stack_t sp;
|
|
2475
|
+
|
|
2476
|
+
_ (ContinuationStack(l->runtime, io_cont, &base, &numSlots));
|
|
2477
|
+
|
|
2478
|
+
_ (GetLEB_u32(l, &numActivations));
|
|
2479
|
+
_throwif(m3Err_wasmMalformed, numActivations == 0 or numActivations > d_m3ContinuationMaxFrames);
|
|
2480
|
+
|
|
2481
|
+
io_cont->pc = NULL;
|
|
2482
|
+
io_cont->r0 = 0;
|
|
2483
|
+
io_cont->numSuspendResults = 0;
|
|
2484
|
+
io_cont->suspendPoint = safepoint_op;
|
|
2485
|
+
# if d_m3HasFloat
|
|
2486
|
+
io_cont->fp0 = 0.;
|
|
2487
|
+
# endif
|
|
2488
|
+
|
|
2489
|
+
sp = base;
|
|
2490
|
+
|
|
2491
|
+
for (u32 a = 0; a < numActivations; ++a) {
|
|
2492
|
+
bool isInnermost = (a + 1 == numActivations);
|
|
2493
|
+
const M3SafePoint* point;
|
|
2494
|
+
M3FrameRegisters registers;
|
|
2495
|
+
u8 kind = 0;
|
|
2496
|
+
u32 wasmOffset = 0, targetLoop = 0;
|
|
2497
|
+
const M3SnapshotMap* map;
|
|
2498
|
+
|
|
2499
|
+
memset(®isters, 0, sizeof(registers));
|
|
2500
|
+
|
|
2501
|
+
_ (GetFunction(l, &function));
|
|
2502
|
+
|
|
2503
|
+
// A return_call hands its frame to the function it calls, so the
|
|
2504
|
+
// function standing here need not be the one the continuation was made
|
|
2505
|
+
// with, or the one the call outside it named. It does return what
|
|
2506
|
+
// they would have.
|
|
2507
|
+
_throwif("a frame in the snapshot does not return what its caller expects",
|
|
2508
|
+
not ReturnsWhat(function->funcType, expected));
|
|
2509
|
+
|
|
2510
|
+
_ (PrepareFunction(function));
|
|
2511
|
+
|
|
2512
|
+
map = function->snapshotMap;
|
|
2513
|
+
|
|
2514
|
+
_throwif("the snapshot's stack does not fit this runtime's",
|
|
2515
|
+
(u32)(sp - base) + function->maxStackSlots > numSlots);
|
|
2516
|
+
|
|
2517
|
+
// the call the frame outside this one is waiting on
|
|
2518
|
+
if (callFrame) {
|
|
2519
|
+
callFrame->call.function = function;
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
# if d_m3EntryKeepsFrame
|
|
2523
|
+
{
|
|
2524
|
+
M3Frame* entry;
|
|
2525
|
+
_ (AddFrame(l, &numFrames, frame_entry, sp, &entry));
|
|
2526
|
+
entry->pc = function->compiled;
|
|
2527
|
+
entry->entry.function = function;
|
|
2528
|
+
}
|
|
2529
|
+
# endif
|
|
2530
|
+
|
|
2531
|
+
_ (GetU8(l, &kind));
|
|
2532
|
+
_ (GetLEB_u32(l, &wasmOffset));
|
|
2533
|
+
|
|
2534
|
+
// A back edge says which loop it goes to. That has to be a loop around
|
|
2535
|
+
// the instruction; one the instruction does not branch to has no
|
|
2536
|
+
// safepoint to match.
|
|
2537
|
+
if (kind == safepoint_op) {
|
|
2538
|
+
const M3BlockStart* loop;
|
|
2539
|
+
|
|
2540
|
+
_ (GetLEB_u32(l, &targetLoop));
|
|
2541
|
+
|
|
2542
|
+
loop = FindBlockAt(map, c_waOp_loop, targetLoop);
|
|
2543
|
+
_throwif("a back edge in the snapshot does not go to a loop of its function", not loop);
|
|
2544
|
+
_throwif("a back edge in the snapshot goes to a loop that is not around it",
|
|
2545
|
+
not EnclosesOffset(loop, wasmOffset));
|
|
2546
|
+
}
|
|
2547
|
+
|
|
2548
|
+
point = FindSafePointAt(map, wasmOffset, kind, targetLoop);
|
|
2549
|
+
_throwif("a frame in the snapshot is not at a safepoint of this build's", not point);
|
|
2550
|
+
|
|
2551
|
+
_ (AddBlockFrames(l, map, point, sp, &numFrames));
|
|
2552
|
+
|
|
2553
|
+
if (not isInnermost) {
|
|
2554
|
+
_throwif(m3Err_wasmMalformed, kind != safepoint_call);
|
|
2555
|
+
|
|
2556
|
+
_ (AddFrame(l, &numFrames, frame_call, sp, &callFrame));
|
|
2557
|
+
callFrame->pc = point->pc;
|
|
2558
|
+
registers.r0 = &callFrame->call.r0;
|
|
2559
|
+
# if d_m3HasFloat
|
|
2560
|
+
registers.fp0 = &callFrame->call.fp0;
|
|
2561
|
+
# endif
|
|
2562
|
+
} else if (kind == safepoint_resume) {
|
|
2563
|
+
M3Frame* frame;
|
|
2564
|
+
u32 numResults = point->numResults;
|
|
2565
|
+
pc_t resultsPC = point->pc - 2 * numResults;
|
|
2566
|
+
|
|
2567
|
+
_ (AddFrame(l, &numFrames, frame_resume, sp, &frame));
|
|
2568
|
+
frame->pc = point->pc;
|
|
2569
|
+
frame->resume.resultsPC = resultsPC;
|
|
2570
|
+
frame->resume.numResults = numResults;
|
|
2571
|
+
frame->resume.numHandlers = point->aux;
|
|
2572
|
+
frame->resume.handlersPC = resultsPC - 1 - 3 * point->aux;
|
|
2573
|
+
|
|
2574
|
+
// a replayed resume carries on with clear registers, so the frame
|
|
2575
|
+
// keeps nothing in them
|
|
2576
|
+
callFrame = frame;
|
|
2577
|
+
} else {
|
|
2578
|
+
_throwif(m3Err_wasmMalformed, kind != safepoint_op and kind != safepoint_entry and kind != safepoint_suspend);
|
|
2579
|
+
|
|
2580
|
+
io_cont->pc = point->pc;
|
|
2581
|
+
io_cont->suspendPoint = kind;
|
|
2582
|
+
registers.r0 = &io_cont->r0;
|
|
2583
|
+
# if d_m3HasFloat
|
|
2584
|
+
registers.fp0 = &io_cont->fp0;
|
|
2585
|
+
# endif
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
_ (LoadValues(l, base, numSlots, sp, map, point, ®isters));
|
|
2589
|
+
|
|
2590
|
+
if (not l->checking) {
|
|
2591
|
+
RestoreConstants(function, sp);
|
|
2592
|
+
}
|
|
2593
|
+
|
|
2594
|
+
if (not isInnermost) {
|
|
2595
|
+
sp = sp + point->aux;
|
|
2596
|
+
_ (CallSiteType(function, point, &expected));
|
|
2597
|
+
} else if (kind == safepoint_resume) {
|
|
2598
|
+
void* cont = NULL;
|
|
2599
|
+
_ (GetRefValue(l, c_m3Type_contref, &cont));
|
|
2600
|
+
_throwif(m3Err_wasmMalformed, cont == NULL);
|
|
2601
|
+
|
|
2602
|
+
callFrame->resume.cont = (IM3Continuation)cont;
|
|
2603
|
+
if (not l->checking) {
|
|
2604
|
+
l->suspendSites[i_id].point = point;
|
|
2605
|
+
for (u32 i = 1; i < l->numContinuations; ++i) {
|
|
2606
|
+
if (l->continuations[i] == cont) {
|
|
2607
|
+
// a continuation runs under one resume at a time
|
|
2608
|
+
_throwif("a snapshot continuation is resumed by two frames", l->suspendSites[i].resumed);
|
|
2609
|
+
|
|
2610
|
+
l->suspendSites[i_id].target = i;
|
|
2611
|
+
l->suspendSites[i].resumed = true;
|
|
2612
|
+
break;
|
|
2613
|
+
}
|
|
2614
|
+
}
|
|
2615
|
+
}
|
|
2616
|
+
} else {
|
|
2617
|
+
// op_ContinueLoopIf suspends before it has branched, and goes on
|
|
2618
|
+
// by reading the condition again
|
|
2619
|
+
if (point->flags & d_m3SafePointTakenBranch) {
|
|
2620
|
+
io_cont->r0 = 1;
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
// a suspend waits for its results in the slots at the top of what
|
|
2624
|
+
// it holds, which is where whatever resumes it writes them
|
|
2625
|
+
if (kind == safepoint_suspend) {
|
|
2626
|
+
_throwif(m3Err_wasmMalformed, point->numResults > point->numValues or
|
|
2627
|
+
point->numResults > d_m3MaxContinuationPayload);
|
|
2628
|
+
|
|
2629
|
+
io_cont->numSuspendResults = point->numResults;
|
|
2630
|
+
if (not l->checking) {
|
|
2631
|
+
l->suspendSites[i_id].map = map;
|
|
2632
|
+
l->suspendSites[i_id].point = point;
|
|
2633
|
+
}
|
|
2634
|
+
|
|
2635
|
+
for (u32 i = 0; i < point->numResults; ++i) {
|
|
2636
|
+
const M3SlotValue* value = &map->values[point->firstValue + point->numValues - point->numResults + i];
|
|
2637
|
+
|
|
2638
|
+
_throwif(m3Err_wasmMalformed, IsRegisterSlotAlias(value->slot));
|
|
2639
|
+
|
|
2640
|
+
io_cont->suspendResultOffsets[i] = value->slot;
|
|
2641
|
+
io_cont->suspendResultIs64[i] = Is64BitType(value->type);
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2644
|
+
}
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
io_cont->sp = sp;
|
|
2648
|
+
if (l->checking) {
|
|
2649
|
+
return m3Err_none;
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
// held innermost first
|
|
2653
|
+
if (numFrames > io_cont->framesCap) {
|
|
2654
|
+
M3Frame* frames = m3_ReallocArray(M3Frame, io_cont->frames, numFrames, io_cont->framesCap);
|
|
2655
|
+
_throwifnull(frames);
|
|
2656
|
+
|
|
2657
|
+
io_cont->frames = frames;
|
|
2658
|
+
io_cont->framesCap = numFrames;
|
|
2659
|
+
}
|
|
2660
|
+
|
|
2661
|
+
for (u32 k = 0; k < numFrames; ++k) {
|
|
2662
|
+
io_cont->frames[numFrames - 1 - k] = l->frames[k];
|
|
2663
|
+
}
|
|
2664
|
+
|
|
2665
|
+
io_cont->numFrames = numFrames;
|
|
2666
|
+
|
|
2667
|
+
_catch:
|
|
2668
|
+
return result;
|
|
2669
|
+
}
|
|
2670
|
+
|
|
2671
|
+
static
|
|
2672
|
+
M3Result LoadContinuation (M3SnapshotLoad* l, u32 i_id)
|
|
2673
|
+
{
|
|
2674
|
+
M3Result result = m3Err_none;
|
|
2675
|
+
IM3Module module = l->module;
|
|
2676
|
+
IM3Continuation cont = l->continuations[i_id];
|
|
2677
|
+
M3Continuation scratch;
|
|
2678
|
+
if (l->checking) {
|
|
2679
|
+
scratch = *cont;
|
|
2680
|
+
scratch.valStack = l->checkStack;
|
|
2681
|
+
if (i_id == 0) {
|
|
2682
|
+
scratch.valStack = (m3slot_t*)l->runtime->originStack;
|
|
2683
|
+
}
|
|
2684
|
+
cont = &scratch;
|
|
2685
|
+
}
|
|
2686
|
+
|
|
2687
|
+
u32 contId = 0, typeIndex = 0, entryIndex = 0, boundArgsCount = 0;
|
|
2688
|
+
u8 state = 0, isRoot = 0;
|
|
2689
|
+
|
|
2690
|
+
_ (GetLEB_u32(l, &contId));
|
|
2691
|
+
_ (GetU8(l, &state));
|
|
2692
|
+
_ (GetBool(l, &isRoot));
|
|
2693
|
+
_ (GetLEB_u32(l, &typeIndex));
|
|
2694
|
+
_ (GetLEB_u32(l, &entryIndex));
|
|
2695
|
+
|
|
2696
|
+
_throwif(m3Err_wasmMalformed, contId != i_id);
|
|
2697
|
+
_throwif(m3Err_wasmMalformed, (bool)isRoot != (i_id == 0));
|
|
2698
|
+
_throwif(m3Err_wasmMalformed, isRoot and state != snapshot_contSuspended);
|
|
2699
|
+
if (not isRoot and (state == snapshot_contAllocated or state == snapshot_contSuspended)) {
|
|
2700
|
+
l->loadedStacks++;
|
|
2701
|
+
}
|
|
2702
|
+
_throwif(m3Err_wasmMalformed, typeIndex != d_m3SnapshotNone and typeIndex >= module->numFuncTypes);
|
|
2703
|
+
// every continuation, the root included, was made with a function
|
|
2704
|
+
_throwif(m3Err_wasmMalformed, entryIndex >= module->numFunctions);
|
|
2705
|
+
_throwif(m3Err_wasmMalformed, isRoot ? typeIndex != d_m3SnapshotNone : (typeIndex == d_m3SnapshotNone or not module->funcTypes[typeIndex]->isContinuation));
|
|
2706
|
+
|
|
2707
|
+
if (not l->checking and not isRoot and state != snapshot_contFinished) {
|
|
2708
|
+
_ (Continuation_AcquireStack(l->runtime, cont));
|
|
2709
|
+
}
|
|
2710
|
+
cont->type = (typeIndex != d_m3SnapshotNone) ? module->funcTypes[typeIndex] : NULL;
|
|
2711
|
+
cont->entryFunction = &module->functions[entryIndex];
|
|
2712
|
+
cont->numHandlers = 0;
|
|
2713
|
+
cont->handlersPC = NULL;
|
|
2714
|
+
cont->parent = NULL;
|
|
2715
|
+
cont->numFrames = 0;
|
|
2716
|
+
|
|
2717
|
+
# if d_m3HasExceptionHandling
|
|
2718
|
+
{
|
|
2719
|
+
void* ref = NULL;
|
|
2720
|
+
_ (GetRefValue(l, c_m3Type_exnref, &ref));
|
|
2721
|
+
cont->resumeThrow = (M3Exception*)ref;
|
|
2722
|
+
}
|
|
2723
|
+
// the root was not resumed by anything, and a finished continuation has
|
|
2724
|
+
// nowhere left to raise an exception
|
|
2725
|
+
_throwif(m3Err_wasmMalformed, (isRoot or state == snapshot_contFinished) and cont->resumeThrow != NULL);
|
|
2726
|
+
# else
|
|
2727
|
+
{
|
|
2728
|
+
void* ref = NULL;
|
|
2729
|
+
_ (GetRefValue(l, c_m3Type_exnref, &ref));
|
|
2730
|
+
_throwif(m3Err_wasmMalformed, ref != NULL);
|
|
2731
|
+
}
|
|
2732
|
+
# endif
|
|
2733
|
+
|
|
2734
|
+
if (state == snapshot_contAllocated) {
|
|
2735
|
+
IM3FuncType inner;
|
|
2736
|
+
u16 numRets;
|
|
2737
|
+
|
|
2738
|
+
_ (GetLEB_u32(l, &boundArgsCount));
|
|
2739
|
+
cont->boundArgsCount = boundArgsCount;
|
|
2740
|
+
inner = cont->entryFunction->funcType;
|
|
2741
|
+
|
|
2742
|
+
_throwif(m3Err_wasmMalformed, boundArgsCount > GetFuncTypeNumParams(inner));
|
|
2743
|
+
|
|
2744
|
+
{
|
|
2745
|
+
IM3FuncType remaining = cont->type->contFuncType;
|
|
2746
|
+
_throwif(m3Err_wasmMalformed, not remaining or
|
|
2747
|
+
GetFuncTypeNumParams(remaining) != GetFuncTypeNumParams(inner) - boundArgsCount or
|
|
2748
|
+
GetFuncTypeNumResults(remaining) != GetFuncTypeNumResults(inner));
|
|
2749
|
+
for (u16 i = 0; i < GetFuncTypeNumResults(inner); ++i) {
|
|
2750
|
+
_throwif(m3Err_wasmMalformed, GetFuncTypeResultType(inner, i) != GetFuncTypeResultType(remaining, i));
|
|
2751
|
+
}
|
|
2752
|
+
for (u16 i = 0; i < GetFuncTypeNumParams(remaining); ++i) {
|
|
2753
|
+
_throwif(m3Err_wasmMalformed, GetFuncTypeParamType(inner, (u16)(boundArgsCount + i)) != GetFuncTypeParamType(remaining, i));
|
|
2754
|
+
}
|
|
2755
|
+
}
|
|
2756
|
+
|
|
2757
|
+
numRets = GetFuncTypeNumResults(inner);
|
|
2758
|
+
|
|
2759
|
+
_throwif(m3Err_wasmMalformed, (numRets + boundArgsCount) * c_ioSlotCount > cont->numStackSlots);
|
|
2760
|
+
|
|
2761
|
+
for (u32 i = 0; i < boundArgsCount; ++i) {
|
|
2762
|
+
_ (GetValue(l, GetFuncTypeParamType(inner, (u16)i), cont->valStack + (numRets + i) * c_ioSlotCount));
|
|
2763
|
+
}
|
|
2764
|
+
|
|
2765
|
+
cont->state = cont_allocated;
|
|
2766
|
+
cont->sp = cont->valStack;
|
|
2767
|
+
cont->pc = cont->entryFunction->compiled;
|
|
2768
|
+
} else if (state == snapshot_contSuspended) {
|
|
2769
|
+
_ (GetLEB_u32(l, &boundArgsCount));
|
|
2770
|
+
_throwif(m3Err_wasmMalformed, isRoot and boundArgsCount != 0);
|
|
2771
|
+
cont->boundArgsCount = boundArgsCount;
|
|
2772
|
+
|
|
2773
|
+
_ (LoadSuspendedBody(l, cont, i_id));
|
|
2774
|
+
|
|
2775
|
+
cont->state = cont_suspended;
|
|
2776
|
+
} else if (state == snapshot_contFinished) {
|
|
2777
|
+
_ (GetLEB_u32(l, &boundArgsCount));
|
|
2778
|
+
_throwif(m3Err_wasmMalformed, boundArgsCount != 0);
|
|
2779
|
+
cont->boundArgsCount = 0;
|
|
2780
|
+
cont->state = cont_consumed;
|
|
2781
|
+
} else {
|
|
2782
|
+
_throw(m3Err_wasmMalformed);
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
_catch:
|
|
2786
|
+
if (result) {
|
|
2787
|
+
cont->numFrames = 0;
|
|
2788
|
+
}
|
|
2789
|
+
|
|
2790
|
+
return result;
|
|
2791
|
+
}
|
|
2792
|
+
|
|
2793
|
+
static
|
|
2794
|
+
M3Result LoadContinuations (M3SnapshotLoad* l)
|
|
2795
|
+
{
|
|
2796
|
+
M3Result result = m3Err_none;
|
|
2797
|
+
u32 count = 0;
|
|
2798
|
+
l->loadedStacks = 0;
|
|
2799
|
+
|
|
2800
|
+
_ (GetLEB_u32(l, &count));
|
|
2801
|
+
_throwif(m3Err_wasmMalformed, count == 0 or count != l->numContinuations);
|
|
2802
|
+
|
|
2803
|
+
for (u32 i = 0; i < count; ++i) {
|
|
2804
|
+
_ (LoadContinuation(l, i));
|
|
2805
|
+
}
|
|
2806
|
+
_throwif(m3Err_wasmMalformed, l->loadedStacks != l->requiredStacks);
|
|
2807
|
+
|
|
2808
|
+
_catch:
|
|
2809
|
+
return result;
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
// Cross-record constraints cannot be checked while reading a reference: both
|
|
2813
|
+
// its target and a nested suspension point may occur later in the stream.
|
|
2814
|
+
static
|
|
2815
|
+
M3Result CheckContinuationReferences (M3SnapshotLoad* l)
|
|
2816
|
+
{
|
|
2817
|
+
M3Result result = m3Err_none;
|
|
2818
|
+
|
|
2819
|
+
for (u32 id = 0; id < l->numContinuations; ++id) {
|
|
2820
|
+
IM3Continuation cont = l->continuations[id];
|
|
2821
|
+
u32 leaf = id;
|
|
2822
|
+
u32 depth = 0;
|
|
2823
|
+
# if d_m3HasTypedRefs
|
|
2824
|
+
m3type_t expectedType = l->suspendSites[id].expectedType;
|
|
2825
|
+
_throwif("a snapshot continuation reference has the wrong type", expectedType and
|
|
2826
|
+
(not cont->type or not IsSubTypeOf(RefTypeOfFuncType(cont->type, true), expectedType)));
|
|
2827
|
+
# endif
|
|
2828
|
+
if (l->suspendSites[id].target) {
|
|
2829
|
+
// The child may have switched to a different parameter signature
|
|
2830
|
+
// after this resume started. Its results must still fit this site.
|
|
2831
|
+
IM3FuncType expected = Module_ContTypeOfRef(l->module, l->suspendSites[id].point->resumeType);
|
|
2832
|
+
IM3Continuation child = l->continuations[l->suspendSites[id].target];
|
|
2833
|
+
IM3FuncType actual = child->entryFunction ? child->entryFunction->funcType : NULL;
|
|
2834
|
+
_throwif(m3Err_wasmMalformed, not expected or not actual);
|
|
2835
|
+
expected = expected->contFuncType;
|
|
2836
|
+
_throwif(m3Err_wasmMalformed, GetFuncTypeNumResults(actual) != GetFuncTypeNumResults(expected));
|
|
2837
|
+
for (u16 i = 0; i < GetFuncTypeNumResults(expected); ++i) {
|
|
2838
|
+
_throwif(m3Err_wasmMalformed, GetFuncTypeResultType(actual, i) != GetFuncTypeResultType(expected, i));
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
while (l->suspendSites[leaf].target) {
|
|
2842
|
+
leaf = l->suspendSites[leaf].target;
|
|
2843
|
+
_throwif(m3Err_wasmMalformed, ++depth >= l->numContinuations or
|
|
2844
|
+
l->continuations[leaf]->state != cont_suspended);
|
|
2845
|
+
}
|
|
2846
|
+
if (cont->state != cont_suspended or id == 0) {
|
|
2847
|
+
continue;
|
|
2848
|
+
}
|
|
2849
|
+
|
|
2850
|
+
// A continuation currently executing under a saved resume already
|
|
2851
|
+
// received its arguments. Only an independently captured suspension
|
|
2852
|
+
// (possibly containing nested resumes) still expects the tag's results.
|
|
2853
|
+
if (l->suspendSites[id].resumed) {
|
|
2854
|
+
_throwif(m3Err_wasmMalformed, cont->boundArgsCount != 0);
|
|
2855
|
+
continue;
|
|
2856
|
+
}
|
|
2857
|
+
|
|
2858
|
+
const M3SnapshotSuspendSite* site = &l->suspendSites[leaf];
|
|
2859
|
+
IM3Continuation inner = l->continuations[leaf];
|
|
2860
|
+
IM3FuncType remaining = cont->type->contFuncType;
|
|
2861
|
+
_throwif("a captured continuation has no suspension point", not site->point or not remaining);
|
|
2862
|
+
_throwif("a captured continuation has the wrong binding count", cont->boundArgsCount > inner->numSuspendResults or
|
|
2863
|
+
GetFuncTypeNumParams(remaining) != inner->numSuspendResults - cont->boundArgsCount);
|
|
2864
|
+
_throwif(m3Err_wasmMalformed,
|
|
2865
|
+
GetFuncTypeNumResults(remaining) != GetFuncTypeNumResults(cont->entryFunction->funcType));
|
|
2866
|
+
for (u16 i = 0; i < GetFuncTypeNumResults(remaining); ++i) {
|
|
2867
|
+
_throwif(m3Err_wasmMalformed, GetFuncTypeResultType(remaining, i) !=
|
|
2868
|
+
GetFuncTypeResultType(cont->entryFunction->funcType, i));
|
|
2869
|
+
}
|
|
2870
|
+
|
|
2871
|
+
for (u32 i = 0; i < inner->numSuspendResults; ++i) {
|
|
2872
|
+
const M3SlotValue* value = &site->map->values[site->point->firstValue +
|
|
2873
|
+
site->point->numValues - site->point->numResults + i];
|
|
2874
|
+
if (i < cont->boundArgsCount) {
|
|
2875
|
+
if (IsRefType(value->type) and not IsNullableRef(value->type)) {
|
|
2876
|
+
void* ref = NULL;
|
|
2877
|
+
memcpy(&ref, inner->sp + value->slot, sizeof(ref));
|
|
2878
|
+
_throwif(m3Err_wasmMalformed, not ref);
|
|
2879
|
+
}
|
|
2880
|
+
} else {
|
|
2881
|
+
_throwif("a captured continuation has the wrong parameter type", value->type !=
|
|
2882
|
+
GetFuncTypeParamType(remaining, (u16)(i - cont->boundArgsCount)));
|
|
2883
|
+
}
|
|
2884
|
+
}
|
|
2885
|
+
}
|
|
2886
|
+
|
|
2887
|
+
_catch:
|
|
2888
|
+
return result;
|
|
2889
|
+
}
|
|
2890
|
+
|
|
2891
|
+
# if d_m3HasExceptionHandling
|
|
2892
|
+
|
|
2893
|
+
static
|
|
2894
|
+
M3Result LoadExceptions (M3SnapshotLoad* l)
|
|
2895
|
+
{
|
|
2896
|
+
M3Result result = m3Err_none;
|
|
2897
|
+
u32 numExceptions = 0;
|
|
2898
|
+
|
|
2899
|
+
_ (GetLEB_u32(l, &numExceptions));
|
|
2900
|
+
_throwif(m3Err_wasmMalformed, numExceptions == 0 or numExceptions != l->numExceptions);
|
|
2901
|
+
|
|
2902
|
+
for (u32 i = 0; i < numExceptions; ++i) {
|
|
2903
|
+
u32 exnId = 0, numArgs = 0;
|
|
2904
|
+
M3Exception* exn;
|
|
2905
|
+
IM3FuncType type;
|
|
2906
|
+
|
|
2907
|
+
_ (GetLEB_u32(l, &exnId));
|
|
2908
|
+
_ (GetLEB_u32(l, &numArgs));
|
|
2909
|
+
|
|
2910
|
+
_throwif(m3Err_wasmMalformed, exnId != i);
|
|
2911
|
+
|
|
2912
|
+
exn = l->exceptions[i];
|
|
2913
|
+
type = exn->tag->type;
|
|
2914
|
+
|
|
2915
|
+
_throwif(m3Err_wasmMalformed, numArgs != exn->numArgs);
|
|
2916
|
+
|
|
2917
|
+
for (u32 a = 0; a < numArgs; ++a) {
|
|
2918
|
+
m3type_t argType = GetFuncTypeParamType(type, (u16)a);
|
|
2919
|
+
u64 bits = 0;
|
|
2920
|
+
_ (GetValueBits(l, argType, &bits));
|
|
2921
|
+
if (not l->checking) {
|
|
2922
|
+
exn->args[a] = bits;
|
|
2923
|
+
}
|
|
2924
|
+
}
|
|
2925
|
+
}
|
|
2926
|
+
|
|
2927
|
+
_catch:
|
|
2928
|
+
return result;
|
|
2929
|
+
}
|
|
2930
|
+
|
|
2931
|
+
# endif
|
|
2932
|
+
|
|
2933
|
+
// Reads the embedder's state through a reader that stops where it ends
|
|
2934
|
+
typedef struct M3BoundedReader {
|
|
2935
|
+
M3SnapshotLoad* load;
|
|
2936
|
+
u64 remaining;
|
|
2937
|
+
M3Result error;
|
|
2938
|
+
} M3BoundedReader;
|
|
2939
|
+
|
|
2940
|
+
static
|
|
2941
|
+
M3Result BoundedReader_Read (void* o_buffer, size_t i_size, void* i_userdata)
|
|
2942
|
+
{
|
|
2943
|
+
M3BoundedReader* br = (M3BoundedReader*)i_userdata;
|
|
2944
|
+
|
|
2945
|
+
if (i_size > br->remaining) {
|
|
2946
|
+
br->error = "the host read past the end of its state";
|
|
2947
|
+
return br->error;
|
|
2948
|
+
}
|
|
2949
|
+
|
|
2950
|
+
br->remaining -= i_size;
|
|
2951
|
+
|
|
2952
|
+
return Get(br->load, o_buffer, i_size);
|
|
2953
|
+
}
|
|
2954
|
+
|
|
2955
|
+
static
|
|
2956
|
+
M3Result LoadHostState (M3SnapshotLoad* l)
|
|
2957
|
+
{
|
|
2958
|
+
M3Result result = m3Err_none;
|
|
2959
|
+
const M3SnapshotHooks* hooks = &l->runtime->snapshotHooks;
|
|
2960
|
+
M3BoundedReader reader;
|
|
2961
|
+
size_t size = (size_t)(l->end - l->pos); // the whole section, which is never empty
|
|
2962
|
+
|
|
2963
|
+
_throwif("the snapshot carries host state, and nothing here restores it", not hooks->loadHostState);
|
|
2964
|
+
|
|
2965
|
+
if (l->checking) {
|
|
2966
|
+
return SkipBytes(l, size);
|
|
2967
|
+
}
|
|
2968
|
+
reader.load = l;
|
|
2969
|
+
reader.remaining = size;
|
|
2970
|
+
reader.error = m3Err_none;
|
|
2971
|
+
|
|
2972
|
+
_ (hooks->loadHostState(hooks->userdata, BoundedReader_Read, &reader, size));
|
|
2973
|
+
_ (reader.error);
|
|
2974
|
+
_throwif("the host did not read all of its state", reader.remaining != 0);
|
|
2975
|
+
|
|
2976
|
+
_catch:
|
|
2977
|
+
return result;
|
|
2978
|
+
}
|
|
2979
|
+
|
|
2980
|
+
static
|
|
2981
|
+
M3Result LoadHeader (M3SnapshotLoad* l)
|
|
2982
|
+
{
|
|
2983
|
+
M3Result result = m3Err_none;
|
|
2984
|
+
u8 magic[sizeof(c_snapshotMagic)];
|
|
2985
|
+
u32 version = 0;
|
|
2986
|
+
|
|
2987
|
+
_ (Get(l, magic, sizeof(magic)));
|
|
2988
|
+
_throwif(m3Err_wasmMalformed, memcmp(magic, c_snapshotMagic, sizeof(magic)) != 0);
|
|
2989
|
+
|
|
2990
|
+
_ (GetLE32(l, &version));
|
|
2991
|
+
_throwif(m3Err_wasmMalformed, version != c_snapshotVersion);
|
|
2992
|
+
|
|
2993
|
+
_catch:
|
|
2994
|
+
return result;
|
|
2995
|
+
}
|
|
2996
|
+
|
|
2997
|
+
static
|
|
2998
|
+
M3Result LoadMeta (M3SnapshotLoad* l, u32* o_flags)
|
|
2999
|
+
{
|
|
3000
|
+
M3Result result = m3Err_none;
|
|
3001
|
+
u64 timestamp = 0;
|
|
3002
|
+
u64 moduleHash = 0;
|
|
3003
|
+
u64 ownHash = 0;
|
|
3004
|
+
u64 memoryBytes = 0, tableElements = 0;
|
|
3005
|
+
u32 activeStacks = 0;
|
|
3006
|
+
u64 currentMemory, currentTables;
|
|
3007
|
+
SnapshotResourceTotals(l->module, ¤tMemory, ¤tTables);
|
|
3008
|
+
|
|
3009
|
+
_ (GetLEB_u32(l, o_flags));
|
|
3010
|
+
_ (GetLEB_u64(l, ×tamp));
|
|
3011
|
+
_ (GetLEB_u64(l, &moduleHash));
|
|
3012
|
+
|
|
3013
|
+
(void)timestamp;
|
|
3014
|
+
|
|
3015
|
+
_ (ModuleFingerprint(l->module, &ownHash));
|
|
3016
|
+
_throwif("the snapshot was saved from a different module", moduleHash != ownHash);
|
|
3017
|
+
|
|
3018
|
+
// a flag this build does not know changes what the rest of the file means
|
|
3019
|
+
_throwif("the snapshot sets a flag this build does not know", *o_flags & ~(u32)d_m3SnapshotFlagPostmortem);
|
|
3020
|
+
|
|
3021
|
+
_ (GetLEB_u32(l, &l->numContinuations));
|
|
3022
|
+
_ (GetLEB_u32(l, &l->numExceptions));
|
|
3023
|
+
_ (GetLEB_u64(l, &memoryBytes));
|
|
3024
|
+
_ (GetLEB_u64(l, &tableElements));
|
|
3025
|
+
_ (GetLEB_u32(l, &activeStacks));
|
|
3026
|
+
l->requiredStacks = activeStacks;
|
|
3027
|
+
l->requiredMemoryBytes = memoryBytes;
|
|
3028
|
+
l->requiredTableElements = tableElements;
|
|
3029
|
+
_throwif(m3Err_wasmMalformed, memoryBytes < currentMemory or tableElements < currentTables);
|
|
3030
|
+
_throwif(m3Err_wasmMalformed, not l->module->numMemories and memoryBytes != 0);
|
|
3031
|
+
_throwif(m3Err_wasmMalformed, not l->module->numTables and tableElements != 0);
|
|
3032
|
+
|
|
3033
|
+
// the rest of Meta means the same thing in a postmortem, so it is read
|
|
3034
|
+
// whole and the refusal to resume comes after the section, not inside it
|
|
3035
|
+
if (*o_flags & d_m3SnapshotFlagPostmortem) {
|
|
3036
|
+
for (u32 i = 0; i < l->numExceptions; ++i) {
|
|
3037
|
+
u32 tagIndex = 0;
|
|
3038
|
+
_ (GetLEB_u32(l, &tagIndex));
|
|
3039
|
+
}
|
|
3040
|
+
return m3Err_none;
|
|
3041
|
+
}
|
|
3042
|
+
|
|
3043
|
+
_throwif(m3Err_memoryLimitExceeded, l->runtime->memoryBytesLimit and
|
|
3044
|
+
memoryBytes - currentMemory > l->runtime->memoryBytesLimit - l->runtime->memoryBytesUsed);
|
|
3045
|
+
_throwif(m3Err_tableLimitExceeded, l->runtime->tableElementsLimit and
|
|
3046
|
+
tableElements - currentTables > l->runtime->tableElementsLimit - l->runtime->tableElementsUsed);
|
|
3047
|
+
_throwif(m3Err_continuationLimitExceeded, l->runtime->continuationsLimit and
|
|
3048
|
+
activeStacks > l->runtime->continuationsLimit - l->runtime->continuationsAllocated);
|
|
3049
|
+
|
|
3050
|
+
_throwif(m3Err_wasmMalformed, l->numContinuations == 0 or activeStacks >= l->numContinuations);
|
|
3051
|
+
# if !d_m3HasExceptionHandling
|
|
3052
|
+
_throwif("the snapshot holds exceptions, and this build has none", l->numExceptions != 0);
|
|
3053
|
+
# endif
|
|
3054
|
+
|
|
3055
|
+
if (l->checking) {
|
|
3056
|
+
for (u32 i = 0; i < l->numExceptions; ++i) {
|
|
3057
|
+
u32 tagIndex = 0;
|
|
3058
|
+
_ (GetLEB_u32(l, &tagIndex));
|
|
3059
|
+
# if d_m3HasExceptionHandling
|
|
3060
|
+
_throwif(m3Err_wasmMalformed, tagIndex >= l->module->numTags);
|
|
3061
|
+
# endif
|
|
3062
|
+
}
|
|
3063
|
+
return m3Err_none;
|
|
3064
|
+
}
|
|
3065
|
+
|
|
3066
|
+
l->checkStack = m3_AllocArray(m3slot_t, d_m3ContinuationStackSlots + 4);
|
|
3067
|
+
_throwifnull(l->checkStack);
|
|
3068
|
+
l->frames = m3_AllocArray(M3Frame, d_m3ContinuationMaxFrames);
|
|
3069
|
+
_throwifnull(l->frames);
|
|
3070
|
+
|
|
3071
|
+
l->continuations = m3_AllocArray(IM3Continuation, l->numContinuations);
|
|
3072
|
+
_throwifnull(l->continuations);
|
|
3073
|
+
l->suspendSites = m3_AllocArray(M3SnapshotSuspendSite, l->numContinuations);
|
|
3074
|
+
_throwifnull(l->suspendSites);
|
|
3075
|
+
|
|
3076
|
+
if (not l->runtime->rootContinuation) {
|
|
3077
|
+
IM3Continuation root = Continuation_New(l->runtime, NULL, NULL);
|
|
3078
|
+
_throwifnull(root);
|
|
3079
|
+
|
|
3080
|
+
// the root borrows the runtime's stack, so it holds none of its own
|
|
3081
|
+
root->numStackSlots = l->runtime->numStackSlots;
|
|
3082
|
+
l->runtime->rootContinuation = root;
|
|
3083
|
+
}
|
|
3084
|
+
|
|
3085
|
+
l->continuations[0] = l->runtime->rootContinuation;
|
|
3086
|
+
|
|
3087
|
+
for (u32 i = 1; i < l->numContinuations; ++i) {
|
|
3088
|
+
l->continuations[i] = Continuation_New(l->runtime, NULL, NULL);
|
|
3089
|
+
_throwifnull(l->continuations[i]);
|
|
3090
|
+
}
|
|
3091
|
+
|
|
3092
|
+
# if d_m3HasExceptionHandling
|
|
3093
|
+
if (l->numExceptions) {
|
|
3094
|
+
l->exceptions = m3_AllocArray(M3Exception*, l->numExceptions);
|
|
3095
|
+
_throwifnull(l->exceptions);
|
|
3096
|
+
}
|
|
3097
|
+
|
|
3098
|
+
for (u32 i = 0; i < l->numExceptions; ++i) {
|
|
3099
|
+
u32 tagIndex = 0;
|
|
3100
|
+
IM3Tag tag;
|
|
3101
|
+
u32 numArgs;
|
|
3102
|
+
|
|
3103
|
+
_ (GetLEB_u32(l, &tagIndex));
|
|
3104
|
+
_throwif(m3Err_wasmMalformed, tagIndex >= l->module->numTags);
|
|
3105
|
+
|
|
3106
|
+
tag = TagOfIndex(l->module, tagIndex);
|
|
3107
|
+
numArgs = GetFuncTypeNumParams(tag->type);
|
|
3108
|
+
|
|
3109
|
+
l->exceptions[i] = NewException(l->runtime, tag, numArgs);
|
|
3110
|
+
_throwifnull(l->exceptions[i]);
|
|
3111
|
+
|
|
3112
|
+
l->exceptions[i]->reified = true;
|
|
3113
|
+
}
|
|
3114
|
+
# else
|
|
3115
|
+
for (u32 i = 0; i < l->numExceptions; ++i) {
|
|
3116
|
+
u32 tagIndex = 0;
|
|
3117
|
+
_ (GetLEB_u32(l, &tagIndex));
|
|
3118
|
+
}
|
|
3119
|
+
# endif
|
|
3120
|
+
|
|
3121
|
+
_catch:
|
|
3122
|
+
return result;
|
|
3123
|
+
}
|
|
3124
|
+
|
|
3125
|
+
// Each section is decoded once without changing guest state or calling the host,
|
|
3126
|
+
// then applied with the same decoder. This checks its complete structure before
|
|
3127
|
+
// any of its values or opaque host bytes are installed.
|
|
3128
|
+
static
|
|
3129
|
+
M3Result LoadSection (M3SnapshotLoad* l, u8 i_sectionId)
|
|
3130
|
+
{
|
|
3131
|
+
M3Result result = m3Err_none;
|
|
3132
|
+
u32 flags = 0;
|
|
3133
|
+
switch (i_sectionId) {
|
|
3134
|
+
case d_m3SnapshotSection_Meta:
|
|
3135
|
+
_ (LoadMeta(l, &flags));
|
|
3136
|
+
_throwif("postmortem snapshots cannot be resumed", flags & d_m3SnapshotFlagPostmortem);
|
|
3137
|
+
l->started = true;
|
|
3138
|
+
break;
|
|
3139
|
+
|
|
3140
|
+
case d_m3SnapshotSection_Memory:
|
|
3141
|
+
_ (LoadMemories(l));
|
|
3142
|
+
break;
|
|
3143
|
+
|
|
3144
|
+
case d_m3SnapshotSection_Table:
|
|
3145
|
+
_ (LoadTables(l));
|
|
3146
|
+
break;
|
|
3147
|
+
|
|
3148
|
+
case d_m3SnapshotSection_Global:
|
|
3149
|
+
_ (LoadGlobals(l));
|
|
3150
|
+
break;
|
|
3151
|
+
|
|
3152
|
+
case d_m3SnapshotSection_Segment:
|
|
3153
|
+
_ (LoadSegments(l));
|
|
3154
|
+
break;
|
|
3155
|
+
|
|
3156
|
+
# if d_m3HasExceptionHandling
|
|
3157
|
+
case d_m3SnapshotSection_Exception:
|
|
3158
|
+
_ (LoadExceptions(l));
|
|
3159
|
+
break;
|
|
3160
|
+
# endif
|
|
3161
|
+
|
|
3162
|
+
case d_m3SnapshotSection_Continuation:
|
|
3163
|
+
_ (LoadContinuations(l));
|
|
3164
|
+
break;
|
|
3165
|
+
|
|
3166
|
+
case d_m3SnapshotSection_HostState:
|
|
3167
|
+
_ (LoadHostState(l));
|
|
3168
|
+
break;
|
|
3169
|
+
|
|
3170
|
+
default:
|
|
3171
|
+
_ (SkipBytes(l, (size_t)(l->end - l->pos)));
|
|
3172
|
+
break;
|
|
3173
|
+
}
|
|
3174
|
+
|
|
3175
|
+
_catch:
|
|
3176
|
+
return result;
|
|
3177
|
+
}
|
|
3178
|
+
|
|
3179
|
+
static
|
|
3180
|
+
M3Result LoadSections (M3SnapshotLoad* l, bool i_embedded)
|
|
3181
|
+
{
|
|
3182
|
+
M3Result result = m3Err_none;
|
|
3183
|
+
u8* payload = NULL;
|
|
3184
|
+
|
|
3185
|
+
if (not i_embedded) {
|
|
3186
|
+
_ (LoadHeader(l));
|
|
3187
|
+
}
|
|
3188
|
+
|
|
3189
|
+
while (true) {
|
|
3190
|
+
u8 sectionId = 0;
|
|
3191
|
+
u32 sectionSize = 0;
|
|
3192
|
+
|
|
3193
|
+
// the last section ends the file, so the reader running out here is
|
|
3194
|
+
// the end of the snapshot rather than a truncated one
|
|
3195
|
+
if (GetU8(l, §ionId)) {
|
|
3196
|
+
break;
|
|
3197
|
+
}
|
|
3198
|
+
|
|
3199
|
+
_ (GetLEB_u32(l, §ionSize));
|
|
3200
|
+
|
|
3201
|
+
_throwif("the snapshot holds a section twice", l->seenSections[sectionId]);
|
|
3202
|
+
l->seenSections[sectionId] = true;
|
|
3203
|
+
_throwif("the snapshot holds an empty section", sectionSize == 0);
|
|
3204
|
+
|
|
3205
|
+
// the Meta section says what the runtime has to be made ready for, so
|
|
3206
|
+
// nothing may come before it
|
|
3207
|
+
_throwif(m3Err_wasmMalformed, not l->started and sectionId != d_m3SnapshotSection_Meta);
|
|
3208
|
+
|
|
3209
|
+
// Read the entire payload before letting a loader or host callback
|
|
3210
|
+
// touch the instance. Every body decoder is then bounded by this buffer.
|
|
3211
|
+
payload = (u8*)m3_Malloc("snapshot_section", sectionSize);
|
|
3212
|
+
_throwifnull(payload);
|
|
3213
|
+
_ (Get(l, payload, sectionSize));
|
|
3214
|
+
l->pos = payload;
|
|
3215
|
+
l->end = payload + sectionSize;
|
|
3216
|
+
l->inSection = true;
|
|
3217
|
+
|
|
3218
|
+
{
|
|
3219
|
+
M3SnapshotLoad check = *l;
|
|
3220
|
+
check.checking = true;
|
|
3221
|
+
_ (LoadSection(&check, sectionId));
|
|
3222
|
+
_throwif("a section in the snapshot does not match its declared length", check.pos != check.end);
|
|
3223
|
+
}
|
|
3224
|
+
l->changed = l->changed or sectionId != d_m3SnapshotSection_Meta;
|
|
3225
|
+
_ (LoadSection(l, sectionId));
|
|
3226
|
+
|
|
3227
|
+
// a section that reads past its own length has desynced the stream,
|
|
3228
|
+
// and one that stops short would leave the next section misread
|
|
3229
|
+
_throwif("a section in the snapshot does not match its declared length",
|
|
3230
|
+
l->pos != l->end);
|
|
3231
|
+
l->inSection = false;
|
|
3232
|
+
m3_Free(payload);
|
|
3233
|
+
}
|
|
3234
|
+
|
|
3235
|
+
_throwif("the snapshot has no meta section", not l->started);
|
|
3236
|
+
_throwif("the snapshot has no continuation section",
|
|
3237
|
+
not l->seenSections[d_m3SnapshotSection_Continuation]);
|
|
3238
|
+
_throwif("the snapshot names exceptions it does not hold",
|
|
3239
|
+
l->numExceptions and not l->seenSections[d_m3SnapshotSection_Exception]);
|
|
3240
|
+
|
|
3241
|
+
// a store section left out is not "unchanged": it would resume against
|
|
3242
|
+
// whatever instantiation happened to leave behind
|
|
3243
|
+
_throwif("the snapshot has no memory section",
|
|
3244
|
+
l->module->numMemories and not l->seenSections[d_m3SnapshotSection_Memory]);
|
|
3245
|
+
_throwif("the snapshot has no table section",
|
|
3246
|
+
l->module->numTables and not l->seenSections[d_m3SnapshotSection_Table]);
|
|
3247
|
+
_throwif("the snapshot has no global section",
|
|
3248
|
+
l->module->numGlobals and not l->seenSections[d_m3SnapshotSection_Global]);
|
|
3249
|
+
_throwif("the snapshot has no segment section",
|
|
3250
|
+
(l->module->numDataSegments or l->module->numElementSegments) and
|
|
3251
|
+
not l->seenSections[d_m3SnapshotSection_Segment]);
|
|
3252
|
+
|
|
3253
|
+
_ (CheckContinuationReferences(l));
|
|
3254
|
+
|
|
3255
|
+
_catch:
|
|
3256
|
+
m3_Free(payload);
|
|
3257
|
+
return result;
|
|
3258
|
+
}
|
|
3259
|
+
|
|
3260
|
+
#endif // d_m3HasSnapshots
|
|
3261
|
+
|
|
3262
|
+
|
|
3263
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
3264
|
+
// API
|
|
3265
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
3266
|
+
|
|
3267
|
+
#if d_m3HasSnapshots
|
|
3268
|
+
|
|
3269
|
+
static
|
|
3270
|
+
M3Result SaveSnapshot (IM3Runtime io_runtime, M3SnapshotWriter i_writer, void* i_userdata,
|
|
3271
|
+
IM3Module* o_module, bool i_embedded)
|
|
3272
|
+
{
|
|
3273
|
+
M3Result result = m3Err_none;
|
|
3274
|
+
M3SnapshotSave save;
|
|
3275
|
+
|
|
3276
|
+
memset(&save, 0, sizeof(save));
|
|
3277
|
+
|
|
3278
|
+
IM3Continuation root = io_runtime->rootContinuation;
|
|
3279
|
+
|
|
3280
|
+
// Anything but a paused invocation is saved as a postmortem: the state of
|
|
3281
|
+
// the module the last call entered, which needs no continuation and so no
|
|
3282
|
+
// suspendable runtime
|
|
3283
|
+
save.runtime = io_runtime;
|
|
3284
|
+
save.writer = i_writer;
|
|
3285
|
+
save.userdata = i_userdata;
|
|
3286
|
+
save.embedded = i_embedded;
|
|
3287
|
+
save.postmortem = not root or root->state != cont_suspended;
|
|
3288
|
+
|
|
3289
|
+
IM3Function entry = save.postmortem ? io_runtime->entered : root->entryFunction;
|
|
3290
|
+
|
|
3291
|
+
if (o_module) {
|
|
3292
|
+
*o_module = NULL;
|
|
3293
|
+
}
|
|
3294
|
+
|
|
3295
|
+
// no call has entered anything, so there is no module to describe
|
|
3296
|
+
_throwif("there is nothing to snapshot", not entry);
|
|
3297
|
+
|
|
3298
|
+
save.module = entry->module;
|
|
3299
|
+
|
|
3300
|
+
_throwif("a snapshot failed to restore into the module, so it cannot be saved", save.module->isUnusable);
|
|
3301
|
+
|
|
3302
|
+
if (o_module) {
|
|
3303
|
+
*o_module = save.module;
|
|
3304
|
+
}
|
|
3305
|
+
|
|
3306
|
+
for (IM3Continuation cont = io_runtime->continuations; cont; cont = cont->next) {
|
|
3307
|
+
u32 id;
|
|
3308
|
+
_ (PointerIds_Add(&save.liveContinuations, cont, &id));
|
|
3309
|
+
}
|
|
3310
|
+
|
|
3311
|
+
# if d_m3HasExceptionHandling
|
|
3312
|
+
for (M3Exception* exception = io_runtime->exceptions; exception; exception = exception->next) {
|
|
3313
|
+
u32 id;
|
|
3314
|
+
_ (PointerIds_Add(&save.liveExceptions, exception, &id));
|
|
3315
|
+
}
|
|
3316
|
+
# endif
|
|
3317
|
+
|
|
3318
|
+
if (not save.postmortem) {
|
|
3319
|
+
u32 id;
|
|
3320
|
+
_ (PointerIds_Add(&save.continuations, root, &id));
|
|
3321
|
+
}
|
|
3322
|
+
|
|
3323
|
+
_ (SaveSections(&save));
|
|
3324
|
+
|
|
3325
|
+
save.writing = true;
|
|
3326
|
+
|
|
3327
|
+
_ (SaveSections(&save));
|
|
3328
|
+
|
|
3329
|
+
_catch:
|
|
3330
|
+
PointerIds_Free(&save.liveContinuations);
|
|
3331
|
+
PointerIds_Free(&save.liveExceptions);
|
|
3332
|
+
PointerIds_Free(&save.continuations);
|
|
3333
|
+
PointerIds_Free(&save.exceptions);
|
|
3334
|
+
|
|
3335
|
+
return result;
|
|
3336
|
+
}
|
|
3337
|
+
|
|
3338
|
+
#endif
|
|
3339
|
+
|
|
3340
|
+
M3Result m3_SaveSnapshot (IM3Runtime io_runtime, M3SnapshotWriter i_writer, void* i_userdata)
|
|
3341
|
+
{
|
|
3342
|
+
if (not io_runtime or not i_writer) {
|
|
3343
|
+
return m3Err_mallocFailed;
|
|
3344
|
+
}
|
|
3345
|
+
|
|
3346
|
+
#if d_m3HasSnapshots
|
|
3347
|
+
return SaveSnapshot(io_runtime, i_writer, i_userdata, NULL, false);
|
|
3348
|
+
#else
|
|
3349
|
+
(void)i_userdata;
|
|
3350
|
+
return "snapshots are not available in this build of Wasm3";
|
|
3351
|
+
#endif
|
|
3352
|
+
}
|
|
3353
|
+
|
|
3354
|
+
|
|
3355
|
+
static
|
|
3356
|
+
M3Result LoadSnapshot (IM3Runtime io_runtime, IM3Module i_module, M3SnapshotReader i_reader, void* i_userdata, bool i_embedded)
|
|
3357
|
+
{
|
|
3358
|
+
M3Result result = m3Err_none;
|
|
3359
|
+
_throwifnull(io_runtime);
|
|
3360
|
+
_throwifnull(i_module);
|
|
3361
|
+
_throwifnull(i_reader);
|
|
3362
|
+
|
|
3363
|
+
#if d_m3HasSnapshots
|
|
3364
|
+
// A snapshot is the whole of the module's state, and is checked against
|
|
3365
|
+
// what instantiation made: memories and tables no smaller than they were
|
|
3366
|
+
// declared, and nothing the module did since to account for
|
|
3367
|
+
_throwif("a snapshot failed to restore into the module, so it cannot be used", i_module->isUnusable);
|
|
3368
|
+
_throwif("a snapshot restores only into a freshly instantiated module, and this one has run", i_module->hasRun);
|
|
3369
|
+
|
|
3370
|
+
{
|
|
3371
|
+
M3SnapshotLoad load;
|
|
3372
|
+
memset(&load, 0, sizeof(load));
|
|
3373
|
+
|
|
3374
|
+
load.runtime = io_runtime;
|
|
3375
|
+
load.module = i_module;
|
|
3376
|
+
load.reader = i_reader;
|
|
3377
|
+
load.userdata = i_userdata;
|
|
3378
|
+
|
|
3379
|
+
// Before anything here compiles a function: what comes out of a
|
|
3380
|
+
// snapshot has to be able to suspend again, and a body compiled while
|
|
3381
|
+
// this is still false has neither the back edges nor the maps for it
|
|
3382
|
+
io_runtime->isSuspendable = true;
|
|
3383
|
+
|
|
3384
|
+
result = LoadSections(&load, i_embedded);
|
|
3385
|
+
|
|
3386
|
+
IM3Continuation root = io_runtime->rootContinuation;
|
|
3387
|
+
|
|
3388
|
+
if (result) {
|
|
3389
|
+
if (load.changed) {
|
|
3390
|
+
// Part of the snapshot is in the module and part is not: a
|
|
3391
|
+
// state the program was never in. It is not resumed, run or
|
|
3392
|
+
// saved again; the embedder has to throw it away.
|
|
3393
|
+
i_module->isUnusable = true;
|
|
3394
|
+
|
|
3395
|
+
if (root) {
|
|
3396
|
+
root->state = cont_returned;
|
|
3397
|
+
root->numFrames = 0;
|
|
3398
|
+
}
|
|
3399
|
+
}
|
|
3400
|
+
} else {
|
|
3401
|
+
// A suspension is only ever reached with the start function run,
|
|
3402
|
+
// or under way in the invocation being restored, so the memories
|
|
3403
|
+
// and globals already hold what it did
|
|
3404
|
+
i_module->startFunction = -1;
|
|
3405
|
+
i_module->hasRun = true;
|
|
3406
|
+
}
|
|
3407
|
+
|
|
3408
|
+
Runtime_PlaceCallStack(io_runtime);
|
|
3409
|
+
|
|
3410
|
+
m3_Free(load.checkStack);
|
|
3411
|
+
m3_Free(load.frames);
|
|
3412
|
+
m3_Free(load.suspendSites);
|
|
3413
|
+
m3_Free(load.continuations);
|
|
3414
|
+
# if d_m3HasExceptionHandling
|
|
3415
|
+
m3_Free(load.exceptions);
|
|
3416
|
+
# endif
|
|
3417
|
+
}
|
|
3418
|
+
#else
|
|
3419
|
+
(void)i_userdata;
|
|
3420
|
+
_throw("snapshots are not available in this build of Wasm3");
|
|
3421
|
+
#endif
|
|
3422
|
+
|
|
3423
|
+
_catch:
|
|
3424
|
+
return result;
|
|
3425
|
+
}
|
|
3426
|
+
|
|
3427
|
+
|
|
3428
|
+
M3Result m3_LoadSnapshot (IM3Runtime io_runtime, IM3Module i_module, M3SnapshotReader i_reader, void* i_userdata)
|
|
3429
|
+
{
|
|
3430
|
+
return LoadSnapshot(io_runtime, i_module, i_reader, i_userdata, false);
|
|
3431
|
+
}
|
|
3432
|
+
|
|
3433
|
+
|
|
3434
|
+
void m3_SetSnapshotHooks (IM3Runtime io_runtime, const M3SnapshotHooks* i_hooks)
|
|
3435
|
+
{
|
|
3436
|
+
#if d_m3HasSnapshots
|
|
3437
|
+
if (io_runtime) {
|
|
3438
|
+
if (i_hooks) {
|
|
3439
|
+
io_runtime->snapshotHooks = *i_hooks;
|
|
3440
|
+
} else {
|
|
3441
|
+
memset(&io_runtime->snapshotHooks, 0, sizeof(io_runtime->snapshotHooks));
|
|
3442
|
+
}
|
|
3443
|
+
}
|
|
3444
|
+
#else
|
|
3445
|
+
(void)io_runtime;
|
|
3446
|
+
(void)i_hooks;
|
|
3447
|
+
#endif
|
|
3448
|
+
}
|
|
3449
|
+
|
|
3450
|
+
|
|
3451
|
+
M3Result m3_SaveSnapshotToBuffer (IM3Runtime io_runtime, void** o_bytes, size_t* o_size)
|
|
3452
|
+
{
|
|
3453
|
+
if (!io_runtime || !o_bytes || !o_size) {
|
|
3454
|
+
return m3Err_mallocFailed;
|
|
3455
|
+
}
|
|
3456
|
+
#if d_m3HasSnapshots
|
|
3457
|
+
M3BufferWriter bw = { NULL, 0, 0 };
|
|
3458
|
+
M3Result result = m3_SaveSnapshot(io_runtime, BufferWriter_Write, &bw);
|
|
3459
|
+
if (result) {
|
|
3460
|
+
m3_Free(bw.buffer);
|
|
3461
|
+
*o_bytes = NULL;
|
|
3462
|
+
*o_size = 0;
|
|
3463
|
+
return result;
|
|
3464
|
+
}
|
|
3465
|
+
*o_bytes = bw.buffer;
|
|
3466
|
+
*o_size = bw.size;
|
|
3467
|
+
return m3Err_none;
|
|
3468
|
+
#else
|
|
3469
|
+
*o_bytes = NULL;
|
|
3470
|
+
*o_size = 0;
|
|
3471
|
+
return "snapshots are not available in this build of Wasm3";
|
|
3472
|
+
#endif
|
|
3473
|
+
}
|
|
3474
|
+
|
|
3475
|
+
#if d_m3HasSnapshots
|
|
3476
|
+
|
|
3477
|
+
// Appends one LEB128-encoded u32 to a plain byte cursor
|
|
3478
|
+
static
|
|
3479
|
+
u8* EmitLEB_u32 (u8* o_where, u32 i_value)
|
|
3480
|
+
{
|
|
3481
|
+
do {
|
|
3482
|
+
u8 byte = i_value & 0x7F;
|
|
3483
|
+
i_value >>= 7;
|
|
3484
|
+
if (i_value != 0) {
|
|
3485
|
+
byte |= 0x80;
|
|
3486
|
+
}
|
|
3487
|
+
*o_where++ = byte;
|
|
3488
|
+
} while (i_value != 0);
|
|
3489
|
+
|
|
3490
|
+
return o_where;
|
|
3491
|
+
}
|
|
3492
|
+
|
|
3493
|
+
static
|
|
3494
|
+
u32 SizeOfLEB_u32 (u32 i_value)
|
|
3495
|
+
{
|
|
3496
|
+
u32 size = 0;
|
|
3497
|
+
|
|
3498
|
+
do {
|
|
3499
|
+
i_value >>= 7;
|
|
3500
|
+
++size;
|
|
3501
|
+
} while (i_value != 0);
|
|
3502
|
+
|
|
3503
|
+
return size;
|
|
3504
|
+
}
|
|
3505
|
+
|
|
3506
|
+
// Walks the custom sections of a Wasm binary and finds the one named
|
|
3507
|
+
// i_secName. o_secStart/o_secEnd frame the whole section, so it can be dropped
|
|
3508
|
+
// when a new one replaces it; o_data/o_size frame its snapshot payload. Any of
|
|
3509
|
+
// the four may be NULL. Not finding one is not an error: o_secStart and o_data
|
|
3510
|
+
// are left NULL.
|
|
3511
|
+
static
|
|
3512
|
+
M3Result FindEmbeddedSnapshot (const u8* i_start, const u8* i_end, cstr_t i_secName,
|
|
3513
|
+
const u8** o_secStart, const u8** o_secEnd,
|
|
3514
|
+
const u8** o_data, u32* o_size)
|
|
3515
|
+
{
|
|
3516
|
+
M3Result result = m3Err_none;
|
|
3517
|
+
bytes_t pos = i_start;
|
|
3518
|
+
bool found = false;
|
|
3519
|
+
cstr_t name = NULL;
|
|
3520
|
+
|
|
3521
|
+
if (o_secStart) {
|
|
3522
|
+
*o_secStart = NULL;
|
|
3523
|
+
}
|
|
3524
|
+
if (o_secEnd) {
|
|
3525
|
+
*o_secEnd = NULL;
|
|
3526
|
+
}
|
|
3527
|
+
if (o_data) {
|
|
3528
|
+
*o_data = NULL;
|
|
3529
|
+
}
|
|
3530
|
+
if (o_size) {
|
|
3531
|
+
*o_size = 0;
|
|
3532
|
+
}
|
|
3533
|
+
|
|
3534
|
+
_throwif(m3Err_wasmMalformed, i_end - pos < 8 or memcmp(pos, "\0asm\1\0\0\0", 8) != 0);
|
|
3535
|
+
pos += 8;
|
|
3536
|
+
|
|
3537
|
+
while (pos < i_end) {
|
|
3538
|
+
bytes_t secStart = pos;
|
|
3539
|
+
u8 sectionId = *pos++;
|
|
3540
|
+
u32 size = 0;
|
|
3541
|
+
_ (ReadLEB_u32(&size, &pos, i_end));
|
|
3542
|
+
_throwif(m3Err_wasmUnderrun, size > (size_t)(i_end - pos));
|
|
3543
|
+
{
|
|
3544
|
+
bytes_t payload = pos;
|
|
3545
|
+
pos += size;
|
|
3546
|
+
if (sectionId == 0) {
|
|
3547
|
+
_ (Read_utf8(&name, &payload, pos));
|
|
3548
|
+
if (strcmp(name, i_secName) == 0) {
|
|
3549
|
+
_throwif("duplicate embedded snapshot name", found);
|
|
3550
|
+
found = true;
|
|
3551
|
+
if (o_secStart) {
|
|
3552
|
+
*o_secStart = secStart;
|
|
3553
|
+
}
|
|
3554
|
+
if (o_secEnd) {
|
|
3555
|
+
*o_secEnd = pos;
|
|
3556
|
+
}
|
|
3557
|
+
if (o_data) {
|
|
3558
|
+
*o_data = payload;
|
|
3559
|
+
}
|
|
3560
|
+
if (o_size) {
|
|
3561
|
+
*o_size = (u32)(pos - payload);
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
3564
|
+
m3_Free(name);
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
}
|
|
3568
|
+
_catch:
|
|
3569
|
+
m3_Free(name);
|
|
3570
|
+
return result;
|
|
3571
|
+
}
|
|
3572
|
+
|
|
3573
|
+
// Builds the custom section name a snapshot of i_name lives under
|
|
3574
|
+
static
|
|
3575
|
+
M3Result SnapshotSectionName (cstr_t i_name, char* o_secName, size_t i_size)
|
|
3576
|
+
{
|
|
3577
|
+
if (i_name and *i_name) {
|
|
3578
|
+
if (strlen(i_name) > d_m3MaxSnapshotNameLength) {
|
|
3579
|
+
return "the snapshot name is too long";
|
|
3580
|
+
}
|
|
3581
|
+
snprintf(o_secName, i_size, "snapshot.%s", i_name);
|
|
3582
|
+
} else {
|
|
3583
|
+
snprintf(o_secName, i_size, "snapshot");
|
|
3584
|
+
}
|
|
3585
|
+
|
|
3586
|
+
return m3Err_none;
|
|
3587
|
+
}
|
|
3588
|
+
|
|
3589
|
+
// The snapshot of this name the module was parsed with, or NULL. Parsing keeps
|
|
3590
|
+
// two of the same name, and selecting that name is where they are refused.
|
|
3591
|
+
static
|
|
3592
|
+
M3Result ModuleSnapshot (IM3Module i_module, cstr_t i_name, M3EmbeddedSnapshot** o_snapshot)
|
|
3593
|
+
{
|
|
3594
|
+
M3Result result = m3Err_none;
|
|
3595
|
+
cstr_t name = i_name ? i_name : "";
|
|
3596
|
+
|
|
3597
|
+
*o_snapshot = NULL;
|
|
3598
|
+
|
|
3599
|
+
for (M3EmbeddedSnapshot* snap = i_module->snapshots; snap; snap = snap->next) {
|
|
3600
|
+
if (strcmp(snap->name, name) == 0) {
|
|
3601
|
+
_throwif("duplicate embedded snapshot name", *o_snapshot);
|
|
3602
|
+
*o_snapshot = snap;
|
|
3603
|
+
}
|
|
3604
|
+
}
|
|
3605
|
+
|
|
3606
|
+
_catch:
|
|
3607
|
+
return result;
|
|
3608
|
+
}
|
|
3609
|
+
#endif
|
|
3610
|
+
|
|
3611
|
+
M3Result m3_LoadSnapshotFromBuffer (IM3Runtime io_runtime, IM3Module i_module, const void* i_bytes, size_t i_size)
|
|
3612
|
+
{
|
|
3613
|
+
if (!io_runtime || !i_module || !i_bytes) {
|
|
3614
|
+
return m3Err_mallocFailed;
|
|
3615
|
+
}
|
|
3616
|
+
#if d_m3HasSnapshots
|
|
3617
|
+
// A Wasm binary carries its snapshot in a custom section. It is taken from
|
|
3618
|
+
// the buffer that was handed over, which need not be the one i_module was
|
|
3619
|
+
// parsed from - the snapshot still restores into i_module.
|
|
3620
|
+
if (i_size >= 8 && memcmp(i_bytes, "\0asm", 4) == 0) {
|
|
3621
|
+
const u8* data = NULL;
|
|
3622
|
+
u32 size = 0;
|
|
3623
|
+
M3Result result = FindEmbeddedSnapshot((const u8*)i_bytes, (const u8*)i_bytes + i_size,
|
|
3624
|
+
"snapshot", NULL, NULL, &data, &size);
|
|
3625
|
+
if (result) {
|
|
3626
|
+
return result;
|
|
3627
|
+
}
|
|
3628
|
+
if (not data) {
|
|
3629
|
+
return "the Wasm binary holds no embedded snapshot";
|
|
3630
|
+
}
|
|
3631
|
+
|
|
3632
|
+
{
|
|
3633
|
+
M3BufferReader br = { data, size, 0 };
|
|
3634
|
+
return LoadSnapshot(io_runtime, i_module, BufferReader_Read, &br, true);
|
|
3635
|
+
}
|
|
3636
|
+
}
|
|
3637
|
+
{
|
|
3638
|
+
M3BufferReader br = { (const u8*)i_bytes, i_size, 0 };
|
|
3639
|
+
return LoadSnapshot(io_runtime, i_module, BufferReader_Read, &br,
|
|
3640
|
+
i_size < 4 or memcmp(i_bytes, c_snapshotMagic, 4) != 0);
|
|
3641
|
+
}
|
|
3642
|
+
#else
|
|
3643
|
+
(void)i_size;
|
|
3644
|
+
return "snapshots are not available in this build of Wasm3";
|
|
3645
|
+
#endif
|
|
3646
|
+
}
|
|
3647
|
+
|
|
3648
|
+
bool m3_HasSnapshot (IM3Module i_module, const char* i_name)
|
|
3649
|
+
{
|
|
3650
|
+
if (not i_module) {
|
|
3651
|
+
return false;
|
|
3652
|
+
}
|
|
3653
|
+
#if d_m3HasSnapshots
|
|
3654
|
+
// two of that name are still one it carries: loading it is what fails
|
|
3655
|
+
M3EmbeddedSnapshot* snap = NULL;
|
|
3656
|
+
(void)ModuleSnapshot(i_module, i_name, &snap);
|
|
3657
|
+
|
|
3658
|
+
return snap != NULL;
|
|
3659
|
+
#else
|
|
3660
|
+
(void)i_name;
|
|
3661
|
+
return false;
|
|
3662
|
+
#endif
|
|
3663
|
+
}
|
|
3664
|
+
|
|
3665
|
+
M3Result m3_GetEmbeddedSnapshot (IM3Module i_module, const char* i_name,
|
|
3666
|
+
const void** o_bytes, size_t* o_size)
|
|
3667
|
+
{
|
|
3668
|
+
if (not i_module or not o_bytes or not o_size) {
|
|
3669
|
+
return m3Err_mallocFailed;
|
|
3670
|
+
}
|
|
3671
|
+
|
|
3672
|
+
*o_bytes = NULL;
|
|
3673
|
+
*o_size = 0;
|
|
3674
|
+
|
|
3675
|
+
#if d_m3HasSnapshots
|
|
3676
|
+
{
|
|
3677
|
+
M3EmbeddedSnapshot* snap = NULL;
|
|
3678
|
+
M3Result result = ModuleSnapshot(i_module, i_name, &snap);
|
|
3679
|
+
|
|
3680
|
+
if (result) {
|
|
3681
|
+
return result;
|
|
3682
|
+
}
|
|
3683
|
+
if (not snap) {
|
|
3684
|
+
return "the module holds no embedded snapshot of that name";
|
|
3685
|
+
}
|
|
3686
|
+
|
|
3687
|
+
*o_bytes = snap->data;
|
|
3688
|
+
*o_size = snap->size;
|
|
3689
|
+
|
|
3690
|
+
return m3Err_none;
|
|
3691
|
+
}
|
|
3692
|
+
#else
|
|
3693
|
+
(void)i_name;
|
|
3694
|
+
return "snapshots are not available in this build of Wasm3";
|
|
3695
|
+
#endif
|
|
3696
|
+
}
|
|
3697
|
+
|
|
3698
|
+
M3Result m3_LoadEmbeddedSnapshot (IM3Runtime io_runtime, IM3Module i_module, const char* i_name)
|
|
3699
|
+
{
|
|
3700
|
+
if (not io_runtime or not i_module) {
|
|
3701
|
+
return m3Err_mallocFailed;
|
|
3702
|
+
}
|
|
3703
|
+
#if d_m3HasSnapshots
|
|
3704
|
+
{
|
|
3705
|
+
M3EmbeddedSnapshot* snap = NULL;
|
|
3706
|
+
M3Result result = ModuleSnapshot(i_module, i_name, &snap);
|
|
3707
|
+
M3BufferReader br;
|
|
3708
|
+
|
|
3709
|
+
if (result) {
|
|
3710
|
+
return result;
|
|
3711
|
+
}
|
|
3712
|
+
if (not snap) {
|
|
3713
|
+
return "the module holds no embedded snapshot of that name";
|
|
3714
|
+
}
|
|
3715
|
+
|
|
3716
|
+
br.buffer = snap->data;
|
|
3717
|
+
br.size = snap->size;
|
|
3718
|
+
br.cursor = 0;
|
|
3719
|
+
|
|
3720
|
+
return LoadSnapshot(io_runtime, i_module, BufferReader_Read, &br, true);
|
|
3721
|
+
}
|
|
3722
|
+
#else
|
|
3723
|
+
(void)i_name;
|
|
3724
|
+
return "snapshots are not available in this build of Wasm3";
|
|
3725
|
+
#endif
|
|
3726
|
+
}
|
|
3727
|
+
|
|
3728
|
+
|
|
3729
|
+
M3Result m3_SaveSnapshotToModule (IM3Runtime io_runtime, IM3Module i_module, const char* i_name,
|
|
3730
|
+
void** o_bytes, size_t* o_size)
|
|
3731
|
+
{
|
|
3732
|
+
#if d_m3HasSnapshots
|
|
3733
|
+
M3Result result = m3Err_none;
|
|
3734
|
+
M3BufferWriter bw = { NULL, 0, 0 };
|
|
3735
|
+
u8* out = NULL;
|
|
3736
|
+
|
|
3737
|
+
char secName[d_m3MaxSnapshotNameLength + sizeof("snapshot.")];
|
|
3738
|
+
u32 nameLen, nameLenLebSize, payloadSize, payloadSizeLebSize;
|
|
3739
|
+
size_t origSize, totalSize;
|
|
3740
|
+
const u8* dropStart = NULL;
|
|
3741
|
+
const u8* dropEnd = NULL;
|
|
3742
|
+
IM3Module saved = NULL;
|
|
3743
|
+
u8* p;
|
|
3744
|
+
|
|
3745
|
+
if (not io_runtime or not i_module or not o_bytes or not o_size) {
|
|
3746
|
+
return m3Err_mallocFailed;
|
|
3747
|
+
}
|
|
3748
|
+
|
|
3749
|
+
*o_bytes = NULL;
|
|
3750
|
+
*o_size = 0;
|
|
3751
|
+
|
|
3752
|
+
_throwif("the module carries no Wasm bytecode to embed a snapshot in",
|
|
3753
|
+
not i_module->wasmStart or i_module->wasmEnd <= i_module->wasmStart);
|
|
3754
|
+
|
|
3755
|
+
_ (SnapshotSectionName(i_name, secName, sizeof(secName)));
|
|
3756
|
+
|
|
3757
|
+
// An embedded snapshot is there to be resumed - the unnamed one is resumed
|
|
3758
|
+
// just by running the module - and a postmortem cannot be. With nothing
|
|
3759
|
+
// paused, a save is a postmortem.
|
|
3760
|
+
_throwif("a postmortem cannot be embedded in a module",
|
|
3761
|
+
not io_runtime->rootContinuation or io_runtime->rootContinuation->state != cont_suspended);
|
|
3762
|
+
|
|
3763
|
+
_ (SaveSnapshot(io_runtime, BufferWriter_Write, &bw, &saved, true));
|
|
3764
|
+
|
|
3765
|
+
_throwif("there is nothing to snapshot", not bw.size);
|
|
3766
|
+
|
|
3767
|
+
// the snapshot is of the module the paused call is in: embedding it in any
|
|
3768
|
+
// other leaves a binary whose fingerprint can never match
|
|
3769
|
+
_throwif("the snapshot belongs to another module", saved != i_module);
|
|
3770
|
+
|
|
3771
|
+
_throwif("the snapshot is too large to embed in a Wasm module", bw.size > 0xF0000000u);
|
|
3772
|
+
|
|
3773
|
+
// a snapshot of this name already in the module is replaced, not added to
|
|
3774
|
+
_ (FindEmbeddedSnapshot(i_module->wasmStart, i_module->wasmEnd, secName, &dropStart, &dropEnd, NULL, NULL));
|
|
3775
|
+
|
|
3776
|
+
origSize = (size_t)(i_module->wasmEnd - i_module->wasmStart) - (dropStart ? (size_t)(dropEnd - dropStart) : 0);
|
|
3777
|
+
nameLen = (u32)strlen(secName);
|
|
3778
|
+
nameLenLebSize = SizeOfLEB_u32(nameLen);
|
|
3779
|
+
payloadSize = nameLenLebSize + nameLen + (u32)bw.size;
|
|
3780
|
+
payloadSizeLebSize = SizeOfLEB_u32(payloadSize);
|
|
3781
|
+
totalSize = origSize + 1 + payloadSizeLebSize + payloadSize;
|
|
3782
|
+
|
|
3783
|
+
out = (u8*)m3_Malloc("wasm_snapshot_module", totalSize);
|
|
3784
|
+
_throwifnull(out);
|
|
3785
|
+
|
|
3786
|
+
p = out;
|
|
3787
|
+
|
|
3788
|
+
if (dropStart) {
|
|
3789
|
+
size_t before = (size_t)(dropStart - i_module->wasmStart);
|
|
3790
|
+
size_t after = (size_t)(i_module->wasmEnd - dropEnd);
|
|
3791
|
+
|
|
3792
|
+
memcpy(p, i_module->wasmStart, before);
|
|
3793
|
+
p += before;
|
|
3794
|
+
memcpy(p, dropEnd, after);
|
|
3795
|
+
p += after;
|
|
3796
|
+
} else {
|
|
3797
|
+
memcpy(p, i_module->wasmStart, origSize);
|
|
3798
|
+
p += origSize;
|
|
3799
|
+
}
|
|
3800
|
+
|
|
3801
|
+
*p++ = 0x00; // custom section
|
|
3802
|
+
p = EmitLEB_u32(p, payloadSize);
|
|
3803
|
+
p = EmitLEB_u32(p, nameLen);
|
|
3804
|
+
memcpy(p, secName, nameLen);
|
|
3805
|
+
p += nameLen;
|
|
3806
|
+
memcpy(p, bw.buffer, bw.size);
|
|
3807
|
+
|
|
3808
|
+
*o_bytes = out;
|
|
3809
|
+
*o_size = totalSize;
|
|
3810
|
+
out = NULL;
|
|
3811
|
+
|
|
3812
|
+
_catch:
|
|
3813
|
+
m3_Free(bw.buffer);
|
|
3814
|
+
m3_Free(out);
|
|
3815
|
+
|
|
3816
|
+
return result;
|
|
3817
|
+
#else
|
|
3818
|
+
(void)io_runtime;
|
|
3819
|
+
(void)i_module;
|
|
3820
|
+
(void)i_name;
|
|
3821
|
+
(void)o_bytes;
|
|
3822
|
+
(void)o_size;
|
|
3823
|
+
return "snapshots are not available in this build of Wasm3";
|
|
3824
|
+
#endif
|
|
3825
|
+
}
|