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.
Files changed (224) hide show
  1. package/AGENTS.md +1 -1
  2. package/dist/src/cli/extensions.js +11 -3
  3. package/dist/src/cli/extensions.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/cli/help.js +1 -1
  6. package/dist/src/codegen/generator/state.js +7 -0
  7. package/dist/src/codegen/generator/state.js.map +1 -1
  8. package/dist/src/codegen/generator/tables.js +19 -1
  9. package/dist/src/codegen/generator/tables.js.map +1 -1
  10. package/dist/src/driver/bundler/graph.js +9 -5
  11. package/dist/src/driver/bundler/graph.js.map +1 -1
  12. package/dist/src/driver/fingerprint.d.ts +2 -2
  13. package/dist/src/driver/fingerprint.js +3 -3
  14. package/dist/src/driver/fingerprint.js.map +1 -1
  15. package/dist/src/driver/link-features.js +1 -1
  16. package/dist/src/driver/link-features.js.map +1 -1
  17. package/dist/src/driver/paths.js +8 -4
  18. package/dist/src/driver/paths.js.map +1 -1
  19. package/dist/src/driver/runtime-objects.d.ts +1 -1
  20. package/dist/src/driver/runtime-objects.js +13 -1
  21. package/dist/src/driver/runtime-objects.js.map +1 -1
  22. package/dist/src/driver/version.d.ts +1 -1
  23. package/dist/src/driver/version.js +1 -1
  24. package/dist/src/extensions/catalog.js +5 -1
  25. package/dist/src/extensions/catalog.js.map +1 -1
  26. package/dist/src/extensions/gui/index.js +0 -1
  27. package/dist/src/extensions/gui/index.js.map +1 -1
  28. package/dist/src/extensions/link-feature.d.ts +8 -0
  29. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  30. package/dist/src/extensions/node/stream-web/index.js +59 -4
  31. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  32. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  33. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  34. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  35. package/dist/src/extensions/registry.d.ts +9 -1
  36. package/dist/src/extensions/registry.js +4 -1
  37. package/dist/src/extensions/registry.js.map +1 -1
  38. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  39. package/dist/src/extensions/wasm-asset.js +28 -0
  40. package/dist/src/extensions/wasm-asset.js.map +1 -0
  41. package/dist/src/extensions/wasm.d.ts +17 -0
  42. package/dist/src/extensions/wasm.js +68 -0
  43. package/dist/src/extensions/wasm.js.map +1 -0
  44. package/dist/src/extensions/webworker/index.d.ts +24 -0
  45. package/dist/src/extensions/webworker/index.js +54 -0
  46. package/dist/src/extensions/webworker/index.js.map +1 -0
  47. package/dist/src/index.d.ts +1 -0
  48. package/dist/src/index.js +1 -0
  49. package/dist/src/index.js.map +1 -1
  50. package/dist/tests/driver/link-features.test.js +9 -0
  51. package/dist/tests/driver/link-features.test.js.map +1 -1
  52. package/dist/tests/e2e/async-fs.test.js +44 -0
  53. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  54. package/dist/tests/e2e/harness.d.ts +6 -0
  55. package/dist/tests/e2e/harness.js +13 -1
  56. package/dist/tests/e2e/harness.js.map +1 -1
  57. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  58. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  59. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  60. package/dist/tests/e2e/node-modules.test.js +1 -1
  61. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  62. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  63. package/dist/tests/e2e/web-streams.test.js +274 -0
  64. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  65. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  66. package/dist/tests/e2e/web-worker.test.js +74 -0
  67. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  68. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  69. package/dist/tests/e2e/webassembly.test.js +113 -0
  70. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  71. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  72. package/dist/tests/e2e/worker-api.test.js +460 -0
  73. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  74. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  75. package/dist/tests/e2e/worker-threads.test.js +160 -0
  76. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  77. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  78. package/dist/tests/extensions/wasm.test.js +53 -0
  79. package/dist/tests/extensions/wasm.test.js.map +1 -0
  80. package/doc/ai/cli.md +1 -1
  81. package/doc/ai/contributing.md +11 -4
  82. package/doc/ai/extensions.md +25 -2
  83. package/doc/ai/language-support.md +14 -2
  84. package/doc/ai/zh-CN/cli.md +1 -1
  85. package/doc/ai/zh-CN/contributing.md +8 -3
  86. package/doc/ai/zh-CN/extensions.md +23 -2
  87. package/doc/ai/zh-CN/language-support.md +13 -2
  88. package/doc/gui/api.md +1 -1
  89. package/doc/implemented/cli.md +1 -1
  90. package/doc/implemented/extensions.md +6 -5
  91. package/doc/implemented/node/stream-web.md +31 -0
  92. package/doc/implemented/node/worker-threads.md +88 -5
  93. package/doc/implemented/node.md +2 -1
  94. package/doc/implemented/streams.md +82 -0
  95. package/doc/implemented/wasm.md +80 -18
  96. package/doc/implemented/web-worker.md +84 -0
  97. package/doc/implemented.md +6 -2
  98. package/doc/unimplemented/deviations.md +1 -0
  99. package/doc/unimplemented/node/worker-threads.md +23 -0
  100. package/doc/unimplemented/node.md +1 -0
  101. package/doc/unimplemented/stdlib.md +2 -1
  102. package/doc/unimplemented/wasm.md +21 -9
  103. package/doc/unimplemented.md +1 -1
  104. package/doc/zh-CN/gui/api.md +1 -1
  105. package/doc/zh-CN/implemented/cli.md +1 -1
  106. package/doc/zh-CN/implemented/extensions.md +7 -6
  107. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  108. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  109. package/doc/zh-CN/implemented/node.md +2 -1
  110. package/doc/zh-CN/implemented/streams.md +60 -0
  111. package/doc/zh-CN/implemented/wasm.md +73 -16
  112. package/doc/zh-CN/implemented/web-worker.md +79 -0
  113. package/doc/zh-CN/implemented.md +6 -2
  114. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  115. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  116. package/doc/zh-CN/unimplemented/node.md +1 -0
  117. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  118. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  119. package/doc/zh-CN/unimplemented.md +1 -1
  120. package/llms.txt +2 -0
  121. package/package.json +8 -7
  122. package/runtime/ext_gui/browser.cpp +1 -1
  123. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  124. package/runtime/worker/worker_api.c +422 -0
  125. package/runtime/worker/worker_codec.c +489 -0
  126. package/runtime/worker/worker_core.c +445 -0
  127. package/runtime/worker/worker_internal.h +223 -0
  128. package/runtime/worker/worker_platform.c +344 -0
  129. package/runtime/worker/worker_ports.c +203 -0
  130. package/runtime/worker/worker_transport.c +464 -0
  131. package/runtime/worker/worker_wire.c +150 -0
  132. package/runtime/xt_builtins.c +15 -0
  133. package/runtime/xt_constructors.c +49 -2
  134. package/runtime/xt_stdlib2/error.inc +4 -0
  135. package/runtime/xt_streams/common.inc +232 -0
  136. package/runtime/xt_streams/constructors.inc +232 -0
  137. package/runtime/xt_streams/encoding.inc +124 -0
  138. package/runtime/xt_streams/readable-extra.inc +372 -0
  139. package/runtime/xt_streams/readable.inc +415 -0
  140. package/runtime/xt_streams/transform.inc +195 -0
  141. package/runtime/xt_streams/writable.inc +542 -0
  142. package/runtime/xt_streams.c +30 -0
  143. package/runtime/xt_wasm/wasm.h +148 -0
  144. package/runtime/xt_wasm/wasm_api.c +357 -0
  145. package/runtime/xt_wasm/wasm_api.h +37 -0
  146. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  147. package/runtime/xt_wasm/wasm_instance.c +591 -0
  148. package/runtime/xt_wasm/wasm_module.c +270 -0
  149. package/runtime/xt_wasm/wasm_values.c +177 -0
  150. package/runtime/xt_webassembly.c +15 -4
  151. package/scripts/fetch-vendored.ts +187 -20
  152. package/scripts/package-release.ts +3 -2
  153. package/src/cli/extensions.ts +11 -3
  154. package/src/cli/help.ts +1 -1
  155. package/src/codegen/generator/state.ts +7 -0
  156. package/src/codegen/generator/tables.ts +19 -1
  157. package/src/driver/bundler/graph.ts +9 -5
  158. package/src/driver/fingerprint.ts +3 -3
  159. package/src/driver/link-features.ts +1 -1
  160. package/src/driver/paths.ts +8 -4
  161. package/src/driver/runtime-objects.ts +13 -1
  162. package/src/driver/version.ts +1 -1
  163. package/src/extensions/catalog.ts +5 -1
  164. package/src/extensions/gui/index.ts +0 -1
  165. package/src/extensions/link-feature.ts +8 -0
  166. package/src/extensions/node/stream-web/index.ts +59 -4
  167. package/src/extensions/node/worker_threads/index.ts +24 -11
  168. package/src/extensions/registry.ts +13 -2
  169. package/src/extensions/wasm-asset.ts +32 -0
  170. package/src/extensions/wasm.ts +74 -0
  171. package/src/extensions/webworker/index.ts +58 -0
  172. package/src/index.ts +1 -0
  173. package/vendor/wasm3/.pin +1 -0
  174. package/vendor/wasm3/LICENSE +21 -0
  175. package/vendor/wasm3/m3_api_libc.c +265 -0
  176. package/vendor/wasm3/m3_api_libc.h +19 -0
  177. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  178. package/vendor/wasm3/m3_api_tracer.c +173 -0
  179. package/vendor/wasm3/m3_api_tracer.h +19 -0
  180. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  181. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  182. package/vendor/wasm3/m3_api_wasi.h +74 -0
  183. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  184. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  185. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  186. package/vendor/wasm3/m3_atomic.h +114 -0
  187. package/vendor/wasm3/m3_bind.c +174 -0
  188. package/vendor/wasm3/m3_bind.h +20 -0
  189. package/vendor/wasm3/m3_code.c +232 -0
  190. package/vendor/wasm3/m3_code.h +74 -0
  191. package/vendor/wasm3/m3_compile.c +6603 -0
  192. package/vendor/wasm3/m3_compile.h +354 -0
  193. package/vendor/wasm3/m3_config.h +448 -0
  194. package/vendor/wasm3/m3_config_platforms.h +345 -0
  195. package/vendor/wasm3/m3_core.c +1034 -0
  196. package/vendor/wasm3/m3_core.h +526 -0
  197. package/vendor/wasm3/m3_deterministic.c +65 -0
  198. package/vendor/wasm3/m3_deterministic.h +59 -0
  199. package/vendor/wasm3/m3_env.c +3146 -0
  200. package/vendor/wasm3/m3_env.h +748 -0
  201. package/vendor/wasm3/m3_exception.h +33 -0
  202. package/vendor/wasm3/m3_exec.c +8 -0
  203. package/vendor/wasm3/m3_exec.h +4110 -0
  204. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  205. package/vendor/wasm3/m3_exec_defs.h +76 -0
  206. package/vendor/wasm3/m3_function.c +278 -0
  207. package/vendor/wasm3/m3_function.h +244 -0
  208. package/vendor/wasm3/m3_host.h +226 -0
  209. package/vendor/wasm3/m3_host_none.h +76 -0
  210. package/vendor/wasm3/m3_host_posix.h +543 -0
  211. package/vendor/wasm3/m3_host_win32.h +253 -0
  212. package/vendor/wasm3/m3_info.c +616 -0
  213. package/vendor/wasm3/m3_info.h +38 -0
  214. package/vendor/wasm3/m3_math_utils.h +560 -0
  215. package/vendor/wasm3/m3_module.c +431 -0
  216. package/vendor/wasm3/m3_parse.c +1549 -0
  217. package/vendor/wasm3/m3_snapshot.c +3825 -0
  218. package/vendor/wasm3/m3_validate.c +1770 -0
  219. package/vendor/wasm3/m3_validate.h +25 -0
  220. package/vendor/wasm3/m3_wasi.h +10 -0
  221. package/vendor/wasm3/m3_xxh64.c +211 -0
  222. package/vendor/wasm3/wasm3.h +684 -0
  223. package/vendor/wasm3/wasm3_defs.h +350 -0
  224. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -0,0 +1,1770 @@
