xbintsc 0.6.23 → 0.6.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) 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/concatenate.d.ts +23 -0
  11. package/dist/src/driver/bundler/concatenate.js +210 -0
  12. package/dist/src/driver/bundler/concatenate.js.map +1 -0
  13. package/dist/src/driver/bundler/graph.js +9 -5
  14. package/dist/src/driver/bundler/graph.js.map +1 -1
  15. package/dist/src/driver/bundler/merge.d.ts +4 -4
  16. package/dist/src/driver/bundler/merge.js +24 -207
  17. package/dist/src/driver/bundler/merge.js.map +1 -1
  18. package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
  19. package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
  20. package/dist/src/driver/bundler/namespace-object.js.map +1 -0
  21. package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
  22. package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
  23. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
  24. package/dist/src/driver/fingerprint.d.ts +2 -2
  25. package/dist/src/driver/fingerprint.js +3 -3
  26. package/dist/src/driver/fingerprint.js.map +1 -1
  27. package/dist/src/driver/link-features.js +1 -1
  28. package/dist/src/driver/link-features.js.map +1 -1
  29. package/dist/src/driver/modules.d.ts +12 -5
  30. package/dist/src/driver/modules.js +12 -5
  31. package/dist/src/driver/modules.js.map +1 -1
  32. package/dist/src/driver/paths.js +8 -4
  33. package/dist/src/driver/paths.js.map +1 -1
  34. package/dist/src/driver/runtime-objects.d.ts +1 -1
  35. package/dist/src/driver/runtime-objects.js +13 -1
  36. package/dist/src/driver/runtime-objects.js.map +1 -1
  37. package/dist/src/driver/version.d.ts +1 -1
  38. package/dist/src/driver/version.js +1 -1
  39. package/dist/src/extensions/catalog.js +5 -1
  40. package/dist/src/extensions/catalog.js.map +1 -1
  41. package/dist/src/extensions/gui/index.js +0 -1
  42. package/dist/src/extensions/gui/index.js.map +1 -1
  43. package/dist/src/extensions/link-feature.d.ts +8 -0
  44. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  45. package/dist/src/extensions/node/stream-web/index.js +59 -4
  46. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  47. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  48. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  49. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  50. package/dist/src/extensions/registry.d.ts +9 -1
  51. package/dist/src/extensions/registry.js +4 -1
  52. package/dist/src/extensions/registry.js.map +1 -1
  53. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  54. package/dist/src/extensions/wasm-asset.js +28 -0
  55. package/dist/src/extensions/wasm-asset.js.map +1 -0
  56. package/dist/src/extensions/wasm.d.ts +17 -0
  57. package/dist/src/extensions/wasm.js +68 -0
  58. package/dist/src/extensions/wasm.js.map +1 -0
  59. package/dist/src/extensions/webworker/index.d.ts +24 -0
  60. package/dist/src/extensions/webworker/index.js +54 -0
  61. package/dist/src/extensions/webworker/index.js.map +1 -0
  62. package/dist/src/index.d.ts +1 -0
  63. package/dist/src/index.js +1 -0
  64. package/dist/src/index.js.map +1 -1
  65. package/dist/tests/driver/deferred-reexport.test.js +25 -0
  66. package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
  67. package/dist/tests/driver/link-features.test.js +9 -0
  68. package/dist/tests/driver/link-features.test.js.map +1 -1
  69. package/dist/tests/e2e/async-fs.test.js +44 -0
  70. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  71. package/dist/tests/e2e/harness.d.ts +6 -0
  72. package/dist/tests/e2e/harness.js +13 -1
  73. package/dist/tests/e2e/harness.js.map +1 -1
  74. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  75. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  76. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  77. package/dist/tests/e2e/node-modules.test.js +1 -1
  78. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  79. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  80. package/dist/tests/e2e/web-streams.test.js +274 -0
  81. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  82. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  83. package/dist/tests/e2e/web-worker.test.js +74 -0
  84. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  85. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  86. package/dist/tests/e2e/webassembly.test.js +113 -0
  87. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  88. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  89. package/dist/tests/e2e/worker-api.test.js +460 -0
  90. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  91. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  92. package/dist/tests/e2e/worker-threads.test.js +160 -0
  93. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  94. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  95. package/dist/tests/extensions/wasm.test.js +53 -0
  96. package/dist/tests/extensions/wasm.test.js.map +1 -0
  97. package/doc/ai/cli.md +1 -1
  98. package/doc/ai/contributing.md +11 -4
  99. package/doc/ai/extensions.md +25 -2
  100. package/doc/ai/language-support.md +14 -2
  101. package/doc/ai/zh-CN/cli.md +1 -1
  102. package/doc/ai/zh-CN/contributing.md +8 -3
  103. package/doc/ai/zh-CN/extensions.md +23 -2
  104. package/doc/ai/zh-CN/language-support.md +13 -2
  105. package/doc/gui/api.md +1 -1
  106. package/doc/implemented/cli.md +1 -1
  107. package/doc/implemented/extensions.md +6 -5
  108. package/doc/implemented/node/stream-web.md +31 -0
  109. package/doc/implemented/node/worker-threads.md +88 -5
  110. package/doc/implemented/node.md +2 -1
  111. package/doc/implemented/streams.md +82 -0
  112. package/doc/implemented/wasm.md +80 -18
  113. package/doc/implemented/web-worker.md +84 -0
  114. package/doc/implemented.md +6 -2
  115. package/doc/unimplemented/deviations.md +1 -0
  116. package/doc/unimplemented/node/worker-threads.md +23 -0
  117. package/doc/unimplemented/node.md +1 -0
  118. package/doc/unimplemented/stdlib.md +2 -1
  119. package/doc/unimplemented/wasm.md +21 -9
  120. package/doc/unimplemented.md +1 -1
  121. package/doc/zh-CN/gui/api.md +1 -1
  122. package/doc/zh-CN/implemented/cli.md +1 -1
  123. package/doc/zh-CN/implemented/extensions.md +7 -6
  124. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  125. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  126. package/doc/zh-CN/implemented/node.md +2 -1
  127. package/doc/zh-CN/implemented/streams.md +60 -0
  128. package/doc/zh-CN/implemented/wasm.md +73 -16
  129. package/doc/zh-CN/implemented/web-worker.md +79 -0
  130. package/doc/zh-CN/implemented.md +6 -2
  131. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  132. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  133. package/doc/zh-CN/unimplemented/node.md +1 -0
  134. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  135. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  136. package/doc/zh-CN/unimplemented.md +1 -1
  137. package/llms.txt +2 -0
  138. package/package.json +8 -7
  139. package/runtime/ext_gui/browser.cpp +1 -1
  140. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  141. package/runtime/worker/worker_api.c +422 -0
  142. package/runtime/worker/worker_codec.c +489 -0
  143. package/runtime/worker/worker_core.c +445 -0
  144. package/runtime/worker/worker_internal.h +223 -0
  145. package/runtime/worker/worker_platform.c +344 -0
  146. package/runtime/worker/worker_ports.c +203 -0
  147. package/runtime/worker/worker_transport.c +464 -0
  148. package/runtime/worker/worker_wire.c +150 -0
  149. package/runtime/xt_builtins.c +15 -0
  150. package/runtime/xt_constructors.c +49 -2
  151. package/runtime/xt_stdlib2/error.inc +4 -0
  152. package/runtime/xt_streams/common.inc +232 -0
  153. package/runtime/xt_streams/constructors.inc +232 -0
  154. package/runtime/xt_streams/encoding.inc +124 -0
  155. package/runtime/xt_streams/readable-extra.inc +372 -0
  156. package/runtime/xt_streams/readable.inc +415 -0
  157. package/runtime/xt_streams/transform.inc +195 -0
  158. package/runtime/xt_streams/writable.inc +542 -0
  159. package/runtime/xt_streams.c +30 -0
  160. package/runtime/xt_wasm/wasm.h +148 -0
  161. package/runtime/xt_wasm/wasm_api.c +357 -0
  162. package/runtime/xt_wasm/wasm_api.h +37 -0
  163. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  164. package/runtime/xt_wasm/wasm_instance.c +591 -0
  165. package/runtime/xt_wasm/wasm_module.c +270 -0
  166. package/runtime/xt_wasm/wasm_values.c +177 -0
  167. package/runtime/xt_webassembly.c +15 -4
  168. package/scripts/fetch-vendored.ts +187 -20
  169. package/scripts/package-release.ts +3 -2
  170. package/src/cli/extensions.ts +11 -3
  171. package/src/cli/help.ts +1 -1
  172. package/src/codegen/generator/state.ts +7 -0
  173. package/src/codegen/generator/tables.ts +19 -1
  174. package/src/driver/bundler/README.md +55 -0
  175. package/src/driver/bundler/concatenate.ts +244 -0
  176. package/src/driver/bundler/graph.ts +9 -5
  177. package/src/driver/bundler/merge.ts +24 -215
  178. package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
  179. package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
  180. package/src/driver/fingerprint.ts +3 -3
  181. package/src/driver/link-features.ts +1 -1
  182. package/src/driver/modules.ts +12 -5
  183. package/src/driver/paths.ts +8 -4
  184. package/src/driver/runtime-objects.ts +13 -1
  185. package/src/driver/version.ts +1 -1
  186. package/src/extensions/catalog.ts +5 -1
  187. package/src/extensions/gui/index.ts +0 -1
  188. package/src/extensions/link-feature.ts +8 -0
  189. package/src/extensions/node/stream-web/index.ts +59 -4
  190. package/src/extensions/node/worker_threads/index.ts +24 -11
  191. package/src/extensions/registry.ts +13 -2
  192. package/src/extensions/wasm-asset.ts +32 -0
  193. package/src/extensions/wasm.ts +74 -0
  194. package/src/extensions/webworker/index.ts +58 -0
  195. package/src/index.ts +1 -0
  196. package/vendor/wasm3/.pin +1 -0
  197. package/vendor/wasm3/LICENSE +21 -0
  198. package/vendor/wasm3/m3_api_libc.c +265 -0
  199. package/vendor/wasm3/m3_api_libc.h +19 -0
  200. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  201. package/vendor/wasm3/m3_api_tracer.c +173 -0
  202. package/vendor/wasm3/m3_api_tracer.h +19 -0
  203. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  204. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  205. package/vendor/wasm3/m3_api_wasi.h +74 -0
  206. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  207. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  208. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  209. package/vendor/wasm3/m3_atomic.h +114 -0
  210. package/vendor/wasm3/m3_bind.c +174 -0
  211. package/vendor/wasm3/m3_bind.h +20 -0
  212. package/vendor/wasm3/m3_code.c +232 -0
  213. package/vendor/wasm3/m3_code.h +74 -0
  214. package/vendor/wasm3/m3_compile.c +6603 -0
  215. package/vendor/wasm3/m3_compile.h +354 -0
  216. package/vendor/wasm3/m3_config.h +448 -0
  217. package/vendor/wasm3/m3_config_platforms.h +345 -0
  218. package/vendor/wasm3/m3_core.c +1034 -0
  219. package/vendor/wasm3/m3_core.h +526 -0
  220. package/vendor/wasm3/m3_deterministic.c +65 -0
  221. package/vendor/wasm3/m3_deterministic.h +59 -0
  222. package/vendor/wasm3/m3_env.c +3146 -0
  223. package/vendor/wasm3/m3_env.h +748 -0
  224. package/vendor/wasm3/m3_exception.h +33 -0
  225. package/vendor/wasm3/m3_exec.c +8 -0
  226. package/vendor/wasm3/m3_exec.h +4110 -0
  227. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  228. package/vendor/wasm3/m3_exec_defs.h +76 -0
  229. package/vendor/wasm3/m3_function.c +278 -0
  230. package/vendor/wasm3/m3_function.h +244 -0
  231. package/vendor/wasm3/m3_host.h +226 -0
  232. package/vendor/wasm3/m3_host_none.h +76 -0
  233. package/vendor/wasm3/m3_host_posix.h +543 -0
  234. package/vendor/wasm3/m3_host_win32.h +253 -0
  235. package/vendor/wasm3/m3_info.c +616 -0
  236. package/vendor/wasm3/m3_info.h +38 -0
  237. package/vendor/wasm3/m3_math_utils.h +560 -0
  238. package/vendor/wasm3/m3_module.c +431 -0
  239. package/vendor/wasm3/m3_parse.c +1549 -0
  240. package/vendor/wasm3/m3_snapshot.c +3825 -0
  241. package/vendor/wasm3/m3_validate.c +1770 -0
  242. package/vendor/wasm3/m3_validate.h +25 -0
  243. package/vendor/wasm3/m3_wasi.h +10 -0
  244. package/vendor/wasm3/m3_xxh64.c +211 -0
  245. package/vendor/wasm3/wasm3.h +684 -0
  246. package/vendor/wasm3/wasm3_defs.h +350 -0
  247. package/dist/src/driver/bundler/namespace-import.js.map +0 -1
  248. package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
  249. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -0,0 +1,543 @@
