libc_wasi_wrapper.c 74 KB

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