1
+ //
2
+ // m3_validate.c
3
+ //
4
+ // Pre-pass WebAssembly bytecode validator.
5
+ // Implements the spec's type-checking algorithm with operand/control stacks.
6
+ //
7
+
8
+ #include "m3_validate.h"
9
+ #include "m3_exception.h"
10
+ #include "m3_info.h"
11
+
12
+ #if d_m3EnableValidation
13
+
14
+ // clang-format off
15
+
16
+ // The spec's bottom type: an operand of unknown type on an unreachable stack,
17
+ // which unifies with every concrete type. Deliberately not c_m3Type_unknown -
18
+ // that one means "invalid type" and must never be accepted by a type check.
19
+ #define c_valBottom 0xFF
20
+
21
+ // ---------- Control frame ----------
22
+
23
+ typedef struct {
24
+ m3opcode_t opcode;
25
+ u16 height; // operand stack height at block entry
26
+ u16 param_count;
27
+ u16 result_count;
28
+ IM3FuncType type; // block type (for params/results)
29
+ bool is_unreachable;
30
+ } ValCtrlFrame;
31
+
32
+ // ---------- Validator context ----------
33
+
34
+ struct ValCtx {
35
+ bytes_t wasm;
36
+ bytes_t wasmEnd;
37
+ IM3Module module;
38
+ IM3Function function;
39
+
40
+ m3type_t opd [d_m3ValStack];
41
+ u16 opdTop;
42
+
43
+ ValCtrlFrame ctrl [d_m3ValCtrlDepth];
44
+ u16 ctrlTop;
45
+
46
+ m3type_t localTypes [d_m3ValStack];
47
+ u16 numLocals;
48
+ };
49
+
50
+ // A memory op is only valid if the memory it names is in the module's index
51
+ // space; for a module that declares none, every index is out of range.
52
+ static bool v_has_memory_idx (ValCtx * v, u32 i_memoryIdx)
53
+ {
54
+ return v->module and i_memoryIdx < v->module->numMemories;
55
+ }
56
+
57
+ // The type of the addresses, sizes and page counts a memory instruction takes:
58
+ // whichever the memory it names was declared with. Only ever called after
59
+ // v_has_memory_idx has said the index is in range.
60
+ static m3type_t v_memory_addrtype (ValCtx * v, u32 i_memoryIdx)
61
+ {
62
+ return Memory_AddrType (v->module->memories [i_memoryIdx]);
63
+ }
64
+
65
+ // Spec: the static offset of a memory access must be in range of the address
66
+ // type, so anything at all for a 64-bit memory and below 2^32 for a 32-bit one.
67
+ static bool v_offset_in_range (ValCtx * v, u32 i_memoryIdx, u64 i_offset)
68
+ {
69
+ return v_memory_addrtype (v, i_memoryIdx) == c_m3Type_i64 or i_offset <= 0xFFFFFFFFull;
70
+ }
71
+
72
+ // The type a table's indexes and sizes are given in - table64 is the same
73
+ // choice as memory64, made per table. Only ever called once the table index has
74
+ // been checked against the module's index space.
75
+ static m3type_t v_table_addrtype (ValCtx * v, u32 i_tableIdx)
76
+ {
77
+ return Table_AddrType (v->module->tables [i_tableIdx]);
78
+ }
79
+
80
+ // Spec: the alignment immediate of a memory access must not be larger than the
81
+ // natural alignment of the operation. Natural alignment: 8-bit=0, 16-bit=1,
82
+ // 32-bit=2, 64-bit=3.
83
+ static u32 v_max_align (m3opcode_t opcode)
84
+ {
85
+ switch (opcode) {
86
+ case 0x2c: case 0x2d: // i32.load8_s, i32.load8_u
87
+ case 0x30: case 0x31: // i64.load8_s, i64.load8_u
88
+ case 0x3a: // i32.store8
89
+ case 0x3c: // i64.store8
90
+ return 0;
91
+ case 0x2e: case 0x2f: // i32.load16_s, i32.load16_u
92
+ case 0x32: case 0x33: // i64.load16_s, i64.load16_u
93
+ case 0x3b: // i32.store16
94
+ case 0x3d: // i64.store16
95
+ return 1;
96
+ case 0x29: // i64.load
97
+ case 0x2b: // f64.load
98
+ case 0x37: // i64.store
99
+ case 0x39: // f64.store
100
+ return 3;
101
+ default: // 32-bit accesses, and a safe fallback
102
+ return 2;
103
+ }
104
+ }
105
+
106
+ #if d_m3HasAtomics
107
+
108
+ // What an instruction of the 0xFE prefix (other than atomic.fence) touches and
109
+ // takes: the width in bytes of the cell it addresses, how many operands it pops
110
+ // counting the address, the type of every operand but the address - a wait's
111
+ // timeout aside - and what it pushes. False for a sub-opcode that is not one.
112
+ //
113
+ // Outside notify and wait, the accesses come in runs of seven, one per pairing of
114
+ // value type and cell width: i32/32, i64/64, i32/8, i32/16, i64/8, i64/16, i64/32.
115
+ static bool v_atomic_shape (u32 sub, u32 * o_width, u32 * o_numOperands, m3type_t * o_valueType, m3type_t * o_resultType)
116
+ {
117
+ static const u8 c_widths [7] = { 4, 8, 1, 2, 1, 2, 4 };
118
+ static const m3type_t c_types [7] = {
119
+ c_m3Type_i32, c_m3Type_i64,
120
+ c_m3Type_i32, c_m3Type_i32,
121
+ c_m3Type_i64, c_m3Type_i64, c_m3Type_i64
122
+ };
123
+
124
+ u32 variant;
125
+
126
+ if (sub <= 0x02) { // memory.atomic.notify, wait32, wait64
127
+ *o_width = (sub == 0x02) ? 8 : 4;
128
+ *o_numOperands = (sub == 0x00) ? 2 : 3;
129
+ *o_valueType = (sub == 0x02) ? c_m3Type_i64 : c_m3Type_i32;
130
+ *o_resultType = c_m3Type_i32;
131
+ return true;
132
+ }
133
+
134
+ if (sub < 0x10 or sub > c_waOp_lastAtomic) return false;
135
+
136
+ if (sub <= 0x16) { // loads
137
+ variant = sub - 0x10; *o_numOperands = 1;
138
+ } else if (sub <= 0x1d) { // stores
139
+ variant = sub - 0x17; *o_numOperands = 2;
140
+ } else if (sub <= 0x47) { // add, sub, and, or, xor, xchg
141
+ variant = (sub - 0x1e) % 7; *o_numOperands = 2;
142
+ } else { // cmpxchg
143
+ variant = sub - 0x48; *o_numOperands = 3;
144
+ }
145
+
146
+ *o_width = c_widths [variant];
147
+ *o_valueType = c_types [variant];
148
+ *o_resultType = (sub >= 0x17 and sub <= 0x1d) ? c_m3Type_none : c_types [variant];
149
+ return true;
150
+ }
151
+
152
+ #endif
153
+
154
+ // ---------- Operand stack ----------
155
+
156
+ static M3Result v_push (ValCtx * v, m3type_t type)
157
+ {
158
+ if (M3_UNLIKELY(v->opdTop >= d_m3ValStack))
159
+ return m3Err_functionStackOverflow;
160
+ v->opd[v->opdTop++] = type;
161
+ return m3Err_none;
162
+ }
163
+
164
+ static M3Result v_pop (ValCtx * v, m3type_t * o_type)
165
+ {
166
+ ValCtrlFrame * f = &v->ctrl[v->ctrlTop - 1];
167
+ if (M3_UNLIKELY(v->opdTop == f->height)) {
168
+ if (f->is_unreachable) { *o_type = c_valBottom; return m3Err_none; }
169
+ return m3Err_functionStackUnderrun;
170
+ }
171
+ *o_type = v->opd[--v->opdTop];
172
+ return m3Err_none;
173
+ }
174
+
175
+ static M3Result v_pop_expect (ValCtx * v, m3type_t expect, m3type_t * o_actual)
176
+ {
177
+ m3type_t actual = c_valBottom;
178
+ M3Result r = v_pop(v, &actual);
179
+ if (M3_UNLIKELY(r)) return r;
180
+ if (M3_UNLIKELY(expect != c_valBottom && actual != c_valBottom && actual != expect))
181
+ return m3Err_typeMismatch;
182
+ *o_actual = (actual == c_valBottom) ? expect : actual;
183
+ return m3Err_none;
184
+ }
185
+
186
+ // ---------- Control stack ----------
187
+
188
+ static M3Result v_push_ctrl (ValCtx * v, m3opcode_t op, IM3FuncType type)
189
+ {
190
+ if (M3_UNLIKELY(v->ctrlTop >= d_m3ValCtrlDepth))
191
+ return m3Err_functionStackOverflow;
192
+ ValCtrlFrame * f = &v->ctrl[v->ctrlTop++];
193
+ f->opcode = op;
194
+ f->type = type;
195
+ f->param_count = type ? type->numArgs : 0;
196
+ f->result_count = type ? type->numRets : 0;
197
+ f->height = v->opdTop;
198
+ f->is_unreachable = false;
199
+ return m3Err_none;
200
+ }
201
+
202
+ static M3Result v_pop_ctrl (ValCtx * v, ValCtrlFrame * o_frame)
203
+ {
204
+ if (M3_UNLIKELY(v->ctrlTop == 0))
205
+ return m3Err_wasmMalformed;
206
+ ValCtrlFrame * f = &v->ctrl[v->ctrlTop - 1];
207
+ // pop result types
208
+ if (f->type) {
209
+ for (u16 i = f->result_count; i > 0; i--) {
210
+ m3type_t a;
211
+ M3Result r = v_pop_expect(v, BaseTypeOf(f->type->types[i - 1]), &a);
212
+ if (M3_UNLIKELY(r)) return r;
213
+ }
214
+ }
215
+ if (M3_UNLIKELY(v->opdTop != f->height))
216
+ return m3Err_typeCountMismatch;
217
+ if (o_frame) *o_frame = *f;
218
+ v->ctrlTop--;
219
+ return m3Err_none;
220
+ }
221
+
222
+ static void v_unreachable (ValCtx * v)
223
+ {
224
+ ValCtrlFrame * f = &v->ctrl[v->ctrlTop - 1];
225
+ v->opdTop = f->height;
226
+ f->is_unreachable = true;
227
+ }
228
+
229
+ // Label types: loop -> params, block/if/else/func -> results
230
+ static u16 v_label_n (ValCtrlFrame * f)
231
+ {
232
+ return (f->opcode == 0x03) ? f->param_count : f->result_count;
233
+ }
234
+
235
+ static m3type_t v_label_t (ValCtrlFrame * f, u16 i)
236
+ {
237
+ if (!f->type) return c_m3Type_none;
238
+ if (f->opcode == 0x03)
239
+ return BaseTypeOf(f->type->types[f->type->numRets + i]); // params
240
+ return BaseTypeOf(f->type->types[i]); // results
241
+ }
242
+
243
+ // The operand i_depth below the top, without popping. Anything at or below the
244
+ // current frame's height is implicitly bottom in an unreachable frame; reporting
245
+ // unknown there defers a genuine underrun to the pop that follows.
246
+ static m3type_t v_peek (ValCtx * v, u16 i_depth)
247
+ {
248
+ ValCtrlFrame * f = &v->ctrl[v->ctrlTop - 1];
249
+
250
+ if (v->opdTop <= i_depth || (u16)(v->opdTop - i_depth - 1) < f->height)
251
+ return c_valBottom;
252
+
253
+ return v->opd[v->opdTop - i_depth - 1];
254
+ }
255
+
256
+ // Pop label types for branch target
257
+ static M3Result v_pop_labels (ValCtx * v, ValCtrlFrame * tgt)
258
+ {
259
+ u16 n = v_label_n(tgt);
260
+ for (u16 i = n; i > 0; i--) {
261
+ m3type_t a;
262
+ M3Result r = v_pop_expect(v, v_label_t(tgt, i - 1), &a);
263
+ if (M3_UNLIKELY(r)) return r;
264
+ }
265
+ return m3Err_none;
266
+ }
267
+
268
+ // Push label types back
269
+ static M3Result v_push_labels (ValCtx * v, ValCtrlFrame * tgt)
270
+ {
271
+ u16 n = v_label_n(tgt);
272
+ for (u16 i = 0; i < n; i++) {
273
+ M3Result r = v_push(v, v_label_t(tgt, i));
274
+ if (M3_UNLIKELY(r)) return r;
275
+ }
276
+ return m3Err_none;
277
+ }
278
+
279
+ // ---------- Block type resolution ----------
280
+
281
+ static M3Result v_read_blocktype (ValCtx * v, IM3FuncType * o_type)
282
+ {
283
+ if (v->wasm >= v->wasmEnd)
284
+ return m3Err_wasmUnderrun;
285
+
286
+ #if d_m3HasTypedRefs
287
+ if (*v->wasm == d_waEncode_ref or *v->wasm == d_waEncode_refNull) {
288
+ m3type_t refType;
289
+ M3Result rr = ParseValueType(v->module, &refType, &v->wasm, v->wasmEnd);
290
+ if (rr) return rr;
291
+ IM3FuncType ftype = NULL;
292
+ rr = AllocFuncType(&ftype, 1);
293
+ if (rr) return rr;
294
+ ftype->numRets = 1;
295
+ ftype->types[0] = refType;
296
+ // on failure this leaves ftype NULL, which would read downstream as a
297
+ // block that returns nothing
298
+ rr = Environment_AddFuncType(v->module->environment, &ftype);
299
+ if (rr) return rr;
300
+ *o_type = ftype;
301
+ return m3Err_none;
302
+ }
303
+ #endif
304
+
305
+ i64 type;
306
+ M3Result r = ReadLebSigned(&type, 33, &v->wasm, v->wasmEnd);
307
+ if (r) return r;
308
+
309
+ if (type < 0) {
310
+ if (type < -64) return m3Err_invalidTypeId;
311
+ u8 valtype;
312
+ r = NormalizeType(&valtype, (i8)type);
313
+ if (r) return r;
314
+ IM3Environment env = v->module->environment;
315
+ *o_type = env->retFuncTypes[valtype];
316
+ } else {
317
+ if ((u32)type >= v->module->numFuncTypes)
318
+ return m3Err_unknownType;
319
+ *o_type = v->module->funcTypes[(u32)type];
320
+ }
321
+ return m3Err_none;
322
+ }
323
+
324
+ // ---------- Convenience ----------
325
+
326
+ static M3Result v_unop (ValCtx * v, m3type_t in, m3type_t out)
327
+ {
328
+ m3type_t a; M3Result r = v_pop_expect(v, in, &a);
329
+ if (M3_UNLIKELY(r)) return r;
330
+ return v_push(v, out);
331
+ }
332
+
333
+ // return_call/return_call_indirect return the callee's results straight to the enclosing
334
+ // function's caller, so the callee's result types must be the enclosing function's
335
+ static M3Result v_check_tail_results (ValCtx * v, IM3FuncType i_calleeType)
336
+ {
337
+ IM3FuncType ft = v->function ? v->function->funcType : NULL;
338
+
339
+ u16 numCalleeRets = i_calleeType ? i_calleeType->numRets : 0;
340
+ u16 numFuncRets = ft ? ft->numRets : 0;
341
+
342
+ if (numCalleeRets != numFuncRets)
343
+ return m3Err_typeMismatch;
344
+
345
+ for (u16 i = 0; i < numCalleeRets; i++) {
346
+ if (i_calleeType->types[i] != ft->types[i])
347
+ return m3Err_typeMismatch;
348
+ }
349
+
350
+ return m3Err_none;
351
+ }
352
+
353
+ static M3Result v_binop (ValCtx * v, m3type_t t)
354
+ {
355
+ m3type_t a; M3Result r;
356
+ r = v_pop_expect(v, t, &a); if (M3_UNLIKELY(r)) return r;
357
+ r = v_pop_expect(v, t, &a); if (M3_UNLIKELY(r)) return r;
358
+ return v_push(v, t);
359
+ }
360
+
361
+ static M3Result v_relop (ValCtx * v, m3type_t t)
362
+ {
363
+ m3type_t a; M3Result r;
364
+ r = v_pop_expect(v, t, &a); if (M3_UNLIKELY(r)) return r;
365
+ r = v_pop_expect(v, t, &a); if (M3_UNLIKELY(r)) return r;
366
+ return v_push(v, c_m3Type_i32);
367
+ }
368
+
369
+ static M3Result v_testop (ValCtx * v, m3type_t t)
370
+ {
371
+ return v_unop(v, t, c_m3Type_i32);
372
+ }
373
+
374
+ static M3Result v_cvtop (ValCtx * v, m3type_t in, m3type_t out)
375
+ {
376
+ return v_unop(v, in, out);
377
+ }
378
+
379
+
380
+ // ---------- Main validation loop ----------
381
+
382
+ #if d_m3HasExceptionHandling
383
+
384
+ // One catch clause of a try_table. The label is resolved in the context the
385
+ // try_table itself appears in - the spec checks the clauses against C, not
386
+ // against C extended with the block's own label - so depth 0 names the block
387
+ // enclosing the try, not the try. What the clause hands the label is the tag's
388
+ // payload, optionally followed by the exnref that reifies the caught exception.
389
+ static M3Result v_catch_clause (ValCtx * v)
390
+ {
391
+ u8 kind;
392
+ M3Result r = Read_u8(&kind, &v->wasm, v->wasmEnd);
393
+ if (r) return r;
394
+ if (kind > 0x03) return m3Err_wasmMalformed;
395
+
396
+ bool hasTag = (kind == 0x00 || kind == 0x01);
397
+ bool hasRef = (kind == 0x01 || kind == 0x03);
398
+
399
+ IM3FuncType tagType = NULL;
400
+ if (hasTag) {
401
+ u32 tagIndex;
402
+ r = ReadLEB_u32(&tagIndex, &v->wasm, v->wasmEnd);
403
+ if (r) return r;
404
+ if (!v->module || tagIndex >= v->module->numTags) return m3Err_unknownTag;
405
+ tagType = v->module->tags[tagIndex].type;
406
+ }
407
+
408
+ u32 depth;
409
+ r = ReadLEB_u32(&depth, &v->wasm, v->wasmEnd);
410
+ if (r) return r;
411
+ if (depth >= v->ctrlTop) return m3Err_unknownLabel;
412
+
413
+ ValCtrlFrame * tgt = &v->ctrl[v->ctrlTop - 1 - depth];
414
+
415
+ u16 numPayload = tagType ? tagType->numArgs : 0;
416
+ u16 numLabel = v_label_n(tgt);
417
+
418
+ if (numLabel != numPayload + (hasRef ? 1u : 0u))
419
+ return m3Err_typeCountMismatch;
420
+
421
+ for (u16 i = 0; i < numPayload; i++) {
422
+ if (v_label_t(tgt, i) != BaseTypeOf(tagType->types[tagType->numRets + i]))
423
+ return m3Err_typeMismatch;
424
+ }
425
+
426
+ if (hasRef and v_label_t(tgt, numPayload) != c_m3Type_exnref)
427
+ return m3Err_typeMismatch;
428
+
429
+ return m3Err_none;
430
+ }
431
+
432
+ #endif // d_m3HasExceptionHandling
433
+
434
+ #if d_m3HasStackSwitching
435
+
436
+ static M3Result v_resume_clause (ValCtx * v, u8 kind, u32 tagIdx, IM3FuncType ct)
437
+ {
438
+ if (kind > 0x01) return m3Err_wasmMalformed;
439
+ if (!v->module || tagIdx >= v->module->numTags) return m3Err_unknownTag;
440
+ IM3FuncType tagType = v->module->tags[tagIdx].type;
441
+
442
+ if (kind == 0x00) {
443
+ u32 depth;
444
+ M3Result r = ReadLEB_u32(&depth, &v->wasm, v->wasmEnd); if (r) return r;
445
+ if (depth >= v->ctrlTop) return m3Err_unknownLabel;
446
+
447
+ ValCtrlFrame * tgt = &v->ctrl[v->ctrlTop - 1 - depth];
448
+ u16 numPayload = tagType ? tagType->numArgs : 0;
449
+ u16 numLabel = v_label_n(tgt);
450
+
451
+ if (numLabel != numPayload + 1u) return m3Err_typeCountMismatch;
452
+
453
+ for (u16 j = 0; j < numPayload; j++) {
454
+ if (v_label_t(tgt, j) != BaseTypeOf(tagType->types[tagType->numRets + j]))
455
+ return m3Err_typeMismatch;
456
+ }
457
+ if (v_label_t(tgt, numPayload) != c_m3Type_contref)
458
+ return m3Err_typeMismatch;
459
+
460
+ #if d_m3HasTypedRefs
461
+ m3type_t labelContType = (tgt->opcode == 0x03)
462
+ ? tgt->type->types[tgt->type->numRets + numPayload]
463
+ : tgt->type->types[numPayload];
464
+
465
+ if (HeapTypeOf(labelContType) == d_m3Type_heapAbstract ||
466
+ HeapTypeOf(labelContType) == d_m3Type_heapNone) {
467
+ return m3Err_typeMismatch;
468
+ }
469
+
470
+ u32 heapType = HeapTypeOf(labelContType);
471
+ IM3FuncType labelCt = NULL;
472
+ for (u32 idx = 0; idx < v->module->numFuncTypes; ++idx) {
473
+ if (v->module->funcTypes[idx]->canonicalIndex == heapType &&
474
+ v->module->funcTypes[idx]->isContinuation) {
475
+ labelCt = v->module->funcTypes[idx];
476
+ break;
477
+ }
478
+ }
479
+ if (!labelCt) return m3Err_typeMismatch;
480
+
481
+ IM3FuncType labelFt = labelCt->contFuncType ? labelCt->contFuncType : labelCt;
482
+ IM3FuncType ft = ct->contFuncType ? ct->contFuncType : ct;
483
+
484
+ u16 expectedNumParams = tagType ? tagType->numRets : 0;
485
+ if (labelFt->numArgs != expectedNumParams) return m3Err_typeMismatch;
486
+ for (u16 p = 0; p < expectedNumParams; ++p) {
487
+ if (labelFt->types[labelFt->numRets + p] != tagType->types[p])
488
+ return m3Err_typeMismatch;
489
+ }
490
+
491
+ u16 expectedNumRets = ft ? ft->numRets : 0;
492
+ if (labelFt->numRets != expectedNumRets) return m3Err_typeMismatch;
493
+ for (u16 r_idx = 0; r_idx < expectedNumRets; ++r_idx) {
494
+ if (labelFt->types[r_idx] != ft->types[r_idx])
495
+ return m3Err_typeMismatch;
496
+ }
497
+ #endif
498
+ } else {
499
+ if (tagType && tagType->numArgs != 0) return m3Err_typeMismatch;
500
+ }
501
+ return m3Err_none;
502
+ }
503
+
504
+ #endif // d_m3HasStackSwitching
505
+
506
+ static M3Result v_validate_body (ValCtx * v)
507
+ {
508
+ M3Result r = m3Err_none;
509
+ m3type_t a = c_valBottom;
510
+
511
+ while (v->wasm < v->wasmEnd)
512
+ {
513
+ m3opcode_t opcode;
514
+ r = Read_opcode(&opcode, &v->wasm, v->wasmEnd);
515
+ if (M3_UNLIKELY(r)) return r;
516
+
517
+ switch (opcode)
518
+ {
519
+ // ---- Control ----
520
+ case 0x00: // unreachable
521
+ v_unreachable(v);
522
+ break;
523
+
524
+ case 0x01: // nop
525
+ break;
526
+
527
+ case 0x02: // block
528
+ case 0x03: // loop
529
+ case 0x04: // if
530
+ {
531
+ IM3FuncType bt = NULL;
532
+ r = v_read_blocktype(v, &bt);
533
+ if (r) return r;
534
+ if (opcode == 0x04) {
535
+ r = v_pop_expect(v, c_m3Type_i32, &a);
536
+ if (r) return r;
537
+ }
538
+ // Pop block params from caller stack
539
+ if (bt) {
540
+ for (u16 i = bt->numArgs; i > 0; i--) {
541
+ r = v_pop_expect(v, BaseTypeOf(bt->types[bt->numRets + i - 1]), &a);
542
+ if (r) return r;
543
+ }
544
+ }
545
+ r = v_push_ctrl(v, opcode, bt);
546
+ if (r) return r;
547
+ // Push params inside block
548
+ if (bt) {
549
+ for (u16 i = 0; i < bt->numArgs; i++) {
550
+ r = v_push(v, BaseTypeOf(bt->types[bt->numRets + i]));
551
+ if (r) return r;
552
+ }
553
+ }
554
+ break;
555
+ }
556
+
557
+ #if d_m3HasExceptionHandling
558
+ case 0x1f: // try_table
559
+ {
560
+ IM3FuncType bt = NULL;
561
+ r = v_read_blocktype(v, &bt);
562
+ if (r) return r;
563
+ if (bt) {
564
+ for (u16 i = bt->numArgs; i > 0; i--) {
565
+ r = v_pop_expect(v, BaseTypeOf(bt->types[bt->numRets + i - 1]), &a);
566
+ if (r) return r;
567
+ }
568
+ }
569
+ // the clauses are checked before the frame goes on, so their
570
+ // labels count from outside the try block
571
+ u32 numCatch;
572
+ r = ReadLEB_u32(&numCatch, &v->wasm, v->wasmEnd);
573
+ if (r) return r;
574
+ for (u32 i = 0; i < numCatch; i++) {
575
+ r = v_catch_clause(v);
576
+ if (r) return r;
577
+ }
578
+
579
+ r = v_push_ctrl(v, opcode, bt);
580
+ if (r) return r;
581
+
582
+ if (bt) {
583
+ for (u16 i = 0; i < bt->numArgs; i++) {
584
+ r = v_push(v, BaseTypeOf(bt->types[bt->numRets + i]));
585
+ if (r) return r;
586
+ }
587
+ }
588
+ break;
589
+ }
590
+
591
+ case 0x08: // throw
592
+ {
593
+ u32 tagIndex;
594
+ r = ReadLEB_u32(&tagIndex, &v->wasm, v->wasmEnd);
595
+ if (r) return r;
596
+ if (!v->module || tagIndex >= v->module->numTags) return m3Err_unknownTag;
597
+ IM3FuncType tt = v->module->tags[tagIndex].type;
598
+ if (tt) {
599
+ for (u16 i = tt->numArgs; i > 0; i--) {
600
+ r = v_pop_expect(v, BaseTypeOf(tt->types[tt->numRets + i - 1]), &a);
601
+ if (r) return r;
602
+ }
603
+ }
604
+ v_unreachable(v);
605
+ break;
606
+ }
607
+
608
+ case 0x0a: // throw_ref
609
+ r = v_pop_expect(v, c_m3Type_exnref, &a);
610
+ if (r) return r;
611
+ v_unreachable(v);
612
+ break;
613
+ #endif // d_m3HasExceptionHandling
614
+
615
+ case 0x05: // else
616
+ {
617
+ ValCtrlFrame frame;
618
+ r = v_pop_ctrl(v, &frame);
619
+ if (r) return r;
620
+ if (frame.opcode != 0x04)
621
+ return m3Err_wasmMalformed;
622
+ r = v_push_ctrl(v, 0x05, frame.type);
623
+ if (r) return r;
624
+ if (frame.type) {
625
+ for (u16 i = 0; i < frame.type->numArgs; i++) {
626
+ r = v_push(v, BaseTypeOf(frame.type->types[frame.type->numRets + i]));
627
+ if (r) return r;
628
+ }
629
+ }
630
+ break;
631
+ }
632
+
633
+ case 0x0b: // end
634
+ {
635
+ ValCtrlFrame frame;
636
+ r = v_pop_ctrl(v, &frame);
637
+ if (r) return r;
638
+ // Push results
639
+ if (frame.type) {
640
+ for (u16 i = 0; i < frame.result_count; i++) {
641
+ r = v_push(v, BaseTypeOf(frame.type->types[i]));
642
+ if (r) return r;
643
+ }
644
+ }
645
+ // If this was the outermost frame, we're done
646
+ if (v->ctrlTop == 0)
647
+ return m3Err_none;
648
+ break;
649
+ }
650
+
651
+ case 0x0c: // br
652
+ {
653
+ u32 depth;
654
+ r = ReadLEB_u32(&depth, &v->wasm, v->wasmEnd);
655
+ if (r) return r;
656
+ if (depth >= v->ctrlTop) return m3Err_unknownLabel;
657
+ ValCtrlFrame * tgt = &v->ctrl[v->ctrlTop - 1 - depth];
658
+ r = v_pop_labels(v, tgt);
659
+ if (r) return r;
660
+ v_unreachable(v);
661
+ break;
662
+ }
663
+
664
+ case 0x0d: // br_if
665
+ {
666
+ u32 depth;
667
+ r = ReadLEB_u32(&depth, &v->wasm, v->wasmEnd);
668
+ if (r) return r;
669
+ if (depth >= v->ctrlTop) return m3Err_unknownLabel;
670
+ r = v_pop_expect(v, c_m3Type_i32, &a);
671
+ if (r) return r;
672
+ ValCtrlFrame * tgt = &v->ctrl[v->ctrlTop - 1 - depth];
673
+ r = v_pop_labels(v, tgt);
674
+ if (r) return r;
675
+ r = v_push_labels(v, tgt);
676
+ if (r) return r;
677
+ break;
678
+ }
679
+
680
+ case 0x0e: // br_table
681
+ {
682
+ u32 count;
683
+ r = ReadLEB_u32(&count, &v->wasm, v->wasmEnd);
684
+ if (r) return r;
685
+ u32 defDepth = 0;
686
+ u16 arity = 0;
687
+ // First pass: read all depths and validate arity + types match default
688
+ bytes_t savedPos = v->wasm;
689
+ // Read all targets to find the default (last one)
690
+ for (u32 i = 0; i <= count; i++) {
691
+ u32 d;
692
+ r = ReadLEB_u32(&d, &v->wasm, v->wasmEnd);
693
+ if (r) return r;
694
+ if (d >= v->ctrlTop) return m3Err_unknownLabel;
695
+ if (i == count) defDepth = d;
696
+ }
697
+ // Now validate all labels match the default's types
698
+ ValCtrlFrame * defTgt = &v->ctrl[v->ctrlTop - 1 - defDepth];
699
+ arity = v_label_n(defTgt);
700
+ v->wasm = savedPos;
701
+
702
+ r = v_pop_expect(v, c_m3Type_i32, &a); // the index, before the labels are inspected
703
+ if (r) return r;
704
+
705
+ for (u32 i = 0; i <= count; i++) {
706
+ u32 d;
707
+ r = ReadLEB_u32(&d, &v->wasm, v->wasmEnd);
708
+ if (r) return r;
709
+ ValCtrlFrame * t = &v->ctrl[v->ctrlTop - 1 - d];
710
+ u16 n = v_label_n(t);
711
+ if (n != arity) return m3Err_typeCountMismatch;
712
+ // Spec: each target's types must be compatible with the operand
713
+ // stack, not identical to the default's. In unreachable code the
714
+ // operands are bottom, so the targets may legitimately differ.
715
+ for (u16 j = 0; j < n; j++) {
716
+ m3type_t want = v_label_t(t, n - 1 - j);
717
+ m3type_t have = v_peek(v, j);
718
+ if (want != c_valBottom && have != c_valBottom && want != have)
719
+ return m3Err_typeMismatch;
720
+ }
721
+ }
722
+ ValCtrlFrame * dt = &v->ctrl[v->ctrlTop - 1 - defDepth];
723
+ r = v_pop_labels(v, dt);
724
+ if (r) return r;
725
+ v_unreachable(v);
726
+ break;
727
+ }
728
+
729
+ case 0x0f: // return
730
+ {
731
+ IM3FuncType ft = v->function->funcType;
732
+ if (ft) {
733
+ for (u16 i = ft->numRets; i > 0; i--) {
734
+ r = v_pop_expect(v, BaseTypeOf(ft->types[i - 1]), &a);
735
+ if (r) return r;
736
+ }
737
+ }
738
+ v_unreachable(v);
739
+ break;
740
+ }
741
+
742
+ // ---- Call ----
743
+ case 0x10: // call
744
+ {
745
+ u32 idx;
746
+ r = ReadLEB_u32(&idx, &v->wasm, v->wasmEnd);
747
+ if (r) return r;
748
+ if (idx >= v->module->numFunctions) return m3Err_unknownFunction;
749
+ IM3FuncType ft = v->module->functions[idx].funcType;
750
+ if (ft) {
751
+ for (u16 i = ft->numArgs; i > 0; i--) {
752
+ r = v_pop_expect(v, BaseTypeOf(ft->types[ft->numRets + i - 1]), &a);
753
+ if (r) return r;
754
+ }
755
+ for (u16 i = 0; i < ft->numRets; i++) {
756
+ r = v_push(v, BaseTypeOf(ft->types[i]));
757
+ if (r) return r;
758
+ }
759
+ }
760
+ break;
761
+ }
762
+
763
+ case 0x11: // call_indirect
764
+ {
765
+ u32 typeIdx;
766
+ r = ReadLEB_u32(&typeIdx, &v->wasm, v->wasmEnd);
767
+ if (r) return r;
768
+ u32 tableIdx;
769
+ r = ReadLEB_u32(&tableIdx, &v->wasm, v->wasmEnd);
770
+ if (r) return r;
771
+ if (typeIdx >= v->module->numFuncTypes) return m3Err_unknownType;
772
+ // the table must exist and hold funcrefs
773
+ if (tableIdx >= v->module->numTables) return m3Err_unknownTable;
774
+ if (v->module->tables[tableIdx]->type != c_m3Type_funcref) return m3Err_typeMismatch;
775
+ IM3FuncType ft = v->module->funcTypes[typeIdx];
776
+ r = v_pop_expect(v, v_table_addrtype(v, tableIdx), &a); // table index operand
777
+ if (r) return r;
778
+ if (ft) {
779
+ for (u16 i = ft->numArgs; i > 0; i--) {
780
+ r = v_pop_expect(v, BaseTypeOf(ft->types[ft->numRets + i - 1]), &a);
781
+ if (r) return r;
782
+ }
783
+ for (u16 i = 0; i < ft->numRets; i++) {
784
+ r = v_push(v, BaseTypeOf(ft->types[i]));
785
+ if (r) return r;
786
+ }
787
+ }
788
+ break;
789
+ }
790
+
791
+ case 0x12: // return_call
792
+ {
793
+ u32 idx;
794
+ r = ReadLEB_u32(&idx, &v->wasm, v->wasmEnd);
795
+ if (r) return r;
796
+ if (idx >= v->module->numFunctions) return m3Err_unknownFunction;
797
+ IM3FuncType ft = v->module->functions[idx].funcType;
798
+ r = v_check_tail_results(v, ft);
799
+ if (r) return r;
800
+ if (ft) {
801
+ for (u16 i = ft->numArgs; i > 0; i--) {
802
+ r = v_pop_expect(v, BaseTypeOf(ft->types[ft->numRets + i - 1]), &a);
803
+ if (r) return r;
804
+ }
805
+ }
806
+ v_unreachable(v);
807
+ break;
808
+ }
809
+
810
+ case 0x13: // return_call_indirect
811
+ {
812
+ u32 typeIdx;
813
+ r = ReadLEB_u32(&typeIdx, &v->wasm, v->wasmEnd);
814
+ if (r) return r;
815
+ u32 tableIdx;
816
+ r = ReadLEB_u32(&tableIdx, &v->wasm, v->wasmEnd);
817
+ if (r) return r;
818
+ if (typeIdx >= v->module->numFuncTypes) return m3Err_unknownType;
819
+ // the table must exist and hold funcrefs
820
+ if (tableIdx >= v->module->numTables) return m3Err_unknownTable;
821
+ if (v->module->tables[tableIdx]->type != c_m3Type_funcref) return m3Err_typeMismatch;
822
+ IM3FuncType ft = v->module->funcTypes[typeIdx];
823
+ r = v_check_tail_results(v, ft);
824
+ if (r) return r;
825
+ r = v_pop_expect(v, v_table_addrtype(v, tableIdx), &a); // table index operand
826
+ if (r) return r;
827
+ if (ft) {
828
+ for (u16 i = ft->numArgs; i > 0; i--) {
829
+ r = v_pop_expect(v, BaseTypeOf(ft->types[ft->numRets + i - 1]), &a);
830
+ if (r) return r;
831
+ }
832
+ }
833
+ v_unreachable(v);
834
+ break;
835
+ }
836
+
837
+ #if d_m3HasTypedRefs
838
+ case 0x14: // call_ref
839
+ case 0x15: // return_call_ref
840
+ {
841
+ u32 typeIdx;
842
+ r = ReadLEB_u32(&typeIdx, &v->wasm, v->wasmEnd);
843
+ if (r) return r;
844
+ if (typeIdx >= v->module->numFuncTypes) return m3Err_unknownType;
845
+ IM3FuncType ft = v->module->funcTypes[typeIdx];
846
+ bool isTail = (opcode == 0x15);
847
+ if (isTail) {
848
+ r = v_check_tail_results(v, ft);
849
+ if (r) return r;
850
+ }
851
+ // the callee itself, as a reference to a function of this type
852
+ r = v_pop_expect(v, c_m3Type_funcref, &a);
853
+ if (r) return r;
854
+ if (ft) {
855
+ for (u16 i = ft->numArgs; i > 0; i--) {
856
+ r = v_pop_expect(v, BaseTypeOf(ft->types[ft->numRets + i - 1]), &a);
857
+ if (r) return r;
858
+ }
859
+ if (not isTail) {
860
+ for (u16 i = 0; i < ft->numRets; i++) {
861
+ r = v_push(v, BaseTypeOf(ft->types[i]));
862
+ if (r) return r;
863
+ }
864
+ }
865
+ }
866
+ if (isTail) v_unreachable(v);
867
+ break;
868
+ }
869
+
870
+ case 0xd4: // ref.as_non_null
871
+ {
872
+ r = v_pop_expect(v, c_m3Type_funcref, &a);
873
+ if (r) return r;
874
+ r = v_push(v, (a == c_valBottom) ? c_m3Type_funcref : a);
875
+ if (r) return r;
876
+ break;
877
+ }
878
+ #endif // d_m3HasTypedRefs
879
+
880
+ // ---- Parametric ----
881
+ case 0x1a: // drop
882
+ r = v_pop(v, &a);
883
+ if (r) return r;
884
+ break;
885
+
886
+ case 0x1b: // select
887
+ {
888
+ r = v_pop_expect(v, c_m3Type_i32, &a);
889
+ if (r) return r;
890
+ m3type_t t2 = c_valBottom;
891
+ r = v_pop(v, &t2);
892
+ if (r) return r;
893
+ m3type_t t1;
894
+ r = v_pop_expect(v, t2, &t1);
895
+ if (r) return r;
896
+ // untyped select is numeric only; references need the 0x1c form
897
+ if (t2 != c_valBottom && IsRefType(t2)) return m3Err_typeMismatch;
898
+ r = v_push(v, (t2 == c_valBottom) ? t1 : t2);
899
+ if (r) return r;
900
+ break;
901
+ }
902
+
903
+ #if d_m3HasRefTypes
904
+ case 0x1c: // select with an explicit result type
905
+ {
906
+ u32 numTypes;
907
+ r = ReadLEB_u32(&numTypes, &v->wasm, v->wasmEnd);
908
+ if (r) return r;
909
+ if (numTypes != 1) return m3Err_wasmMalformed;
910
+
911
+ m3type_t selType;
912
+ r = ParseValueType(v->module, &selType, &v->wasm, v->wasmEnd); if (r) return r;
913
+ u8 t = BaseTypeOf(selType);
914
+
915
+ r = v_pop_expect(v, c_m3Type_i32, &a); if (r) return r;
916
+ r = v_pop_expect(v, t, &a); if (r) return r;
917
+ r = v_pop_expect(v, t, &a); if (r) return r;
918
+ r = v_push(v, t); if (r) return r;
919
+ break;
920
+ }
921
+
922
+ case 0x25: // table.get
923
+ case 0x26: // table.set
924
+ {
925
+ u32 tableIdx;
926
+ r = ReadLEB_u32(&tableIdx, &v->wasm, v->wasmEnd);
927
+ if (r) return r;
928
+ if (tableIdx >= v->module->numTables) return m3Err_unknownTable;
929
+ u8 t = BaseTypeOf(v->module->tables[tableIdx]->type);
930
+ m3type_t at = v_table_addrtype(v, tableIdx);
931
+
932
+ if (opcode == 0x26) {
933
+ r = v_pop_expect(v, t, &a); if (r) return r;
934
+ r = v_pop_expect(v, at, &a); if (r) return r;
935
+ } else {
936
+ r = v_pop_expect(v, at, &a); if (r) return r;
937
+ r = v_push(v, t); if (r) return r;
938
+ }
939
+ break;
940
+ }
941
+
942
+ case 0xd0: // ref.null
943
+ {
944
+ #if d_m3HasTypedRefs
945
+ m3type_t heapBits;
946
+ r = ParseHeapType(v->module, &heapBits, &v->wasm, v->wasmEnd); if (r) return r;
947
+ u8 t = (heapBits & d_m3Type_refExtern) ? c_m3Type_externref :
948
+ # if d_m3HasStackSwitching
949
+ (heapBits & d_m3Type_refCont) ? c_m3Type_contref :
950
+ # endif
951
+ c_m3Type_funcref;
952
+ #else
953
+ i8 waType;
954
+ u8 t;
955
+ r = ReadLEB_i7(&waType, &v->wasm, v->wasmEnd); if (r) return r;
956
+ r = NormalizeType(&t, waType); if (r) return r;
957
+ if (!IsRefType(t)) return m3Err_wasmMalformed;
958
+ #endif
959
+ r = v_push(v, t); if (r) return r;
960
+ break;
961
+ }
962
+
963
+ case 0xd1: // ref.is_null
964
+ {
965
+ m3type_t t = c_valBottom;
966
+ r = v_pop(v, &t);
967
+ if (r) return r;
968
+ if (t != c_valBottom && !IsRefType(t)) return m3Err_typeMismatch;
969
+ r = v_push(v, c_m3Type_i32);
970
+ if (r) return r;
971
+ break;
972
+ }
973
+
974
+ case 0xd2: // ref.func
975
+ {
976
+ u32 funcIdx;
977
+ r = ReadLEB_u32(&funcIdx, &v->wasm, v->wasmEnd);
978
+ if (r) return r;
979
+ if (funcIdx >= v->module->numFunctions) return m3Err_unknownFunction;
980
+ if (!Module_IsFunctionDeclared(v->module, funcIdx)) return m3Err_undeclaredFuncRef;
981
+ r = v_push(v, c_m3Type_funcref);
982
+ if (r) return r;
983
+ break;
984
+ }
985
+ #endif
986
+
987
+ // ---- Variable ----
988
+ case 0x20: // local.get
989
+ {
990
+ u32 idx;
991
+ r = ReadLEB_u32(&idx, &v->wasm, v->wasmEnd);
992
+ if (r) return r;
993
+ if (idx >= v->numLocals) return m3Err_unknownLocal;
994
+ r = v_push(v, v->localTypes[idx]);
995
+ if (r) return r;
996
+ break;
997
+ }
998
+
999
+ case 0x21: // local.set
1000
+ {
1001
+ u32 idx;
1002
+ r = ReadLEB_u32(&idx, &v->wasm, v->wasmEnd);
1003
+ if (r) return r;
1004
+ if (idx >= v->numLocals) return m3Err_unknownLocal;
1005
+ r = v_pop_expect(v, v->localTypes[idx], &a);
1006
+ if (r) return r;
1007
+ break;
1008
+ }
1009
+
1010
+ case 0x22: // local.tee
1011
+ {
1012
+ u32 idx;
1013
+ r = ReadLEB_u32(&idx, &v->wasm, v->wasmEnd);
1014
+ if (r) return r;
1015
+ if (idx >= v->numLocals) return m3Err_unknownLocal;
1016
+ r = v_pop_expect(v, v->localTypes[idx], &a);
1017
+ if (r) return r;
1018
+ r = v_push(v, v->localTypes[idx]);
1019
+ if (r) return r;
1020
+ break;
1021
+ }
1022
+
1023
+ case 0x23: // global.get
1024
+ {
1025
+ u32 idx;
1026
+ r = ReadLEB_u32(&idx, &v->wasm, v->wasmEnd);
1027
+ if (r) return r;
1028
+ if (idx >= v->module->numGlobals) return m3Err_unknownGlobal;
1029
+ r = v_push(v, BaseTypeOf(v->module->globals[idx].type));
1030
+ if (r) return r;
1031
+ break;
1032
+ }
1033
+
1034
+ case 0x24: // global.set
1035
+ {
1036
+ u32 idx;
1037
+ r = ReadLEB_u32(&idx, &v->wasm, v->wasmEnd);
1038
+ if (r) return r;
1039
+ if (idx >= v->module->numGlobals) return m3Err_unknownGlobal;
1040
+ r = v_pop_expect(v, BaseTypeOf(v->module->globals[idx].type), &a);
1041
+ if (r) return r;
1042
+ break;
1043
+ }
1044
+
1045
+ // ---- Memory load ----
1046
+ case 0x28: case 0x29: case 0x2a: case 0x2b: // i32/i64/f32/f64.load
1047
+ case 0x2c: case 0x2d: case 0x2e: case 0x2f: // i32.load8/16 s/u
1048
+ case 0x30: case 0x31: case 0x32: case 0x33: // i64.load8/16 s/u
1049
+ case 0x34: case 0x35: // i64.load32 s/u
1050
+ {
1051
+ u32 align, memidx; u64 offset;
1052
+ r = ReadMemoryArg(&align, &memidx, &offset, &v->wasm, v->wasmEnd); if (r) return r;
1053
+ if (align > v_max_align(opcode)) return m3Err_invalidAlignment;
1054
+ if (not v_has_memory_idx(v, memidx)) return m3Err_unknownMemory;
1055
+ if (not v_offset_in_range(v, memidx, offset)) return m3Err_wasmMalformed;
1056
+ r = v_pop_expect(v, v_memory_addrtype(v, memidx), &a); if (r) return r;
1057
+ u8 result;
1058
+ if (opcode == 0x28) result = c_m3Type_i32;
1059
+ else if (opcode == 0x29) result = c_m3Type_i64;
1060
+ else if (opcode == 0x2a) result = c_m3Type_f32;
1061
+ else if (opcode == 0x2b) result = c_m3Type_f64;
1062
+ else if (opcode <= 0x2f) result = c_m3Type_i32;
1063
+ else result = c_m3Type_i64;
1064
+ r = v_push(v, result);
1065
+ if (r) return r;
1066
+ break;
1067
+ }
1068
+
1069
+ // ---- Memory store ----
1070
+ case 0x36: case 0x37: case 0x38: case 0x39: // i32/i64/f32/f64.store
1071
+ case 0x3a: case 0x3b: // i32.store8/16
1072
+ case 0x3c: case 0x3d: case 0x3e: // i64.store8/16/32
1073
+ {
1074
+ u32 align, memidx; u64 offset;
1075
+ r = ReadMemoryArg(&align, &memidx, &offset, &v->wasm, v->wasmEnd); if (r) return r;
1076
+ if (align > v_max_align(opcode)) return m3Err_invalidAlignment;
1077
+ if (not v_has_memory_idx(v, memidx)) return m3Err_unknownMemory;
1078
+ if (not v_offset_in_range(v, memidx, offset)) return m3Err_wasmMalformed;
1079
+ u8 valtype;
1080
+ if (opcode == 0x36) valtype = c_m3Type_i32;
1081
+ else if (opcode == 0x37) valtype = c_m3Type_i64;
1082
+ else if (opcode == 0x38) valtype = c_m3Type_f32;
1083
+ else if (opcode == 0x39) valtype = c_m3Type_f64;
1084
+ else if (opcode <= 0x3b) valtype = c_m3Type_i32;
1085
+ else valtype = c_m3Type_i64;
1086
+ r = v_pop_expect(v, valtype, &a); if (r) return r;
1087
+ r = v_pop_expect(v, v_memory_addrtype(v, memidx), &a); if (r) return r;
1088
+ break;
1089
+ }
1090
+
1091
+ // ---- Memory size/grow ----
1092
+ case 0x3f: // memory.size
1093
+ {
1094
+ u32 memidx;
1095
+ r = ReadLEB_u32(&memidx, &v->wasm, v->wasmEnd); if (r) return r;
1096
+ if (not v_has_memory_idx(v, memidx)) return m3Err_unknownMemory;
1097
+ r = v_push(v, v_memory_addrtype(v, memidx)); if (r) return r;
1098
+ break;
1099
+ }
1100
+ case 0x40: // memory.grow
1101
+ {
1102
+ u32 memidx;
1103
+ r = ReadLEB_u32(&memidx, &v->wasm, v->wasmEnd); if (r) return r;
1104
+ if (not v_has_memory_idx(v, memidx)) return m3Err_unknownMemory;
1105
+ r = v_pop_expect(v, v_memory_addrtype(v, memidx), &a); if (r) return r;
1106
+ r = v_push(v, v_memory_addrtype(v, memidx)); if (r) return r;
1107
+ break;
1108
+ }
1109
+
1110
+ // ---- Constants ----
1111
+ case 0x41: { // i32.const
1112
+ i32 val;
1113
+ r = ReadLEB_i32(&val, &v->wasm, v->wasmEnd); if (r) return r;
1114
+ r = v_push(v, c_m3Type_i32); if (r) return r;
1115
+ break;
1116
+ }
1117
+ case 0x42: { // i64.const
1118
+ i64 val;
1119
+ r = ReadLEB_i64(&val, &v->wasm, v->wasmEnd); if (r) return r;
1120
+ r = v_push(v, c_m3Type_i64); if (r) return r;
1121
+ break;
1122
+ }
1123
+ case 0x43: { // f32.const
1124
+ if (v->wasm + 4 > v->wasmEnd) return m3Err_wasmUnderrun;
1125
+ v->wasm += 4;
1126
+ r = v_push(v, c_m3Type_f32); if (r) return r;
1127
+ break;
1128
+ }
1129
+ case 0x44: { // f64.const
1130
+ if (v->wasm + 8 > v->wasmEnd) return m3Err_wasmUnderrun;
1131
+ v->wasm += 8;
1132
+ r = v_push(v, c_m3Type_f64); if (r) return r;
1133
+ break;
1134
+ }
1135
+
1136
+
1137
+ // ---- i32 comparison ----
1138
+ case 0x45: r = v_testop(v, c_m3Type_i32); break; // i32.eqz
1139
+ case 0x46: case 0x47: case 0x48: case 0x49: case 0x4a:
1140
+ case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
1141
+ r = v_relop(v, c_m3Type_i32); break;
1142
+
1143
+ // ---- i64 comparison ----
1144
+ case 0x50: r = v_testop(v, c_m3Type_i64); break; // i64.eqz
1145
+ case 0x51: case 0x52: case 0x53: case 0x54: case 0x55:
1146
+ case 0x56: case 0x57: case 0x58: case 0x59: case 0x5a:
1147
+ r = v_relop(v, c_m3Type_i64); break;
1148
+
1149
+ // ---- f32 comparison ----
1150
+ case 0x5b: case 0x5c: case 0x5d: case 0x5e: case 0x5f: case 0x60:
1151
+ r = v_relop(v, c_m3Type_f32); break;
1152
+
1153
+ // ---- f64 comparison ----
1154
+ case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66:
1155
+ r = v_relop(v, c_m3Type_f64); break;
1156
+
1157
+ // ---- i32 unary ----
1158
+ case 0x67: case 0x68: case 0x69: // clz, ctz, popcnt
1159
+ r = v_unop(v, c_m3Type_i32, c_m3Type_i32); break;
1160
+
1161
+ // ---- i32 binary ----
1162
+ case 0x6a: case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
1163
+ case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75:
1164
+ case 0x76: case 0x77: case 0x78: // add..rotr
1165
+ r = v_binop(v, c_m3Type_i32); break;
1166
+
1167
+ // ---- i64 unary ----
1168
+ case 0x79: case 0x7a: case 0x7b: // clz, ctz, popcnt
1169
+ r = v_unop(v, c_m3Type_i64, c_m3Type_i64); break;
1170
+
1171
+ // ---- i64 binary ----
1172
+ case 0x7c: case 0x7d: case 0x7e: case 0x7f: case 0x80: case 0x81:
1173
+ case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
1174
+ case 0x88: case 0x89: case 0x8a: // add..rotr
1175
+ r = v_binop(v, c_m3Type_i64); break;
1176
+
1177
+ // ---- f32 unary ----
1178
+ case 0x8b: case 0x8c: case 0x8d: case 0x8e: case 0x8f:
1179
+ case 0x90: case 0x91: // abs, neg, ceil, floor, trunc, nearest, sqrt
1180
+ r = v_unop(v, c_m3Type_f32, c_m3Type_f32); break;
1181
+
1182
+ // ---- f32 binary ----
1183
+ case 0x92: case 0x93: case 0x94: case 0x95: case 0x96:
1184
+ case 0x97: case 0x98: // add, sub, mul, div, min, max, copysign
1185
+ r = v_binop(v, c_m3Type_f32); break;
1186
+
1187
+ // ---- f64 unary ----
1188
+ case 0x99: case 0x9a: case 0x9b: case 0x9c: case 0x9d:
1189
+ case 0x9e: case 0x9f: // abs, neg, ceil, floor, trunc, nearest, sqrt
1190
+ r = v_unop(v, c_m3Type_f64, c_m3Type_f64); break;
1191
+
1192
+ // ---- f64 binary ----
1193
+ case 0xa0: case 0xa1: case 0xa2: case 0xa3: case 0xa4:
1194
+ case 0xa5: case 0xa6: // add, sub, mul, div, min, max, copysign
1195
+ r = v_binop(v, c_m3Type_f64); break;
1196
+
1197
+ // ---- Conversions ----
1198
+ case 0xa7: r = v_cvtop(v, c_m3Type_i64, c_m3Type_i32); break; // i32.wrap/i64
1199
+ case 0xa8: case 0xa9: // i32.trunc_s/f32, i32.trunc_u/f32
1200
+ r = v_cvtop(v, c_m3Type_f32, c_m3Type_i32); break;
1201
+ case 0xaa: case 0xab: // i32.trunc_s/f64, i32.trunc_u/f64
1202
+ r = v_cvtop(v, c_m3Type_f64, c_m3Type_i32); break;
1203
+ case 0xac: case 0xad: // i64.extend_s/i32, i64.extend_u/i32
1204
+ r = v_cvtop(v, c_m3Type_i32, c_m3Type_i64); break;
1205
+ case 0xae: case 0xaf: // i64.trunc_s/f32, i64.trunc_u/f32
1206
+ r = v_cvtop(v, c_m3Type_f32, c_m3Type_i64); break;
1207
+ case 0xb0: case 0xb1: // i64.trunc_s/f64, i64.trunc_u/f64
1208
+ r = v_cvtop(v, c_m3Type_f64, c_m3Type_i64); break;
1209
+ case 0xb2: case 0xb3: // f32.convert_s/i32, f32.convert_u/i32
1210
+ r = v_cvtop(v, c_m3Type_i32, c_m3Type_f32); break;
1211
+ case 0xb4: case 0xb5: // f32.convert_s/i64, f32.convert_u/i64
1212
+ r = v_cvtop(v, c_m3Type_i64, c_m3Type_f32); break;
1213
+ case 0xb6: // f32.demote/f64
1214
+ r = v_cvtop(v, c_m3Type_f64, c_m3Type_f32); break;
1215
+ case 0xb7: case 0xb8: // f64.convert_s/i32, f64.convert_u/i32
1216
+ r = v_cvtop(v, c_m3Type_i32, c_m3Type_f64); break;
1217
+ case 0xb9: case 0xba: // f64.convert_s/i64, f64.convert_u/i64
1218
+ r = v_cvtop(v, c_m3Type_i64, c_m3Type_f64); break;
1219
+ case 0xbb: // f64.promote/f32
1220
+ r = v_cvtop(v, c_m3Type_f32, c_m3Type_f64); break;
1221
+ case 0xbc: // i32.reinterpret/f32
1222
+ r = v_cvtop(v, c_m3Type_f32, c_m3Type_i32); break;
1223
+ case 0xbd: // i64.reinterpret/f64
1224
+ r = v_cvtop(v, c_m3Type_f64, c_m3Type_i64); break;
1225
+ case 0xbe: // f32.reinterpret/i32
1226
+ r = v_cvtop(v, c_m3Type_i32, c_m3Type_f32); break;
1227
+ case 0xbf: // f64.reinterpret/i64
1228
+ r = v_cvtop(v, c_m3Type_i64, c_m3Type_f64); break;
1229
+
1230
+ // ---- Sign-extension (MVP post) ----
1231
+ case 0xc0: case 0xc1: // i32.extend8_s, i32.extend16_s
1232
+ r = v_unop(v, c_m3Type_i32, c_m3Type_i32); break;
1233
+ case 0xc2: case 0xc3: case 0xc4: // i64.extend8/16/32_s
1234
+ r = v_unop(v, c_m3Type_i64, c_m3Type_i64); break;
1235
+
1236
+ // ---- 0xFC prefix (saturating truncations, bulk memory, wide arithmetic) ----
1237
+ case 0xfc:
1238
+ {
1239
+ u32 sub;
1240
+ r = ReadLEB_u32(&sub, &v->wasm, v->wasmEnd);
1241
+ if (r) return r;
1242
+ switch (sub) {
1243
+ case 0x00: case 0x01: // i32.trunc_sat_f32_s/u
1244
+ r = v_cvtop(v, c_m3Type_f32, c_m3Type_i32); break;
1245
+ case 0x02: case 0x03: // i32.trunc_sat_f64_s/u
1246
+ r = v_cvtop(v, c_m3Type_f64, c_m3Type_i32); break;
1247
+ case 0x04: case 0x05: // i64.trunc_sat_f32_s/u
1248
+ r = v_cvtop(v, c_m3Type_f32, c_m3Type_i64); break;
1249
+ case 0x06: case 0x07: // i64.trunc_sat_f64_s/u
1250
+ r = v_cvtop(v, c_m3Type_f64, c_m3Type_i64); break;
1251
+ case 0x08: // memory.init
1252
+ {
1253
+ u32 dataidx, memidx;
1254
+ r = ReadLEB_u32(&dataidx, &v->wasm, v->wasmEnd); if (r) return r;
1255
+ r = ReadLEB_u32(&memidx, &v->wasm, v->wasmEnd); if (r) return r;
1256
+ if (not v_has_memory_idx(v, memidx)) return m3Err_unknownMemory;
1257
+ // the segments must have been declared up front by a data count section
1258
+ if (not v->module->hasDataCount) return m3Err_dataCountRequired;
1259
+ if (dataidx >= v->module->numDataSegments) return m3Err_unknownDataSegment;
1260
+ // the segment is indexed as an i32 no matter how the memory is
1261
+ // addressed; only the destination follows the address type
1262
+ r = v_pop_expect(v, c_m3Type_i32, &a); if (r) return r; // n
1263
+ r = v_pop_expect(v, c_m3Type_i32, &a); if (r) return r; // src
1264
+ r = v_pop_expect(v, v_memory_addrtype(v, memidx), &a); if (r) return r; // dst
1265
+ break;
1266
+ }
1267
+ case 0x09: // data.drop
1268
+ {
1269
+ u32 dataidx;
1270
+ r = ReadLEB_u32(&dataidx, &v->wasm, v->wasmEnd); if (r) return r;
1271
+ // needs the segment, but not a memory
1272
+ if (not v->module->hasDataCount) return m3Err_dataCountRequired;
1273
+ if (dataidx >= v->module->numDataSegments) return m3Err_unknownDataSegment;
1274
+ break;
1275
+ }
1276
+ #if d_m3HasRefTypes
1277
+ case 0x0c: // table.init
1278
+ case 0x0e: // table.copy
1279
+ {
1280
+ u32 a1, a2;
1281
+ r = ReadLEB_u32(&a1, &v->wasm, v->wasmEnd); if (r) return r;
1282
+ r = ReadLEB_u32(&a2, &v->wasm, v->wasmEnd); if (r) return r;
1283
+
1284
+ if (sub == 0x0c) { // elemidx, tableidx
1285
+ // the immediates are encoded elem-then-table, but the spec's
1286
+ // rule requires the table to be defined before the segment
1287
+ if (a2 >= v->module->numTables) return m3Err_unknownTable;
1288
+ if (a1 >= v->module->numElementSegments) return m3Err_unknownElemSegment;
1289
+ if (v->module->elementSegments[a1].type != v->module->tables[a2]->type)
1290
+ return m3Err_typeMismatch;
1291
+ } else { // dst, src
1292
+ if (a1 >= v->module->numTables) return m3Err_unknownTable;
1293
+ if (a2 >= v->module->numTables) return m3Err_unknownTable;
1294
+ if (v->module->tables[a1]->type != v->module->tables[a2]->type)
1295
+ return m3Err_typeMismatch;
1296
+ }
1297
+
1298
+ if (sub == 0x0c) {
1299
+ // table.init indexes the segment as an i32 whatever the
1300
+ // table is; only the destination follows the table
1301
+ r = v_pop_expect(v, c_m3Type_i32, &a); if (r) return r; // n
1302
+ r = v_pop_expect(v, c_m3Type_i32, &a); if (r) return r; // s
1303
+ r = v_pop_expect(v, v_table_addrtype(v, a2), &a); if (r) return r; // d
1304
+ } else {
1305
+ // table.copy: each index follows its own table, and the
1306
+ // length the narrower of the two
1307
+ m3type_t dType = v_table_addrtype(v, a1);
1308
+ m3type_t sType = v_table_addrtype(v, a2);
1309
+ m3type_t nType = (dType == c_m3Type_i64 and sType == c_m3Type_i64)
1310
+ ? c_m3Type_i64 : c_m3Type_i32;
1311
+ r = v_pop_expect(v, nType, &a); if (r) return r; // n
1312
+ r = v_pop_expect(v, sType, &a); if (r) return r; // s
1313
+ r = v_pop_expect(v, dType, &a); if (r) return r; // d
1314
+ }
1315
+ break;
1316
+ }
1317
+ case 0x0d: // elem.drop
1318
+ {
1319
+ u32 elemIdx;
1320
+ r = ReadLEB_u32(&elemIdx, &v->wasm, v->wasmEnd); if (r) return r;
1321
+ if (elemIdx >= v->module->numElementSegments) return m3Err_unknownElemSegment;
1322
+ break;
1323
+ }
1324
+ case 0x0f: // table.grow
1325
+ case 0x10: // table.size
1326
+ case 0x11: // table.fill
1327
+ {
1328
+ u32 tableIdx;
1329
+ r = ReadLEB_u32(&tableIdx, &v->wasm, v->wasmEnd); if (r) return r;
1330
+ if (tableIdx >= v->module->numTables) return m3Err_unknownTable;
1331
+ u8 t = BaseTypeOf(v->module->tables[tableIdx]->type);
1332
+ m3type_t at = v_table_addrtype(v, tableIdx);
1333
+
1334
+ if (sub == 0x10) { // table.size
1335
+ r = v_push(v, at); if (r) return r;
1336
+ } else if (sub == 0x0f) { // table.grow
1337
+ r = v_pop_expect(v, at, &a); if (r) return r; // n
1338
+ r = v_pop_expect(v, t, &a); if (r) return r; // init
1339
+ r = v_push(v, at); if (r) return r;
1340
+ } else { // table.fill
1341
+ r = v_pop_expect(v, at, &a); if (r) return r; // n
1342
+ r = v_pop_expect(v, t, &a); if (r) return r; // val
1343
+ r = v_pop_expect(v, at, &a); if (r) return r; // i
1344
+ }
1345
+ break;
1346
+ }
1347
+ #endif
1348
+ case 0x0a: // memory.copy
1349
+ {
1350
+ u32 dst, src;
1351
+ r = ReadLEB_u32(&dst, &v->wasm, v->wasmEnd); if (r) return r;
1352
+ r = ReadLEB_u32(&src, &v->wasm, v->wasmEnd); if (r) return r;
1353
+ if (not v_has_memory_idx(v, dst) or not v_has_memory_idx(v, src)) return m3Err_unknownMemory;
1354
+ // Spec: each address follows its own memory, and the length is
1355
+ // typed by the narrower of the two - so copying between an i32
1356
+ // and an i64 memory takes an i32 length.
1357
+ m3type_t dstType = v_memory_addrtype(v, dst);
1358
+ m3type_t srcType = v_memory_addrtype(v, src);
1359
+ m3type_t lenType = (dstType == c_m3Type_i64 and srcType == c_m3Type_i64)
1360
+ ? c_m3Type_i64 : c_m3Type_i32;
1361
+ r = v_pop_expect(v, lenType, &a); if (r) return r; // n
1362
+ r = v_pop_expect(v, srcType, &a); if (r) return r; // src
1363
+ r = v_pop_expect(v, dstType, &a); if (r) return r; // dst
1364
+ break;
1365
+ }
1366
+ case 0x0b: // memory.fill
1367
+ {
1368
+ u32 memidx;
1369
+ r = ReadLEB_u32(&memidx, &v->wasm, v->wasmEnd); if (r) return r;
1370
+ if (not v_has_memory_idx(v, memidx)) return m3Err_unknownMemory;
1371
+ m3type_t addrType = v_memory_addrtype(v, memidx);
1372
+ r = v_pop_expect(v, addrType, &a); if (r) return r; // n
1373
+ r = v_pop_expect(v, c_m3Type_i32, &a); if (r) return r; // val
1374
+ r = v_pop_expect(v, addrType, &a); if (r) return r; // dst
1375
+ break;
1376
+ }
1377
+ #if d_m3HasWideArithmetic
1378
+ case 0x13: case 0x14: // i64.add128, i64.sub128
1379
+ case 0x15: case 0x16: // i64.mul_wide_s, i64.mul_wide_u
1380
+ {
1381
+ // two 128-bit operands of two i64 halves each, or two i64 factors;
1382
+ // either way a 128-bit result, the low half pushed first
1383
+ u32 numOperands = (sub <= 0x14) ? 4 : 2;
1384
+ for (u32 i = 0; i < numOperands; ++i) {
1385
+ r = v_pop_expect(v, c_m3Type_i64, &a); if (r) return r;
1386
+ }
1387
+ r = v_push(v, c_m3Type_i64); if (r) return r;
1388
+ r = v_push(v, c_m3Type_i64); if (r) return r;
1389
+ break;
1390
+ }
1391
+ #endif
1392
+ default:
1393
+ // Unknown FC sub-opcode: skip validation (allow forward compat)
1394
+ break;
1395
+ }
1396
+ break;
1397
+ }
1398
+
1399
+ #if d_m3HasAtomics
1400
+ // ---- 0xFE prefix (atomics) ----
1401
+ case 0xfe:
1402
+ {
1403
+ u32 sub;
1404
+ r = ReadLEB_u32(&sub, &v->wasm, v->wasmEnd);
1405
+ if (r) return r;
1406
+
1407
+ if (sub == 0x03) { // atomic.fence
1408
+ u8 reserved;
1409
+ r = Read_u8(&reserved, &v->wasm, v->wasmEnd); if (r) return r;
1410
+ if (reserved != 0) return m3Err_wasmMalformed;
1411
+ break;
1412
+ }
1413
+
1414
+ u32 width, numOperands; m3type_t valueType, resultType;
1415
+ if (not v_atomic_shape(sub, &width, &numOperands, &valueType, &resultType)) return m3Err_unknownOpcode;
1416
+
1417
+ u32 align, memidx; u64 offset;
1418
+ r = ReadMemoryArg(&align, &memidx, &offset, &v->wasm, v->wasmEnd); if (r) return r;
1419
+ // Spec: unlike an ordinary access, an atomic one must be aligned exactly
1420
+ if ((1u << align) != width) return m3Err_invalidAtomicAlignment;
1421
+ if (not v_has_memory_idx(v, memidx)) return m3Err_unknownMemory;
1422
+ if (not v_offset_in_range(v, memidx, offset)) return m3Err_wasmMalformed;
1423
+
1424
+ // the operands above the address are all of the one type, except a
1425
+ // wait, whose timeout is an i64 whatever it is waiting on
1426
+ if (sub == 0x01 or sub == 0x02) {
1427
+ r = v_pop_expect(v, c_m3Type_i64, &a); if (r) return r;
1428
+ numOperands--;
1429
+ }
1430
+ for (u32 i = 1; i < numOperands; ++i) {
1431
+ r = v_pop_expect(v, valueType, &a); if (r) return r;
1432
+ }
1433
+ r = v_pop_expect(v, v_memory_addrtype(v, memidx), &a); if (r) return r;
1434
+ if (resultType != c_m3Type_none) {
1435
+ r = v_push(v, resultType); if (r) return r;
1436
+ }
1437
+ break;
1438
+ }
1439
+ #endif
1440
+
1441
+ #if d_m3HasStackSwitching
1442
+ // ---- Stack Switching ----
1443
+ case 0xe0: // cont.new
1444
+ {
1445
+ u32 typeIdx;
1446
+ r = ReadLEB_u32(&typeIdx, &v->wasm, v->wasmEnd); if (r) return r;
1447
+ if (typeIdx >= v->module->numFuncTypes) return m3Err_unknownType;
1448
+ IM3FuncType ft = v->module->funcTypes[typeIdx];
1449
+ if (!ft || !ft->isContinuation) return m3Err_typeMismatch;
1450
+ r = v_pop_expect(v, c_m3Type_funcref, &a); if (r) return r;
1451
+ r = v_push(v, c_m3Type_contref); if (r) return r;
1452
+ break;
1453
+ }
1454
+
1455
+ case 0xe1: // cont.bind
1456
+ {
1457
+ u32 typeIdx1, typeIdx2;
1458
+ r = ReadLEB_u32(&typeIdx1, &v->wasm, v->wasmEnd); if (r) return r;
1459
+ r = ReadLEB_u32(&typeIdx2, &v->wasm, v->wasmEnd); if (r) return r;
1460
+ if (typeIdx1 >= v->module->numFuncTypes || typeIdx2 >= v->module->numFuncTypes) return m3Err_unknownType;
1461
+ IM3FuncType ct1 = v->module->funcTypes[typeIdx1];
1462
+ IM3FuncType ct2 = v->module->funcTypes[typeIdx2];
1463
+ if (!ct1 || !ct2 || !ct1->isContinuation || !ct2->isContinuation) return m3Err_typeMismatch;
1464
+
1465
+ IM3FuncType ft1 = ct1->contFuncType ? ct1->contFuncType : ct1;
1466
+ IM3FuncType ft2 = ct2->contFuncType ? ct2->contFuncType : ct2;
1467
+
1468
+ u16 numParams1 = ft1->numArgs;
1469
+ u16 numParams2 = ft2->numArgs;
1470
+ if (numParams1 < numParams2) return m3Err_typeMismatch;
1471
+ u32 numBound = numParams1 - numParams2;
1472
+
1473
+ if (ft1->numRets != ft2->numRets) return m3Err_typeMismatch;
1474
+ for (u16 i = 0; i < ft1->numRets; ++i) {
1475
+ if (ft1->types[i] != ft2->types[i]) return m3Err_typeMismatch;
1476
+ }
1477
+ for (u16 i = 0; i < numParams2; ++i) {
1478
+ if (ft1->types[ft1->numRets + numBound + i] != ft2->types[ft2->numRets + i]) return m3Err_typeMismatch;
1479
+ }
1480
+
1481
+ r = v_pop_expect(v, c_m3Type_contref, &a); if (r) return r;
1482
+
1483
+ for (u32 i = numBound; i > 0; i--) {
1484
+ r = v_pop_expect(v, BaseTypeOf(ft1->types[ft1->numRets + i - 1]), &a);
1485
+ if (r) return r;
1486
+ }
1487
+
1488
+ r = v_push(v, c_m3Type_contref); if (r) return r;
1489
+ break;
1490
+ }
1491
+
1492
+ case 0xe2: // suspend
1493
+ {
1494
+ u32 tagIdx;
1495
+ r = ReadLEB_u32(&tagIdx, &v->wasm, v->wasmEnd); if (r) return r;
1496
+ if (!v->module || tagIdx >= v->module->numTags) return m3Err_unknownTag;
1497
+ IM3FuncType tt = v->module->tags[tagIdx].type;
1498
+ if (tt) {
1499
+ for (u16 i = tt->numArgs; i > 0; i--) {
1500
+ r = v_pop_expect(v, BaseTypeOf(tt->types[tt->numRets + i - 1]), &a);
1501
+ if (r) return r;
1502
+ }
1503
+ for (u16 i = 0; i < tt->numRets; i++) {
1504
+ r = v_push(v, BaseTypeOf(tt->types[i]));
1505
+ if (r) return r;
1506
+ }
1507
+ }
1508
+ break;
1509
+ }
1510
+
1511
+ case 0xe3: // resume
1512
+ {
1513
+ u32 typeIdx;
1514
+ r = ReadLEB_u32(&typeIdx, &v->wasm, v->wasmEnd); if (r) return r;
1515
+ if (typeIdx >= v->module->numFuncTypes) return m3Err_unknownType;
1516
+ IM3FuncType ct = v->module->funcTypes[typeIdx];
1517
+ if (!ct || !ct->isContinuation) return m3Err_typeMismatch;
1518
+
1519
+ u32 numClauses;
1520
+ r = ReadLEB_u32(&numClauses, &v->wasm, v->wasmEnd); if (r) return r;
1521
+
1522
+ for (u32 i = 0; i < numClauses; i++) {
1523
+ u8 kind;
1524
+ r = Read_u8(&kind, &v->wasm, v->wasmEnd); if (r) return r;
1525
+ u32 tagIdx;
1526
+ r = ReadLEB_u32(&tagIdx, &v->wasm, v->wasmEnd); if (r) return r;
1527
+ r = v_resume_clause(v, kind, tagIdx, ct); if (r) return r;
1528
+ }
1529
+
1530
+ r = v_pop_expect(v, c_m3Type_contref, &a); if (r) return r;
1531
+
1532
+ IM3FuncType ft = ct->contFuncType ? ct->contFuncType : ct;
1533
+ if (ft) {
1534
+ for (u16 i = ft->numArgs; i > 0; i--) {
1535
+ r = v_pop_expect(v, BaseTypeOf(ft->types[ft->numRets + i - 1]), &a);
1536
+ if (r) return r;
1537
+ }
1538
+ for (u16 i = 0; i < ft->numRets; i++) {
1539
+ r = v_push(v, BaseTypeOf(ft->types[i]));
1540
+ if (r) return r;
1541
+ }
1542
+ }
1543
+ break;
1544
+ }
1545
+
1546
+ case 0xe4: // resume_throw
1547
+ {
1548
+ u32 typeIdx, tagIdx, numClauses;
1549
+ r = ReadLEB_u32(&typeIdx, &v->wasm, v->wasmEnd); if (r) return r;
1550
+ r = ReadLEB_u32(&tagIdx, &v->wasm, v->wasmEnd); if (r) return r;
1551
+ r = ReadLEB_u32(&numClauses, &v->wasm, v->wasmEnd); if (r) return r;
1552
+
1553
+ if (typeIdx >= v->module->numFuncTypes) return m3Err_unknownType;
1554
+ if (!v->module || tagIdx >= v->module->numTags) return m3Err_unknownTag;
1555
+ IM3FuncType ct = v->module->funcTypes[typeIdx];
1556
+ if (!ct || !ct->isContinuation) return m3Err_typeMismatch;
1557
+ IM3FuncType tagType = v->module->tags[tagIdx].type;
1558
+
1559
+ // it is an exception that gets raised, so the tag has to be one
1560
+ // throw could have raised: no results
1561
+ if (tagType && tagType->numRets != 0) return m3Err_typeMismatch;
1562
+
1563
+ for (u32 i = 0; i < numClauses; i++) {
1564
+ u8 kind;
1565
+ r = Read_u8(&kind, &v->wasm, v->wasmEnd); if (r) return r;
1566
+ u32 cTagIdx;
1567
+ r = ReadLEB_u32(&cTagIdx, &v->wasm, v->wasmEnd); if (r) return r;
1568
+ r = v_resume_clause(v, kind, cTagIdx, ct); if (r) return r;
1569
+ }
1570
+
1571
+ r = v_pop_expect(v, c_m3Type_contref, &a); if (r) return r;
1572
+
1573
+ if (tagType) {
1574
+ for (u16 i = tagType->numArgs; i > 0; i--) {
1575
+ r = v_pop_expect(v, BaseTypeOf(tagType->types[tagType->numRets + i - 1]), &a);
1576
+ if (r) return r;
1577
+ }
1578
+ }
1579
+
1580
+ IM3FuncType ft = ct->contFuncType ? ct->contFuncType : ct;
1581
+ if (ft) {
1582
+ for (u16 i = 0; i < ft->numRets; i++) {
1583
+ r = v_push(v, BaseTypeOf(ft->types[i]));
1584
+ if (r) return r;
1585
+ }
1586
+ }
1587
+ break;
1588
+ }
1589
+
1590
+ case 0xe5: // resume_throw_ref
1591
+ {
1592
+ u32 typeIdx, numClauses;
1593
+ r = ReadLEB_u32(&typeIdx, &v->wasm, v->wasmEnd); if (r) return r;
1594
+ r = ReadLEB_u32(&numClauses, &v->wasm, v->wasmEnd); if (r) return r;
1595
+
1596
+ if (typeIdx >= v->module->numFuncTypes) return m3Err_unknownType;
1597
+ IM3FuncType ct = v->module->funcTypes[typeIdx];
1598
+ if (!ct || !ct->isContinuation) return m3Err_typeMismatch;
1599
+
1600
+ for (u32 i = 0; i < numClauses; i++) {
1601
+ u8 kind;
1602
+ r = Read_u8(&kind, &v->wasm, v->wasmEnd); if (r) return r;
1603
+ u32 cTagIdx;
1604
+ r = ReadLEB_u32(&cTagIdx, &v->wasm, v->wasmEnd); if (r) return r;
1605
+ r = v_resume_clause(v, kind, cTagIdx, ct); if (r) return r;
1606
+ }
1607
+
1608
+ r = v_pop_expect(v, c_m3Type_contref, &a); if (r) return r;
1609
+ r = v_pop_expect(v, c_m3Type_exnref, &a); if (r) return r;
1610
+
1611
+ IM3FuncType ft = ct->contFuncType ? ct->contFuncType : ct;
1612
+ if (ft) {
1613
+ for (u16 i = 0; i < ft->numRets; i++) {
1614
+ r = v_push(v, BaseTypeOf(ft->types[i]));
1615
+ if (r) return r;
1616
+ }
1617
+ }
1618
+ break;
1619
+ }
1620
+
1621
+ case 0xe6: // switch
1622
+ {
1623
+ u32 typeIdx, tagIdx;
1624
+ r = ReadLEB_u32(&typeIdx, &v->wasm, v->wasmEnd); if (r) return r;
1625
+ r = ReadLEB_u32(&tagIdx, &v->wasm, v->wasmEnd); if (r) return r;
1626
+
1627
+ if (typeIdx >= v->module->numFuncTypes) return m3Err_unknownType;
1628
+ if (!v->module || tagIdx >= v->module->numTags) return m3Err_unknownTag;
1629
+ IM3FuncType ct = v->module->funcTypes[typeIdx];
1630
+ if (!ct || !ct->isContinuation) return m3Err_typeMismatch;
1631
+
1632
+ // a switch tag carries no payload of its own
1633
+ IM3FuncType tagType = v->module->tags[tagIdx].type;
1634
+ if (tagType && tagType->numArgs != 0) return m3Err_typeMismatch;
1635
+
1636
+ // switch $ct1 : [t1* (ref null $ct1)] -> [t2*], where $ct1's last
1637
+ // parameter names $ct2 - the continuation this instruction
1638
+ // suspends - and t2* is $ct2's parameters, not $ct1's results
1639
+ IM3FuncType ft = ct->contFuncType;
1640
+ if (!ft || ft->numArgs == 0) return m3Err_typeMismatch;
1641
+
1642
+ IM3FuncType peer = Module_ContTypeOfRef(v->module, ft->types[ft->numRets + ft->numArgs - 1]);
1643
+ if (!peer || !peer->contFuncType) return m3Err_typeMismatch;
1644
+
1645
+ IM3FuncType self = peer->contFuncType;
1646
+
1647
+ r = v_pop_expect(v, c_m3Type_contref, &a); if (r) return r;
1648
+
1649
+ for (u16 i = (u16)(ft->numArgs - 1); i > 0; i--) {
1650
+ r = v_pop_expect(v, BaseTypeOf(ft->types[ft->numRets + i - 1]), &a);
1651
+ if (r) return r;
1652
+ }
1653
+
1654
+ for (u16 i = 0; i < self->numArgs; i++) {
1655
+ r = v_push(v, BaseTypeOf(self->types[self->numRets + i]));
1656
+ if (r) return r;
1657
+ }
1658
+ break;
1659
+ }
1660
+
1661
+ #endif // d_m3HasStackSwitching
1662
+
1663
+ default:
1664
+ // Unknown opcode - skip rather than fail for forward compat
1665
+ // (the compiler will reject truly unsupported ops later)
1666
+ break;
1667
+
1668
+ } // switch
1669
+
1670
+ if (r) return r;
1671
+
1672
+ } // while
1673
+
1674
+ // If we ran out of bytes without hitting the final end
1675
+ return m3Err_wasmMalformed;
1676
+ }
1677
+
1678
+ // ---------- Public entry point ----------
1679
+
1680
+ M3Result ValidateFunction (IM3Function i_function)
1681
+ {
1682
+ if (!i_function->wasm) return m3Err_none;
1683
+
1684
+ IM3FuncType funcType = i_function->funcType;
1685
+ IM3Module module = i_function->module;
1686
+
1687
+ IM3Runtime runtime = module->runtime;
1688
+
1689
+ ValCtx* v = runtime->validator;
1690
+ if (!v) {
1691
+ v = m3_AllocStruct (ValCtx);
1692
+ if (!v) return m3Err_mallocFailed;
1693
+ runtime->validator = v;
1694
+ }
1695
+
1696
+ memset(v, 0, sizeof(*v));
1697
+ v->module = module;
1698
+ v->function = i_function;
1699
+ v->wasm = i_function->wasm;
1700
+ v->wasmEnd = i_function->wasmEnd;
1701
+
1702
+ // Skip code size LEB
1703
+ u32 size;
1704
+ M3Result r = ReadLEB_u32(&size, &v->wasm, v->wasmEnd);
1705
+ if (r) return r;
1706
+
1707
+ // Parse locals
1708
+ u32 numLocalBlocks;
1709
+ r = ReadLEB_u32(&numLocalBlocks, &v->wasm, v->wasmEnd);
1710
+ if (r) return r;
1711
+
1712
+ // First: params. Running out of room has to be an error, not a truncation:
1713
+ // a short localTypes would make later local.get indices read as unknown
1714
+ u16 numParams = funcType ? funcType->numArgs : 0;
1715
+ if (numParams > d_m3ValStack) return m3Err_functionStackOverflow;
1716
+ for (u16 i = 0; i < numParams; i++) {
1717
+ v->localTypes[v->numLocals++] = BaseTypeOf(funcType->types[funcType->numRets + i]);
1718
+ }
1719
+
1720
+ // Then: declared locals
1721
+ for (u32 b = 0; b < numLocalBlocks; b++) {
1722
+ u32 count;
1723
+ r = ReadLEB_u32(&count, &v->wasm, v->wasmEnd);
1724
+ if (r) return r;
1725
+ m3type_t localType;
1726
+ r = ParseValueType(v->module, &localType, &v->wasm, v->wasmEnd);
1727
+ if (r) return r;
1728
+ u8 normalized = BaseTypeOf(localType);
1729
+ if (count > (u32) (d_m3ValStack - v->numLocals)) return m3Err_functionStackOverflow;
1730
+ for (u32 c = 0; c < count; c++) {
1731
+ v->localTypes[v->numLocals++] = normalized;
1732
+ }
1733
+ }
1734
+
1735
+ // Push the function-level control frame
1736
+ r = v_push_ctrl(v, 0x00, funcType); // opcode 0x00 marks function frame
1737
+ if (r) return r;
1738
+
1739
+ // Push params onto operand stack (they're part of the function body's initial stack)
1740
+ // Actually per the spec, locals are indexed but not on the operand stack.
1741
+ // The function frame's params are NOT pushed to the operand stack.
1742
+ // Only block params would be pushed (and for the function frame there are no block params
1743
+ // since the function body's "block type" has results = function returns, params = 0).
1744
+ // The function frame's label_types = results (since it's not a loop).
1745
+
1746
+ // Validate the body
1747
+ r = v_validate_body(v);
1748
+ if (r) return r;
1749
+
1750
+ // After validation, control stack should be empty and all declared bytes consumed
1751
+ if (v->ctrlTop != 0)
1752
+ return m3Err_wasmMalformed;
1753
+
1754
+ if (v->wasm != v->wasmEnd)
1755
+ return m3Err_wasmSectionUnderrun;
1756
+
1757
+ return m3Err_none;
1758
+ }
1759
+
1760
+ #else // !d_m3EnableValidation
1761
+
1762
+ M3Result ValidateFunction (IM3Function i_function)
1763
+ {
1764
+ (void)i_function;
1765
+ return m3Err_none;
1766
+ }
1767
+
1768
+ // clang-format on
1769
+
1770
+ #endif // d_m3EnableValidation