libc_wasi_wrapper.c 72 KB

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