libc_wasi_wrapper.c 74 KB

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