libc_wasi_wrapper.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "libc_wasi_wrapper.h"
  6. #include "bh_platform.h"
  7. #include "wasm_export.h"
  8. void
  9. wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
  10. /* clang-format off */
  11. #define get_module_inst(exec_env) \
  12. wasm_runtime_get_module_inst(exec_env)
  13. #define get_wasi_ctx(module_inst) \
  14. wasm_runtime_get_wasi_ctx(module_inst)
  15. #define validate_app_addr(offset, size) \
  16. wasm_runtime_validate_app_addr(module_inst, offset, size)
  17. #define validate_native_addr(addr, size) \
  18. wasm_runtime_validate_native_addr(module_inst, addr, size)
  19. #define addr_app_to_native(offset) \
  20. wasm_runtime_addr_app_to_native(module_inst, offset)
  21. #define addr_native_to_app(ptr) \
  22. wasm_runtime_addr_native_to_app(module_inst, ptr)
  23. #define module_malloc(size, p_native_addr) \
  24. wasm_runtime_module_malloc(module_inst, size, p_native_addr)
  25. #define module_free(offset) \
  26. wasm_runtime_module_free(module_inst, offset)
  27. /* clang-format on */
  28. typedef struct wasi_prestat_app {
  29. wasi_preopentype_t pr_type;
  30. uint32 pr_name_len;
  31. } wasi_prestat_app_t;
  32. typedef struct iovec_app {
  33. uint32 buf_offset;
  34. uint32 buf_len;
  35. } iovec_app_t;
  36. typedef struct WASIContext {
  37. struct fd_table *curfds;
  38. struct fd_prestats *prestats;
  39. struct argv_environ_values *argv_environ;
  40. char *argv_buf;
  41. char **argv_list;
  42. char *env_buf;
  43. char **env_list;
  44. } * wasi_ctx_t;
  45. wasi_ctx_t
  46. wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst);
  47. static inline struct fd_table *
  48. wasi_ctx_get_curfds(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
  49. {
  50. if (!wasi_ctx)
  51. return NULL;
  52. return wasi_ctx->curfds;
  53. }
  54. static inline struct argv_environ_values *
  55. wasi_ctx_get_argv_environ(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
  56. {
  57. if (!wasi_ctx)
  58. return NULL;
  59. return wasi_ctx->argv_environ;
  60. }
  61. static inline struct fd_prestats *
  62. wasi_ctx_get_prestats(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
  63. {
  64. if (!wasi_ctx)
  65. return NULL;
  66. return wasi_ctx->prestats;
  67. }
  68. static wasi_errno_t
  69. wasi_args_get(wasm_exec_env_t exec_env, uint32 *argv_offsets, char *argv_buf)
  70. {
  71. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  72. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  73. struct argv_environ_values *argv_environ =
  74. wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
  75. size_t argc, argv_buf_size, i;
  76. char **argv;
  77. uint64 total_size;
  78. wasi_errno_t err;
  79. if (!wasi_ctx)
  80. return (wasi_errno_t)-1;
  81. err = wasmtime_ssp_args_sizes_get(argv_environ, &argc, &argv_buf_size);
  82. if (err)
  83. return err;
  84. total_size = sizeof(int32) * ((uint64)argc + 1);
  85. if (total_size >= UINT32_MAX
  86. || !validate_native_addr(argv_offsets, (uint32)total_size)
  87. || argv_buf_size >= UINT32_MAX
  88. || !validate_native_addr(argv_buf, (uint32)argv_buf_size))
  89. return (wasi_errno_t)-1;
  90. total_size = sizeof(char *) * ((uint64)argc + 1);
  91. if (total_size >= UINT32_MAX
  92. || !(argv = wasm_runtime_malloc((uint32)total_size)))
  93. return (wasi_errno_t)-1;
  94. err = wasmtime_ssp_args_get(argv_environ, argv, argv_buf);
  95. if (err) {
  96. wasm_runtime_free(argv);
  97. return err;
  98. }
  99. for (i = 0; i < argc; i++)
  100. argv_offsets[i] = addr_native_to_app(argv[i]);
  101. argv_offsets[argc] = 0;
  102. wasm_runtime_free(argv);
  103. return 0;
  104. }
  105. static wasi_errno_t
  106. wasi_args_sizes_get(wasm_exec_env_t exec_env, uint32 *argc_app,
  107. uint32 *argv_buf_size_app)
  108. {
  109. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  110. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  111. struct argv_environ_values *argv_environ;
  112. size_t argc, argv_buf_size;
  113. wasi_errno_t err;
  114. if (!wasi_ctx)
  115. return (wasi_errno_t)-1;
  116. if (!validate_native_addr(argc_app, sizeof(uint32))
  117. || !validate_native_addr(argv_buf_size_app, sizeof(uint32)))
  118. return (wasi_errno_t)-1;
  119. argv_environ = wasi_ctx->argv_environ;
  120. err = wasmtime_ssp_args_sizes_get(argv_environ, &argc, &argv_buf_size);
  121. if (err)
  122. return err;
  123. *argc_app = (uint32)argc;
  124. *argv_buf_size_app = (uint32)argv_buf_size;
  125. return 0;
  126. }
  127. static wasi_errno_t
  128. wasi_clock_res_get(wasm_exec_env_t exec_env,
  129. wasi_clockid_t clock_id, /* uint32 clock_id */
  130. wasi_timestamp_t *resolution /* uint64 *resolution */)
  131. {
  132. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  133. if (!validate_native_addr(resolution, sizeof(wasi_timestamp_t)))
  134. return (wasi_errno_t)-1;
  135. return wasmtime_ssp_clock_res_get(clock_id, resolution);
  136. }
  137. static wasi_errno_t
  138. wasi_clock_time_get(wasm_exec_env_t exec_env,
  139. wasi_clockid_t clock_id, /* uint32 clock_id */
  140. wasi_timestamp_t precision, /* uint64 precision */
  141. wasi_timestamp_t *time /* uint64 *time */)
  142. {
  143. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  144. if (!validate_native_addr(time, sizeof(wasi_timestamp_t)))
  145. return (wasi_errno_t)-1;
  146. return wasmtime_ssp_clock_time_get(clock_id, precision, time);
  147. }
  148. static wasi_errno_t
  149. wasi_environ_get(wasm_exec_env_t exec_env, uint32 *environ_offsets,
  150. char *environ_buf)
  151. {
  152. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  153. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  154. struct argv_environ_values *argv_environ =
  155. wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
  156. size_t environ_count, environ_buf_size, i;
  157. uint64 total_size;
  158. char **environs;
  159. wasi_errno_t err;
  160. if (!wasi_ctx)
  161. return (wasi_errno_t)-1;
  162. err = wasmtime_ssp_environ_sizes_get(argv_environ, &environ_count,
  163. &environ_buf_size);
  164. if (err)
  165. return err;
  166. total_size = sizeof(int32) * ((uint64)environ_count + 1);
  167. if (total_size >= UINT32_MAX
  168. || !validate_native_addr(environ_offsets, (uint32)total_size)
  169. || environ_buf_size >= UINT32_MAX
  170. || !validate_native_addr(environ_buf, (uint32)environ_buf_size))
  171. return (wasi_errno_t)-1;
  172. total_size = sizeof(char *) * (((uint64)environ_count + 1));
  173. if (total_size >= UINT32_MAX
  174. || !(environs = wasm_runtime_malloc((uint32)total_size)))
  175. return (wasi_errno_t)-1;
  176. err = wasmtime_ssp_environ_get(argv_environ, environs, environ_buf);
  177. if (err) {
  178. wasm_runtime_free(environs);
  179. return err;
  180. }
  181. for (i = 0; i < environ_count; i++)
  182. environ_offsets[i] = addr_native_to_app(environs[i]);
  183. environ_offsets[environ_count] = 0;
  184. wasm_runtime_free(environs);
  185. return 0;
  186. }
  187. static wasi_errno_t
  188. wasi_environ_sizes_get(wasm_exec_env_t exec_env, uint32 *environ_count_app,
  189. uint32 *environ_buf_size_app)
  190. {
  191. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  192. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  193. struct argv_environ_values *argv_environ =
  194. wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
  195. size_t environ_count, environ_buf_size;
  196. wasi_errno_t err;
  197. if (!wasi_ctx)
  198. return (wasi_errno_t)-1;
  199. if (!validate_native_addr(environ_count_app, sizeof(uint32))
  200. || !validate_native_addr(environ_buf_size_app, sizeof(uint32)))
  201. return (wasi_errno_t)-1;
  202. err = wasmtime_ssp_environ_sizes_get(argv_environ, &environ_count,
  203. &environ_buf_size);
  204. if (err)
  205. return err;
  206. *environ_count_app = (uint32)environ_count;
  207. *environ_buf_size_app = (uint32)environ_buf_size;
  208. return 0;
  209. }
  210. static wasi_errno_t
  211. wasi_fd_prestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
  212. wasi_prestat_app_t *prestat_app)
  213. {
  214. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  215. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  216. struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
  217. wasi_prestat_t prestat;
  218. wasi_errno_t err;
  219. if (!wasi_ctx)
  220. return (wasi_errno_t)-1;
  221. if (!validate_native_addr(prestat_app, sizeof(wasi_prestat_app_t)))
  222. return (wasi_errno_t)-1;
  223. err = wasmtime_ssp_fd_prestat_get(prestats, fd, &prestat);
  224. if (err)
  225. return err;
  226. prestat_app->pr_type = prestat.pr_type;
  227. prestat_app->pr_name_len = (uint32)prestat.u.dir.pr_name_len;
  228. return 0;
  229. }
  230. static wasi_errno_t
  231. wasi_fd_prestat_dir_name(wasm_exec_env_t exec_env, wasi_fd_t fd, char *path,
  232. uint32 path_len)
  233. {
  234. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  235. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  236. struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
  237. if (!wasi_ctx)
  238. return (wasi_errno_t)-1;
  239. return wasmtime_ssp_fd_prestat_dir_name(prestats, fd, path, path_len);
  240. }
  241. static wasi_errno_t
  242. wasi_fd_close(wasm_exec_env_t exec_env, wasi_fd_t fd)
  243. {
  244. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  245. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  246. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  247. struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
  248. if (!wasi_ctx)
  249. return (wasi_errno_t)-1;
  250. return wasmtime_ssp_fd_close(curfds, prestats, fd);
  251. }
  252. static wasi_errno_t
  253. wasi_fd_datasync(wasm_exec_env_t exec_env, wasi_fd_t fd)
  254. {
  255. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  256. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  257. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  258. if (!wasi_ctx)
  259. return (wasi_errno_t)-1;
  260. return wasmtime_ssp_fd_datasync(curfds, fd);
  261. }
  262. static wasi_errno_t
  263. wasi_fd_pread(wasm_exec_env_t exec_env, wasi_fd_t fd, iovec_app_t *iovec_app,
  264. uint32 iovs_len, wasi_filesize_t offset, uint32 *nread_app)
  265. {
  266. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  267. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  268. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  269. wasi_iovec_t *iovec, *iovec_begin;
  270. uint64 total_size;
  271. size_t nread;
  272. uint32 i;
  273. wasi_errno_t err;
  274. if (!wasi_ctx)
  275. return (wasi_errno_t)-1;
  276. total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
  277. if (!validate_native_addr(nread_app, (uint32)sizeof(uint32))
  278. || total_size >= UINT32_MAX
  279. || !validate_native_addr(iovec_app, (uint32)total_size))
  280. return (wasi_errno_t)-1;
  281. total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
  282. if (total_size >= UINT32_MAX
  283. || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
  284. return (wasi_errno_t)-1;
  285. iovec = iovec_begin;
  286. for (i = 0; i < iovs_len; i++, iovec_app++, iovec++) {
  287. if (!validate_app_addr(iovec_app->buf_offset, iovec_app->buf_len)) {
  288. err = (wasi_errno_t)-1;
  289. goto fail;
  290. }
  291. iovec->buf = (void *)addr_app_to_native(iovec_app->buf_offset);
  292. iovec->buf_len = iovec_app->buf_len;
  293. }
  294. err = wasmtime_ssp_fd_pread(curfds, fd, iovec_begin, iovs_len, offset,
  295. &nread);
  296. if (err)
  297. goto fail;
  298. *nread_app = (uint32)nread;
  299. /* success */
  300. err = 0;
  301. fail:
  302. wasm_runtime_free(iovec_begin);
  303. return err;
  304. }
  305. static wasi_errno_t
  306. wasi_fd_pwrite(wasm_exec_env_t exec_env, wasi_fd_t fd,
  307. const iovec_app_t *iovec_app, uint32 iovs_len,
  308. wasi_filesize_t offset, uint32 *nwritten_app)
  309. {
  310. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  311. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  312. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  313. wasi_ciovec_t *ciovec, *ciovec_begin;
  314. uint64 total_size;
  315. size_t nwritten;
  316. uint32 i;
  317. wasi_errno_t err;
  318. if (!wasi_ctx)
  319. return (wasi_errno_t)-1;
  320. total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
  321. if (!validate_native_addr(nwritten_app, (uint32)sizeof(uint32))
  322. || total_size >= UINT32_MAX
  323. || !validate_native_addr((void *)iovec_app, (uint32)total_size))
  324. return (wasi_errno_t)-1;
  325. total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
  326. if (total_size >= UINT32_MAX
  327. || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
  328. return (wasi_errno_t)-1;
  329. ciovec = ciovec_begin;
  330. for (i = 0; i < iovs_len; i++, iovec_app++, ciovec++) {
  331. if (!validate_app_addr(iovec_app->buf_offset, iovec_app->buf_len)) {
  332. err = (wasi_errno_t)-1;
  333. goto fail;
  334. }
  335. ciovec->buf = (char *)addr_app_to_native(iovec_app->buf_offset);
  336. ciovec->buf_len = iovec_app->buf_len;
  337. }
  338. err = wasmtime_ssp_fd_pwrite(curfds, fd, ciovec_begin, iovs_len, offset,
  339. &nwritten);
  340. if (err)
  341. goto fail;
  342. *nwritten_app = (uint32)nwritten;
  343. /* success */
  344. err = 0;
  345. fail:
  346. wasm_runtime_free(ciovec_begin);
  347. return err;
  348. }
  349. static wasi_errno_t
  350. wasi_fd_read(wasm_exec_env_t exec_env, wasi_fd_t fd,
  351. const iovec_app_t *iovec_app, uint32 iovs_len, uint32 *nread_app)
  352. {
  353. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  354. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  355. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  356. wasi_iovec_t *iovec, *iovec_begin;
  357. uint64 total_size;
  358. size_t nread;
  359. uint32 i;
  360. wasi_errno_t err;
  361. if (!wasi_ctx)
  362. return (wasi_errno_t)-1;
  363. total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
  364. if (!validate_native_addr(nread_app, (uint32)sizeof(uint32))
  365. || total_size >= UINT32_MAX
  366. || !validate_native_addr((void *)iovec_app, (uint32)total_size))
  367. return (wasi_errno_t)-1;
  368. total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
  369. if (total_size >= UINT32_MAX
  370. || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
  371. return (wasi_errno_t)-1;
  372. iovec = iovec_begin;
  373. for (i = 0; i < iovs_len; i++, iovec_app++, iovec++) {
  374. if (!validate_app_addr(iovec_app->buf_offset, iovec_app->buf_len)) {
  375. err = (wasi_errno_t)-1;
  376. goto fail;
  377. }
  378. iovec->buf = (void *)addr_app_to_native(iovec_app->buf_offset);
  379. iovec->buf_len = iovec_app->buf_len;
  380. }
  381. err = wasmtime_ssp_fd_read(curfds, fd, iovec_begin, iovs_len, &nread);
  382. if (err)
  383. goto fail;
  384. *nread_app = (uint32)nread;
  385. /* success */
  386. err = 0;
  387. fail:
  388. wasm_runtime_free(iovec_begin);
  389. return err;
  390. }
  391. static wasi_errno_t
  392. wasi_fd_renumber(wasm_exec_env_t exec_env, wasi_fd_t from, wasi_fd_t to)
  393. {
  394. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  395. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  396. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  397. struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
  398. if (!wasi_ctx)
  399. return (wasi_errno_t)-1;
  400. return wasmtime_ssp_fd_renumber(curfds, prestats, from, to);
  401. }
  402. static wasi_errno_t
  403. wasi_fd_seek(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filedelta_t offset,
  404. wasi_whence_t whence, wasi_filesize_t *newoffset)
  405. {
  406. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  407. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  408. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  409. if (!wasi_ctx)
  410. return (wasi_errno_t)-1;
  411. if (!validate_native_addr(newoffset, sizeof(wasi_filesize_t)))
  412. return (wasi_errno_t)-1;
  413. return wasmtime_ssp_fd_seek(curfds, fd, offset, whence, newoffset);
  414. }
  415. static wasi_errno_t
  416. wasi_fd_tell(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t *newoffset)
  417. {
  418. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  419. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  420. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  421. if (!wasi_ctx)
  422. return (wasi_errno_t)-1;
  423. if (!validate_native_addr(newoffset, sizeof(wasi_filesize_t)))
  424. return (wasi_errno_t)-1;
  425. return wasmtime_ssp_fd_tell(curfds, fd, newoffset);
  426. }
  427. static wasi_errno_t
  428. wasi_fd_fdstat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
  429. wasi_fdstat_t *fdstat_app)
  430. {
  431. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  432. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  433. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  434. wasi_fdstat_t fdstat;
  435. wasi_errno_t err;
  436. if (!wasi_ctx)
  437. return (wasi_errno_t)-1;
  438. if (!validate_native_addr(fdstat_app, sizeof(wasi_fdstat_t)))
  439. return (wasi_errno_t)-1;
  440. err = wasmtime_ssp_fd_fdstat_get(curfds, fd, &fdstat);
  441. if (err)
  442. return err;
  443. memcpy(fdstat_app, &fdstat, sizeof(wasi_fdstat_t));
  444. return 0;
  445. }
  446. static wasi_errno_t
  447. wasi_fd_fdstat_set_flags(wasm_exec_env_t exec_env, wasi_fd_t fd,
  448. wasi_fdflags_t flags)
  449. {
  450. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  451. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  452. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  453. if (!wasi_ctx)
  454. return (wasi_errno_t)-1;
  455. return wasmtime_ssp_fd_fdstat_set_flags(curfds, fd, flags);
  456. }
  457. static wasi_errno_t
  458. wasi_fd_fdstat_set_rights(wasm_exec_env_t exec_env, wasi_fd_t fd,
  459. wasi_rights_t fs_rights_base,
  460. wasi_rights_t fs_rights_inheriting)
  461. {
  462. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  463. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  464. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  465. if (!wasi_ctx)
  466. return (wasi_errno_t)-1;
  467. return wasmtime_ssp_fd_fdstat_set_rights(curfds, fd, fs_rights_base,
  468. fs_rights_inheriting);
  469. }
  470. static wasi_errno_t
  471. wasi_fd_sync(wasm_exec_env_t exec_env, wasi_fd_t fd)
  472. {
  473. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  474. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  475. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  476. if (!wasi_ctx)
  477. return (wasi_errno_t)-1;
  478. return wasmtime_ssp_fd_sync(curfds, fd);
  479. }
  480. static wasi_errno_t
  481. wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
  482. const iovec_app_t *iovec_app, uint32 iovs_len,
  483. uint32 *nwritten_app)
  484. {
  485. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  486. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  487. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  488. wasi_ciovec_t *ciovec, *ciovec_begin;
  489. uint64 total_size;
  490. size_t nwritten;
  491. uint32 i;
  492. wasi_errno_t err;
  493. if (!wasi_ctx)
  494. return (wasi_errno_t)-1;
  495. total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
  496. if (!validate_native_addr(nwritten_app, (uint32)sizeof(uint32))
  497. || total_size >= UINT32_MAX
  498. || !validate_native_addr((void *)iovec_app, (uint32)total_size))
  499. return (wasi_errno_t)-1;
  500. total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
  501. if (total_size >= UINT32_MAX
  502. || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
  503. return (wasi_errno_t)-1;
  504. ciovec = ciovec_begin;
  505. for (i = 0; i < iovs_len; i++, iovec_app++, ciovec++) {
  506. if (!validate_app_addr(iovec_app->buf_offset, iovec_app->buf_len)) {
  507. err = (wasi_errno_t)-1;
  508. goto fail;
  509. }
  510. ciovec->buf = (char *)addr_app_to_native(iovec_app->buf_offset);
  511. ciovec->buf_len = iovec_app->buf_len;
  512. }
  513. err = wasmtime_ssp_fd_write(curfds, fd, ciovec_begin, iovs_len, &nwritten);
  514. if (err)
  515. goto fail;
  516. *nwritten_app = (uint32)nwritten;
  517. /* success */
  518. err = 0;
  519. fail:
  520. wasm_runtime_free(ciovec_begin);
  521. return err;
  522. }
  523. static wasi_errno_t
  524. wasi_fd_advise(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t offset,
  525. wasi_filesize_t len, wasi_advice_t advice)
  526. {
  527. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  528. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  529. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  530. if (!wasi_ctx)
  531. return (wasi_errno_t)-1;
  532. return wasmtime_ssp_fd_advise(curfds, fd, offset, len, advice);
  533. }
  534. static wasi_errno_t
  535. wasi_fd_allocate(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t offset,
  536. wasi_filesize_t len)
  537. {
  538. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  539. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  540. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  541. if (!wasi_ctx)
  542. return (wasi_errno_t)-1;
  543. return wasmtime_ssp_fd_allocate(curfds, fd, offset, len);
  544. }
  545. static wasi_errno_t
  546. wasi_path_create_directory(wasm_exec_env_t exec_env, wasi_fd_t fd,
  547. const char *path, uint32 path_len)
  548. {
  549. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  550. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  551. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  552. if (!wasi_ctx)
  553. return (wasi_errno_t)-1;
  554. return wasmtime_ssp_path_create_directory(curfds, fd, path, path_len);
  555. }
  556. static wasi_errno_t
  557. wasi_path_link(wasm_exec_env_t exec_env, wasi_fd_t old_fd,
  558. wasi_lookupflags_t old_flags, const char *old_path,
  559. uint32 old_path_len, wasi_fd_t new_fd, const char *new_path,
  560. uint32 new_path_len)
  561. {
  562. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  563. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  564. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  565. struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
  566. if (!wasi_ctx)
  567. return (wasi_errno_t)-1;
  568. return wasmtime_ssp_path_link(curfds, prestats, old_fd, old_flags, old_path,
  569. old_path_len, new_fd, new_path, new_path_len);
  570. }
  571. static wasi_errno_t
  572. wasi_path_open(wasm_exec_env_t exec_env, wasi_fd_t dirfd,
  573. wasi_lookupflags_t dirflags, const char *path, uint32 path_len,
  574. wasi_oflags_t oflags, wasi_rights_t fs_rights_base,
  575. wasi_rights_t fs_rights_inheriting, wasi_fdflags_t fs_flags,
  576. wasi_fd_t *fd_app)
  577. {
  578. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  579. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  580. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  581. wasi_fd_t fd = (wasi_fd_t)-1; /* set fd_app -1 if path open failed */
  582. wasi_errno_t err;
  583. if (!wasi_ctx)
  584. return (wasi_errno_t)-1;
  585. if (!validate_native_addr(fd_app, sizeof(wasi_fd_t)))
  586. return (wasi_errno_t)-1;
  587. err = wasmtime_ssp_path_open(curfds, dirfd, dirflags, path, path_len,
  588. oflags, fs_rights_base, fs_rights_inheriting,
  589. fs_flags, &fd);
  590. *fd_app = fd;
  591. return err;
  592. }
  593. static wasi_errno_t
  594. wasi_fd_readdir(wasm_exec_env_t exec_env, wasi_fd_t fd, void *buf,
  595. uint32 buf_len, wasi_dircookie_t cookie, uint32 *bufused_app)
  596. {
  597. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  598. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  599. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  600. size_t bufused;
  601. wasi_errno_t err;
  602. if (!wasi_ctx)
  603. return (wasi_errno_t)-1;
  604. if (!validate_native_addr(bufused_app, sizeof(uint32)))
  605. return (wasi_errno_t)-1;
  606. err = wasmtime_ssp_fd_readdir(curfds, fd, buf, buf_len, cookie, &bufused);
  607. if (err)
  608. return err;
  609. *bufused_app = (uint32)bufused;
  610. return 0;
  611. }
  612. static wasi_errno_t
  613. wasi_path_readlink(wasm_exec_env_t exec_env, wasi_fd_t fd, const char *path,
  614. uint32 path_len, char *buf, uint32 buf_len,
  615. uint32 *bufused_app)
  616. {
  617. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  618. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  619. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  620. size_t bufused;
  621. wasi_errno_t err;
  622. if (!wasi_ctx)
  623. return (wasi_errno_t)-1;
  624. if (!validate_native_addr(bufused_app, sizeof(uint32)))
  625. return (wasi_errno_t)-1;
  626. err = wasmtime_ssp_path_readlink(curfds, fd, path, path_len, buf, buf_len,
  627. &bufused);
  628. if (err)
  629. return err;
  630. *bufused_app = (uint32)bufused;
  631. return 0;
  632. }
  633. static wasi_errno_t
  634. wasi_path_rename(wasm_exec_env_t exec_env, wasi_fd_t old_fd,
  635. const char *old_path, uint32 old_path_len, wasi_fd_t new_fd,
  636. const char *new_path, uint32 new_path_len)
  637. {
  638. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  639. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  640. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  641. if (!wasi_ctx)
  642. return (wasi_errno_t)-1;
  643. return wasmtime_ssp_path_rename(curfds, old_fd, old_path, old_path_len,
  644. new_fd, new_path, new_path_len);
  645. }
  646. static wasi_errno_t
  647. wasi_fd_filestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
  648. wasi_filestat_t *filestat)
  649. {
  650. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  651. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  652. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  653. if (!wasi_ctx)
  654. return (wasi_errno_t)-1;
  655. if (!validate_native_addr(filestat, sizeof(wasi_filestat_t)))
  656. return (wasi_errno_t)-1;
  657. return wasmtime_ssp_fd_filestat_get(curfds, fd, filestat);
  658. }
  659. static wasi_errno_t
  660. wasi_fd_filestat_set_times(wasm_exec_env_t exec_env, wasi_fd_t fd,
  661. wasi_timestamp_t st_atim, wasi_timestamp_t st_mtim,
  662. wasi_fstflags_t fstflags)
  663. {
  664. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  665. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  666. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  667. if (!wasi_ctx)
  668. return (wasi_errno_t)-1;
  669. return wasmtime_ssp_fd_filestat_set_times(curfds, fd, st_atim, st_mtim,
  670. fstflags);
  671. }
  672. static wasi_errno_t
  673. wasi_fd_filestat_set_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
  674. wasi_filesize_t st_size)
  675. {
  676. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  677. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  678. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  679. if (!wasi_ctx)
  680. return (wasi_errno_t)-1;
  681. return wasmtime_ssp_fd_filestat_set_size(curfds, fd, st_size);
  682. }
  683. static wasi_errno_t
  684. wasi_path_filestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
  685. wasi_lookupflags_t flags, const char *path,
  686. uint32 path_len, wasi_filestat_t *filestat)
  687. {
  688. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  689. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  690. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  691. if (!wasi_ctx)
  692. return (wasi_errno_t)-1;
  693. if (!validate_native_addr(filestat, sizeof(wasi_filestat_t)))
  694. return (wasi_errno_t)-1;
  695. return wasmtime_ssp_path_filestat_get(curfds, fd, flags, path, path_len,
  696. filestat);
  697. }
  698. static wasi_errno_t
  699. wasi_path_filestat_set_times(wasm_exec_env_t exec_env, wasi_fd_t fd,
  700. wasi_lookupflags_t flags, const char *path,
  701. uint32 path_len, wasi_timestamp_t st_atim,
  702. wasi_timestamp_t st_mtim, wasi_fstflags_t fstflags)
  703. {
  704. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  705. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  706. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  707. if (!wasi_ctx)
  708. return (wasi_errno_t)-1;
  709. return wasmtime_ssp_path_filestat_set_times(
  710. curfds, fd, flags, path, path_len, st_atim, st_mtim, fstflags);
  711. }
  712. static wasi_errno_t
  713. wasi_path_symlink(wasm_exec_env_t exec_env, const char *old_path,
  714. uint32 old_path_len, wasi_fd_t fd, const char *new_path,
  715. uint32 new_path_len)
  716. {
  717. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  718. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  719. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  720. struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
  721. if (!wasi_ctx)
  722. return (wasi_errno_t)-1;
  723. return wasmtime_ssp_path_symlink(curfds, prestats, old_path, old_path_len,
  724. fd, new_path, new_path_len);
  725. }
  726. static wasi_errno_t
  727. wasi_path_unlink_file(wasm_exec_env_t exec_env, wasi_fd_t fd, const char *path,
  728. uint32 path_len)
  729. {
  730. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  731. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  732. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  733. if (!wasi_ctx)
  734. return (wasi_errno_t)-1;
  735. return wasmtime_ssp_path_unlink_file(curfds, fd, path, path_len);
  736. }
  737. static wasi_errno_t
  738. wasi_path_remove_directory(wasm_exec_env_t exec_env, wasi_fd_t fd,
  739. const char *path, uint32 path_len)
  740. {
  741. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  742. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  743. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  744. if (!wasi_ctx)
  745. return (wasi_errno_t)-1;
  746. return wasmtime_ssp_path_remove_directory(curfds, fd, path, path_len);
  747. }
  748. static wasi_errno_t
  749. wasi_poll_oneoff(wasm_exec_env_t exec_env, const wasi_subscription_t *in,
  750. wasi_event_t *out, uint32 nsubscriptions, uint32 *nevents_app)
  751. {
  752. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  753. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  754. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  755. size_t nevents;
  756. wasi_errno_t err;
  757. if (!wasi_ctx)
  758. return (wasi_errno_t)-1;
  759. if (!validate_native_addr((void *)in, sizeof(wasi_subscription_t))
  760. || !validate_native_addr(out, sizeof(wasi_event_t))
  761. || !validate_native_addr(nevents_app, sizeof(uint32)))
  762. return (wasi_errno_t)-1;
  763. err = wasmtime_ssp_poll_oneoff(curfds, in, out, nsubscriptions, &nevents);
  764. if (err)
  765. return err;
  766. *nevents_app = (uint32)nevents;
  767. return 0;
  768. }
  769. static void
  770. wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval)
  771. {
  772. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  773. /* Here throwing exception is just to let wasm app exit,
  774. the upper layer should clear the exception and return
  775. as normal */
  776. wasm_runtime_set_exception(module_inst, "wasi proc exit");
  777. }
  778. static wasi_errno_t
  779. wasi_proc_raise(wasm_exec_env_t exec_env, wasi_signal_t sig)
  780. {
  781. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  782. char buf[32];
  783. snprintf(buf, sizeof(buf), "%s%d", "wasi proc raise ", sig);
  784. wasm_runtime_set_exception(module_inst, buf);
  785. return 0;
  786. }
  787. static wasi_errno_t
  788. wasi_random_get(wasm_exec_env_t exec_env, void *buf, uint32 buf_len)
  789. {
  790. return wasmtime_ssp_random_get(buf, buf_len);
  791. }
  792. static wasi_errno_t
  793. wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data,
  794. uint32 ri_data_len, wasi_riflags_t ri_flags,
  795. uint32 *ro_datalen_app, wasi_roflags_t *ro_flags)
  796. {
  797. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  798. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  799. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  800. wasi_iovec_t *iovec, *iovec_begin;
  801. uint64 total_size;
  802. size_t ro_datalen;
  803. uint32 i;
  804. wasi_errno_t err;
  805. if (!wasi_ctx)
  806. return (wasi_errno_t)-1;
  807. total_size = sizeof(iovec_app_t) * (uint64)ri_data_len;
  808. if (!validate_native_addr(ro_datalen_app, (uint32)sizeof(uint32))
  809. || !validate_native_addr(ro_flags, (uint32)sizeof(wasi_roflags_t))
  810. || total_size >= UINT32_MAX
  811. || !validate_native_addr(ri_data, (uint32)total_size))
  812. return (wasi_errno_t)-1;
  813. total_size = sizeof(wasi_iovec_t) * (uint64)ri_data_len;
  814. if (total_size >= UINT32_MAX
  815. || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
  816. return (wasi_errno_t)-1;
  817. iovec = iovec_begin;
  818. for (i = 0; i < ri_data_len; i++, ri_data++, iovec++) {
  819. if (!validate_app_addr(ri_data->buf_offset, ri_data->buf_len)) {
  820. err = (wasi_errno_t)-1;
  821. goto fail;
  822. }
  823. iovec->buf = (void *)addr_app_to_native(ri_data->buf_offset);
  824. iovec->buf_len = ri_data->buf_len;
  825. }
  826. err = wasmtime_ssp_sock_recv(curfds, sock, iovec_begin, ri_data_len,
  827. ri_flags, &ro_datalen, ro_flags);
  828. if (err)
  829. goto fail;
  830. *(uint32 *)ro_datalen_app = (uint32)ro_datalen;
  831. /* success */
  832. err = 0;
  833. fail:
  834. wasm_runtime_free(iovec_begin);
  835. return err;
  836. }
  837. static wasi_errno_t
  838. wasi_sock_send(wasm_exec_env_t exec_env, wasi_fd_t sock,
  839. const iovec_app_t *si_data, uint32 si_data_len,
  840. wasi_siflags_t si_flags, uint32 *so_datalen_app)
  841. {
  842. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  843. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  844. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  845. wasi_ciovec_t *ciovec, *ciovec_begin;
  846. uint64 total_size;
  847. size_t so_datalen;
  848. uint32 i;
  849. wasi_errno_t err;
  850. if (!wasi_ctx)
  851. return (wasi_errno_t)-1;
  852. total_size = sizeof(iovec_app_t) * (uint64)si_data_len;
  853. if (!validate_native_addr(so_datalen_app, sizeof(uint32))
  854. || total_size >= UINT32_MAX
  855. || !validate_native_addr((void *)si_data, (uint32)total_size))
  856. return (wasi_errno_t)-1;
  857. total_size = sizeof(wasi_ciovec_t) * (uint64)si_data_len;
  858. if (total_size >= UINT32_MAX
  859. || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
  860. return (wasi_errno_t)-1;
  861. ciovec = ciovec_begin;
  862. for (i = 0; i < si_data_len; i++, si_data++, ciovec++) {
  863. if (!validate_app_addr(si_data->buf_offset, si_data->buf_len)) {
  864. err = (wasi_errno_t)-1;
  865. goto fail;
  866. }
  867. ciovec->buf = (char *)addr_app_to_native(si_data->buf_offset);
  868. ciovec->buf_len = si_data->buf_len;
  869. }
  870. err = wasmtime_ssp_sock_send(curfds, sock, ciovec_begin, si_data_len,
  871. si_flags, &so_datalen);
  872. if (err)
  873. goto fail;
  874. *so_datalen_app = (uint32)so_datalen;
  875. /* success */
  876. err = 0;
  877. fail:
  878. wasm_runtime_free(ciovec_begin);
  879. return err;
  880. }
  881. static wasi_errno_t
  882. wasi_sock_shutdown(wasm_exec_env_t exec_env, wasi_fd_t sock, wasi_sdflags_t how)
  883. {
  884. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  885. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  886. struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
  887. if (!wasi_ctx)
  888. return (wasi_errno_t)-1;
  889. return wasmtime_ssp_sock_shutdown(curfds, sock, how);
  890. }
  891. static wasi_errno_t
  892. wasi_sched_yield(wasm_exec_env_t exec_env)
  893. {
  894. return wasmtime_ssp_sched_yield();
  895. }
  896. /* clang-format off */
  897. #define REG_NATIVE_FUNC(func_name, signature) \
  898. { #func_name, wasi_##func_name, signature, NULL }
  899. /* clang-format on */
  900. static NativeSymbol native_symbols_libc_wasi[] = {
  901. REG_NATIVE_FUNC(args_get, "(**)i"),
  902. REG_NATIVE_FUNC(args_sizes_get, "(**)i"),
  903. REG_NATIVE_FUNC(clock_res_get, "(i*)i"),
  904. REG_NATIVE_FUNC(clock_time_get, "(iI*)i"),
  905. REG_NATIVE_FUNC(environ_get, "(**)i"),
  906. REG_NATIVE_FUNC(environ_sizes_get, "(**)i"),
  907. REG_NATIVE_FUNC(fd_prestat_get, "(i*)i"),
  908. REG_NATIVE_FUNC(fd_prestat_dir_name, "(i*~)i"),
  909. REG_NATIVE_FUNC(fd_close, "(i)i"),
  910. REG_NATIVE_FUNC(fd_datasync, "(i)i"),
  911. REG_NATIVE_FUNC(fd_pread, "(i*iI*)i"),
  912. REG_NATIVE_FUNC(fd_pwrite, "(i*iI*)i"),
  913. REG_NATIVE_FUNC(fd_read, "(i*i*)i"),
  914. REG_NATIVE_FUNC(fd_renumber, "(ii)i"),
  915. REG_NATIVE_FUNC(fd_seek, "(iIi*)i"),
  916. REG_NATIVE_FUNC(fd_tell, "(i*)i"),
  917. REG_NATIVE_FUNC(fd_fdstat_get, "(i*)i"),
  918. REG_NATIVE_FUNC(fd_fdstat_set_flags, "(ii)i"),
  919. REG_NATIVE_FUNC(fd_fdstat_set_rights, "(iII)i"),
  920. REG_NATIVE_FUNC(fd_sync, "(i)i"),
  921. REG_NATIVE_FUNC(fd_write, "(i*i*)i"),
  922. REG_NATIVE_FUNC(fd_advise, "(iIIi)i"),
  923. REG_NATIVE_FUNC(fd_allocate, "(iII)i"),
  924. REG_NATIVE_FUNC(path_create_directory, "(i*~)i"),
  925. REG_NATIVE_FUNC(path_link, "(ii*~i*~)i"),
  926. REG_NATIVE_FUNC(path_open, "(ii*~iIIi*)i"),
  927. REG_NATIVE_FUNC(fd_readdir, "(i*~I*)i"),
  928. REG_NATIVE_FUNC(path_readlink, "(i*~*~*)i"),
  929. REG_NATIVE_FUNC(path_rename, "(i*~i*~)i"),
  930. REG_NATIVE_FUNC(fd_filestat_get, "(i*)i"),
  931. REG_NATIVE_FUNC(fd_filestat_set_times, "(iIIi)i"),
  932. REG_NATIVE_FUNC(fd_filestat_set_size, "(iI)i"),
  933. REG_NATIVE_FUNC(path_filestat_get, "(ii*~*)i"),
  934. REG_NATIVE_FUNC(path_filestat_set_times, "(ii*~IIi)i"),
  935. REG_NATIVE_FUNC(path_symlink, "(*~i*~)i"),
  936. REG_NATIVE_FUNC(path_unlink_file, "(i*~)i"),
  937. REG_NATIVE_FUNC(path_remove_directory, "(i*~)i"),
  938. REG_NATIVE_FUNC(poll_oneoff, "(**i*)i"),
  939. REG_NATIVE_FUNC(proc_exit, "(i)"),
  940. REG_NATIVE_FUNC(proc_raise, "(i)i"),
  941. REG_NATIVE_FUNC(random_get, "(*~)i"),
  942. REG_NATIVE_FUNC(sock_recv, "(i*ii**)i"),
  943. REG_NATIVE_FUNC(sock_send, "(i*ii*)i"),
  944. REG_NATIVE_FUNC(sock_shutdown, "(ii)i"),
  945. REG_NATIVE_FUNC(sched_yield, "()i"),
  946. };
  947. uint32
  948. get_libc_wasi_export_apis(NativeSymbol **p_libc_wasi_apis)
  949. {
  950. *p_libc_wasi_apis = native_symbols_libc_wasi;
  951. return sizeof(native_symbols_libc_wasi) / sizeof(NativeSymbol);
  952. }