libc_wasi_wrapper.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  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. #include "wasm_runtime_common.h"
  9. #include "wasmtime_ssp.h"
  10. #if WASM_ENABLE_THREAD_MGR != 0
  11. #include "../../../thread-mgr/thread_manager.h"
  12. #endif
  13. void
  14. wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
  15. /* clang-format off */
  16. #define get_module_inst(exec_env) \
  17. wasm_runtime_get_module_inst(exec_env)
  18. #define get_wasi_ctx(module_inst) \
  19. wasm_runtime_get_wasi_ctx(module_inst)
  20. #define validate_app_addr(offset, size) \
  21. wasm_runtime_validate_app_addr(module_inst, offset, size)
  22. #define validate_native_addr(addr, size) \
  23. wasm_runtime_validate_native_addr(module_inst, addr, size)
  24. #define addr_app_to_native(offset) \
  25. wasm_runtime_addr_app_to_native(module_inst, offset)
  26. #define addr_native_to_app(ptr) \
  27. wasm_runtime_addr_native_to_app(module_inst, ptr)
  28. #define module_malloc(size, p_native_addr) \
  29. wasm_runtime_module_malloc(module_inst, size, p_native_addr)
  30. #define module_free(offset) \
  31. wasm_runtime_module_free(module_inst, offset)
  32. /* clang-format on */
  33. typedef struct wasi_prestat_app {
  34. wasi_preopentype_t pr_type;
  35. uint32 pr_name_len;
  36. } wasi_prestat_app_t;
  37. typedef struct iovec_app {
  38. uint32 buf_offset;
  39. uint32 buf_len;
  40. } iovec_app_t;
  41. typedef struct WASIContext *wasi_ctx_t;
  42. wasi_ctx_t
  43. wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst);
  44. #if WASM_ENABLE_THREAD_MGR != 0
  45. static inline uint64_t
  46. min_uint64(uint64_t a, uint64_t b)
  47. {
  48. return a > b ? b : a;
  49. }
  50. #endif
  51. static inline uint32_t
  52. min_uint32(uint32_t a, uint32_t b)
  53. {
  54. return a > b ? b : a;
  55. }
  56. static inline struct fd_table *
  57. wasi_ctx_get_curfds(wasi_ctx_t wasi_ctx)
  58. {
  59. if (!wasi_ctx)
  60. return NULL;
  61. return wasi_ctx->curfds;
  62. }
  63. static inline struct argv_environ_values *
  64. wasi_ctx_get_argv_environ(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
  65. {
  66. if (!wasi_ctx)
  67. return NULL;
  68. return wasi_ctx->argv_environ;
  69. }
  70. static inline struct fd_prestats *
  71. wasi_ctx_get_prestats(wasi_ctx_t wasi_ctx)
  72. {
  73. if (!wasi_ctx)
  74. return NULL;
  75. return wasi_ctx->prestats;
  76. }
  77. static inline struct addr_pool *
  78. wasi_ctx_get_addr_pool(wasi_ctx_t wasi_ctx)
  79. {
  80. if (!wasi_ctx)
  81. return NULL;
  82. return wasi_ctx->addr_pool;
  83. }
  84. static inline char **
  85. wasi_ctx_get_ns_lookup_list(wasi_ctx_t wasi_ctx)
  86. {
  87. if (!wasi_ctx)
  88. return NULL;
  89. return wasi_ctx->ns_lookup_list;
  90. }
  91. static wasi_errno_t
  92. wasi_args_get(wasm_exec_env_t exec_env, uint32 *argv_offsets, char *argv_buf)
  93. {
  94. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  95. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  96. struct argv_environ_values *argv_environ =
  97. wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
  98. size_t argc, argv_buf_size, i;
  99. char **argv;
  100. uint64 total_size;
  101. wasi_errno_t err;
  102. if (!wasi_ctx)
  103. return (wasi_errno_t)-1;
  104. err = wasmtime_ssp_args_sizes_get(argv_environ, &argc, &argv_buf_size);
  105. if (err)
  106. return err;
  107. total_size = sizeof(int32) * ((uint64)argc + 1);
  108. if (total_size >= UINT32_MAX
  109. || !validate_native_addr(argv_offsets, total_size)
  110. || argv_buf_size >= UINT32_MAX
  111. || !validate_native_addr(argv_buf, (uint64)argv_buf_size))
  112. return (wasi_errno_t)-1;
  113. total_size = sizeof(char *) * ((uint64)argc + 1);
  114. if (total_size >= UINT32_MAX
  115. || !(argv = wasm_runtime_malloc((uint32)total_size)))
  116. return (wasi_errno_t)-1;
  117. err = wasmtime_ssp_args_get(argv_environ, argv, argv_buf);
  118. if (err) {
  119. wasm_runtime_free(argv);
  120. return err;
  121. }
  122. for (i = 0; i < argc; i++)
  123. argv_offsets[i] = (uint32)addr_native_to_app(argv[i]);
  124. wasm_runtime_free(argv);
  125. return 0;
  126. }
  127. static wasi_errno_t
  128. wasi_args_sizes_get(wasm_exec_env_t exec_env, uint32 *argc_app,
  129. uint32 *argv_buf_size_app)
  130. {
  131. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  132. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  133. struct argv_environ_values *argv_environ;
  134. size_t argc, argv_buf_size;
  135. wasi_errno_t err;
  136. if (!wasi_ctx)
  137. return (wasi_errno_t)-1;
  138. if (!validate_native_addr(argc_app, (uint64)sizeof(uint32))
  139. || !validate_native_addr(argv_buf_size_app, (uint64)sizeof(uint32)))
  140. return (wasi_errno_t)-1;
  141. argv_environ = wasi_ctx->argv_environ;
  142. err = wasmtime_ssp_args_sizes_get(argv_environ, &argc, &argv_buf_size);
  143. if (err)
  144. return err;
  145. *argc_app = (uint32)argc;
  146. *argv_buf_size_app = (uint32)argv_buf_size;
  147. return 0;
  148. }
  149. static wasi_errno_t
  150. wasi_clock_res_get(wasm_exec_env_t exec_env,
  151. wasi_clockid_t clock_id, /* uint32 clock_id */
  152. wasi_timestamp_t *resolution /* uint64 *resolution */)
  153. {
  154. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  155. if (!validate_native_addr(resolution, (uint64)sizeof(wasi_timestamp_t)))
  156. return (wasi_errno_t)-1;
  157. return os_clock_res_get(clock_id, resolution);
  158. }
  159. static wasi_errno_t
  160. wasi_clock_time_get(wasm_exec_env_t exec_env,
  161. wasi_clockid_t clock_id, /* uint32 clock_id */
  162. wasi_timestamp_t precision, /* uint64 precision */
  163. wasi_timestamp_t *time /* uint64 *time */)
  164. {
  165. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  166. if (!validate_native_addr(time, (uint64)sizeof(wasi_timestamp_t)))
  167. return (wasi_errno_t)-1;
  168. return os_clock_time_get(clock_id, precision, time);
  169. }
  170. static wasi_errno_t
  171. wasi_environ_get(wasm_exec_env_t exec_env, uint32 *environ_offsets,
  172. char *environ_buf)
  173. {
  174. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  175. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  176. struct argv_environ_values *argv_environ =
  177. wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
  178. size_t environ_count, environ_buf_size, i;
  179. uint64 total_size;
  180. char **environs;
  181. wasi_errno_t err;
  182. if (!wasi_ctx)
  183. return (wasi_errno_t)-1;
  184. err = wasmtime_ssp_environ_sizes_get(argv_environ, &environ_count,
  185. &environ_buf_size);
  186. if (err)
  187. return err;
  188. total_size = sizeof(int32) * ((uint64)environ_count + 1);
  189. if (total_size >= UINT32_MAX
  190. || !validate_native_addr(environ_offsets, total_size)
  191. || environ_buf_size >= UINT32_MAX
  192. || !validate_native_addr(environ_buf, (uint64)environ_buf_size))
  193. return (wasi_errno_t)-1;
  194. total_size = sizeof(char *) * (((uint64)environ_count + 1));
  195. if (total_size >= UINT32_MAX
  196. || !(environs = wasm_runtime_malloc((uint32)total_size)))
  197. return (wasi_errno_t)-1;
  198. err = wasmtime_ssp_environ_get(argv_environ, environs, environ_buf);
  199. if (err) {
  200. wasm_runtime_free(environs);
  201. return err;
  202. }
  203. for (i = 0; i < environ_count; i++)
  204. environ_offsets[i] = (uint32)addr_native_to_app(environs[i]);
  205. wasm_runtime_free(environs);
  206. return 0;
  207. }
  208. static wasi_errno_t
  209. wasi_environ_sizes_get(wasm_exec_env_t exec_env, uint32 *environ_count_app,
  210. uint32 *environ_buf_size_app)
  211. {
  212. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  213. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  214. struct argv_environ_values *argv_environ =
  215. wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
  216. size_t environ_count, environ_buf_size;
  217. wasi_errno_t err;
  218. if (!wasi_ctx)
  219. return (wasi_errno_t)-1;
  220. if (!validate_native_addr(environ_count_app, (uint64)sizeof(uint32))
  221. || !validate_native_addr(environ_buf_size_app, (uint64)sizeof(uint32)))
  222. return (wasi_errno_t)-1;
  223. err = wasmtime_ssp_environ_sizes_get(argv_environ, &environ_count,
  224. &environ_buf_size);
  225. if (err)
  226. return err;
  227. *environ_count_app = (uint32)environ_count;
  228. *environ_buf_size_app = (uint32)environ_buf_size;
  229. return 0;
  230. }
  231. static wasi_errno_t
  232. wasi_fd_prestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
  233. wasi_prestat_app_t *prestat_app)
  234. {
  235. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  236. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  237. struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx);
  238. wasi_prestat_t prestat;
  239. wasi_errno_t err;
  240. if (!wasi_ctx)
  241. return (wasi_errno_t)-1;
  242. if (!validate_native_addr(prestat_app, (uint64)sizeof(wasi_prestat_app_t)))
  243. return (wasi_errno_t)-1;
  244. err = wasmtime_ssp_fd_prestat_get(prestats, fd, &prestat);
  245. if (err)
  246. return err;
  247. prestat_app->pr_type = prestat.pr_type;
  248. prestat_app->pr_name_len = (uint32)prestat.u.dir.pr_name_len;
  249. return 0;
  250. }
  251. static wasi_errno_t
  252. wasi_fd_prestat_dir_name(wasm_exec_env_t exec_env, wasi_fd_t fd, char *path,
  253. uint32 path_len)
  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_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx);
  258. if (!wasi_ctx)
  259. return (wasi_errno_t)-1;
  260. return wasmtime_ssp_fd_prestat_dir_name(prestats, fd, path, path_len);
  261. }
  262. static wasi_errno_t
  263. wasi_fd_close(wasm_exec_env_t exec_env, wasi_fd_t fd)
  264. {
  265. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  266. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  267. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  268. struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx);
  269. if (!wasi_ctx)
  270. return (wasi_errno_t)-1;
  271. return wasmtime_ssp_fd_close(exec_env, curfds, prestats, fd);
  272. }
  273. static wasi_errno_t
  274. wasi_fd_datasync(wasm_exec_env_t exec_env, wasi_fd_t fd)
  275. {
  276. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  277. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  278. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  279. if (!wasi_ctx)
  280. return (wasi_errno_t)-1;
  281. return wasmtime_ssp_fd_datasync(exec_env, curfds, fd);
  282. }
  283. static wasi_errno_t
  284. wasi_fd_pread(wasm_exec_env_t exec_env, wasi_fd_t fd, iovec_app_t *iovec_app,
  285. uint32 iovs_len, wasi_filesize_t offset, uint32 *nread_app)
  286. {
  287. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  288. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  289. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  290. wasi_iovec_t *iovec, *iovec_begin;
  291. uint64 total_size;
  292. size_t nread;
  293. uint32 i;
  294. wasi_errno_t err;
  295. if (!wasi_ctx)
  296. return (wasi_errno_t)-1;
  297. total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
  298. if (!validate_native_addr(nread_app, (uint64)sizeof(uint32))
  299. || total_size >= UINT32_MAX
  300. || !validate_native_addr(iovec_app, total_size))
  301. return (wasi_errno_t)-1;
  302. total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
  303. if (total_size >= UINT32_MAX
  304. || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
  305. return (wasi_errno_t)-1;
  306. iovec = iovec_begin;
  307. for (i = 0; i < iovs_len; i++, iovec_app++, iovec++) {
  308. if (!validate_app_addr((uint64)iovec_app->buf_offset,
  309. (uint64)iovec_app->buf_len)) {
  310. err = (wasi_errno_t)-1;
  311. goto fail;
  312. }
  313. iovec->buf = (void *)addr_app_to_native((uint64)iovec_app->buf_offset);
  314. iovec->buf_len = iovec_app->buf_len;
  315. }
  316. err = wasmtime_ssp_fd_pread(exec_env, curfds, fd, iovec_begin, iovs_len,
  317. offset, &nread);
  318. if (err)
  319. goto fail;
  320. *nread_app = (uint32)nread;
  321. /* success */
  322. err = 0;
  323. fail:
  324. wasm_runtime_free(iovec_begin);
  325. return err;
  326. }
  327. static wasi_errno_t
  328. wasi_fd_pwrite(wasm_exec_env_t exec_env, wasi_fd_t fd,
  329. const iovec_app_t *iovec_app, uint32 iovs_len,
  330. wasi_filesize_t offset, uint32 *nwritten_app)
  331. {
  332. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  333. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  334. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  335. wasi_ciovec_t *ciovec, *ciovec_begin;
  336. uint64 total_size;
  337. size_t nwritten;
  338. uint32 i;
  339. wasi_errno_t err;
  340. if (!wasi_ctx)
  341. return (wasi_errno_t)-1;
  342. total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
  343. if (!validate_native_addr(nwritten_app, (uint64)sizeof(uint32))
  344. || total_size >= UINT32_MAX
  345. || !validate_native_addr((void *)iovec_app, total_size))
  346. return (wasi_errno_t)-1;
  347. total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
  348. if (total_size >= UINT32_MAX
  349. || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
  350. return (wasi_errno_t)-1;
  351. ciovec = ciovec_begin;
  352. for (i = 0; i < iovs_len; i++, iovec_app++, ciovec++) {
  353. if (!validate_app_addr((uint64)iovec_app->buf_offset,
  354. (uint64)iovec_app->buf_len)) {
  355. err = (wasi_errno_t)-1;
  356. goto fail;
  357. }
  358. ciovec->buf = (char *)addr_app_to_native((uint64)iovec_app->buf_offset);
  359. ciovec->buf_len = iovec_app->buf_len;
  360. }
  361. err = wasmtime_ssp_fd_pwrite(exec_env, curfds, fd, ciovec_begin, iovs_len,
  362. offset, &nwritten);
  363. if (err)
  364. goto fail;
  365. *nwritten_app = (uint32)nwritten;
  366. /* success */
  367. err = 0;
  368. fail:
  369. wasm_runtime_free(ciovec_begin);
  370. return err;
  371. }
  372. static wasi_errno_t
  373. wasi_fd_read(wasm_exec_env_t exec_env, wasi_fd_t fd,
  374. const iovec_app_t *iovec_app, uint32 iovs_len, uint32 *nread_app)
  375. {
  376. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  377. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  378. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  379. wasi_iovec_t *iovec, *iovec_begin;
  380. uint64 total_size;
  381. size_t nread;
  382. uint32 i;
  383. wasi_errno_t err;
  384. if (!wasi_ctx)
  385. return (wasi_errno_t)-1;
  386. total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
  387. if (!validate_native_addr(nread_app, (uint64)sizeof(uint32))
  388. || total_size >= UINT32_MAX
  389. || !validate_native_addr((void *)iovec_app, total_size))
  390. return (wasi_errno_t)-1;
  391. total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
  392. if (total_size >= UINT32_MAX
  393. || !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
  394. return (wasi_errno_t)-1;
  395. iovec = iovec_begin;
  396. for (i = 0; i < iovs_len; i++, iovec_app++, iovec++) {
  397. if (!validate_app_addr((uint64)iovec_app->buf_offset,
  398. (uint64)iovec_app->buf_len)) {
  399. err = (wasi_errno_t)-1;
  400. goto fail;
  401. }
  402. iovec->buf = (void *)addr_app_to_native((uint64)iovec_app->buf_offset);
  403. iovec->buf_len = iovec_app->buf_len;
  404. }
  405. err = wasmtime_ssp_fd_read(exec_env, curfds, fd, iovec_begin, iovs_len,
  406. &nread);
  407. if (err)
  408. goto fail;
  409. *nread_app = (uint32)nread;
  410. /* success */
  411. err = 0;
  412. fail:
  413. wasm_runtime_free(iovec_begin);
  414. return err;
  415. }
  416. static wasi_errno_t
  417. wasi_fd_renumber(wasm_exec_env_t exec_env, wasi_fd_t from, wasi_fd_t to)
  418. {
  419. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  420. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  421. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  422. struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx);
  423. if (!wasi_ctx)
  424. return (wasi_errno_t)-1;
  425. return wasmtime_ssp_fd_renumber(exec_env, curfds, prestats, from, to);
  426. }
  427. static wasi_errno_t
  428. wasi_fd_seek(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filedelta_t offset,
  429. wasi_whence_t whence, wasi_filesize_t *newoffset)
  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(wasi_ctx);
  434. if (!wasi_ctx)
  435. return (wasi_errno_t)-1;
  436. if (!validate_native_addr(newoffset, (uint64)sizeof(wasi_filesize_t)))
  437. return (wasi_errno_t)-1;
  438. return wasmtime_ssp_fd_seek(exec_env, curfds, fd, offset, whence,
  439. newoffset);
  440. }
  441. static wasi_errno_t
  442. wasi_fd_tell(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t *newoffset)
  443. {
  444. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  445. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  446. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  447. if (!wasi_ctx)
  448. return (wasi_errno_t)-1;
  449. if (!validate_native_addr(newoffset, (uint64)sizeof(wasi_filesize_t)))
  450. return (wasi_errno_t)-1;
  451. return wasmtime_ssp_fd_tell(exec_env, curfds, fd, newoffset);
  452. }
  453. static wasi_errno_t
  454. wasi_fd_fdstat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
  455. wasi_fdstat_t *fdstat_app)
  456. {
  457. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  458. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  459. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  460. wasi_fdstat_t fdstat;
  461. wasi_errno_t err;
  462. if (!wasi_ctx)
  463. return (wasi_errno_t)-1;
  464. if (!validate_native_addr(fdstat_app, (uint64)sizeof(wasi_fdstat_t)))
  465. return (wasi_errno_t)-1;
  466. err = wasmtime_ssp_fd_fdstat_get(exec_env, curfds, fd, &fdstat);
  467. if (err)
  468. return err;
  469. memcpy(fdstat_app, &fdstat, sizeof(wasi_fdstat_t));
  470. return 0;
  471. }
  472. static wasi_errno_t
  473. wasi_fd_fdstat_set_flags(wasm_exec_env_t exec_env, wasi_fd_t fd,
  474. wasi_fdflags_t flags)
  475. {
  476. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  477. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  478. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  479. if (!wasi_ctx)
  480. return (wasi_errno_t)-1;
  481. return wasmtime_ssp_fd_fdstat_set_flags(exec_env, curfds, fd, flags);
  482. }
  483. static wasi_errno_t
  484. wasi_fd_fdstat_set_rights(wasm_exec_env_t exec_env, wasi_fd_t fd,
  485. wasi_rights_t fs_rights_base,
  486. wasi_rights_t fs_rights_inheriting)
  487. {
  488. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  489. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  490. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  491. if (!wasi_ctx)
  492. return (wasi_errno_t)-1;
  493. return wasmtime_ssp_fd_fdstat_set_rights(
  494. exec_env, curfds, fd, fs_rights_base, fs_rights_inheriting);
  495. }
  496. static wasi_errno_t
  497. wasi_fd_sync(wasm_exec_env_t exec_env, wasi_fd_t fd)
  498. {
  499. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  500. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  501. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  502. if (!wasi_ctx)
  503. return (wasi_errno_t)-1;
  504. return wasmtime_ssp_fd_sync(exec_env, curfds, fd);
  505. }
  506. static wasi_errno_t
  507. wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
  508. const iovec_app_t *iovec_app, uint32 iovs_len,
  509. uint32 *nwritten_app)
  510. {
  511. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  512. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  513. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  514. wasi_ciovec_t *ciovec, *ciovec_begin;
  515. uint64 total_size;
  516. size_t nwritten;
  517. uint32 i;
  518. wasi_errno_t err;
  519. if (!wasi_ctx)
  520. return (wasi_errno_t)-1;
  521. total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
  522. if (!validate_native_addr(nwritten_app, (uint64)sizeof(uint32))
  523. || total_size >= UINT32_MAX
  524. || !validate_native_addr((void *)iovec_app, total_size))
  525. return (wasi_errno_t)-1;
  526. total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
  527. if (total_size >= UINT32_MAX
  528. || !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
  529. return (wasi_errno_t)-1;
  530. ciovec = ciovec_begin;
  531. for (i = 0; i < iovs_len; i++, iovec_app++, ciovec++) {
  532. if (!validate_app_addr((uint64)iovec_app->buf_offset,
  533. (uint64)iovec_app->buf_len)) {
  534. err = (wasi_errno_t)-1;
  535. goto fail;
  536. }
  537. ciovec->buf = (char *)addr_app_to_native((uint64)iovec_app->buf_offset);
  538. ciovec->buf_len = iovec_app->buf_len;
  539. }
  540. err = wasmtime_ssp_fd_write(exec_env, curfds, fd, ciovec_begin, iovs_len,
  541. &nwritten);
  542. if (err)
  543. goto fail;
  544. *nwritten_app = (uint32)nwritten;
  545. /* success */
  546. err = 0;
  547. fail:
  548. wasm_runtime_free(ciovec_begin);
  549. return err;
  550. }
  551. static wasi_errno_t
  552. wasi_fd_advise(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t offset,
  553. wasi_filesize_t len, wasi_advice_t advice)
  554. {
  555. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  556. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  557. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  558. if (!wasi_ctx)
  559. return (wasi_errno_t)-1;
  560. return wasmtime_ssp_fd_advise(exec_env, curfds, fd, offset, len, advice);
  561. }
  562. static wasi_errno_t
  563. wasi_fd_allocate(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t offset,
  564. wasi_filesize_t len)
  565. {
  566. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  567. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  568. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  569. if (!wasi_ctx)
  570. return (wasi_errno_t)-1;
  571. return wasmtime_ssp_fd_allocate(exec_env, curfds, fd, offset, len);
  572. }
  573. static wasi_errno_t
  574. wasi_path_create_directory(wasm_exec_env_t exec_env, wasi_fd_t fd,
  575. const char *path, uint32 path_len)
  576. {
  577. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  578. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  579. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  580. if (!wasi_ctx)
  581. return (wasi_errno_t)-1;
  582. return wasmtime_ssp_path_create_directory(exec_env, curfds, fd, path,
  583. path_len);
  584. }
  585. static wasi_errno_t
  586. wasi_path_link(wasm_exec_env_t exec_env, wasi_fd_t old_fd,
  587. wasi_lookupflags_t old_flags, const char *old_path,
  588. uint32 old_path_len, wasi_fd_t new_fd, const char *new_path,
  589. uint32 new_path_len)
  590. {
  591. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  592. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  593. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  594. struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx);
  595. if (!wasi_ctx)
  596. return (wasi_errno_t)-1;
  597. return wasmtime_ssp_path_link(exec_env, curfds, prestats, old_fd, old_flags,
  598. old_path, old_path_len, new_fd, new_path,
  599. new_path_len);
  600. }
  601. static wasi_errno_t
  602. wasi_path_open(wasm_exec_env_t exec_env, wasi_fd_t dirfd,
  603. wasi_lookupflags_t dirflags, const char *path, uint32 path_len,
  604. wasi_oflags_t oflags, wasi_rights_t fs_rights_base,
  605. wasi_rights_t fs_rights_inheriting, wasi_fdflags_t fs_flags,
  606. wasi_fd_t *fd_app)
  607. {
  608. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  609. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  610. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  611. wasi_fd_t fd = (wasi_fd_t)-1; /* set fd_app -1 if path open failed */
  612. wasi_errno_t err;
  613. if (!wasi_ctx)
  614. return (wasi_errno_t)-1;
  615. if (!validate_native_addr(fd_app, (uint64)sizeof(wasi_fd_t)))
  616. return (wasi_errno_t)-1;
  617. err = wasmtime_ssp_path_open(exec_env, curfds, dirfd, dirflags, path,
  618. path_len, oflags, fs_rights_base,
  619. fs_rights_inheriting, fs_flags, &fd);
  620. *fd_app = fd;
  621. return err;
  622. }
  623. static wasi_errno_t
  624. wasi_fd_readdir(wasm_exec_env_t exec_env, wasi_fd_t fd, void *buf,
  625. uint32 buf_len, wasi_dircookie_t cookie, uint32 *bufused_app)
  626. {
  627. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  628. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  629. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  630. size_t bufused;
  631. wasi_errno_t err;
  632. if (!wasi_ctx)
  633. return (wasi_errno_t)-1;
  634. if (!validate_native_addr(bufused_app, (uint64)sizeof(uint32)))
  635. return (wasi_errno_t)-1;
  636. err = wasmtime_ssp_fd_readdir(exec_env, curfds, fd, buf, buf_len, cookie,
  637. &bufused);
  638. if (err)
  639. return err;
  640. *bufused_app = (uint32)bufused;
  641. return 0;
  642. }
  643. static wasi_errno_t
  644. wasi_path_readlink(wasm_exec_env_t exec_env, wasi_fd_t fd, const char *path,
  645. uint32 path_len, char *buf, uint32 buf_len,
  646. uint32 *bufused_app)
  647. {
  648. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  649. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  650. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  651. size_t bufused;
  652. wasi_errno_t err;
  653. if (!wasi_ctx)
  654. return (wasi_errno_t)-1;
  655. if (!validate_native_addr(bufused_app, (uint64)sizeof(uint32)))
  656. return (wasi_errno_t)-1;
  657. err = wasmtime_ssp_path_readlink(exec_env, curfds, fd, path, path_len, buf,
  658. buf_len, &bufused);
  659. if (err)
  660. return err;
  661. *bufused_app = (uint32)bufused;
  662. return 0;
  663. }
  664. static wasi_errno_t
  665. wasi_path_rename(wasm_exec_env_t exec_env, wasi_fd_t old_fd,
  666. const char *old_path, uint32 old_path_len, wasi_fd_t new_fd,
  667. const char *new_path, uint32 new_path_len)
  668. {
  669. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  670. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  671. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  672. if (!wasi_ctx)
  673. return (wasi_errno_t)-1;
  674. return wasmtime_ssp_path_rename(exec_env, curfds, old_fd, old_path,
  675. old_path_len, new_fd, new_path,
  676. new_path_len);
  677. }
  678. static wasi_errno_t
  679. wasi_fd_filestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
  680. wasi_filestat_t *filestat)
  681. {
  682. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  683. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  684. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  685. if (!wasi_ctx)
  686. return (wasi_errno_t)-1;
  687. if (!validate_native_addr(filestat, (uint64)sizeof(wasi_filestat_t)))
  688. return (wasi_errno_t)-1;
  689. return wasmtime_ssp_fd_filestat_get(exec_env, curfds, fd, filestat);
  690. }
  691. static wasi_errno_t
  692. wasi_fd_filestat_set_times(wasm_exec_env_t exec_env, wasi_fd_t fd,
  693. wasi_timestamp_t st_atim, wasi_timestamp_t st_mtim,
  694. wasi_fstflags_t fstflags)
  695. {
  696. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  697. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  698. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  699. if (!wasi_ctx)
  700. return (wasi_errno_t)-1;
  701. return wasmtime_ssp_fd_filestat_set_times(exec_env, curfds, fd, st_atim,
  702. st_mtim, fstflags);
  703. }
  704. static wasi_errno_t
  705. wasi_fd_filestat_set_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
  706. wasi_filesize_t st_size)
  707. {
  708. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  709. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  710. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  711. if (!wasi_ctx)
  712. return (wasi_errno_t)-1;
  713. return wasmtime_ssp_fd_filestat_set_size(exec_env, curfds, fd, st_size);
  714. }
  715. static wasi_errno_t
  716. wasi_path_filestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
  717. wasi_lookupflags_t flags, const char *path,
  718. uint32 path_len, wasi_filestat_t *filestat)
  719. {
  720. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  721. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  722. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  723. if (!wasi_ctx)
  724. return (wasi_errno_t)-1;
  725. if (!validate_native_addr(filestat, (uint64)sizeof(wasi_filestat_t)))
  726. return (wasi_errno_t)-1;
  727. return wasmtime_ssp_path_filestat_get(exec_env, curfds, fd, flags, path,
  728. path_len, filestat);
  729. }
  730. static wasi_errno_t
  731. wasi_path_filestat_set_times(wasm_exec_env_t exec_env, wasi_fd_t fd,
  732. wasi_lookupflags_t flags, const char *path,
  733. uint32 path_len, wasi_timestamp_t st_atim,
  734. wasi_timestamp_t st_mtim, wasi_fstflags_t fstflags)
  735. {
  736. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  737. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  738. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  739. if (!wasi_ctx)
  740. return (wasi_errno_t)-1;
  741. return wasmtime_ssp_path_filestat_set_times(exec_env, curfds, fd, flags,
  742. path, path_len, st_atim,
  743. st_mtim, fstflags);
  744. }
  745. static wasi_errno_t
  746. wasi_path_symlink(wasm_exec_env_t exec_env, const char *old_path,
  747. uint32 old_path_len, wasi_fd_t fd, const char *new_path,
  748. uint32 new_path_len)
  749. {
  750. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  751. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  752. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  753. struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx);
  754. if (!wasi_ctx)
  755. return (wasi_errno_t)-1;
  756. return wasmtime_ssp_path_symlink(exec_env, curfds, prestats, old_path,
  757. old_path_len, fd, new_path, new_path_len);
  758. }
  759. static wasi_errno_t
  760. wasi_path_unlink_file(wasm_exec_env_t exec_env, wasi_fd_t fd, const char *path,
  761. uint32 path_len)
  762. {
  763. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  764. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  765. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  766. if (!wasi_ctx)
  767. return (wasi_errno_t)-1;
  768. return wasmtime_ssp_path_unlink_file(exec_env, curfds, fd, path, path_len);
  769. }
  770. static wasi_errno_t
  771. wasi_path_remove_directory(wasm_exec_env_t exec_env, wasi_fd_t fd,
  772. const char *path, uint32 path_len)
  773. {
  774. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  775. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  776. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  777. if (!wasi_ctx)
  778. return (wasi_errno_t)-1;
  779. return wasmtime_ssp_path_remove_directory(exec_env, curfds, fd, path,
  780. path_len);
  781. }
  782. #if WASM_ENABLE_THREAD_MGR != 0
  783. static __wasi_timestamp_t
  784. get_timeout_for_poll_oneoff(const wasi_subscription_t *in,
  785. uint32 nsubscriptions)
  786. {
  787. __wasi_timestamp_t timeout = (__wasi_timestamp_t)-1;
  788. uint32 i = 0;
  789. for (i = 0; i < nsubscriptions; ++i) {
  790. const __wasi_subscription_t *s = &in[i];
  791. if (s->u.type == __WASI_EVENTTYPE_CLOCK
  792. && (s->u.u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) == 0) {
  793. timeout = min_uint64(timeout, s->u.u.clock.timeout);
  794. }
  795. }
  796. return timeout;
  797. }
  798. static void
  799. update_clock_subscription_data(wasi_subscription_t *in, uint32 nsubscriptions,
  800. const wasi_timestamp_t new_timeout)
  801. {
  802. uint32 i = 0;
  803. for (i = 0; i < nsubscriptions; ++i) {
  804. __wasi_subscription_t *s = &in[i];
  805. if (s->u.type == __WASI_EVENTTYPE_CLOCK) {
  806. s->u.u.clock.timeout = new_timeout;
  807. }
  808. }
  809. }
  810. static wasi_errno_t
  811. execute_interruptible_poll_oneoff(
  812. #if !defined(WASMTIME_SSP_STATIC_CURFDS)
  813. struct fd_table *curfds,
  814. #endif
  815. const __wasi_subscription_t *in, __wasi_event_t *out, size_t nsubscriptions,
  816. size_t *nevents, wasm_exec_env_t exec_env)
  817. {
  818. if (nsubscriptions == 0) {
  819. *nevents = 0;
  820. return __WASI_ESUCCESS;
  821. }
  822. wasi_errno_t err;
  823. __wasi_timestamp_t elapsed = 0;
  824. bool all_outs_are_type_clock;
  825. uint32 i;
  826. const __wasi_timestamp_t timeout = get_timeout_for_poll_oneoff(
  827. in, (uint32)nsubscriptions),
  828. time_quant = (__wasi_timestamp_t)1e9;
  829. const uint64 size_to_copy =
  830. nsubscriptions * (uint64)sizeof(wasi_subscription_t);
  831. __wasi_subscription_t *in_copy = NULL;
  832. if (size_to_copy >= UINT32_MAX
  833. || !(in_copy = (__wasi_subscription_t *)wasm_runtime_malloc(
  834. (uint32)size_to_copy))) {
  835. return __WASI_ENOMEM;
  836. }
  837. bh_memcpy_s(in_copy, (uint32)size_to_copy, in, (uint32)size_to_copy);
  838. while (timeout == (__wasi_timestamp_t)-1 || elapsed <= timeout) {
  839. /* update timeout for clock subscription events */
  840. update_clock_subscription_data(
  841. in_copy, (uint32)nsubscriptions,
  842. min_uint64(time_quant, timeout - elapsed));
  843. err = wasmtime_ssp_poll_oneoff(exec_env, curfds, in_copy, out,
  844. nsubscriptions, nevents);
  845. elapsed += time_quant;
  846. if (err) {
  847. wasm_runtime_free(in_copy);
  848. return err;
  849. }
  850. if (wasm_cluster_is_thread_terminated(exec_env)) {
  851. wasm_runtime_free(in_copy);
  852. return __WASI_EINTR;
  853. }
  854. else if (*nevents > 0) {
  855. all_outs_are_type_clock = true;
  856. for (i = 0; i < *nevents; i++) {
  857. if (out[i].type != __WASI_EVENTTYPE_CLOCK) {
  858. all_outs_are_type_clock = false;
  859. break;
  860. }
  861. }
  862. if (!all_outs_are_type_clock) {
  863. wasm_runtime_free(in_copy);
  864. return __WASI_ESUCCESS;
  865. }
  866. }
  867. }
  868. wasm_runtime_free(in_copy);
  869. return __WASI_ESUCCESS;
  870. }
  871. #endif
  872. static wasi_errno_t
  873. wasi_poll_oneoff(wasm_exec_env_t exec_env, const wasi_subscription_t *in,
  874. wasi_event_t *out, uint32 nsubscriptions, uint32 *nevents_app)
  875. {
  876. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  877. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  878. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  879. size_t nevents = 0;
  880. wasi_errno_t err;
  881. if (!wasi_ctx)
  882. return (wasi_errno_t)-1;
  883. if (!validate_native_addr((void *)in, (uint64)sizeof(wasi_subscription_t))
  884. || !validate_native_addr(out, (uint64)sizeof(wasi_event_t))
  885. || !validate_native_addr(nevents_app, (uint64)sizeof(uint32)))
  886. return (wasi_errno_t)-1;
  887. #if WASM_ENABLE_THREAD_MGR == 0
  888. err = wasmtime_ssp_poll_oneoff(exec_env, curfds, in, out, nsubscriptions,
  889. &nevents);
  890. #else
  891. err = execute_interruptible_poll_oneoff(curfds, in, out, nsubscriptions,
  892. &nevents, exec_env);
  893. #endif
  894. if (err)
  895. return err;
  896. *nevents_app = (uint32)nevents;
  897. return 0;
  898. }
  899. static void
  900. wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval)
  901. {
  902. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  903. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  904. /* Here throwing exception is just to let wasm app exit,
  905. the upper layer should clear the exception and return
  906. as normal */
  907. wasm_runtime_set_exception(module_inst, "wasi proc exit");
  908. wasi_ctx->exit_code = rval;
  909. }
  910. static wasi_errno_t
  911. wasi_proc_raise(wasm_exec_env_t exec_env, wasi_signal_t sig)
  912. {
  913. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  914. char buf[32];
  915. snprintf(buf, sizeof(buf), "%s%d", "wasi proc raise ", sig);
  916. wasm_runtime_set_exception(module_inst, buf);
  917. return 0;
  918. }
  919. static wasi_errno_t
  920. wasi_random_get(wasm_exec_env_t exec_env, void *buf, uint32 buf_len)
  921. {
  922. (void)exec_env;
  923. return wasmtime_ssp_random_get(buf, buf_len);
  924. }
  925. static wasi_errno_t
  926. wasi_sock_accept(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_fdflags_t flags,
  927. wasi_fd_t *fd_new)
  928. {
  929. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  930. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  931. struct fd_table *curfds = NULL;
  932. if (!wasi_ctx)
  933. return __WASI_EACCES;
  934. curfds = wasi_ctx_get_curfds(wasi_ctx);
  935. return wasi_ssp_sock_accept(exec_env, curfds, fd, flags, fd_new);
  936. }
  937. static wasi_errno_t
  938. wasi_sock_addr_local(wasm_exec_env_t exec_env, wasi_fd_t fd,
  939. __wasi_addr_t *addr)
  940. {
  941. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  942. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  943. struct fd_table *curfds = NULL;
  944. if (!wasi_ctx)
  945. return __WASI_EACCES;
  946. if (!validate_native_addr(addr, (uint64)sizeof(__wasi_addr_t)))
  947. return __WASI_EINVAL;
  948. curfds = wasi_ctx_get_curfds(wasi_ctx);
  949. return wasi_ssp_sock_addr_local(exec_env, curfds, fd, addr);
  950. }
  951. static wasi_errno_t
  952. wasi_sock_addr_remote(wasm_exec_env_t exec_env, wasi_fd_t fd,
  953. __wasi_addr_t *addr)
  954. {
  955. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  956. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  957. struct fd_table *curfds = NULL;
  958. if (!wasi_ctx)
  959. return __WASI_EACCES;
  960. if (!validate_native_addr(addr, (uint64)sizeof(__wasi_addr_t)))
  961. return __WASI_EINVAL;
  962. curfds = wasi_ctx_get_curfds(wasi_ctx);
  963. return wasi_ssp_sock_addr_remote(exec_env, curfds, fd, addr);
  964. }
  965. static wasi_errno_t
  966. wasi_sock_addr_resolve(wasm_exec_env_t exec_env, const char *host,
  967. const char *service, __wasi_addr_info_hints_t *hints,
  968. __wasi_addr_info_t *addr_info,
  969. __wasi_size_t addr_info_size,
  970. __wasi_size_t *max_info_size)
  971. {
  972. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  973. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  974. struct fd_table *curfds = NULL;
  975. char **ns_lookup_list = NULL;
  976. if (!wasi_ctx)
  977. return __WASI_EACCES;
  978. curfds = wasi_ctx_get_curfds(wasi_ctx);
  979. ns_lookup_list = wasi_ctx_get_ns_lookup_list(wasi_ctx);
  980. return wasi_ssp_sock_addr_resolve(exec_env, curfds, ns_lookup_list, host,
  981. service, hints, addr_info, addr_info_size,
  982. max_info_size);
  983. }
  984. static wasi_errno_t
  985. wasi_sock_bind(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr)
  986. {
  987. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  988. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  989. struct fd_table *curfds = NULL;
  990. struct addr_pool *addr_pool = NULL;
  991. if (!wasi_ctx)
  992. return __WASI_EACCES;
  993. curfds = wasi_ctx_get_curfds(wasi_ctx);
  994. addr_pool = wasi_ctx_get_addr_pool(wasi_ctx);
  995. return wasi_ssp_sock_bind(exec_env, curfds, addr_pool, fd, addr);
  996. }
  997. static wasi_errno_t
  998. wasi_sock_close(wasm_exec_env_t exec_env, wasi_fd_t fd)
  999. {
  1000. (void)exec_env;
  1001. (void)fd;
  1002. return __WASI_ENOSYS;
  1003. }
  1004. static wasi_errno_t
  1005. wasi_sock_connect(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr)
  1006. {
  1007. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1008. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1009. struct fd_table *curfds = NULL;
  1010. struct addr_pool *addr_pool = NULL;
  1011. if (!wasi_ctx)
  1012. return __WASI_EACCES;
  1013. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1014. addr_pool = wasi_ctx_get_addr_pool(wasi_ctx);
  1015. return wasi_ssp_sock_connect(exec_env, curfds, addr_pool, fd, addr);
  1016. }
  1017. static wasi_errno_t
  1018. wasi_sock_get_broadcast(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1019. bool *is_enabled)
  1020. {
  1021. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1022. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1023. struct fd_table *curfds = NULL;
  1024. if (!wasi_ctx)
  1025. return __WASI_EACCES;
  1026. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1027. return __WASI_EINVAL;
  1028. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1029. return wasmtime_ssp_sock_get_broadcast(exec_env, curfds, fd, is_enabled);
  1030. }
  1031. static wasi_errno_t
  1032. wasi_sock_get_keep_alive(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1033. bool *is_enabled)
  1034. {
  1035. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1036. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1037. struct fd_table *curfds = NULL;
  1038. if (!wasi_ctx)
  1039. return __WASI_EACCES;
  1040. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1041. return __WASI_EINVAL;
  1042. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1043. return wasmtime_ssp_sock_get_keep_alive(exec_env, curfds, fd, is_enabled);
  1044. }
  1045. static wasi_errno_t
  1046. wasi_sock_get_linger(wasm_exec_env_t exec_env, wasi_fd_t fd, bool *is_enabled,
  1047. int *linger_s)
  1048. {
  1049. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1050. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1051. struct fd_table *curfds = NULL;
  1052. if (!wasi_ctx)
  1053. return __WASI_EACCES;
  1054. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool))
  1055. || !validate_native_addr(linger_s, (uint64)sizeof(int)))
  1056. return __WASI_EINVAL;
  1057. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1058. return wasmtime_ssp_sock_get_linger(exec_env, curfds, fd, is_enabled,
  1059. linger_s);
  1060. }
  1061. static wasi_errno_t
  1062. wasi_sock_get_recv_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1063. size_t *size)
  1064. {
  1065. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1066. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1067. struct fd_table *curfds = NULL;
  1068. if (!wasi_ctx)
  1069. return __WASI_EACCES;
  1070. if (!validate_native_addr(size, (uint64)sizeof(wasi_size_t)))
  1071. return __WASI_EINVAL;
  1072. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1073. return wasmtime_ssp_sock_get_recv_buf_size(exec_env, curfds, fd, size);
  1074. }
  1075. static wasi_errno_t
  1076. wasi_sock_get_recv_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1077. uint64_t *timeout_us)
  1078. {
  1079. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1080. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1081. struct fd_table *curfds = NULL;
  1082. if (!wasi_ctx)
  1083. return __WASI_EACCES;
  1084. if (!validate_native_addr(timeout_us, (uint64)sizeof(uint64_t)))
  1085. return __WASI_EINVAL;
  1086. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1087. return wasmtime_ssp_sock_get_recv_timeout(exec_env, curfds, fd, timeout_us);
  1088. }
  1089. static wasi_errno_t
  1090. wasi_sock_get_reuse_addr(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1091. bool *is_enabled)
  1092. {
  1093. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1094. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1095. struct fd_table *curfds = NULL;
  1096. if (!wasi_ctx)
  1097. return __WASI_EACCES;
  1098. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1099. return __WASI_EINVAL;
  1100. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1101. return wasmtime_ssp_sock_get_reuse_addr(exec_env, curfds, fd, is_enabled);
  1102. }
  1103. static wasi_errno_t
  1104. wasi_sock_get_reuse_port(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1105. bool *is_enabled)
  1106. {
  1107. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1108. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1109. struct fd_table *curfds = NULL;
  1110. if (!wasi_ctx)
  1111. return __WASI_EACCES;
  1112. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1113. return __WASI_EINVAL;
  1114. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1115. return wasmtime_ssp_sock_get_reuse_port(exec_env, curfds, fd, is_enabled);
  1116. }
  1117. static wasi_errno_t
  1118. wasi_sock_get_send_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1119. size_t *size)
  1120. {
  1121. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1122. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1123. struct fd_table *curfds = NULL;
  1124. if (!wasi_ctx)
  1125. return __WASI_EACCES;
  1126. if (!validate_native_addr(size, (uint64)sizeof(__wasi_size_t)))
  1127. return __WASI_EINVAL;
  1128. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1129. return wasmtime_ssp_sock_get_send_buf_size(exec_env, curfds, fd, size);
  1130. }
  1131. static wasi_errno_t
  1132. wasi_sock_get_send_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1133. uint64_t *timeout_us)
  1134. {
  1135. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1136. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1137. struct fd_table *curfds = NULL;
  1138. if (!wasi_ctx)
  1139. return __WASI_EACCES;
  1140. if (!validate_native_addr(timeout_us, (uint64)sizeof(uint64_t)))
  1141. return __WASI_EINVAL;
  1142. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1143. return wasmtime_ssp_sock_get_send_timeout(exec_env, curfds, fd, timeout_us);
  1144. }
  1145. static wasi_errno_t
  1146. wasi_sock_get_tcp_fastopen_connect(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1147. bool *is_enabled)
  1148. {
  1149. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1150. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1151. struct fd_table *curfds = NULL;
  1152. if (!wasi_ctx)
  1153. return __WASI_EACCES;
  1154. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1155. return __WASI_EINVAL;
  1156. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1157. return wasmtime_ssp_sock_get_tcp_fastopen_connect(exec_env, curfds, fd,
  1158. is_enabled);
  1159. }
  1160. static wasi_errno_t
  1161. wasi_sock_get_tcp_no_delay(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1162. bool *is_enabled)
  1163. {
  1164. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1165. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1166. struct fd_table *curfds = NULL;
  1167. if (!wasi_ctx)
  1168. return __WASI_EACCES;
  1169. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1170. return __WASI_EINVAL;
  1171. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1172. return wasmtime_ssp_sock_get_tcp_no_delay(exec_env, curfds, fd, is_enabled);
  1173. }
  1174. static wasi_errno_t
  1175. wasi_sock_get_tcp_quick_ack(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1176. bool *is_enabled)
  1177. {
  1178. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1179. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1180. struct fd_table *curfds = NULL;
  1181. if (!wasi_ctx)
  1182. return __WASI_EACCES;
  1183. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1184. return __WASI_EINVAL;
  1185. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1186. return wasmtime_ssp_sock_get_tcp_quick_ack(exec_env, curfds, fd,
  1187. is_enabled);
  1188. }
  1189. static wasi_errno_t
  1190. wasi_sock_get_tcp_keep_idle(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1191. uint32_t *time_s)
  1192. {
  1193. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1194. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1195. struct fd_table *curfds = NULL;
  1196. if (!wasi_ctx)
  1197. return __WASI_EACCES;
  1198. if (!validate_native_addr(time_s, (uint64)sizeof(uint32_t)))
  1199. return __WASI_EINVAL;
  1200. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1201. return wasmtime_ssp_sock_get_tcp_keep_idle(exec_env, curfds, fd, time_s);
  1202. }
  1203. static wasi_errno_t
  1204. wasi_sock_get_tcp_keep_intvl(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1205. uint32_t *time_s)
  1206. {
  1207. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1208. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1209. struct fd_table *curfds = NULL;
  1210. if (!wasi_ctx)
  1211. return __WASI_EACCES;
  1212. if (!validate_native_addr(time_s, (uint64)sizeof(uint32_t)))
  1213. return __WASI_EINVAL;
  1214. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1215. return wasmtime_ssp_sock_get_tcp_keep_intvl(exec_env, curfds, fd, time_s);
  1216. }
  1217. static wasi_errno_t
  1218. wasi_sock_get_ip_multicast_loop(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1219. bool ipv6, bool *is_enabled)
  1220. {
  1221. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1222. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1223. struct fd_table *curfds = NULL;
  1224. if (!wasi_ctx)
  1225. return __WASI_EACCES;
  1226. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1227. return __WASI_EINVAL;
  1228. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1229. return wasmtime_ssp_sock_get_ip_multicast_loop(exec_env, curfds, fd, ipv6,
  1230. is_enabled);
  1231. }
  1232. static wasi_errno_t
  1233. wasi_sock_get_ip_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd, uint8_t *ttl_s)
  1234. {
  1235. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1236. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1237. struct fd_table *curfds = NULL;
  1238. if (!wasi_ctx)
  1239. return __WASI_EACCES;
  1240. if (!validate_native_addr(ttl_s, (uint64)sizeof(uint8_t)))
  1241. return __WASI_EINVAL;
  1242. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1243. return wasmtime_ssp_sock_get_ip_ttl(exec_env, curfds, fd, ttl_s);
  1244. }
  1245. static wasi_errno_t
  1246. wasi_sock_get_ip_multicast_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1247. uint8_t *ttl_s)
  1248. {
  1249. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1250. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1251. struct fd_table *curfds = NULL;
  1252. if (!wasi_ctx)
  1253. return __WASI_EACCES;
  1254. if (!validate_native_addr(ttl_s, (uint64)sizeof(uint8_t)))
  1255. return __WASI_EINVAL;
  1256. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1257. return wasmtime_ssp_sock_get_ip_multicast_ttl(exec_env, curfds, fd, ttl_s);
  1258. }
  1259. static wasi_errno_t
  1260. wasi_sock_get_ipv6_only(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1261. bool *is_enabled)
  1262. {
  1263. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1264. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1265. struct fd_table *curfds = NULL;
  1266. if (!wasi_ctx)
  1267. return __WASI_EACCES;
  1268. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1269. return __WASI_EINVAL;
  1270. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1271. return wasmtime_ssp_sock_get_ipv6_only(exec_env, curfds, fd, is_enabled);
  1272. }
  1273. static wasi_errno_t
  1274. wasi_sock_listen(wasm_exec_env_t exec_env, wasi_fd_t fd, uint32 backlog)
  1275. {
  1276. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1277. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1278. struct fd_table *curfds = NULL;
  1279. if (!wasi_ctx)
  1280. return __WASI_EACCES;
  1281. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1282. return wasi_ssp_sock_listen(exec_env, curfds, fd, backlog);
  1283. }
  1284. static wasi_errno_t
  1285. wasi_sock_open(wasm_exec_env_t exec_env, wasi_fd_t poolfd,
  1286. wasi_address_family_t af, wasi_sock_type_t socktype,
  1287. wasi_fd_t *sockfd)
  1288. {
  1289. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1290. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1291. struct fd_table *curfds = NULL;
  1292. if (!wasi_ctx)
  1293. return __WASI_EACCES;
  1294. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1295. return wasi_ssp_sock_open(exec_env, curfds, poolfd, af, socktype, sockfd);
  1296. }
  1297. static wasi_errno_t
  1298. wasi_sock_set_broadcast(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled)
  1299. {
  1300. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1301. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1302. struct fd_table *curfds = NULL;
  1303. if (!wasi_ctx)
  1304. return __WASI_EACCES;
  1305. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1306. return wasmtime_ssp_sock_set_broadcast(exec_env, curfds, fd, is_enabled);
  1307. }
  1308. static wasi_errno_t
  1309. wasi_sock_set_keep_alive(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1310. bool is_enabled)
  1311. {
  1312. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1313. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1314. struct fd_table *curfds = NULL;
  1315. if (!wasi_ctx)
  1316. return __WASI_EACCES;
  1317. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1318. return wasmtime_ssp_sock_set_keep_alive(exec_env, curfds, fd, is_enabled);
  1319. }
  1320. static wasi_errno_t
  1321. wasi_sock_set_linger(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled,
  1322. int linger_s)
  1323. {
  1324. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1325. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1326. struct fd_table *curfds = NULL;
  1327. if (!wasi_ctx)
  1328. return __WASI_EACCES;
  1329. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1330. return wasmtime_ssp_sock_set_linger(exec_env, curfds, fd, is_enabled,
  1331. linger_s);
  1332. }
  1333. static wasi_errno_t
  1334. wasi_sock_set_recv_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd, size_t size)
  1335. {
  1336. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1337. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1338. struct fd_table *curfds = NULL;
  1339. if (!wasi_ctx)
  1340. return __WASI_EACCES;
  1341. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1342. return wasmtime_ssp_sock_set_recv_buf_size(exec_env, curfds, fd, size);
  1343. }
  1344. static wasi_errno_t
  1345. wasi_sock_set_recv_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1346. uint64_t timeout_us)
  1347. {
  1348. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1349. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1350. struct fd_table *curfds = NULL;
  1351. if (!wasi_ctx)
  1352. return __WASI_EACCES;
  1353. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1354. return wasmtime_ssp_sock_set_recv_timeout(exec_env, curfds, fd, timeout_us);
  1355. }
  1356. static wasi_errno_t
  1357. wasi_sock_set_reuse_addr(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1358. bool is_enabled)
  1359. {
  1360. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1361. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1362. struct fd_table *curfds = NULL;
  1363. if (!wasi_ctx)
  1364. return __WASI_EACCES;
  1365. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1366. return wasmtime_ssp_sock_set_reuse_addr(exec_env, curfds, fd, is_enabled);
  1367. }
  1368. static wasi_errno_t
  1369. wasi_sock_set_reuse_port(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1370. bool is_enabled)
  1371. {
  1372. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1373. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1374. struct fd_table *curfds = NULL;
  1375. if (!wasi_ctx)
  1376. return __WASI_EACCES;
  1377. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1378. return wasmtime_ssp_sock_set_reuse_port(exec_env, curfds, fd, is_enabled);
  1379. }
  1380. static wasi_errno_t
  1381. wasi_sock_set_send_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd, size_t size)
  1382. {
  1383. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1384. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1385. struct fd_table *curfds = NULL;
  1386. if (!wasi_ctx)
  1387. return __WASI_EACCES;
  1388. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1389. return wasmtime_ssp_sock_set_send_buf_size(exec_env, curfds, fd, size);
  1390. }
  1391. static wasi_errno_t
  1392. wasi_sock_set_send_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1393. uint64_t timeout_us)
  1394. {
  1395. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1396. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1397. struct fd_table *curfds = NULL;
  1398. if (!wasi_ctx)
  1399. return __WASI_EACCES;
  1400. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1401. return wasmtime_ssp_sock_set_send_timeout(exec_env, curfds, fd, timeout_us);
  1402. }
  1403. static wasi_errno_t
  1404. wasi_sock_set_tcp_fastopen_connect(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1405. bool is_enabled)
  1406. {
  1407. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1408. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1409. struct fd_table *curfds = NULL;
  1410. if (!wasi_ctx)
  1411. return __WASI_EACCES;
  1412. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1413. return wasmtime_ssp_sock_set_tcp_fastopen_connect(exec_env, curfds, fd,
  1414. is_enabled);
  1415. }
  1416. static wasi_errno_t
  1417. wasi_sock_set_tcp_no_delay(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1418. bool is_enabled)
  1419. {
  1420. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1421. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1422. struct fd_table *curfds = NULL;
  1423. if (!wasi_ctx)
  1424. return __WASI_EACCES;
  1425. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1426. return wasmtime_ssp_sock_set_tcp_no_delay(exec_env, curfds, fd, is_enabled);
  1427. }
  1428. static wasi_errno_t
  1429. wasi_sock_set_tcp_quick_ack(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1430. bool is_enabled)
  1431. {
  1432. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1433. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1434. struct fd_table *curfds = NULL;
  1435. if (!wasi_ctx)
  1436. return __WASI_EACCES;
  1437. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1438. return wasmtime_ssp_sock_set_tcp_quick_ack(exec_env, curfds, fd,
  1439. is_enabled);
  1440. }
  1441. static wasi_errno_t
  1442. wasi_sock_set_tcp_keep_idle(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1443. uint32_t time_s)
  1444. {
  1445. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1446. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1447. struct fd_table *curfds = NULL;
  1448. if (!wasi_ctx)
  1449. return __WASI_EACCES;
  1450. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1451. return wasmtime_ssp_sock_set_tcp_keep_idle(exec_env, curfds, fd, time_s);
  1452. }
  1453. static wasi_errno_t
  1454. wasi_sock_set_tcp_keep_intvl(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1455. uint32_t time_s)
  1456. {
  1457. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1458. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1459. struct fd_table *curfds = NULL;
  1460. if (!wasi_ctx)
  1461. return __WASI_EACCES;
  1462. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1463. return wasmtime_ssp_sock_set_tcp_keep_intvl(exec_env, curfds, fd, time_s);
  1464. }
  1465. static wasi_errno_t
  1466. wasi_sock_set_ip_multicast_loop(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1467. bool ipv6, bool is_enabled)
  1468. {
  1469. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1470. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1471. struct fd_table *curfds = NULL;
  1472. if (!wasi_ctx)
  1473. return __WASI_EACCES;
  1474. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1475. return wasmtime_ssp_sock_set_ip_multicast_loop(exec_env, curfds, fd, ipv6,
  1476. is_enabled);
  1477. }
  1478. static wasi_errno_t
  1479. wasi_sock_set_ip_add_membership(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1480. __wasi_addr_ip_t *imr_multiaddr,
  1481. uint32_t imr_interface)
  1482. {
  1483. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1484. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1485. struct fd_table *curfds = NULL;
  1486. if (!wasi_ctx)
  1487. return __WASI_EACCES;
  1488. if (!validate_native_addr(imr_multiaddr, (uint64)sizeof(__wasi_addr_ip_t)))
  1489. return __WASI_EINVAL;
  1490. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1491. return wasmtime_ssp_sock_set_ip_add_membership(
  1492. exec_env, curfds, fd, imr_multiaddr, imr_interface);
  1493. }
  1494. static wasi_errno_t
  1495. wasi_sock_set_ip_drop_membership(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1496. __wasi_addr_ip_t *imr_multiaddr,
  1497. uint32_t imr_interface)
  1498. {
  1499. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1500. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1501. struct fd_table *curfds = NULL;
  1502. if (!wasi_ctx)
  1503. return __WASI_EACCES;
  1504. if (!validate_native_addr(imr_multiaddr, (uint64)sizeof(__wasi_addr_ip_t)))
  1505. return __WASI_EINVAL;
  1506. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1507. return wasmtime_ssp_sock_set_ip_drop_membership(
  1508. exec_env, curfds, fd, imr_multiaddr, imr_interface);
  1509. }
  1510. static wasi_errno_t
  1511. wasi_sock_set_ip_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd, uint8_t ttl_s)
  1512. {
  1513. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1514. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1515. struct fd_table *curfds = NULL;
  1516. if (!wasi_ctx)
  1517. return __WASI_EACCES;
  1518. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1519. return wasmtime_ssp_sock_set_ip_ttl(exec_env, curfds, fd, ttl_s);
  1520. }
  1521. static wasi_errno_t
  1522. wasi_sock_set_ip_multicast_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1523. uint8_t ttl_s)
  1524. {
  1525. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1526. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1527. struct fd_table *curfds = NULL;
  1528. if (!wasi_ctx)
  1529. return __WASI_EACCES;
  1530. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1531. return wasmtime_ssp_sock_set_ip_multicast_ttl(exec_env, curfds, fd, ttl_s);
  1532. }
  1533. static wasi_errno_t
  1534. wasi_sock_set_ipv6_only(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled)
  1535. {
  1536. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1537. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1538. struct fd_table *curfds = NULL;
  1539. if (!wasi_ctx)
  1540. return __WASI_EACCES;
  1541. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1542. return wasmtime_ssp_sock_set_ipv6_only(exec_env, curfds, fd, is_enabled);
  1543. }
  1544. static wasi_errno_t
  1545. allocate_iovec_app_buffer(wasm_module_inst_t module_inst,
  1546. const iovec_app_t *data, uint32 data_len,
  1547. uint8 **buf_ptr, uint64 *buf_len)
  1548. {
  1549. uint64 total_size = 0;
  1550. uint32 i;
  1551. uint8 *buf_begin = NULL;
  1552. if (data_len == 0) {
  1553. return __WASI_EINVAL;
  1554. }
  1555. total_size = sizeof(iovec_app_t) * (uint64)data_len;
  1556. if (total_size >= UINT32_MAX
  1557. || !validate_native_addr((void *)data, total_size))
  1558. return __WASI_EINVAL;
  1559. for (total_size = 0, i = 0; i < data_len; i++, data++) {
  1560. total_size += data->buf_len;
  1561. }
  1562. if (total_size == 0) {
  1563. return __WASI_EINVAL;
  1564. }
  1565. if (total_size >= UINT32_MAX
  1566. || !(buf_begin = wasm_runtime_malloc((uint32)total_size))) {
  1567. return __WASI_ENOMEM;
  1568. }
  1569. *buf_len = total_size;
  1570. *buf_ptr = buf_begin;
  1571. return __WASI_ESUCCESS;
  1572. }
  1573. static wasi_errno_t
  1574. copy_buffer_to_iovec_app(wasm_module_inst_t module_inst, uint8 *buf_begin,
  1575. uint32 buf_size, iovec_app_t *data, uint32 data_len,
  1576. uint32 size_to_copy)
  1577. {
  1578. uint8 *buf = buf_begin;
  1579. uint32 i;
  1580. uint32 size_to_copy_into_iovec;
  1581. if (buf_size < size_to_copy) {
  1582. return __WASI_EINVAL;
  1583. }
  1584. for (i = 0; i < data_len; data++, i++) {
  1585. char *native_addr;
  1586. if (!validate_app_addr((uint64)data->buf_offset,
  1587. (uint64)data->buf_len)) {
  1588. return __WASI_EINVAL;
  1589. }
  1590. if (buf >= buf_begin + buf_size
  1591. /* integer overflow */
  1592. || data->buf_len > UINTPTR_MAX - (uintptr_t)buf
  1593. || buf + data->buf_len > buf_begin + buf_size
  1594. || size_to_copy == 0) {
  1595. break;
  1596. }
  1597. /**
  1598. * If our app buffer size is smaller than the amount to be copied,
  1599. * only copy the amount in the app buffer. Otherwise, we fill the iovec
  1600. * buffer and reduce size to copy on the next iteration
  1601. */
  1602. size_to_copy_into_iovec = min_uint32(data->buf_len, size_to_copy);
  1603. native_addr = (void *)addr_app_to_native((uint64)data->buf_offset);
  1604. bh_memcpy_s(native_addr, size_to_copy_into_iovec, buf,
  1605. size_to_copy_into_iovec);
  1606. buf += size_to_copy_into_iovec;
  1607. size_to_copy -= size_to_copy_into_iovec;
  1608. }
  1609. return __WASI_ESUCCESS;
  1610. }
  1611. static wasi_errno_t
  1612. wasi_sock_recv_from(wasm_exec_env_t exec_env, wasi_fd_t sock,
  1613. iovec_app_t *ri_data, uint32 ri_data_len,
  1614. wasi_riflags_t ri_flags, __wasi_addr_t *src_addr,
  1615. uint32 *ro_data_len)
  1616. {
  1617. /**
  1618. * ri_data_len is the length of a list of iovec_app_t, which head is
  1619. * ri_data. ro_data_len is the number of bytes received
  1620. **/
  1621. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1622. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1623. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  1624. uint64 total_size;
  1625. uint8 *buf_begin = NULL;
  1626. wasi_errno_t err;
  1627. size_t recv_bytes = 0;
  1628. if (!wasi_ctx) {
  1629. return __WASI_EINVAL;
  1630. }
  1631. if (!validate_native_addr(ro_data_len, (uint64)sizeof(uint32)))
  1632. return __WASI_EINVAL;
  1633. err = allocate_iovec_app_buffer(module_inst, ri_data, ri_data_len,
  1634. &buf_begin, &total_size);
  1635. if (err != __WASI_ESUCCESS) {
  1636. goto fail;
  1637. }
  1638. memset(buf_begin, 0, total_size);
  1639. *ro_data_len = 0;
  1640. err = wasmtime_ssp_sock_recv_from(exec_env, curfds, sock, buf_begin,
  1641. total_size, ri_flags, src_addr,
  1642. &recv_bytes);
  1643. if (err != __WASI_ESUCCESS) {
  1644. goto fail;
  1645. }
  1646. *ro_data_len = (uint32)recv_bytes;
  1647. err = copy_buffer_to_iovec_app(module_inst, buf_begin, (uint32)total_size,
  1648. ri_data, ri_data_len, (uint32)recv_bytes);
  1649. fail:
  1650. if (buf_begin) {
  1651. wasm_runtime_free(buf_begin);
  1652. }
  1653. return err;
  1654. }
  1655. static wasi_errno_t
  1656. wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data,
  1657. uint32 ri_data_len, wasi_riflags_t ri_flags, uint32 *ro_data_len,
  1658. wasi_roflags_t *ro_flags)
  1659. {
  1660. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1661. __wasi_addr_t src_addr;
  1662. wasi_errno_t error;
  1663. if (!validate_native_addr(ro_flags, (uint64)sizeof(wasi_roflags_t)))
  1664. return __WASI_EINVAL;
  1665. error = wasi_sock_recv_from(exec_env, sock, ri_data, ri_data_len, ri_flags,
  1666. &src_addr, ro_data_len);
  1667. *ro_flags = ri_flags;
  1668. return error;
  1669. }
  1670. static wasi_errno_t
  1671. convert_iovec_app_to_buffer(wasm_module_inst_t module_inst,
  1672. const iovec_app_t *si_data, uint32 si_data_len,
  1673. uint8 **buf_ptr, uint64 *buf_len)
  1674. {
  1675. uint32 i;
  1676. const iovec_app_t *si_data_orig = si_data;
  1677. uint8 *buf = NULL;
  1678. wasi_errno_t error;
  1679. error = allocate_iovec_app_buffer(module_inst, si_data, si_data_len,
  1680. buf_ptr, buf_len);
  1681. if (error != __WASI_ESUCCESS) {
  1682. return error;
  1683. }
  1684. buf = *buf_ptr;
  1685. si_data = si_data_orig;
  1686. for (i = 0; i < si_data_len; i++, si_data++) {
  1687. char *native_addr;
  1688. if (!validate_app_addr((uint64)si_data->buf_offset,
  1689. (uint64)si_data->buf_len)) {
  1690. wasm_runtime_free(*buf_ptr);
  1691. return __WASI_EINVAL;
  1692. }
  1693. native_addr = (char *)addr_app_to_native((uint64)si_data->buf_offset);
  1694. bh_memcpy_s(buf, si_data->buf_len, native_addr, si_data->buf_len);
  1695. buf += si_data->buf_len;
  1696. }
  1697. return __WASI_ESUCCESS;
  1698. }
  1699. static wasi_errno_t
  1700. wasi_sock_send(wasm_exec_env_t exec_env, wasi_fd_t sock,
  1701. const iovec_app_t *si_data, uint32 si_data_len,
  1702. wasi_siflags_t si_flags, uint32 *so_data_len)
  1703. {
  1704. /**
  1705. * si_data_len is the length of a list of iovec_app_t, which head is
  1706. * si_data. so_data_len is the number of bytes sent
  1707. **/
  1708. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1709. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1710. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  1711. uint64 buf_size = 0;
  1712. uint8 *buf = NULL;
  1713. wasi_errno_t err;
  1714. size_t send_bytes = 0;
  1715. if (!wasi_ctx) {
  1716. return __WASI_EINVAL;
  1717. }
  1718. if (!validate_native_addr(so_data_len, (uint64)sizeof(uint32)))
  1719. return __WASI_EINVAL;
  1720. err = convert_iovec_app_to_buffer(module_inst, si_data, si_data_len, &buf,
  1721. &buf_size);
  1722. if (err != __WASI_ESUCCESS)
  1723. return err;
  1724. *so_data_len = 0;
  1725. err = wasmtime_ssp_sock_send(exec_env, curfds, sock, buf, buf_size,
  1726. &send_bytes);
  1727. *so_data_len = (uint32)send_bytes;
  1728. wasm_runtime_free(buf);
  1729. return err;
  1730. }
  1731. static wasi_errno_t
  1732. wasi_sock_send_to(wasm_exec_env_t exec_env, wasi_fd_t sock,
  1733. const iovec_app_t *si_data, uint32 si_data_len,
  1734. wasi_siflags_t si_flags, const __wasi_addr_t *dest_addr,
  1735. uint32 *so_data_len)
  1736. {
  1737. /**
  1738. * si_data_len is the length of a list of iovec_app_t, which head is
  1739. * si_data. so_data_len is the number of bytes sent
  1740. **/
  1741. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1742. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1743. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  1744. uint64 buf_size = 0;
  1745. uint8 *buf = NULL;
  1746. wasi_errno_t err;
  1747. size_t send_bytes = 0;
  1748. struct addr_pool *addr_pool = wasi_ctx_get_addr_pool(wasi_ctx);
  1749. if (!wasi_ctx) {
  1750. return __WASI_EINVAL;
  1751. }
  1752. if (!validate_native_addr(so_data_len, (uint64)sizeof(uint32)))
  1753. return __WASI_EINVAL;
  1754. err = convert_iovec_app_to_buffer(module_inst, si_data, si_data_len, &buf,
  1755. &buf_size);
  1756. if (err != __WASI_ESUCCESS)
  1757. return err;
  1758. *so_data_len = 0;
  1759. err = wasmtime_ssp_sock_send_to(exec_env, curfds, addr_pool, sock, buf,
  1760. buf_size, si_flags, dest_addr, &send_bytes);
  1761. *so_data_len = (uint32)send_bytes;
  1762. wasm_runtime_free(buf);
  1763. return err;
  1764. }
  1765. static wasi_errno_t
  1766. wasi_sock_shutdown(wasm_exec_env_t exec_env, wasi_fd_t sock, wasi_sdflags_t how)
  1767. {
  1768. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1769. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1770. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  1771. if (!wasi_ctx)
  1772. return __WASI_EINVAL;
  1773. return wasmtime_ssp_sock_shutdown(exec_env, curfds, sock);
  1774. }
  1775. static wasi_errno_t
  1776. wasi_sched_yield(wasm_exec_env_t exec_env)
  1777. {
  1778. (void)exec_env;
  1779. return wasmtime_ssp_sched_yield();
  1780. }
  1781. /* clang-format off */
  1782. #define REG_NATIVE_FUNC(func_name, signature) \
  1783. { #func_name, wasi_##func_name, signature, NULL }
  1784. /* clang-format on */
  1785. static NativeSymbol native_symbols_libc_wasi[] = {
  1786. REG_NATIVE_FUNC(args_get, "(**)i"),
  1787. REG_NATIVE_FUNC(args_sizes_get, "(**)i"),
  1788. REG_NATIVE_FUNC(clock_res_get, "(i*)i"),
  1789. REG_NATIVE_FUNC(clock_time_get, "(iI*)i"),
  1790. REG_NATIVE_FUNC(environ_get, "(**)i"),
  1791. REG_NATIVE_FUNC(environ_sizes_get, "(**)i"),
  1792. REG_NATIVE_FUNC(fd_prestat_get, "(i*)i"),
  1793. REG_NATIVE_FUNC(fd_prestat_dir_name, "(i*~)i"),
  1794. REG_NATIVE_FUNC(fd_close, "(i)i"),
  1795. REG_NATIVE_FUNC(fd_datasync, "(i)i"),
  1796. REG_NATIVE_FUNC(fd_pread, "(i*iI*)i"),
  1797. REG_NATIVE_FUNC(fd_pwrite, "(i*iI*)i"),
  1798. REG_NATIVE_FUNC(fd_read, "(i*i*)i"),
  1799. REG_NATIVE_FUNC(fd_renumber, "(ii)i"),
  1800. REG_NATIVE_FUNC(fd_seek, "(iIi*)i"),
  1801. REG_NATIVE_FUNC(fd_tell, "(i*)i"),
  1802. REG_NATIVE_FUNC(fd_fdstat_get, "(i*)i"),
  1803. REG_NATIVE_FUNC(fd_fdstat_set_flags, "(ii)i"),
  1804. REG_NATIVE_FUNC(fd_fdstat_set_rights, "(iII)i"),
  1805. REG_NATIVE_FUNC(fd_sync, "(i)i"),
  1806. REG_NATIVE_FUNC(fd_write, "(i*i*)i"),
  1807. REG_NATIVE_FUNC(fd_advise, "(iIIi)i"),
  1808. REG_NATIVE_FUNC(fd_allocate, "(iII)i"),
  1809. REG_NATIVE_FUNC(path_create_directory, "(i*~)i"),
  1810. REG_NATIVE_FUNC(path_link, "(ii*~i*~)i"),
  1811. REG_NATIVE_FUNC(path_open, "(ii*~iIIi*)i"),
  1812. REG_NATIVE_FUNC(fd_readdir, "(i*~I*)i"),
  1813. REG_NATIVE_FUNC(path_readlink, "(i*~*~*)i"),
  1814. REG_NATIVE_FUNC(path_rename, "(i*~i*~)i"),
  1815. REG_NATIVE_FUNC(fd_filestat_get, "(i*)i"),
  1816. REG_NATIVE_FUNC(fd_filestat_set_times, "(iIIi)i"),
  1817. REG_NATIVE_FUNC(fd_filestat_set_size, "(iI)i"),
  1818. REG_NATIVE_FUNC(path_filestat_get, "(ii*~*)i"),
  1819. REG_NATIVE_FUNC(path_filestat_set_times, "(ii*~IIi)i"),
  1820. REG_NATIVE_FUNC(path_symlink, "(*~i*~)i"),
  1821. REG_NATIVE_FUNC(path_unlink_file, "(i*~)i"),
  1822. REG_NATIVE_FUNC(path_remove_directory, "(i*~)i"),
  1823. REG_NATIVE_FUNC(poll_oneoff, "(**i*)i"),
  1824. REG_NATIVE_FUNC(proc_exit, "(i)"),
  1825. REG_NATIVE_FUNC(proc_raise, "(i)i"),
  1826. REG_NATIVE_FUNC(random_get, "(*~)i"),
  1827. REG_NATIVE_FUNC(sock_accept, "(ii*)i"),
  1828. REG_NATIVE_FUNC(sock_addr_local, "(i*)i"),
  1829. REG_NATIVE_FUNC(sock_addr_remote, "(i*)i"),
  1830. REG_NATIVE_FUNC(sock_addr_resolve, "($$**i*)i"),
  1831. REG_NATIVE_FUNC(sock_bind, "(i*)i"),
  1832. REG_NATIVE_FUNC(sock_close, "(i)i"),
  1833. REG_NATIVE_FUNC(sock_connect, "(i*)i"),
  1834. REG_NATIVE_FUNC(sock_get_broadcast, "(i*)i"),
  1835. REG_NATIVE_FUNC(sock_get_keep_alive, "(i*)i"),
  1836. REG_NATIVE_FUNC(sock_get_linger, "(i**)i"),
  1837. REG_NATIVE_FUNC(sock_get_recv_buf_size, "(i*)i"),
  1838. REG_NATIVE_FUNC(sock_get_recv_timeout, "(i*)i"),
  1839. REG_NATIVE_FUNC(sock_get_reuse_addr, "(i*)i"),
  1840. REG_NATIVE_FUNC(sock_get_reuse_port, "(i*)i"),
  1841. REG_NATIVE_FUNC(sock_get_send_buf_size, "(i*)i"),
  1842. REG_NATIVE_FUNC(sock_get_send_timeout, "(i*)i"),
  1843. REG_NATIVE_FUNC(sock_get_tcp_fastopen_connect, "(i*)i"),
  1844. REG_NATIVE_FUNC(sock_get_tcp_keep_idle, "(i*)i"),
  1845. REG_NATIVE_FUNC(sock_get_tcp_keep_intvl, "(i*)i"),
  1846. REG_NATIVE_FUNC(sock_get_tcp_no_delay, "(i*)i"),
  1847. REG_NATIVE_FUNC(sock_get_tcp_quick_ack, "(i*)i"),
  1848. REG_NATIVE_FUNC(sock_get_ip_multicast_loop, "(ii*)i"),
  1849. REG_NATIVE_FUNC(sock_get_ip_multicast_ttl, "(i*)i"),
  1850. REG_NATIVE_FUNC(sock_get_ip_ttl, "(i*)i"),
  1851. REG_NATIVE_FUNC(sock_get_ipv6_only, "(i*)i"),
  1852. REG_NATIVE_FUNC(sock_listen, "(ii)i"),
  1853. REG_NATIVE_FUNC(sock_open, "(iii*)i"),
  1854. REG_NATIVE_FUNC(sock_recv, "(i*ii**)i"),
  1855. REG_NATIVE_FUNC(sock_recv_from, "(i*ii**)i"),
  1856. REG_NATIVE_FUNC(sock_send, "(i*ii*)i"),
  1857. REG_NATIVE_FUNC(sock_send_to, "(i*ii**)i"),
  1858. REG_NATIVE_FUNC(sock_set_broadcast, "(ii)i"),
  1859. REG_NATIVE_FUNC(sock_set_keep_alive, "(ii)i"),
  1860. REG_NATIVE_FUNC(sock_set_linger, "(iii)i"),
  1861. REG_NATIVE_FUNC(sock_set_recv_buf_size, "(ii)i"),
  1862. REG_NATIVE_FUNC(sock_set_recv_timeout, "(iI)i"),
  1863. REG_NATIVE_FUNC(sock_set_reuse_addr, "(ii)i"),
  1864. REG_NATIVE_FUNC(sock_set_reuse_port, "(ii)i"),
  1865. REG_NATIVE_FUNC(sock_set_send_buf_size, "(ii)i"),
  1866. REG_NATIVE_FUNC(sock_set_send_timeout, "(iI)i"),
  1867. REG_NATIVE_FUNC(sock_set_tcp_fastopen_connect, "(ii)i"),
  1868. REG_NATIVE_FUNC(sock_set_tcp_keep_idle, "(ii)i"),
  1869. REG_NATIVE_FUNC(sock_set_tcp_keep_intvl, "(ii)i"),
  1870. REG_NATIVE_FUNC(sock_set_tcp_no_delay, "(ii)i"),
  1871. REG_NATIVE_FUNC(sock_set_tcp_quick_ack, "(ii)i"),
  1872. REG_NATIVE_FUNC(sock_set_ip_multicast_loop, "(iii)i"),
  1873. REG_NATIVE_FUNC(sock_set_ip_multicast_ttl, "(ii)i"),
  1874. REG_NATIVE_FUNC(sock_set_ip_add_membership, "(i*i)i"),
  1875. REG_NATIVE_FUNC(sock_set_ip_drop_membership, "(i*i)i"),
  1876. REG_NATIVE_FUNC(sock_set_ip_ttl, "(ii)i"),
  1877. REG_NATIVE_FUNC(sock_set_ipv6_only, "(ii)i"),
  1878. REG_NATIVE_FUNC(sock_shutdown, "(ii)i"),
  1879. REG_NATIVE_FUNC(sched_yield, "()i"),
  1880. };
  1881. uint32
  1882. get_libc_wasi_export_apis(NativeSymbol **p_libc_wasi_apis)
  1883. {
  1884. *p_libc_wasi_apis = native_symbols_libc_wasi;
  1885. return sizeof(native_symbols_libc_wasi) / sizeof(NativeSymbol);
  1886. }