libc_wasi_wrapper.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  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, nsubscriptions),
  828. time_quant = 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, size_to_copy, in, 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, nsubscriptions, min_uint64(time_quant, timeout - elapsed));
  842. err = wasmtime_ssp_poll_oneoff(exec_env, curfds, in_copy, out,
  843. nsubscriptions, nevents);
  844. elapsed += time_quant;
  845. if (err) {
  846. wasm_runtime_free(in_copy);
  847. return err;
  848. }
  849. if (wasm_cluster_is_thread_terminated(exec_env)) {
  850. wasm_runtime_free(in_copy);
  851. return __WASI_EINTR;
  852. }
  853. else if (*nevents > 0) {
  854. all_outs_are_type_clock = true;
  855. for (i = 0; i < *nevents; i++) {
  856. if (out[i].type != __WASI_EVENTTYPE_CLOCK) {
  857. all_outs_are_type_clock = false;
  858. break;
  859. }
  860. }
  861. if (!all_outs_are_type_clock) {
  862. wasm_runtime_free(in_copy);
  863. return __WASI_ESUCCESS;
  864. }
  865. }
  866. }
  867. wasm_runtime_free(in_copy);
  868. return __WASI_ESUCCESS;
  869. }
  870. #endif
  871. static wasi_errno_t
  872. wasi_poll_oneoff(wasm_exec_env_t exec_env, const wasi_subscription_t *in,
  873. wasi_event_t *out, uint32 nsubscriptions, uint32 *nevents_app)
  874. {
  875. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  876. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  877. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  878. size_t nevents = 0;
  879. wasi_errno_t err;
  880. if (!wasi_ctx)
  881. return (wasi_errno_t)-1;
  882. if (!validate_native_addr((void *)in, (uint64)sizeof(wasi_subscription_t))
  883. || !validate_native_addr(out, (uint64)sizeof(wasi_event_t))
  884. || !validate_native_addr(nevents_app, (uint64)sizeof(uint32)))
  885. return (wasi_errno_t)-1;
  886. #if WASM_ENABLE_THREAD_MGR == 0
  887. err = wasmtime_ssp_poll_oneoff(exec_env, curfds, in, out, nsubscriptions,
  888. &nevents);
  889. #else
  890. err = execute_interruptible_poll_oneoff(curfds, in, out, nsubscriptions,
  891. &nevents, exec_env);
  892. #endif
  893. if (err)
  894. return err;
  895. *nevents_app = (uint32)nevents;
  896. return 0;
  897. }
  898. static void
  899. wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval)
  900. {
  901. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  902. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  903. /* Here throwing exception is just to let wasm app exit,
  904. the upper layer should clear the exception and return
  905. as normal */
  906. wasm_runtime_set_exception(module_inst, "wasi proc exit");
  907. wasi_ctx->exit_code = rval;
  908. }
  909. static wasi_errno_t
  910. wasi_proc_raise(wasm_exec_env_t exec_env, wasi_signal_t sig)
  911. {
  912. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  913. char buf[32];
  914. snprintf(buf, sizeof(buf), "%s%d", "wasi proc raise ", sig);
  915. wasm_runtime_set_exception(module_inst, buf);
  916. return 0;
  917. }
  918. static wasi_errno_t
  919. wasi_random_get(wasm_exec_env_t exec_env, void *buf, uint32 buf_len)
  920. {
  921. (void)exec_env;
  922. return wasmtime_ssp_random_get(buf, buf_len);
  923. }
  924. static wasi_errno_t
  925. wasi_sock_accept(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_fdflags_t flags,
  926. wasi_fd_t *fd_new)
  927. {
  928. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  929. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  930. struct fd_table *curfds = NULL;
  931. if (!wasi_ctx)
  932. return __WASI_EACCES;
  933. curfds = wasi_ctx_get_curfds(wasi_ctx);
  934. return wasi_ssp_sock_accept(exec_env, curfds, fd, flags, fd_new);
  935. }
  936. static wasi_errno_t
  937. wasi_sock_addr_local(wasm_exec_env_t exec_env, wasi_fd_t fd,
  938. __wasi_addr_t *addr)
  939. {
  940. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  941. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  942. struct fd_table *curfds = NULL;
  943. if (!wasi_ctx)
  944. return __WASI_EACCES;
  945. if (!validate_native_addr(addr, (uint64)sizeof(__wasi_addr_t)))
  946. return __WASI_EINVAL;
  947. curfds = wasi_ctx_get_curfds(wasi_ctx);
  948. return wasi_ssp_sock_addr_local(exec_env, curfds, fd, addr);
  949. }
  950. static wasi_errno_t
  951. wasi_sock_addr_remote(wasm_exec_env_t exec_env, wasi_fd_t fd,
  952. __wasi_addr_t *addr)
  953. {
  954. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  955. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  956. struct fd_table *curfds = NULL;
  957. if (!wasi_ctx)
  958. return __WASI_EACCES;
  959. if (!validate_native_addr(addr, (uint64)sizeof(__wasi_addr_t)))
  960. return __WASI_EINVAL;
  961. curfds = wasi_ctx_get_curfds(wasi_ctx);
  962. return wasi_ssp_sock_addr_remote(exec_env, curfds, fd, addr);
  963. }
  964. static wasi_errno_t
  965. wasi_sock_addr_resolve(wasm_exec_env_t exec_env, const char *host,
  966. const char *service, __wasi_addr_info_hints_t *hints,
  967. __wasi_addr_info_t *addr_info,
  968. __wasi_size_t addr_info_size,
  969. __wasi_size_t *max_info_size)
  970. {
  971. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  972. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  973. struct fd_table *curfds = NULL;
  974. char **ns_lookup_list = NULL;
  975. if (!wasi_ctx)
  976. return __WASI_EACCES;
  977. curfds = wasi_ctx_get_curfds(wasi_ctx);
  978. ns_lookup_list = wasi_ctx_get_ns_lookup_list(wasi_ctx);
  979. return wasi_ssp_sock_addr_resolve(exec_env, curfds, ns_lookup_list, host,
  980. service, hints, addr_info, addr_info_size,
  981. max_info_size);
  982. }
  983. static wasi_errno_t
  984. wasi_sock_bind(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr)
  985. {
  986. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  987. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  988. struct fd_table *curfds = NULL;
  989. struct addr_pool *addr_pool = NULL;
  990. if (!wasi_ctx)
  991. return __WASI_EACCES;
  992. curfds = wasi_ctx_get_curfds(wasi_ctx);
  993. addr_pool = wasi_ctx_get_addr_pool(wasi_ctx);
  994. return wasi_ssp_sock_bind(exec_env, curfds, addr_pool, fd, addr);
  995. }
  996. static wasi_errno_t
  997. wasi_sock_close(wasm_exec_env_t exec_env, wasi_fd_t fd)
  998. {
  999. (void)exec_env;
  1000. (void)fd;
  1001. return __WASI_ENOSYS;
  1002. }
  1003. static wasi_errno_t
  1004. wasi_sock_connect(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr)
  1005. {
  1006. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1007. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1008. struct fd_table *curfds = NULL;
  1009. struct addr_pool *addr_pool = NULL;
  1010. if (!wasi_ctx)
  1011. return __WASI_EACCES;
  1012. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1013. addr_pool = wasi_ctx_get_addr_pool(wasi_ctx);
  1014. return wasi_ssp_sock_connect(exec_env, curfds, addr_pool, fd, addr);
  1015. }
  1016. static wasi_errno_t
  1017. wasi_sock_get_broadcast(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1018. bool *is_enabled)
  1019. {
  1020. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1021. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1022. struct fd_table *curfds = NULL;
  1023. if (!wasi_ctx)
  1024. return __WASI_EACCES;
  1025. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1026. return __WASI_EINVAL;
  1027. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1028. return wasmtime_ssp_sock_get_broadcast(exec_env, curfds, fd, is_enabled);
  1029. }
  1030. static wasi_errno_t
  1031. wasi_sock_get_keep_alive(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1032. bool *is_enabled)
  1033. {
  1034. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1035. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1036. struct fd_table *curfds = NULL;
  1037. if (!wasi_ctx)
  1038. return __WASI_EACCES;
  1039. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1040. return __WASI_EINVAL;
  1041. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1042. return wasmtime_ssp_sock_get_keep_alive(exec_env, curfds, fd, is_enabled);
  1043. }
  1044. static wasi_errno_t
  1045. wasi_sock_get_linger(wasm_exec_env_t exec_env, wasi_fd_t fd, bool *is_enabled,
  1046. int *linger_s)
  1047. {
  1048. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1049. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1050. struct fd_table *curfds = NULL;
  1051. if (!wasi_ctx)
  1052. return __WASI_EACCES;
  1053. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool))
  1054. || !validate_native_addr(linger_s, (uint64)sizeof(int)))
  1055. return __WASI_EINVAL;
  1056. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1057. return wasmtime_ssp_sock_get_linger(exec_env, curfds, fd, is_enabled,
  1058. linger_s);
  1059. }
  1060. static wasi_errno_t
  1061. wasi_sock_get_recv_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1062. size_t *size)
  1063. {
  1064. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1065. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1066. struct fd_table *curfds = NULL;
  1067. if (!wasi_ctx)
  1068. return __WASI_EACCES;
  1069. if (!validate_native_addr(size, (uint64)sizeof(wasi_size_t)))
  1070. return __WASI_EINVAL;
  1071. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1072. return wasmtime_ssp_sock_get_recv_buf_size(exec_env, curfds, fd, size);
  1073. }
  1074. static wasi_errno_t
  1075. wasi_sock_get_recv_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1076. uint64_t *timeout_us)
  1077. {
  1078. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1079. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1080. struct fd_table *curfds = NULL;
  1081. if (!wasi_ctx)
  1082. return __WASI_EACCES;
  1083. if (!validate_native_addr(timeout_us, (uint64)sizeof(uint64_t)))
  1084. return __WASI_EINVAL;
  1085. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1086. return wasmtime_ssp_sock_get_recv_timeout(exec_env, curfds, fd, timeout_us);
  1087. }
  1088. static wasi_errno_t
  1089. wasi_sock_get_reuse_addr(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1090. bool *is_enabled)
  1091. {
  1092. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1093. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1094. struct fd_table *curfds = NULL;
  1095. if (!wasi_ctx)
  1096. return __WASI_EACCES;
  1097. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1098. return __WASI_EINVAL;
  1099. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1100. return wasmtime_ssp_sock_get_reuse_addr(exec_env, curfds, fd, is_enabled);
  1101. }
  1102. static wasi_errno_t
  1103. wasi_sock_get_reuse_port(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1104. bool *is_enabled)
  1105. {
  1106. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1107. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1108. struct fd_table *curfds = NULL;
  1109. if (!wasi_ctx)
  1110. return __WASI_EACCES;
  1111. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1112. return __WASI_EINVAL;
  1113. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1114. return wasmtime_ssp_sock_get_reuse_port(exec_env, curfds, fd, is_enabled);
  1115. }
  1116. static wasi_errno_t
  1117. wasi_sock_get_send_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1118. size_t *size)
  1119. {
  1120. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1121. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1122. struct fd_table *curfds = NULL;
  1123. if (!wasi_ctx)
  1124. return __WASI_EACCES;
  1125. if (!validate_native_addr(size, (uint64)sizeof(__wasi_size_t)))
  1126. return __WASI_EINVAL;
  1127. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1128. return wasmtime_ssp_sock_get_send_buf_size(exec_env, curfds, fd, size);
  1129. }
  1130. static wasi_errno_t
  1131. wasi_sock_get_send_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1132. uint64_t *timeout_us)
  1133. {
  1134. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1135. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1136. struct fd_table *curfds = NULL;
  1137. if (!wasi_ctx)
  1138. return __WASI_EACCES;
  1139. if (!validate_native_addr(timeout_us, (uint64)sizeof(uint64_t)))
  1140. return __WASI_EINVAL;
  1141. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1142. return wasmtime_ssp_sock_get_send_timeout(exec_env, curfds, fd, timeout_us);
  1143. }
  1144. static wasi_errno_t
  1145. wasi_sock_get_tcp_fastopen_connect(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1146. bool *is_enabled)
  1147. {
  1148. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1149. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1150. struct fd_table *curfds = NULL;
  1151. if (!wasi_ctx)
  1152. return __WASI_EACCES;
  1153. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1154. return __WASI_EINVAL;
  1155. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1156. return wasmtime_ssp_sock_get_tcp_fastopen_connect(exec_env, curfds, fd,
  1157. is_enabled);
  1158. }
  1159. static wasi_errno_t
  1160. wasi_sock_get_tcp_no_delay(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1161. bool *is_enabled)
  1162. {
  1163. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1164. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1165. struct fd_table *curfds = NULL;
  1166. if (!wasi_ctx)
  1167. return __WASI_EACCES;
  1168. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1169. return __WASI_EINVAL;
  1170. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1171. return wasmtime_ssp_sock_get_tcp_no_delay(exec_env, curfds, fd, is_enabled);
  1172. }
  1173. static wasi_errno_t
  1174. wasi_sock_get_tcp_quick_ack(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1175. bool *is_enabled)
  1176. {
  1177. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1178. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1179. struct fd_table *curfds = NULL;
  1180. if (!wasi_ctx)
  1181. return __WASI_EACCES;
  1182. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1183. return __WASI_EINVAL;
  1184. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1185. return wasmtime_ssp_sock_get_tcp_quick_ack(exec_env, curfds, fd,
  1186. is_enabled);
  1187. }
  1188. static wasi_errno_t
  1189. wasi_sock_get_tcp_keep_idle(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1190. uint32_t *time_s)
  1191. {
  1192. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1193. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1194. struct fd_table *curfds = NULL;
  1195. if (!wasi_ctx)
  1196. return __WASI_EACCES;
  1197. if (!validate_native_addr(time_s, (uint64)sizeof(uint32_t)))
  1198. return __WASI_EINVAL;
  1199. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1200. return wasmtime_ssp_sock_get_tcp_keep_idle(exec_env, curfds, fd, time_s);
  1201. }
  1202. static wasi_errno_t
  1203. wasi_sock_get_tcp_keep_intvl(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1204. uint32_t *time_s)
  1205. {
  1206. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1207. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1208. struct fd_table *curfds = NULL;
  1209. if (!wasi_ctx)
  1210. return __WASI_EACCES;
  1211. if (!validate_native_addr(time_s, (uint64)sizeof(uint32_t)))
  1212. return __WASI_EINVAL;
  1213. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1214. return wasmtime_ssp_sock_get_tcp_keep_intvl(exec_env, curfds, fd, time_s);
  1215. }
  1216. static wasi_errno_t
  1217. wasi_sock_get_ip_multicast_loop(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1218. bool ipv6, bool *is_enabled)
  1219. {
  1220. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1221. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1222. struct fd_table *curfds = NULL;
  1223. if (!wasi_ctx)
  1224. return __WASI_EACCES;
  1225. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1226. return __WASI_EINVAL;
  1227. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1228. return wasmtime_ssp_sock_get_ip_multicast_loop(exec_env, curfds, fd, ipv6,
  1229. is_enabled);
  1230. }
  1231. static wasi_errno_t
  1232. wasi_sock_get_ip_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd, uint8_t *ttl_s)
  1233. {
  1234. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1235. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1236. struct fd_table *curfds = NULL;
  1237. if (!wasi_ctx)
  1238. return __WASI_EACCES;
  1239. if (!validate_native_addr(ttl_s, (uint64)sizeof(uint8_t)))
  1240. return __WASI_EINVAL;
  1241. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1242. return wasmtime_ssp_sock_get_ip_ttl(exec_env, curfds, fd, ttl_s);
  1243. }
  1244. static wasi_errno_t
  1245. wasi_sock_get_ip_multicast_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1246. uint8_t *ttl_s)
  1247. {
  1248. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1249. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1250. struct fd_table *curfds = NULL;
  1251. if (!wasi_ctx)
  1252. return __WASI_EACCES;
  1253. if (!validate_native_addr(ttl_s, (uint64)sizeof(uint8_t)))
  1254. return __WASI_EINVAL;
  1255. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1256. return wasmtime_ssp_sock_get_ip_multicast_ttl(exec_env, curfds, fd, ttl_s);
  1257. }
  1258. static wasi_errno_t
  1259. wasi_sock_get_ipv6_only(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1260. bool *is_enabled)
  1261. {
  1262. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1263. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1264. struct fd_table *curfds = NULL;
  1265. if (!wasi_ctx)
  1266. return __WASI_EACCES;
  1267. if (!validate_native_addr(is_enabled, (uint64)sizeof(bool)))
  1268. return __WASI_EINVAL;
  1269. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1270. return wasmtime_ssp_sock_get_ipv6_only(exec_env, curfds, fd, is_enabled);
  1271. }
  1272. static wasi_errno_t
  1273. wasi_sock_listen(wasm_exec_env_t exec_env, wasi_fd_t fd, uint32 backlog)
  1274. {
  1275. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1276. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1277. struct fd_table *curfds = NULL;
  1278. if (!wasi_ctx)
  1279. return __WASI_EACCES;
  1280. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1281. return wasi_ssp_sock_listen(exec_env, curfds, fd, backlog);
  1282. }
  1283. static wasi_errno_t
  1284. wasi_sock_open(wasm_exec_env_t exec_env, wasi_fd_t poolfd,
  1285. wasi_address_family_t af, wasi_sock_type_t socktype,
  1286. wasi_fd_t *sockfd)
  1287. {
  1288. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1289. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1290. struct fd_table *curfds = NULL;
  1291. if (!wasi_ctx)
  1292. return __WASI_EACCES;
  1293. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1294. return wasi_ssp_sock_open(exec_env, curfds, poolfd, af, socktype, sockfd);
  1295. }
  1296. static wasi_errno_t
  1297. wasi_sock_set_broadcast(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled)
  1298. {
  1299. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1300. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1301. struct fd_table *curfds = NULL;
  1302. if (!wasi_ctx)
  1303. return __WASI_EACCES;
  1304. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1305. return wasmtime_ssp_sock_set_broadcast(exec_env, curfds, fd, is_enabled);
  1306. }
  1307. static wasi_errno_t
  1308. wasi_sock_set_keep_alive(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1309. bool is_enabled)
  1310. {
  1311. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1312. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1313. struct fd_table *curfds = NULL;
  1314. if (!wasi_ctx)
  1315. return __WASI_EACCES;
  1316. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1317. return wasmtime_ssp_sock_set_keep_alive(exec_env, curfds, fd, is_enabled);
  1318. }
  1319. static wasi_errno_t
  1320. wasi_sock_set_linger(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled,
  1321. int linger_s)
  1322. {
  1323. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1324. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1325. struct fd_table *curfds = NULL;
  1326. if (!wasi_ctx)
  1327. return __WASI_EACCES;
  1328. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1329. return wasmtime_ssp_sock_set_linger(exec_env, curfds, fd, is_enabled,
  1330. linger_s);
  1331. }
  1332. static wasi_errno_t
  1333. wasi_sock_set_recv_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd, size_t size)
  1334. {
  1335. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1336. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1337. struct fd_table *curfds = NULL;
  1338. if (!wasi_ctx)
  1339. return __WASI_EACCES;
  1340. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1341. return wasmtime_ssp_sock_set_recv_buf_size(exec_env, curfds, fd, size);
  1342. }
  1343. static wasi_errno_t
  1344. wasi_sock_set_recv_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1345. uint64_t timeout_us)
  1346. {
  1347. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1348. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1349. struct fd_table *curfds = NULL;
  1350. if (!wasi_ctx)
  1351. return __WASI_EACCES;
  1352. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1353. return wasmtime_ssp_sock_set_recv_timeout(exec_env, curfds, fd, timeout_us);
  1354. }
  1355. static wasi_errno_t
  1356. wasi_sock_set_reuse_addr(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1357. bool is_enabled)
  1358. {
  1359. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1360. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1361. struct fd_table *curfds = NULL;
  1362. if (!wasi_ctx)
  1363. return __WASI_EACCES;
  1364. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1365. return wasmtime_ssp_sock_set_reuse_addr(exec_env, curfds, fd, is_enabled);
  1366. }
  1367. static wasi_errno_t
  1368. wasi_sock_set_reuse_port(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1369. bool is_enabled)
  1370. {
  1371. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1372. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1373. struct fd_table *curfds = NULL;
  1374. if (!wasi_ctx)
  1375. return __WASI_EACCES;
  1376. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1377. return wasmtime_ssp_sock_set_reuse_port(exec_env, curfds, fd, is_enabled);
  1378. }
  1379. static wasi_errno_t
  1380. wasi_sock_set_send_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd, size_t size)
  1381. {
  1382. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1383. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1384. struct fd_table *curfds = NULL;
  1385. if (!wasi_ctx)
  1386. return __WASI_EACCES;
  1387. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1388. return wasmtime_ssp_sock_set_send_buf_size(exec_env, curfds, fd, size);
  1389. }
  1390. static wasi_errno_t
  1391. wasi_sock_set_send_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1392. uint64_t timeout_us)
  1393. {
  1394. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1395. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1396. struct fd_table *curfds = NULL;
  1397. if (!wasi_ctx)
  1398. return __WASI_EACCES;
  1399. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1400. return wasmtime_ssp_sock_set_send_timeout(exec_env, curfds, fd, timeout_us);
  1401. }
  1402. static wasi_errno_t
  1403. wasi_sock_set_tcp_fastopen_connect(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1404. bool is_enabled)
  1405. {
  1406. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1407. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1408. struct fd_table *curfds = NULL;
  1409. if (!wasi_ctx)
  1410. return __WASI_EACCES;
  1411. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1412. return wasmtime_ssp_sock_set_tcp_fastopen_connect(exec_env, curfds, fd,
  1413. is_enabled);
  1414. }
  1415. static wasi_errno_t
  1416. wasi_sock_set_tcp_no_delay(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1417. bool is_enabled)
  1418. {
  1419. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1420. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1421. struct fd_table *curfds = NULL;
  1422. if (!wasi_ctx)
  1423. return __WASI_EACCES;
  1424. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1425. return wasmtime_ssp_sock_set_tcp_no_delay(exec_env, curfds, fd, is_enabled);
  1426. }
  1427. static wasi_errno_t
  1428. wasi_sock_set_tcp_quick_ack(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1429. bool is_enabled)
  1430. {
  1431. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1432. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1433. struct fd_table *curfds = NULL;
  1434. if (!wasi_ctx)
  1435. return __WASI_EACCES;
  1436. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1437. return wasmtime_ssp_sock_set_tcp_quick_ack(exec_env, curfds, fd,
  1438. is_enabled);
  1439. }
  1440. static wasi_errno_t
  1441. wasi_sock_set_tcp_keep_idle(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1442. uint32_t time_s)
  1443. {
  1444. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1445. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1446. struct fd_table *curfds = NULL;
  1447. if (!wasi_ctx)
  1448. return __WASI_EACCES;
  1449. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1450. return wasmtime_ssp_sock_set_tcp_keep_idle(exec_env, curfds, fd, time_s);
  1451. }
  1452. static wasi_errno_t
  1453. wasi_sock_set_tcp_keep_intvl(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1454. uint32_t time_s)
  1455. {
  1456. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1457. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1458. struct fd_table *curfds = NULL;
  1459. if (!wasi_ctx)
  1460. return __WASI_EACCES;
  1461. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1462. return wasmtime_ssp_sock_set_tcp_keep_intvl(exec_env, curfds, fd, time_s);
  1463. }
  1464. static wasi_errno_t
  1465. wasi_sock_set_ip_multicast_loop(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1466. bool ipv6, bool is_enabled)
  1467. {
  1468. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1469. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1470. struct fd_table *curfds = NULL;
  1471. if (!wasi_ctx)
  1472. return __WASI_EACCES;
  1473. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1474. return wasmtime_ssp_sock_set_ip_multicast_loop(exec_env, curfds, fd, ipv6,
  1475. is_enabled);
  1476. }
  1477. static wasi_errno_t
  1478. wasi_sock_set_ip_add_membership(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1479. __wasi_addr_ip_t *imr_multiaddr,
  1480. uint32_t imr_interface)
  1481. {
  1482. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1483. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1484. struct fd_table *curfds = NULL;
  1485. if (!wasi_ctx)
  1486. return __WASI_EACCES;
  1487. if (!validate_native_addr(imr_multiaddr, (uint64)sizeof(__wasi_addr_ip_t)))
  1488. return __WASI_EINVAL;
  1489. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1490. return wasmtime_ssp_sock_set_ip_add_membership(
  1491. exec_env, curfds, fd, imr_multiaddr, imr_interface);
  1492. }
  1493. static wasi_errno_t
  1494. wasi_sock_set_ip_drop_membership(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1495. __wasi_addr_ip_t *imr_multiaddr,
  1496. uint32_t imr_interface)
  1497. {
  1498. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1499. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1500. struct fd_table *curfds = NULL;
  1501. if (!wasi_ctx)
  1502. return __WASI_EACCES;
  1503. if (!validate_native_addr(imr_multiaddr, (uint64)sizeof(__wasi_addr_ip_t)))
  1504. return __WASI_EINVAL;
  1505. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1506. return wasmtime_ssp_sock_set_ip_drop_membership(
  1507. exec_env, curfds, fd, imr_multiaddr, imr_interface);
  1508. }
  1509. static wasi_errno_t
  1510. wasi_sock_set_ip_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd, uint8_t ttl_s)
  1511. {
  1512. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1513. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1514. struct fd_table *curfds = NULL;
  1515. if (!wasi_ctx)
  1516. return __WASI_EACCES;
  1517. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1518. return wasmtime_ssp_sock_set_ip_ttl(exec_env, curfds, fd, ttl_s);
  1519. }
  1520. static wasi_errno_t
  1521. wasi_sock_set_ip_multicast_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd,
  1522. uint8_t ttl_s)
  1523. {
  1524. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1525. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1526. struct fd_table *curfds = NULL;
  1527. if (!wasi_ctx)
  1528. return __WASI_EACCES;
  1529. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1530. return wasmtime_ssp_sock_set_ip_multicast_ttl(exec_env, curfds, fd, ttl_s);
  1531. }
  1532. static wasi_errno_t
  1533. wasi_sock_set_ipv6_only(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled)
  1534. {
  1535. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1536. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1537. struct fd_table *curfds = NULL;
  1538. if (!wasi_ctx)
  1539. return __WASI_EACCES;
  1540. curfds = wasi_ctx_get_curfds(wasi_ctx);
  1541. return wasmtime_ssp_sock_set_ipv6_only(exec_env, curfds, fd, is_enabled);
  1542. }
  1543. static wasi_errno_t
  1544. allocate_iovec_app_buffer(wasm_module_inst_t module_inst,
  1545. const iovec_app_t *data, uint32 data_len,
  1546. uint8 **buf_ptr, uint64 *buf_len)
  1547. {
  1548. uint64 total_size = 0;
  1549. uint32 i;
  1550. uint8 *buf_begin = NULL;
  1551. if (data_len == 0) {
  1552. return __WASI_EINVAL;
  1553. }
  1554. total_size = sizeof(iovec_app_t) * (uint64)data_len;
  1555. if (total_size >= UINT32_MAX
  1556. || !validate_native_addr((void *)data, total_size))
  1557. return __WASI_EINVAL;
  1558. for (total_size = 0, i = 0; i < data_len; i++, data++) {
  1559. total_size += data->buf_len;
  1560. }
  1561. if (total_size == 0) {
  1562. return __WASI_EINVAL;
  1563. }
  1564. if (total_size >= UINT32_MAX
  1565. || !(buf_begin = wasm_runtime_malloc((uint32)total_size))) {
  1566. return __WASI_ENOMEM;
  1567. }
  1568. *buf_len = total_size;
  1569. *buf_ptr = buf_begin;
  1570. return __WASI_ESUCCESS;
  1571. }
  1572. static wasi_errno_t
  1573. copy_buffer_to_iovec_app(wasm_module_inst_t module_inst, uint8 *buf_begin,
  1574. uint32 buf_size, iovec_app_t *data, uint32 data_len,
  1575. uint32 size_to_copy)
  1576. {
  1577. uint8 *buf = buf_begin;
  1578. uint32 i;
  1579. uint32 size_to_copy_into_iovec;
  1580. if (buf_size < size_to_copy) {
  1581. return __WASI_EINVAL;
  1582. }
  1583. for (i = 0; i < data_len; data++, i++) {
  1584. char *native_addr;
  1585. if (!validate_app_addr((uint64)data->buf_offset,
  1586. (uint64)data->buf_len)) {
  1587. return __WASI_EINVAL;
  1588. }
  1589. if (buf >= buf_begin + buf_size
  1590. /* integer overflow */
  1591. || data->buf_len > UINTPTR_MAX - (uintptr_t)buf
  1592. || buf + data->buf_len > buf_begin + buf_size
  1593. || size_to_copy == 0) {
  1594. break;
  1595. }
  1596. /**
  1597. * If our app buffer size is smaller than the amount to be copied,
  1598. * only copy the amount in the app buffer. Otherwise, we fill the iovec
  1599. * buffer and reduce size to copy on the next iteration
  1600. */
  1601. size_to_copy_into_iovec = min_uint32(data->buf_len, size_to_copy);
  1602. native_addr = (void *)addr_app_to_native((uint64)data->buf_offset);
  1603. bh_memcpy_s(native_addr, size_to_copy_into_iovec, buf,
  1604. size_to_copy_into_iovec);
  1605. buf += size_to_copy_into_iovec;
  1606. size_to_copy -= size_to_copy_into_iovec;
  1607. }
  1608. return __WASI_ESUCCESS;
  1609. }
  1610. static wasi_errno_t
  1611. wasi_sock_recv_from(wasm_exec_env_t exec_env, wasi_fd_t sock,
  1612. iovec_app_t *ri_data, uint32 ri_data_len,
  1613. wasi_riflags_t ri_flags, __wasi_addr_t *src_addr,
  1614. uint32 *ro_data_len)
  1615. {
  1616. /**
  1617. * ri_data_len is the length of a list of iovec_app_t, which head is
  1618. * ri_data. ro_data_len is the number of bytes received
  1619. **/
  1620. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1621. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1622. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  1623. uint64 total_size;
  1624. uint8 *buf_begin = NULL;
  1625. wasi_errno_t err;
  1626. size_t recv_bytes = 0;
  1627. if (!wasi_ctx) {
  1628. return __WASI_EINVAL;
  1629. }
  1630. if (!validate_native_addr(ro_data_len, (uint64)sizeof(uint32)))
  1631. return __WASI_EINVAL;
  1632. err = allocate_iovec_app_buffer(module_inst, ri_data, ri_data_len,
  1633. &buf_begin, &total_size);
  1634. if (err != __WASI_ESUCCESS) {
  1635. goto fail;
  1636. }
  1637. memset(buf_begin, 0, total_size);
  1638. *ro_data_len = 0;
  1639. err = wasmtime_ssp_sock_recv_from(exec_env, curfds, sock, buf_begin,
  1640. total_size, ri_flags, src_addr,
  1641. &recv_bytes);
  1642. if (err != __WASI_ESUCCESS) {
  1643. goto fail;
  1644. }
  1645. *ro_data_len = (uint32)recv_bytes;
  1646. err = copy_buffer_to_iovec_app(module_inst, buf_begin, (uint32)total_size,
  1647. ri_data, ri_data_len, (uint32)recv_bytes);
  1648. fail:
  1649. if (buf_begin) {
  1650. wasm_runtime_free(buf_begin);
  1651. }
  1652. return err;
  1653. }
  1654. static wasi_errno_t
  1655. wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data,
  1656. uint32 ri_data_len, wasi_riflags_t ri_flags, uint32 *ro_data_len,
  1657. wasi_roflags_t *ro_flags)
  1658. {
  1659. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1660. __wasi_addr_t src_addr;
  1661. wasi_errno_t error;
  1662. if (!validate_native_addr(ro_flags, (uint64)sizeof(wasi_roflags_t)))
  1663. return __WASI_EINVAL;
  1664. error = wasi_sock_recv_from(exec_env, sock, ri_data, ri_data_len, ri_flags,
  1665. &src_addr, ro_data_len);
  1666. *ro_flags = ri_flags;
  1667. return error;
  1668. }
  1669. static wasi_errno_t
  1670. convert_iovec_app_to_buffer(wasm_module_inst_t module_inst,
  1671. const iovec_app_t *si_data, uint32 si_data_len,
  1672. uint8 **buf_ptr, uint64 *buf_len)
  1673. {
  1674. uint32 i;
  1675. const iovec_app_t *si_data_orig = si_data;
  1676. uint8 *buf = NULL;
  1677. wasi_errno_t error;
  1678. error = allocate_iovec_app_buffer(module_inst, si_data, si_data_len,
  1679. buf_ptr, buf_len);
  1680. if (error != __WASI_ESUCCESS) {
  1681. return error;
  1682. }
  1683. buf = *buf_ptr;
  1684. si_data = si_data_orig;
  1685. for (i = 0; i < si_data_len; i++, si_data++) {
  1686. char *native_addr;
  1687. if (!validate_app_addr((uint64)si_data->buf_offset,
  1688. (uint64)si_data->buf_len)) {
  1689. wasm_runtime_free(*buf_ptr);
  1690. return __WASI_EINVAL;
  1691. }
  1692. native_addr = (char *)addr_app_to_native((uint64)si_data->buf_offset);
  1693. bh_memcpy_s(buf, si_data->buf_len, native_addr, si_data->buf_len);
  1694. buf += si_data->buf_len;
  1695. }
  1696. return __WASI_ESUCCESS;
  1697. }
  1698. static wasi_errno_t
  1699. wasi_sock_send(wasm_exec_env_t exec_env, wasi_fd_t sock,
  1700. const iovec_app_t *si_data, uint32 si_data_len,
  1701. wasi_siflags_t si_flags, uint32 *so_data_len)
  1702. {
  1703. /**
  1704. * si_data_len is the length of a list of iovec_app_t, which head is
  1705. * si_data. so_data_len is the number of bytes sent
  1706. **/
  1707. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1708. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1709. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  1710. uint64 buf_size = 0;
  1711. uint8 *buf = NULL;
  1712. wasi_errno_t err;
  1713. size_t send_bytes = 0;
  1714. if (!wasi_ctx) {
  1715. return __WASI_EINVAL;
  1716. }
  1717. if (!validate_native_addr(so_data_len, (uint64)sizeof(uint32)))
  1718. return __WASI_EINVAL;
  1719. err = convert_iovec_app_to_buffer(module_inst, si_data, si_data_len, &buf,
  1720. &buf_size);
  1721. if (err != __WASI_ESUCCESS)
  1722. return err;
  1723. *so_data_len = 0;
  1724. err = wasmtime_ssp_sock_send(exec_env, curfds, sock, buf, buf_size,
  1725. &send_bytes);
  1726. *so_data_len = (uint32)send_bytes;
  1727. wasm_runtime_free(buf);
  1728. return err;
  1729. }
  1730. static wasi_errno_t
  1731. wasi_sock_send_to(wasm_exec_env_t exec_env, wasi_fd_t sock,
  1732. const iovec_app_t *si_data, uint32 si_data_len,
  1733. wasi_siflags_t si_flags, const __wasi_addr_t *dest_addr,
  1734. uint32 *so_data_len)
  1735. {
  1736. /**
  1737. * si_data_len is the length of a list of iovec_app_t, which head is
  1738. * si_data. so_data_len is the number of bytes sent
  1739. **/
  1740. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1741. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1742. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  1743. uint64 buf_size = 0;
  1744. uint8 *buf = NULL;
  1745. wasi_errno_t err;
  1746. size_t send_bytes = 0;
  1747. struct addr_pool *addr_pool = wasi_ctx_get_addr_pool(wasi_ctx);
  1748. if (!wasi_ctx) {
  1749. return __WASI_EINVAL;
  1750. }
  1751. if (!validate_native_addr(so_data_len, (uint64)sizeof(uint32)))
  1752. return __WASI_EINVAL;
  1753. err = convert_iovec_app_to_buffer(module_inst, si_data, si_data_len, &buf,
  1754. &buf_size);
  1755. if (err != __WASI_ESUCCESS)
  1756. return err;
  1757. *so_data_len = 0;
  1758. err = wasmtime_ssp_sock_send_to(exec_env, curfds, addr_pool, sock, buf,
  1759. buf_size, si_flags, dest_addr, &send_bytes);
  1760. *so_data_len = (uint32)send_bytes;
  1761. wasm_runtime_free(buf);
  1762. return err;
  1763. }
  1764. static wasi_errno_t
  1765. wasi_sock_shutdown(wasm_exec_env_t exec_env, wasi_fd_t sock, wasi_sdflags_t how)
  1766. {
  1767. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1768. wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
  1769. struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx);
  1770. if (!wasi_ctx)
  1771. return __WASI_EINVAL;
  1772. return wasmtime_ssp_sock_shutdown(exec_env, curfds, sock);
  1773. }
  1774. static wasi_errno_t
  1775. wasi_sched_yield(wasm_exec_env_t exec_env)
  1776. {
  1777. (void)exec_env;
  1778. return wasmtime_ssp_sched_yield();
  1779. }
  1780. /* clang-format off */
  1781. #define REG_NATIVE_FUNC(func_name, signature) \
  1782. { #func_name, wasi_##func_name, signature, NULL }
  1783. /* clang-format on */
  1784. static NativeSymbol native_symbols_libc_wasi[] = {
  1785. REG_NATIVE_FUNC(args_get, "(**)i"),
  1786. REG_NATIVE_FUNC(args_sizes_get, "(**)i"),
  1787. REG_NATIVE_FUNC(clock_res_get, "(i*)i"),
  1788. REG_NATIVE_FUNC(clock_time_get, "(iI*)i"),
  1789. REG_NATIVE_FUNC(environ_get, "(**)i"),
  1790. REG_NATIVE_FUNC(environ_sizes_get, "(**)i"),
  1791. REG_NATIVE_FUNC(fd_prestat_get, "(i*)i"),
  1792. REG_NATIVE_FUNC(fd_prestat_dir_name, "(i*~)i"),
  1793. REG_NATIVE_FUNC(fd_close, "(i)i"),
  1794. REG_NATIVE_FUNC(fd_datasync, "(i)i"),
  1795. REG_NATIVE_FUNC(fd_pread, "(i*iI*)i"),
  1796. REG_NATIVE_FUNC(fd_pwrite, "(i*iI*)i"),
  1797. REG_NATIVE_FUNC(fd_read, "(i*i*)i"),
  1798. REG_NATIVE_FUNC(fd_renumber, "(ii)i"),
  1799. REG_NATIVE_FUNC(fd_seek, "(iIi*)i"),
  1800. REG_NATIVE_FUNC(fd_tell, "(i*)i"),
  1801. REG_NATIVE_FUNC(fd_fdstat_get, "(i*)i"),
  1802. REG_NATIVE_FUNC(fd_fdstat_set_flags, "(ii)i"),
  1803. REG_NATIVE_FUNC(fd_fdstat_set_rights, "(iII)i"),
  1804. REG_NATIVE_FUNC(fd_sync, "(i)i"),
  1805. REG_NATIVE_FUNC(fd_write, "(i*i*)i"),
  1806. REG_NATIVE_FUNC(fd_advise, "(iIIi)i"),
  1807. REG_NATIVE_FUNC(fd_allocate, "(iII)i"),
  1808. REG_NATIVE_FUNC(path_create_directory, "(i*~)i"),
  1809. REG_NATIVE_FUNC(path_link, "(ii*~i*~)i"),
  1810. REG_NATIVE_FUNC(path_open, "(ii*~iIIi*)i"),
  1811. REG_NATIVE_FUNC(fd_readdir, "(i*~I*)i"),
  1812. REG_NATIVE_FUNC(path_readlink, "(i*~*~*)i"),
  1813. REG_NATIVE_FUNC(path_rename, "(i*~i*~)i"),
  1814. REG_NATIVE_FUNC(fd_filestat_get, "(i*)i"),
  1815. REG_NATIVE_FUNC(fd_filestat_set_times, "(iIIi)i"),
  1816. REG_NATIVE_FUNC(fd_filestat_set_size, "(iI)i"),
  1817. REG_NATIVE_FUNC(path_filestat_get, "(ii*~*)i"),
  1818. REG_NATIVE_FUNC(path_filestat_set_times, "(ii*~IIi)i"),
  1819. REG_NATIVE_FUNC(path_symlink, "(*~i*~)i"),
  1820. REG_NATIVE_FUNC(path_unlink_file, "(i*~)i"),
  1821. REG_NATIVE_FUNC(path_remove_directory, "(i*~)i"),
  1822. REG_NATIVE_FUNC(poll_oneoff, "(**i*)i"),
  1823. REG_NATIVE_FUNC(proc_exit, "(i)"),
  1824. REG_NATIVE_FUNC(proc_raise, "(i)i"),
  1825. REG_NATIVE_FUNC(random_get, "(*~)i"),
  1826. REG_NATIVE_FUNC(sock_accept, "(ii*)i"),
  1827. REG_NATIVE_FUNC(sock_addr_local, "(i*)i"),
  1828. REG_NATIVE_FUNC(sock_addr_remote, "(i*)i"),
  1829. REG_NATIVE_FUNC(sock_addr_resolve, "($$**i*)i"),
  1830. REG_NATIVE_FUNC(sock_bind, "(i*)i"),
  1831. REG_NATIVE_FUNC(sock_close, "(i)i"),
  1832. REG_NATIVE_FUNC(sock_connect, "(i*)i"),
  1833. REG_NATIVE_FUNC(sock_get_broadcast, "(i*)i"),
  1834. REG_NATIVE_FUNC(sock_get_keep_alive, "(i*)i"),
  1835. REG_NATIVE_FUNC(sock_get_linger, "(i**)i"),
  1836. REG_NATIVE_FUNC(sock_get_recv_buf_size, "(i*)i"),
  1837. REG_NATIVE_FUNC(sock_get_recv_timeout, "(i*)i"),
  1838. REG_NATIVE_FUNC(sock_get_reuse_addr, "(i*)i"),
  1839. REG_NATIVE_FUNC(sock_get_reuse_port, "(i*)i"),
  1840. REG_NATIVE_FUNC(sock_get_send_buf_size, "(i*)i"),
  1841. REG_NATIVE_FUNC(sock_get_send_timeout, "(i*)i"),
  1842. REG_NATIVE_FUNC(sock_get_tcp_fastopen_connect, "(i*)i"),
  1843. REG_NATIVE_FUNC(sock_get_tcp_keep_idle, "(i*)i"),
  1844. REG_NATIVE_FUNC(sock_get_tcp_keep_intvl, "(i*)i"),
  1845. REG_NATIVE_FUNC(sock_get_tcp_no_delay, "(i*)i"),
  1846. REG_NATIVE_FUNC(sock_get_tcp_quick_ack, "(i*)i"),
  1847. REG_NATIVE_FUNC(sock_get_ip_multicast_loop, "(ii*)i"),
  1848. REG_NATIVE_FUNC(sock_get_ip_multicast_ttl, "(i*)i"),
  1849. REG_NATIVE_FUNC(sock_get_ip_ttl, "(i*)i"),
  1850. REG_NATIVE_FUNC(sock_get_ipv6_only, "(i*)i"),
  1851. REG_NATIVE_FUNC(sock_listen, "(ii)i"),
  1852. REG_NATIVE_FUNC(sock_open, "(iii*)i"),
  1853. REG_NATIVE_FUNC(sock_recv, "(i*ii**)i"),
  1854. REG_NATIVE_FUNC(sock_recv_from, "(i*ii**)i"),
  1855. REG_NATIVE_FUNC(sock_send, "(i*ii*)i"),
  1856. REG_NATIVE_FUNC(sock_send_to, "(i*ii**)i"),
  1857. REG_NATIVE_FUNC(sock_set_broadcast, "(ii)i"),
  1858. REG_NATIVE_FUNC(sock_set_keep_alive, "(ii)i"),
  1859. REG_NATIVE_FUNC(sock_set_linger, "(iii)i"),
  1860. REG_NATIVE_FUNC(sock_set_recv_buf_size, "(ii)i"),
  1861. REG_NATIVE_FUNC(sock_set_recv_timeout, "(iI)i"),
  1862. REG_NATIVE_FUNC(sock_set_reuse_addr, "(ii)i"),
  1863. REG_NATIVE_FUNC(sock_set_reuse_port, "(ii)i"),
  1864. REG_NATIVE_FUNC(sock_set_send_buf_size, "(ii)i"),
  1865. REG_NATIVE_FUNC(sock_set_send_timeout, "(iI)i"),
  1866. REG_NATIVE_FUNC(sock_set_tcp_fastopen_connect, "(ii)i"),
  1867. REG_NATIVE_FUNC(sock_set_tcp_keep_idle, "(ii)i"),
  1868. REG_NATIVE_FUNC(sock_set_tcp_keep_intvl, "(ii)i"),
  1869. REG_NATIVE_FUNC(sock_set_tcp_no_delay, "(ii)i"),
  1870. REG_NATIVE_FUNC(sock_set_tcp_quick_ack, "(ii)i"),
  1871. REG_NATIVE_FUNC(sock_set_ip_multicast_loop, "(iii)i"),
  1872. REG_NATIVE_FUNC(sock_set_ip_multicast_ttl, "(ii)i"),
  1873. REG_NATIVE_FUNC(sock_set_ip_add_membership, "(i*i)i"),
  1874. REG_NATIVE_FUNC(sock_set_ip_drop_membership, "(i*i)i"),
  1875. REG_NATIVE_FUNC(sock_set_ip_ttl, "(ii)i"),
  1876. REG_NATIVE_FUNC(sock_set_ipv6_only, "(ii)i"),
  1877. REG_NATIVE_FUNC(sock_shutdown, "(ii)i"),
  1878. REG_NATIVE_FUNC(sched_yield, "()i"),
  1879. };
  1880. uint32
  1881. get_libc_wasi_export_apis(NativeSymbol **p_libc_wasi_apis)
  1882. {
  1883. *p_libc_wasi_apis = native_symbols_libc_wasi;
  1884. return sizeof(native_symbols_libc_wasi) / sizeof(NativeSymbol);
  1885. }