1
+ //
2
+ // m3_host_posix.h
3
+ //
4
+ // The POSIX implementation of m3_host.h.
5
+ //
6
+ // A header rather than a translation unit of its own, and included by m3_core.c
7
+ // alone: a new .c would have to be added to every source list that names them one by
8
+ // one, and most of those are in build scripts outside this repository.
9
+ //
10
+
11
+ #ifndef m3_host_posix_h
12
+ #define m3_host_posix_h
13
+
14
+ #include "m3_core.h"
15
+
16
+ #if defined(_WIN32)
17
+ # error "This file is not for Windows"
18
+ #endif
19
+
20
+ #include "m3_host.h"
21
+
22
+ #include <fcntl.h>
23
+ #include <sys/file.h> // flock, which m3_HostMapFile takes a shared one with
24
+ #include <sys/mman.h>
25
+ #include <sys/stat.h>
26
+ #include <unistd.h>
27
+ #include <signal.h>
28
+ #include <errno.h>
29
+ #include <time.h>
30
+
31
+ // Whether pthread_getattr_np - or Darwin's pair of calls - can be reached without
32
+ // asking the build to link a threading library it may not be linking. glibc moved
33
+ // libpthread into libc in 2.34; before that this call was the one thing here that
34
+ // would not link, so a build on an older glibc keeps the compile-time budget unless
35
+ // it says otherwise. Decided here rather than in m3_config_platforms.h because
36
+ // __GLIBC__ only exists once a libc header has been seen, which m3_core.h above is.
37
+ #ifndef d_m3HasThreadStackProbe
38
+ # if defined(__APPLE__) || defined(__ANDROID__)
39
+ # define d_m3HasThreadStackProbe 1 // libSystem, bionic
40
+ # elif defined(__GLIBC__) && defined(__GLIBC_PREREQ)
41
+ # if __GLIBC_PREREQ(2, 34)
42
+ # define d_m3HasThreadStackProbe 1
43
+ # else
44
+ # define d_m3HasThreadStackProbe 0 // still in libpthread here
45
+ # endif
46
+ # elif defined(__linux__)
47
+ # define d_m3HasThreadStackProbe 1 // musl keeps it in libc
48
+ # else
49
+ # define d_m3HasThreadStackProbe 0
50
+ # endif
51
+ #endif
52
+
53
+ #if d_m3HasThreadStackProbe
54
+ # include <pthread.h>
55
+ # if defined(__linux__)
56
+ # include <sys/resource.h>
57
+ # include <sys/syscall.h>
58
+ # endif
59
+ #endif
60
+
61
+ #if d_m3HasThreadStackProbe && defined(__linux__)
62
+
63
+ // The first thread's stack, asked of the kernel rather than of pthreads, because
64
+ // pthreads is not always cheap about it: musl finds that stack by probing for its
65
+ // end a page at a time - a system call per page, and there can be a great many -
66
+ // where this is one line of text and a getrlimit. A thread given a stack of its own
67
+ // is left to pthreads, which has that one written down and answers at once.
68
+ //
69
+ // /proc says where the stack started, RLIMIT_STACK how far down it may grow. Another
70
+ // mapping can stop it growing that far, which this does not go looking for: the
71
+ // answer is then a stack larger than the real one, the budget stays what the build
72
+ // asked for, and that is what a build unable to measure gets anyway.
73
+ static
74
+ void* linux_main_stack_base (void)
75
+ {
76
+ if ((long)syscall(SYS_gettid) != (long)getpid()) {
77
+ return NULL;
78
+ }
79
+
80
+ struct rlimit limit;
81
+
82
+ if (getrlimit(RLIMIT_STACK, &limit) != 0 or
83
+ limit.rlim_cur == RLIM_INFINITY or limit.rlim_cur == 0) {
84
+ return NULL;
85
+ }
86
+
87
+ int fd = open("/proc/self/stat", O_RDONLY);
88
+
89
+ if (fd < 0) {
90
+ return NULL;
91
+ }
92
+
93
+ char line[512];
94
+ ssize_t got = read(fd, line, sizeof(line) - 1);
95
+
96
+ close(fd);
97
+
98
+ if (got <= 0) {
99
+ return NULL;
100
+ }
101
+
102
+ line[got] = 0;
103
+
104
+ // Field 28 is where the stack started. The fields before it include the
105
+ // executable's name in parentheses, and that name may hold anything at all -
106
+ // spaces and parentheses included - so the count begins after the last of those.
107
+ const char* field = strrchr(line, ')');
108
+
109
+ for (int number = 2; field and number < 28; ++number) {
110
+ field = strchr(field + 1, ' ');
111
+ }
112
+
113
+ if (field == NULL) {
114
+ return NULL;
115
+ }
116
+
117
+ unsigned long long top = strtoull(field + 1, NULL, 10);
118
+
119
+ // A limit reaching below nothing is not a stack this can describe
120
+ if (top == 0 or (unsigned long long) limit.rlim_cur > top) {
121
+ return NULL;
122
+ }
123
+
124
+ return (u8*)(uintptr_t)top - (size_t)limit.rlim_cur;
125
+ }
126
+
127
+ #endif
128
+
129
+ // Answers for every thread and not just the first, which is what asking pthreads is
130
+ // for: RLIMIT_STACK describes the stack the process started on, and a thread given a
131
+ // stack of its own is not it. Where the first thread can be recognized, it is asked
132
+ // about separately - see linux_main_stack_base.
133
+ void* m3_HostStackBase (void)
134
+ {
135
+ #if !d_m3HasThreadStackProbe
136
+
137
+ return NULL;
138
+
139
+ #elif defined(__APPLE__)
140
+
141
+ // Darwin describes a stack by its high end and its size, the opposite way round
142
+ pthread_t self = pthread_self();
143
+
144
+ u8* high = (u8*)pthread_get_stackaddr_np(self);
145
+ size_t size = pthread_get_stacksize_np(self);
146
+
147
+ if (high == NULL or size == 0) {
148
+ return NULL;
149
+ }
150
+
151
+ return high - size;
152
+
153
+ #else
154
+
155
+ # if defined(__linux__)
156
+
157
+ // Only where it answers: a system with no /proc, or a stack with no limit on how
158
+ // far it grows, falls through to pthreads like everything else
159
+ u8* main_base = (u8*)linux_main_stack_base();
160
+
161
+ if (main_base) {
162
+ return main_base;
163
+ }
164
+
165
+ # endif
166
+
167
+ pthread_attr_t attr;
168
+
169
+ if (pthread_getattr_np(pthread_self(), &attr) != 0) {
170
+ return NULL;
171
+ }
172
+
173
+ void* base = NULL;
174
+ size_t size = 0;
175
+
176
+ if (pthread_attr_getstack(&attr, &base, &size) != 0 or size == 0) {
177
+ base = NULL;
178
+ }
179
+
180
+ pthread_attr_destroy(&attr);
181
+
182
+ return base;
183
+
184
+ #endif
185
+ }
186
+
187
+ static IM3Runtime s_posixSuspendRuntime = NULL;
188
+ static struct sigaction s_oldSigTstp;
189
+ static struct sigaction s_oldSigInt;
190
+ static bool s_suspendHandlersInstalled = false;
191
+
192
+ static
193
+ void m3_PosixSignalHandler (int sig)
194
+ {
195
+ (void)sig;
196
+ if (s_posixSuspendRuntime) {
197
+ s_posixSuspendRuntime->suspendRequested = true;
198
+ }
199
+ }
200
+
201
+ void m3_HostInstallInterruptHandler (IM3Runtime io_runtime)
202
+ {
203
+ s_posixSuspendRuntime = io_runtime;
204
+ if (!s_suspendHandlersInstalled) {
205
+ struct sigaction sa;
206
+ memset(&sa, 0, sizeof(sa));
207
+ sa.sa_handler = m3_PosixSignalHandler;
208
+ sigemptyset(&sa.sa_mask);
209
+ sa.sa_flags = 0;
210
+
211
+ sigaction(SIGTSTP, &sa, &s_oldSigTstp);
212
+ sigaction(SIGINT, &sa, &s_oldSigInt);
213
+ s_suspendHandlersInstalled = true;
214
+ }
215
+ }
216
+
217
+ void m3_HostRemoveInterruptHandler (void)
218
+ {
219
+ if (s_suspendHandlersInstalled) {
220
+ sigaction(SIGTSTP, &s_oldSigTstp, NULL);
221
+ sigaction(SIGINT, &s_oldSigInt, NULL);
222
+ s_suspendHandlersInstalled = false;
223
+ }
224
+ s_posixSuspendRuntime = NULL;
225
+ }
226
+
227
+ u64 m3_HostTimeMs (void)
228
+ {
229
+ struct timespec now;
230
+
231
+ if (clock_gettime(CLOCK_REALTIME, &now) != 0 || now.tv_sec < 0) {
232
+ return 0;
233
+ }
234
+
235
+ return (u64)now.tv_sec * 1000 + (u64)now.tv_nsec / 1000000;
236
+ }
237
+
238
+ void m3_HostSleepNs (u64 i_ns)
239
+ {
240
+ struct timespec left;
241
+ left.tv_sec = (time_t)(i_ns / 1000000000u);
242
+ left.tv_nsec = (long)(i_ns % 1000000000u);
243
+
244
+ // a signal cuts the sleep short and reports what remains
245
+ while (nanosleep(&left, &left) != 0 and errno == EINTR) {
246
+ }
247
+ }
248
+
249
+ bool m3_HostMapFile (const char* i_path, size_t i_maxBytes, M3HostFile* o_file)
250
+ {
251
+ int fd = open(i_path, O_RDONLY);
252
+ if (fd < 0) {
253
+ return false;
254
+ }
255
+
256
+ struct stat info;
257
+
258
+ // Only a regular file of a size worth having: a directory cannot be mapped, a
259
+ // pipe or a device has no size to map, and an empty one is no module anyway.
260
+ // Anything this turns down is read instead, which is what makes a module out of
261
+ // a process substitution work.
262
+ if (fstat(fd, &info) != 0 or not S_ISREG(info.st_mode) or info.st_size <= 0 or
263
+ (off_t)(size_t) info.st_size != info.st_size or
264
+ (i_maxBytes and (uint64_t) info.st_size > (uint64_t)i_maxBytes)) {
265
+ close(fd);
266
+ return m3_HostReadFile(i_path, i_maxBytes, o_file);
267
+ }
268
+
269
+ size_t size = (size_t)info.st_size;
270
+ void* base = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
271
+
272
+ if (base == MAP_FAILED) {
273
+ close(fd);
274
+ return m3_HostReadFile(i_path, i_maxBytes, o_file);
275
+ }
276
+
277
+ #if defined(LOCK_SH)
278
+ // Shared, so that two wasm3 processes can run the same module, and
279
+ // non-blocking, so that a file somebody else has locked is mapped anyway rather
280
+ // than waiting on them. Failing changes nothing: the lock is advisory either
281
+ // way, and a filesystem that has no opinion about locks is not a reason to
282
+ // refuse the module. See m3_host.h for how little this promises.
283
+ (void)flock(fd, LOCK_SH | LOCK_NB);
284
+ #endif
285
+
286
+ // Unlike the mapping, the lock lives on the open file description, so the
287
+ // descriptor has to stay open for as long as the mapping does
288
+ o_file->data = base;
289
+ o_file->size = size;
290
+ o_file->handle = (intptr_t)fd;
291
+ o_file->mapped = true;
292
+ return true;
293
+ }
294
+
295
+ void m3_HostUnmapFile (M3HostFile* io_file)
296
+ {
297
+ if (io_file->mapped) {
298
+ munmap(io_file->data, io_file->size);
299
+
300
+ if (io_file->handle != d_m3HostNoHandle) {
301
+ close((int)io_file->handle); // and with it the lock
302
+ }
303
+ } else {
304
+ m3_Free(io_file->data);
305
+ }
306
+
307
+ io_file->data = NULL;
308
+ io_file->size = 0;
309
+ io_file->handle = d_m3HostNoHandle;
310
+ io_file->mapped = false;
311
+ }
312
+
313
+ // rename is atomic within a filesystem, which a file and its neighbour share
314
+ bool m3_HostReplaceFile (const char* i_from, const char* i_to)
315
+ {
316
+ return rename(i_from, i_to) == 0;
317
+ }
318
+
319
+ #if d_m3GuardedMemory
320
+
321
+ # include <setjmp.h>
322
+ # include <signal.h>
323
+
324
+ size_t m3_HostPageSize (void)
325
+ {
326
+ long size = sysconf(_SC_PAGESIZE);
327
+
328
+ return (size > 0) ? (size_t)size : 4096;
329
+ }
330
+
331
+ void* m3_HostReserve (size_t i_bytes)
332
+ {
333
+ // MAP_NORESERVE so that a reservation this large is not charged against the
334
+ // overcommit budget: almost none of it is ever going to be touched
335
+ void* base = mmap(NULL, i_bytes, PROT_NONE,
336
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
337
+
338
+ return (base == MAP_FAILED) ? NULL : base;
339
+ }
340
+
341
+ bool m3_HostCommit (void* i_address, size_t i_bytes)
342
+ {
343
+ if (i_bytes == 0) {
344
+ return true;
345
+ }
346
+
347
+ // The pages are already there and already zero - the reservation was anonymous -
348
+ // so committing is only a matter of being allowed to reach them
349
+ return mprotect(i_address, i_bytes, PROT_READ | PROT_WRITE) == 0;
350
+ }
351
+
352
+ bool m3_HostDecommit (void* i_address, size_t i_bytes)
353
+ {
354
+ if (i_bytes == 0) {
355
+ return true;
356
+ }
357
+
358
+ // Mapping fresh anonymous pages over the range is what drops the old ones -
359
+ // mprotect alone would leave them allocated and still holding what was written
360
+ void* again = mmap(i_address, i_bytes, PROT_NONE,
361
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_NORESERVE, -1, 0);
362
+
363
+ return again == i_address;
364
+ }
365
+
366
+ void m3_HostRelease (void* i_address, size_t i_bytes)
367
+ {
368
+ munmap(i_address, i_bytes);
369
+ }
370
+
371
+
372
+ // What a protected call left behind for the signal handler to find. Per-thread,
373
+ // because the fault arrives on the thread that caused it and nowhere else, and a
374
+ // chain, because a host function may call back into Wasm from inside one.
375
+ typedef struct M3GuardFrame {
376
+ sigjmp_buf jmp;
377
+ const u8* low;
378
+ size_t bytes;
379
+ struct M3GuardFrame* previous;
380
+ } M3GuardFrame;
381
+
382
+ // Installing happens once and is never retried: a second pass would capture guard_signal
383
+ // itself as the previous handler, and an unclaimed fault would then call it forever. So
384
+ // the attempt and what came of it are one piece of state, settled on the first ask.
385
+ typedef enum M3GuardState {
386
+ guards_initial = 0,
387
+ guards_active,
388
+ guards_unavailable
389
+ } M3GuardState;
390
+
391
+ static M3_THREAD_LOCAL M3GuardFrame* g_guardFrame;
392
+
393
+ // The handlers that were there first. A fault this does not claim has to end the way
394
+ // it would have without any of this, so those are called rather than replaced.
395
+ static struct sigaction g_previousSegv;
396
+ static struct sigaction g_previousBus;
397
+ static M3GuardState g_guards;
398
+
399
+ static
400
+ void guard_signal (int i_signal, siginfo_t* i_info, void* i_ucontext)
401
+ {
402
+ const M3GuardFrame* frame = g_guardFrame;
403
+
404
+ if (frame and i_info) {
405
+ const u8* address = (const u8*)i_info->si_addr;
406
+
407
+ if (address >= frame->low and address < frame->low + frame->bytes) {
408
+ // Everything the interpreter was in the middle of is data on the heap,
409
+ // so there is nothing here to unwind - see m3_HostProtectedCall
410
+ siglongjmp(((M3GuardFrame*)frame)->jmp, 1);
411
+ }
412
+ }
413
+
414
+ const struct sigaction* previous = (i_signal == SIGBUS) ? &g_previousBus : &g_previousSegv;
415
+
416
+ if ((previous->sa_flags & SA_SIGINFO) and previous->sa_sigaction) {
417
+ previous->sa_sigaction(i_signal, i_info, i_ucontext);
418
+ } else if (previous->sa_handler != SIG_DFL and previous->sa_handler != SIG_IGN) {
419
+ previous->sa_handler(i_signal);
420
+ } else {
421
+ // Put the default back and return: the instruction runs again, faults again,
422
+ // and the process dies where it stood rather than somewhere in here
423
+ struct sigaction dfl;
424
+ memset(&dfl, 0, sizeof(dfl));
425
+ dfl.sa_handler = SIG_DFL;
426
+ sigemptyset(&dfl.sa_mask);
427
+ sigaction(i_signal, &dfl, NULL);
428
+ }
429
+ }
430
+
431
+ // Whether i_signal is being delivered to guard_signal. sigaction reporting success is
432
+ // not enough on its own: AddressSanitizer's interceptor returns 0 for a handler it has
433
+ // no intention of installing, so the only answer to trust is the one the system gives
434
+ // back when asked what is installed.
435
+ static
436
+ bool guard_handler_took (int i_signal)
437
+ {
438
+ struct sigaction installed;
439
+ memset(&installed, 0, sizeof(installed));
440
+
441
+ if (sigaction(i_signal, NULL, &installed) != 0) {
442
+ return false;
443
+ }
444
+
445
+ return (installed.sa_flags & SA_SIGINFO) and installed.sa_sigaction == guard_signal;
446
+ }
447
+
448
+ static
449
+ bool install_guard_handlers (void)
450
+ {
451
+ if (g_guards != guards_initial) {
452
+ return g_guards == guards_active;
453
+ }
454
+
455
+ struct sigaction action;
456
+ memset(&action, 0, sizeof(action));
457
+
458
+ action.sa_sigaction = guard_signal;
459
+ action.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_NODEFER;
460
+ sigemptyset(&action.sa_mask);
461
+
462
+ sigaction(SIGSEGV, &action, &g_previousSegv);
463
+
464
+ // Darwin reports a write to a PROT_NONE page as SIGBUS where Linux reports
465
+ // SIGSEGV, so both have to be claimed
466
+ sigaction(SIGBUS, &action, &g_previousBus);
467
+
468
+ g_guards = (guard_handler_took(SIGSEGV) and guard_handler_took(SIGBUS))
469
+ ? guards_active
470
+ : guards_unavailable;
471
+
472
+ return g_guards == guards_active;
473
+ }
474
+
475
+ // A stack for the handler to run on, so that a fault which happens because the
476
+ // thread's own stack ran out still has somewhere to be handled. One per thread, and
477
+ // only where the thread has not already been given one.
478
+ static
479
+ void install_alt_stack (void)
480
+ {
481
+ // A fixed size rather than SIGSTKSZ, which recent glibc made a call to sysconf
482
+ // and so no longer something an array can be declared with. 64KiB is well over
483
+ // what any of these handlers need - the claimed path is a siglongjmp, and the
484
+ // unclaimed one is a call to whatever was there before.
485
+ static M3_THREAD_LOCAL u8 stack[64 * 1024];
486
+ static M3_THREAD_LOCAL bool installed;
487
+
488
+ if (installed) {
489
+ return;
490
+ }
491
+ installed = true;
492
+
493
+ stack_t current;
494
+ if (sigaltstack(NULL, &current) == 0 and not (current.ss_flags & SS_DISABLE)) {
495
+ return; // this thread already has one
496
+ }
497
+
498
+ stack_t alt;
499
+ alt.ss_sp = stack;
500
+ alt.ss_size = sizeof(stack);
501
+ alt.ss_flags = 0;
502
+
503
+ sigaltstack(&alt, NULL);
504
+ }
505
+
506
+ bool m3_HostGuardsActive (void)
507
+ {
508
+ return install_guard_handlers();
509
+ }
510
+
511
+ bool m3_HostProtectedCall (void (*i_body)(void*), void* i_context,
512
+ void* i_guardLow, size_t i_guardBytes)
513
+ {
514
+ install_guard_handlers();
515
+ install_alt_stack();
516
+
517
+ M3GuardFrame frame;
518
+ frame.low = (const u8*)i_guardLow;
519
+ frame.bytes = i_guardBytes;
520
+ frame.previous = g_guardFrame;
521
+
522
+ g_guardFrame = &frame;
523
+
524
+ bool finished;
525
+
526
+ // The mask is deliberately not saved: doing so costs a system call on every
527
+ // call into Wasm, and the one thing it would have restored - SIGSEGV unblocked -
528
+ // SA_NODEFER already arranges by never blocking it in the first place.
529
+ if (sigsetjmp(frame.jmp, 0) == 0) {
530
+ i_body(i_context);
531
+ finished = true;
532
+ } else {
533
+ finished = false;
534
+ }
535
+
536
+ g_guardFrame = frame.previous;
537
+
538
+ return finished;
539
+ }
540
+
541
+ #endif // d_m3GuardedMemory
542
+
543
+ #endif // m3_host_posix_h