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,979 @@
1
+ //
2
+ // m3_api_wasi_posix.h
3
+ //
4
+ // The POSIX implementation of m3_api_wasi_host.h. Every path here is resolved with
5
+ // an *at() call against the descriptor a preopen holds, which is what keeps the
6
+ // guest inside the directories it was given.
7
+ //
8
+ // A header rather than a translation unit of its own, and included by m3_api_wasi.c
9
+ // alone: a new .c would have to be added to every source list that names them one by
10
+ // one, and most of those are in build scripts outside this repository.
11
+ //
12
+
13
+ #ifndef m3_api_wasi_posix_h
14
+ #define m3_api_wasi_posix_h
15
+
16
+ #include "m3_core.h"
17
+
18
+ #if defined(_WIN32)
19
+ # error "This file is not for Windows"
20
+ #endif
21
+
22
+ #include <sys/types.h>
23
+ #include <sys/stat.h>
24
+ #include <errno.h>
25
+ #include <fcntl.h>
26
+ #include <limits.h>
27
+ #include <poll.h>
28
+ #include <string.h>
29
+ #include <time.h>
30
+
31
+ // FIONREAD, which poll_oneoff uses to tell the guest how much is waiting on a
32
+ // descriptor. Nothing here needs it - a descriptor whose count cannot be taken is
33
+ // reported as having something rather than nothing - so a system without the header
34
+ // simply does without.
35
+ #if !defined(__wasi__)
36
+ # include <sys/ioctl.h>
37
+ #endif
38
+
39
+ #if defined(__wasi__) || defined(__APPLE__) || defined(__ANDROID_API__) || defined(__OpenBSD__) || defined(__linux__) || defined(__EMSCRIPTEN__) || defined(__CYGWIN__)
40
+ # include <unistd.h>
41
+ # include <sys/uio.h>
42
+ # if defined(__APPLE__)
43
+ # include <TargetConditionals.h>
44
+ # if TARGET_OS_OSX // TARGET_OS_MAC includes iOS
45
+ # include <sys/random.h>
46
+ # else // iOS / Simulator
47
+ # include <Security/Security.h>
48
+ # endif
49
+ # else
50
+ # include <sys/random.h>
51
+ # endif
52
+ # define d_m3WasiHasIovec
53
+ #endif
54
+
55
+ #include "m3_api_wasi_host.h"
56
+
57
+
58
+ /*
59
+ * Plumbing
60
+ */
61
+
62
+ static
63
+ __wasi_errno_t errno_to_wasi (int errnum)
64
+ {
65
+ // clang-format off
66
+ switch (errnum) {
67
+ case EPERM: return __WASI_ERRNO_PERM;
68
+ case ENOENT: return __WASI_ERRNO_NOENT;
69
+ case ESRCH: return __WASI_ERRNO_SRCH;
70
+ case EINTR: return __WASI_ERRNO_INTR;
71
+ case EIO: return __WASI_ERRNO_IO;
72
+ case ENXIO: return __WASI_ERRNO_NXIO;
73
+ case E2BIG: return __WASI_ERRNO_2BIG;
74
+ case ENOEXEC: return __WASI_ERRNO_NOEXEC;
75
+ case EBADF: return __WASI_ERRNO_BADF;
76
+ case ECHILD: return __WASI_ERRNO_CHILD;
77
+ case EAGAIN: return __WASI_ERRNO_AGAIN;
78
+ case ENOMEM: return __WASI_ERRNO_NOMEM;
79
+ case EACCES: return __WASI_ERRNO_ACCES;
80
+ case EFAULT: return __WASI_ERRNO_FAULT;
81
+ case EBUSY: return __WASI_ERRNO_BUSY;
82
+ case EEXIST: return __WASI_ERRNO_EXIST;
83
+ case EXDEV: return __WASI_ERRNO_XDEV;
84
+ case ENODEV: return __WASI_ERRNO_NODEV;
85
+ case ENOTDIR: return __WASI_ERRNO_NOTDIR;
86
+ case EISDIR: return __WASI_ERRNO_ISDIR;
87
+ case EINVAL: return __WASI_ERRNO_INVAL;
88
+ case ENFILE: return __WASI_ERRNO_NFILE;
89
+ case EMFILE: return __WASI_ERRNO_MFILE;
90
+ case ENOTTY: return __WASI_ERRNO_NOTTY;
91
+ case ETXTBSY: return __WASI_ERRNO_TXTBSY;
92
+ case EFBIG: return __WASI_ERRNO_FBIG;
93
+ case ENOSPC: return __WASI_ERRNO_NOSPC;
94
+ case ESPIPE: return __WASI_ERRNO_SPIPE;
95
+ case EROFS: return __WASI_ERRNO_ROFS;
96
+ case EMLINK: return __WASI_ERRNO_MLINK;
97
+ case EPIPE: return __WASI_ERRNO_PIPE;
98
+ case EDOM: return __WASI_ERRNO_DOM;
99
+ case ERANGE: return __WASI_ERRNO_RANGE;
100
+ }
101
+ // clang-format on
102
+ return __WASI_ERRNO_INVAL;
103
+ }
104
+
105
+ static
106
+ __wasi_filetype_t filetype_from_stat_mode (int mode)
107
+ {
108
+ return (S_ISBLK(mode) ? __WASI_FILETYPE_BLOCK_DEVICE : 0) |
109
+ (S_ISCHR(mode) ? __WASI_FILETYPE_CHARACTER_DEVICE : 0) |
110
+ (S_ISDIR(mode) ? __WASI_FILETYPE_DIRECTORY : 0) |
111
+ (S_ISREG(mode) ? __WASI_FILETYPE_REGULAR_FILE : 0) |
112
+ //(S_ISSOCK(mode) ? __WASI_FILETYPE_SOCKET_STREAM : 0) |
113
+ (S_ISLNK(mode) ? __WASI_FILETYPE_SYMBOLIC_LINK : 0);
114
+ }
115
+
116
+ static
117
+ void filestat_from_stat (const struct stat* i_stat, m3_wasi_filestat_t* o_stat)
118
+ {
119
+ o_stat->dev = (uint64_t)i_stat->st_dev;
120
+ o_stat->ino = (uint64_t)i_stat->st_ino;
121
+ o_stat->nlink = (uint64_t)i_stat->st_nlink;
122
+ o_stat->size = (uint64_t)i_stat->st_size;
123
+ o_stat->atim = (uint64_t)i_stat->st_atime * 1000000000ULL;
124
+ o_stat->mtim = (uint64_t)i_stat->st_mtime * 1000000000ULL;
125
+ o_stat->ctim = (uint64_t)i_stat->st_ctime * 1000000000ULL;
126
+ o_stat->filetype = filetype_from_stat_mode(i_stat->st_mode);
127
+ }
128
+
129
+ // The descriptor a preopened directory holds, or -1 where i_fd names no preopen
130
+ static
131
+ int preopen_fd (__wasi_fd_t i_fd)
132
+ {
133
+ if (i_fd >= d_m3WasiPreopenCount) {
134
+ return -1;
135
+ }
136
+ return m3_wasi_preopen[i_fd].fd;
137
+ }
138
+
139
+ // wasi-libc (>= wasi-sdk 12) makes clockid_t an opaque pointer rather than an int,
140
+ // so the "no such clock" sentinel has to be the null one
141
+ #if defined(__wasi__)
142
+ # define d_m3ClockIdInvalid ((clockid_t) NULL)
143
+ #else
144
+ # define d_m3ClockIdInvalid ((clockid_t) -1)
145
+ #endif
146
+
147
+ static
148
+ clockid_t convert_clockid (__wasi_clockid_t in)
149
+ {
150
+ switch (in) {
151
+ case __WASI_CLOCKID_MONOTONIC: return CLOCK_MONOTONIC;
152
+ case __WASI_CLOCKID_REALTIME: return CLOCK_REALTIME;
153
+ #if defined(CLOCK_PROCESS_CPUTIME_ID)
154
+ case __WASI_CLOCKID_PROCESS_CPUTIME_ID: return CLOCK_PROCESS_CPUTIME_ID;
155
+ #endif
156
+ #if defined(CLOCK_THREAD_CPUTIME_ID)
157
+ case __WASI_CLOCKID_THREAD_CPUTIME_ID: return CLOCK_THREAD_CPUTIME_ID;
158
+ #endif
159
+ default: return d_m3ClockIdInvalid;
160
+ }
161
+ }
162
+
163
+ static
164
+ __wasi_timestamp_t convert_timespec (const struct timespec* ts)
165
+ {
166
+ if (ts->tv_sec < 0) {
167
+ return 0;
168
+ }
169
+ if ((__wasi_timestamp_t)ts->tv_sec >= UINT64_MAX / 1000000000) {
170
+ return UINT64_MAX;
171
+ }
172
+ return (__wasi_timestamp_t)ts->tv_sec * 1000000000 + ts->tv_nsec;
173
+ }
174
+
175
+
176
+ /*
177
+ * Setup
178
+ */
179
+
180
+ static
181
+ void m3_wasi_host_init ()
182
+ {
183
+ for (int i = 3; i < d_m3WasiPreopenCount; i++) {
184
+ if (m3_wasi_preopen[i].fd < 0) {
185
+ m3_wasi_preopen[i].fd = open(m3_wasi_preopen[i].real_path, O_RDONLY);
186
+ }
187
+ }
188
+ }
189
+
190
+
191
+ /*
192
+ * Descriptors
193
+ */
194
+
195
+ static
196
+ __wasi_errno_t m3_wasi_host_fd_close (__wasi_fd_t i_fd)
197
+ {
198
+ if (close(i_fd) != 0) {
199
+ return errno_to_wasi(errno);
200
+ }
201
+ return __WASI_ERRNO_SUCCESS;
202
+ }
203
+
204
+ // no fdatasync on Apple, where fsync is the stronger promise of the two, and neither
205
+ // on a platform this does not name
206
+ #if defined(__APPLE__)
207
+ # define d_m3WasiDatasync(FD) fsync(FD)
208
+ #elif defined(__ANDROID_API__) || defined(__OpenBSD__) || defined(__linux__) || defined(__EMSCRIPTEN__)
209
+ # define d_m3WasiDatasync(FD) fdatasync(FD)
210
+ #endif
211
+
212
+ static
213
+ __wasi_errno_t m3_wasi_host_fd_datasync (__wasi_fd_t i_fd)
214
+ {
215
+ #if defined(d_m3WasiDatasync)
216
+ if (d_m3WasiDatasync(i_fd) != 0) {
217
+ return errno_to_wasi(errno);
218
+ }
219
+ return __WASI_ERRNO_SUCCESS;
220
+ #else
221
+ return __WASI_ERRNO_NOSYS;
222
+ #endif
223
+ }
224
+
225
+ static
226
+ __wasi_errno_t m3_wasi_host_fd_advise (__wasi_fd_t i_fd, __wasi_filesize_t i_offset,
227
+ __wasi_filesize_t i_length, __wasi_advice_t i_advice)
228
+ {
229
+ // Purely advisory: passing it on is a best effort, and dropping it is
230
+ // still a conforming implementation. Only the fd has to be valid.
231
+ #if defined(POSIX_FADV_NORMAL)
232
+ int adv;
233
+ switch (i_advice) {
234
+ case __WASI_ADVICE_NORMAL: adv = POSIX_FADV_NORMAL; break;
235
+ case __WASI_ADVICE_SEQUENTIAL: adv = POSIX_FADV_SEQUENTIAL; break;
236
+ case __WASI_ADVICE_RANDOM: adv = POSIX_FADV_RANDOM; break;
237
+ case __WASI_ADVICE_WILLNEED: adv = POSIX_FADV_WILLNEED; break;
238
+ case __WASI_ADVICE_DONTNEED: adv = POSIX_FADV_DONTNEED; break;
239
+ case __WASI_ADVICE_NOREUSE: adv = POSIX_FADV_NOREUSE; break;
240
+ default: return __WASI_ERRNO_INVAL;
241
+ }
242
+
243
+ int ret = posix_fadvise(i_fd, i_offset, i_length, adv);
244
+ if (ret != 0) {
245
+ return errno_to_wasi(ret);
246
+ }
247
+ #else
248
+ if (i_advice > __WASI_ADVICE_NOREUSE) {
249
+ return __WASI_ERRNO_INVAL;
250
+ }
251
+
252
+ struct stat fd_stat;
253
+ if (fstat(i_fd, &fd_stat) != 0) {
254
+ return errno_to_wasi(errno);
255
+ }
256
+ #endif
257
+
258
+ return __WASI_ERRNO_SUCCESS;
259
+ }
260
+
261
+ static
262
+ __wasi_errno_t m3_wasi_host_fd_fdstat (__wasi_fd_t i_fd, __wasi_filetype_t* o_filetype,
263
+ __wasi_fdflags_t* o_flags)
264
+ {
265
+ *o_filetype = __WASI_FILETYPE_UNKNOWN;
266
+ *o_flags = 0;
267
+
268
+ int fl = fcntl(i_fd, F_GETFL);
269
+ if (fl < 0) {
270
+ return errno_to_wasi(errno);
271
+ }
272
+
273
+ struct stat fd_stat;
274
+ if (fstat(i_fd, &fd_stat) != 0) {
275
+ return errno_to_wasi(errno);
276
+ }
277
+
278
+ *o_filetype = filetype_from_stat_mode(fd_stat.st_mode);
279
+ *o_flags = ((fl & O_APPEND) ? __WASI_FDFLAGS_APPEND : 0) |
280
+ ((fl & O_DSYNC) ? __WASI_FDFLAGS_DSYNC : 0) |
281
+ ((fl & O_NONBLOCK) ? __WASI_FDFLAGS_NONBLOCK : 0) |
282
+ //((fl & O_RSYNC) ? __WASI_FDFLAGS_RSYNC : 0) |
283
+ ((fl & O_SYNC) ? __WASI_FDFLAGS_SYNC : 0);
284
+ return __WASI_ERRNO_SUCCESS;
285
+ }
286
+
287
+ // F_SETFL changes only a handful of the bits a file was opened with: the sync ones
288
+ // are fixed at open time, so a request to turn one on is refused rather than
289
+ // accepted and quietly dropped. Turning one off is refused the same way - the guest
290
+ // asked for a state the descriptor cannot be put into either way.
291
+ static
292
+ __wasi_errno_t m3_wasi_host_fd_fdstat_set_flags (__wasi_fd_t i_fd, __wasi_fdflags_t i_flags)
293
+ {
294
+ int fl = fcntl(i_fd, F_GETFL);
295
+ if (fl < 0) {
296
+ return errno_to_wasi(errno);
297
+ }
298
+
299
+ const __wasi_fdflags_t fixed = __WASI_FDFLAGS_DSYNC | __WASI_FDFLAGS_RSYNC | __WASI_FDFLAGS_SYNC;
300
+
301
+ // RSYNC is left out of the readback for the same reason fd_fdstat does not
302
+ // report it: it has no bit of its own everywhere this builds
303
+ __wasi_fdflags_t current = ((fl & O_DSYNC) ? __WASI_FDFLAGS_DSYNC : 0) |
304
+ ((fl & O_SYNC) ? __WASI_FDFLAGS_SYNC : 0);
305
+
306
+ if ((i_flags & fixed) != (current & fixed)) {
307
+ return __WASI_ERRNO_NOTSUP;
308
+ }
309
+
310
+ fl &= ~(O_APPEND | O_NONBLOCK);
311
+ fl |= (i_flags & __WASI_FDFLAGS_APPEND) ? O_APPEND : 0;
312
+ fl |= (i_flags & __WASI_FDFLAGS_NONBLOCK) ? O_NONBLOCK : 0;
313
+
314
+ if (fcntl(i_fd, F_SETFL, fl) < 0) {
315
+ return errno_to_wasi(errno);
316
+ }
317
+
318
+ return __WASI_ERRNO_SUCCESS;
319
+ }
320
+
321
+ static
322
+ __wasi_errno_t m3_wasi_host_fd_filestat (__wasi_fd_t i_fd, m3_wasi_filestat_t* o_stat)
323
+ {
324
+ memset(o_stat, 0, sizeof(*o_stat));
325
+
326
+ struct stat fd_stat;
327
+ if (fstat(i_fd, &fd_stat) != 0) {
328
+ return errno_to_wasi(errno);
329
+ }
330
+
331
+ filestat_from_stat(&fd_stat, o_stat);
332
+ return __WASI_ERRNO_SUCCESS;
333
+ }
334
+
335
+ static
336
+ __wasi_errno_t m3_wasi_host_fd_set_size (__wasi_fd_t i_fd, __wasi_filesize_t i_size)
337
+ {
338
+ if (ftruncate(i_fd, (off_t)i_size) != 0) {
339
+ return errno_to_wasi(errno);
340
+ }
341
+ return __WASI_ERRNO_SUCCESS;
342
+ }
343
+
344
+ static
345
+ __wasi_errno_t m3_wasi_host_fd_set_times (__wasi_fd_t i_fd, __wasi_timestamp_t i_atim,
346
+ __wasi_timestamp_t i_mtim, __wasi_fstflags_t i_flags)
347
+ {
348
+ // a time is either given, asked for as now, or left alone
349
+ struct timespec times[2];
350
+
351
+ if (i_flags & __WASI_FSTFLAGS_ATIM_NOW) {
352
+ times[0].tv_sec = 0;
353
+ times[0].tv_nsec = UTIME_NOW;
354
+ } else if (i_flags & __WASI_FSTFLAGS_ATIM) {
355
+ times[0].tv_sec = i_atim / 1000000000ULL;
356
+ times[0].tv_nsec = i_atim % 1000000000ULL;
357
+ } else {
358
+ times[0].tv_sec = 0;
359
+ times[0].tv_nsec = UTIME_OMIT;
360
+ }
361
+
362
+ if (i_flags & __WASI_FSTFLAGS_MTIM_NOW) {
363
+ times[1].tv_sec = 0;
364
+ times[1].tv_nsec = UTIME_NOW;
365
+ } else if (i_flags & __WASI_FSTFLAGS_MTIM) {
366
+ times[1].tv_sec = i_mtim / 1000000000ULL;
367
+ times[1].tv_nsec = i_mtim % 1000000000ULL;
368
+ } else {
369
+ times[1].tv_sec = 0;
370
+ times[1].tv_nsec = UTIME_OMIT;
371
+ }
372
+
373
+ if (futimens(i_fd, times) != 0) {
374
+ return errno_to_wasi(errno);
375
+ }
376
+ return __WASI_ERRNO_SUCCESS;
377
+ }
378
+
379
+ static
380
+ __wasi_errno_t m3_wasi_host_fd_seek (__wasi_fd_t i_fd, int64_t i_offset, int i_whence,
381
+ __wasi_filesize_t* o_pos)
382
+ {
383
+ *o_pos = 0;
384
+
385
+ off_t ret = lseek(i_fd, (off_t)i_offset, i_whence);
386
+ if (ret < 0) {
387
+ return errno_to_wasi(errno);
388
+ }
389
+
390
+ *o_pos = (__wasi_filesize_t)ret;
391
+ return __WASI_ERRNO_SUCCESS;
392
+ }
393
+
394
+
395
+ /*
396
+ * Transfers
397
+ */
398
+
399
+ #if defined(d_m3WasiHasIovec)
400
+
401
+ // m3_wasi_iovec_t is laid out as struct iovec is, but they are still two types, so
402
+ // the list is copied across rather than cast.
403
+ static
404
+ __wasi_errno_t transfer_vectored (__wasi_fd_t i_fd, const m3_wasi_iovec_t* i_iovs,
405
+ __wasi_size_t i_iovsLen, bool i_write, __wasi_size_t* o_done)
406
+ {
407
+ *o_done = 0;
408
+
409
+ struct iovec iovs[d_m3WasiMaxIovs];
410
+ for (__wasi_size_t i = 0; i < i_iovsLen; i++) {
411
+ iovs[i].iov_base = i_iovs[i].buf;
412
+ iovs[i].iov_len = i_iovs[i].len;
413
+ }
414
+
415
+ ssize_t ret = i_write ? writev(i_fd, iovs, (int)i_iovsLen) : readv(i_fd, iovs, (int)i_iovsLen);
416
+ if (ret < 0) {
417
+ return errno_to_wasi(errno);
418
+ }
419
+
420
+ *o_done = (__wasi_size_t)ret;
421
+ return __WASI_ERRNO_SUCCESS;
422
+ }
423
+
424
+ #else
425
+
426
+ // No readv/writev here, so the list is walked one buffer at a time and a short
427
+ // transfer ends it, which is what readv would have done
428
+ static
429
+ __wasi_errno_t transfer_vectored (__wasi_fd_t i_fd, const m3_wasi_iovec_t* i_iovs,
430
+ __wasi_size_t i_iovsLen, bool i_write, __wasi_size_t* o_done)
431
+ {
432
+ *o_done = 0;
433
+
434
+ size_t done = 0;
435
+
436
+ for (__wasi_size_t i = 0; i < i_iovsLen; i++) {
437
+ void* addr = i_iovs[i].buf;
438
+ size_t len = i_iovs[i].len;
439
+ if (len == 0) {
440
+ continue;
441
+ }
442
+
443
+ ssize_t ret = i_write ? write(i_fd, addr, len) : read(i_fd, addr, len);
444
+ if (ret < 0) {
445
+ return errno_to_wasi(errno);
446
+ }
447
+
448
+ done += (size_t)ret;
449
+ if ((size_t)ret < len) {
450
+ break;
451
+ }
452
+ }
453
+
454
+ *o_done = (__wasi_size_t)done;
455
+ return __WASI_ERRNO_SUCCESS;
456
+ }
457
+
458
+ #endif // d_m3WasiHasIovec
459
+
460
+ // pread/pwrite over the list. The offset walks with the data, so a short transfer
461
+ // stops it rather than leaving a hole.
462
+ static
463
+ __wasi_errno_t transfer_at (__wasi_fd_t i_fd, const m3_wasi_iovec_t* i_iovs,
464
+ __wasi_size_t i_iovsLen, __wasi_filesize_t i_offset, bool i_write,
465
+ __wasi_size_t* o_done)
466
+ {
467
+ *o_done = 0;
468
+
469
+ size_t done = 0;
470
+
471
+ for (__wasi_size_t i = 0; i < i_iovsLen; i++) {
472
+ void* addr = i_iovs[i].buf;
473
+ size_t len = i_iovs[i].len;
474
+ if (len == 0) {
475
+ continue;
476
+ }
477
+
478
+ ssize_t ret = i_write ? pwrite(i_fd, addr, len, (off_t)(i_offset + done))
479
+ : pread(i_fd, addr, len, (off_t)(i_offset + done));
480
+ if (ret < 0) {
481
+ return errno_to_wasi(errno);
482
+ }
483
+
484
+ done += (size_t)ret;
485
+ if ((size_t)ret < len) {
486
+ break;
487
+ }
488
+ }
489
+
490
+ *o_done = (__wasi_size_t)done;
491
+ return __WASI_ERRNO_SUCCESS;
492
+ }
493
+
494
+ static
495
+ __wasi_errno_t m3_wasi_host_fd_read (__wasi_fd_t i_fd, const m3_wasi_iovec_t* i_iovs,
496
+ __wasi_size_t i_iovsLen, __wasi_size_t* o_done)
497
+ {
498
+ return transfer_vectored(i_fd, i_iovs, i_iovsLen, false, o_done);
499
+ }
500
+
501
+ static
502
+ __wasi_errno_t m3_wasi_host_fd_write (__wasi_fd_t i_fd, const m3_wasi_iovec_t* i_iovs,
503
+ __wasi_size_t i_iovsLen, __wasi_size_t* o_done)
504
+ {
505
+ return transfer_vectored(i_fd, i_iovs, i_iovsLen, true, o_done);
506
+ }
507
+
508
+ static
509
+ __wasi_errno_t m3_wasi_host_fd_pread (__wasi_fd_t i_fd, const m3_wasi_iovec_t* i_iovs,
510
+ __wasi_size_t i_iovsLen, __wasi_filesize_t i_offset,
511
+ __wasi_size_t* o_done)
512
+ {
513
+ return transfer_at(i_fd, i_iovs, i_iovsLen, i_offset, false, o_done);
514
+ }
515
+
516
+ static
517
+ __wasi_errno_t m3_wasi_host_fd_pwrite (__wasi_fd_t i_fd, const m3_wasi_iovec_t* i_iovs,
518
+ __wasi_size_t i_iovsLen, __wasi_filesize_t i_offset,
519
+ __wasi_size_t* o_done)
520
+ {
521
+ return transfer_at(i_fd, i_iovs, i_iovsLen, i_offset, true, o_done);
522
+ }
523
+
524
+
525
+ /*
526
+ * Paths
527
+ */
528
+
529
+ static
530
+ __wasi_errno_t m3_wasi_host_path_open (__wasi_fd_t i_dirfd, ccstr_t i_path,
531
+ __wasi_oflags_t i_oflags, __wasi_rights_t i_rights,
532
+ __wasi_fdflags_t i_fdFlags, __wasi_fd_t* o_fd)
533
+ {
534
+ *o_fd = 0;
535
+
536
+ int dirfd = preopen_fd(i_dirfd);
537
+ if (dirfd < 0) {
538
+ return __WASI_ERRNO_BADF;
539
+ }
540
+
541
+ // translate o_flags and fs_flags into flags and mode
542
+ int flags = ((i_oflags & __WASI_OFLAGS_CREAT) ? O_CREAT : 0) |
543
+ //((i_oflags & __WASI_OFLAGS_DIRECTORY) ? O_DIRECTORY : 0) |
544
+ ((i_oflags & __WASI_OFLAGS_EXCL) ? O_EXCL : 0) |
545
+ ((i_oflags & __WASI_OFLAGS_TRUNC) ? O_TRUNC : 0) |
546
+ ((i_fdFlags & __WASI_FDFLAGS_APPEND) ? O_APPEND : 0) |
547
+ ((i_fdFlags & __WASI_FDFLAGS_DSYNC) ? O_DSYNC : 0) |
548
+ ((i_fdFlags & __WASI_FDFLAGS_NONBLOCK) ? O_NONBLOCK : 0) |
549
+ //((i_fdFlags & __WASI_FDFLAGS_RSYNC) ? O_RSYNC : 0) |
550
+ ((i_fdFlags & __WASI_FDFLAGS_SYNC) ? O_SYNC : 0);
551
+
552
+ if ((i_rights & __WASI_RIGHTS_FD_READ) && (i_rights & __WASI_RIGHTS_FD_WRITE)) {
553
+ flags |= O_RDWR;
554
+ } else if ((i_rights & __WASI_RIGHTS_FD_WRITE)) {
555
+ flags |= O_WRONLY;
556
+ } else if ((i_rights & __WASI_RIGHTS_FD_READ)) {
557
+ flags |= O_RDONLY; // no-op because O_RDONLY is 0
558
+ }
559
+
560
+ int host_fd = openat(dirfd, i_path, flags, 0644);
561
+ if (host_fd < 0) {
562
+ return errno_to_wasi(errno);
563
+ }
564
+
565
+ *o_fd = (__wasi_fd_t)host_fd;
566
+ return __WASI_ERRNO_SUCCESS;
567
+ }
568
+
569
+ static
570
+ __wasi_errno_t m3_wasi_host_path_filestat (__wasi_fd_t i_dirfd, ccstr_t i_path, bool i_follow,
571
+ m3_wasi_filestat_t* o_stat)
572
+ {
573
+ memset(o_stat, 0, sizeof(*o_stat));
574
+
575
+ int dirfd = preopen_fd(i_dirfd);
576
+ if (dirfd < 0) {
577
+ return __WASI_ERRNO_BADF;
578
+ }
579
+
580
+ struct stat path_stat;
581
+ if (fstatat(dirfd, i_path, &path_stat, i_follow ? 0 : AT_SYMLINK_NOFOLLOW) != 0) {
582
+ return errno_to_wasi(errno);
583
+ }
584
+
585
+ filestat_from_stat(&path_stat, o_stat);
586
+ return __WASI_ERRNO_SUCCESS;
587
+ }
588
+
589
+ static
590
+ __wasi_errno_t m3_wasi_host_path_create_directory (__wasi_fd_t i_dirfd, ccstr_t i_path)
591
+ {
592
+ int dirfd = preopen_fd(i_dirfd);
593
+ if (dirfd < 0) {
594
+ return __WASI_ERRNO_BADF;
595
+ }
596
+
597
+ if (mkdirat(dirfd, i_path, 0755) != 0) {
598
+ return errno_to_wasi(errno);
599
+ }
600
+ return __WASI_ERRNO_SUCCESS;
601
+ }
602
+
603
+ static
604
+ __wasi_errno_t m3_wasi_host_path_remove_directory (__wasi_fd_t i_dirfd, ccstr_t i_path)
605
+ {
606
+ int dirfd = preopen_fd(i_dirfd);
607
+ if (dirfd < 0) {
608
+ return __WASI_ERRNO_BADF;
609
+ }
610
+
611
+ if (unlinkat(dirfd, i_path, AT_REMOVEDIR) != 0) {
612
+ return errno_to_wasi(errno);
613
+ }
614
+ return __WASI_ERRNO_SUCCESS;
615
+ }
616
+
617
+ static
618
+ __wasi_errno_t m3_wasi_host_path_unlink_file (__wasi_fd_t i_dirfd, ccstr_t i_path)
619
+ {
620
+ int dirfd = preopen_fd(i_dirfd);
621
+ if (dirfd < 0) {
622
+ return __WASI_ERRNO_BADF;
623
+ }
624
+
625
+ if (unlinkat(dirfd, i_path, 0) != 0) {
626
+ return errno_to_wasi(errno);
627
+ }
628
+ return __WASI_ERRNO_SUCCESS;
629
+ }
630
+
631
+ static
632
+ __wasi_errno_t m3_wasi_host_path_rename (__wasi_fd_t i_oldDirfd, ccstr_t i_oldPath,
633
+ __wasi_fd_t i_newDirfd, ccstr_t i_newPath)
634
+ {
635
+ int oldDirfd = preopen_fd(i_oldDirfd);
636
+ int newDirfd = preopen_fd(i_newDirfd);
637
+ if (oldDirfd < 0 || newDirfd < 0) {
638
+ return __WASI_ERRNO_BADF;
639
+ }
640
+
641
+ if (renameat(oldDirfd, i_oldPath, newDirfd, i_newPath) != 0) {
642
+ return errno_to_wasi(errno);
643
+ }
644
+ return __WASI_ERRNO_SUCCESS;
645
+ }
646
+
647
+ static
648
+ __wasi_errno_t m3_wasi_host_path_link (__wasi_fd_t i_oldDirfd, ccstr_t i_oldPath, bool i_follow,
649
+ __wasi_fd_t i_newDirfd, ccstr_t i_newPath)
650
+ {
651
+ int oldDirfd = preopen_fd(i_oldDirfd);
652
+ int newDirfd = preopen_fd(i_newDirfd);
653
+ if (oldDirfd < 0 || newDirfd < 0) {
654
+ return __WASI_ERRNO_BADF;
655
+ }
656
+
657
+ if (linkat(oldDirfd, i_oldPath, newDirfd, i_newPath, i_follow ? AT_SYMLINK_FOLLOW : 0) != 0) {
658
+ return errno_to_wasi(errno);
659
+ }
660
+ return __WASI_ERRNO_SUCCESS;
661
+ }
662
+
663
+ static
664
+ __wasi_errno_t m3_wasi_host_path_symlink (ccstr_t i_target, __wasi_fd_t i_dirfd, ccstr_t i_path)
665
+ {
666
+ int dirfd = preopen_fd(i_dirfd);
667
+ if (dirfd < 0) {
668
+ return __WASI_ERRNO_BADF;
669
+ }
670
+
671
+ if (symlinkat(i_target, dirfd, i_path) != 0) {
672
+ return errno_to_wasi(errno);
673
+ }
674
+ return __WASI_ERRNO_SUCCESS;
675
+ }
676
+
677
+ static
678
+ __wasi_errno_t m3_wasi_host_path_readlink (__wasi_fd_t i_dirfd, ccstr_t i_path, char* o_buf,
679
+ __wasi_size_t i_bufLen, __wasi_size_t* o_bufUsed)
680
+ {
681
+ *o_bufUsed = 0;
682
+
683
+ int dirfd = preopen_fd(i_dirfd);
684
+ if (dirfd < 0) {
685
+ return __WASI_ERRNO_BADF;
686
+ }
687
+
688
+ ssize_t ret = readlinkat(dirfd, i_path, o_buf, i_bufLen);
689
+ if (ret < 0) {
690
+ return errno_to_wasi(errno);
691
+ }
692
+
693
+ *o_bufUsed = (__wasi_size_t)ret;
694
+ return __WASI_ERRNO_SUCCESS;
695
+ }
696
+
697
+
698
+ /*
699
+ * Clocks and entropy
700
+ */
701
+
702
+ static
703
+ __wasi_errno_t m3_wasi_host_clock_res_get (__wasi_clockid_t i_clockId, __wasi_timestamp_t* o_res)
704
+ {
705
+ *o_res = 0;
706
+
707
+ clockid_t clk = convert_clockid(i_clockId);
708
+ if (clk == d_m3ClockIdInvalid) {
709
+ return __WASI_ERRNO_INVAL;
710
+ }
711
+
712
+ struct timespec tp;
713
+ if (clock_getres(clk, &tp) != 0) {
714
+ *o_res = 1000000;
715
+ } else {
716
+ *o_res = convert_timespec(&tp);
717
+ }
718
+ return __WASI_ERRNO_SUCCESS;
719
+ }
720
+
721
+ static
722
+ __wasi_errno_t m3_wasi_host_clock_time_get (__wasi_clockid_t i_clockId, __wasi_timestamp_t* o_time)
723
+ {
724
+ *o_time = 0;
725
+
726
+ clockid_t clk = convert_clockid(i_clockId);
727
+ if (clk == d_m3ClockIdInvalid) {
728
+ return __WASI_ERRNO_INVAL;
729
+ }
730
+
731
+ struct timespec tp;
732
+ if (clock_gettime(clk, &tp) != 0) {
733
+ return errno_to_wasi(errno);
734
+ }
735
+
736
+ *o_time = convert_timespec(&tp);
737
+ return __WASI_ERRNO_SUCCESS;
738
+ }
739
+
740
+ static
741
+ __wasi_errno_t m3_wasi_host_random_get (void* o_buf, __wasi_size_t i_len)
742
+ {
743
+ uint8_t* buf = (uint8_t*)o_buf;
744
+
745
+ while (i_len) {
746
+ ssize_t retlen;
747
+
748
+ #if defined(__wasi__) || defined(__APPLE__) || defined(__ANDROID_API__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__)
749
+ size_t reqlen = M3_MIN(i_len, 256);
750
+ # if defined(__APPLE__) && (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
751
+ retlen = SecRandomCopyBytes(kSecRandomDefault, reqlen, buf) < 0 ? -1 : (ssize_t)reqlen;
752
+ # else
753
+ retlen = getentropy(buf, reqlen) < 0 ? -1 : (ssize_t)reqlen;
754
+ # endif
755
+ #elif defined(__FreeBSD__) || defined(__linux__)
756
+ retlen = getrandom(buf, i_len, 0);
757
+ #else
758
+ return __WASI_ERRNO_NOSYS;
759
+ #endif
760
+
761
+ if (retlen < 0) {
762
+ if (errno == EINTR || errno == EAGAIN) {
763
+ continue;
764
+ }
765
+ return errno_to_wasi(errno);
766
+ }
767
+
768
+ buf += retlen;
769
+ i_len -= (__wasi_size_t)retlen;
770
+ }
771
+
772
+ return __WASI_ERRNO_SUCCESS;
773
+ }
774
+
775
+
776
+ /*
777
+ * Waiting
778
+ */
779
+
780
+ // How much longer a clock subscription has to wait, in nanoseconds, given that
781
+ // i_elapsedNs of this call have already gone by - so 0 means it is ready. A relative
782
+ // subscription counts down from the timeout it named; an absolute one is held
783
+ // against the clock it names and ignores i_elapsedNs, that clock having moved on by
784
+ // itself. A clock this system has not got answers UINT64_MAX so that it never
785
+ // becomes the soonest deadline - the caller reports EINVAL for it separately.
786
+ static
787
+ uint64_t poll_remaining_ns (const m3_wasi_pollsub_t* i_sub, uint64_t i_elapsedNs)
788
+ {
789
+ if (not i_sub->isAbsolute) {
790
+ return (i_sub->timeout > i_elapsedNs) ? i_sub->timeout - i_elapsedNs : 0;
791
+ }
792
+
793
+ clockid_t clk = convert_clockid(i_sub->clockId);
794
+ if (clk == d_m3ClockIdInvalid) {
795
+ return UINT64_MAX;
796
+ }
797
+
798
+ struct timespec tp;
799
+ if (clock_gettime(clk, &tp) != 0) {
800
+ return 0;
801
+ }
802
+
803
+ __wasi_timestamp_t now = convert_timespec(&tp);
804
+ return (i_sub->timeout > now) ? i_sub->timeout - now : 0;
805
+ }
806
+
807
+ // How much a descriptor ready for reading has waiting on it. The guest is told a
808
+ // count so that it can size the read that follows, and a wrong one only costs it a
809
+ // short read - so a descriptor that cannot answer says one byte rather than nothing,
810
+ // which would read as end of file.
811
+ static
812
+ __wasi_filesize_t poll_readable_bytes (int i_fd)
813
+ {
814
+ #if defined(FIONREAD)
815
+ int available = 0;
816
+ if (ioctl(i_fd, FIONREAD, &available) == 0 and available > 0) {
817
+ return (__wasi_filesize_t)available;
818
+ }
819
+ #endif
820
+ return 1;
821
+ }
822
+
823
+ static
824
+ __wasi_errno_t m3_wasi_host_poll_oneoff (const m3_wasi_pollsub_t* i_subs, __wasi_size_t i_count,
825
+ m3_wasi_pollevent_t* o_events, __wasi_size_t* o_eventCount)
826
+ {
827
+ *o_eventCount = 0;
828
+
829
+ // The soonest clock deadline is what bounds the wait; without one, poll() is
830
+ // told to wait forever, which is what a subscription list of descriptors alone
831
+ // asks for. A clock this system has not got sets no deadline but is still a
832
+ // subscription, and is reported below as one event carrying EINVAL.
833
+ uint64_t soonest = UINT64_MAX;
834
+ bool hasDeadline = false;
835
+ bool hasClock = false;
836
+
837
+ __wasi_size_t numFds = 0;
838
+
839
+ for (__wasi_size_t i = 0; i < i_count; i++) {
840
+ if (i_subs[i].type == __WASI_EVENTTYPE_CLOCK) {
841
+ hasClock = true;
842
+
843
+ uint64_t delay = poll_remaining_ns(&i_subs[i], 0);
844
+ if (delay != UINT64_MAX) {
845
+ soonest = M3_MIN(soonest, delay);
846
+ hasDeadline = true;
847
+ }
848
+ } else {
849
+ numFds++;
850
+ }
851
+ }
852
+
853
+ struct pollfd* pollfds = NULL;
854
+
855
+ if (numFds) {
856
+ pollfds = (struct pollfd*)m3_Malloc("WASI poll_oneoff", numFds * sizeof(struct pollfd));
857
+ if (pollfds == NULL) {
858
+ return __WASI_ERRNO_NOMEM;
859
+ }
860
+
861
+ __wasi_size_t n = 0;
862
+ for (__wasi_size_t i = 0; i < i_count; i++) {
863
+ if (i_subs[i].type == __WASI_EVENTTYPE_CLOCK) {
864
+ continue;
865
+ }
866
+ pollfds[n].fd = (int)i_subs[i].fd;
867
+ pollfds[n].events = (i_subs[i].type == __WASI_EVENTTYPE_FD_READ) ? POLLIN : POLLOUT;
868
+ pollfds[n].revents = 0;
869
+ n++;
870
+ }
871
+ }
872
+
873
+ // poll() counts in milliseconds and cannot say "no wait at all" apart from a
874
+ // wait that rounds down to nothing, which is the same thing here. Rounding up
875
+ // is what keeps a sub-millisecond deadline from becoming a spin.
876
+ int timeoutMs = -1;
877
+ if (hasDeadline) {
878
+ uint64_t ms = (soonest + 999999) / 1000000;
879
+ timeoutMs = (ms > (uint64_t)INT_MAX) ? INT_MAX : (int)ms;
880
+ }
881
+
882
+ int ready;
883
+ if (numFds) {
884
+ do {
885
+ ready = poll(pollfds, (nfds_t)numFds, timeoutMs);
886
+ } while (ready < 0 and errno == EINTR);
887
+ } else if (hasDeadline) {
888
+ // nothing to watch, so the deadline is the whole of the wait
889
+ struct timespec req;
890
+ req.tv_sec = (time_t)(soonest / 1000000000);
891
+ req.tv_nsec = (long)(soonest % 1000000000);
892
+
893
+ while (nanosleep(&req, &req) != 0 and errno == EINTR) {
894
+ // interrupted: req holds what is left of it
895
+ }
896
+ ready = 0;
897
+ } else if (hasClock) {
898
+ // every clock named is one this system has not got, so there is nothing to
899
+ // wait for and every one of them is about to be reported as a bad argument
900
+ ready = 0;
901
+ } else {
902
+ // no clocks and no descriptors: nothing that could ever become ready
903
+ return __WASI_ERRNO_INVAL;
904
+ }
905
+
906
+ if (ready < 0) {
907
+ __wasi_errno_t err = errno_to_wasi(errno);
908
+ m3_Free(pollfds);
909
+ return err;
910
+ }
911
+
912
+ // How much of the wait actually happened. poll() answers 0 only when it waited
913
+ // the whole timeout out, and that timeout was the soonest deadline; anything
914
+ // else came back on a descriptor, which by definition beat every deadline. So
915
+ // the two cases are the full wait and none of it, and neither needs the clock
916
+ // read again - an absolute deadline is held against its own clock regardless.
917
+ uint64_t elapsedNs = (ready == 0 and hasDeadline) ? soonest : 0;
918
+
919
+ __wasi_size_t out = 0;
920
+ __wasi_size_t n = 0;
921
+
922
+ for (__wasi_size_t i = 0; i < i_count; i++) {
923
+ const m3_wasi_pollsub_t* sub = &i_subs[i];
924
+
925
+ if (sub->type == __WASI_EVENTTYPE_CLOCK) {
926
+ // Every deadline is asked again rather than just the soonest: waiting
927
+ // that one out may have passed a later one too, and an absolute
928
+ // deadline already in the past fires without any wait at all.
929
+ if (convert_clockid(sub->clockId) == d_m3ClockIdInvalid) {
930
+ o_events[out].userdata = sub->userdata;
931
+ o_events[out].error = __WASI_ERRNO_INVAL;
932
+ o_events[out].type = __WASI_EVENTTYPE_CLOCK;
933
+ o_events[out].nbytes = 0;
934
+ o_events[out].flags = 0;
935
+ out++;
936
+ } else if (poll_remaining_ns(sub, elapsedNs) == 0) {
937
+ o_events[out].userdata = sub->userdata;
938
+ o_events[out].error = __WASI_ERRNO_SUCCESS;
939
+ o_events[out].type = __WASI_EVENTTYPE_CLOCK;
940
+ o_events[out].nbytes = 0;
941
+ o_events[out].flags = 0;
942
+ out++;
943
+ }
944
+ continue;
945
+ }
946
+
947
+ short revents = pollfds[n].revents;
948
+ n++;
949
+
950
+ if (revents == 0) {
951
+ continue;
952
+ }
953
+
954
+ o_events[out].userdata = sub->userdata;
955
+ o_events[out].type = sub->type;
956
+ o_events[out].nbytes = 0;
957
+ o_events[out].flags = (revents & POLLHUP) ? __WASI_EVENTRWFLAGS_FD_READWRITE_HANGUP : 0;
958
+
959
+ if (revents & POLLNVAL) {
960
+ o_events[out].error = __WASI_ERRNO_BADF;
961
+ } else if (revents & POLLERR) {
962
+ o_events[out].error = __WASI_ERRNO_IO;
963
+ } else {
964
+ o_events[out].error = __WASI_ERRNO_SUCCESS;
965
+ if (sub->type == __WASI_EVENTTYPE_FD_READ and (revents & POLLIN)) {
966
+ o_events[out].nbytes = poll_readable_bytes(pollfds[n - 1].fd);
967
+ }
968
+ }
969
+
970
+ out++;
971
+ }
972
+
973
+ m3_Free(pollfds);
974
+
975
+ *o_eventCount = out;
976
+ return __WASI_ERRNO_SUCCESS;
977
+ }
978
+
979
+ #endif // m3_api_wasi_posix_h