xbintsc 0.6.24 → 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/graph.js +9 -5
- package/dist/src/driver/bundler/graph.js.map +1 -1
- 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/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/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/graph.ts +9 -5
- package/src/driver/fingerprint.ts +3 -3
- package/src/driver/link-features.ts +1 -1
- 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/runtime/ext_node/worker_threads/worker_threads.c +0 -214
|
@@ -0,0 +1,3146 @@
|
|
|
1
|
+
//
|
|
2
|
+
// m3_env.c
|
|
3
|
+
//
|
|
4
|
+
// Created by Steven Massey on 4/19/19.
|
|
5
|
+
// Copyright © 2019 Steven Massey. All rights reserved.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#include <stdarg.h>
|
|
9
|
+
#include <limits.h>
|
|
10
|
+
#include <errno.h>
|
|
11
|
+
#include <float.h>
|
|
12
|
+
#include <ctype.h>
|
|
13
|
+
|
|
14
|
+
#include "m3_env.h"
|
|
15
|
+
#include "m3_bind.h"
|
|
16
|
+
#include "m3_deterministic.h"
|
|
17
|
+
#include "m3_compile.h"
|
|
18
|
+
#include "m3_host.h"
|
|
19
|
+
#include "m3_exception.h"
|
|
20
|
+
#include "m3_info.h"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
IM3Environment m3_NewEnvironment ()
|
|
24
|
+
{
|
|
25
|
+
IM3Environment env = m3_AllocStruct(M3Environment);
|
|
26
|
+
|
|
27
|
+
if (env) {
|
|
28
|
+
_try
|
|
29
|
+
{
|
|
30
|
+
// create FuncTypes for all simple block return ValueTypes.
|
|
31
|
+
// v128 is skipped: it parses as a slot but has no operations.
|
|
32
|
+
for (u8 t = c_m3Type_none; t < c_m3Type_count; t++) {
|
|
33
|
+
if (t == c_m3Type_v128) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
IM3FuncType ftype;
|
|
38
|
+
_ (AllocFuncType(&ftype, 1));
|
|
39
|
+
|
|
40
|
+
ftype->numArgs = 0;
|
|
41
|
+
ftype->numRets = (t == c_m3Type_none) ? 0 : 1;
|
|
42
|
+
ftype->types[0] = t;
|
|
43
|
+
|
|
44
|
+
Environment_AddFuncType(env, &ftype);
|
|
45
|
+
|
|
46
|
+
env->retFuncTypes[t] = ftype;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_catch:
|
|
51
|
+
if (result) {
|
|
52
|
+
m3_FreeEnvironment(env);
|
|
53
|
+
env = NULL;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return env;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
void Environment_Release (IM3Environment i_environment)
|
|
62
|
+
{
|
|
63
|
+
IM3FuncType ftype = i_environment->funcTypes;
|
|
64
|
+
|
|
65
|
+
while (ftype) {
|
|
66
|
+
IM3FuncType next = ftype->next;
|
|
67
|
+
m3_Free(ftype);
|
|
68
|
+
ftype = next;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
m3log(runtime, "freeing %d pages from environment", CountCodePages(i_environment->pagesReleased));
|
|
72
|
+
FreeCodePages(&i_environment->pagesReleased);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
void m3_FreeEnvironment (IM3Environment i_environment)
|
|
77
|
+
{
|
|
78
|
+
if (i_environment) {
|
|
79
|
+
Environment_Release(i_environment);
|
|
80
|
+
m3_Free(i_environment);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
void m3_SetCustomSectionHandler (IM3Environment i_environment, M3SectionHandler i_handler)
|
|
86
|
+
{
|
|
87
|
+
if (i_environment) {
|
|
88
|
+
i_environment->customSectionHandler = i_handler;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
// returns the same io_funcType or replaces it with an equivalent that's already in the type linked list
|
|
94
|
+
M3Result Environment_AddFuncType (IM3Environment i_environment, IM3FuncType* io_funcType)
|
|
95
|
+
{
|
|
96
|
+
IM3FuncType addType = *io_funcType;
|
|
97
|
+
IM3FuncType newType = i_environment->funcTypes;
|
|
98
|
+
|
|
99
|
+
while (newType) {
|
|
100
|
+
if (not newType->inRecGroup and AreFuncTypesEqual(newType, addType)) {
|
|
101
|
+
m3_Free(addType);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
newType = newType->next;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (newType == NULL) {
|
|
109
|
+
// a type index has to fit in the heap type field of an m3type_t
|
|
110
|
+
if (i_environment->numFuncTypes >= d_m3MaxSaneTypesCount) {
|
|
111
|
+
m3_Free(addType);
|
|
112
|
+
*io_funcType = NULL;
|
|
113
|
+
return "too many distinct function types";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
newType = addType;
|
|
117
|
+
newType->canonicalIndex = i_environment->numFuncTypes++;
|
|
118
|
+
newType->next = i_environment->funcTypes;
|
|
119
|
+
i_environment->funcTypes = newType;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
*io_funcType = newType;
|
|
123
|
+
|
|
124
|
+
return m3Err_none;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
// Adds a type without looking for an equal one first, and hands back the index
|
|
129
|
+
// it was given.
|
|
130
|
+
//
|
|
131
|
+
// Members of a recursive group do not get to share an entry with a type that
|
|
132
|
+
// merely looks like them: the group is their identity, so two groups spelled
|
|
133
|
+
// the same way are still two groups, and a type inside one is never the type
|
|
134
|
+
// outside it that matches field for field. Reserving the indices up front is
|
|
135
|
+
// also what lets the members refer to each other while they are being read.
|
|
136
|
+
M3Result Environment_ReserveFuncTypes (IM3Environment i_environment, u32 i_count, u16* o_firstIndex)
|
|
137
|
+
{
|
|
138
|
+
if ((u64)i_environment->numFuncTypes + i_count > d_m3MaxSaneTypesCount) {
|
|
139
|
+
return "too many distinct function types";
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
*o_firstIndex = i_environment->numFuncTypes;
|
|
143
|
+
i_environment->numFuncTypes += (u16)i_count;
|
|
144
|
+
|
|
145
|
+
return m3Err_none;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
void Environment_AdoptFuncType (IM3Environment i_environment, IM3FuncType i_funcType, u16 i_canonicalIndex)
|
|
150
|
+
{
|
|
151
|
+
i_funcType->inRecGroup = true;
|
|
152
|
+
i_funcType->canonicalIndex = i_canonicalIndex;
|
|
153
|
+
i_funcType->next = i_environment->funcTypes;
|
|
154
|
+
i_environment->funcTypes = i_funcType;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
IM3CodePage RemoveCodePageOfCapacity (M3CodePage** io_list, u32 i_minimumLineCount)
|
|
159
|
+
{
|
|
160
|
+
IM3CodePage prev = NULL;
|
|
161
|
+
IM3CodePage page = *io_list;
|
|
162
|
+
|
|
163
|
+
while (page) {
|
|
164
|
+
if (NumFreeLines(page) >= i_minimumLineCount) { d_m3Assert (page->info.usageCount == 0);
|
|
165
|
+
IM3CodePage next = page->info.next;
|
|
166
|
+
if (prev) {
|
|
167
|
+
prev->info.next = next; // mid-list
|
|
168
|
+
} else {
|
|
169
|
+
*io_list = next; // front of list
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
prev = page;
|
|
176
|
+
page = page->info.next;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return page;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
IM3CodePage Environment_AcquireCodePage (IM3Environment i_environment, u32 i_minimumLineCount)
|
|
184
|
+
{
|
|
185
|
+
return RemoveCodePageOfCapacity(&i_environment->pagesReleased, i_minimumLineCount);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
void Environment_ReleaseCodePages (IM3Environment i_environment, IM3CodePage i_codePageList)
|
|
190
|
+
{
|
|
191
|
+
IM3CodePage end = i_codePageList;
|
|
192
|
+
|
|
193
|
+
while (end) {
|
|
194
|
+
end->info.lineIndex = 0; // reset page
|
|
195
|
+
#if d_m3RecordBacktraces
|
|
196
|
+
end->info.mapping->size = 0;
|
|
197
|
+
#endif // d_m3RecordBacktraces
|
|
198
|
+
|
|
199
|
+
IM3CodePage next = end->info.next;
|
|
200
|
+
if (not next) {
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
end = next;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (end) {
|
|
208
|
+
// push list to front
|
|
209
|
+
end->info.next = i_environment->pagesReleased;
|
|
210
|
+
i_environment->pagesReleased = i_codePageList;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
IM3Runtime m3_NewRuntime (IM3Environment i_environment, u32 i_stackSizeInBytes, void* i_userdata)
|
|
216
|
+
{
|
|
217
|
+
IM3Runtime runtime = m3_AllocStruct(M3Runtime);
|
|
218
|
+
|
|
219
|
+
if (runtime) {
|
|
220
|
+
m3_ResetErrorInfo(runtime);
|
|
221
|
+
|
|
222
|
+
runtime->environment = i_environment;
|
|
223
|
+
runtime->userdata = i_userdata;
|
|
224
|
+
|
|
225
|
+
Deterministic_InitRuntime(runtime);
|
|
226
|
+
|
|
227
|
+
runtime->originStack = m3_Malloc("Wasm Stack", i_stackSizeInBytes + 4 * sizeof(m3slot_t)); // TODO: more precise stack checks
|
|
228
|
+
|
|
229
|
+
if (runtime->originStack) {
|
|
230
|
+
runtime->stack = runtime->originStack;
|
|
231
|
+
runtime->numStackSlots = i_stackSizeInBytes / sizeof(m3slot_t); m3log (runtime, "new stack: %p, slots: %u", runtime->originStack, runtime->numStackSlots);
|
|
232
|
+
} else {
|
|
233
|
+
m3_Free(runtime);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return runtime;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
void m3_SetValidation (IM3Runtime i_runtime, bool i_enable)
|
|
241
|
+
{
|
|
242
|
+
#if d_m3EnableValidation
|
|
243
|
+
if (i_runtime) {
|
|
244
|
+
i_runtime->skipValidation = not i_enable;
|
|
245
|
+
}
|
|
246
|
+
#else
|
|
247
|
+
(void)i_runtime;
|
|
248
|
+
(void)i_enable; // nothing to skip: the validator was compiled out
|
|
249
|
+
#endif
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
M3Result m3_SetResourceLimit (IM3Runtime i_runtime, M3ResourceLimit i_limit, uint64_t i_value)
|
|
253
|
+
{
|
|
254
|
+
if (not i_runtime) {
|
|
255
|
+
return m3Err_mallocFailed;
|
|
256
|
+
}
|
|
257
|
+
switch (i_limit) {
|
|
258
|
+
case c_m3Limit_MemoryBytes:
|
|
259
|
+
if (i_value and i_value < i_runtime->memoryBytesUsed) {
|
|
260
|
+
return m3Err_resourceLimitBelowUsage;
|
|
261
|
+
}
|
|
262
|
+
i_runtime->memoryBytesLimit = i_value;
|
|
263
|
+
return m3Err_none;
|
|
264
|
+
case c_m3Limit_TableElements:
|
|
265
|
+
if (i_value and i_value < i_runtime->tableElementsUsed) {
|
|
266
|
+
return m3Err_resourceLimitBelowUsage;
|
|
267
|
+
}
|
|
268
|
+
i_runtime->tableElementsLimit = i_value;
|
|
269
|
+
return m3Err_none;
|
|
270
|
+
#if d_m3HasStackSwitching
|
|
271
|
+
case c_m3Limit_Continuations:
|
|
272
|
+
if (i_value and i_value < i_runtime->continuationsAllocated) {
|
|
273
|
+
return m3Err_resourceLimitBelowUsage;
|
|
274
|
+
}
|
|
275
|
+
i_runtime->continuationsLimit = (u32)M3_MIN(i_value, (u64)UINT32_MAX);
|
|
276
|
+
while (i_value and i_runtime->valStackPoolCount > i_value - i_runtime->continuationsAllocated) {
|
|
277
|
+
m3_Free(i_runtime->valStackPool[--i_runtime->valStackPoolCount]);
|
|
278
|
+
}
|
|
279
|
+
return m3Err_none;
|
|
280
|
+
#else
|
|
281
|
+
case c_m3Limit_Continuations: return m3Err_resourceLimitNotSupported;
|
|
282
|
+
#endif
|
|
283
|
+
case c_m3Limit_GasUnits:
|
|
284
|
+
#if d_m3HasGasMetering
|
|
285
|
+
i_runtime->gasLimit = i_runtime->gasRemaining = (i64)M3_MIN(i_value, (u64)INT64_MAX);
|
|
286
|
+
return m3Err_none;
|
|
287
|
+
#else
|
|
288
|
+
return m3Err_resourceLimitNotSupported;
|
|
289
|
+
#endif
|
|
290
|
+
default: return m3Err_unknownResourceLimit;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
uint64_t m3_GetResourceLimit (IM3Runtime i_runtime, M3ResourceLimit i_limit)
|
|
295
|
+
{
|
|
296
|
+
if (not i_runtime) {
|
|
297
|
+
return 0;
|
|
298
|
+
}
|
|
299
|
+
switch (i_limit) {
|
|
300
|
+
case c_m3Limit_MemoryBytes: return i_runtime->memoryBytesLimit;
|
|
301
|
+
case c_m3Limit_TableElements: return i_runtime->tableElementsLimit;
|
|
302
|
+
#if d_m3HasStackSwitching
|
|
303
|
+
case c_m3Limit_Continuations: return i_runtime->continuationsLimit;
|
|
304
|
+
#endif
|
|
305
|
+
#if d_m3HasGasMetering
|
|
306
|
+
case c_m3Limit_GasUnits: return (u64)i_runtime->gasLimit;
|
|
307
|
+
#endif
|
|
308
|
+
default: return 0;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
uint64_t m3_GetResourceUsage (IM3Runtime i_runtime, M3ResourceLimit i_limit)
|
|
313
|
+
{
|
|
314
|
+
if (not i_runtime) {
|
|
315
|
+
return 0;
|
|
316
|
+
}
|
|
317
|
+
switch (i_limit) {
|
|
318
|
+
case c_m3Limit_MemoryBytes: return i_runtime->memoryBytesUsed;
|
|
319
|
+
case c_m3Limit_TableElements: return i_runtime->tableElementsUsed;
|
|
320
|
+
#if d_m3HasStackSwitching
|
|
321
|
+
case c_m3Limit_Continuations: return i_runtime->continuationsAllocated;
|
|
322
|
+
#endif
|
|
323
|
+
#if d_m3HasGasMetering
|
|
324
|
+
case c_m3Limit_GasUnits: return (u64)i_runtime->gasLimit - (u64)i_runtime->gasRemaining;
|
|
325
|
+
#endif
|
|
326
|
+
default: return 0;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
void* m3_GetUserData (IM3Runtime i_runtime)
|
|
331
|
+
{
|
|
332
|
+
return i_runtime ? i_runtime->userdata : NULL;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
void* ForEachModule (IM3Runtime i_runtime, ModuleVisitor i_visitor, void* i_info)
|
|
337
|
+
{
|
|
338
|
+
void* r = NULL;
|
|
339
|
+
|
|
340
|
+
IM3Module module = i_runtime->modules;
|
|
341
|
+
|
|
342
|
+
while (module) {
|
|
343
|
+
IM3Module next = module->next;
|
|
344
|
+
if ((r = i_visitor(module, i_info))) {
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
module = next;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return r;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
void* _FreeModule (IM3Module i_module, void* i_info)
|
|
356
|
+
{
|
|
357
|
+
m3_FreeModule(i_module);
|
|
358
|
+
return NULL;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
// A module's memory and table index spaces hold borrowed pointers: linking
|
|
363
|
+
// repoints an imported slot at the memory or table the exporting module owns.
|
|
364
|
+
// m3_FreeModule tells a borrowed slot from an owned one by reading that owner,
|
|
365
|
+
// which only holds while the object is still allocated - and the free walk
|
|
366
|
+
// below visits modules in load order, with no ordering between an owner and
|
|
367
|
+
// the modules borrowing from it. So clear the borrowed slots first, in a pass
|
|
368
|
+
// that frees nothing; the walk then skips them on the NULL check it already
|
|
369
|
+
// does, instead of reading a memory some earlier module has freed.
|
|
370
|
+
void* _ReleaseBorrowedSlots (IM3Module i_module, void* i_info)
|
|
371
|
+
{
|
|
372
|
+
for (u32 i = 0; i < i_module->numMemories; ++i) {
|
|
373
|
+
IM3Memory memory = i_module->memories[i];
|
|
374
|
+
|
|
375
|
+
if (memory and memory->owner != i_module) {
|
|
376
|
+
i_module->memories[i] = NULL;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
for (u32 i = 0; i < i_module->numTables; ++i) {
|
|
381
|
+
IM3Table table = i_module->tables[i];
|
|
382
|
+
|
|
383
|
+
if (table and table->owner != i_module) {
|
|
384
|
+
i_module->tables[i] = NULL;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return NULL;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
void Runtime_Release (IM3Runtime i_runtime)
|
|
393
|
+
{
|
|
394
|
+
ForEachModule(i_runtime, _ReleaseBorrowedSlots, NULL);
|
|
395
|
+
ForEachModule(i_runtime, _FreeModule, NULL); d_m3Assert (i_runtime->numActiveCodePages == 0);
|
|
396
|
+
|
|
397
|
+
Environment_ReleaseCodePages(i_runtime->environment, i_runtime->pagesOpen);
|
|
398
|
+
Environment_ReleaseCodePages(i_runtime->environment, i_runtime->pagesFull);
|
|
399
|
+
|
|
400
|
+
m3_Free(i_runtime->originStack);
|
|
401
|
+
|
|
402
|
+
#if d_m3EnableValidation
|
|
403
|
+
m3_Free(i_runtime->validator);
|
|
404
|
+
#endif
|
|
405
|
+
|
|
406
|
+
#if d_m3HasExceptionHandling
|
|
407
|
+
// a runtime let go of while suspended still holds its exceptions
|
|
408
|
+
FreeExceptions(i_runtime);
|
|
409
|
+
m3_Free(i_runtime->uncaughtException);
|
|
410
|
+
#endif
|
|
411
|
+
#if d_m3HasExceptionHandling || d_m3HasStackSwitching
|
|
412
|
+
while (i_runtime->hostTags) {
|
|
413
|
+
M3HostTag* next = i_runtime->hostTags->next;
|
|
414
|
+
m3_Free(i_runtime->hostTags);
|
|
415
|
+
i_runtime->hostTags = next;
|
|
416
|
+
}
|
|
417
|
+
#endif
|
|
418
|
+
#if d_m3HasStackSwitching
|
|
419
|
+
Continuation_ReleaseAll(i_runtime);
|
|
420
|
+
#endif
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
void m3_FreeRuntime (IM3Runtime i_runtime)
|
|
425
|
+
{
|
|
426
|
+
if (i_runtime) {
|
|
427
|
+
m3_PrintProfilerInfo();
|
|
428
|
+
|
|
429
|
+
Runtime_Release(i_runtime);
|
|
430
|
+
m3_Free(i_runtime);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
#if d_m3HasStackSwitching
|
|
436
|
+
|
|
437
|
+
IM3Continuation Continuation_New (IM3Runtime i_runtime, IM3FuncType i_type, IM3Function i_function)
|
|
438
|
+
{
|
|
439
|
+
IM3Continuation cont = m3_AllocStruct(M3Continuation);
|
|
440
|
+
if (!cont) {
|
|
441
|
+
return NULL;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
cont->type = i_type;
|
|
445
|
+
cont->entryFunction = i_function;
|
|
446
|
+
cont->state = cont_allocated;
|
|
447
|
+
|
|
448
|
+
cont->numStackSlots = d_m3ContinuationStackSlots;
|
|
449
|
+
// frames are recorded only when this continuation actually suspends, and
|
|
450
|
+
// only as deep as it got, so the array starts out unallocated
|
|
451
|
+
cont->frames = NULL;
|
|
452
|
+
cont->framesCap = 0;
|
|
453
|
+
cont->numFrames = 0;
|
|
454
|
+
|
|
455
|
+
cont->sp = cont->valStack;
|
|
456
|
+
cont->pc = i_function ? i_function->compiled : NULL;
|
|
457
|
+
cont->r0 = 0;
|
|
458
|
+
# if d_m3HasFloat
|
|
459
|
+
cont->fp0 = 0;
|
|
460
|
+
# endif
|
|
461
|
+
cont->boundArgsCount = 0;
|
|
462
|
+
cont->numSuspendResults = 0;
|
|
463
|
+
cont->numHandlers = 0;
|
|
464
|
+
cont->handlersPC = NULL;
|
|
465
|
+
cont->parent = NULL;
|
|
466
|
+
|
|
467
|
+
cont->next = i_runtime->continuations;
|
|
468
|
+
i_runtime->continuations = cont;
|
|
469
|
+
|
|
470
|
+
return cont;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
IM3Continuation Continuation_ForkSuspended (IM3Runtime i_runtime, IM3Continuation i_cont)
|
|
474
|
+
{
|
|
475
|
+
IM3Continuation newCont = Continuation_New(i_runtime, i_cont->type, i_cont->entryFunction);
|
|
476
|
+
if (M3_UNLIKELY(not newCont)) {
|
|
477
|
+
return NULL;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
newCont->valStack = i_cont->valStack;
|
|
481
|
+
newCont->numStackSlots = i_cont->numStackSlots;
|
|
482
|
+
newCont->sp = i_cont->sp;
|
|
483
|
+
newCont->pc = i_cont->pc;
|
|
484
|
+
newCont->suspendPoint = i_cont->suspendPoint;
|
|
485
|
+
newCont->r0 = i_cont->r0;
|
|
486
|
+
# if d_m3HasFloat
|
|
487
|
+
newCont->fp0 = i_cont->fp0;
|
|
488
|
+
# endif
|
|
489
|
+
newCont->frames = i_cont->frames;
|
|
490
|
+
newCont->framesCap = i_cont->framesCap;
|
|
491
|
+
newCont->numFrames = i_cont->numFrames;
|
|
492
|
+
|
|
493
|
+
newCont->boundArgsCount = 0;
|
|
494
|
+
# if d_m3HasExceptionHandling
|
|
495
|
+
newCont->resumeThrow = i_cont->resumeThrow;
|
|
496
|
+
i_cont->resumeThrow = NULL;
|
|
497
|
+
# endif
|
|
498
|
+
newCont->numSuspendResults = i_cont->numSuspendResults;
|
|
499
|
+
for (u32 i = 0; i < i_cont->numSuspendResults; ++i) {
|
|
500
|
+
newCont->suspendResultOffsets[i] = i_cont->suspendResultOffsets[i];
|
|
501
|
+
newCont->suspendResultIs64[i] = i_cont->suspendResultIs64[i];
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
newCont->numHandlers = i_cont->numHandlers;
|
|
505
|
+
newCont->handlersPC = i_cont->handlersPC;
|
|
506
|
+
newCont->parent = i_cont->parent;
|
|
507
|
+
newCont->state = cont_suspended;
|
|
508
|
+
|
|
509
|
+
i_cont->valStack = NULL;
|
|
510
|
+
i_cont->frames = NULL;
|
|
511
|
+
i_cont->framesCap = 0;
|
|
512
|
+
i_cont->numFrames = 0;
|
|
513
|
+
i_cont->state = cont_consumed;
|
|
514
|
+
|
|
515
|
+
return newCont;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Only cont.new and snapshot restoration acquire stacks. Binding and suspension
|
|
519
|
+
// transfer them between records without changing the active count.
|
|
520
|
+
M3Result Continuation_AcquireStack (IM3Runtime runtime, IM3Continuation cont)
|
|
521
|
+
{
|
|
522
|
+
if (not Continuation_CanAcquireStack(runtime)) {
|
|
523
|
+
return m3Err_continuationLimitExceeded;
|
|
524
|
+
}
|
|
525
|
+
if (runtime->valStackPoolCount) {
|
|
526
|
+
cont->valStack = runtime->valStackPool[--runtime->valStackPoolCount];
|
|
527
|
+
memset(cont->valStack, 0, (cont->numStackSlots + 4) * sizeof(m3slot_t));
|
|
528
|
+
} else {
|
|
529
|
+
cont->valStack = m3_AllocArray(m3slot_t, cont->numStackSlots + 4);
|
|
530
|
+
}
|
|
531
|
+
if (not cont->valStack) {
|
|
532
|
+
return m3Err_mallocFailed;
|
|
533
|
+
}
|
|
534
|
+
cont->sp = cont->valStack;
|
|
535
|
+
runtime->continuationsAllocated++;
|
|
536
|
+
return m3Err_none;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
void Continuation_RecycleStack (IM3Runtime runtime, IM3Continuation cont)
|
|
540
|
+
{
|
|
541
|
+
if (not cont->valStack or cont == runtime->rootContinuation) {
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
if (not runtime->valStackPool) {
|
|
545
|
+
runtime->valStackPool = m3_AllocArray(m3slot_t*, 16);
|
|
546
|
+
if (runtime->valStackPool) {
|
|
547
|
+
runtime->valStackPoolCap = 16;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
if (runtime->valStackPoolCount < runtime->valStackPoolCap and
|
|
551
|
+
(not runtime->continuationsLimit or runtime->valStackPoolCount <
|
|
552
|
+
runtime->continuationsLimit - runtime->continuationsAllocated + 1)) {
|
|
553
|
+
runtime->valStackPool[runtime->valStackPoolCount++] = cont->valStack;
|
|
554
|
+
cont->valStack = NULL;
|
|
555
|
+
} else {
|
|
556
|
+
m3_Free(cont->valStack);
|
|
557
|
+
}
|
|
558
|
+
cont->sp = NULL;
|
|
559
|
+
runtime->continuationsAllocated--;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
void Continuation_ReleaseAll (IM3Runtime io_runtime)
|
|
563
|
+
{
|
|
564
|
+
// Records live until teardown: a consumed reference can still be in a table
|
|
565
|
+
// or on a Wasm stack. Its value stack has already been recycled.
|
|
566
|
+
IM3Continuation cont = io_runtime->continuations;
|
|
567
|
+
|
|
568
|
+
while (cont) {
|
|
569
|
+
IM3Continuation next = cont->next;
|
|
570
|
+
|
|
571
|
+
m3_Free(cont->valStack);
|
|
572
|
+
m3_Free(cont->frames);
|
|
573
|
+
m3_Free(cont);
|
|
574
|
+
|
|
575
|
+
cont = next;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
for (u32 i = 0; i < io_runtime->valStackPoolCount; ++i) {
|
|
579
|
+
m3_Free(io_runtime->valStackPool[i]);
|
|
580
|
+
}
|
|
581
|
+
m3_Free(io_runtime->valStackPool);
|
|
582
|
+
io_runtime->valStackPoolCount = io_runtime->valStackPoolCap = 0;
|
|
583
|
+
io_runtime->continuationsAllocated = 0;
|
|
584
|
+
io_runtime->continuations = NULL;
|
|
585
|
+
io_runtime->activeContinuation = NULL;
|
|
586
|
+
io_runtime->rootContinuation = NULL;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
m3ret_t Continuation_RecordFrame (IM3Runtime i_runtime, const M3Frame* i_frame)
|
|
591
|
+
{
|
|
592
|
+
IM3Continuation cont = i_runtime->activeContinuation;
|
|
593
|
+
|
|
594
|
+
// the marker only travels between a suspend and the resume that runs it,
|
|
595
|
+
// and that whole stretch has an active continuation
|
|
596
|
+
d_m3Assert(cont);
|
|
597
|
+
|
|
598
|
+
if (cont->numFrames == cont->framesCap) {
|
|
599
|
+
u32 newCap = cont->framesCap ? cont->framesCap * 2 : 8;
|
|
600
|
+
|
|
601
|
+
if (newCap > d_m3ContinuationMaxFrames) {
|
|
602
|
+
newCap = d_m3ContinuationMaxFrames;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (newCap == cont->framesCap) {
|
|
606
|
+
return m3Err_trapStackOverflow;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
M3Frame* frames = m3_ReallocArray(M3Frame, cont->frames, newCap, cont->framesCap);
|
|
610
|
+
|
|
611
|
+
if (not frames) {
|
|
612
|
+
return m3Err_mallocFailed;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
cont->frames = frames;
|
|
616
|
+
cont->framesCap = newCap;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
cont->frames[cont->numFrames++] = *i_frame;
|
|
620
|
+
|
|
621
|
+
return m3Err_continuationSuspended;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
#endif // d_m3HasStackSwitching
|
|
625
|
+
|
|
626
|
+
M3Result EvaluateExpression (IM3Module i_module, void* o_expressed, m3type_t i_type, bytes_t* io_bytes, cbytes_t i_end)
|
|
627
|
+
{
|
|
628
|
+
M3Result result = m3Err_none;
|
|
629
|
+
|
|
630
|
+
// OPTZ: use a simplified interpreter for expressions
|
|
631
|
+
|
|
632
|
+
// create a temporary runtime context
|
|
633
|
+
#if defined(d_m3PreferStaticAlloc)
|
|
634
|
+
static M3Runtime runtime;
|
|
635
|
+
#else
|
|
636
|
+
M3Runtime runtime;
|
|
637
|
+
#endif
|
|
638
|
+
M3_INIT(runtime);
|
|
639
|
+
|
|
640
|
+
runtime.environment = i_module->runtime->environment;
|
|
641
|
+
runtime.numStackSlots = i_module->runtime->numStackSlots;
|
|
642
|
+
runtime.stack = i_module->runtime->stack;
|
|
643
|
+
|
|
644
|
+
m3stack_t stack = (m3stack_t)runtime.stack;
|
|
645
|
+
|
|
646
|
+
IM3Runtime savedRuntime = i_module->runtime;
|
|
647
|
+
i_module->runtime = &runtime;
|
|
648
|
+
|
|
649
|
+
IM3Compilation o = &runtime.compilation;
|
|
650
|
+
o->runtime = &runtime;
|
|
651
|
+
o->module = i_module;
|
|
652
|
+
o->wasm = *io_bytes;
|
|
653
|
+
o->wasmEnd = i_end;
|
|
654
|
+
o->lastOpcodeStart = o->wasm;
|
|
655
|
+
|
|
656
|
+
// Borrowed for the length of this expression only. Runtime_Release below hands it
|
|
657
|
+
// to Environment_ReleaseCodePages, which resets lineIndex, so the page comes back
|
|
658
|
+
// empty and the next expression reuses it rather than burning fresh lines.
|
|
659
|
+
o->page = AcquireCodePage(&runtime);
|
|
660
|
+
|
|
661
|
+
if (o->page) {
|
|
662
|
+
IM3FuncType ftype = NULL;
|
|
663
|
+
|
|
664
|
+
#if d_m3HasTypedRefs
|
|
665
|
+
// A reference type has to be checked as the type it is: the shared
|
|
666
|
+
// return types are one per storage class, so borrowing one of those
|
|
667
|
+
// would let any reference at all satisfy a particular one.
|
|
668
|
+
if (IsSpelledRefType(i_type)) {
|
|
669
|
+
result = AllocFuncType(&ftype, 1);
|
|
670
|
+
|
|
671
|
+
if (not result) {
|
|
672
|
+
ftype->numRets = 1;
|
|
673
|
+
ftype->types[0] = i_type;
|
|
674
|
+
|
|
675
|
+
result = Environment_AddFuncType(runtime.environment, &ftype);
|
|
676
|
+
}
|
|
677
|
+
} else
|
|
678
|
+
#endif
|
|
679
|
+
{
|
|
680
|
+
ftype = runtime.environment->retFuncTypes[BaseTypeOf(i_type)];
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
pc_t m3code = GetPagePC(o->page);
|
|
684
|
+
|
|
685
|
+
if (not result) {
|
|
686
|
+
result = CompileExpression(o, ftype);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
if (not result && o->maxStackSlots >= runtime.numStackSlots) {
|
|
690
|
+
result = m3Err_trapStackOverflow;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
if (not result) {
|
|
694
|
+
#if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
|
|
695
|
+
m3ret_t r = RunCode(m3code, stack, NULL, d_m3OpDefaultArgs, d_m3BaseCstr);
|
|
696
|
+
#else
|
|
697
|
+
m3ret_t r = RunCode(m3code, stack, NULL, d_m3OpDefaultArgs);
|
|
698
|
+
#endif
|
|
699
|
+
|
|
700
|
+
if (r == 0) { m3log (runtime, "expression result: %s", SPrintValue (stack, i_type));
|
|
701
|
+
if (SizeOfType(BaseTypeOf(i_type)) == sizeof(u32)) {
|
|
702
|
+
*(u32*)o_expressed = *((u32*)stack);
|
|
703
|
+
} else {
|
|
704
|
+
*(u64*)o_expressed = *((u64*)stack);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
ReleaseCodePage(&runtime, o->page);
|
|
710
|
+
} else {
|
|
711
|
+
result = m3Err_mallocFailedCodePage;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
runtime.originStack = NULL; // prevent free(stack) in ReleaseRuntime
|
|
715
|
+
Runtime_Release(&runtime);
|
|
716
|
+
i_module->runtime = savedRuntime;
|
|
717
|
+
|
|
718
|
+
*io_bytes = o->wasm;
|
|
719
|
+
|
|
720
|
+
return result;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
725
|
+
// Linking a module's imports against the exports of the modules already loaded
|
|
726
|
+
// into the same runtime, matched on the name a module was registered under.
|
|
727
|
+
//
|
|
728
|
+
// This is best-effort: an import nothing satisfies is left alone rather than
|
|
729
|
+
// rejected, because a host function may still be bound to it after the module
|
|
730
|
+
// is loaded (m3_LinkRawFunction needs the runtime, so it cannot run earlier),
|
|
731
|
+
// and because an unsatisfiable memory or global still has to be backed by
|
|
732
|
+
// something for the module to be loadable at all.
|
|
733
|
+
//---------------------------------------------------------------------------------------------------------------------------------
|
|
734
|
+
|
|
735
|
+
// Whether an exporting memory or table satisfies what an import asks for. The
|
|
736
|
+
// exporter's *current* size is its minimum - one that has been grown satisfies
|
|
737
|
+
// a larger import than its declaration would - and it may be no less bounded.
|
|
738
|
+
static
|
|
739
|
+
bool LimitsSatisfy (u64 i_exportedSize, bool i_exportedHasMax, u64 i_exportedMax,
|
|
740
|
+
u64 i_importMin, bool i_importHasMax, u64 i_importMax)
|
|
741
|
+
{
|
|
742
|
+
if (i_exportedSize < i_importMin) {
|
|
743
|
+
return false;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
if (i_importHasMax and (not i_exportedHasMax or i_exportedMax > i_importMax)) {
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
return true;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
// The export of this kind under this name, or NULL. The index it carries is
|
|
755
|
+
// resolved by the caller, against the module's index space as it stands then -
|
|
756
|
+
// after linking, a re-exported import's slot holds the entity it was linked to.
|
|
757
|
+
static
|
|
758
|
+
const M3Export* Module_FindExport (IM3Module i_module, u8 i_kind, cstr_t i_name)
|
|
759
|
+
{
|
|
760
|
+
for (u32 i = 0; i < i_module->numExports; ++i) {
|
|
761
|
+
const M3Export* e = &i_module->exports[i];
|
|
762
|
+
|
|
763
|
+
if (e->kind == i_kind and strcmp(e->name, i_name) == 0) {
|
|
764
|
+
return e;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
return NULL;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
static
|
|
773
|
+
IM3Function Module_FindExportedFunction (IM3Module i_module, cstr_t i_name)
|
|
774
|
+
{
|
|
775
|
+
const M3Export* e = Module_FindExport(i_module, d_externalKind_function, i_name);
|
|
776
|
+
|
|
777
|
+
// A module that re-exports an import names the placeholder here. Resolve to
|
|
778
|
+
// the function that actually runs, or a host-side call would run it against
|
|
779
|
+
// the importing module's memory.
|
|
780
|
+
return e ? Function_Implementation(&i_module->functions[e->index]) : NULL;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
static
|
|
785
|
+
IM3Memory Module_FindExportedMemory (IM3Module i_module, cstr_t i_name)
|
|
786
|
+
{
|
|
787
|
+
const M3Export* e = Module_FindExport(i_module, d_externalKind_memory, i_name);
|
|
788
|
+
|
|
789
|
+
return e ? i_module->memories[e->index] : NULL;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
static
|
|
794
|
+
IM3Table Module_FindExportedTable (IM3Module i_module, cstr_t i_name)
|
|
795
|
+
{
|
|
796
|
+
const M3Export* e = Module_FindExport(i_module, d_externalKind_table, i_name);
|
|
797
|
+
|
|
798
|
+
return e ? i_module->tables[e->index] : NULL;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
static
|
|
803
|
+
IM3Global Module_FindExportedGlobal (IM3Module i_module, cstr_t i_name)
|
|
804
|
+
{
|
|
805
|
+
const M3Export* e = Module_FindExport(i_module, d_externalKind_global, i_name);
|
|
806
|
+
|
|
807
|
+
return e ? &i_module->globals[e->index] : NULL;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
#if d_m3HasExceptionHandling || d_m3HasStackSwitching
|
|
812
|
+
static
|
|
813
|
+
IM3Tag Module_FindExportedTag (IM3Module i_module, cstr_t i_name)
|
|
814
|
+
{
|
|
815
|
+
const M3Export* e = Module_FindExport(i_module, d_externalKind_tag, i_name);
|
|
816
|
+
|
|
817
|
+
if (not e) {
|
|
818
|
+
return NULL;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
IM3Tag tag = &i_module->tags[e->index];
|
|
822
|
+
|
|
823
|
+
return tag->resolved ? tag->resolved : tag;
|
|
824
|
+
}
|
|
825
|
+
#endif
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
// Whether the module exports anything at all under this name. Export names are
|
|
829
|
+
// unique within a module, so a name one of the lookups above missed but this
|
|
830
|
+
// one finds is exported as something else - a kind the import cannot be
|
|
831
|
+
// satisfied by, rather than a name the module never exported.
|
|
832
|
+
static
|
|
833
|
+
bool Module_HasExport (IM3Module i_module, cstr_t i_name)
|
|
834
|
+
{
|
|
835
|
+
for (u32 i = 0; i < i_module->numExports; ++i) {
|
|
836
|
+
if (strcmp(i_module->exports[i].name, i_name) == 0) {
|
|
837
|
+
return true;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
return false;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
// Points each of the module's imports at whatever already-loaded module exports
|
|
846
|
+
// it. Runs before anything is allocated or initialized: a memory import has to
|
|
847
|
+
// be resolved before InitMemory would give it pages of its own, and a global
|
|
848
|
+
// import before InitGlobals runs an initializer that reads it.
|
|
849
|
+
static
|
|
850
|
+
M3Result LinkImports (IM3Runtime io_runtime, IM3Module io_module)
|
|
851
|
+
{
|
|
852
|
+
M3Result result = m3Err_none;
|
|
853
|
+
|
|
854
|
+
for (u32 i = 0; i < io_module->numFunctions; ++i) {
|
|
855
|
+
IM3Function f = &io_module->functions[i];
|
|
856
|
+
|
|
857
|
+
if (f->wasm or not (f->import.moduleUtf8 and f->import.fieldUtf8)) {
|
|
858
|
+
continue;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
IM3Module from = m3_FindModule(io_runtime, f->import.moduleUtf8);
|
|
862
|
+
if (not from) {
|
|
863
|
+
continue;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
IM3Function exported = Module_FindExportedFunction(from, f->import.fieldUtf8);
|
|
867
|
+
if (not exported) {
|
|
868
|
+
// the import names a module that is loaded, so its exports settle the
|
|
869
|
+
// question: a name it exports as something else is a type mismatch, and
|
|
870
|
+
// one it does not export at all is an unknown import
|
|
871
|
+
_throwif(m3Err_incompatibleImportType, Module_HasExport(from, f->import.fieldUtf8));
|
|
872
|
+
_throw(m3Err_unknownImport);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// func types are canonical within an environment, so this is the
|
|
876
|
+
// structural equivalence the spec asks for
|
|
877
|
+
_throwif(m3Err_incompatibleImportType, exported->funcType != f->funcType);
|
|
878
|
+
|
|
879
|
+
f->resolved = Function_Implementation(exported);
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
for (u32 i = 0; i < io_module->numMemories; ++i) {
|
|
883
|
+
IM3Memory memory = io_module->memories[i];
|
|
884
|
+
|
|
885
|
+
if (not memory->imported or memory->owner != io_module) {
|
|
886
|
+
continue;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
IM3Module from = m3_FindModule(io_runtime, memory->import.moduleUtf8);
|
|
890
|
+
if (not from) {
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
IM3Memory exported = Module_FindExportedMemory(from, memory->import.fieldUtf8);
|
|
895
|
+
if (not exported) {
|
|
896
|
+
_throwif(m3Err_incompatibleImportType, Module_HasExport(from, memory->import.fieldUtf8));
|
|
897
|
+
_throw(m3Err_unknownImport);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// the address type is part of the memory type, so an i64 memory does
|
|
901
|
+
// not satisfy an i32 import, or the other way round - and so is the page
|
|
902
|
+
// size, which custom page sizes made a declared property of a memory
|
|
903
|
+
_throwif(m3Err_incompatibleImportType, exported->isMemory64 != memory->isMemory64);
|
|
904
|
+
_throwif(m3Err_incompatibleImportType, Memory_PageSize(exported) != Memory_PageSize(memory));
|
|
905
|
+
|
|
906
|
+
// a shared memory is not a subtype of an unshared one, or the other way round
|
|
907
|
+
_throwif(m3Err_incompatibleImportType, exported->isShared != memory->isShared);
|
|
908
|
+
|
|
909
|
+
_throwif(m3Err_incompatibleImportType,
|
|
910
|
+
not LimitsSatisfy(exported->numPages, exported->hasMax, exported->maxPages,
|
|
911
|
+
memory->initPages, memory->hasMax, memory->maxPages));
|
|
912
|
+
|
|
913
|
+
// hand the slot over to the exporter's memory, and drop the placeholder
|
|
914
|
+
FreeMemoryBlock(memory);
|
|
915
|
+
FreeImportInfo(&memory->import);
|
|
916
|
+
m3_Free(memory);
|
|
917
|
+
|
|
918
|
+
io_module->memories[i] = exported;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
for (u32 i = 0; i < io_module->numTables; ++i) {
|
|
922
|
+
IM3Table table = io_module->tables[i];
|
|
923
|
+
|
|
924
|
+
if (not table->imported or table->owner != io_module) {
|
|
925
|
+
continue;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
IM3Module from = m3_FindModule(io_runtime, table->import.moduleUtf8);
|
|
929
|
+
if (not from) {
|
|
930
|
+
continue;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
IM3Table exported = Module_FindExportedTable(from, table->import.fieldUtf8);
|
|
934
|
+
if (not exported) {
|
|
935
|
+
_throwif(m3Err_incompatibleImportType, Module_HasExport(from, table->import.fieldUtf8));
|
|
936
|
+
_throw(m3Err_unknownImport);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
_throwif(m3Err_incompatibleImportType, exported->type != table->type);
|
|
940
|
+
|
|
941
|
+
// the index type is part of the table type, the same way it is for a memory
|
|
942
|
+
_throwif(m3Err_incompatibleImportType, exported->isTable64 != table->isTable64);
|
|
943
|
+
|
|
944
|
+
_throwif(m3Err_incompatibleImportType,
|
|
945
|
+
not LimitsSatisfy(exported->size, exported->hasMax, exported->maxSize,
|
|
946
|
+
table->initSize, table->hasMax, table->maxSize));
|
|
947
|
+
|
|
948
|
+
// hand the slot over to the exporter's table, and drop the placeholder
|
|
949
|
+
m3_Free(table->elements);
|
|
950
|
+
FreeImportInfo(&table->import);
|
|
951
|
+
m3_Free(table);
|
|
952
|
+
|
|
953
|
+
io_module->tables[i] = exported;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
for (u32 i = 0; i < io_module->numGlobals; ++i) {
|
|
957
|
+
IM3Global g = &io_module->globals[i];
|
|
958
|
+
|
|
959
|
+
if (not g->imported or not (g->import.moduleUtf8 and g->import.fieldUtf8)) {
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
IM3Module from = m3_FindModule(io_runtime, g->import.moduleUtf8);
|
|
964
|
+
if (not from) {
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
IM3Global exported = Module_FindExportedGlobal(from, g->import.fieldUtf8);
|
|
969
|
+
if (not exported) {
|
|
970
|
+
_throwif(m3Err_incompatibleImportType, Module_HasExport(from, g->import.fieldUtf8));
|
|
971
|
+
_throw(m3Err_unknownImport);
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
_throwif(m3Err_incompatibleImportType, exported->type != g->type);
|
|
975
|
+
_throwif(m3Err_incompatibleImportType, exported->isMutable != g->isMutable);
|
|
976
|
+
|
|
977
|
+
g->resolved = exported->resolved ? exported->resolved : exported;
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
#if d_m3HasExceptionHandling || d_m3HasStackSwitching
|
|
981
|
+
for (u32 i = 0; i < io_module->numTags; ++i) {
|
|
982
|
+
IM3Tag tag = &io_module->tags[i];
|
|
983
|
+
|
|
984
|
+
if (not tag->imported or not (tag->import.moduleUtf8 and tag->import.fieldUtf8)) {
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
IM3Module from = m3_FindModule(io_runtime, tag->import.moduleUtf8);
|
|
989
|
+
if (not from) {
|
|
990
|
+
continue;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
IM3Tag exported = Module_FindExportedTag(from, tag->import.fieldUtf8);
|
|
994
|
+
if (not exported) {
|
|
995
|
+
_throwif(m3Err_incompatibleImportType, Module_HasExport(from, tag->import.fieldUtf8));
|
|
996
|
+
_throw(m3Err_unknownImport);
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
_throwif(m3Err_incompatibleImportType, exported->type != tag->type);
|
|
1000
|
+
|
|
1001
|
+
tag->resolved = exported->resolved ? exported->resolved : exported;
|
|
1002
|
+
}
|
|
1003
|
+
#endif
|
|
1004
|
+
|
|
1005
|
+
_catch: return result;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
// Backs each of the module's memories with pages. LinkImports has already
|
|
1010
|
+
// pointed any import it could satisfy at the exporting module's memory, and
|
|
1011
|
+
// those are skipped here. An import nothing satisfied is still backed locally
|
|
1012
|
+
// from its own declared limits, so that the module remains loadable.
|
|
1013
|
+
M3Result InitMemory (IM3Runtime io_runtime, IM3Module i_module)
|
|
1014
|
+
{
|
|
1015
|
+
M3Result result = m3Err_none;
|
|
1016
|
+
|
|
1017
|
+
// Fixed from here on: the index space stops changing after parse, and
|
|
1018
|
+
// linking has already repointed any slot it was going to.
|
|
1019
|
+
i_module->memory0 = i_module->numMemories ? i_module->memories[0]
|
|
1020
|
+
: &i_module->emptyMemory;
|
|
1021
|
+
|
|
1022
|
+
if (i_module->numMemories == 0) {
|
|
1023
|
+
// nothing addressable, but _mem still has to point somewhere
|
|
1024
|
+
i_module->emptyMemory.owner = i_module;
|
|
1025
|
+
i_module->emptyMemory.pageSize = d_m3DefaultMemPageSize;
|
|
1026
|
+
|
|
1027
|
+
_ (ResizeMemory(io_runtime, &i_module->emptyMemory, 0));
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
for (u32 i = 0; i < i_module->numMemories; ++i) {
|
|
1031
|
+
IM3Memory memory = i_module->memories[i];
|
|
1032
|
+
|
|
1033
|
+
// a slot that already points at another module's memory is that
|
|
1034
|
+
// module's to allocate
|
|
1035
|
+
if (memory->owner != i_module or memory->mallocated) {
|
|
1036
|
+
continue;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
u32 pageSize = Memory_PageSize(memory);
|
|
1040
|
+
|
|
1041
|
+
memory->pageSize = pageSize;
|
|
1042
|
+
|
|
1043
|
+
// Without a declared maximum a memory may grow to the spec limit of
|
|
1044
|
+
// 2^|addrtype|/pagesize pages, which is the usual 65536 at the default
|
|
1045
|
+
// page size, 2^48 for a 64-bit memory, and a whole address space of
|
|
1046
|
+
// them when a page is a single byte. A declared maximum of zero is a
|
|
1047
|
+
// real limit, not the absence of one.
|
|
1048
|
+
//
|
|
1049
|
+
// 2^64/pagesize overflows a u64 only when a page is a single byte, and
|
|
1050
|
+
// a power-of-two page size divides the address space exactly, so the
|
|
1051
|
+
// 64-bit division below is 2^64/pagesize written so that it fits.
|
|
1052
|
+
if (not memory->hasMax) {
|
|
1053
|
+
memory->maxPages = memory->isMemory64
|
|
1054
|
+
? (pageSize > 1 ? (UINT64_MAX / pageSize) + 1 : UINT64_MAX)
|
|
1055
|
+
: (0x100000000ull / pageSize);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
_ (ResizeMemory(io_runtime, memory, memory->initPages));
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
_catch: return result;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
M3Result ResizeMemory (IM3Runtime io_runtime, IM3Memory memory, u64 i_numPages)
|
|
1066
|
+
{
|
|
1067
|
+
M3Result result = m3Err_none;
|
|
1068
|
+
|
|
1069
|
+
u64 numPagesToAlloc = i_numPages;
|
|
1070
|
+
|
|
1071
|
+
if (numPagesToAlloc <= memory->maxPages) {
|
|
1072
|
+
// A 64-bit memory may ask for up to 2^48 pages, which overflows a u64
|
|
1073
|
+
// of bytes. Nothing that large can be backed, so refuse it up front
|
|
1074
|
+
// rather than multiplying into a wrapped size.
|
|
1075
|
+
_throwif("linear memory limitation exceeded",
|
|
1076
|
+
numPagesToAlloc > d_m3AddressLimit / memory->pageSize);
|
|
1077
|
+
|
|
1078
|
+
u64 numPageBytes = numPagesToAlloc * memory->pageSize;
|
|
1079
|
+
|
|
1080
|
+
#if d_m3MaxLinearMemoryPages > 0
|
|
1081
|
+
// the limit is a memory size, counted in default-sized pages; comparing
|
|
1082
|
+
// it against a raw page count would make it 65536 times stricter for a
|
|
1083
|
+
// module whose pages are one byte
|
|
1084
|
+
_throwif("linear memory limitation exceeded",
|
|
1085
|
+
numPageBytes > (u64)d_m3MaxLinearMemoryPages * d_m3DefaultMemPageSize);
|
|
1086
|
+
#endif
|
|
1087
|
+
|
|
1088
|
+
// Limit the amount of memory that gets actually allocated
|
|
1089
|
+
if (io_runtime->memoryLimit) {
|
|
1090
|
+
numPageBytes = M3_MIN(numPageBytes, (u64)io_runtime->memoryLimit);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
u64 oldLength = memory->mallocated ? memory->mallocated->length : 0;
|
|
1094
|
+
u64 otherUsed = io_runtime->memoryBytesUsed - oldLength;
|
|
1095
|
+
_throwif(m3Err_memoryLimitExceeded, io_runtime->memoryBytesLimit and
|
|
1096
|
+
(numPageBytes > io_runtime->memoryBytesLimit or
|
|
1097
|
+
otherUsed > io_runtime->memoryBytesLimit - numPageBytes));
|
|
1098
|
+
|
|
1099
|
+
_throwif("linear memory limitation exceeded", numPageBytes > (u64)SIZE_MAX - sizeof(M3MemoryHeader));
|
|
1100
|
+
|
|
1101
|
+
#if d_m3GuardedMemory
|
|
1102
|
+
|
|
1103
|
+
// A slot holds every address a Wasm access can name, and nothing beyond
|
|
1104
|
+
// that: a memory larger than the data part of one would put addresses past
|
|
1105
|
+
// the guard, where a fault is nobody's to recognize
|
|
1106
|
+
_throwif("linear memory limitation exceeded", numPageBytes > d_m3GuardedDataBytes);
|
|
1107
|
+
|
|
1108
|
+
// What makes an access past the end fault is the page after the memory not
|
|
1109
|
+
// being committed, so the end has to fall on one of the system's page
|
|
1110
|
+
// boundaries. A Wasm page divides evenly into them and always will; a memory
|
|
1111
|
+
// that declared a page size of its own may not, and then the bytes between
|
|
1112
|
+
// the end of the memory and the end of its last system page would answer
|
|
1113
|
+
// instead of trapping. Refusing to back such a memory is the honest way out:
|
|
1114
|
+
// the custom page sizes proposal exists for targets far too small to reserve
|
|
1115
|
+
// 8GiB per memory in the first place. See d_m3GuardedMemory.
|
|
1116
|
+
_throwif("guarded memory needs a page size the system's divides into",
|
|
1117
|
+
(memory->pageSize % m3_HostPageSize()) != 0);
|
|
1118
|
+
|
|
1119
|
+
if (memory->guardSlot == NULL) {
|
|
1120
|
+
memory->guardSlot = Guard_TakeSlot();
|
|
1121
|
+
_throwifnull(memory->guardSlot);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
// Growing only ever commits more of the same slot, so the memory never
|
|
1125
|
+
// moves - which the interpreter does not rely on, but nothing minds
|
|
1126
|
+
_throwif(m3Err_mallocFailed, not Guard_CommitSlot(memory->guardSlot, (size_t)numPageBytes));
|
|
1127
|
+
|
|
1128
|
+
memory->mallocated = Guard_SlotHeader(memory->guardSlot);
|
|
1129
|
+
|
|
1130
|
+
#else
|
|
1131
|
+
|
|
1132
|
+
size_t numBytes = (size_t)numPageBytes + sizeof(M3MemoryHeader);
|
|
1133
|
+
|
|
1134
|
+
// What is really there, which is not numPages * pageSize: the limit above
|
|
1135
|
+
// caps the bytes without capping the page count, so a memory can be short
|
|
1136
|
+
// of a page - by design, since a whole 64 KiB page is often more than an
|
|
1137
|
+
// MCU has. Measuring the old block by the page count would overstate it,
|
|
1138
|
+
// and then realloc never sees the size it already has and copies the
|
|
1139
|
+
// whole limit again on every grow past it.
|
|
1140
|
+
size_t numPreviousBytes = memory->mallocated ? memory->mallocated->length + sizeof(M3MemoryHeader) : 0;
|
|
1141
|
+
|
|
1142
|
+
void* newMem = m3_Realloc("Wasm Linear Memory", memory->mallocated, numBytes, numPreviousBytes);
|
|
1143
|
+
_throwifnull(newMem);
|
|
1144
|
+
|
|
1145
|
+
memory->mallocated = (M3MemoryHeader*)newMem;
|
|
1146
|
+
|
|
1147
|
+
#endif
|
|
1148
|
+
|
|
1149
|
+
#if d_m3LogRuntime
|
|
1150
|
+
M3MemoryHeader* oldMallocated = memory->mallocated;
|
|
1151
|
+
#endif
|
|
1152
|
+
|
|
1153
|
+
io_runtime->memoryBytesUsed = otherUsed + numPageBytes;
|
|
1154
|
+
memory->numPages = numPagesToAlloc;
|
|
1155
|
+
|
|
1156
|
+
memory->mallocated->length = (size_t)numPageBytes;
|
|
1157
|
+
memory->mallocated->runtime = io_runtime;
|
|
1158
|
+
memory->mallocated->memory = memory;
|
|
1159
|
+
|
|
1160
|
+
memory->mallocated->maxStack = (m3slot_t*)io_runtime->originStack + io_runtime->numStackSlots;
|
|
1161
|
+
|
|
1162
|
+
m3log(runtime, "resized old: %p; mem: %p; length: %zu; pages: %llu", oldMallocated, memory->mallocated, memory->mallocated->length, (unsigned long long)memory->numPages);
|
|
1163
|
+
} else {
|
|
1164
|
+
result = m3Err_wasmMemoryOverflow;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
_catch: return result;
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
void FreeMemoryBlock (IM3Memory io_memory)
|
|
1172
|
+
{
|
|
1173
|
+
if (io_memory->mallocated and io_memory->mallocated->runtime) {
|
|
1174
|
+
io_memory->mallocated->runtime->memoryBytesUsed -= io_memory->mallocated->length;
|
|
1175
|
+
}
|
|
1176
|
+
#if d_m3GuardedMemory
|
|
1177
|
+
Guard_GiveSlot(io_memory->guardSlot);
|
|
1178
|
+
io_memory->guardSlot = NULL;
|
|
1179
|
+
|
|
1180
|
+
io_memory->mallocated = NULL;
|
|
1181
|
+
#else
|
|
1182
|
+
m3_Free(io_memory->mallocated);
|
|
1183
|
+
#endif
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
M3Result InitGlobals (IM3Module io_module)
|
|
1188
|
+
{
|
|
1189
|
+
M3Result result = m3Err_none;
|
|
1190
|
+
|
|
1191
|
+
if (io_module->numGlobals) {
|
|
1192
|
+
// placing the globals in their structs isn't good for cache locality, but i don't really know what the global
|
|
1193
|
+
// access patterns typically look like yet.
|
|
1194
|
+
|
|
1195
|
+
// io_module->globalMemory = m3Alloc (m3reg_t, io_module->numGlobals);
|
|
1196
|
+
|
|
1197
|
+
// if (io_module->globalMemory)
|
|
1198
|
+
{
|
|
1199
|
+
for (u32 i = 0; i < io_module->numGlobals; ++i) {
|
|
1200
|
+
M3Global* g = &io_module->globals[i]; m3log (runtime, "initializing global: %d", i);
|
|
1201
|
+
|
|
1202
|
+
if (g->initExpr) {
|
|
1203
|
+
bytes_t start = g->initExpr;
|
|
1204
|
+
|
|
1205
|
+
result = EvaluateExpression(io_module, &g->i64Value, g->type, &start, g->initExpr + g->initExprSize);
|
|
1206
|
+
|
|
1207
|
+
if (not result) {
|
|
1208
|
+
// io_module->globalMemory [i] = initValue;
|
|
1209
|
+
} else {
|
|
1210
|
+
break;
|
|
1211
|
+
}
|
|
1212
|
+
} else { m3log (runtime, "importing global");
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
// else result = ErrorModule (m3Err_mallocFailed, io_module, "could allocate globals for module: '%s", io_module->name);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
return result;
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
M3Result InitDataSegments (IM3Module io_module)
|
|
1224
|
+
{
|
|
1225
|
+
M3Result result = m3Err_none;
|
|
1226
|
+
|
|
1227
|
+
for (u32 i = 0; i < io_module->numDataSegments; ++i) {
|
|
1228
|
+
M3DataSegment* segment = &io_module->dataSegments[i];
|
|
1229
|
+
|
|
1230
|
+
// A passive segment stays available for memory.init until data.drop.
|
|
1231
|
+
// An active one is copied here and then counts as dropped.
|
|
1232
|
+
if (segment->isPassive) {
|
|
1233
|
+
continue;
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
_throwif("data segment memory index out of range",
|
|
1237
|
+
segment->memoryRegion >= io_module->numMemories);
|
|
1238
|
+
|
|
1239
|
+
IM3Memory io_memory = io_module->memories[segment->memoryRegion];
|
|
1240
|
+
|
|
1241
|
+
_throwif("unallocated linear memory", !(io_memory->mallocated));
|
|
1242
|
+
|
|
1243
|
+
// The offset expression has the memory's address type, and is
|
|
1244
|
+
// unsigned: an i32 offset of -1 is 4294967295, way out of bounds
|
|
1245
|
+
// rather than negative.
|
|
1246
|
+
u64 segmentOffset = 0;
|
|
1247
|
+
bytes_t start = segment->initExpr;
|
|
1248
|
+
|
|
1249
|
+
if (io_memory->isMemory64) {
|
|
1250
|
+
_ (EvaluateExpression(io_module, &segmentOffset, c_m3Type_i64, &start, segment->initExpr + segment->initExprSize));
|
|
1251
|
+
} else {
|
|
1252
|
+
u32 offset32;
|
|
1253
|
+
_ (EvaluateExpression(io_module, &offset32, c_m3Type_i32, &start, segment->initExpr + segment->initExprSize));
|
|
1254
|
+
segmentOffset = offset32;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
m3log(runtime, "loading data segment: %d; size: %d; offset: %llu", i, segment->size, (unsigned long long)segmentOffset);
|
|
1258
|
+
|
|
1259
|
+
if (segmentOffset <= io_memory->mallocated->length &&
|
|
1260
|
+
(u64)segment->size <= io_memory->mallocated->length - segmentOffset) {
|
|
1261
|
+
u8* dest = m3MemData(io_memory->mallocated) + segmentOffset;
|
|
1262
|
+
memcpy(dest, segment->data, segment->size);
|
|
1263
|
+
} else {
|
|
1264
|
+
_throw("data segment out of bounds");
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
segment->dropped = true;
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
_catch: return result;
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
// Turns a segment's elements into references. Element expressions are constant
|
|
1275
|
+
// expressions restricted to ref.null/ref.func, so they're read directly rather
|
|
1276
|
+
// than run through the compiler.
|
|
1277
|
+
static
|
|
1278
|
+
M3Result ResolveElements (IM3Module io_module, M3ElementSegment* i_segment, void** o_elements)
|
|
1279
|
+
{
|
|
1280
|
+
M3Result result = m3Err_none;
|
|
1281
|
+
|
|
1282
|
+
bytes_t pos = i_segment->elements;
|
|
1283
|
+
cbytes_t end = io_module->elementSectionEnd;
|
|
1284
|
+
|
|
1285
|
+
for (u32 e = 0; e < i_segment->numElements; ++e) {
|
|
1286
|
+
u32 funcIndex;
|
|
1287
|
+
void* ref = NULL;
|
|
1288
|
+
|
|
1289
|
+
if (i_segment->isExpr) {
|
|
1290
|
+
m3opcode_t opcode;
|
|
1291
|
+
_ (Read_opcode(&opcode, &pos, end));
|
|
1292
|
+
|
|
1293
|
+
if (opcode == c_waOp_refFunc) {
|
|
1294
|
+
_ (ReadLEB_u32(&funcIndex, &pos, end));
|
|
1295
|
+
_throwif("function index out of range", funcIndex >= io_module->numFunctions);
|
|
1296
|
+
ref = Function_Implementation(&io_module->functions[funcIndex]);
|
|
1297
|
+
} else if (opcode == c_waOp_refNull) {
|
|
1298
|
+
i8 waType;
|
|
1299
|
+
u8 nullType;
|
|
1300
|
+
_ (ReadLEB_i7(&waType, &pos, end));
|
|
1301
|
+
_ (NormalizeType(&nullType, waType));
|
|
1302
|
+
_throwif(m3Err_typeMismatch, nullType != i_segment->type);
|
|
1303
|
+
} else if (opcode == c_waOp_getGlobal) {
|
|
1304
|
+
// wasm 2.0 lets an element expression read an imported
|
|
1305
|
+
// immutable global, which is how one module seeds another's
|
|
1306
|
+
// table with a reference it exported.
|
|
1307
|
+
u32 globalIndex;
|
|
1308
|
+
_ (ReadLEB_u32(&globalIndex, &pos, end));
|
|
1309
|
+
_throwif(m3Err_globaIndexOutOfBounds, globalIndex >= io_module->numGlobals);
|
|
1310
|
+
|
|
1311
|
+
IM3Global global = &io_module->globals[globalIndex];
|
|
1312
|
+
|
|
1313
|
+
_throwif(m3Err_globaIndexOutOfBounds, not global->imported);
|
|
1314
|
+
_throwif(m3Err_wasmMalformed, global->isMutable);
|
|
1315
|
+
_throwif(m3Err_typeMismatch, BaseTypeOf(global->type) != BaseTypeOf(i_segment->type));
|
|
1316
|
+
|
|
1317
|
+
// read the cell the import was linked to, not the placeholder
|
|
1318
|
+
if (global->resolved) {
|
|
1319
|
+
global = global->resolved;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
ref = global->refValue;
|
|
1323
|
+
} else {
|
|
1324
|
+
_throw("constant expression required");
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
_ (Read_opcode(&opcode, &pos, end));
|
|
1328
|
+
_throwif(m3Err_wasmMalformed, opcode != c_waOp_end);
|
|
1329
|
+
} else {
|
|
1330
|
+
_ (ReadLEB_u32(&funcIndex, &pos, end));
|
|
1331
|
+
_throwif("function index out of range", funcIndex >= io_module->numFunctions);
|
|
1332
|
+
ref = Function_Implementation(&io_module->functions[funcIndex]);
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
o_elements[e] = ref;
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
_catch: return result;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
M3Result InitTableAndElements (IM3Module io_module)
|
|
1343
|
+
{
|
|
1344
|
+
M3Result result = m3Err_none;
|
|
1345
|
+
|
|
1346
|
+
cbytes_t end = io_module->elementSectionEnd;
|
|
1347
|
+
M3Table* table;
|
|
1348
|
+
|
|
1349
|
+
for (u32 i = 0; i < io_module->numTables; ++i) {
|
|
1350
|
+
table = io_module->tables[i];
|
|
1351
|
+
|
|
1352
|
+
// a slot pointing at another module's table is that module's to fill
|
|
1353
|
+
if (table->owner != io_module) {
|
|
1354
|
+
continue;
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
if (table->size) {
|
|
1358
|
+
IM3Runtime runtime = io_module->runtime;
|
|
1359
|
+
_throwif(m3Err_tableLimitExceeded, runtime->tableElementsLimit and
|
|
1360
|
+
table->size > runtime->tableElementsLimit - runtime->tableElementsUsed);
|
|
1361
|
+
table->elements = m3_AllocArray(void*, table->size);
|
|
1362
|
+
_throwifnull(table->elements);
|
|
1363
|
+
runtime->tableElementsUsed += table->size;
|
|
1364
|
+
|
|
1365
|
+
if (table->initExpr) {
|
|
1366
|
+
void* value = NULL;
|
|
1367
|
+
bytes_t start = table->initExpr;
|
|
1368
|
+
_ (EvaluateExpression(io_module, &value, BaseTypeOf(table->type),
|
|
1369
|
+
&start, table->initExpr + table->initExprSize));
|
|
1370
|
+
|
|
1371
|
+
for (u32 e = 0; e < table->size; ++e) {
|
|
1372
|
+
table->elements[e] = value;
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
for (u32 i = 0; i < io_module->numElementSegments; ++i) {
|
|
1379
|
+
M3ElementSegment* segment = &io_module->elementSegments[i];
|
|
1380
|
+
|
|
1381
|
+
// Declarative segments only make their functions referenceable, and
|
|
1382
|
+
// passive ones wait for table.init, so neither is written out here.
|
|
1383
|
+
if (segment->mode == c_m3Elem_declarative) {
|
|
1384
|
+
segment->dropped = true;
|
|
1385
|
+
continue;
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
if (segment->mode == c_m3Elem_passive) {
|
|
1389
|
+
if (segment->numElements) {
|
|
1390
|
+
segment->resolved = m3_AllocArray(void*, segment->numElements);
|
|
1391
|
+
_throwifnull(segment->resolved);
|
|
1392
|
+
_ (ResolveElements(io_module, segment, segment->resolved));
|
|
1393
|
+
}
|
|
1394
|
+
continue;
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
table = io_module->tables[segment->tableIndex];
|
|
1398
|
+
|
|
1399
|
+
// The offset expression has the table's index type, and is unsigned:
|
|
1400
|
+
// an i32 offset of -1 is 4294967295, out of bounds rather than negative.
|
|
1401
|
+
u64 offset = 0;
|
|
1402
|
+
bytes_t expr = segment->initExpr;
|
|
1403
|
+
|
|
1404
|
+
if (table->isTable64) {
|
|
1405
|
+
_ (EvaluateExpression(io_module, &offset, c_m3Type_i64, &expr, end));
|
|
1406
|
+
} else {
|
|
1407
|
+
u32 offset32;
|
|
1408
|
+
_ (EvaluateExpression(io_module, &offset32, c_m3Type_i32, &expr, end));
|
|
1409
|
+
offset = offset32;
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
_throwif("out of bounds table access",
|
|
1413
|
+
offset > table->size or segment->numElements > table->size - offset);
|
|
1414
|
+
|
|
1415
|
+
_ (ResolveElements(io_module, segment, table->elements + offset));
|
|
1416
|
+
|
|
1417
|
+
segment->dropped = true;
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
_catch: return result;
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
M3Result m3_CompileModule (IM3Module io_module)
|
|
1424
|
+
{
|
|
1425
|
+
M3Result result = m3Err_none;
|
|
1426
|
+
|
|
1427
|
+
for (u32 i = 0; i < io_module->numFunctions; ++i) {
|
|
1428
|
+
IM3Function f = &io_module->functions[i];
|
|
1429
|
+
if (f->wasm and not f->compiled) {
|
|
1430
|
+
_ (CompileFunction(f));
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
_catch: return result;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
#if d_m3HasExceptionHandling
|
|
1438
|
+
|
|
1439
|
+
M3Exception* NewException (IM3Runtime io_runtime, IM3Tag i_tag, u32 i_numArgs)
|
|
1440
|
+
{
|
|
1441
|
+
M3Exception* exception = (M3Exception*)m3_Malloc("M3Exception", sizeof(M3Exception) + i_numArgs * sizeof(u64));
|
|
1442
|
+
|
|
1443
|
+
if (exception) {
|
|
1444
|
+
exception->tag = i_tag;
|
|
1445
|
+
exception->numArgs = i_numArgs;
|
|
1446
|
+
exception->reified = false;
|
|
1447
|
+
exception->prev = NULL;
|
|
1448
|
+
exception->next = io_runtime->exceptions;
|
|
1449
|
+
|
|
1450
|
+
if (exception->next) {
|
|
1451
|
+
exception->next->prev = exception;
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
io_runtime->exceptions = exception;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
return exception;
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
// Releases one exception ahead of the rest. The caller has to know nothing can
|
|
1462
|
+
// still name it: no exnref was ever taken of it, and its payload has already
|
|
1463
|
+
// been copied out.
|
|
1464
|
+
void FreeException (IM3Runtime io_runtime, M3Exception* i_exception)
|
|
1465
|
+
{
|
|
1466
|
+
if (i_exception->prev) {
|
|
1467
|
+
i_exception->prev->next = i_exception->next;
|
|
1468
|
+
} else {
|
|
1469
|
+
io_runtime->exceptions = i_exception->next;
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
if (i_exception->next) {
|
|
1473
|
+
i_exception->next->prev = i_exception->prev;
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
if (io_runtime->pendingException == i_exception) {
|
|
1477
|
+
io_runtime->pendingException = NULL;
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
m3_Free_Impl(i_exception);
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
|
|
1484
|
+
// Releases every exception the runtime still holds. Only safe once the Wasm
|
|
1485
|
+
// stack is empty, which is why the outermost RunCodeChecked() is the one that
|
|
1486
|
+
// calls it.
|
|
1487
|
+
void FreeExceptions (IM3Runtime io_runtime)
|
|
1488
|
+
{
|
|
1489
|
+
M3Exception* exception = io_runtime->exceptions;
|
|
1490
|
+
|
|
1491
|
+
io_runtime->exceptions = NULL;
|
|
1492
|
+
io_runtime->pendingException = NULL;
|
|
1493
|
+
|
|
1494
|
+
while (exception) {
|
|
1495
|
+
M3Exception* next = exception->next;
|
|
1496
|
+
m3_Free_Impl(exception);
|
|
1497
|
+
exception = next;
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
|
|
1502
|
+
// Keeps a copy of the exception a call just ended with, for m3_GetExceptionTag,
|
|
1503
|
+
// or forgets the last one when the call ended any other way. A copy, because
|
|
1504
|
+
// the original is freed with the rest before the outermost call returns - and
|
|
1505
|
+
// one a host function's nested call ended with may still be named by an
|
|
1506
|
+
// exnref further down the stack.
|
|
1507
|
+
static
|
|
1508
|
+
void RecordUncaughtException (IM3Runtime io_runtime, M3Result i_result)
|
|
1509
|
+
{
|
|
1510
|
+
if (io_runtime->uncaughtException) {
|
|
1511
|
+
m3_Free(io_runtime->uncaughtException);
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
M3Exception* exception = io_runtime->pendingException;
|
|
1515
|
+
|
|
1516
|
+
if (M3_UNLIKELY(i_result == m3Err_trapUncaughtException) and exception) {
|
|
1517
|
+
M3Exception* copy = (M3Exception*)m3_CopyMem(exception, sizeof(M3Exception) + exception->numArgs * sizeof(u64));
|
|
1518
|
+
|
|
1519
|
+
if (copy) {
|
|
1520
|
+
copy->next = NULL;
|
|
1521
|
+
copy->prev = NULL;
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
io_runtime->uncaughtException = copy;
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
#endif // d_m3HasExceptionHandling
|
|
1529
|
+
|
|
1530
|
+
|
|
1531
|
+
#if d_m3HasExceptionHandling || d_m3HasStackSwitching
|
|
1532
|
+
|
|
1533
|
+
IM3Tag m3_FindTag (IM3Module i_module, const char* const i_tagName)
|
|
1534
|
+
{
|
|
1535
|
+
if (not i_module or not i_tagName) {
|
|
1536
|
+
return NULL;
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
return Module_FindExportedTag(i_module, i_tagName);
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
M3Result m3_NewTag (IM3Runtime io_runtime, IM3Tag* o_tag, const char* const i_signature)
|
|
1544
|
+
{
|
|
1545
|
+
IM3FuncType type = NULL;
|
|
1546
|
+
M3HostTag* hostTag = NULL;
|
|
1547
|
+
|
|
1548
|
+
_try {
|
|
1549
|
+
_throwif("null tag", not o_tag);
|
|
1550
|
+
*o_tag = NULL;
|
|
1551
|
+
_throwif(m3Err_mallocFailed, not io_runtime);
|
|
1552
|
+
|
|
1553
|
+
_ (SignatureToFuncType(&type, i_signature));
|
|
1554
|
+
// tags are the same type exactly when their types are the same object
|
|
1555
|
+
_ (Environment_AddFuncType(io_runtime->environment, &type));
|
|
1556
|
+
|
|
1557
|
+
hostTag = m3_AllocStruct(M3HostTag);
|
|
1558
|
+
_throwifnull(hostTag);
|
|
1559
|
+
|
|
1560
|
+
hostTag->tag.type = type;
|
|
1561
|
+
hostTag->next = io_runtime->hostTags;
|
|
1562
|
+
io_runtime->hostTags = hostTag;
|
|
1563
|
+
|
|
1564
|
+
*o_tag = &hostTag->tag;
|
|
1565
|
+
} _catch:
|
|
1566
|
+
return result;
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
|
|
1570
|
+
M3Result m3_LinkTag (IM3Module io_module,
|
|
1571
|
+
const char* const i_moduleName,
|
|
1572
|
+
const char* const i_tagName,
|
|
1573
|
+
IM3Tag i_tag)
|
|
1574
|
+
{
|
|
1575
|
+
if (not io_module or not i_moduleName or not i_tagName or not i_tag) {
|
|
1576
|
+
return m3Err_tagLookupFailed;
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
const bool wildcardModule = (strcmp(i_moduleName, "*") == 0);
|
|
1580
|
+
IM3Tag target = i_tag->resolved ? i_tag->resolved : i_tag;
|
|
1581
|
+
|
|
1582
|
+
M3Result result = m3Err_tagLookupFailed;
|
|
1583
|
+
|
|
1584
|
+
for (u32 i = 0; i < io_module->numTags; ++i) {
|
|
1585
|
+
IM3Tag tag = &io_module->tags[i];
|
|
1586
|
+
|
|
1587
|
+
if (tag->imported and tag->import.moduleUtf8 and tag->import.fieldUtf8 and
|
|
1588
|
+
strcmp(tag->import.fieldUtf8, i_tagName) == 0 and
|
|
1589
|
+
(wildcardModule or strcmp(tag->import.moduleUtf8, i_moduleName) == 0)) {
|
|
1590
|
+
if (tag->type != target->type) {
|
|
1591
|
+
return m3Err_incompatibleImportType;
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
tag->resolved = target;
|
|
1595
|
+
result = m3Err_none;
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
return result;
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
|
|
1603
|
+
uint32_t m3_GetTagArgCount (IM3Tag i_tag)
|
|
1604
|
+
{
|
|
1605
|
+
return i_tag ? i_tag->type->numArgs : 0;
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
|
|
1609
|
+
M3ValueType m3_GetTagArgType (IM3Tag i_tag, uint32_t i_index)
|
|
1610
|
+
{
|
|
1611
|
+
if (i_tag and i_index < i_tag->type->numArgs) {
|
|
1612
|
+
return (M3ValueType)BaseTypeOf(d_FuncArgType(i_tag->type, i_index));
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
return c_m3Type_none;
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
#else
|
|
1619
|
+
|
|
1620
|
+
IM3Tag m3_FindTag (IM3Module i_module, const char* const i_tagName)
|
|
1621
|
+
{
|
|
1622
|
+
return NULL;
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
M3Result m3_NewTag (IM3Runtime io_runtime, IM3Tag* o_tag, const char* const i_signature)
|
|
1626
|
+
{
|
|
1627
|
+
if (o_tag) {
|
|
1628
|
+
*o_tag = NULL;
|
|
1629
|
+
}
|
|
1630
|
+
return m3Err_tagLookupFailed;
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
M3Result m3_LinkTag (IM3Module io_module, const char* const i_moduleName, const char* const i_tagName, IM3Tag i_tag)
|
|
1634
|
+
{
|
|
1635
|
+
return m3Err_tagLookupFailed;
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
uint32_t m3_GetTagArgCount (IM3Tag i_tag)
|
|
1639
|
+
{
|
|
1640
|
+
return 0;
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
M3ValueType m3_GetTagArgType (IM3Tag i_tag, uint32_t i_index)
|
|
1644
|
+
{
|
|
1645
|
+
return c_m3Type_none;
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
#endif // d_m3HasExceptionHandling || d_m3HasStackSwitching
|
|
1649
|
+
|
|
1650
|
+
|
|
1651
|
+
#if d_m3HasExceptionHandling
|
|
1652
|
+
|
|
1653
|
+
M3Result m3_ThrowException (IM3Runtime io_runtime, IM3Tag i_tag, uint32_t i_argc, const void* i_argptrs[])
|
|
1654
|
+
{
|
|
1655
|
+
if (not io_runtime or not i_tag) {
|
|
1656
|
+
return m3Err_tagLookupFailed;
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
IM3Tag tag = i_tag->resolved ? i_tag->resolved : i_tag;
|
|
1660
|
+
IM3FuncType type = tag->type;
|
|
1661
|
+
|
|
1662
|
+
// a tag with results is a stack switching control tag, which is suspended
|
|
1663
|
+
// with rather than thrown
|
|
1664
|
+
if (type->numRets) {
|
|
1665
|
+
return m3Err_typeMismatch;
|
|
1666
|
+
}
|
|
1667
|
+
if (i_argc != type->numArgs) {
|
|
1668
|
+
return m3Err_argumentCountMismatch;
|
|
1669
|
+
}
|
|
1670
|
+
// with nothing on the stack there is nothing to unwind, and the exception
|
|
1671
|
+
// would sit on the runtime's list until some later call ended
|
|
1672
|
+
if (io_runtime->callNesting == 0) {
|
|
1673
|
+
return "m3_ThrowException called outside a host function";
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
M3Exception* exception = NewException(io_runtime, tag, i_argc);
|
|
1677
|
+
if (not exception) {
|
|
1678
|
+
return m3Err_mallocFailed;
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
// the layout op_Throw gives a payload: 32-bit values zero-extended, floats
|
|
1682
|
+
// as their bit pattern
|
|
1683
|
+
for (u32 i = 0; i < i_argc; ++i) {
|
|
1684
|
+
const void* value = i_argptrs[i];
|
|
1685
|
+
|
|
1686
|
+
switch (BaseTypeOf(d_FuncArgType(type, i))) {
|
|
1687
|
+
case c_m3Type_i32:
|
|
1688
|
+
case c_m3Type_f32: {
|
|
1689
|
+
u32 bits;
|
|
1690
|
+
memcpy(&bits, value, sizeof(bits));
|
|
1691
|
+
exception->args[i] = bits;
|
|
1692
|
+
break;
|
|
1693
|
+
}
|
|
1694
|
+
case c_m3Type_i64:
|
|
1695
|
+
case c_m3Type_f64:
|
|
1696
|
+
memcpy(&exception->args[i], value, sizeof(u64));
|
|
1697
|
+
break;
|
|
1698
|
+
case c_m3Type_funcref:
|
|
1699
|
+
case c_m3Type_externref:
|
|
1700
|
+
case c_m3Type_exnref:
|
|
1701
|
+
case c_m3Type_contref:
|
|
1702
|
+
exception->args[i] = (u64)(*(const uintptr_t*)value);
|
|
1703
|
+
break;
|
|
1704
|
+
default:
|
|
1705
|
+
FreeException(io_runtime, exception);
|
|
1706
|
+
return "unknown argument type";
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
io_runtime->pendingException = exception;
|
|
1711
|
+
|
|
1712
|
+
return m3Err_pendingException;
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
|
|
1716
|
+
IM3Tag m3_GetExceptionTag (IM3Runtime i_runtime)
|
|
1717
|
+
{
|
|
1718
|
+
if (i_runtime and i_runtime->uncaughtException) {
|
|
1719
|
+
return i_runtime->uncaughtException->tag;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
return NULL;
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
|
|
1726
|
+
M3Result m3_GetExceptionArgs (IM3Runtime i_runtime, uint32_t i_argc, const void* o_argptrs[])
|
|
1727
|
+
{
|
|
1728
|
+
if (not i_runtime or not i_runtime->uncaughtException) {
|
|
1729
|
+
return "no uncaught exception";
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
const M3Exception* exception = i_runtime->uncaughtException;
|
|
1733
|
+
IM3FuncType type = exception->tag->type;
|
|
1734
|
+
|
|
1735
|
+
if (i_argc != exception->numArgs) {
|
|
1736
|
+
return m3Err_argumentCountMismatch;
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
for (u32 i = 0; i < i_argc; ++i) {
|
|
1740
|
+
void* value = (void*)o_argptrs[i];
|
|
1741
|
+
u64 bits = exception->args[i];
|
|
1742
|
+
|
|
1743
|
+
switch (BaseTypeOf(d_FuncArgType(type, i))) {
|
|
1744
|
+
case c_m3Type_i32:
|
|
1745
|
+
case c_m3Type_f32: {
|
|
1746
|
+
u32 low = (u32)bits;
|
|
1747
|
+
memcpy(value, &low, sizeof(low));
|
|
1748
|
+
break;
|
|
1749
|
+
}
|
|
1750
|
+
case c_m3Type_i64:
|
|
1751
|
+
case c_m3Type_f64:
|
|
1752
|
+
memcpy(value, &bits, sizeof(bits));
|
|
1753
|
+
break;
|
|
1754
|
+
case c_m3Type_funcref:
|
|
1755
|
+
case c_m3Type_externref:
|
|
1756
|
+
case c_m3Type_exnref:
|
|
1757
|
+
case c_m3Type_contref:
|
|
1758
|
+
*(uintptr_t*)value = (uintptr_t)bits;
|
|
1759
|
+
break;
|
|
1760
|
+
default:
|
|
1761
|
+
return "unknown argument type";
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
return m3Err_none;
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
#else
|
|
1769
|
+
|
|
1770
|
+
M3Result m3_ThrowException (IM3Runtime io_runtime, IM3Tag i_tag, uint32_t i_argc, const void* i_argptrs[])
|
|
1771
|
+
{
|
|
1772
|
+
return m3Err_tagLookupFailed;
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
IM3Tag m3_GetExceptionTag (IM3Runtime i_runtime)
|
|
1776
|
+
{
|
|
1777
|
+
return NULL;
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
M3Result m3_GetExceptionArgs (IM3Runtime i_runtime, uint32_t i_argc, const void* o_argptrs[])
|
|
1781
|
+
{
|
|
1782
|
+
return "no uncaught exception";
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
#endif // d_m3HasExceptionHandling
|
|
1786
|
+
|
|
1787
|
+
|
|
1788
|
+
#if d_m3GuardedMemory
|
|
1789
|
+
|
|
1790
|
+
// What RunCodeProtected hands to the platform layer to run, and what comes back
|
|
1791
|
+
typedef struct M3ProtectedRun {
|
|
1792
|
+
pc_t pc;
|
|
1793
|
+
IM3Runtime runtime;
|
|
1794
|
+
M3MemoryHeader* mem;
|
|
1795
|
+
M3Result result;
|
|
1796
|
+
} M3ProtectedRun;
|
|
1797
|
+
|
|
1798
|
+
static
|
|
1799
|
+
void RunCodeBody (void* io_context)
|
|
1800
|
+
{
|
|
1801
|
+
M3ProtectedRun* run = (M3ProtectedRun*)io_context;
|
|
1802
|
+
|
|
1803
|
+
# if (d_m3EnableOpProfiling || d_m3EnableOpTracing)
|
|
1804
|
+
run->result = (M3Result)RunCode(run->pc, (m3stack_t)run->runtime->stack, run->mem,
|
|
1805
|
+
d_m3OpDefaultArgs, d_m3BaseCstr);
|
|
1806
|
+
# else
|
|
1807
|
+
run->result = (M3Result)RunCode(run->pc, (m3stack_t)run->runtime->stack, run->mem,
|
|
1808
|
+
d_m3OpDefaultArgs);
|
|
1809
|
+
# endif
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
// The bounds check the load and store operations no longer do, done once here by the
|
|
1813
|
+
// system instead: an address past the end of a memory reaches the uncommitted part
|
|
1814
|
+
// of that memory's reservation, and the fault comes back as the trap the spec asks
|
|
1815
|
+
// for. Nothing is left half-done by it - the Wasm stack and the memories are heap,
|
|
1816
|
+
// and the caller unwinds the rest.
|
|
1817
|
+
static
|
|
1818
|
+
M3Result RunCodeProtected (IM3Runtime i_runtime, pc_t i_pc, M3MemoryHeader* i_mem)
|
|
1819
|
+
{
|
|
1820
|
+
// Memory operations in this build carry no bounds check, so code only runs where a
|
|
1821
|
+
// fault past the end of a memory can still be turned into a trap
|
|
1822
|
+
if (M3_UNLIKELY(not m3_HostGuardsActive())) {
|
|
1823
|
+
return ErrorRuntime(m3Err_memoryGuardsUnavailable, i_runtime,
|
|
1824
|
+
"another part of the process holds the fault signal");
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
M3ProtectedRun run;
|
|
1828
|
+
run.pc = i_pc;
|
|
1829
|
+
run.runtime = i_runtime;
|
|
1830
|
+
run.mem = i_mem;
|
|
1831
|
+
run.result = m3Err_none;
|
|
1832
|
+
|
|
1833
|
+
void* arena;
|
|
1834
|
+
size_t arenaBytes;
|
|
1835
|
+
Guard_ArenaRange(&arena, &arenaBytes);
|
|
1836
|
+
|
|
1837
|
+
if (not m3_HostProtectedCall(RunCodeBody, &run, arena, arenaBytes)) {
|
|
1838
|
+
return m3Err_trapOutOfBoundsMemoryAccess;
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
return run.result;
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
#endif // d_m3GuardedMemory
|
|
1845
|
+
|
|
1846
|
+
|
|
1847
|
+
#if d_m3HasStackSwitching
|
|
1848
|
+
|
|
1849
|
+
// The function a suspended continuation stopped in: the callee of its innermost
|
|
1850
|
+
// call frame, or the function it was entered with if it made no call. Frames are
|
|
1851
|
+
// recorded innermost first, so that is the lowest-numbered call frame.
|
|
1852
|
+
static
|
|
1853
|
+
IM3Function Continuation_InnermostFunction (IM3Continuation i_cont)
|
|
1854
|
+
{
|
|
1855
|
+
for (u32 i = 0; i < i_cont->numFrames; ++i) {
|
|
1856
|
+
if (i_cont->frames[i].kind == frame_call && i_cont->frames[i].call.function) {
|
|
1857
|
+
return i_cont->frames[i].call.function;
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
return i_cont->entryFunction;
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
|
|
1865
|
+
// Where the host's next call puts its arguments and finds its results. A
|
|
1866
|
+
// suspended root keeps its frames on the bottom of the runtime's stack, so a call
|
|
1867
|
+
// made before it is resumed starts above the last of them rather than on top of
|
|
1868
|
+
// them. The top is the innermost function's frame base plus everything that
|
|
1869
|
+
// function can use. That base is the root's own sp, unless it stopped inside a
|
|
1870
|
+
// resume: then the suspension point belongs to the resumed continuation, and the
|
|
1871
|
+
// root's innermost frame is that resume.
|
|
1872
|
+
void Runtime_PlaceCallStack (IM3Runtime io_runtime)
|
|
1873
|
+
{
|
|
1874
|
+
IM3Continuation root = io_runtime->rootContinuation;
|
|
1875
|
+
|
|
1876
|
+
io_runtime->stack = io_runtime->originStack;
|
|
1877
|
+
|
|
1878
|
+
if (not root or root->state != cont_suspended) {
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
m3stack_t sp = root->sp;
|
|
1883
|
+
IM3Function function = Continuation_InnermostFunction(root);
|
|
1884
|
+
|
|
1885
|
+
if (root->numFrames and root->frames[0].kind == frame_resume) {
|
|
1886
|
+
sp = root->frames[0].sp;
|
|
1887
|
+
}
|
|
1888
|
+
|
|
1889
|
+
m3slot_t* base = (m3slot_t*)io_runtime->originStack;
|
|
1890
|
+
m3slot_t* top = (m3slot_t*)sp + (function ? function->maxStackSlots : 0);
|
|
1891
|
+
|
|
1892
|
+
if (top > base + io_runtime->numStackSlots) {
|
|
1893
|
+
top = base + io_runtime->numStackSlots;
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
io_runtime->stack = top;
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
#endif // d_m3HasStackSwitching
|
|
1900
|
+
|
|
1901
|
+
|
|
1902
|
+
// Run compiled code on the runtime's stack, bounding native recursion for the
|
|
1903
|
+
// duration of the call. The outermost invocation establishes the stack limit;
|
|
1904
|
+
// nested ones (an imported function calling back into Wasm) inherit it.
|
|
1905
|
+
static inline
|
|
1906
|
+
M3Result RunCodeChecked (IM3Runtime i_runtime, IM3Function i_function)
|
|
1907
|
+
{
|
|
1908
|
+
pc_t i_pc = i_function->compiled;
|
|
1909
|
+
|
|
1910
|
+
// execution runs against the memory of the module the entry point belongs
|
|
1911
|
+
// to, not against some runtime-wide one
|
|
1912
|
+
M3MemoryHeader* _mem = Module_MemoryHeader(i_function->module);
|
|
1913
|
+
|
|
1914
|
+
#if d_m3HasSnapshots
|
|
1915
|
+
if (M3_UNLIKELY(i_function->module->isUnusable)) {
|
|
1916
|
+
return "a snapshot failed to restore into the module, so it cannot run";
|
|
1917
|
+
}
|
|
1918
|
+
i_function->module->hasRun = true;
|
|
1919
|
+
#endif
|
|
1920
|
+
|
|
1921
|
+
d_m3StackLimitEnter(i_runtime);
|
|
1922
|
+
|
|
1923
|
+
// a host function calling back into Wasm does not change which call a trap
|
|
1924
|
+
// ends: that is still the one the host made first
|
|
1925
|
+
if (i_runtime->callNesting++ == 0) {
|
|
1926
|
+
i_runtime->entered = i_function;
|
|
1927
|
+
}
|
|
1928
|
+
#if d_m3HasExceptionHandling
|
|
1929
|
+
// handler stacks don't nest across a call boundary: a host function calling
|
|
1930
|
+
// back into Wasm cannot be caught by a try_table its own caller entered
|
|
1931
|
+
u32 savedTryDepth = i_runtime->tryDepth;
|
|
1932
|
+
i_runtime->tryDepth = 0;
|
|
1933
|
+
#endif
|
|
1934
|
+
#if d_m3HasStackSwitching
|
|
1935
|
+
// nor do continuations: the native frames on the far side of a host call
|
|
1936
|
+
// are not ours to record, so a suspend in here has no handler to reach and
|
|
1937
|
+
// no resume to unwind to
|
|
1938
|
+
IM3Continuation savedContinuation = i_runtime->activeContinuation;
|
|
1939
|
+
|
|
1940
|
+
// A suspended root continuation is a paused program waiting to be resumed,
|
|
1941
|
+
// not scratch space. A call the host makes before resuming it gets a
|
|
1942
|
+
// context of its own, the way any other re-entry does, rather than having
|
|
1943
|
+
// the frames of the suspension reset out from under it.
|
|
1944
|
+
bool claimRoot = i_runtime->isSuspendable and not savedContinuation and
|
|
1945
|
+
not (i_runtime->rootContinuation and
|
|
1946
|
+
i_runtime->rootContinuation->state == cont_suspended);
|
|
1947
|
+
|
|
1948
|
+
if (claimRoot and not i_runtime->rootContinuation) {
|
|
1949
|
+
i_runtime->rootContinuation = Continuation_New(i_runtime, i_function->funcType, i_function);
|
|
1950
|
+
|
|
1951
|
+
if (i_runtime->rootContinuation) {
|
|
1952
|
+
// The root borrows the runtime's stack and is not charged as cont.new.
|
|
1953
|
+
i_runtime->rootContinuation->numStackSlots = i_runtime->numStackSlots;
|
|
1954
|
+
} else {
|
|
1955
|
+
claimRoot = false;
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
if (claimRoot) {
|
|
1960
|
+
i_runtime->activeContinuation = i_runtime->rootContinuation;
|
|
1961
|
+
i_runtime->rootContinuation->entryFunction = i_function;
|
|
1962
|
+
i_runtime->rootContinuation->state = cont_running;
|
|
1963
|
+
i_runtime->rootContinuation->numFrames = 0;
|
|
1964
|
+
} else {
|
|
1965
|
+
i_runtime->activeContinuation = NULL;
|
|
1966
|
+
}
|
|
1967
|
+
#endif
|
|
1968
|
+
#if d_m3GuardedMemory
|
|
1969
|
+
M3Result result = RunCodeProtected(i_runtime, i_pc, _mem);
|
|
1970
|
+
#elif (d_m3EnableOpProfiling || d_m3EnableOpTracing)
|
|
1971
|
+
M3Result result = (M3Result)RunCode(i_pc, (m3stack_t)i_runtime->stack, _mem, d_m3OpDefaultArgs, d_m3BaseCstr);
|
|
1972
|
+
#else
|
|
1973
|
+
M3Result result = (M3Result)RunCode(i_pc, (m3stack_t)i_runtime->stack, _mem, d_m3OpDefaultArgs);
|
|
1974
|
+
#endif
|
|
1975
|
+
#if d_m3HasStackSwitching
|
|
1976
|
+
if (claimRoot) {
|
|
1977
|
+
i_runtime->rootContinuation->state =
|
|
1978
|
+
(result == m3Err_continuationSuspended) ? cont_suspended : cont_returned;
|
|
1979
|
+
|
|
1980
|
+
Runtime_PlaceCallStack(i_runtime);
|
|
1981
|
+
}
|
|
1982
|
+
i_runtime->activeContinuation = savedContinuation;
|
|
1983
|
+
#endif
|
|
1984
|
+
#if d_m3HasExceptionHandling
|
|
1985
|
+
i_runtime->tryDepth = savedTryDepth;
|
|
1986
|
+
|
|
1987
|
+
// an exception that reached the bottom of the call stack found no handler
|
|
1988
|
+
if (M3_UNLIKELY(result == m3Err_pendingException)) {
|
|
1989
|
+
result = m3Err_trapUncaughtException;
|
|
1990
|
+
}
|
|
1991
|
+
RecordUncaughtException(i_runtime, result);
|
|
1992
|
+
|
|
1993
|
+
// a paused invocation can still reach the exceptions it holds, so they wait
|
|
1994
|
+
// for it to finish
|
|
1995
|
+
if (i_runtime->callNesting == 1 and not m3_IsSuspended(i_runtime)) {
|
|
1996
|
+
FreeExceptions(i_runtime);
|
|
1997
|
+
}
|
|
1998
|
+
#endif
|
|
1999
|
+
i_runtime->callNesting--;
|
|
2000
|
+
d_m3StackLimitLeave(i_runtime);
|
|
2001
|
+
|
|
2002
|
+
return result;
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
M3Result m3_RunStart (IM3Module io_module)
|
|
2006
|
+
{
|
|
2007
|
+
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
|
2008
|
+
// Execution disabled for fuzzing builds
|
|
2009
|
+
return m3Err_none;
|
|
2010
|
+
#endif
|
|
2011
|
+
|
|
2012
|
+
M3Result result = m3Err_none;
|
|
2013
|
+
i32 startFunctionTmp = -1;
|
|
2014
|
+
|
|
2015
|
+
if (io_module and io_module->startFunction >= 0) {
|
|
2016
|
+
IM3Function function = &io_module->functions[io_module->startFunction];
|
|
2017
|
+
|
|
2018
|
+
if (not function->compiled) {
|
|
2019
|
+
_ (CompileFunction(function));
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
IM3FuncType ftype = function->funcType;
|
|
2023
|
+
if (ftype->numArgs != 0 || ftype->numRets != 0) {
|
|
2024
|
+
_throw(m3Err_argumentCountMismatch);
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
IM3Module module = function->module;
|
|
2028
|
+
IM3Runtime runtime = module->runtime;
|
|
2029
|
+
|
|
2030
|
+
startFunctionTmp = io_module->startFunction;
|
|
2031
|
+
|
|
2032
|
+
io_module->startFunction = -1;
|
|
2033
|
+
|
|
2034
|
+
result = RunCodeChecked(runtime, function);
|
|
2035
|
+
|
|
2036
|
+
// a start function that suspended is not to be started again: resuming
|
|
2037
|
+
// is what finishes it
|
|
2038
|
+
if (result and result != m3Err_continuationSuspended) {
|
|
2039
|
+
io_module->startFunction = startFunctionTmp;
|
|
2040
|
+
EXCEPTION_PRINT(result);
|
|
2041
|
+
goto _catch;
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
|
|
2045
|
+
_catch: return result;
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
// TODO: deal with main + side-modules loading efforcement
|
|
2049
|
+
M3Result m3_LoadModule (IM3Runtime io_runtime, IM3Module io_module)
|
|
2050
|
+
{
|
|
2051
|
+
M3Result result = m3Err_none;
|
|
2052
|
+
|
|
2053
|
+
if (M3_UNLIKELY(io_module->runtime)) {
|
|
2054
|
+
return m3Err_moduleAlreadyLinked;
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
io_module->runtime = io_runtime;
|
|
2058
|
+
|
|
2059
|
+
// linking first: a memory import has to be resolved before InitMemory would
|
|
2060
|
+
// give it pages of its own, and a global import before an initializer reads it
|
|
2061
|
+
_ (LinkImports(io_runtime, io_module));
|
|
2062
|
+
|
|
2063
|
+
_ (InitMemory(io_runtime, io_module));
|
|
2064
|
+
_ (InitGlobals(io_module));
|
|
2065
|
+
// Spec order: element segments are applied before data segments. It matters
|
|
2066
|
+
// when one of them traps - whatever ran before the trap stays done.
|
|
2067
|
+
_ (InitTableAndElements(io_module));
|
|
2068
|
+
_ (InitDataSegments(io_module));
|
|
2069
|
+
|
|
2070
|
+
// Start func might use imported functions, which are not liked here yet,
|
|
2071
|
+
// so it will be called before a function call is attempted (in m3_FindFunction)
|
|
2072
|
+
|
|
2073
|
+
#ifdef DEBUG
|
|
2074
|
+
Module_GenerateNames(io_module);
|
|
2075
|
+
#endif
|
|
2076
|
+
|
|
2077
|
+
io_module->next = io_runtime->modules;
|
|
2078
|
+
io_runtime->modules = io_module;
|
|
2079
|
+
return result; // ok
|
|
2080
|
+
|
|
2081
|
+
_catch:
|
|
2082
|
+
// The runtime owns the module either way. Instantiation may already have
|
|
2083
|
+
// written this module's functions into a table another module owns, and the
|
|
2084
|
+
// spec keeps whatever it managed to do before the trap, so those entries
|
|
2085
|
+
// stay callable - retaining the module is what stops them dangling.
|
|
2086
|
+
//
|
|
2087
|
+
// It goes on the tail of the list rather than the head: it is not a module
|
|
2088
|
+
// anyone should find by name, only one whose functions may still be
|
|
2089
|
+
// reachable through someone else's table.
|
|
2090
|
+
io_module->next = NULL;
|
|
2091
|
+
|
|
2092
|
+
IM3Module* tail = &io_runtime->modules;
|
|
2093
|
+
while (*tail) {
|
|
2094
|
+
tail = &(*tail)->next;
|
|
2095
|
+
}
|
|
2096
|
+
*tail = io_module;
|
|
2097
|
+
|
|
2098
|
+
return result;
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
IM3Global m3_FindGlobal (IM3Module io_module,
|
|
2102
|
+
const char* const i_globalName)
|
|
2103
|
+
{
|
|
2104
|
+
// Search exports
|
|
2105
|
+
IM3Global exported = Module_FindExportedGlobal(io_module, i_globalName);
|
|
2106
|
+
if (exported) {
|
|
2107
|
+
// a re-exported global is the one that was imported, so reads and
|
|
2108
|
+
// writes have to reach the cell that actually holds the value
|
|
2109
|
+
return exported->resolved ? exported->resolved : exported;
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
// Search imports
|
|
2113
|
+
for (u32 i = 0; i < io_module->numGlobals; ++i) {
|
|
2114
|
+
IM3Global g = &io_module->globals[i];
|
|
2115
|
+
|
|
2116
|
+
if (g->import.moduleUtf8 and g->import.fieldUtf8) {
|
|
2117
|
+
if (strcmp(g->import.fieldUtf8, i_globalName) == 0) {
|
|
2118
|
+
return g->resolved ? g->resolved : g;
|
|
2119
|
+
}
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
return NULL;
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
M3Result m3_GetGlobal (IM3Global i_global,
|
|
2126
|
+
IM3TaggedValue o_value)
|
|
2127
|
+
{
|
|
2128
|
+
if (not i_global) {
|
|
2129
|
+
return m3Err_globalLookupFailed;
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
switch (i_global->type) {
|
|
2133
|
+
case c_m3Type_i32: o_value->value.i32 = i_global->i32Value; break;
|
|
2134
|
+
case c_m3Type_i64: o_value->value.i64 = i_global->i64Value; break;
|
|
2135
|
+
#if d_m3HasFloat
|
|
2136
|
+
case c_m3Type_f32: o_value->value.f32 = i_global->f32Value; break;
|
|
2137
|
+
case c_m3Type_f64: o_value->value.f64 = i_global->f64Value; break;
|
|
2138
|
+
#endif
|
|
2139
|
+
default: return m3Err_invalidTypeId;
|
|
2140
|
+
}
|
|
2141
|
+
|
|
2142
|
+
o_value->type = (M3ValueType)(i_global->type);
|
|
2143
|
+
return m3Err_none;
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2146
|
+
M3Result m3_SetGlobal (IM3Global i_global,
|
|
2147
|
+
const IM3TaggedValue i_value)
|
|
2148
|
+
{
|
|
2149
|
+
if (not i_global) {
|
|
2150
|
+
return m3Err_globalLookupFailed;
|
|
2151
|
+
}
|
|
2152
|
+
if (not i_global->isMutable) {
|
|
2153
|
+
return m3Err_globalNotMutable;
|
|
2154
|
+
}
|
|
2155
|
+
if (i_global->type != i_value->type) {
|
|
2156
|
+
return m3Err_globalTypeMismatch;
|
|
2157
|
+
}
|
|
2158
|
+
|
|
2159
|
+
switch (i_value->type) {
|
|
2160
|
+
case c_m3Type_i32: i_global->i32Value = i_value->value.i32; break;
|
|
2161
|
+
case c_m3Type_i64: i_global->i64Value = i_value->value.i64; break;
|
|
2162
|
+
#if d_m3HasFloat
|
|
2163
|
+
case c_m3Type_f32: i_global->f32Value = i_value->value.f32; break;
|
|
2164
|
+
case c_m3Type_f64: i_global->f64Value = i_value->value.f64; break;
|
|
2165
|
+
#endif
|
|
2166
|
+
default: return m3Err_invalidTypeId;
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
return m3Err_none;
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
M3ValueType m3_GetGlobalType (IM3Global i_global)
|
|
2173
|
+
{
|
|
2174
|
+
return (i_global) ? (M3ValueType)(i_global->type) : c_m3Type_none;
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
|
|
2178
|
+
void* v_FindFunction (IM3Module i_module, void* i_info)
|
|
2179
|
+
{
|
|
2180
|
+
const char* const i_name = (const char*)i_info;
|
|
2181
|
+
|
|
2182
|
+
// Prefer exported functions
|
|
2183
|
+
IM3Function exported = Module_FindExportedFunction(i_module, i_name);
|
|
2184
|
+
if (exported) {
|
|
2185
|
+
return exported;
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
// Search internal functions
|
|
2189
|
+
for (u32 i = 0; i < i_module->numFunctions; ++i) {
|
|
2190
|
+
IM3Function f = &i_module->functions[i];
|
|
2191
|
+
|
|
2192
|
+
bool isImported = f->import.moduleUtf8 or f->import.fieldUtf8;
|
|
2193
|
+
|
|
2194
|
+
if (isImported) {
|
|
2195
|
+
continue;
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
for (int j = 0; j < f->numNames; j++) {
|
|
2199
|
+
if (f->names[j] and strcmp(f->names[j], i_name) == 0) {
|
|
2200
|
+
return f;
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
return NULL;
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
|
|
2209
|
+
// Shared tail of the two lookups: a function is only usable once it has code.
|
|
2210
|
+
static
|
|
2211
|
+
M3Result PrepareFoundFunction (IM3Function* o_function, IM3Function i_function)
|
|
2212
|
+
{
|
|
2213
|
+
M3Result result = m3Err_none;
|
|
2214
|
+
|
|
2215
|
+
if (not i_function->compiled) {
|
|
2216
|
+
_ (CompileFunction(i_function))
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
_catch:
|
|
2220
|
+
*o_function = result ? NULL : i_function;
|
|
2221
|
+
|
|
2222
|
+
return result;
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
|
|
2226
|
+
// Searches every module in the runtime, most recently loaded first. That is a
|
|
2227
|
+
// guess once more than one module is loaded and two of them export the same
|
|
2228
|
+
// name - m3_FindFunctionIn says which module is meant.
|
|
2229
|
+
M3Result m3_FindFunction (IM3Function* o_function, IM3Runtime i_runtime, const char* const i_functionName)
|
|
2230
|
+
{
|
|
2231
|
+
d_m3Assert (o_function and i_runtime and i_functionName);
|
|
2232
|
+
IM3Function function = NULL;
|
|
2233
|
+
|
|
2234
|
+
if (not i_runtime->modules) {
|
|
2235
|
+
*o_function = NULL;
|
|
2236
|
+
return "no modules loaded";
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2239
|
+
function = (IM3Function)ForEachModule(i_runtime, v_FindFunction, (void*)i_functionName);
|
|
2240
|
+
|
|
2241
|
+
if (not function) {
|
|
2242
|
+
*o_function = NULL;
|
|
2243
|
+
return ErrorModule(m3Err_functionLookupFailed, i_runtime->modules, "'%s'", i_functionName);
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
return PrepareFoundFunction(o_function, function);
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
|
|
2250
|
+
IM3Module m3_FindModule (IM3Runtime i_runtime, const char* const i_moduleName)
|
|
2251
|
+
{
|
|
2252
|
+
if (not i_runtime or not i_moduleName) {
|
|
2253
|
+
return NULL;
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
// the list is newest-first, so a name registered twice names the newer one
|
|
2257
|
+
for (IM3Module m = i_runtime->modules; m; m = m->next) {
|
|
2258
|
+
if (m->name and strcmp(m->name, i_moduleName) == 0) {
|
|
2259
|
+
return m;
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
return NULL;
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
|
|
2267
|
+
// Searches one module's exports, which is what naming a module means.
|
|
2268
|
+
M3Result m3_FindFunctionIn (IM3Function* o_function, IM3Module i_module, const char* const i_functionName)
|
|
2269
|
+
{
|
|
2270
|
+
d_m3Assert (o_function and i_functionName);
|
|
2271
|
+
if (not i_module) {
|
|
2272
|
+
*o_function = NULL;
|
|
2273
|
+
return m3Err_functionLookupFailed; // ErrorModule would deref it
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
IM3Function function = (IM3Function)v_FindFunction(i_module, (void*)i_functionName);
|
|
2277
|
+
|
|
2278
|
+
if (not function) {
|
|
2279
|
+
*o_function = NULL;
|
|
2280
|
+
return ErrorModule(m3Err_functionLookupFailed, i_module, "'%s'", i_functionName);
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
return PrepareFoundFunction(o_function, function);
|
|
2284
|
+
}
|
|
2285
|
+
|
|
2286
|
+
|
|
2287
|
+
M3Result m3_GetTableFunction (IM3Function* o_function, IM3Module i_module, uint32_t i_index)
|
|
2288
|
+
{
|
|
2289
|
+
_try {
|
|
2290
|
+
M3Table* table;
|
|
2291
|
+
IM3Function function;
|
|
2292
|
+
|
|
2293
|
+
_throwif("no table", i_module->numTables == 0);
|
|
2294
|
+
|
|
2295
|
+
table = i_module->tables[0];
|
|
2296
|
+
_throwif("function index out of range", i_index >= table->size);
|
|
2297
|
+
|
|
2298
|
+
function = (IM3Function)table->elements[i_index];
|
|
2299
|
+
|
|
2300
|
+
if (function) {
|
|
2301
|
+
if (not function->compiled) {
|
|
2302
|
+
_ (CompileFunction(function))
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
|
|
2306
|
+
*o_function = function;
|
|
2307
|
+
} _catch:
|
|
2308
|
+
return result;
|
|
2309
|
+
}
|
|
2310
|
+
|
|
2311
|
+
|
|
2312
|
+
static
|
|
2313
|
+
M3Result checkStartFunction (IM3Module i_module)
|
|
2314
|
+
{
|
|
2315
|
+
M3Result result = m3Err_none; d_m3Assert(i_module);
|
|
2316
|
+
|
|
2317
|
+
// Check if start function needs to be called
|
|
2318
|
+
if (i_module->startFunction >= 0) {
|
|
2319
|
+
result = m3_RunStart(i_module);
|
|
2320
|
+
}
|
|
2321
|
+
|
|
2322
|
+
return result;
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
uint32_t m3_GetArgCount (IM3Function i_function)
|
|
2326
|
+
{
|
|
2327
|
+
if (i_function) {
|
|
2328
|
+
IM3FuncType ft = i_function->funcType;
|
|
2329
|
+
if (ft) {
|
|
2330
|
+
return ft->numArgs;
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
return 0;
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
uint32_t m3_GetRetCount (IM3Function i_function)
|
|
2337
|
+
{
|
|
2338
|
+
if (i_function) {
|
|
2339
|
+
IM3FuncType ft = i_function->funcType;
|
|
2340
|
+
if (ft) {
|
|
2341
|
+
return ft->numRets;
|
|
2342
|
+
}
|
|
2343
|
+
}
|
|
2344
|
+
return 0;
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
|
|
2348
|
+
M3ValueType m3_GetArgType (IM3Function i_function, uint32_t index)
|
|
2349
|
+
{
|
|
2350
|
+
if (i_function) {
|
|
2351
|
+
IM3FuncType ft = i_function->funcType;
|
|
2352
|
+
if (ft and index < ft->numArgs) {
|
|
2353
|
+
return (M3ValueType)BaseTypeOf(d_FuncArgType(ft, index));
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
return c_m3Type_none;
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
M3ValueType m3_GetRetType (IM3Function i_function, uint32_t index)
|
|
2360
|
+
{
|
|
2361
|
+
if (i_function) {
|
|
2362
|
+
IM3FuncType ft = i_function->funcType;
|
|
2363
|
+
if (ft and index < ft->numRets) {
|
|
2364
|
+
return (M3ValueType)BaseTypeOf(d_FuncRetType(ft, index));
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2367
|
+
return c_m3Type_none;
|
|
2368
|
+
}
|
|
2369
|
+
|
|
2370
|
+
|
|
2371
|
+
// The call APIs marshal their arguments into the runtime stack before any
|
|
2372
|
+
// compiled code runs, so op_Entry's overflow check comes too late to cover those
|
|
2373
|
+
// writes - and for a host function or an import linked to another module there
|
|
2374
|
+
// is no op_Entry of our own to reach at all. Bound the area about to be written
|
|
2375
|
+
// here instead: results and arguments each take a u64 at this boundary, however
|
|
2376
|
+
// wide the slot is inside. Every caller goes through this, so none of them can
|
|
2377
|
+
// be the one that forgets.
|
|
2378
|
+
static
|
|
2379
|
+
M3Result GetStackPointerForArgs (IM3Function i_function, u8** o_stack)
|
|
2380
|
+
{
|
|
2381
|
+
IM3Runtime runtime = i_function->module->runtime;
|
|
2382
|
+
IM3FuncType ftype = i_function->funcType;
|
|
2383
|
+
|
|
2384
|
+
// the stack a paused invocation holds is not available to this one
|
|
2385
|
+
size_t used = (size_t)((u8*)runtime->stack - (u8*)runtime->originStack);
|
|
2386
|
+
size_t needed = ((size_t)ftype->numRets + ftype->numArgs) * sizeof(u64);
|
|
2387
|
+
size_t available = (size_t)runtime->numStackSlots * sizeof(m3slot_t) - used;
|
|
2388
|
+
|
|
2389
|
+
if (needed > available) {
|
|
2390
|
+
return m3Err_trapStackOverflow;
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
u64* stack = (u64*)runtime->stack;
|
|
2394
|
+
|
|
2395
|
+
stack += ftype->numRets;
|
|
2396
|
+
|
|
2397
|
+
*o_stack = (u8*)stack;
|
|
2398
|
+
|
|
2399
|
+
return m3Err_none;
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
|
|
2403
|
+
M3Result m3_CallV (IM3Function i_function, ...)
|
|
2404
|
+
{
|
|
2405
|
+
va_list ap;
|
|
2406
|
+
va_start(ap, i_function);
|
|
2407
|
+
M3Result r = m3_CallVL(i_function, ap);
|
|
2408
|
+
va_end(ap);
|
|
2409
|
+
return r;
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
static
|
|
2413
|
+
void ReportNativeStackUsage ()
|
|
2414
|
+
{
|
|
2415
|
+
#if d_m3LogNativeStack
|
|
2416
|
+
int stackUsed = m3StackGetMax();
|
|
2417
|
+
fprintf(stderr, "Native stack used: %d\n", stackUsed);
|
|
2418
|
+
#endif
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
|
|
2422
|
+
M3Result m3_CallVL (IM3Function i_function, va_list i_args)
|
|
2423
|
+
{
|
|
2424
|
+
IM3Runtime runtime = i_function->module->runtime;
|
|
2425
|
+
IM3FuncType ftype = i_function->funcType;
|
|
2426
|
+
M3Result result = m3Err_none;
|
|
2427
|
+
u8* s = NULL;
|
|
2428
|
+
|
|
2429
|
+
if (!i_function->compiled) {
|
|
2430
|
+
return m3Err_missingCompiledCode;
|
|
2431
|
+
}
|
|
2432
|
+
|
|
2433
|
+
#if d_m3RecordBacktraces
|
|
2434
|
+
ClearBacktrace(runtime);
|
|
2435
|
+
#endif
|
|
2436
|
+
|
|
2437
|
+
m3StackCheckInit();
|
|
2438
|
+
|
|
2439
|
+
_ (checkStartFunction(i_function->module))
|
|
2440
|
+
|
|
2441
|
+
_ (GetStackPointerForArgs(i_function, &s))
|
|
2442
|
+
|
|
2443
|
+
for (u32 i = 0; i < ftype->numArgs; ++i) {
|
|
2444
|
+
switch (d_FuncArgType(ftype, i)) {
|
|
2445
|
+
case c_m3Type_i32:
|
|
2446
|
+
*(i32*)(s) = va_arg(i_args, i32);
|
|
2447
|
+
s += 8;
|
|
2448
|
+
break;
|
|
2449
|
+
case c_m3Type_i64:
|
|
2450
|
+
*(i64*)(s) = va_arg(i_args, i64);
|
|
2451
|
+
s += 8;
|
|
2452
|
+
break;
|
|
2453
|
+
case c_m3Type_funcref:
|
|
2454
|
+
case c_m3Type_externref:
|
|
2455
|
+
case c_m3Type_exnref:
|
|
2456
|
+
*(uintptr_t*)(s) = va_arg(i_args, uintptr_t);
|
|
2457
|
+
s += 8;
|
|
2458
|
+
break;
|
|
2459
|
+
#if d_m3HasFloat
|
|
2460
|
+
case c_m3Type_f32:
|
|
2461
|
+
*(f32*)(s) = (f32)va_arg(i_args, f64);
|
|
2462
|
+
s += 8;
|
|
2463
|
+
break; // f32 is passed as f64
|
|
2464
|
+
case c_m3Type_f64:
|
|
2465
|
+
*(f64*)(s) = va_arg(i_args, f64);
|
|
2466
|
+
s += 8;
|
|
2467
|
+
break;
|
|
2468
|
+
#endif
|
|
2469
|
+
default: return "unknown argument type";
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
result = RunCodeChecked(runtime, i_function);
|
|
2474
|
+
ReportNativeStackUsage();
|
|
2475
|
+
|
|
2476
|
+
runtime->lastCalled = result ? NULL : i_function;
|
|
2477
|
+
|
|
2478
|
+
_catch: return result;
|
|
2479
|
+
}
|
|
2480
|
+
|
|
2481
|
+
M3Result m3_Call (IM3Function i_function, uint32_t i_argc, const void* i_argptrs[])
|
|
2482
|
+
{
|
|
2483
|
+
IM3Runtime runtime = i_function->module->runtime;
|
|
2484
|
+
IM3FuncType ftype = i_function->funcType;
|
|
2485
|
+
M3Result result = m3Err_none;
|
|
2486
|
+
u8* s = NULL;
|
|
2487
|
+
|
|
2488
|
+
if (i_argc != ftype->numArgs) {
|
|
2489
|
+
return m3Err_argumentCountMismatch;
|
|
2490
|
+
}
|
|
2491
|
+
if (!i_function->compiled) {
|
|
2492
|
+
return m3Err_missingCompiledCode;
|
|
2493
|
+
}
|
|
2494
|
+
|
|
2495
|
+
#if d_m3RecordBacktraces
|
|
2496
|
+
ClearBacktrace(runtime);
|
|
2497
|
+
#endif
|
|
2498
|
+
|
|
2499
|
+
m3StackCheckInit();
|
|
2500
|
+
|
|
2501
|
+
_ (checkStartFunction(i_function->module))
|
|
2502
|
+
|
|
2503
|
+
_ (GetStackPointerForArgs(i_function, &s))
|
|
2504
|
+
|
|
2505
|
+
for (u32 i = 0; i < ftype->numArgs; ++i) {
|
|
2506
|
+
switch (d_FuncArgType(ftype, i)) {
|
|
2507
|
+
case c_m3Type_i32:
|
|
2508
|
+
*(i32*)(s) = *(i32*)i_argptrs[i];
|
|
2509
|
+
s += 8;
|
|
2510
|
+
break;
|
|
2511
|
+
case c_m3Type_i64:
|
|
2512
|
+
*(i64*)(s) = *(i64*)i_argptrs[i];
|
|
2513
|
+
s += 8;
|
|
2514
|
+
break;
|
|
2515
|
+
case c_m3Type_funcref:
|
|
2516
|
+
case c_m3Type_externref:
|
|
2517
|
+
case c_m3Type_exnref:
|
|
2518
|
+
*(uintptr_t*)(s) = *(uintptr_t*)i_argptrs[i];
|
|
2519
|
+
s += 8;
|
|
2520
|
+
break;
|
|
2521
|
+
#if d_m3HasFloat
|
|
2522
|
+
case c_m3Type_f32:
|
|
2523
|
+
*(f32*)(s) = *(f32*)i_argptrs[i];
|
|
2524
|
+
s += 8;
|
|
2525
|
+
break;
|
|
2526
|
+
case c_m3Type_f64:
|
|
2527
|
+
*(f64*)(s) = *(f64*)i_argptrs[i];
|
|
2528
|
+
s += 8;
|
|
2529
|
+
break;
|
|
2530
|
+
#endif
|
|
2531
|
+
default: return "unknown argument type";
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
result = RunCodeChecked(runtime, i_function);
|
|
2536
|
+
|
|
2537
|
+
ReportNativeStackUsage();
|
|
2538
|
+
|
|
2539
|
+
runtime->lastCalled = result ? NULL : i_function;
|
|
2540
|
+
|
|
2541
|
+
_catch: return result;
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2544
|
+
// Argument parsing for m3_CallArgv. Strict on purpose: the whole string has to
|
|
2545
|
+
// be consumed, so "12abc" is rejected rather than read as 12, and an empty or
|
|
2546
|
+
// unparsable argument is an error rather than the zero that strtoul with a
|
|
2547
|
+
// NULL end pointer used to hand back. See wasm3/wasm3#367.
|
|
2548
|
+
static
|
|
2549
|
+
M3Result ParseArgInteger (ccstr_t i_arg, u32 i_numBits, u64* o_value)
|
|
2550
|
+
{
|
|
2551
|
+
if (not i_arg or not *i_arg) {
|
|
2552
|
+
return "empty argument";
|
|
2553
|
+
}
|
|
2554
|
+
|
|
2555
|
+
// strtoull would skip leading space, but trailing space is rejected below;
|
|
2556
|
+
// accepting one and not the other would just be confusing
|
|
2557
|
+
if (isspace((unsigned char)*i_arg)) {
|
|
2558
|
+
return "argument is not a number";
|
|
2559
|
+
}
|
|
2560
|
+
|
|
2561
|
+
char* end = NULL;
|
|
2562
|
+
u64 value;
|
|
2563
|
+
|
|
2564
|
+
errno = 0;
|
|
2565
|
+
|
|
2566
|
+
// an argument may be spelled signed or unsigned: -1 and 4294967295 name the
|
|
2567
|
+
// same i32
|
|
2568
|
+
if (*i_arg == '-') {
|
|
2569
|
+
i64 signedValue = strtoll(i_arg, &end, 10);
|
|
2570
|
+
|
|
2571
|
+
if (i_numBits == 32 and (signedValue < INT32_MIN or signedValue > INT32_MAX)) {
|
|
2572
|
+
return "argument out of range";
|
|
2573
|
+
}
|
|
2574
|
+
|
|
2575
|
+
value = (u64)signedValue;
|
|
2576
|
+
} else {
|
|
2577
|
+
value = strtoull(i_arg, &end, 10);
|
|
2578
|
+
|
|
2579
|
+
if (i_numBits == 32 and value > UINT32_MAX) {
|
|
2580
|
+
return "argument out of range";
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
2583
|
+
|
|
2584
|
+
if (errno == ERANGE) {
|
|
2585
|
+
return "argument out of range";
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
if (end == i_arg or *end) {
|
|
2589
|
+
return "argument is not a number";
|
|
2590
|
+
}
|
|
2591
|
+
|
|
2592
|
+
*o_value = value;
|
|
2593
|
+
|
|
2594
|
+
return m3Err_none;
|
|
2595
|
+
}
|
|
2596
|
+
|
|
2597
|
+
|
|
2598
|
+
#if d_m3HasFloat
|
|
2599
|
+
static
|
|
2600
|
+
M3Result ParseArgFloat (ccstr_t i_arg, f64* o_value)
|
|
2601
|
+
{
|
|
2602
|
+
if (not i_arg or not *i_arg) {
|
|
2603
|
+
return "empty argument";
|
|
2604
|
+
}
|
|
2605
|
+
|
|
2606
|
+
if (isspace((unsigned char)*i_arg)) {
|
|
2607
|
+
return "argument is not a number";
|
|
2608
|
+
}
|
|
2609
|
+
|
|
2610
|
+
char* end = NULL;
|
|
2611
|
+
|
|
2612
|
+
errno = 0;
|
|
2613
|
+
|
|
2614
|
+
f64 value = strtod(i_arg, &end);
|
|
2615
|
+
|
|
2616
|
+
if (end == i_arg or *end) {
|
|
2617
|
+
return "argument is not a number";
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
// strtod reports underflow through ERANGE as well, and a denormal result is
|
|
2621
|
+
// perfectly usable, so only an overflow to infinity is out of range
|
|
2622
|
+
if (errno == ERANGE and (value > DBL_MAX or value < -DBL_MAX)) {
|
|
2623
|
+
return "argument out of range";
|
|
2624
|
+
}
|
|
2625
|
+
|
|
2626
|
+
*o_value = value;
|
|
2627
|
+
|
|
2628
|
+
return m3Err_none;
|
|
2629
|
+
}
|
|
2630
|
+
#endif
|
|
2631
|
+
|
|
2632
|
+
|
|
2633
|
+
// A reference argument is either the null reference or a host handle written as
|
|
2634
|
+
// an integer.
|
|
2635
|
+
static
|
|
2636
|
+
M3Result ParseArgReference (ccstr_t i_arg, u64* o_value)
|
|
2637
|
+
{
|
|
2638
|
+
if (i_arg and strcmp(i_arg, "null") == 0) {
|
|
2639
|
+
*o_value = 0;
|
|
2640
|
+
return m3Err_none;
|
|
2641
|
+
}
|
|
2642
|
+
|
|
2643
|
+
return ParseArgInteger(i_arg, 64, o_value);
|
|
2644
|
+
}
|
|
2645
|
+
|
|
2646
|
+
|
|
2647
|
+
M3Result m3_CallArgv (IM3Function i_function, uint32_t i_argc, const char* i_argv[])
|
|
2648
|
+
{
|
|
2649
|
+
IM3FuncType ftype = i_function->funcType;
|
|
2650
|
+
IM3Runtime runtime = i_function->module->runtime;
|
|
2651
|
+
M3Result result = m3Err_none;
|
|
2652
|
+
u8* s = NULL;
|
|
2653
|
+
|
|
2654
|
+
if (i_argc != ftype->numArgs) {
|
|
2655
|
+
return m3Err_argumentCountMismatch;
|
|
2656
|
+
}
|
|
2657
|
+
if (!i_function->compiled) {
|
|
2658
|
+
return m3Err_missingCompiledCode;
|
|
2659
|
+
}
|
|
2660
|
+
|
|
2661
|
+
#if d_m3RecordBacktraces
|
|
2662
|
+
ClearBacktrace(runtime);
|
|
2663
|
+
#endif
|
|
2664
|
+
|
|
2665
|
+
m3StackCheckInit();
|
|
2666
|
+
|
|
2667
|
+
_ (checkStartFunction(i_function->module))
|
|
2668
|
+
|
|
2669
|
+
_ (GetStackPointerForArgs(i_function, &s))
|
|
2670
|
+
|
|
2671
|
+
for (u32 i = 0; i < ftype->numArgs; ++i) {
|
|
2672
|
+
u64 value = 0;
|
|
2673
|
+
#if d_m3HasFloat
|
|
2674
|
+
f64 fvalue = 0;
|
|
2675
|
+
#endif
|
|
2676
|
+
switch (d_FuncArgType(ftype, i)) {
|
|
2677
|
+
case c_m3Type_i32:
|
|
2678
|
+
_ (ParseArgInteger(i_argv[i], 32, &value)) * (i32*)(s) = (i32)value;
|
|
2679
|
+
s += 8;
|
|
2680
|
+
break;
|
|
2681
|
+
case c_m3Type_i64:
|
|
2682
|
+
_ (ParseArgInteger(i_argv[i], 64, &value)) * (i64*)(s) = (i64)value;
|
|
2683
|
+
s += 8;
|
|
2684
|
+
break;
|
|
2685
|
+
case c_m3Type_funcref:
|
|
2686
|
+
case c_m3Type_externref:
|
|
2687
|
+
case c_m3Type_exnref:
|
|
2688
|
+
_ (ParseArgReference(i_argv[i], &value)) * (uintptr_t*)(s) = (uintptr_t)value;
|
|
2689
|
+
s += 8;
|
|
2690
|
+
break;
|
|
2691
|
+
#if d_m3HasFloat
|
|
2692
|
+
// strtof would be less portable
|
|
2693
|
+
case c_m3Type_f32:
|
|
2694
|
+
_ (ParseArgFloat(i_argv[i], &fvalue)) * (f32*)(s) = (f32)fvalue;
|
|
2695
|
+
s += 8;
|
|
2696
|
+
break;
|
|
2697
|
+
case c_m3Type_f64:
|
|
2698
|
+
_ (ParseArgFloat(i_argv[i], &fvalue)) * (f64*)(s) = fvalue;
|
|
2699
|
+
s += 8;
|
|
2700
|
+
break;
|
|
2701
|
+
#endif
|
|
2702
|
+
default: _throw("unknown argument type");
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
|
|
2706
|
+
result = RunCodeChecked(runtime, i_function);
|
|
2707
|
+
|
|
2708
|
+
ReportNativeStackUsage();
|
|
2709
|
+
|
|
2710
|
+
runtime->lastCalled = result ? NULL : i_function;
|
|
2711
|
+
|
|
2712
|
+
_catch: return result;
|
|
2713
|
+
}
|
|
2714
|
+
|
|
2715
|
+
|
|
2716
|
+
//u8 * AlignStackPointerTo64Bits (const u8 * i_stack)
|
|
2717
|
+
//{
|
|
2718
|
+
// uintptr_t ptr = (uintptr_t) i_stack;
|
|
2719
|
+
// return (u8 *) ((ptr + 7) & ~7);
|
|
2720
|
+
//}
|
|
2721
|
+
|
|
2722
|
+
|
|
2723
|
+
M3Result m3_GetResults (IM3Function i_function, uint32_t i_retc, const void* o_retptrs[])
|
|
2724
|
+
{
|
|
2725
|
+
IM3FuncType ftype = i_function->funcType;
|
|
2726
|
+
IM3Runtime runtime = i_function->module->runtime;
|
|
2727
|
+
|
|
2728
|
+
if (i_retc != ftype->numRets) {
|
|
2729
|
+
return m3Err_argumentCountMismatch;
|
|
2730
|
+
}
|
|
2731
|
+
if (i_function != runtime->lastCalled) {
|
|
2732
|
+
return "function not called";
|
|
2733
|
+
}
|
|
2734
|
+
|
|
2735
|
+
u8* s = (u8*)runtime->stack;
|
|
2736
|
+
|
|
2737
|
+
for (u32 i = 0; i < ftype->numRets; ++i) {
|
|
2738
|
+
switch (d_FuncRetType(ftype, i)) {
|
|
2739
|
+
case c_m3Type_i32:
|
|
2740
|
+
*(i32*)o_retptrs[i] = *(i32*)(s);
|
|
2741
|
+
s += 8;
|
|
2742
|
+
break;
|
|
2743
|
+
case c_m3Type_i64:
|
|
2744
|
+
*(i64*)o_retptrs[i] = *(i64*)(s);
|
|
2745
|
+
s += 8;
|
|
2746
|
+
break;
|
|
2747
|
+
case c_m3Type_funcref:
|
|
2748
|
+
case c_m3Type_externref:
|
|
2749
|
+
case c_m3Type_exnref:
|
|
2750
|
+
*(uintptr_t*)o_retptrs[i] = *(uintptr_t*)(s);
|
|
2751
|
+
s += 8;
|
|
2752
|
+
break;
|
|
2753
|
+
#if d_m3HasFloat
|
|
2754
|
+
case c_m3Type_f32:
|
|
2755
|
+
*(f32*)o_retptrs[i] = *(f32*)(s);
|
|
2756
|
+
s += 8;
|
|
2757
|
+
break;
|
|
2758
|
+
case c_m3Type_f64:
|
|
2759
|
+
*(f64*)o_retptrs[i] = *(f64*)(s);
|
|
2760
|
+
s += 8;
|
|
2761
|
+
break;
|
|
2762
|
+
#endif
|
|
2763
|
+
default: return "unknown return type";
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
return m3Err_none;
|
|
2767
|
+
}
|
|
2768
|
+
|
|
2769
|
+
M3Result m3_GetResultsV (IM3Function i_function, ...)
|
|
2770
|
+
{
|
|
2771
|
+
va_list ap;
|
|
2772
|
+
va_start(ap, i_function);
|
|
2773
|
+
M3Result r = m3_GetResultsVL(i_function, ap);
|
|
2774
|
+
va_end(ap);
|
|
2775
|
+
return r;
|
|
2776
|
+
}
|
|
2777
|
+
|
|
2778
|
+
M3Result m3_GetResultsVL (IM3Function i_function, va_list o_rets)
|
|
2779
|
+
{
|
|
2780
|
+
IM3Runtime runtime = i_function->module->runtime;
|
|
2781
|
+
IM3FuncType ftype = i_function->funcType;
|
|
2782
|
+
|
|
2783
|
+
if (i_function != runtime->lastCalled) {
|
|
2784
|
+
return "function not called";
|
|
2785
|
+
}
|
|
2786
|
+
|
|
2787
|
+
u8* s = (u8*)runtime->stack;
|
|
2788
|
+
for (u32 i = 0; i < ftype->numRets; ++i) {
|
|
2789
|
+
switch (d_FuncRetType(ftype, i)) {
|
|
2790
|
+
case c_m3Type_i32:
|
|
2791
|
+
*va_arg(o_rets, i32*) = *(i32*)(s);
|
|
2792
|
+
s += 8;
|
|
2793
|
+
break;
|
|
2794
|
+
case c_m3Type_i64:
|
|
2795
|
+
*va_arg(o_rets, i64*) = *(i64*)(s);
|
|
2796
|
+
s += 8;
|
|
2797
|
+
break;
|
|
2798
|
+
case c_m3Type_funcref:
|
|
2799
|
+
case c_m3Type_externref:
|
|
2800
|
+
case c_m3Type_exnref:
|
|
2801
|
+
*va_arg(o_rets, uintptr_t*) = *(uintptr_t*)(s);
|
|
2802
|
+
s += 8;
|
|
2803
|
+
break;
|
|
2804
|
+
#if d_m3HasFloat
|
|
2805
|
+
case c_m3Type_f32:
|
|
2806
|
+
*va_arg(o_rets, f32*) = *(f32*)(s);
|
|
2807
|
+
s += 8;
|
|
2808
|
+
break;
|
|
2809
|
+
case c_m3Type_f64:
|
|
2810
|
+
*va_arg(o_rets, f64*) = *(f64*)(s);
|
|
2811
|
+
s += 8;
|
|
2812
|
+
break;
|
|
2813
|
+
#endif
|
|
2814
|
+
default: return "unknown argument type";
|
|
2815
|
+
}
|
|
2816
|
+
}
|
|
2817
|
+
return m3Err_none;
|
|
2818
|
+
}
|
|
2819
|
+
|
|
2820
|
+
void ReleaseCodePageNoTrack (IM3Runtime i_runtime, IM3CodePage i_codePage)
|
|
2821
|
+
{
|
|
2822
|
+
if (i_codePage) {
|
|
2823
|
+
IM3CodePage* list;
|
|
2824
|
+
|
|
2825
|
+
bool pageFull = (NumFreeLines(i_codePage) < d_m3CodePageFreeLinesThreshold);
|
|
2826
|
+
if (pageFull) {
|
|
2827
|
+
list = &i_runtime->pagesFull;
|
|
2828
|
+
} else {
|
|
2829
|
+
list = &i_runtime->pagesOpen;
|
|
2830
|
+
}
|
|
2831
|
+
|
|
2832
|
+
PushCodePage(list, i_codePage); m3log (emit, "release page: %d to queue: '%s'", i_codePage->info.sequence, pageFull ? "full" : "open")
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2836
|
+
|
|
2837
|
+
IM3CodePage AcquireCodePageWithCapacity (IM3Runtime i_runtime, u32 i_minLineCount)
|
|
2838
|
+
{
|
|
2839
|
+
IM3CodePage page = RemoveCodePageOfCapacity(&i_runtime->pagesOpen, i_minLineCount);
|
|
2840
|
+
|
|
2841
|
+
if (not page) {
|
|
2842
|
+
page = Environment_AcquireCodePage(i_runtime->environment, i_minLineCount);
|
|
2843
|
+
|
|
2844
|
+
if (not page) {
|
|
2845
|
+
page = NewCodePage(i_runtime, i_minLineCount);
|
|
2846
|
+
}
|
|
2847
|
+
|
|
2848
|
+
if (page) {
|
|
2849
|
+
i_runtime->numCodePages++;
|
|
2850
|
+
}
|
|
2851
|
+
}
|
|
2852
|
+
|
|
2853
|
+
if (page) { m3log (emit, "acquire page: %d", page->info.sequence);
|
|
2854
|
+
i_runtime->numActiveCodePages++;
|
|
2855
|
+
}
|
|
2856
|
+
|
|
2857
|
+
return page;
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
|
|
2861
|
+
IM3CodePage AcquireCodePage (IM3Runtime i_runtime)
|
|
2862
|
+
{
|
|
2863
|
+
return AcquireCodePageWithCapacity(i_runtime, d_m3CodePageFreeLinesThreshold);
|
|
2864
|
+
}
|
|
2865
|
+
|
|
2866
|
+
|
|
2867
|
+
void ReleaseCodePage (IM3Runtime i_runtime, IM3CodePage i_codePage)
|
|
2868
|
+
{
|
|
2869
|
+
if (i_codePage) {
|
|
2870
|
+
ReleaseCodePageNoTrack(i_runtime, i_codePage);
|
|
2871
|
+
i_runtime->numActiveCodePages--;
|
|
2872
|
+
|
|
2873
|
+
#if defined(DEBUG)
|
|
2874
|
+
u32 numOpen = CountCodePages(i_runtime->pagesOpen);
|
|
2875
|
+
u32 numFull = CountCodePages(i_runtime->pagesFull);
|
|
2876
|
+
|
|
2877
|
+
m3log(runtime, "runtime: %p; open-pages: %d; full-pages: %d; active: %d; total: %d", i_runtime, numOpen, numFull, i_runtime->numActiveCodePages, i_runtime->numCodePages);
|
|
2878
|
+
|
|
2879
|
+
d_m3Assert(numOpen + numFull + i_runtime->numActiveCodePages == i_runtime->numCodePages);
|
|
2880
|
+
|
|
2881
|
+
# if d_m3LogCodePages
|
|
2882
|
+
dump_code_page(i_codePage, /* startPC: */ NULL);
|
|
2883
|
+
# endif
|
|
2884
|
+
#endif
|
|
2885
|
+
}
|
|
2886
|
+
}
|
|
2887
|
+
|
|
2888
|
+
|
|
2889
|
+
#if d_m3VerboseErrorMessages
|
|
2890
|
+
M3Result m3Error (M3Result i_result, IM3Runtime i_runtime, IM3Module i_module, IM3Function i_function,
|
|
2891
|
+
const char* const i_file, u32 i_lineNum, const char* const i_errorMessage, ...)
|
|
2892
|
+
{
|
|
2893
|
+
if (i_runtime) {
|
|
2894
|
+
i_runtime->error = (M3ErrorInfo){ .result = i_result, .runtime = i_runtime, .module = i_module, .function = i_function, .file = i_file, .line = i_lineNum };
|
|
2895
|
+
i_runtime->error.message = i_runtime->error_message;
|
|
2896
|
+
|
|
2897
|
+
va_list args;
|
|
2898
|
+
va_start(args, i_errorMessage);
|
|
2899
|
+
vsnprintf(i_runtime->error_message, sizeof(i_runtime->error_message), i_errorMessage, args);
|
|
2900
|
+
va_end(args);
|
|
2901
|
+
}
|
|
2902
|
+
|
|
2903
|
+
return i_result;
|
|
2904
|
+
}
|
|
2905
|
+
#endif
|
|
2906
|
+
|
|
2907
|
+
|
|
2908
|
+
void m3_GetErrorInfo (IM3Runtime i_runtime, M3ErrorInfo* o_info)
|
|
2909
|
+
{
|
|
2910
|
+
if (i_runtime) {
|
|
2911
|
+
*o_info = i_runtime->error;
|
|
2912
|
+
m3_ResetErrorInfo(i_runtime);
|
|
2913
|
+
}
|
|
2914
|
+
}
|
|
2915
|
+
|
|
2916
|
+
|
|
2917
|
+
void m3_ResetErrorInfo (IM3Runtime i_runtime)
|
|
2918
|
+
{
|
|
2919
|
+
if (i_runtime) {
|
|
2920
|
+
M3_INIT(i_runtime->error);
|
|
2921
|
+
i_runtime->error.message = "";
|
|
2922
|
+
}
|
|
2923
|
+
}
|
|
2924
|
+
|
|
2925
|
+
uint8_t* m3_GetMemory (IM3Module i_module, size_t* o_memorySizeInBytes, uint32_t i_memoryIndex)
|
|
2926
|
+
{
|
|
2927
|
+
uint8_t* memory = NULL;
|
|
2928
|
+
size_t size = 0;
|
|
2929
|
+
|
|
2930
|
+
if (i_module and i_memoryIndex < i_module->numMemories) {
|
|
2931
|
+
IM3Memory mem = i_module->memories[i_memoryIndex];
|
|
2932
|
+
|
|
2933
|
+
if (mem->mallocated) {
|
|
2934
|
+
size = mem->mallocated->length;
|
|
2935
|
+
|
|
2936
|
+
if (size) {
|
|
2937
|
+
memory = m3MemData(mem->mallocated);
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
|
|
2942
|
+
if (o_memorySizeInBytes) {
|
|
2943
|
+
*o_memorySizeInBytes = size;
|
|
2944
|
+
}
|
|
2945
|
+
|
|
2946
|
+
return memory;
|
|
2947
|
+
}
|
|
2948
|
+
|
|
2949
|
+
|
|
2950
|
+
size_t m3_GetMemorySize (IM3Module i_module, uint32_t i_memoryIndex)
|
|
2951
|
+
{
|
|
2952
|
+
if (not i_module or i_memoryIndex >= i_module->numMemories) {
|
|
2953
|
+
return 0;
|
|
2954
|
+
}
|
|
2955
|
+
|
|
2956
|
+
IM3Memory mem = i_module->memories[i_memoryIndex];
|
|
2957
|
+
|
|
2958
|
+
return mem->mallocated ? mem->mallocated->length : 0;
|
|
2959
|
+
}
|
|
2960
|
+
|
|
2961
|
+
|
|
2962
|
+
size_t m3_GetMemorySizeAt (const void* i_memory)
|
|
2963
|
+
{
|
|
2964
|
+
if (not i_memory) {
|
|
2965
|
+
return 0;
|
|
2966
|
+
}
|
|
2967
|
+
|
|
2968
|
+
// the header sits immediately before the data it describes
|
|
2969
|
+
const M3MemoryHeader* header = ((const M3MemoryHeader*)i_memory) - 1;
|
|
2970
|
+
|
|
2971
|
+
return header->length;
|
|
2972
|
+
}
|
|
2973
|
+
|
|
2974
|
+
|
|
2975
|
+
M3Result m3_FindExportedMemory (IM3Module i_module, cstr_t i_name, u32* o_memoryIndex)
|
|
2976
|
+
{
|
|
2977
|
+
if (not i_module or not i_name or not o_memoryIndex) {
|
|
2978
|
+
return m3Err_unknownMemory;
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2981
|
+
const M3Export* e = Module_FindExport(i_module, d_externalKind_memory, i_name);
|
|
2982
|
+
|
|
2983
|
+
if (not e) {
|
|
2984
|
+
return m3Err_unknownMemory;
|
|
2985
|
+
}
|
|
2986
|
+
|
|
2987
|
+
*o_memoryIndex = e->index;
|
|
2988
|
+
return m3Err_none;
|
|
2989
|
+
}
|
|
2990
|
+
|
|
2991
|
+
|
|
2992
|
+
bool Module_HasLinkedHostImport (IM3Module i_module, cstr_t i_importModule)
|
|
2993
|
+
{
|
|
2994
|
+
for (u32 i = 0; i < i_module->numFunctions; ++i) {
|
|
2995
|
+
IM3Function f = &i_module->functions[i];
|
|
2996
|
+
|
|
2997
|
+
// hostMemory is set when a host function is bound to the import, so it
|
|
2998
|
+
// is also the mark of one - see CompileRawFunction
|
|
2999
|
+
if (not f->hostMemory or f->wasm or f->resolved) {
|
|
3000
|
+
continue;
|
|
3001
|
+
}
|
|
3002
|
+
|
|
3003
|
+
if (f->import.moduleUtf8 and strcmp(f->import.moduleUtf8, i_importModule) == 0) {
|
|
3004
|
+
return true;
|
|
3005
|
+
}
|
|
3006
|
+
}
|
|
3007
|
+
|
|
3008
|
+
return false;
|
|
3009
|
+
}
|
|
3010
|
+
|
|
3011
|
+
|
|
3012
|
+
M3Result m3_BindImportMemory (IM3Module io_module, cstr_t i_importModule, u32 i_memoryIndex)
|
|
3013
|
+
{
|
|
3014
|
+
if (not io_module or not i_importModule) {
|
|
3015
|
+
return m3Err_moduleNotLinked;
|
|
3016
|
+
}
|
|
3017
|
+
|
|
3018
|
+
if (i_memoryIndex >= io_module->numMemories) {
|
|
3019
|
+
return m3Err_unknownMemory;
|
|
3020
|
+
}
|
|
3021
|
+
|
|
3022
|
+
// the memory itself, not the index: linking may have pointed this slot at
|
|
3023
|
+
// another module's memory, and growing one reallocates behind it
|
|
3024
|
+
IM3Memory memory = io_module->memories[i_memoryIndex];
|
|
3025
|
+
|
|
3026
|
+
const bool wildcardModule = (strcmp(i_importModule, "*") == 0);
|
|
3027
|
+
|
|
3028
|
+
for (u32 i = 0; i < io_module->numFunctions; ++i) {
|
|
3029
|
+
IM3Function f = &io_module->functions[i];
|
|
3030
|
+
|
|
3031
|
+
// only an import that a host function was bound to has a memory to
|
|
3032
|
+
// pin: one with a body of its own reads memory through the interpreter,
|
|
3033
|
+
// and one resolved to another module's export runs that module's code
|
|
3034
|
+
if (not f->hostMemory or f->wasm or f->resolved) {
|
|
3035
|
+
continue;
|
|
3036
|
+
}
|
|
3037
|
+
|
|
3038
|
+
if (wildcardModule or (f->import.moduleUtf8 and strcmp(f->import.moduleUtf8, i_importModule) == 0)) {
|
|
3039
|
+
f->hostMemory = memory;
|
|
3040
|
+
}
|
|
3041
|
+
}
|
|
3042
|
+
|
|
3043
|
+
return m3Err_none;
|
|
3044
|
+
}
|
|
3045
|
+
|
|
3046
|
+
|
|
3047
|
+
M3BacktraceInfo* m3_GetBacktrace (IM3Runtime i_runtime)
|
|
3048
|
+
{
|
|
3049
|
+
#if d_m3RecordBacktraces
|
|
3050
|
+
return &i_runtime->backtrace;
|
|
3051
|
+
#else
|
|
3052
|
+
return NULL;
|
|
3053
|
+
#endif
|
|
3054
|
+
}
|
|
3055
|
+
|
|
3056
|
+
|
|
3057
|
+
//-------------------------------------------------------------------------------------------------------------------------------
|
|
3058
|
+
// snapshots & suspendable execution
|
|
3059
|
+
//-------------------------------------------------------------------------------------------------------------------------------
|
|
3060
|
+
|
|
3061
|
+
void m3_SetSuspendable (IM3Runtime io_runtime, bool i_suspendable)
|
|
3062
|
+
{
|
|
3063
|
+
if (io_runtime) {
|
|
3064
|
+
io_runtime->isSuspendable = i_suspendable;
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
|
|
3068
|
+
void m3_RequestSuspend (IM3Runtime io_runtime)
|
|
3069
|
+
{
|
|
3070
|
+
if (io_runtime) {
|
|
3071
|
+
io_runtime->suspendRequested = true;
|
|
3072
|
+
}
|
|
3073
|
+
}
|
|
3074
|
+
|
|
3075
|
+
bool m3_IsSuspended (IM3Runtime i_runtime)
|
|
3076
|
+
{
|
|
3077
|
+
if (!i_runtime) {
|
|
3078
|
+
return false;
|
|
3079
|
+
}
|
|
3080
|
+
#if d_m3HasStackSwitching
|
|
3081
|
+
if (i_runtime->rootContinuation && i_runtime->rootContinuation->state == cont_suspended) {
|
|
3082
|
+
return true;
|
|
3083
|
+
}
|
|
3084
|
+
#endif
|
|
3085
|
+
return false;
|
|
3086
|
+
}
|
|
3087
|
+
|
|
3088
|
+
M3Result m3_ResumeRuntime (IM3Runtime io_runtime)
|
|
3089
|
+
{
|
|
3090
|
+
if (!io_runtime) {
|
|
3091
|
+
return m3Err_mallocFailed;
|
|
3092
|
+
}
|
|
3093
|
+
if (!io_runtime->isSuspendable) {
|
|
3094
|
+
return m3Err_none;
|
|
3095
|
+
}
|
|
3096
|
+
#if d_m3HasSnapshots
|
|
3097
|
+
// a restore that failed leaves nothing to resume, and says so every time
|
|
3098
|
+
// rather than resuming nothing successfully
|
|
3099
|
+
if (io_runtime->rootContinuation and io_runtime->rootContinuation->entryFunction and
|
|
3100
|
+
io_runtime->rootContinuation->entryFunction->module->isUnusable) {
|
|
3101
|
+
return "a snapshot failed to restore into the module, so it cannot run";
|
|
3102
|
+
}
|
|
3103
|
+
#endif
|
|
3104
|
+
#if d_m3HasStackSwitching
|
|
3105
|
+
if (!io_runtime->rootContinuation || io_runtime->rootContinuation->state != cont_suspended) {
|
|
3106
|
+
return m3Err_none;
|
|
3107
|
+
}
|
|
3108
|
+
// what finishes returns into the bottom of the stack, where it was entered
|
|
3109
|
+
io_runtime->stack = io_runtime->originStack;
|
|
3110
|
+
|
|
3111
|
+
// this is the outermost call again, as far as a host function calling back
|
|
3112
|
+
// into Wasm from inside it can tell
|
|
3113
|
+
io_runtime->entered = io_runtime->rootContinuation->entryFunction;
|
|
3114
|
+
io_runtime->callNesting++;
|
|
3115
|
+
|
|
3116
|
+
d_m3StackLimitEnter(io_runtime);
|
|
3117
|
+
io_runtime->resumePastCheck = true;
|
|
3118
|
+
M3Result result = (M3Result)ResumeContinuation(io_runtime, io_runtime->rootContinuation);
|
|
3119
|
+
io_runtime->resumePastCheck = false;
|
|
3120
|
+
d_m3StackLimitLeave(io_runtime);
|
|
3121
|
+
|
|
3122
|
+
# if d_m3HasExceptionHandling
|
|
3123
|
+
if (M3_UNLIKELY(result == m3Err_pendingException)) {
|
|
3124
|
+
result = m3Err_trapUncaughtException;
|
|
3125
|
+
}
|
|
3126
|
+
RecordUncaughtException(io_runtime, result);
|
|
3127
|
+
# endif
|
|
3128
|
+
|
|
3129
|
+
io_runtime->callNesting--;
|
|
3130
|
+
|
|
3131
|
+
# if d_m3HasExceptionHandling
|
|
3132
|
+
if (result != m3Err_continuationSuspended and io_runtime->callNesting == 0) {
|
|
3133
|
+
FreeExceptions(io_runtime);
|
|
3134
|
+
}
|
|
3135
|
+
# endif
|
|
3136
|
+
|
|
3137
|
+
// the results are where the paused call left room for them, so a finished
|
|
3138
|
+
// resume can be asked for them the way the call itself could have been
|
|
3139
|
+
io_runtime->lastCalled = (result == m3Err_none) ? io_runtime->rootContinuation->entryFunction : NULL;
|
|
3140
|
+
|
|
3141
|
+
Runtime_PlaceCallStack(io_runtime);
|
|
3142
|
+
return result;
|
|
3143
|
+
#else
|
|
3144
|
+
return m3Err_none;
|
|
3145
|
+
#endif
|
|
3146
|
+
}
|