wasm_c_api.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_c_api_internal.h"
  6. #include "wasm_memory.h"
  7. #if WASM_ENABLE_INTERP != 0
  8. #include "wasm_runtime.h"
  9. #endif
  10. #if WASM_ENABLE_AOT != 0
  11. #include "aot_runtime.h"
  12. #endif
  13. #define ASSERT_NOT_IMPLEMENTED() bh_assert(!"not implemented")
  14. typedef struct wasm_module_ex_t wasm_module_ex_t;
  15. static void
  16. wasm_module_delete_internal(wasm_module_t *);
  17. static void
  18. wasm_instance_delete_internal(wasm_instance_t *);
  19. /* temporarily put stubs here */
  20. static wasm_store_t *
  21. wasm_store_copy(const wasm_store_t *src)
  22. {
  23. (void)src;
  24. LOG_WARNING("in the stub of %s", __FUNCTION__);
  25. return NULL;
  26. }
  27. wasm_module_t *
  28. wasm_module_copy(const wasm_module_t *src)
  29. {
  30. (void)src;
  31. LOG_WARNING("in the stub of %s", __FUNCTION__);
  32. return NULL;
  33. }
  34. wasm_instance_t *
  35. wasm_instance_copy(const wasm_instance_t *src)
  36. {
  37. (void)src;
  38. LOG_WARNING("in the stub of %s", __FUNCTION__);
  39. return NULL;
  40. }
  41. static void *
  42. malloc_internal(uint64 size)
  43. {
  44. void *mem = NULL;
  45. if (size < UINT32_MAX && (mem = wasm_runtime_malloc((uint32)size))) {
  46. memset(mem, 0, size);
  47. }
  48. return mem;
  49. }
  50. #define FREEIF(p) \
  51. if (p) { \
  52. wasm_runtime_free(p); \
  53. }
  54. /* clang-format off */
  55. #define RETURN_OBJ(obj, obj_del_func) \
  56. return obj; \
  57. failed: \
  58. obj_del_func(obj); \
  59. return NULL;
  60. #define RETURN_VOID(obj, obj_del_func) \
  61. return; \
  62. failed: \
  63. obj_del_func(obj); \
  64. return;
  65. /* clang-format on */
  66. /* Vectors */
  67. #define INIT_VEC(vector_p, init_func, ...) \
  68. do { \
  69. if (!(vector_p = malloc_internal(sizeof(*(vector_p))))) { \
  70. goto failed; \
  71. } \
  72. \
  73. init_func(vector_p, ##__VA_ARGS__); \
  74. if (vector_p->size && !vector_p->data) { \
  75. LOG_DEBUG("%s failed", #init_func); \
  76. goto failed; \
  77. } \
  78. } while (false)
  79. #define DEINIT_VEC(vector_p, deinit_func) \
  80. if ((vector_p)) { \
  81. deinit_func(vector_p); \
  82. wasm_runtime_free(vector_p); \
  83. vector_p = NULL; \
  84. }
  85. #define WASM_DEFINE_VEC(name) \
  86. void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t *out) \
  87. { \
  88. wasm_##name##_vec_new_uninitialized(out, 0); \
  89. } \
  90. void wasm_##name##_vec_new_uninitialized(own wasm_##name##_vec_t *out, \
  91. size_t size) \
  92. { \
  93. wasm_##name##_vec_new(out, size, NULL); \
  94. }
  95. /* vectors with no ownership management of elements */
  96. #define WASM_DEFINE_VEC_PLAIN(name) \
  97. WASM_DEFINE_VEC(name) \
  98. void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \
  99. own wasm_##name##_t const data[]) \
  100. { \
  101. if (!out) { \
  102. return; \
  103. } \
  104. \
  105. memset(out, 0, sizeof(wasm_##name##_vec_t)); \
  106. \
  107. if (!size) { \
  108. return; \
  109. } \
  110. \
  111. if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t))) { \
  112. LOG_DEBUG("bh_vector_init failed"); \
  113. goto failed; \
  114. } \
  115. \
  116. if (data) { \
  117. unsigned int size_in_bytes = 0; \
  118. size_in_bytes = size * sizeof(wasm_##name##_t); \
  119. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \
  120. out->num_elems = size; \
  121. } \
  122. \
  123. RETURN_VOID(out, wasm_##name##_vec_delete) \
  124. } \
  125. void wasm_##name##_vec_copy(wasm_##name##_vec_t *out, \
  126. const wasm_##name##_vec_t *src) \
  127. { \
  128. wasm_##name##_vec_new(out, src->size, src->data); \
  129. } \
  130. void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \
  131. { \
  132. if (v) { \
  133. bh_vector_destroy((Vector *)v); \
  134. } \
  135. }
  136. /* vectors that own their elements */
  137. #define WASM_DEFINE_VEC_OWN(name, elem_destroy_func) \
  138. WASM_DEFINE_VEC(name) \
  139. void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \
  140. own wasm_##name##_t *const data[]) \
  141. { \
  142. if (!out) { \
  143. return; \
  144. } \
  145. \
  146. memset(out, 0, sizeof(wasm_##name##_vec_t)); \
  147. \
  148. if (!size) { \
  149. return; \
  150. } \
  151. \
  152. if (!bh_vector_init((Vector *)out, size, \
  153. sizeof(wasm_##name##_t *))) { \
  154. LOG_DEBUG("bh_vector_init failed"); \
  155. goto failed; \
  156. } \
  157. \
  158. if (data) { \
  159. unsigned int size_in_bytes = 0; \
  160. size_in_bytes = size * sizeof(wasm_##name##_t *); \
  161. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \
  162. out->num_elems = size; \
  163. } \
  164. \
  165. RETURN_VOID(out, wasm_##name##_vec_delete) \
  166. } \
  167. void wasm_##name##_vec_copy(own wasm_##name##_vec_t *out, \
  168. const wasm_##name##_vec_t *src) \
  169. { \
  170. size_t i = 0; \
  171. memset(out, 0, sizeof(Vector)); \
  172. \
  173. if (!src || !src->size) { \
  174. return; \
  175. } \
  176. \
  177. if (!bh_vector_init((Vector *)out, src->size, \
  178. sizeof(wasm_##name##_t *))) { \
  179. LOG_DEBUG("bh_vector_init failed"); \
  180. goto failed; \
  181. } \
  182. \
  183. for (i = 0; i != src->num_elems; ++i) { \
  184. if (!(out->data[i] = wasm_##name##_copy(src->data[i]))) { \
  185. LOG_DEBUG("wasm_%s_copy failed", #name); \
  186. goto failed; \
  187. } \
  188. } \
  189. out->num_elems = src->num_elems; \
  190. \
  191. RETURN_VOID(out, wasm_##name##_vec_delete) \
  192. } \
  193. void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \
  194. { \
  195. size_t i = 0; \
  196. if (!v) { \
  197. return; \
  198. } \
  199. for (i = 0; i != v->num_elems; ++i) { \
  200. elem_destroy_func(*(v->data + i)); \
  201. } \
  202. bh_vector_destroy((Vector *)v); \
  203. }
  204. WASM_DEFINE_VEC_PLAIN(byte)
  205. WASM_DEFINE_VEC_PLAIN(val)
  206. WASM_DEFINE_VEC_OWN(valtype, wasm_valtype_delete)
  207. WASM_DEFINE_VEC_OWN(functype, wasm_functype_delete)
  208. WASM_DEFINE_VEC_OWN(exporttype, wasm_exporttype_delete)
  209. WASM_DEFINE_VEC_OWN(importtype, wasm_importtype_delete)
  210. WASM_DEFINE_VEC_OWN(store, wasm_store_delete)
  211. WASM_DEFINE_VEC_OWN(module, wasm_module_delete_internal)
  212. WASM_DEFINE_VEC_OWN(instance, wasm_instance_delete_internal)
  213. WASM_DEFINE_VEC_OWN(extern, wasm_extern_delete)
  214. WASM_DEFINE_VEC_OWN(frame, wasm_frame_delete)
  215. static inline bool
  216. valid_module_type(uint32 module_type)
  217. {
  218. bool result = false;
  219. #if WASM_ENABLE_INTERP != 0
  220. result = result || (module_type == Wasm_Module_Bytecode);
  221. #endif
  222. #if WASM_ENABLE_AOT != 0
  223. result = result || (module_type == Wasm_Module_AoT);
  224. #endif
  225. if (!result) {
  226. LOG_VERBOSE(
  227. "current building isn't compatiable with the module, may need "
  228. "recompile");
  229. }
  230. return result;
  231. }
  232. /* conflicting declaration between aot_export.h and aot.h */
  233. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  234. bool
  235. aot_compile_wasm_file_init();
  236. void
  237. aot_compile_wasm_file_destroy();
  238. uint8*
  239. aot_compile_wasm_file(const uint8 *wasm_file_buf, uint32 wasm_file_size,
  240. uint32 opt_level, uint32 size_level,
  241. char *error_buf, uint32 error_buf_size,
  242. uint32 *p_aot_file_size);
  243. #endif
  244. /* Runtime Environment */
  245. static void
  246. wasm_engine_delete_internal(wasm_engine_t *engine)
  247. {
  248. if (engine) {
  249. DEINIT_VEC(engine->stores, wasm_store_vec_delete);
  250. wasm_runtime_free(engine);
  251. }
  252. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  253. aot_compile_wasm_file_destroy();
  254. #endif
  255. wasm_runtime_destroy();
  256. }
  257. static wasm_engine_t *
  258. wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts)
  259. {
  260. wasm_engine_t *engine = NULL;
  261. /* init runtime */
  262. RuntimeInitArgs init_args = { 0 };
  263. init_args.mem_alloc_type = type;
  264. if (type == Alloc_With_Pool) {
  265. if (!opts) {
  266. return NULL;
  267. }
  268. init_args.mem_alloc_option.pool.heap_buf = opts->pool.heap_buf;
  269. init_args.mem_alloc_option.pool.heap_size = opts->pool.heap_size;
  270. }
  271. else if (type == Alloc_With_Allocator) {
  272. if (!opts) {
  273. return NULL;
  274. }
  275. init_args.mem_alloc_option.allocator.malloc_func =
  276. opts->allocator.malloc_func;
  277. init_args.mem_alloc_option.allocator.free_func =
  278. opts->allocator.free_func;
  279. init_args.mem_alloc_option.allocator.realloc_func =
  280. opts->allocator.realloc_func;
  281. }
  282. else {
  283. init_args.mem_alloc_option.pool.heap_buf = NULL;
  284. init_args.mem_alloc_option.pool.heap_size = 0;
  285. }
  286. if (!wasm_runtime_full_init(&init_args)) {
  287. LOG_DEBUG("wasm_runtime_full_init failed");
  288. goto failed;
  289. }
  290. #if BH_DEBUG != 0
  291. bh_log_set_verbose_level(5);
  292. #else
  293. bh_log_set_verbose_level(3);
  294. #endif
  295. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  296. if (!aot_compile_wasm_file_init()) {
  297. goto failed;
  298. }
  299. #endif
  300. /* create wasm_engine_t */
  301. if (!(engine = malloc_internal(sizeof(wasm_engine_t)))) {
  302. goto failed;
  303. }
  304. /* create wasm_store_vec_t */
  305. INIT_VEC(engine->stores, wasm_store_vec_new_uninitialized, 1);
  306. RETURN_OBJ(engine, wasm_engine_delete_internal)
  307. }
  308. /* global engine instance */
  309. static wasm_engine_t *singleton_engine = NULL;
  310. wasm_engine_t *
  311. wasm_engine_new()
  312. {
  313. if (!singleton_engine) {
  314. singleton_engine =
  315. wasm_engine_new_internal(Alloc_With_System_Allocator, NULL);
  316. }
  317. return singleton_engine;
  318. }
  319. wasm_engine_t *
  320. wasm_engine_new_with_args(mem_alloc_type_t type, const MemAllocOption *opts)
  321. {
  322. if (!singleton_engine) {
  323. singleton_engine = wasm_engine_new_internal(type, opts);
  324. }
  325. return singleton_engine;
  326. }
  327. /* BE AWARE: will RESET the singleton */
  328. void
  329. wasm_engine_delete(wasm_engine_t *engine)
  330. {
  331. if (engine) {
  332. wasm_engine_delete_internal(engine);
  333. singleton_engine = NULL;
  334. }
  335. }
  336. wasm_store_t *
  337. wasm_store_new(wasm_engine_t *engine)
  338. {
  339. wasm_store_t *store = NULL;
  340. if (!engine || singleton_engine != engine) {
  341. return NULL;
  342. }
  343. if (!wasm_runtime_init_thread_env()) {
  344. LOG_ERROR("init thread environment failed");
  345. return NULL;
  346. }
  347. if (!(store = malloc_internal(sizeof(wasm_store_t)))) {
  348. wasm_runtime_destroy_thread_env();
  349. return NULL;
  350. }
  351. /* new a vector, and new its data */
  352. INIT_VEC(store->modules, wasm_module_vec_new_uninitialized,
  353. DEFAULT_VECTOR_INIT_LENGTH);
  354. INIT_VEC(store->instances, wasm_instance_vec_new_uninitialized,
  355. DEFAULT_VECTOR_INIT_LENGTH);
  356. /* append to a store list of engine */
  357. if (!bh_vector_append((Vector *)singleton_engine->stores, &store)) {
  358. LOG_DEBUG("bh_vector_append failed");
  359. goto failed;
  360. }
  361. return store;
  362. failed:
  363. wasm_store_delete(store);
  364. return NULL;
  365. }
  366. void
  367. wasm_store_delete(wasm_store_t *store)
  368. {
  369. size_t i, store_count;
  370. if (!store) {
  371. return;
  372. }
  373. /* remove it from the list in the engine */
  374. store_count = bh_vector_size((Vector *)singleton_engine->stores);
  375. for (i = 0; i != store_count; ++i) {
  376. wasm_store_t *tmp;
  377. if (!bh_vector_get((Vector *)singleton_engine->stores, i, &tmp)) {
  378. break;
  379. }
  380. if (tmp == store) {
  381. bh_vector_remove((Vector *)singleton_engine->stores, i, NULL);
  382. break;
  383. }
  384. }
  385. DEINIT_VEC(store->modules, wasm_module_vec_delete);
  386. DEINIT_VEC(store->instances, wasm_instance_vec_delete);
  387. wasm_runtime_free(store);
  388. wasm_runtime_destroy_thread_env();
  389. }
  390. /* Type Representations */
  391. static wasm_valkind_t
  392. val_type_rt_2_valkind(uint8 val_type_rt)
  393. {
  394. switch (val_type_rt) {
  395. case VALUE_TYPE_I32:
  396. return WASM_I32;
  397. case VALUE_TYPE_I64:
  398. return WASM_I64;
  399. case VALUE_TYPE_F32:
  400. return WASM_F32;
  401. case VALUE_TYPE_F64:
  402. return WASM_F64;
  403. case VALUE_TYPE_ANY:
  404. return WASM_ANYREF;
  405. case VALUE_TYPE_FUNCREF:
  406. return WASM_FUNCREF;
  407. default:
  408. LOG_WARNING("%s meets unsupported type: %d", __FUNCTION__,
  409. val_type_rt);
  410. return WASM_ANYREF;
  411. }
  412. }
  413. static wasm_valtype_t *
  414. wasm_valtype_new_internal(uint8 val_type_rt)
  415. {
  416. return wasm_valtype_new(val_type_rt_2_valkind(val_type_rt));
  417. }
  418. wasm_valtype_t *
  419. wasm_valtype_new(wasm_valkind_t kind)
  420. {
  421. wasm_valtype_t *val_type;
  422. if (!(val_type = malloc_internal(sizeof(wasm_valtype_t)))) {
  423. return NULL;
  424. }
  425. val_type->kind = kind;
  426. return val_type;
  427. }
  428. void
  429. wasm_valtype_delete(wasm_valtype_t *val_type)
  430. {
  431. FREEIF(val_type);
  432. }
  433. wasm_valtype_t *
  434. wasm_valtype_copy(const wasm_valtype_t *src)
  435. {
  436. if (!src) {
  437. return NULL;
  438. }
  439. return wasm_valtype_new(src->kind);
  440. }
  441. wasm_valkind_t
  442. wasm_valtype_kind(const wasm_valtype_t *val_type)
  443. {
  444. if (!val_type) {
  445. return WASM_ANYREF;
  446. }
  447. return val_type->kind;
  448. }
  449. bool
  450. wasm_valtype_same(const wasm_valtype_t *vt1, const wasm_valtype_t *vt2)
  451. {
  452. if (!vt1 && !vt2) {
  453. return true;
  454. }
  455. if (!vt1 || !vt2) {
  456. return false;
  457. }
  458. return vt1->kind == vt2->kind;
  459. }
  460. static wasm_functype_t *
  461. wasm_functype_new_internal(WASMType *type_rt)
  462. {
  463. wasm_functype_t *type = NULL;
  464. wasm_valtype_t *param_type = NULL, *result_type = NULL;
  465. uint32 i = 0;
  466. if (!type_rt) {
  467. return NULL;
  468. }
  469. if (!(type = malloc_internal(sizeof(wasm_functype_t)))) {
  470. return NULL;
  471. }
  472. type->extern_kind = WASM_EXTERN_FUNC;
  473. /* WASMType->types[0 : type_rt->param_count) -> type->params */
  474. INIT_VEC(type->params, wasm_valtype_vec_new_uninitialized,
  475. type_rt->param_count);
  476. for (i = 0; i < type_rt->param_count; ++i) {
  477. if (!(param_type = wasm_valtype_new_internal(*(type_rt->types + i)))) {
  478. goto failed;
  479. }
  480. if (!bh_vector_append((Vector *)type->params, &param_type)) {
  481. LOG_DEBUG("bh_vector_append failed");
  482. goto failed;
  483. }
  484. }
  485. /* WASMType->types[type_rt->param_count : type_rt->result_count) -> type->results */
  486. INIT_VEC(type->results, wasm_valtype_vec_new_uninitialized,
  487. type_rt->result_count);
  488. for (i = 0; i < type_rt->result_count; ++i) {
  489. if (!(result_type = wasm_valtype_new_internal(
  490. *(type_rt->types + type_rt->param_count + i)))) {
  491. goto failed;
  492. }
  493. if (!bh_vector_append((Vector *)type->results, &result_type)) {
  494. LOG_DEBUG("bh_vector_append failed");
  495. goto failed;
  496. }
  497. }
  498. return type;
  499. failed:
  500. wasm_valtype_delete(param_type);
  501. wasm_valtype_delete(result_type);
  502. wasm_functype_delete(type);
  503. return NULL;
  504. }
  505. wasm_functype_t *
  506. wasm_functype_new(own wasm_valtype_vec_t *params,
  507. own wasm_valtype_vec_t *results)
  508. {
  509. wasm_functype_t *type = NULL;
  510. if (!params) {
  511. return NULL;
  512. }
  513. if (!results) {
  514. return NULL;
  515. }
  516. if (!(type = malloc_internal(sizeof(wasm_functype_t)))) {
  517. goto failed;
  518. }
  519. type->extern_kind = WASM_EXTERN_FUNC;
  520. /* take ownership */
  521. if (!(type->params = malloc_internal(sizeof(wasm_valtype_vec_t)))) {
  522. goto failed;
  523. }
  524. bh_memcpy_s(type->params, sizeof(wasm_valtype_vec_t), params,
  525. sizeof(wasm_valtype_vec_t));
  526. if (!(type->results = malloc_internal(sizeof(wasm_valtype_vec_t)))) {
  527. goto failed;
  528. }
  529. bh_memcpy_s(type->results, sizeof(wasm_valtype_vec_t), results,
  530. sizeof(wasm_valtype_vec_t));
  531. return type;
  532. failed:
  533. wasm_functype_delete(type);
  534. return NULL;
  535. }
  536. wasm_functype_t *
  537. wasm_functype_copy(const wasm_functype_t *src)
  538. {
  539. wasm_functype_t *functype;
  540. wasm_valtype_vec_t params = { 0 }, results = { 0 };
  541. if (!src) {
  542. return NULL;
  543. }
  544. wasm_valtype_vec_copy(&params, src->params);
  545. if (src->params->size && !params.data) {
  546. goto failed;
  547. }
  548. wasm_valtype_vec_copy(&results, src->results);
  549. if (src->results->size && !results.data) {
  550. goto failed;
  551. }
  552. if (!(functype = wasm_functype_new(&params, &results))) {
  553. goto failed;
  554. }
  555. return functype;
  556. failed:
  557. wasm_valtype_vec_delete(&params);
  558. wasm_valtype_vec_delete(&results);
  559. return NULL;
  560. }
  561. void
  562. wasm_functype_delete(wasm_functype_t *func_type)
  563. {
  564. if (!func_type) {
  565. return;
  566. }
  567. DEINIT_VEC(func_type->params, wasm_valtype_vec_delete);
  568. DEINIT_VEC(func_type->results, wasm_valtype_vec_delete);
  569. wasm_runtime_free(func_type);
  570. }
  571. const wasm_valtype_vec_t *
  572. wasm_functype_params(const wasm_functype_t *func_type)
  573. {
  574. if (!func_type) {
  575. return NULL;
  576. }
  577. return func_type->params;
  578. }
  579. const wasm_valtype_vec_t *
  580. wasm_functype_results(const wasm_functype_t *func_type)
  581. {
  582. if (!func_type) {
  583. return NULL;
  584. }
  585. return func_type->results;
  586. }
  587. wasm_globaltype_t *
  588. wasm_globaltype_new(own wasm_valtype_t *val_type, wasm_mutability_t mut)
  589. {
  590. wasm_globaltype_t *global_type = NULL;
  591. if (!val_type) {
  592. return NULL;
  593. }
  594. if (!(global_type = malloc_internal(sizeof(wasm_globaltype_t)))) {
  595. return NULL;
  596. }
  597. global_type->extern_kind = WASM_EXTERN_GLOBAL;
  598. global_type->val_type = val_type;
  599. global_type->mutability = mut;
  600. return global_type;
  601. }
  602. wasm_globaltype_t *
  603. wasm_globaltype_new_internal(uint8 val_type_rt, bool is_mutable)
  604. {
  605. wasm_globaltype_t *globaltype;
  606. wasm_valtype_t *val_type;
  607. if (!(val_type = wasm_valtype_new(val_type_rt_2_valkind(val_type_rt)))) {
  608. return NULL;
  609. }
  610. if (!(globaltype = wasm_globaltype_new(
  611. val_type, is_mutable ? WASM_VAR : WASM_CONST))) {
  612. wasm_valtype_delete(val_type);
  613. }
  614. return globaltype;
  615. }
  616. void
  617. wasm_globaltype_delete(wasm_globaltype_t *global_type)
  618. {
  619. if (!global_type) {
  620. return;
  621. }
  622. if (global_type->val_type) {
  623. wasm_valtype_delete(global_type->val_type);
  624. global_type->val_type = NULL;
  625. }
  626. wasm_runtime_free(global_type);
  627. }
  628. wasm_globaltype_t *
  629. wasm_globaltype_copy(const wasm_globaltype_t *src)
  630. {
  631. wasm_globaltype_t *global_type;
  632. wasm_valtype_t *val_type;
  633. if (!src) {
  634. return NULL;
  635. }
  636. if (!(val_type = wasm_valtype_copy(src->val_type))) {
  637. return NULL;
  638. }
  639. if (!(global_type = wasm_globaltype_new(val_type, src->mutability))) {
  640. wasm_valtype_delete(val_type);
  641. }
  642. return global_type;
  643. }
  644. const wasm_valtype_t *
  645. wasm_globaltype_content(const wasm_globaltype_t *global_type)
  646. {
  647. if (!global_type) {
  648. return NULL;
  649. }
  650. return global_type->val_type;
  651. }
  652. wasm_mutability_t
  653. wasm_globaltype_mutability(const wasm_globaltype_t *global_type)
  654. {
  655. if (!global_type) {
  656. return false;
  657. }
  658. return global_type->mutability;
  659. }
  660. bool
  661. wasm_globaltype_same(const wasm_globaltype_t *gt1,
  662. const wasm_globaltype_t *gt2)
  663. {
  664. if (!gt1 && !gt2) {
  665. return true;
  666. }
  667. if (!gt1 || !gt2) {
  668. return false;
  669. }
  670. return wasm_valtype_same(gt1->val_type, gt2->val_type)
  671. || gt1->mutability == gt2->mutability;
  672. }
  673. static wasm_tabletype_t *
  674. wasm_tabletype_new_internal(uint8 val_type_rt,
  675. uint32 init_size,
  676. uint32 max_size)
  677. {
  678. wasm_tabletype_t *table_type;
  679. wasm_limits_t limits = { init_size, max_size };
  680. wasm_valtype_t *val_type;
  681. if (!(val_type = wasm_valtype_new_internal(val_type_rt))) {
  682. return NULL;
  683. }
  684. if (!(table_type = wasm_tabletype_new(val_type, &limits))) {
  685. wasm_valtype_delete(val_type);
  686. }
  687. return table_type;
  688. }
  689. wasm_tabletype_t *
  690. wasm_tabletype_new(own wasm_valtype_t *val_type, const wasm_limits_t *limits)
  691. {
  692. wasm_tabletype_t *table_type = NULL;
  693. if (!val_type) {
  694. return NULL;
  695. }
  696. if (!(table_type = malloc_internal(sizeof(wasm_tabletype_t)))) {
  697. return NULL;
  698. }
  699. table_type->extern_kind = WASM_EXTERN_TABLE;
  700. table_type->val_type = val_type;
  701. table_type->limits.min = limits->min;
  702. table_type->limits.max = limits->max;
  703. return table_type;
  704. }
  705. wasm_tabletype_t *
  706. wasm_tabletype_copy(const wasm_tabletype_t *src)
  707. {
  708. wasm_tabletype_t *table_type;
  709. wasm_valtype_t *val_type;
  710. if (!src) {
  711. return NULL;
  712. }
  713. if (!(val_type = wasm_valtype_copy(src->val_type))) {
  714. return NULL;
  715. }
  716. if (!(table_type = wasm_tabletype_new(val_type, &src->limits))) {
  717. wasm_valtype_delete(val_type);
  718. }
  719. return table_type;
  720. }
  721. void
  722. wasm_tabletype_delete(wasm_tabletype_t *table_type)
  723. {
  724. if (!table_type) {
  725. return;
  726. }
  727. if (table_type->val_type) {
  728. wasm_valtype_delete(table_type->val_type);
  729. table_type->val_type = NULL;
  730. }
  731. wasm_runtime_free(table_type);
  732. }
  733. const wasm_valtype_t *
  734. wasm_tabletype_element(const wasm_tabletype_t *table_type)
  735. {
  736. if (!table_type) {
  737. return NULL;
  738. }
  739. return table_type->val_type;
  740. }
  741. const wasm_limits_t *
  742. wasm_tabletype_limits(const wasm_tabletype_t *table_type)
  743. {
  744. if (!table_type) {
  745. return NULL;
  746. }
  747. return &(table_type->limits);
  748. }
  749. static wasm_memorytype_t *
  750. wasm_memorytype_new_internal(uint32 min_pages, uint32 max_pages)
  751. {
  752. wasm_limits_t limits = { min_pages, max_pages };
  753. return wasm_memorytype_new(&limits);
  754. }
  755. wasm_memorytype_t *
  756. wasm_memorytype_new(const wasm_limits_t *limits)
  757. {
  758. wasm_memorytype_t *memory_type = NULL;
  759. if (!limits) {
  760. return NULL;
  761. }
  762. if (!(memory_type = malloc_internal(sizeof(wasm_memorytype_t)))) {
  763. return NULL;
  764. }
  765. memory_type->extern_kind = WASM_EXTERN_MEMORY;
  766. memory_type->limits.min = limits->min;
  767. memory_type->limits.max = limits->max;
  768. return memory_type;
  769. }
  770. wasm_memorytype_t *
  771. wasm_memorytype_copy(const wasm_memorytype_t *src)
  772. {
  773. if (!src) {
  774. return NULL;
  775. }
  776. return wasm_memorytype_new(&src->limits);
  777. }
  778. void
  779. wasm_memorytype_delete(wasm_memorytype_t *memory_type)
  780. {
  781. FREEIF(memory_type);
  782. }
  783. const wasm_limits_t *
  784. wasm_memorytype_limits(const wasm_memorytype_t *memory_type)
  785. {
  786. if (!memory_type) {
  787. return NULL;
  788. }
  789. return &(memory_type->limits);
  790. }
  791. wasm_externkind_t
  792. wasm_externtype_kind(const wasm_externtype_t *extern_type)
  793. {
  794. if (!extern_type) {
  795. return WASM_EXTERN_FUNC;
  796. }
  797. return extern_type->extern_kind;
  798. }
  799. #define BASIC_FOUR_TYPE_LIST(V) \
  800. V(functype) \
  801. V(globaltype) \
  802. V(memorytype) \
  803. V(tabletype)
  804. #define WASM_EXTERNTYPE_AS_OTHERTYPE(name) \
  805. wasm_##name##_t *wasm_externtype_as_##name( \
  806. wasm_externtype_t *extern_type) \
  807. { \
  808. return (wasm_##name##_t *)extern_type; \
  809. }
  810. BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE)
  811. #undef WASM_EXTERNTYPE_AS_OTHERTYPE
  812. #define WASM_OTHERTYPE_AS_EXTERNTYPE(name) \
  813. wasm_externtype_t *wasm_##name##_as_externtype(wasm_##name##_t *other) \
  814. { \
  815. return (wasm_externtype_t *)other; \
  816. }
  817. BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE)
  818. #undef WASM_OTHERTYPE_AS_EXTERNTYPE
  819. #define WASM_EXTERNTYPE_AS_OTHERTYPE_CONST(name) \
  820. const wasm_##name##_t *wasm_externtype_as_##name##_const( \
  821. const wasm_externtype_t *extern_type) \
  822. { \
  823. return (const wasm_##name##_t *)extern_type; \
  824. }
  825. BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE_CONST)
  826. #undef WASM_EXTERNTYPE_AS_OTHERTYPE_CONST
  827. #define WASM_OTHERTYPE_AS_EXTERNTYPE_CONST(name) \
  828. const wasm_externtype_t *wasm_##name##_as_externtype_const( \
  829. const wasm_##name##_t *other) \
  830. { \
  831. return (const wasm_externtype_t *)other; \
  832. }
  833. BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE_CONST)
  834. #undef WASM_OTHERTYPE_AS_EXTERNTYPE_CONST
  835. wasm_externtype_t *
  836. wasm_externtype_copy(const wasm_externtype_t *src)
  837. {
  838. wasm_externtype_t *extern_type = NULL;
  839. if (!src) {
  840. return NULL;
  841. }
  842. switch (src->extern_kind) {
  843. #define COPY_EXTERNTYPE(NAME, name) \
  844. case WASM_EXTERN_##NAME: \
  845. { \
  846. extern_type = wasm_##name##_as_externtype( \
  847. wasm_##name##_copy(wasm_externtype_as_##name##_const(src))); \
  848. break; \
  849. }
  850. COPY_EXTERNTYPE(FUNC, functype)
  851. COPY_EXTERNTYPE(GLOBAL, globaltype)
  852. COPY_EXTERNTYPE(MEMORY, memorytype)
  853. COPY_EXTERNTYPE(TABLE, tabletype)
  854. #undef COPY_EXTERNTYPE
  855. default:
  856. LOG_WARNING("%s meets unsupported kind", __FUNCTION__,
  857. src->extern_kind);
  858. break;
  859. }
  860. return extern_type;
  861. }
  862. void
  863. wasm_externtype_delete(wasm_externtype_t *extern_type)
  864. {
  865. if (!extern_type) {
  866. return;
  867. }
  868. switch (wasm_externtype_kind(extern_type)) {
  869. case WASM_EXTERN_FUNC:
  870. wasm_functype_delete(wasm_externtype_as_functype(extern_type));
  871. break;
  872. case WASM_EXTERN_GLOBAL:
  873. wasm_globaltype_delete(wasm_externtype_as_globaltype(extern_type));
  874. break;
  875. case WASM_EXTERN_MEMORY:
  876. wasm_memorytype_delete(wasm_externtype_as_memorytype(extern_type));
  877. break;
  878. case WASM_EXTERN_TABLE:
  879. wasm_tabletype_delete(wasm_externtype_as_tabletype(extern_type));
  880. break;
  881. default:
  882. LOG_WARNING("%s meets unsupported type", __FUNCTION__,
  883. extern_type);
  884. break;
  885. }
  886. }
  887. own wasm_importtype_t *
  888. wasm_importtype_new(own wasm_byte_vec_t *module_name,
  889. own wasm_byte_vec_t *field_name,
  890. own wasm_externtype_t *extern_type)
  891. {
  892. wasm_importtype_t *import_type = NULL;
  893. if (!(import_type = malloc_internal(sizeof(wasm_importtype_t)))) {
  894. return NULL;
  895. }
  896. /* take ownership */
  897. if (!(import_type->module_name =
  898. malloc_internal(sizeof(wasm_byte_vec_t)))) {
  899. goto failed;
  900. }
  901. bh_memcpy_s(import_type->module_name, sizeof(wasm_byte_vec_t), module_name,
  902. sizeof(wasm_byte_vec_t));
  903. if (!(import_type->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  904. goto failed;
  905. }
  906. bh_memcpy_s(import_type->name, sizeof(wasm_byte_vec_t), field_name,
  907. sizeof(wasm_byte_vec_t));
  908. import_type->extern_type = extern_type;
  909. return import_type;
  910. failed:
  911. wasm_importtype_delete(import_type);
  912. return NULL;
  913. }
  914. void
  915. wasm_importtype_delete(own wasm_importtype_t *import_type)
  916. {
  917. if (!import_type) {
  918. return;
  919. }
  920. DEINIT_VEC(import_type->module_name, wasm_byte_vec_delete);
  921. DEINIT_VEC(import_type->name, wasm_byte_vec_delete);
  922. wasm_externtype_delete(import_type->extern_type);
  923. wasm_runtime_free(import_type);
  924. }
  925. own wasm_importtype_t *
  926. wasm_importtype_copy(const wasm_importtype_t *src)
  927. {
  928. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  929. wasm_externtype_t *extern_type = NULL;
  930. wasm_importtype_t *import_type = NULL;
  931. if (!src) {
  932. return NULL;
  933. }
  934. wasm_byte_vec_copy(&module_name, src->module_name);
  935. if (src->module_name->size && !module_name.data) {
  936. goto failed;
  937. }
  938. wasm_byte_vec_copy(&name, src->name);
  939. if (src->name->size && !name.data) {
  940. goto failed;
  941. }
  942. if (!(extern_type = wasm_externtype_copy(src->extern_type))) {
  943. goto failed;
  944. }
  945. if (!(import_type =
  946. wasm_importtype_new(&module_name, &name, extern_type))) {
  947. goto failed;
  948. }
  949. return import_type;
  950. failed:
  951. wasm_byte_vec_delete(&module_name);
  952. wasm_byte_vec_delete(&name);
  953. wasm_externtype_delete(extern_type);
  954. wasm_importtype_delete(import_type);
  955. return NULL;
  956. }
  957. const wasm_byte_vec_t *
  958. wasm_importtype_module(const wasm_importtype_t *import_type)
  959. {
  960. if (!import_type) {
  961. return NULL;
  962. }
  963. return import_type->module_name;
  964. }
  965. const wasm_byte_vec_t *
  966. wasm_importtype_name(const wasm_importtype_t *import_type)
  967. {
  968. if (!import_type) {
  969. return NULL;
  970. }
  971. return import_type->name;
  972. }
  973. const wasm_externtype_t *
  974. wasm_importtype_type(const wasm_importtype_t *import_type)
  975. {
  976. if (!import_type) {
  977. return NULL;
  978. }
  979. return import_type->extern_type;
  980. }
  981. own wasm_exporttype_t *
  982. wasm_exporttype_new(own wasm_byte_vec_t *name,
  983. own wasm_externtype_t *extern_type)
  984. {
  985. wasm_exporttype_t *export_type = NULL;
  986. if (!(export_type = malloc_internal(sizeof(wasm_exporttype_t)))) {
  987. return NULL;
  988. }
  989. if (!(export_type->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  990. wasm_exporttype_delete(export_type);
  991. return NULL;
  992. }
  993. bh_memcpy_s(export_type->name, sizeof(wasm_byte_vec_t), name,
  994. sizeof(wasm_byte_vec_t));
  995. export_type->extern_type = extern_type;
  996. return export_type;
  997. }
  998. wasm_exporttype_t *
  999. wasm_exporttype_copy(const wasm_exporttype_t *src)
  1000. {
  1001. wasm_exporttype_t *export_type;
  1002. wasm_byte_vec_t name = { 0 };
  1003. wasm_externtype_t *extern_type = NULL;
  1004. if (!src) {
  1005. return NULL;
  1006. }
  1007. wasm_byte_vec_copy(&name, src->name);
  1008. if (src->name->size && !name.data) {
  1009. goto failed;
  1010. }
  1011. if (!(extern_type = wasm_externtype_copy(src->extern_type))) {
  1012. goto failed;
  1013. }
  1014. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  1015. goto failed;
  1016. }
  1017. return export_type;
  1018. failed:
  1019. wasm_byte_vec_delete(&name);
  1020. wasm_externtype_delete(extern_type);
  1021. return NULL;
  1022. }
  1023. void
  1024. wasm_exporttype_delete(wasm_exporttype_t *export_type)
  1025. {
  1026. if (!export_type) {
  1027. return;
  1028. }
  1029. DEINIT_VEC(export_type->name, wasm_byte_vec_delete);
  1030. wasm_externtype_delete(export_type->extern_type);
  1031. wasm_runtime_free(export_type);
  1032. }
  1033. const wasm_byte_vec_t *
  1034. wasm_exporttype_name(const wasm_exporttype_t *export_type)
  1035. {
  1036. if (!export_type) {
  1037. return NULL;
  1038. }
  1039. return export_type->name;
  1040. }
  1041. const wasm_externtype_t *
  1042. wasm_exporttype_type(const wasm_exporttype_t *export_type)
  1043. {
  1044. if (!export_type) {
  1045. return NULL;
  1046. }
  1047. return export_type->extern_type;
  1048. }
  1049. /* Runtime Objects */
  1050. void
  1051. wasm_val_delete(wasm_val_t *v)
  1052. {
  1053. FREEIF(v);
  1054. }
  1055. void
  1056. wasm_val_copy(wasm_val_t *out, const wasm_val_t *src)
  1057. {
  1058. if (!out || !src) {
  1059. return;
  1060. }
  1061. bh_memcpy_s(out, sizeof(wasm_val_t), src, sizeof(wasm_val_t));
  1062. }
  1063. bool
  1064. wasm_val_same(const wasm_val_t *v1, const wasm_val_t *v2)
  1065. {
  1066. if (!v1 && !v2) {
  1067. return true;
  1068. }
  1069. if (!v1 || !v2) {
  1070. return false;
  1071. }
  1072. if (v1->kind != v2->kind) {
  1073. return false;
  1074. }
  1075. switch (v1->kind) {
  1076. case WASM_I32:
  1077. return v1->of.i32 == v2->of.i32;
  1078. case WASM_I64:
  1079. return v1->of.i64 == v2->of.i64;
  1080. case WASM_F32:
  1081. return v1->of.f32 == v2->of.f32;
  1082. case WASM_F64:
  1083. return v1->of.f64 == v2->of.f64;
  1084. case WASM_FUNCREF:
  1085. return v1->of.ref == v2->of.ref;
  1086. default:
  1087. break;
  1088. }
  1089. return false;
  1090. }
  1091. static wasm_frame_t *
  1092. wasm_frame_new(wasm_instance_t *instance,
  1093. size_t module_offset,
  1094. uint32 func_index,
  1095. size_t func_offset)
  1096. {
  1097. wasm_frame_t *frame;
  1098. if (!(frame = malloc_internal(sizeof(wasm_frame_t)))) {
  1099. return NULL;
  1100. }
  1101. frame->instance = instance;
  1102. frame->module_offset = module_offset;
  1103. frame->func_index = func_index;
  1104. frame->func_offset = func_offset;
  1105. return frame;
  1106. }
  1107. own wasm_frame_t *
  1108. wasm_frame_copy(const wasm_frame_t *src)
  1109. {
  1110. if (!src) {
  1111. return NULL;
  1112. }
  1113. return wasm_frame_new(src->instance, src->module_offset, src->func_index,
  1114. src->func_offset);
  1115. }
  1116. void
  1117. wasm_frame_delete(own wasm_frame_t *frame)
  1118. {
  1119. if (!frame) {
  1120. return;
  1121. }
  1122. wasm_runtime_free(frame);
  1123. }
  1124. struct wasm_instance_t *
  1125. wasm_frame_instance(const wasm_frame_t *frame)
  1126. {
  1127. return frame ? frame->instance : NULL;
  1128. }
  1129. size_t
  1130. wasm_frame_module_offset(const wasm_frame_t *frame)
  1131. {
  1132. return frame ? frame->module_offset : 0;
  1133. }
  1134. uint32_t
  1135. wasm_frame_func_index(const wasm_frame_t *frame)
  1136. {
  1137. return frame ? frame->func_index : 0;
  1138. }
  1139. size_t
  1140. wasm_frame_func_offset(const wasm_frame_t *frame)
  1141. {
  1142. return frame ? frame->func_offset : 0;
  1143. }
  1144. static wasm_trap_t *
  1145. wasm_trap_new_internal(WASMModuleInstanceCommon *inst_comm_rt,
  1146. const char *default_error_info)
  1147. {
  1148. wasm_trap_t *trap;
  1149. const char *error_info = NULL;
  1150. wasm_instance_vec_t *instances;
  1151. wasm_instance_t *frame_instance = NULL;
  1152. uint32 i;
  1153. if (!singleton_engine || !singleton_engine->stores
  1154. || !singleton_engine->stores->num_elems) {
  1155. return NULL;
  1156. }
  1157. #if WASM_ENABLE_INTERP != 0
  1158. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  1159. if (!(error_info =
  1160. wasm_get_exception((WASMModuleInstance *)inst_comm_rt))) {
  1161. return NULL;
  1162. }
  1163. }
  1164. #endif
  1165. #if WASM_ENABLE_AOT != 0
  1166. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  1167. if (!(error_info =
  1168. aot_get_exception((AOTModuleInstance *)inst_comm_rt))) {
  1169. return NULL;
  1170. }
  1171. }
  1172. #endif
  1173. if (!error_info && !(error_info = default_error_info)) {
  1174. return NULL;
  1175. }
  1176. if (!(trap = malloc_internal(sizeof(wasm_trap_t)))) {
  1177. return NULL;
  1178. }
  1179. if (!(trap->message = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1180. goto failed;
  1181. }
  1182. wasm_name_new_from_string_nt(trap->message, error_info);
  1183. if (strlen(error_info) && !trap->message->data) {
  1184. goto failed;
  1185. }
  1186. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1187. #if WASM_ENABLE_INTERP != 0
  1188. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  1189. trap->frames = ((WASMModuleInstance *)inst_comm_rt)->frames;
  1190. }
  1191. #endif
  1192. #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
  1193. /* allow a NULL frames list */
  1194. if (!trap->frames) {
  1195. return trap;
  1196. }
  1197. if (!(instances = singleton_engine->stores->data[0]->instances)) {
  1198. goto failed;
  1199. }
  1200. for (i = 0; i < instances->num_elems; i++) {
  1201. if (instances->data[i]->inst_comm_rt == inst_comm_rt) {
  1202. frame_instance = instances->data[i];
  1203. break;
  1204. }
  1205. }
  1206. for (i = 0; i < trap->frames->num_elems; i++) {
  1207. (((wasm_frame_t *)trap->frames->data) + i)->instance = frame_instance;
  1208. }
  1209. return trap;
  1210. failed:
  1211. wasm_trap_delete(trap);
  1212. return NULL;
  1213. }
  1214. wasm_trap_t *
  1215. wasm_trap_new(wasm_store_t *store, const wasm_message_t *message)
  1216. {
  1217. wasm_trap_t *trap;
  1218. if (!store || !message) {
  1219. return NULL;
  1220. }
  1221. if (!(trap = malloc_internal(sizeof(wasm_trap_t)))) {
  1222. return NULL;
  1223. }
  1224. INIT_VEC(trap->message, wasm_byte_vec_new, message->size, message->data);
  1225. return trap;
  1226. failed:
  1227. wasm_trap_delete(trap);
  1228. return NULL;
  1229. }
  1230. void
  1231. wasm_trap_delete(wasm_trap_t *trap)
  1232. {
  1233. if (!trap) {
  1234. return;
  1235. }
  1236. DEINIT_VEC(trap->message, wasm_byte_vec_delete);
  1237. /* reuse frames of WASMModuleInstance, do not free it here */
  1238. wasm_runtime_free(trap);
  1239. }
  1240. void
  1241. wasm_trap_message(const wasm_trap_t *trap, own wasm_message_t *out)
  1242. {
  1243. if (!trap || !out) {
  1244. return;
  1245. }
  1246. wasm_byte_vec_copy(out, trap->message);
  1247. }
  1248. own wasm_frame_t *
  1249. wasm_trap_origin(const wasm_trap_t *trap)
  1250. {
  1251. wasm_frame_t *latest_frame;
  1252. if (!trap || !trap->frames || !trap->frames->num_elems) {
  1253. return NULL;
  1254. }
  1255. /* first frame is the latest frame */
  1256. latest_frame = (wasm_frame_t *)trap->frames->data;
  1257. return wasm_frame_copy(latest_frame);
  1258. }
  1259. void
  1260. wasm_trap_trace(const wasm_trap_t *trap, own wasm_frame_vec_t *out)
  1261. {
  1262. uint32 i;
  1263. if (!trap || !out) {
  1264. return;
  1265. }
  1266. if (!trap->frames || !trap->frames->num_elems) {
  1267. wasm_frame_vec_new_empty(out);
  1268. return;
  1269. }
  1270. wasm_frame_vec_new_uninitialized(out, trap->frames->num_elems);
  1271. if (out->size && !out->data) {
  1272. return;
  1273. }
  1274. for (i = 0; i < trap->frames->num_elems; i++) {
  1275. wasm_frame_t *frame;
  1276. frame = ((wasm_frame_t *)trap->frames->data) + i;
  1277. if (!(out->data[i] =
  1278. wasm_frame_new(frame->instance, frame->module_offset,
  1279. frame->func_index, frame->func_offset))) {
  1280. goto failed;
  1281. }
  1282. out->num_elems++;
  1283. }
  1284. return;
  1285. failed:
  1286. for (i = 0; i < out->num_elems; i++) {
  1287. if (out->data[i]) {
  1288. wasm_runtime_free(out->data[i]);
  1289. }
  1290. }
  1291. if (out->data) {
  1292. wasm_runtime_free(out->data);
  1293. }
  1294. }
  1295. struct wasm_module_ex_t {
  1296. struct WASMModuleCommon *module_comm_rt;
  1297. wasm_byte_vec_t *binary;
  1298. };
  1299. static inline wasm_module_t *
  1300. module_ext_to_module(wasm_module_ex_t *module_ex)
  1301. {
  1302. return (wasm_module_t *)module_ex;
  1303. }
  1304. static inline wasm_module_ex_t *
  1305. module_to_module_ext(wasm_module_t *module)
  1306. {
  1307. return (wasm_module_ex_t *)module;
  1308. }
  1309. #if WASM_ENABLE_INTERP != 0
  1310. #define MODULE_INTERP(module) ((WASMModule *)(*module))
  1311. #endif
  1312. #if WASM_ENABLE_AOT != 0
  1313. #define MODULE_AOT(module) ((AOTModule *)(*module))
  1314. #endif
  1315. wasm_module_t *
  1316. wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
  1317. {
  1318. char error_buf[128] = { 0 };
  1319. wasm_module_ex_t *module_ex = NULL;
  1320. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  1321. uint8 *aot_file_buf = NULL;
  1322. uint32 aot_file_size;
  1323. #endif
  1324. bh_assert(singleton_engine);
  1325. if (!store || !binary || binary->size > UINT32_MAX) {
  1326. LOG_ERROR("%s failed", __FUNCTION__);
  1327. return NULL;
  1328. }
  1329. module_ex = malloc_internal(sizeof(wasm_module_ex_t));
  1330. if (!module_ex) {
  1331. goto failed;
  1332. }
  1333. INIT_VEC(module_ex->binary, wasm_byte_vec_new, binary->size, binary->data);
  1334. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  1335. if (get_package_type((uint8 *)module_ex->binary->data,
  1336. (uint32)module_ex->binary->size)
  1337. == Wasm_Module_Bytecode) {
  1338. if (!(aot_file_buf = aot_compile_wasm_file(
  1339. (uint8 *)module_ex->binary->data,
  1340. (uint32)module_ex->binary->size, 3, 3, error_buf,
  1341. (uint32)sizeof(error_buf), &aot_file_size))) {
  1342. LOG_ERROR(error_buf);
  1343. goto failed;
  1344. }
  1345. if (!(module_ex->module_comm_rt =
  1346. wasm_runtime_load(aot_file_buf, aot_file_size, error_buf,
  1347. (uint32)sizeof(error_buf)))) {
  1348. LOG_ERROR(error_buf);
  1349. goto failed;
  1350. }
  1351. }
  1352. else
  1353. #endif
  1354. {
  1355. module_ex->module_comm_rt = wasm_runtime_load(
  1356. (uint8 *)module_ex->binary->data, (uint32)module_ex->binary->size,
  1357. error_buf, (uint32)sizeof(error_buf));
  1358. if (!(module_ex->module_comm_rt)) {
  1359. LOG_ERROR(error_buf);
  1360. goto failed;
  1361. }
  1362. }
  1363. /* add it to a watching list in store */
  1364. if (!bh_vector_append((Vector *)store->modules, &module_ex)) {
  1365. goto failed;
  1366. }
  1367. return module_ext_to_module(module_ex);
  1368. failed:
  1369. LOG_ERROR("%s failed", __FUNCTION__);
  1370. wasm_module_delete_internal(module_ext_to_module(module_ex));
  1371. return NULL;
  1372. }
  1373. static void
  1374. wasm_module_delete_internal(wasm_module_t *module)
  1375. {
  1376. wasm_module_ex_t *module_ex;
  1377. if (!module) {
  1378. return;
  1379. }
  1380. module_ex = module_to_module_ext(module);
  1381. DEINIT_VEC(module_ex->binary, wasm_byte_vec_delete);
  1382. if (module_ex->module_comm_rt) {
  1383. wasm_runtime_unload(module_ex->module_comm_rt);
  1384. module_ex->module_comm_rt = NULL;
  1385. }
  1386. wasm_runtime_free(module_ex);
  1387. }
  1388. void
  1389. wasm_module_delete(wasm_module_t *module)
  1390. {
  1391. /* the module will be released when releasing the store */
  1392. }
  1393. void
  1394. wasm_module_imports(const wasm_module_t *module,
  1395. own wasm_importtype_vec_t *out)
  1396. {
  1397. uint32 i, import_func_count = 0, import_memory_count = 0,
  1398. import_global_count = 0, import_table_count = 0,
  1399. import_count = 0;
  1400. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  1401. wasm_externtype_t *extern_type = NULL;
  1402. wasm_importtype_t *import_type = NULL;
  1403. if (!module || !out || !valid_module_type((*module)->module_type)) {
  1404. return;
  1405. }
  1406. #if WASM_ENABLE_INTERP != 0
  1407. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1408. import_func_count = MODULE_INTERP(module)->import_function_count;
  1409. import_global_count = MODULE_INTERP(module)->import_global_count;
  1410. import_memory_count = MODULE_INTERP(module)->import_memory_count;
  1411. import_table_count = MODULE_INTERP(module)->import_table_count;
  1412. }
  1413. #endif
  1414. #if WASM_ENABLE_AOT != 0
  1415. if ((*module)->module_type == Wasm_Module_AoT) {
  1416. import_func_count = MODULE_AOT(module)->import_func_count;
  1417. import_global_count = MODULE_AOT(module)->import_global_count;
  1418. import_memory_count = MODULE_AOT(module)->import_memory_count;
  1419. import_table_count = MODULE_AOT(module)->import_table_count;
  1420. }
  1421. #endif
  1422. import_count = import_func_count + import_global_count + import_table_count
  1423. + import_memory_count;
  1424. wasm_importtype_vec_new_uninitialized(out, import_count);
  1425. if (import_count && !out->data) {
  1426. return;
  1427. }
  1428. for (i = 0; i != import_count; ++i) {
  1429. char *module_name_rt = NULL, *field_name_rt = NULL;
  1430. if (i < import_func_count) {
  1431. wasm_functype_t *type = NULL;
  1432. WASMType *type_rt = NULL;
  1433. #if WASM_ENABLE_INTERP != 0
  1434. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1435. WASMImport *import =
  1436. MODULE_INTERP(module)->import_functions + i;
  1437. module_name_rt = import->u.names.module_name;
  1438. field_name_rt = import->u.names.field_name;
  1439. type_rt = import->u.function.func_type;
  1440. }
  1441. #endif
  1442. #if WASM_ENABLE_AOT != 0
  1443. if ((*module)->module_type == Wasm_Module_AoT) {
  1444. AOTImportFunc *import = MODULE_AOT(module)->import_funcs + i;
  1445. module_name_rt = import->module_name;
  1446. field_name_rt = import->func_name;
  1447. type_rt = import->func_type;
  1448. }
  1449. #endif
  1450. if (!module_name_rt || !field_name_rt || !type_rt) {
  1451. continue;
  1452. }
  1453. wasm_name_new_from_string(&module_name, module_name_rt);
  1454. if (strlen(module_name_rt) && !module_name.data) {
  1455. goto failed;
  1456. }
  1457. wasm_name_new_from_string(&name, field_name_rt);
  1458. if (strlen(field_name_rt) && !name.data) {
  1459. goto failed;
  1460. }
  1461. if (!(type = wasm_functype_new_internal(type_rt))) {
  1462. goto failed;
  1463. }
  1464. extern_type = wasm_functype_as_externtype(type);
  1465. }
  1466. else if (i < import_func_count + import_global_count) {
  1467. wasm_globaltype_t *type = NULL;
  1468. uint8 val_type_rt = 0;
  1469. bool mutability_rt = 0;
  1470. #if WASM_ENABLE_INTERP != 0
  1471. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1472. WASMImport *import = MODULE_INTERP(module)->import_globals
  1473. + (i - import_func_count);
  1474. module_name_rt = import->u.names.module_name;
  1475. field_name_rt = import->u.names.field_name;
  1476. val_type_rt = import->u.global.type;
  1477. mutability_rt = import->u.global.is_mutable;
  1478. }
  1479. #endif
  1480. #if WASM_ENABLE_AOT != 0
  1481. if ((*module)->module_type == Wasm_Module_AoT) {
  1482. AOTImportGlobal *import =
  1483. MODULE_AOT(module)->import_globals + (i - import_func_count);
  1484. module_name_rt = import->module_name;
  1485. field_name_rt = import->global_name;
  1486. val_type_rt = import->type;
  1487. mutability_rt = import->is_mutable;
  1488. }
  1489. #endif
  1490. if (!module_name_rt || !field_name_rt) {
  1491. continue;
  1492. }
  1493. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  1494. mutability_rt))) {
  1495. goto failed;
  1496. }
  1497. extern_type = wasm_globaltype_as_externtype(type);
  1498. }
  1499. else if (i < import_func_count + import_global_count
  1500. + import_memory_count) {
  1501. wasm_memorytype_t *type = NULL;
  1502. uint32 min_page = 0, max_page = 0;
  1503. #if WASM_ENABLE_INTERP != 0
  1504. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1505. WASMImport *import =
  1506. MODULE_INTERP(module)->import_memories
  1507. + (i - import_func_count - import_global_count);
  1508. module_name_rt = import->u.names.module_name;
  1509. field_name_rt = import->u.names.field_name;
  1510. min_page = import->u.memory.init_page_count;
  1511. max_page = import->u.memory.max_page_count;
  1512. }
  1513. #endif
  1514. #if WASM_ENABLE_AOT != 0
  1515. if ((*module)->module_type == Wasm_Module_AoT) {
  1516. AOTImportMemory *import =
  1517. MODULE_AOT(module)->import_memories
  1518. + (i - import_func_count - import_global_count);
  1519. module_name_rt = import->module_name;
  1520. field_name_rt = import->memory_name;
  1521. min_page = import->mem_init_page_count;
  1522. max_page = import->mem_max_page_count;
  1523. }
  1524. #endif
  1525. if (!module_name_rt || !field_name_rt) {
  1526. continue;
  1527. }
  1528. wasm_name_new_from_string(&module_name, module_name_rt);
  1529. if (strlen(module_name_rt) && !module_name.data) {
  1530. goto failed;
  1531. }
  1532. wasm_name_new_from_string(&name, field_name_rt);
  1533. if (strlen(field_name_rt) && !name.data) {
  1534. goto failed;
  1535. }
  1536. if (!(type = wasm_memorytype_new_internal(min_page, max_page))) {
  1537. goto failed;
  1538. }
  1539. extern_type = wasm_memorytype_as_externtype(type);
  1540. }
  1541. else {
  1542. wasm_tabletype_t *type = NULL;
  1543. uint8 elem_type_rt = 0;
  1544. uint32 min_size = 0, max_size = 0;
  1545. #if WASM_ENABLE_INTERP != 0
  1546. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1547. WASMImport *import =
  1548. MODULE_INTERP(module)->import_tables
  1549. + (i - import_func_count - import_global_count
  1550. - import_memory_count);
  1551. module_name_rt = import->u.names.module_name;
  1552. field_name_rt = import->u.names.field_name;
  1553. elem_type_rt = import->u.table.elem_type;
  1554. min_size = import->u.table.init_size;
  1555. max_size = import->u.table.max_size;
  1556. }
  1557. #endif
  1558. #if WASM_ENABLE_AOT != 0
  1559. if ((*module)->module_type == Wasm_Module_AoT) {
  1560. AOTImportTable *import =
  1561. MODULE_AOT(module)->import_tables
  1562. + (i - import_func_count - import_global_count
  1563. - import_memory_count);
  1564. module_name_rt = import->module_name;
  1565. field_name_rt = import->table_name;
  1566. elem_type_rt = VALUE_TYPE_FUNCREF;
  1567. min_size = import->table_init_size;
  1568. max_size = import->table_max_size;
  1569. }
  1570. #endif
  1571. if (!module_name_rt || !field_name_rt) {
  1572. continue;
  1573. }
  1574. if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size,
  1575. max_size))) {
  1576. goto failed;
  1577. }
  1578. extern_type = wasm_tabletype_as_externtype(type);
  1579. }
  1580. if (!extern_type) {
  1581. continue;
  1582. }
  1583. if (!(import_type =
  1584. wasm_importtype_new(&module_name, &name, extern_type))) {
  1585. goto failed;
  1586. }
  1587. if (!bh_vector_append((Vector *)out, &import_type)) {
  1588. goto failed_importtype_new;
  1589. }
  1590. }
  1591. return;
  1592. failed:
  1593. wasm_byte_vec_delete(&module_name);
  1594. wasm_byte_vec_delete(&name);
  1595. wasm_externtype_delete(extern_type);
  1596. failed_importtype_new:
  1597. wasm_importtype_delete(import_type);
  1598. wasm_importtype_vec_delete(out);
  1599. }
  1600. void
  1601. wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out)
  1602. {
  1603. uint32 i, export_count = 0;
  1604. wasm_byte_vec_t name = { 0 };
  1605. wasm_externtype_t *extern_type = NULL;
  1606. wasm_exporttype_t *export_type = NULL;
  1607. if (!module || !out || !valid_module_type((*module)->module_type)) {
  1608. return;
  1609. }
  1610. #if WASM_ENABLE_INTERP != 0
  1611. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1612. export_count = MODULE_INTERP(module)->export_count;
  1613. }
  1614. #endif
  1615. #if WASM_ENABLE_AOT != 0
  1616. if ((*module)->module_type == Wasm_Module_AoT) {
  1617. export_count = MODULE_AOT(module)->export_count;
  1618. }
  1619. #endif
  1620. wasm_exporttype_vec_new_uninitialized(out, export_count);
  1621. if (export_count && !out->data) {
  1622. return;
  1623. }
  1624. for (i = 0; i != export_count; i++) {
  1625. WASMExport *export = NULL;
  1626. #if WASM_ENABLE_INTERP != 0
  1627. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1628. export = MODULE_INTERP(module)->exports + i;
  1629. }
  1630. #endif
  1631. #if WASM_ENABLE_AOT != 0
  1632. if ((*module)->module_type == Wasm_Module_AoT) {
  1633. export = MODULE_AOT(module)->exports + i;
  1634. }
  1635. #endif
  1636. if (!export) {
  1637. continue;
  1638. }
  1639. /* byte* -> wasm_byte_vec_t */
  1640. wasm_name_new_from_string(&name, export->name);
  1641. if (strlen(export->name) && !name.data) {
  1642. goto failed;
  1643. }
  1644. /* WASMExport -> (WASMType, (uint8, bool)) -> (wasm_functype_t, wasm_globaltype_t) -> wasm_externtype_t*/
  1645. switch (export->kind) {
  1646. case EXPORT_KIND_FUNC:
  1647. {
  1648. wasm_functype_t *type = NULL;
  1649. WASMType *type_rt;
  1650. if (!wasm_runtime_get_export_func_type(*module, export,
  1651. &type_rt)) {
  1652. goto failed;
  1653. }
  1654. if (!(type = wasm_functype_new_internal(type_rt))) {
  1655. goto failed;
  1656. }
  1657. extern_type = wasm_functype_as_externtype(type);
  1658. break;
  1659. }
  1660. case EXPORT_KIND_GLOBAL:
  1661. {
  1662. wasm_globaltype_t *type = NULL;
  1663. uint8 val_type_rt = 0;
  1664. bool mutability_rt = 0;
  1665. if (!wasm_runtime_get_export_global_type(
  1666. *module, export, &val_type_rt, &mutability_rt)) {
  1667. goto failed;
  1668. }
  1669. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  1670. mutability_rt))) {
  1671. goto failed;
  1672. }
  1673. extern_type = wasm_globaltype_as_externtype(type);
  1674. break;
  1675. }
  1676. case EXPORT_KIND_MEMORY:
  1677. {
  1678. wasm_memorytype_t *type = NULL;
  1679. uint32 min_page = 0, max_page = 0;
  1680. if (!wasm_runtime_get_export_memory_type(
  1681. *module, export, &min_page, &max_page)) {
  1682. goto failed;
  1683. }
  1684. if (!(type =
  1685. wasm_memorytype_new_internal(min_page, max_page))) {
  1686. goto failed;
  1687. }
  1688. extern_type = wasm_memorytype_as_externtype(type);
  1689. break;
  1690. }
  1691. case EXPORT_KIND_TABLE:
  1692. {
  1693. wasm_tabletype_t *type = NULL;
  1694. uint8 elem_type_rt = 0;
  1695. uint32 min_size = 0, max_size = 0;
  1696. if (!wasm_runtime_get_export_table_type(
  1697. *module, export, &elem_type_rt, &min_size, &max_size)) {
  1698. goto failed;
  1699. }
  1700. if (!(type = wasm_tabletype_new_internal(
  1701. elem_type_rt, min_size, max_size))) {
  1702. goto failed;
  1703. }
  1704. extern_type = wasm_tabletype_as_externtype(type);
  1705. break;
  1706. }
  1707. default:
  1708. {
  1709. LOG_WARNING("%s meets unsupported type", __FUNCTION__,
  1710. export->kind);
  1711. break;
  1712. }
  1713. }
  1714. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  1715. goto failed;
  1716. }
  1717. if (!(bh_vector_append((Vector *)out, &export_type))) {
  1718. goto failed_exporttype_new;
  1719. }
  1720. }
  1721. return;
  1722. failed:
  1723. wasm_byte_vec_delete(&name);
  1724. wasm_externtype_delete(extern_type);
  1725. failed_exporttype_new:
  1726. wasm_exporttype_delete(export_type);
  1727. wasm_exporttype_vec_delete(out);
  1728. }
  1729. static wasm_func_t *
  1730. wasm_func_new_basic(const wasm_functype_t *type,
  1731. wasm_func_callback_t func_callback)
  1732. {
  1733. wasm_func_t *func = NULL;
  1734. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  1735. goto failed;
  1736. }
  1737. func->kind = WASM_EXTERN_FUNC;
  1738. func->with_env = false;
  1739. func->u.cb = func_callback;
  1740. if (!(func->type = wasm_functype_copy(type))) {
  1741. goto failed;
  1742. }
  1743. RETURN_OBJ(func, wasm_func_delete)
  1744. }
  1745. static wasm_func_t *
  1746. wasm_func_new_with_env_basic(const wasm_functype_t *type,
  1747. wasm_func_callback_with_env_t callback,
  1748. void *env,
  1749. void (*finalizer)(void *))
  1750. {
  1751. wasm_func_t *func = NULL;
  1752. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  1753. goto failed;
  1754. }
  1755. func->kind = WASM_EXTERN_FUNC;
  1756. func->with_env = true;
  1757. func->u.cb_env.cb = callback;
  1758. func->u.cb_env.env = env;
  1759. func->u.cb_env.finalizer = finalizer;
  1760. if (!(func->type = wasm_functype_copy(type))) {
  1761. goto failed;
  1762. }
  1763. RETURN_OBJ(func, wasm_func_delete)
  1764. }
  1765. wasm_func_t *
  1766. wasm_func_new(wasm_store_t *store,
  1767. const wasm_functype_t *type,
  1768. wasm_func_callback_t callback)
  1769. {
  1770. bh_assert(singleton_engine);
  1771. return wasm_func_new_basic(type, callback);
  1772. }
  1773. wasm_func_t *
  1774. wasm_func_new_with_env(wasm_store_t *store,
  1775. const wasm_functype_t *type,
  1776. wasm_func_callback_with_env_t callback,
  1777. void *env,
  1778. void (*finalizer)(void *))
  1779. {
  1780. bh_assert(singleton_engine);
  1781. return wasm_func_new_with_env_basic(type, callback, env, finalizer);
  1782. }
  1783. static wasm_func_t *
  1784. wasm_func_new_internal(wasm_store_t *store,
  1785. uint16 func_idx_rt,
  1786. WASMModuleInstanceCommon *inst_comm_rt)
  1787. {
  1788. wasm_func_t *func = NULL;
  1789. WASMType *type_rt = NULL;
  1790. bh_assert(singleton_engine);
  1791. if (!inst_comm_rt || !valid_module_type(inst_comm_rt->module_type)) {
  1792. return NULL;
  1793. }
  1794. func = malloc_internal(sizeof(wasm_func_t));
  1795. if (!func) {
  1796. goto failed;
  1797. }
  1798. func->kind = WASM_EXTERN_FUNC;
  1799. #if WASM_ENABLE_INTERP != 0
  1800. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  1801. bh_assert(func_idx_rt
  1802. < ((WASMModuleInstance *)inst_comm_rt)->function_count);
  1803. WASMFunctionInstance *func_interp =
  1804. ((WASMModuleInstance *)inst_comm_rt)->functions + func_idx_rt;
  1805. type_rt = func_interp->is_import_func
  1806. ? func_interp->u.func_import->func_type
  1807. : func_interp->u.func->func_type;
  1808. }
  1809. #endif
  1810. #if WASM_ENABLE_AOT != 0
  1811. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  1812. /* use same index to trace the function type in AOTFuncType **func_types */
  1813. AOTModule *module_aot =
  1814. ((AOTModuleInstance *)inst_comm_rt)->aot_module.ptr;
  1815. if (func_idx_rt < module_aot->import_func_count) {
  1816. type_rt = (module_aot->import_funcs + func_idx_rt)->func_type;
  1817. }
  1818. else {
  1819. type_rt =
  1820. module_aot
  1821. ->func_types[module_aot->func_type_indexes
  1822. [func_idx_rt - module_aot->import_func_count]];
  1823. }
  1824. }
  1825. #endif
  1826. if (!type_rt) {
  1827. goto failed;
  1828. }
  1829. func->type = wasm_functype_new_internal(type_rt);
  1830. if (!func->type) {
  1831. goto failed;
  1832. }
  1833. /* will add name information when processing "exports" */
  1834. func->module_name = NULL;
  1835. func->name = NULL;
  1836. func->func_idx_rt = func_idx_rt;
  1837. func->inst_comm_rt = inst_comm_rt;
  1838. return func;
  1839. failed:
  1840. LOG_DEBUG("%s failed", __FUNCTION__);
  1841. wasm_func_delete(func);
  1842. return NULL;
  1843. }
  1844. void
  1845. wasm_func_delete(wasm_func_t *func)
  1846. {
  1847. if (!func) {
  1848. return;
  1849. }
  1850. if (func->type) {
  1851. wasm_functype_delete(func->type);
  1852. func->type = NULL;
  1853. }
  1854. if (func->with_env) {
  1855. if (func->u.cb_env.finalizer) {
  1856. func->u.cb_env.finalizer(func->u.cb_env.env);
  1857. func->u.cb_env.finalizer = NULL;
  1858. func->u.cb_env.env = NULL;
  1859. }
  1860. }
  1861. wasm_runtime_free(func);
  1862. }
  1863. own wasm_func_t *
  1864. wasm_func_copy(const wasm_func_t *func)
  1865. {
  1866. wasm_func_t *cloned = NULL;
  1867. if (!func) {
  1868. return NULL;
  1869. }
  1870. if (!(cloned = func->with_env
  1871. ? wasm_func_new_with_env_basic(
  1872. func->type, func->u.cb_env.cb, func->u.cb_env.env,
  1873. func->u.cb_env.finalizer)
  1874. : wasm_func_new_basic(func->type, func->u.cb))) {
  1875. goto failed;
  1876. }
  1877. cloned->func_idx_rt = func->func_idx_rt;
  1878. cloned->inst_comm_rt = func->inst_comm_rt;
  1879. RETURN_OBJ(cloned, wasm_func_delete)
  1880. }
  1881. own wasm_functype_t *
  1882. wasm_func_type(const wasm_func_t *func)
  1883. {
  1884. if (!func) {
  1885. return NULL;
  1886. }
  1887. return wasm_functype_copy(func->type);
  1888. }
  1889. static uint32
  1890. params_to_argv(const wasm_val_t *params,
  1891. const wasm_valtype_vec_t *param_defs,
  1892. size_t param_arity,
  1893. uint32 *out)
  1894. {
  1895. size_t i = 0;
  1896. uint32 argc = 0;
  1897. const wasm_val_t *param = NULL;
  1898. if (!param_arity) {
  1899. return 0;
  1900. }
  1901. bh_assert(params && param_defs && out);
  1902. bh_assert(param_defs->num_elems == param_arity);
  1903. for (i = 0; out && i < param_arity; ++i) {
  1904. param = params + i;
  1905. bh_assert((*(param_defs->data + i))->kind == param->kind);
  1906. switch (param->kind) {
  1907. case WASM_I32:
  1908. *(int32 *)out = param->of.i32;
  1909. out += 1;
  1910. argc += 1;
  1911. break;
  1912. case WASM_I64:
  1913. *(int64 *)out = param->of.i64;
  1914. out += 2;
  1915. argc += 2;
  1916. break;
  1917. case WASM_F32:
  1918. *(float32 *)out = param->of.f32;
  1919. out += 1;
  1920. argc += 1;
  1921. break;
  1922. case WASM_F64:
  1923. *(float64 *)out = param->of.f64;
  1924. out += 2;
  1925. argc += 2;
  1926. break;
  1927. default:
  1928. LOG_DEBUG("unexpected parameter val type %d", param->kind);
  1929. goto failed;
  1930. }
  1931. }
  1932. return argc;
  1933. failed:
  1934. LOG_DEBUG("%s failed", __FUNCTION__);
  1935. return 0;
  1936. }
  1937. static uint32
  1938. argv_to_results(const uint32 *results,
  1939. const wasm_valtype_vec_t *result_defs,
  1940. size_t result_arity,
  1941. wasm_val_t *out)
  1942. {
  1943. size_t i = 0;
  1944. uint32 argc = 0;
  1945. const uint32 *result = results;
  1946. const wasm_valtype_t *def = NULL;
  1947. if (!result_arity) {
  1948. return 0;
  1949. }
  1950. bh_assert(results && result_defs && out);
  1951. bh_assert(result_arity == result_defs->num_elems);
  1952. for (i = 0; out && i < result_arity; i++) {
  1953. def = *(result_defs->data + i);
  1954. switch (def->kind) {
  1955. case WASM_I32:
  1956. {
  1957. out->kind = WASM_I32;
  1958. out->of.i32 = *(int32 *)result;
  1959. result += 1;
  1960. break;
  1961. }
  1962. case WASM_I64:
  1963. {
  1964. out->kind = WASM_I64;
  1965. out->of.i64 = *(int64 *)result;
  1966. result += 2;
  1967. break;
  1968. }
  1969. case WASM_F32:
  1970. {
  1971. out->kind = WASM_F32;
  1972. out->of.f32 = *(float32 *)result;
  1973. result += 1;
  1974. break;
  1975. }
  1976. case WASM_F64:
  1977. {
  1978. out->kind = WASM_F64;
  1979. out->of.f64 = *(float64 *)result;
  1980. result += 2;
  1981. break;
  1982. }
  1983. default:
  1984. LOG_WARNING("%s meets unsupported type: %d", __FUNCTION__,
  1985. def->kind);
  1986. goto failed;
  1987. }
  1988. out++;
  1989. argc++;
  1990. }
  1991. return argc;
  1992. failed:
  1993. LOG_DEBUG("%s failed", __FUNCTION__);
  1994. return 0;
  1995. }
  1996. wasm_trap_t *
  1997. wasm_func_call(const wasm_func_t *func,
  1998. const wasm_val_t params[],
  1999. wasm_val_t results[])
  2000. {
  2001. /* parameters count as if all are uint32 */
  2002. /* a int64 or float64 parameter means 2 */
  2003. uint32 argc = 0;
  2004. /* a parameter list and a return value list */
  2005. uint32 argv_buf[32], *argv = argv_buf;
  2006. WASMFunctionInstanceCommon *func_comm_rt = NULL;
  2007. WASMExecEnv *exec_env = NULL;
  2008. size_t param_count, result_count, alloc_count;
  2009. bh_assert(func && func->type && func->inst_comm_rt);
  2010. #if WASM_ENABLE_INTERP != 0
  2011. if (func->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2012. func_comm_rt = ((WASMModuleInstance *)func->inst_comm_rt)->functions
  2013. + func->func_idx_rt;
  2014. }
  2015. #endif
  2016. #if WASM_ENABLE_AOT != 0
  2017. if (func->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2018. if (!(func_comm_rt = func->func_comm_rt)) {
  2019. AOTModuleInstance *inst_aot =
  2020. (AOTModuleInstance *)func->inst_comm_rt;
  2021. AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  2022. uint32 export_i = 0, export_func_j = 0;
  2023. for (; export_i < module_aot->export_count; ++export_i) {
  2024. AOTExport *export = module_aot->exports + export_i;
  2025. if (export->kind == EXPORT_KIND_FUNC) {
  2026. if (export->index == func->func_idx_rt) {
  2027. func_comm_rt =
  2028. (AOTFunctionInstance *)inst_aot->export_funcs.ptr
  2029. + export_func_j;
  2030. ((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
  2031. break;
  2032. }
  2033. export_func_j++;
  2034. }
  2035. }
  2036. }
  2037. }
  2038. #endif
  2039. if (!func_comm_rt) {
  2040. goto failed;
  2041. }
  2042. param_count = wasm_func_param_arity(func);
  2043. result_count = wasm_func_result_arity(func);
  2044. alloc_count = (param_count > result_count) ? param_count : result_count;
  2045. if (alloc_count > sizeof(argv_buf) / sizeof(uint64)) {
  2046. if (!(argv = malloc_internal(sizeof(uint64) * alloc_count))) {
  2047. goto failed;
  2048. }
  2049. }
  2050. /* copy parametes */
  2051. if (param_count
  2052. && !(argc = params_to_argv(params, wasm_functype_params(func->type),
  2053. param_count, argv))) {
  2054. goto failed;
  2055. }
  2056. exec_env = wasm_runtime_get_exec_env_singleton(func->inst_comm_rt);
  2057. if (!exec_env) {
  2058. goto failed;
  2059. }
  2060. if (!wasm_runtime_call_wasm(exec_env, func_comm_rt, argc, argv)) {
  2061. if (wasm_runtime_get_exception(func->inst_comm_rt)) {
  2062. LOG_DEBUG(wasm_runtime_get_exception(func->inst_comm_rt));
  2063. goto failed;
  2064. }
  2065. }
  2066. /* copy results */
  2067. if (result_count) {
  2068. if (!(argc = argv_to_results(argv, wasm_functype_results(func->type),
  2069. result_count, results))) {
  2070. goto failed;
  2071. }
  2072. }
  2073. if (argv != argv_buf)
  2074. wasm_runtime_free(argv);
  2075. return NULL;
  2076. failed:
  2077. if (argv != argv_buf)
  2078. wasm_runtime_free(argv);
  2079. /* trap -> exception -> trap */
  2080. if (wasm_runtime_get_exception(func->inst_comm_rt)) {
  2081. return wasm_trap_new_internal(func->inst_comm_rt, NULL);
  2082. }
  2083. else {
  2084. return wasm_trap_new_internal(func->inst_comm_rt,
  2085. "wasm_func_call failed");
  2086. }
  2087. }
  2088. size_t
  2089. wasm_func_param_arity(const wasm_func_t *func)
  2090. {
  2091. if (!func || !func->type || !func->type->params) {
  2092. return 0;
  2093. }
  2094. return func->type->params->num_elems;
  2095. }
  2096. size_t
  2097. wasm_func_result_arity(const wasm_func_t *func)
  2098. {
  2099. if (!func || !func->type || !func->type->results) {
  2100. return 0;
  2101. }
  2102. return func->type->results->num_elems;
  2103. }
  2104. wasm_global_t *
  2105. wasm_global_new(wasm_store_t *store,
  2106. const wasm_globaltype_t *global_type,
  2107. const wasm_val_t *init)
  2108. {
  2109. wasm_global_t *global = NULL;
  2110. bh_assert(singleton_engine);
  2111. global = malloc_internal(sizeof(wasm_global_t));
  2112. if (!global) {
  2113. goto failed;
  2114. }
  2115. global->kind = WASM_EXTERN_GLOBAL;
  2116. global->type = wasm_globaltype_copy(global_type);
  2117. if (!global->type) {
  2118. goto failed;
  2119. }
  2120. global->init = malloc_internal(sizeof(wasm_val_t));
  2121. if (!global->init) {
  2122. goto failed;
  2123. }
  2124. wasm_val_copy(global->init, init);
  2125. /* TODO: how to check if above is failed */
  2126. return global;
  2127. failed:
  2128. LOG_DEBUG("%s failed", __FUNCTION__);
  2129. wasm_global_delete(global);
  2130. return NULL;
  2131. }
  2132. /* almost same with wasm_global_new */
  2133. wasm_global_t *
  2134. wasm_global_copy(const wasm_global_t *src)
  2135. {
  2136. wasm_global_t *global = NULL;
  2137. if (!src) {
  2138. return NULL;
  2139. }
  2140. global = malloc_internal(sizeof(wasm_global_t));
  2141. if (!global) {
  2142. goto failed;
  2143. }
  2144. global->kind = WASM_EXTERN_GLOBAL;
  2145. global->type = wasm_globaltype_copy(src->type);
  2146. if (!global->type) {
  2147. goto failed;
  2148. }
  2149. global->init = malloc_internal(sizeof(wasm_val_t));
  2150. if (!global->init) {
  2151. goto failed;
  2152. }
  2153. wasm_val_copy(global->init, src->init);
  2154. global->global_idx_rt = src->global_idx_rt;
  2155. global->inst_comm_rt = src->inst_comm_rt;
  2156. return global;
  2157. failed:
  2158. LOG_DEBUG("%s failed", __FUNCTION__);
  2159. wasm_global_delete(global);
  2160. return NULL;
  2161. }
  2162. void
  2163. wasm_global_delete(wasm_global_t *global)
  2164. {
  2165. if (!global) {
  2166. return;
  2167. }
  2168. if (global->init) {
  2169. wasm_val_delete(global->init);
  2170. global->init = NULL;
  2171. }
  2172. if (global->type) {
  2173. wasm_globaltype_delete(global->type);
  2174. global->type = NULL;
  2175. }
  2176. wasm_runtime_free(global);
  2177. }
  2178. bool
  2179. wasm_global_same(const wasm_global_t *g1, const wasm_global_t *g2)
  2180. {
  2181. if (!g1 && !g2) {
  2182. return true;
  2183. }
  2184. if (!g1 || !g2) {
  2185. return false;
  2186. }
  2187. return g1->kind == g2->kind && wasm_globaltype_same(g1->type, g2->type)
  2188. && wasm_val_same(g1->init, g2->init);
  2189. }
  2190. #if WASM_ENABLE_INTERP != 0
  2191. static bool
  2192. interp_global_set(const WASMModuleInstance *inst_interp,
  2193. uint16 global_idx_rt,
  2194. const wasm_val_t *v)
  2195. {
  2196. const WASMGlobalInstance *global_interp =
  2197. inst_interp->globals + global_idx_rt;
  2198. uint8 val_type_rt = global_interp->type;
  2199. #if WASM_ENABLE_MULTI_MODULE != 0
  2200. uint8 *data = global_interp->import_global_inst
  2201. ? global_interp->import_module_inst->global_data
  2202. + global_interp->import_global_inst->data_offset
  2203. : inst_interp->global_data + global_interp->data_offset;
  2204. #else
  2205. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2206. #endif
  2207. bool ret = true;
  2208. switch (val_type_rt) {
  2209. case VALUE_TYPE_I32:
  2210. bh_assert(WASM_I32 == v->kind);
  2211. *((int32 *)data) = v->of.i32;
  2212. break;
  2213. case VALUE_TYPE_F32:
  2214. bh_assert(WASM_F32 == v->kind);
  2215. *((float32 *)data) = v->of.f32;
  2216. break;
  2217. case VALUE_TYPE_I64:
  2218. bh_assert(WASM_I64 == v->kind);
  2219. *((int64 *)data) = v->of.i64;
  2220. break;
  2221. case VALUE_TYPE_F64:
  2222. bh_assert(WASM_F64 == v->kind);
  2223. *((float64 *)data) = v->of.f64;
  2224. break;
  2225. default:
  2226. LOG_DEBUG("unexpected value type %d", val_type_rt);
  2227. ret = false;
  2228. break;
  2229. }
  2230. return ret;
  2231. }
  2232. static bool
  2233. interp_global_get(const WASMModuleInstance *inst_interp,
  2234. uint16 global_idx_rt,
  2235. wasm_val_t *out)
  2236. {
  2237. WASMGlobalInstance *global_interp = inst_interp->globals + global_idx_rt;
  2238. uint8 val_type_rt = global_interp->type;
  2239. #if WASM_ENABLE_MULTI_MODULE != 0
  2240. uint8 *data = global_interp->import_global_inst
  2241. ? global_interp->import_module_inst->global_data
  2242. + global_interp->import_global_inst->data_offset
  2243. : inst_interp->global_data + global_interp->data_offset;
  2244. #else
  2245. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2246. #endif
  2247. bool ret = true;
  2248. switch (val_type_rt) {
  2249. case VALUE_TYPE_I32:
  2250. out->kind = WASM_I32;
  2251. out->of.i32 = *((int32 *)data);
  2252. break;
  2253. case VALUE_TYPE_F32:
  2254. out->kind = WASM_F32;
  2255. out->of.f32 = *((float32 *)data);
  2256. break;
  2257. case VALUE_TYPE_I64:
  2258. out->kind = WASM_I64;
  2259. out->of.i64 = *((int64 *)data);
  2260. break;
  2261. case VALUE_TYPE_F64:
  2262. out->kind = WASM_F64;
  2263. out->of.f64 = *((float64 *)data);
  2264. break;
  2265. default:
  2266. LOG_DEBUG("unexpected value type %d", val_type_rt);
  2267. ret = false;
  2268. }
  2269. return ret;
  2270. }
  2271. #endif
  2272. #if WASM_ENABLE_AOT != 0
  2273. static bool
  2274. aot_global_set(const AOTModuleInstance *inst_aot,
  2275. uint16 global_idx_rt,
  2276. const wasm_val_t *v)
  2277. {
  2278. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2279. uint8 val_type_rt = 0;
  2280. uint32 data_offset = 0;
  2281. void *data = NULL;
  2282. bool ret = true;
  2283. if (global_idx_rt < module_aot->import_global_count) {
  2284. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2285. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2286. }
  2287. else {
  2288. data_offset =
  2289. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2290. .data_offset;
  2291. val_type_rt =
  2292. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2293. .type;
  2294. }
  2295. data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset);
  2296. switch (val_type_rt) {
  2297. case VALUE_TYPE_I32:
  2298. bh_assert(WASM_I32 == v->kind);
  2299. *((int32 *)data) = v->of.i32;
  2300. break;
  2301. case VALUE_TYPE_F32:
  2302. bh_assert(WASM_F32 == v->kind);
  2303. *((float32 *)data) = v->of.f32;
  2304. break;
  2305. case VALUE_TYPE_I64:
  2306. bh_assert(WASM_I64 == v->kind);
  2307. *((int64 *)data) = v->of.i64;
  2308. break;
  2309. case VALUE_TYPE_F64:
  2310. bh_assert(WASM_F64 == v->kind);
  2311. *((float64 *)data) = v->of.f64;
  2312. break;
  2313. default:
  2314. LOG_DEBUG("unexpected value type %d", val_type_rt);
  2315. ret = false;
  2316. }
  2317. return ret;
  2318. }
  2319. static bool
  2320. aot_global_get(const AOTModuleInstance *inst_aot,
  2321. uint16 global_idx_rt,
  2322. wasm_val_t *out)
  2323. {
  2324. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2325. uint8 val_type_rt = 0;
  2326. uint32 data_offset = 0;
  2327. void *data = NULL;
  2328. bool ret = true;
  2329. if (global_idx_rt < module_aot->import_global_count) {
  2330. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2331. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2332. }
  2333. else {
  2334. data_offset =
  2335. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2336. .data_offset;
  2337. val_type_rt =
  2338. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2339. .type;
  2340. }
  2341. data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset);
  2342. switch (val_type_rt) {
  2343. case VALUE_TYPE_I32:
  2344. out->kind = WASM_I32;
  2345. out->of.i32 = *((int32 *)data);
  2346. break;
  2347. case VALUE_TYPE_F32:
  2348. out->kind = WASM_F32;
  2349. out->of.f32 = *((float32 *)data);
  2350. break;
  2351. case VALUE_TYPE_I64:
  2352. out->kind = WASM_I64;
  2353. out->of.i64 = *((int64 *)data);
  2354. break;
  2355. case VALUE_TYPE_F64:
  2356. out->kind = WASM_F64;
  2357. out->of.f64 = *((float64 *)data);
  2358. break;
  2359. default:
  2360. LOG_DEBUG("unexpected value type %d", val_type_rt);
  2361. ret = false;
  2362. }
  2363. return ret;
  2364. }
  2365. #endif
  2366. void
  2367. wasm_global_set(wasm_global_t *global, const wasm_val_t *v)
  2368. {
  2369. if (!global || !v
  2370. || !valid_module_type(global->inst_comm_rt->module_type)) {
  2371. return;
  2372. }
  2373. #if WASM_ENABLE_INTERP != 0
  2374. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2375. (void)interp_global_set((WASMModuleInstance *)global->inst_comm_rt,
  2376. global->global_idx_rt, v);
  2377. }
  2378. #endif
  2379. #if WASM_ENABLE_AOT != 0
  2380. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2381. (void)aot_global_set((AOTModuleInstance *)global->inst_comm_rt,
  2382. global->global_idx_rt, v);
  2383. }
  2384. #endif
  2385. }
  2386. void
  2387. wasm_global_get(const wasm_global_t *global, wasm_val_t *out)
  2388. {
  2389. if (!global || !out
  2390. || !valid_module_type(global->inst_comm_rt->module_type)) {
  2391. return;
  2392. }
  2393. memset(out, 0, sizeof(wasm_val_t));
  2394. #if WASM_ENABLE_INTERP != 0
  2395. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2396. (void)interp_global_get((WASMModuleInstance *)global->inst_comm_rt,
  2397. global->global_idx_rt, out);
  2398. }
  2399. #endif
  2400. #if WASM_ENABLE_AOT != 0
  2401. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2402. (void)aot_global_get((AOTModuleInstance *)global->inst_comm_rt,
  2403. global->global_idx_rt, out);
  2404. }
  2405. #endif
  2406. bh_assert(global->init->kind == out->kind);
  2407. }
  2408. static wasm_global_t *
  2409. wasm_global_new_internal(wasm_store_t *store,
  2410. uint16 global_idx_rt,
  2411. WASMModuleInstanceCommon *inst_comm_rt)
  2412. {
  2413. wasm_global_t *global = NULL;
  2414. uint8 val_type_rt = 0;
  2415. bool is_mutable = 0;
  2416. bh_assert(singleton_engine);
  2417. if (!inst_comm_rt || !valid_module_type(inst_comm_rt->module_type)) {
  2418. return NULL;
  2419. }
  2420. global = malloc_internal(sizeof(wasm_global_t));
  2421. if (!global) {
  2422. goto failed;
  2423. }
  2424. /*
  2425. * global->module_name = NULL;
  2426. * global->name = NULL;
  2427. */
  2428. global->kind = WASM_EXTERN_GLOBAL;
  2429. #if WASM_ENABLE_INTERP != 0
  2430. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2431. WASMGlobalInstance *global_interp =
  2432. ((WASMModuleInstance *)inst_comm_rt)->globals + global_idx_rt;
  2433. val_type_rt = global_interp->type;
  2434. is_mutable = global_interp->is_mutable;
  2435. }
  2436. #endif
  2437. #if WASM_ENABLE_AOT != 0
  2438. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2439. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  2440. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2441. if (global_idx_rt < module_aot->import_global_count) {
  2442. AOTImportGlobal *global_import_aot =
  2443. module_aot->import_globals + global_idx_rt;
  2444. val_type_rt = global_import_aot->type;
  2445. is_mutable = global_import_aot->is_mutable;
  2446. }
  2447. else {
  2448. AOTGlobal *global_aot =
  2449. module_aot->globals
  2450. + (global_idx_rt - module_aot->import_global_count);
  2451. val_type_rt = global_aot->type;
  2452. is_mutable = global_aot->is_mutable;
  2453. }
  2454. }
  2455. #endif
  2456. global->type = wasm_globaltype_new_internal(val_type_rt, is_mutable);
  2457. if (!global->type) {
  2458. goto failed;
  2459. }
  2460. global->init = malloc_internal(sizeof(wasm_val_t));
  2461. if (!global->init) {
  2462. goto failed;
  2463. }
  2464. #if WASM_ENABLE_INTERP != 0
  2465. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2466. interp_global_get((WASMModuleInstance *)inst_comm_rt, global_idx_rt,
  2467. global->init);
  2468. }
  2469. #endif
  2470. #if WASM_ENABLE_AOT != 0
  2471. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2472. aot_global_get((AOTModuleInstance *)inst_comm_rt, global_idx_rt,
  2473. global->init);
  2474. }
  2475. #endif
  2476. global->inst_comm_rt = inst_comm_rt;
  2477. global->global_idx_rt = global_idx_rt;
  2478. return global;
  2479. failed:
  2480. LOG_DEBUG("%s failed", __FUNCTION__);
  2481. wasm_global_delete(global);
  2482. return NULL;
  2483. }
  2484. wasm_globaltype_t *
  2485. wasm_global_type(const wasm_global_t *global)
  2486. {
  2487. if (!global) {
  2488. return NULL;
  2489. }
  2490. return wasm_globaltype_copy(global->type);
  2491. }
  2492. static wasm_table_t *
  2493. wasm_table_new_basic(const wasm_tabletype_t *type)
  2494. {
  2495. wasm_table_t *table = NULL;
  2496. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  2497. goto failed;
  2498. }
  2499. table->kind = WASM_EXTERN_TABLE;
  2500. if (!(table->type = wasm_tabletype_copy(type))) {
  2501. goto failed;
  2502. }
  2503. RETURN_OBJ(table, wasm_table_delete);
  2504. }
  2505. static wasm_table_t *
  2506. wasm_table_new_internal(wasm_store_t *store,
  2507. uint16 table_idx_rt,
  2508. WASMModuleInstanceCommon *inst_comm_rt)
  2509. {
  2510. wasm_table_t *table = NULL;
  2511. uint8 val_type_rt = 0;
  2512. uint32 init_size = 0, max_size = 0;
  2513. bh_assert(singleton_engine);
  2514. if (!inst_comm_rt || !valid_module_type(inst_comm_rt->module_type)) {
  2515. return NULL;
  2516. }
  2517. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  2518. goto failed;
  2519. }
  2520. table->kind = WASM_EXTERN_TABLE;
  2521. #if WASM_ENABLE_INTERP != 0
  2522. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2523. WASMTableInstance *table_interp =
  2524. ((WASMModuleInstance *)inst_comm_rt)->tables[table_idx_rt];
  2525. val_type_rt = table_interp->elem_type;
  2526. init_size = table_interp->cur_size;
  2527. max_size = table_interp->max_size;
  2528. }
  2529. #endif
  2530. #if WASM_ENABLE_AOT != 0
  2531. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2532. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  2533. AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  2534. if (table_idx_rt < module_aot->import_table_count) {
  2535. AOTImportTable *table_aot =
  2536. module_aot->import_tables + table_idx_rt;
  2537. val_type_rt = VALUE_TYPE_FUNCREF;
  2538. init_size = table_aot->table_init_size;
  2539. max_size = table_aot->table_max_size;
  2540. }
  2541. else {
  2542. AOTTable *table_aot =
  2543. module_aot->tables
  2544. + (table_idx_rt - module_aot->import_table_count);
  2545. val_type_rt = VALUE_TYPE_FUNCREF;
  2546. init_size = table_aot->table_init_size;
  2547. max_size = table_aot->table_max_size;
  2548. }
  2549. }
  2550. #endif
  2551. if (!(table->type =
  2552. wasm_tabletype_new_internal(val_type_rt, init_size, max_size))) {
  2553. goto failed;
  2554. }
  2555. table->inst_comm_rt = inst_comm_rt;
  2556. table->table_idx_rt = table_idx_rt;
  2557. RETURN_OBJ(table, wasm_table_delete);
  2558. }
  2559. wasm_table_t *
  2560. wasm_table_new(wasm_store_t *store,
  2561. const wasm_tabletype_t *table_type,
  2562. wasm_ref_t *init)
  2563. {
  2564. (void)init;
  2565. bh_assert(singleton_engine);
  2566. return wasm_table_new_basic(table_type);
  2567. }
  2568. wasm_table_t *
  2569. wasm_table_copy(const wasm_table_t *src)
  2570. {
  2571. return wasm_table_new_basic(src->type);
  2572. }
  2573. void
  2574. wasm_table_delete(wasm_table_t *table)
  2575. {
  2576. if (!table) {
  2577. return;
  2578. }
  2579. if (table->type) {
  2580. wasm_tabletype_delete(table->type);
  2581. table->type = NULL;
  2582. }
  2583. wasm_runtime_free(table);
  2584. }
  2585. wasm_tabletype_t *
  2586. wasm_table_type(const wasm_table_t *table)
  2587. {
  2588. if (!table) {
  2589. return NULL;
  2590. }
  2591. return wasm_tabletype_copy(table->type);
  2592. }
  2593. static wasm_memory_t *
  2594. wasm_memory_new_basic(const wasm_memorytype_t *type)
  2595. {
  2596. wasm_memory_t *memory = NULL;
  2597. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  2598. goto failed;
  2599. }
  2600. memory->kind = WASM_EXTERN_MEMORY;
  2601. memory->type = wasm_memorytype_copy(type);
  2602. RETURN_OBJ(memory, wasm_memory_delete)
  2603. }
  2604. wasm_memory_t *
  2605. wasm_memory_new(wasm_store_t *store, const wasm_memorytype_t *type)
  2606. {
  2607. bh_assert(singleton_engine);
  2608. return wasm_memory_new_basic(type);
  2609. }
  2610. wasm_memory_t *
  2611. wasm_memory_copy(const wasm_memory_t *src)
  2612. {
  2613. wasm_memory_t *dst = NULL;
  2614. if (!src) {
  2615. return NULL;
  2616. }
  2617. if (!(dst = wasm_memory_new_basic(src->type))) {
  2618. goto failed;
  2619. }
  2620. dst->memory_idx_rt = src->memory_idx_rt;
  2621. dst->inst_comm_rt = src->inst_comm_rt;
  2622. RETURN_OBJ(dst, wasm_memory_delete)
  2623. }
  2624. static wasm_memory_t *
  2625. wasm_memory_new_internal(wasm_store_t *store,
  2626. uint16 memory_idx_rt,
  2627. WASMModuleInstanceCommon *inst_comm_rt)
  2628. {
  2629. wasm_memory_t *memory = NULL;
  2630. uint32 min_pages = 0, max_pages = 0;
  2631. bh_assert(singleton_engine);
  2632. if (!inst_comm_rt || !valid_module_type(inst_comm_rt->module_type)) {
  2633. return NULL;
  2634. }
  2635. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  2636. goto failed;
  2637. }
  2638. memory->kind = WASM_EXTERN_MEMORY;
  2639. #if WASM_ENABLE_INTERP != 0
  2640. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2641. WASMMemoryInstance *memory_interp =
  2642. ((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt];
  2643. min_pages = memory_interp->cur_page_count;
  2644. max_pages = memory_interp->max_page_count;
  2645. }
  2646. #endif
  2647. #if WASM_ENABLE_AOT != 0
  2648. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2649. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  2650. AOTModule *module_aot = (AOTModule *)(inst_aot->aot_module.ptr);
  2651. if (memory_idx_rt < module_aot->import_memory_count) {
  2652. min_pages = module_aot->import_memories->mem_init_page_count;
  2653. max_pages = module_aot->import_memories->mem_max_page_count;
  2654. }
  2655. else {
  2656. min_pages = module_aot->memories->mem_init_page_count;
  2657. max_pages = module_aot->memories->mem_max_page_count;
  2658. }
  2659. }
  2660. #endif
  2661. if (!(memory->type = wasm_memorytype_new_internal(min_pages, max_pages))) {
  2662. goto failed;
  2663. }
  2664. memory->inst_comm_rt = inst_comm_rt;
  2665. memory->memory_idx_rt = memory_idx_rt;
  2666. RETURN_OBJ(memory, wasm_memory_delete);
  2667. }
  2668. void
  2669. wasm_memory_delete(wasm_memory_t *memory)
  2670. {
  2671. if (!memory) {
  2672. return;
  2673. }
  2674. if (memory->type) {
  2675. wasm_memorytype_delete(memory->type);
  2676. memory->type = NULL;
  2677. }
  2678. wasm_runtime_free(memory);
  2679. }
  2680. wasm_memorytype_t *
  2681. wasm_memory_type(const wasm_memory_t *memory)
  2682. {
  2683. if (!memory) {
  2684. return NULL;
  2685. }
  2686. return wasm_memorytype_copy(memory->type);
  2687. }
  2688. byte_t *
  2689. wasm_memory_data(wasm_memory_t *memory)
  2690. {
  2691. return (byte_t *)wasm_runtime_get_memory_data(memory->inst_comm_rt,
  2692. memory->memory_idx_rt);
  2693. }
  2694. size_t
  2695. wasm_memory_data_size(const wasm_memory_t *memory)
  2696. {
  2697. return wasm_runtime_get_memory_data_size(memory->inst_comm_rt,
  2698. memory->memory_idx_rt);
  2699. }
  2700. #if WASM_ENABLE_INTERP != 0
  2701. static bool
  2702. interp_link_func(const wasm_instance_t *inst,
  2703. const WASMModule *module_interp,
  2704. uint16 func_idx_rt,
  2705. wasm_func_t *import)
  2706. {
  2707. WASMImport *imported_func_interp = NULL;
  2708. wasm_func_t *cloned = NULL;
  2709. bh_assert(inst && module_interp && import);
  2710. bh_assert(func_idx_rt < module_interp->import_function_count);
  2711. bh_assert(WASM_EXTERN_FUNC == import->kind);
  2712. imported_func_interp = module_interp->import_functions + func_idx_rt;
  2713. bh_assert(imported_func_interp);
  2714. if (!(cloned = wasm_func_copy(import))) {
  2715. return false;
  2716. }
  2717. if (!bh_vector_append((Vector *)inst->imports, &cloned)) {
  2718. wasm_func_delete(cloned);
  2719. return false;
  2720. }
  2721. imported_func_interp->u.function.call_conv_wasm_c_api = true;
  2722. imported_func_interp->u.function.wasm_c_api_with_env = import->with_env;
  2723. if (import->with_env) {
  2724. imported_func_interp->u.function.func_ptr_linked = import->u.cb_env.cb;
  2725. imported_func_interp->u.function.attachment = import->u.cb_env.env;
  2726. }
  2727. else {
  2728. imported_func_interp->u.function.func_ptr_linked = import->u.cb;
  2729. imported_func_interp->u.function.attachment = NULL;
  2730. }
  2731. import->func_idx_rt = func_idx_rt;
  2732. return true;
  2733. }
  2734. static bool
  2735. interp_link_global(const WASMModule *module_interp,
  2736. uint16 global_idx_rt,
  2737. wasm_global_t *import)
  2738. {
  2739. WASMImport *imported_global_interp = NULL;
  2740. bh_assert(module_interp && import);
  2741. bh_assert(global_idx_rt < module_interp->import_global_count);
  2742. bh_assert(WASM_EXTERN_GLOBAL == import->kind);
  2743. imported_global_interp = module_interp->import_globals + global_idx_rt;
  2744. bh_assert(imported_global_interp);
  2745. /* set init value */
  2746. switch (wasm_valtype_kind(import->type->val_type)) {
  2747. case WASM_I32:
  2748. bh_assert(VALUE_TYPE_I32 == imported_global_interp->u.global.type);
  2749. imported_global_interp->u.global.global_data_linked.i32 =
  2750. import->init->of.i32;
  2751. break;
  2752. case WASM_I64:
  2753. bh_assert(VALUE_TYPE_I64 == imported_global_interp->u.global.type);
  2754. imported_global_interp->u.global.global_data_linked.i64 =
  2755. import->init->of.i64;
  2756. break;
  2757. case WASM_F32:
  2758. bh_assert(VALUE_TYPE_F32 == imported_global_interp->u.global.type);
  2759. imported_global_interp->u.global.global_data_linked.f32 =
  2760. import->init->of.f32;
  2761. break;
  2762. case WASM_F64:
  2763. bh_assert(VALUE_TYPE_F64 == imported_global_interp->u.global.type);
  2764. imported_global_interp->u.global.global_data_linked.f64 =
  2765. import->init->of.f64;
  2766. break;
  2767. default:
  2768. return false;
  2769. }
  2770. import->global_idx_rt = global_idx_rt;
  2771. imported_global_interp->u.global.is_linked = true;
  2772. return true;
  2773. }
  2774. static uint32
  2775. interp_link(const wasm_instance_t *inst,
  2776. const WASMModule *module_interp,
  2777. wasm_extern_t *imports[])
  2778. {
  2779. uint32 i = 0;
  2780. uint32 import_func_i = 0;
  2781. uint32 import_global_i = 0;
  2782. bh_assert(inst && module_interp && imports);
  2783. for (i = 0; i < module_interp->import_count; ++i) {
  2784. wasm_extern_t *import = imports[i];
  2785. WASMImport *import_rt = module_interp->imports + i;
  2786. switch (import_rt->kind) {
  2787. case IMPORT_KIND_FUNC:
  2788. {
  2789. if (!interp_link_func(inst, module_interp, import_func_i++,
  2790. wasm_extern_as_func(import))) {
  2791. goto failed;
  2792. }
  2793. break;
  2794. }
  2795. case IMPORT_KIND_GLOBAL:
  2796. {
  2797. if (!interp_link_global(module_interp, import_global_i++,
  2798. wasm_extern_as_global(import))) {
  2799. goto failed;
  2800. }
  2801. break;
  2802. }
  2803. case IMPORT_KIND_MEMORY:
  2804. case IMPORT_KIND_TABLE:
  2805. ASSERT_NOT_IMPLEMENTED();
  2806. break;
  2807. default:
  2808. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  2809. import_rt->kind);
  2810. goto failed;
  2811. }
  2812. }
  2813. return i;
  2814. failed:
  2815. LOG_DEBUG("%s failed", __FUNCTION__);
  2816. return (uint32)-1;
  2817. }
  2818. static bool
  2819. interp_process_export(wasm_store_t *store,
  2820. const WASMModuleInstance *inst_interp,
  2821. wasm_extern_vec_t *externals)
  2822. {
  2823. WASMExport *exports = NULL;
  2824. WASMExport *export = NULL;
  2825. wasm_extern_t *external = NULL;
  2826. uint32 export_cnt = 0;
  2827. uint32 i = 0;
  2828. bh_assert(store && inst_interp && inst_interp->module && externals);
  2829. exports = inst_interp->module->exports;
  2830. export_cnt = inst_interp->module->export_count;
  2831. for (i = 0; i < export_cnt; ++i) {
  2832. export = exports + i;
  2833. switch (export->kind) {
  2834. case EXPORT_KIND_FUNC:
  2835. {
  2836. wasm_func_t *func;
  2837. if (!(func = wasm_func_new_internal(
  2838. store, export->index,
  2839. (WASMModuleInstanceCommon *)inst_interp))) {
  2840. goto failed;
  2841. }
  2842. external = wasm_func_as_extern(func);
  2843. break;
  2844. }
  2845. case EXPORT_KIND_GLOBAL:
  2846. {
  2847. wasm_global_t *global;
  2848. if (!(global = wasm_global_new_internal(
  2849. store, export->index,
  2850. (WASMModuleInstanceCommon *)inst_interp))) {
  2851. goto failed;
  2852. }
  2853. external = wasm_global_as_extern(global);
  2854. break;
  2855. }
  2856. case EXPORT_KIND_TABLE:
  2857. {
  2858. wasm_table_t *table;
  2859. if (!(table = wasm_table_new_internal(
  2860. store, export->index,
  2861. (WASMModuleInstanceCommon *)inst_interp))) {
  2862. goto failed;
  2863. }
  2864. external = wasm_table_as_extern(table);
  2865. break;
  2866. }
  2867. case EXPORT_KIND_MEMORY:
  2868. {
  2869. wasm_memory_t *memory;
  2870. if (!(memory = wasm_memory_new_internal(
  2871. store, export->index,
  2872. (WASMModuleInstanceCommon *)inst_interp))) {
  2873. goto failed;
  2874. }
  2875. external = wasm_memory_as_extern(memory);
  2876. break;
  2877. }
  2878. default:
  2879. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  2880. export->kind);
  2881. goto failed;
  2882. }
  2883. if (!bh_vector_append((Vector *)externals, &external)) {
  2884. goto failed;
  2885. }
  2886. }
  2887. return true;
  2888. failed:
  2889. wasm_extern_delete(external);
  2890. return false;
  2891. }
  2892. #endif /* WASM_ENABLE_INTERP */
  2893. #if WASM_ENABLE_AOT != 0
  2894. static bool
  2895. aot_link_func(const wasm_instance_t *inst,
  2896. const AOTModule *module_aot,
  2897. uint32 import_func_idx_rt,
  2898. wasm_func_t *import)
  2899. {
  2900. AOTImportFunc *import_aot_func = NULL;
  2901. wasm_func_t *cloned = NULL;
  2902. bh_assert(inst && module_aot && import);
  2903. import_aot_func = module_aot->import_funcs + import_func_idx_rt;
  2904. bh_assert(import_aot_func);
  2905. if (!(cloned = wasm_func_copy(import))) {
  2906. return false;
  2907. }
  2908. if (!bh_vector_append((Vector *)inst->imports, &cloned)) {
  2909. wasm_func_delete(cloned);
  2910. return false;
  2911. }
  2912. import_aot_func->call_conv_wasm_c_api = true;
  2913. import_aot_func->wasm_c_api_with_env = import->with_env;
  2914. if (import->with_env) {
  2915. import_aot_func->func_ptr_linked = import->u.cb_env.cb;
  2916. import_aot_func->attachment = import->u.cb_env.env;
  2917. }
  2918. else
  2919. import_aot_func->func_ptr_linked = import->u.cb;
  2920. import->func_idx_rt = import_func_idx_rt;
  2921. return true;
  2922. }
  2923. static bool
  2924. aot_link_global(const AOTModule *module_aot,
  2925. uint16 global_idx_rt,
  2926. wasm_global_t *import)
  2927. {
  2928. AOTImportGlobal *import_aot_global = NULL;
  2929. const wasm_valtype_t *val_type = NULL;
  2930. bh_assert(module_aot && import);
  2931. import_aot_global = module_aot->import_globals + global_idx_rt;
  2932. bh_assert(import_aot_global);
  2933. //TODO: import->type ?
  2934. val_type = wasm_globaltype_content(import->type);
  2935. bh_assert(val_type);
  2936. switch (wasm_valtype_kind(val_type)) {
  2937. case WASM_I32:
  2938. bh_assert(VALUE_TYPE_I32 == import_aot_global->type);
  2939. import_aot_global->global_data_linked.i32 = import->init->of.i32;
  2940. break;
  2941. case WASM_I64:
  2942. bh_assert(VALUE_TYPE_I64 == import_aot_global->type);
  2943. import_aot_global->global_data_linked.i64 = import->init->of.i64;
  2944. break;
  2945. case WASM_F32:
  2946. bh_assert(VALUE_TYPE_F32 == import_aot_global->type);
  2947. import_aot_global->global_data_linked.f32 = import->init->of.f32;
  2948. break;
  2949. case WASM_F64:
  2950. bh_assert(VALUE_TYPE_F64 == import_aot_global->type);
  2951. import_aot_global->global_data_linked.f64 = import->init->of.f64;
  2952. break;
  2953. default:
  2954. goto failed;
  2955. }
  2956. import->global_idx_rt = global_idx_rt;
  2957. return true;
  2958. failed:
  2959. LOG_DEBUG("%s failed", __FUNCTION__);
  2960. return false;
  2961. }
  2962. static uint32
  2963. aot_link(const wasm_instance_t *inst,
  2964. const AOTModule *module_aot,
  2965. wasm_extern_t *imports[])
  2966. {
  2967. uint32 i = 0;
  2968. uint32 import_func_i = 0;
  2969. uint32 import_global_i = 0;
  2970. wasm_extern_t *import = NULL;
  2971. wasm_func_t *func = NULL;
  2972. wasm_global_t *global = NULL;
  2973. bh_assert(inst && module_aot && imports);
  2974. while (import_func_i < module_aot->import_func_count
  2975. || import_global_i < module_aot->import_global_count) {
  2976. import = imports[i++];
  2977. bh_assert(import);
  2978. switch (wasm_extern_kind(import)) {
  2979. case WASM_EXTERN_FUNC:
  2980. bh_assert(import_func_i < module_aot->import_func_count);
  2981. func = wasm_extern_as_func((wasm_extern_t *)import);
  2982. if (!aot_link_func(inst, module_aot, import_func_i++, func)) {
  2983. goto failed;
  2984. }
  2985. break;
  2986. case WASM_EXTERN_GLOBAL:
  2987. bh_assert(import_global_i < module_aot->import_global_count);
  2988. global = wasm_extern_as_global((wasm_extern_t *)import);
  2989. if (!aot_link_global(module_aot, import_global_i++, global)) {
  2990. goto failed;
  2991. }
  2992. break;
  2993. case WASM_EXTERN_MEMORY:
  2994. case WASM_EXTERN_TABLE:
  2995. ASSERT_NOT_IMPLEMENTED();
  2996. break;
  2997. default:
  2998. goto failed;
  2999. }
  3000. }
  3001. return i;
  3002. failed:
  3003. LOG_DEBUG("%s failed", __FUNCTION__);
  3004. return (uint32)-1;
  3005. }
  3006. static bool
  3007. aot_process_export(wasm_store_t *store,
  3008. const AOTModuleInstance *inst_aot,
  3009. wasm_extern_vec_t *externals)
  3010. {
  3011. uint32 i;
  3012. wasm_extern_t *external = NULL;
  3013. AOTModule *module_aot = NULL;
  3014. bh_assert(store && inst_aot && externals);
  3015. module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  3016. bh_assert(module_aot);
  3017. for (i = 0; i < module_aot->export_count; ++i) {
  3018. AOTExport *export = module_aot->exports + i;
  3019. switch (export->kind) {
  3020. case EXPORT_KIND_FUNC:
  3021. {
  3022. wasm_func_t *func = NULL;
  3023. if (!(func = wasm_func_new_internal(
  3024. store, export->index,
  3025. (WASMModuleInstanceCommon *)inst_aot))) {
  3026. goto failed;
  3027. }
  3028. external = wasm_func_as_extern(func);
  3029. break;
  3030. }
  3031. case EXPORT_KIND_GLOBAL:
  3032. {
  3033. wasm_global_t *global = NULL;
  3034. if (!(global = wasm_global_new_internal(
  3035. store, export->index,
  3036. (WASMModuleInstanceCommon *)inst_aot))) {
  3037. goto failed;
  3038. }
  3039. external = wasm_global_as_extern(global);
  3040. break;
  3041. }
  3042. case EXPORT_KIND_TABLE:
  3043. {
  3044. wasm_table_t *table;
  3045. if (!(table = wasm_table_new_internal(
  3046. store, export->index,
  3047. (WASMModuleInstanceCommon *)inst_aot))) {
  3048. goto failed;
  3049. }
  3050. external = wasm_table_as_extern(table);
  3051. break;
  3052. }
  3053. case EXPORT_KIND_MEMORY:
  3054. {
  3055. wasm_memory_t *memory;
  3056. if (!(memory = wasm_memory_new_internal(
  3057. store, export->index,
  3058. (WASMModuleInstanceCommon *)inst_aot))) {
  3059. goto failed;
  3060. }
  3061. external = wasm_memory_as_extern(memory);
  3062. break;
  3063. }
  3064. default:
  3065. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3066. export->kind);
  3067. goto failed;
  3068. }
  3069. if (!(external->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  3070. goto failed;
  3071. }
  3072. wasm_name_new_from_string(external->name, export->name);
  3073. if (strlen(export->name) && !external->name->data) {
  3074. goto failed;
  3075. }
  3076. if (!bh_vector_append((Vector *)externals, &external)) {
  3077. goto failed;
  3078. }
  3079. }
  3080. return true;
  3081. failed:
  3082. wasm_extern_delete(external);
  3083. return false;
  3084. }
  3085. #endif /* WASM_ENABLE_AOT */
  3086. wasm_instance_t *
  3087. wasm_instance_new(wasm_store_t *store,
  3088. const wasm_module_t *module,
  3089. const wasm_extern_t *const imports[],
  3090. own wasm_trap_t **traps)
  3091. {
  3092. char error_buf[128] = { 0 };
  3093. const uint32 stack_size = 32 * 1024;
  3094. const uint32 heap_size = 32 * 1024;
  3095. uint32 import_count = 0;
  3096. wasm_instance_t *instance = NULL;
  3097. uint32 i = 0;
  3098. (void)traps;
  3099. bh_assert(singleton_engine);
  3100. if (!module || !valid_module_type((*module)->module_type)) {
  3101. return NULL;
  3102. }
  3103. instance = malloc_internal(sizeof(wasm_instance_t));
  3104. if (!instance) {
  3105. goto failed;
  3106. }
  3107. /* link module and imports */
  3108. if (imports) {
  3109. #if WASM_ENABLE_INTERP != 0
  3110. if ((*module)->module_type == Wasm_Module_Bytecode) {
  3111. import_count = MODULE_INTERP(module)->import_count;
  3112. INIT_VEC(instance->imports, wasm_extern_vec_new_uninitialized,
  3113. import_count);
  3114. if (import_count) {
  3115. uint32 actual_link_import_count = interp_link(
  3116. instance, MODULE_INTERP(module), (wasm_extern_t **)imports);
  3117. /* make sure a complete import list */
  3118. if ((int32)import_count < 0
  3119. || import_count != actual_link_import_count) {
  3120. goto failed;
  3121. }
  3122. }
  3123. }
  3124. #endif
  3125. #if WASM_ENABLE_AOT != 0
  3126. if ((*module)->module_type == Wasm_Module_AoT) {
  3127. import_count = MODULE_AOT(module)->import_func_count
  3128. + MODULE_AOT(module)->import_global_count
  3129. + MODULE_AOT(module)->import_memory_count
  3130. + MODULE_AOT(module)->import_table_count;
  3131. INIT_VEC(instance->imports, wasm_extern_vec_new_uninitialized,
  3132. import_count);
  3133. if (import_count) {
  3134. import_count = aot_link(instance, MODULE_AOT(module),
  3135. (wasm_extern_t **)imports);
  3136. if ((int32)import_count < 0) {
  3137. goto failed;
  3138. }
  3139. }
  3140. }
  3141. #endif
  3142. }
  3143. instance->inst_comm_rt = wasm_runtime_instantiate(
  3144. *module, stack_size, heap_size, error_buf, sizeof(error_buf));
  3145. if (!instance->inst_comm_rt) {
  3146. LOG_ERROR(error_buf);
  3147. goto failed;
  3148. }
  3149. if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) {
  3150. goto failed;
  3151. }
  3152. /* fill with inst */
  3153. for (i = 0; imports && i < (uint32)import_count; ++i) {
  3154. wasm_extern_t *import = (wasm_extern_t *)imports[i];
  3155. switch (import->kind) {
  3156. case WASM_EXTERN_FUNC:
  3157. wasm_extern_as_func(import)->inst_comm_rt =
  3158. instance->inst_comm_rt;
  3159. break;
  3160. case WASM_EXTERN_GLOBAL:
  3161. wasm_extern_as_global(import)->inst_comm_rt =
  3162. instance->inst_comm_rt;
  3163. break;
  3164. case WASM_EXTERN_MEMORY:
  3165. wasm_extern_as_memory(import)->inst_comm_rt =
  3166. instance->inst_comm_rt;
  3167. break;
  3168. case WASM_EXTERN_TABLE:
  3169. wasm_extern_as_table(import)->inst_comm_rt =
  3170. instance->inst_comm_rt;
  3171. break;
  3172. default:
  3173. goto failed;
  3174. }
  3175. }
  3176. /* build the exports list */
  3177. #if WASM_ENABLE_INTERP != 0
  3178. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3179. uint32 export_cnt =
  3180. ((WASMModuleInstance *)instance->inst_comm_rt)->module->export_count;
  3181. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  3182. export_cnt);
  3183. if (!interp_process_export(
  3184. store, (WASMModuleInstance *)instance->inst_comm_rt,
  3185. instance->exports)) {
  3186. goto failed;
  3187. }
  3188. }
  3189. #endif
  3190. #if WASM_ENABLE_AOT != 0
  3191. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3192. uint32 export_cnt =
  3193. ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count
  3194. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_global_count
  3195. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_tab_count
  3196. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_mem_count;
  3197. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  3198. export_cnt);
  3199. if (!aot_process_export(store,
  3200. (AOTModuleInstance *)instance->inst_comm_rt,
  3201. instance->exports)) {
  3202. goto failed;
  3203. }
  3204. }
  3205. #endif
  3206. /* add it to a watching list in store */
  3207. if (!bh_vector_append((Vector *)store->instances, &instance)) {
  3208. goto failed;
  3209. }
  3210. return instance;
  3211. failed:
  3212. LOG_DEBUG("%s failed", __FUNCTION__);
  3213. wasm_instance_delete_internal(instance);
  3214. return NULL;
  3215. }
  3216. static void
  3217. wasm_instance_delete_internal(wasm_instance_t *instance)
  3218. {
  3219. if (!instance) {
  3220. return;
  3221. }
  3222. DEINIT_VEC(instance->imports, wasm_extern_vec_delete);
  3223. DEINIT_VEC(instance->exports, wasm_extern_vec_delete);
  3224. if (instance->inst_comm_rt) {
  3225. wasm_runtime_deinstantiate(instance->inst_comm_rt);
  3226. instance->inst_comm_rt = NULL;
  3227. }
  3228. wasm_runtime_free(instance);
  3229. }
  3230. void
  3231. wasm_instance_delete(wasm_instance_t *module)
  3232. {
  3233. /* will release instance when releasing the store */
  3234. }
  3235. void
  3236. wasm_instance_exports(const wasm_instance_t *instance,
  3237. own wasm_extern_vec_t *out)
  3238. {
  3239. if (!instance || !out) {
  3240. return;
  3241. }
  3242. wasm_extern_vec_copy(out, instance->exports);
  3243. }
  3244. wasm_extern_t *
  3245. wasm_extern_copy(const wasm_extern_t *src)
  3246. {
  3247. wasm_extern_t *dst = NULL;
  3248. if (!src) {
  3249. return NULL;
  3250. }
  3251. switch (wasm_extern_kind(src)) {
  3252. case WASM_EXTERN_FUNC:
  3253. dst = wasm_func_as_extern(
  3254. wasm_func_copy(wasm_extern_as_func_const(src)));
  3255. break;
  3256. case WASM_EXTERN_GLOBAL:
  3257. dst = wasm_global_as_extern(
  3258. wasm_global_copy(wasm_extern_as_global_const(src)));
  3259. break;
  3260. case WASM_EXTERN_MEMORY:
  3261. dst = wasm_memory_as_extern(
  3262. wasm_memory_copy(wasm_extern_as_memory_const(src)));
  3263. break;
  3264. case WASM_EXTERN_TABLE:
  3265. dst = wasm_table_as_extern(
  3266. wasm_table_copy(wasm_extern_as_table_const(src)));
  3267. break;
  3268. default:
  3269. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3270. src->kind);
  3271. break;
  3272. }
  3273. if (!dst) {
  3274. goto failed;
  3275. }
  3276. return dst;
  3277. failed:
  3278. LOG_DEBUG("%s failed", __FUNCTION__);
  3279. wasm_extern_delete(dst);
  3280. return NULL;
  3281. }
  3282. void
  3283. wasm_extern_delete(wasm_extern_t *external)
  3284. {
  3285. if (!external) {
  3286. return;
  3287. }
  3288. if (external->name) {
  3289. wasm_byte_vec_delete(external->name);
  3290. wasm_runtime_free(external->name);
  3291. external->name = NULL;
  3292. }
  3293. switch (wasm_extern_kind(external)) {
  3294. case WASM_EXTERN_FUNC:
  3295. wasm_func_delete(wasm_extern_as_func(external));
  3296. break;
  3297. case WASM_EXTERN_GLOBAL:
  3298. wasm_global_delete(wasm_extern_as_global(external));
  3299. break;
  3300. case WASM_EXTERN_MEMORY:
  3301. wasm_memory_delete(wasm_extern_as_memory(external));
  3302. break;
  3303. case WASM_EXTERN_TABLE:
  3304. wasm_table_delete(wasm_extern_as_table(external));
  3305. break;
  3306. default:
  3307. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3308. external->kind);
  3309. break;
  3310. }
  3311. }
  3312. wasm_externkind_t
  3313. wasm_extern_kind(const wasm_extern_t *external)
  3314. {
  3315. if (!external) {
  3316. return WASM_ANYREF;
  3317. }
  3318. return external->kind;
  3319. }
  3320. own wasm_externtype_t *
  3321. wasm_extern_type(const wasm_extern_t *external)
  3322. {
  3323. if (!external) {
  3324. return NULL;
  3325. }
  3326. switch (wasm_extern_kind(external)) {
  3327. case WASM_EXTERN_FUNC:
  3328. return wasm_functype_as_externtype(
  3329. wasm_func_type(wasm_extern_as_func_const(external)));
  3330. case WASM_EXTERN_GLOBAL:
  3331. return wasm_globaltype_as_externtype(
  3332. wasm_global_type(wasm_extern_as_global_const(external)));
  3333. case WASM_EXTERN_MEMORY:
  3334. return wasm_memorytype_as_externtype(
  3335. wasm_memory_type(wasm_extern_as_memory_const(external)));
  3336. case WASM_EXTERN_TABLE:
  3337. return wasm_tabletype_as_externtype(
  3338. wasm_table_type(wasm_extern_as_table_const(external)));
  3339. default:
  3340. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3341. external->kind);
  3342. break;
  3343. }
  3344. return NULL;
  3345. }
  3346. #define BASIC_FOUR_LIST(V) \
  3347. V(func) \
  3348. V(global) \
  3349. V(memory) \
  3350. V(table)
  3351. #define WASM_EXTERN_AS_OTHER(name) \
  3352. wasm_##name##_t *wasm_extern_as_##name(wasm_extern_t *external) \
  3353. { \
  3354. return (wasm_##name##_t *)external; \
  3355. }
  3356. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER)
  3357. #undef WASM_EXTERN_AS_OTHER
  3358. #define WASM_OTHER_AS_EXTERN(name) \
  3359. wasm_extern_t *wasm_##name##_as_extern(wasm_##name##_t *other) \
  3360. { \
  3361. return (wasm_extern_t *)other; \
  3362. }
  3363. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN)
  3364. #undef WASM_OTHER_AS_EXTERN
  3365. #define WASM_EXTERN_AS_OTHER_CONST(name) \
  3366. const wasm_##name##_t *wasm_extern_as_##name##_const( \
  3367. const wasm_extern_t *external) \
  3368. { \
  3369. return (const wasm_##name##_t *)external; \
  3370. }
  3371. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER_CONST)
  3372. #undef WASM_EXTERN_AS_OTHER_CONST
  3373. #define WASM_OTHER_AS_EXTERN_CONST(name) \
  3374. const wasm_extern_t *wasm_##name##_as_extern_const( \
  3375. const wasm_##name##_t *other) \
  3376. { \
  3377. return (const wasm_extern_t *)other; \
  3378. }
  3379. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN_CONST)
  3380. #undef WASM_OTHER_AS_EXTERN_CONST