wasm_runtime.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_runtime.h"
  6. #include "wasm_loader.h"
  7. #include "wasm_interp.h"
  8. #include "bh_common.h"
  9. #include "bh_log.h"
  10. #include "mem_alloc.h"
  11. #include "../common/wasm_runtime_common.h"
  12. #include "../common/wasm_multimodules_program.h"
  13. #if WASM_ENABLE_SHARED_MEMORY != 0
  14. #include "../common/wasm_shared_memory.h"
  15. #endif
  16. #if WASM_ENABLE_THREAD_MGR != 0
  17. #include "../libraries/thread-mgr/thread_manager.h"
  18. #endif
  19. #if WASM_ENABLE_DEBUG_INTERP != 0
  20. #include "../libraries/debug-engine/debug_engine.h"
  21. #endif
  22. typedef struct LoadingModuleElem {
  23. bh_list_link l;
  24. WASMModule * module;
  25. } LoadingModuleElem;
  26. static void
  27. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  28. {
  29. if (error_buf != NULL) {
  30. snprintf(error_buf, error_buf_size,
  31. "WASM module instantiate failed: %s", string);
  32. }
  33. }
  34. static void
  35. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  36. {
  37. va_list args;
  38. char buf[128];
  39. if (error_buf != NULL) {
  40. va_start(args, format);
  41. vsnprintf(buf, sizeof(buf), format, args);
  42. va_end(args);
  43. snprintf(error_buf, error_buf_size,
  44. "WASM module instantiate failed: %s", buf);
  45. }
  46. }
  47. static void *
  48. runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  49. {
  50. void *mem;
  51. if (size >= UINT32_MAX
  52. || !(mem = wasm_runtime_malloc((uint32)size))) {
  53. set_error_buf(error_buf, error_buf_size,
  54. "allocate memory failed");
  55. return NULL;
  56. }
  57. memset(mem, 0, (uint32)size);
  58. return mem;
  59. }
  60. inline static WASMModuleInstance *
  61. dylib_entries_map_find(const ConstStrDescription * key, HashMap *map)
  62. {
  63. return bh_hash_map_find(map, (void*)key);
  64. }
  65. WASMModule*
  66. wasm_load(const uint8 *buf, uint32 size,
  67. char *error_buf, uint32 error_buf_size)
  68. {
  69. return wasm_loader_load(buf, size, error_buf, error_buf_size);
  70. }
  71. WASMModule *
  72. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  73. uint32_t error_buf_size)
  74. {
  75. return wasm_loader_load_from_sections(section_list, error_buf,
  76. error_buf_size);
  77. }
  78. void
  79. wasm_unload(WASMModule *module)
  80. {
  81. wasm_loader_unload(module);
  82. }
  83. #if WASM_ENABLE_MULTI_MODULE != 0
  84. static WASMModuleInstance *
  85. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  86. const WASMModule *sub_module)
  87. {
  88. bh_list *sub_module_inst_list = parent_module_inst->sub_module_inst_list;
  89. WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
  90. while (node && sub_module != node->module_inst->module) {
  91. node = bh_list_elem_next(node);
  92. }
  93. return node ? node->module_inst : NULL;
  94. }
  95. #endif
  96. /**
  97. * Destroy memory instances.
  98. */
  99. static void
  100. memories_deinstantiate(WASMModuleInstance *module_inst,
  101. WASMMemoryInstance **memories, uint32 count)
  102. {
  103. uint32 i;
  104. if (memories) {
  105. for (i = 0; i < count; i++) {
  106. if (memories[i]) {
  107. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  108. if (module_inst->program &&
  109. module_inst != (WASMModuleInstance*)module_inst->program->root_module_inst)
  110. continue;
  111. #endif
  112. #if WASM_ENABLE_MULTI_MODULE != 0
  113. if (memories[i]->owner != module_inst)
  114. continue;
  115. #endif
  116. #if WASM_ENABLE_SHARED_MEMORY != 0
  117. if (memories[i]->is_shared) {
  118. int32 ref_count = shared_memory_dec_reference(
  119. (WASMModuleCommon *)module_inst->module);
  120. bh_assert(ref_count >= 0);
  121. /* if the reference count is not zero,
  122. don't free the memory */
  123. if (ref_count > 0)
  124. continue;
  125. }
  126. os_mutex_destroy(&memories[i]->mem_lock);
  127. #endif
  128. if (memories[i]->heap_handle) {
  129. mem_allocator_destroy(memories[i]->heap_handle);
  130. wasm_runtime_free(memories[i]->heap_handle);
  131. memories[i]->heap_handle = NULL;
  132. }
  133. if (memories[i]->memory_data)
  134. wasm_runtime_free(memories[i]->memory_data);
  135. wasm_runtime_free(memories[i]);
  136. }
  137. }
  138. wasm_runtime_free(memories);
  139. }
  140. (void)module_inst;
  141. }
  142. static WASMMemoryInstance *
  143. memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
  144. uint32 init_page_count, uint32 max_page_count,
  145. uint32 heap_size, uint32 flags, char *error_buf,
  146. uint32 error_buf_size)
  147. {
  148. WASMModule *module = module_inst->module;
  149. WASMMemoryInstance *memory;
  150. uint64 memory_data_size;
  151. uint32 heap_offset = num_bytes_per_page * init_page_count;
  152. uint32 inc_page_count, aux_heap_base, global_idx;
  153. uint32 bytes_of_last_page, bytes_to_page_end;
  154. uint8 *global_addr;
  155. #if WASM_ENABLE_SHARED_MEMORY != 0
  156. bool is_shared_memory = flags & 0x02 ? true : false;
  157. /* shared memory */
  158. if (is_shared_memory) {
  159. WASMSharedMemNode *node = wasm_module_get_shared_memory(
  160. (WASMModuleCommon *)module_inst->module);
  161. /* If the memory of this module has been instantiated,
  162. return the memory instance directly */
  163. if (node) {
  164. uint32 ref_count;
  165. ref_count = shared_memory_inc_reference(
  166. (WASMModuleCommon *)module_inst->module);
  167. bh_assert(ref_count > 0);
  168. memory = (WASMMemoryInstance *)shared_memory_get_memory_inst(node);
  169. bh_assert(memory);
  170. (void)ref_count;
  171. return memory;
  172. }
  173. }
  174. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  175. if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1
  176. && module_inst->module->free_function != (uint32)-1) {
  177. /* Disable app heap, use malloc/free function exported
  178. by wasm app to allocate/free memory instead */
  179. heap_size = 0;
  180. }
  181. if (init_page_count == max_page_count && init_page_count == 1) {
  182. /* If only one page and at most one page, we just append
  183. the app heap to the end of linear memory, enlarge the
  184. num_bytes_per_page, and don't change the page count*/
  185. heap_offset = num_bytes_per_page;
  186. num_bytes_per_page += heap_size;
  187. if (num_bytes_per_page < heap_size) {
  188. set_error_buf(error_buf, error_buf_size,
  189. "memory size must be at most 65536 pages (4GiB)");
  190. return NULL;
  191. }
  192. }
  193. else if (heap_size > 0) {
  194. if (module->aux_heap_base_global_index != (uint32)-1
  195. && module->aux_heap_base < num_bytes_per_page * init_page_count) {
  196. /* Insert app heap before __heap_base */
  197. aux_heap_base = module->aux_heap_base;
  198. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  199. if (bytes_of_last_page == 0)
  200. bytes_of_last_page = num_bytes_per_page;
  201. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  202. inc_page_count =
  203. (heap_size - bytes_to_page_end + num_bytes_per_page - 1)
  204. / num_bytes_per_page;
  205. heap_offset = aux_heap_base;
  206. aux_heap_base += heap_size;
  207. bytes_of_last_page = aux_heap_base % num_bytes_per_page;
  208. if (bytes_of_last_page == 0)
  209. bytes_of_last_page = num_bytes_per_page;
  210. bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
  211. if (bytes_to_page_end < 1 * BH_KB) {
  212. aux_heap_base += 1 * BH_KB;
  213. inc_page_count++;
  214. }
  215. /* Adjust __heap_base global value */
  216. global_idx = module->aux_heap_base_global_index;
  217. //global_addr = module_inst->global_data +
  218. // module_inst->globals[global_idx].data_offset;
  219. global_addr = module_inst->globals[global_idx].data;
  220. *(uint32 *)global_addr = aux_heap_base;
  221. LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
  222. }
  223. else {
  224. /* Insert app heap before new page */
  225. inc_page_count =
  226. (heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
  227. heap_offset = num_bytes_per_page * init_page_count;
  228. heap_size = num_bytes_per_page * inc_page_count;
  229. if (heap_size > 0)
  230. heap_size -= 1 * BH_KB;
  231. }
  232. init_page_count += inc_page_count;
  233. max_page_count += inc_page_count;
  234. if (init_page_count > 65536) {
  235. set_error_buf(error_buf, error_buf_size,
  236. "memory size must be at most 65536 pages (4GiB)");
  237. return NULL;
  238. }
  239. if (max_page_count > 65536)
  240. max_page_count = 65536;
  241. }
  242. LOG_VERBOSE("Memory instantiate:");
  243. LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
  244. num_bytes_per_page, init_page_count, max_page_count);
  245. LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
  246. memory_data_size = (uint64)num_bytes_per_page * init_page_count;
  247. #if WASM_ENABLE_SHARED_MEMORY != 0
  248. if (is_shared_memory) {
  249. /* Allocate max page for shared memory */
  250. memory_data_size = (uint64)num_bytes_per_page * max_page_count;
  251. }
  252. #endif
  253. /* Allocate memory space, addr data and global data */
  254. if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance), error_buf,
  255. error_buf_size))) {
  256. return NULL;
  257. }
  258. if (memory_data_size > 0
  259. && !(memory->memory_data =
  260. runtime_malloc(memory_data_size, error_buf, error_buf_size))) {
  261. goto fail1;
  262. }
  263. memory->module_type = Wasm_Module_Bytecode;
  264. memory->num_bytes_per_page = num_bytes_per_page;
  265. memory->cur_page_count = init_page_count;
  266. memory->max_page_count = max_page_count;
  267. memory->heap_data = memory->memory_data + heap_offset;
  268. memory->heap_data_end = memory->heap_data + heap_size;
  269. memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
  270. /* Initialize heap */
  271. if (heap_size > 0) {
  272. uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
  273. if (!(memory->heap_handle = runtime_malloc(
  274. (uint64)heap_struct_size, error_buf, error_buf_size))) {
  275. goto fail2;
  276. }
  277. if (!mem_allocator_create_with_struct_and_pool(
  278. memory->heap_handle, heap_struct_size, memory->heap_data,
  279. heap_size)) {
  280. set_error_buf(error_buf, error_buf_size, "init app heap failed");
  281. goto fail3;
  282. }
  283. }
  284. #if WASM_ENABLE_SHARED_MEMORY != 0
  285. if (0 != os_mutex_init(&memory->mem_lock)) {
  286. set_error_buf(error_buf, error_buf_size, "init mutex failed");
  287. goto fail4;
  288. }
  289. if (is_shared_memory) {
  290. memory->is_shared = true;
  291. if (!shared_memory_set_memory_inst(
  292. (WASMModuleCommon *)module_inst->module,
  293. (WASMMemoryInstanceCommon *)memory)) {
  294. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  295. goto fail5;
  296. }
  297. }
  298. #endif
  299. LOG_VERBOSE("Memory instantiate success.");
  300. return memory;
  301. #if WASM_ENABLE_SHARED_MEMORY != 0
  302. fail5:
  303. os_mutex_destroy(&memory->mem_lock);
  304. fail4:
  305. if (heap_size > 0)
  306. mem_allocator_destroy(memory->heap_handle);
  307. #endif
  308. fail3:
  309. if (heap_size > 0)
  310. wasm_runtime_free(memory->heap_handle);
  311. fail2:
  312. if (memory->memory_data)
  313. wasm_runtime_free(memory->memory_data);
  314. fail1:
  315. wasm_runtime_free(memory);
  316. return NULL;
  317. }
  318. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  319. static WASMMemoryInstance **
  320. dependency_memories_instantiate(const WASMModule *module,
  321. WASMModuleInstance *root_module_inst)
  322. {
  323. uint32 memory_count =
  324. module->import_memory_count;
  325. uint64 total_size;
  326. WASMMemoryInstance **memories;
  327. WASMProgramInstance * program = root_module_inst->program;
  328. bh_assert(module->import_memory_count == 1);
  329. total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count;
  330. if (!(memories = runtime_malloc(total_size,
  331. program->error_buf, program->error_buf_size))) {
  332. return NULL;
  333. }
  334. memories[0] = root_module_inst->memories[0];
  335. return memories;
  336. }
  337. #endif
  338. /**
  339. * Instantiate memories in a module.
  340. */
  341. static WASMMemoryInstance **
  342. memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  343. uint32 heap_size, char *error_buf, uint32 error_buf_size)
  344. {
  345. WASMImport *import;
  346. uint32 mem_index = 0, i,
  347. memory_count = module->import_memory_count + module->memory_count;
  348. uint64 total_size;
  349. WASMMemoryInstance **memories, *memory;
  350. total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count;
  351. if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) {
  352. return NULL;
  353. }
  354. /* instantiate memories from import section */
  355. import = module->import_memories;
  356. for (i = 0; i < module->import_memory_count; i++, import++) {
  357. uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
  358. uint32 init_page_count = import->u.memory.init_page_count;
  359. uint32 max_page_count = import->u.memory.max_page_count;
  360. uint32 flags = import->u.memory.flags;
  361. uint32 actual_heap_size = heap_size;
  362. #if WASM_ENABLE_MULTI_MODULE != 0
  363. if (import->u.memory.import_module != NULL) {
  364. WASMModuleInstance *module_inst_linked;
  365. if (!(module_inst_linked = get_sub_module_inst(
  366. module_inst, import->u.memory.import_module))) {
  367. set_error_buf(error_buf, error_buf_size, "unknown memory");
  368. memories_deinstantiate(module_inst, memories, memory_count);
  369. return NULL;
  370. }
  371. if (!(memory = memories[mem_index++] = wasm_lookup_memory(
  372. module_inst_linked, import->u.memory.field_name))) {
  373. set_error_buf(error_buf, error_buf_size, "unknown memory");
  374. memories_deinstantiate(module_inst, memories, memory_count);
  375. return NULL;
  376. }
  377. }
  378. else
  379. #endif
  380. {
  381. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  382. if (module->dylink_section) {
  383. num_bytes_per_page = module_inst->init_globals.stack_pointer;
  384. init_page_count = 1;
  385. }
  386. #endif
  387. if (!(memory = memories[mem_index++] = memory_instantiate(
  388. module_inst, num_bytes_per_page, init_page_count,
  389. max_page_count, actual_heap_size, flags, error_buf,
  390. error_buf_size))) {
  391. memories_deinstantiate(module_inst, memories, memory_count);
  392. return NULL;
  393. }
  394. #if WASM_ENABLE_MULTI_MODULE != 0
  395. /* The module of the import memory is a builtin module, and
  396. the memory is created by current module, set its owner
  397. to current module, so the memory can be destroyed in
  398. memories_deinstantiate. */
  399. memory->owner = module_inst;
  400. #endif
  401. }
  402. }
  403. /* instantiate memories from memory section */
  404. for (i = 0; i < module->memory_count; i++) {
  405. if (!(memory = memories[mem_index++] = memory_instantiate(
  406. module_inst, module->memories[i].num_bytes_per_page,
  407. module->memories[i].init_page_count,
  408. module->memories[i].max_page_count, heap_size,
  409. module->memories[i].flags, error_buf, error_buf_size))) {
  410. memories_deinstantiate(module_inst, memories, memory_count);
  411. return NULL;
  412. }
  413. #if WASM_ENABLE_MULTI_MODULE != 0
  414. memory->owner = module_inst;
  415. #endif
  416. }
  417. if (mem_index == 0) {
  418. /**
  419. * no import memory and define memory, but still need heap
  420. * for wasm code
  421. */
  422. if (!(memory = memories[mem_index++] =
  423. memory_instantiate(module_inst, 0, 0, 0, heap_size, 0,
  424. error_buf, error_buf_size))) {
  425. memories_deinstantiate(module_inst, memories, memory_count);
  426. return NULL;
  427. }
  428. }
  429. bh_assert(mem_index == memory_count);
  430. (void)module_inst;
  431. return memories;
  432. }
  433. /**
  434. * Destroy table instances.
  435. */
  436. static void
  437. tables_deinstantiate(WASMTableInstance **tables, uint32 count)
  438. {
  439. uint32 i;
  440. if (tables) {
  441. for (i = 0; i < count; i++)
  442. if (tables[i])
  443. wasm_runtime_free(tables[i]);
  444. wasm_runtime_free(tables);
  445. }
  446. }
  447. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  448. static WASMTableInstance **
  449. dependency_tables_instantiate(const WASMModule *module,
  450. const WASMModuleInstance * module_inst,
  451. WASMModuleInstance *root_module_inst)
  452. {
  453. WASMImport *import;
  454. uint32 table_count =
  455. module->import_table_count + module->table_count;
  456. uint32 table_index = 0, cur_size = 0, max_size = 0;
  457. uint64 total_data_size = 0;
  458. WASMTableInstance **tables, *table = NULL;
  459. WASMProgramInstance * program = module_inst->program;
  460. // have to establish a table to keep the export function pointer
  461. if (!table_count)
  462. table_count = 1;
  463. bh_assert(table_count == 1);
  464. total_data_size = sizeof(WASMTableInstance*) * (uint64)table_count;
  465. if (!(tables = runtime_malloc(total_data_size,
  466. program->error_buf, program->error_buf_size))) {
  467. return NULL;
  468. }
  469. /* instantiate tables from import section */
  470. import = module->import_tables;
  471. for (uint32 i = 0; i < module->import_table_count; i++, import++) {
  472. /* it is a built-in table, every module has its own */
  473. max_size = import->u.table.possible_grow?
  474. import->u.table.max_size:import->u.table.init_size;
  475. if (program->config.use_tbl_as_cache) {
  476. max_size += module_inst->export_func_count;
  477. cur_size = import->u.table.init_size + module_inst->export_func_count;
  478. }
  479. if (max_size > TABLE_SPACE_SLOT_SIZE) {
  480. return NULL;
  481. }
  482. total_data_size = offsetof(WASMTableInstance, base_addr);
  483. total_data_size += sizeof(uint32) * (uint64)max_size;
  484. if (!(table = tables[table_index++] = runtime_malloc
  485. (total_data_size , program->error_buf, program->error_buf_size))) {
  486. tables_deinstantiate(tables, table_count);
  487. return NULL;
  488. }
  489. /* Set all elements to -1 to mark them as uninitialized elements */
  490. memset(table, -1, (uint32)total_data_size);
  491. table->elem_type = import->u.table.elem_type;
  492. table->cur_size = cur_size;
  493. table->max_size = max_size;
  494. }
  495. if (!table_index && program->config.use_tbl_as_cache) {
  496. max_size = module_inst->export_func_count;
  497. cur_size = module_inst->export_func_count;
  498. total_data_size = offsetof(WASMTableInstance, base_addr);
  499. total_data_size += sizeof(uint32) * (uint64)max_size;
  500. if (!(table = tables[table_index++] = runtime_malloc
  501. (total_data_size , program->error_buf, program->error_buf_size))) {
  502. tables_deinstantiate(tables, table_count);
  503. return NULL;
  504. }
  505. /* Set all elements to -1 to mark them as uninitialized elements */
  506. memset(table, -1, (uint32)total_data_size);
  507. table->elem_type = VALUE_TYPE_FUNCREF;
  508. table->cur_size = cur_size;
  509. table->max_size = max_size;
  510. }
  511. bh_assert(table_index == table_count);
  512. return tables;
  513. }
  514. #endif
  515. /**
  516. * Instantiate tables in a module.
  517. */
  518. static WASMTableInstance **
  519. tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  520. char *error_buf, uint32 error_buf_size)
  521. {
  522. WASMImport *import;
  523. uint32 table_index = 0, i, table_count =
  524. module->import_table_count + module->table_count;
  525. uint64 total_size = sizeof(WASMTableInstance*) * (uint64)table_count;
  526. uint32 cur_size = 0, max_size = 0;
  527. WASMTableInstance **tables, *table;
  528. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  529. WASMRuntime * runtime = module->runtime;
  530. WASMProgramInstance * program = module_inst->program;
  531. #endif
  532. if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) {
  533. return NULL;
  534. }
  535. /* instantiate tables from import section */
  536. import = module->import_tables;
  537. for (i = 0; i < module->import_table_count; i++, import++) {
  538. uint32 max_size_fixed = 0;
  539. #if WASM_ENABLE_MULTI_MODULE != 0
  540. WASMTableInstance *table_inst_linked = NULL;
  541. WASMModuleInstance *module_inst_linked = NULL;
  542. if (import->u.table.import_module) {
  543. if (!(module_inst_linked = get_sub_module_inst(
  544. module_inst, import->u.table.import_module))) {
  545. set_error_buf(error_buf, error_buf_size, "unknown table");
  546. tables_deinstantiate(tables, table_count);
  547. return NULL;
  548. }
  549. if (!(table_inst_linked = wasm_lookup_table(
  550. module_inst_linked, import->u.table.field_name))) {
  551. set_error_buf(error_buf, error_buf_size, "unknown table");
  552. tables_deinstantiate(tables, table_count);
  553. return NULL;
  554. }
  555. total_size = offsetof(WASMTableInstance, base_addr);
  556. }
  557. else
  558. #endif
  559. {
  560. /* in order to save memory, alloc resource as few as possible */
  561. max_size_fixed = import->u.table.possible_grow
  562. ? import->u.table.max_size
  563. : import->u.table.init_size;
  564. /* it is a built-in table, every module has its own */
  565. total_size = offsetof(WASMTableInstance, base_addr);
  566. max_size = max_size_fixed;
  567. cur_size = import->u.table.init_size;
  568. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  569. if (runtime->config.need_load_dependencies) {
  570. if (program->config.use_tbl_as_cache) {
  571. max_size += module_inst->export_func_count;
  572. cur_size += module_inst->export_func_count;
  573. }
  574. if (module->dylink_section) {
  575. if (wasm_program_is_root_module((WASMModuleInstanceCommon*)module_inst)) {
  576. max_size ++;
  577. cur_size ++;
  578. }
  579. }
  580. if (max_size > TABLE_SPACE_SLOT_SIZE) {
  581. return NULL;
  582. }
  583. }
  584. #endif
  585. total_size += sizeof(uint32) * max_size;
  586. }
  587. if (!(table = tables[table_index++] = runtime_malloc
  588. (total_size , error_buf, error_buf_size))) {
  589. tables_deinstantiate(tables, table_count);
  590. return NULL;
  591. }
  592. /* Set all elements to -1 to mark them as uninitialized elements */
  593. memset(table, -1, (uint32)total_size);
  594. #if WASM_ENABLE_MULTI_MODULE != 0
  595. table->table_inst_linked = table_inst_linked;
  596. if (table_inst_linked != NULL) {
  597. table->elem_type = table_inst_linked->elem_type;
  598. table->cur_size = table_inst_linked->cur_size;
  599. table->max_size = table_inst_linked->max_size;
  600. }
  601. else
  602. #endif
  603. {
  604. table->elem_type = import->u.table.elem_type;
  605. table->cur_size = cur_size;
  606. table->max_size = max_size;
  607. }
  608. }
  609. /* instantiate tables from table section */
  610. for (i = 0; i < module->table_count; i++) {
  611. uint32 max_size_fixed = 0;
  612. total_size = offsetof(WASMTableInstance, base_addr);
  613. #if WASM_ENABLE_MULTI_MODULE != 0
  614. /* in case, a module which imports this table will grow it */
  615. max_size_fixed = module->tables[i].max_size;
  616. #else
  617. max_size_fixed = module->tables[i].possible_grow
  618. ? module->tables[i].max_size
  619. : module->tables[i].init_size;
  620. #endif
  621. total_size += sizeof(uint32) * (uint64)max_size_fixed;
  622. if (!(table = tables[table_index++] =
  623. runtime_malloc(total_size, error_buf, error_buf_size))) {
  624. tables_deinstantiate(tables, table_count);
  625. return NULL;
  626. }
  627. /* Set all elements to -1 to mark them as uninitialized elements */
  628. memset(table, -1, (uint32)total_size);
  629. table->elem_type = module->tables[i].elem_type;
  630. table->cur_size = module->tables[i].init_size;
  631. table->max_size = max_size_fixed;
  632. #if WASM_ENABLE_MULTI_MODULE != 0
  633. table->table_inst_linked = NULL;
  634. #endif
  635. }
  636. bh_assert(table_index == table_count);
  637. (void)module_inst;
  638. return tables;
  639. }
  640. /**
  641. * Destroy function instances.
  642. */
  643. static void
  644. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  645. {
  646. if (functions) {
  647. wasm_runtime_free(functions);
  648. }
  649. }
  650. /**
  651. * Instantiate functions in a module.
  652. */
  653. static WASMFunctionInstance *
  654. functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  655. char *error_buf, uint32 error_buf_size)
  656. {
  657. WASMImport *import;
  658. uint32 i,
  659. function_count = module->import_function_count + module->function_count;
  660. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  661. WASMFunctionInstance *functions, *function;
  662. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  663. WASMProgramInstance * program = module_inst->program;
  664. WASMRuntime * runtime = module->runtime;
  665. #endif
  666. if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) {
  667. return NULL;
  668. }
  669. /* instantiate functions from import section */
  670. function = functions;
  671. import = module->import_functions;
  672. for (i = 0; i < module->import_function_count; i++, import++) {
  673. function->module_inst = module_inst;
  674. function->is_import_func = true;
  675. #if WASM_ENABLE_MULTI_MODULE != 0
  676. if (import->u.function.import_module) {
  677. function->import_module_inst = get_sub_module_inst(
  678. module_inst, import->u.function.import_module);
  679. if (function->import_module_inst) {
  680. function->import_func_inst =
  681. wasm_lookup_function(function->import_module_inst,
  682. import->u.function.field_name->str, NULL);
  683. }
  684. }
  685. #endif /* WASM_ENABLE_MULTI_MODULE */
  686. function->u.func_import = &import->u.function;
  687. function->func_type = import->u.function.func_type;
  688. function->local_cell_num = 0;
  689. function->local_count = 0;
  690. function->local_types = NULL;
  691. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  692. // reset malloc/free/realloc func pointer because it's likely that they are not correct.
  693. // will be re-linked in resolving stage later
  694. if (program && program->root_module_inst &&
  695. (WASMModuleInstance*)program->root_module_inst != module_inst &&
  696. program->config.import_memop_mode == FROM_ROOT &&
  697. wasm_runtime_is_memop_symbol(runtime, import->u.function.field_name) &&
  698. import->u.function.module_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_env)) {
  699. import->u.function.func_ptr_linked = NULL;
  700. }
  701. #endif
  702. function++;
  703. }
  704. /* instantiate functions from function section */
  705. for (i = 0; i < module->function_count; i++) {
  706. function->module_inst = module_inst;
  707. function->is_import_func = false;
  708. function->u.func = module->functions[i];
  709. function->func_type = module->functions[i]->func_type;
  710. function->local_cell_num = function->u.func->local_cell_num;
  711. function->local_count = (uint16)function->u.func->local_count;
  712. function->local_types = function->u.func->local_types;
  713. function->local_offsets = function->u.func->local_offsets;
  714. #if WASM_ENABLE_FAST_INTERP != 0
  715. function->const_cell_num = function->u.func->const_cell_num;
  716. #endif
  717. function++;
  718. }
  719. bh_assert((uint32)(function - functions) == function_count);
  720. (void)module_inst;
  721. return functions;
  722. }
  723. /**
  724. * Destroy global instances.
  725. */
  726. static void
  727. globals_deinstantiate(WASMGlobalInstance *globals)
  728. {
  729. if (globals)
  730. wasm_runtime_free(globals);
  731. }
  732. static bool
  733. check_global_init_expr(const WASMModule *module, uint32 global_index,
  734. char *error_buf, uint32 error_buf_size)
  735. {
  736. if (global_index >= module->import_global_count + module->global_count) {
  737. set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
  738. global_index);
  739. return false;
  740. }
  741. /**
  742. * Currently, constant expressions occurring as initializers of
  743. * globals are further constrained in that contained global.get
  744. * instructions are only allowed to refer to imported globals.
  745. *
  746. * And initializer expression cannot reference a mutable global.
  747. */
  748. if (global_index >= module->import_global_count
  749. || (module->import_globals + global_index)->u.global.is_mutable) {
  750. set_error_buf(error_buf, error_buf_size,
  751. "constant expression required");
  752. return false;
  753. }
  754. return true;
  755. }
  756. /**
  757. * Instantiate globals in a module.
  758. */
  759. static WASMGlobalInstance *
  760. globals_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
  761. uint32 *p_global_data_size, char *error_buf,
  762. uint32 error_buf_size)
  763. {
  764. WASMImport *import;
  765. uint32 global_data_offset = 0;
  766. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  767. WASMRuntime * runtime = module->runtime;
  768. #endif
  769. uint32 i, global_count =
  770. module->import_global_count + module->global_count;
  771. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  772. WASMGlobalInstance *globals, *global;
  773. if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) {
  774. return NULL;
  775. }
  776. /* instantiate globals from import section */
  777. global = globals;
  778. import = module->import_globals;
  779. for (i = 0; i < module->import_global_count; i++, import++) {
  780. WASMGlobalImport *global_import = &import->u.global;
  781. global->type = global_import->type;
  782. global->is_mutable = global_import->is_mutable;
  783. global->data = NULL;
  784. #if WASM_ENABLE_MULTI_MODULE != 0
  785. if (global_import->import_module) {
  786. if (!(global->import_module_inst = get_sub_module_inst(
  787. module_inst, global_import->import_module))) {
  788. set_error_buf(error_buf, error_buf_size, "unknown global");
  789. return NULL;
  790. }
  791. if (!(global->import_global_inst = wasm_lookup_global(
  792. global->import_module_inst, global_import->field_name->str))) {
  793. set_error_buf(error_buf, error_buf_size, "unknown global");
  794. return NULL;
  795. }
  796. /* The linked global instance has been initialized, we
  797. just need to copy the value. */
  798. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  799. &(global_import->import_global_linked->init_expr),
  800. sizeof(WASMValue));
  801. }
  802. else
  803. #endif
  804. {
  805. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  806. if (global_import->module_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_env)) {
  807. if (global_import->field_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_var_stack_pointer)) {
  808. bh_assert(global->is_mutable == true);
  809. global->initial_value.u32 = module_inst->init_globals.stack_pointer;
  810. global_import->is_linked = true;
  811. //global->data = (uint8*)&module_inst->init_globals.stack_pointer;
  812. } else if (global_import->field_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_var_memory_base)) {
  813. bh_assert(global->is_mutable == false);
  814. global->initial_value.u32 = module_inst->init_globals.memory_base;
  815. global_import->is_linked = true;
  816. } else if (global_import->field_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_var_table_base)) {
  817. bh_assert(global->is_mutable == false);
  818. global->initial_value.u32 = module_inst->init_globals.table_base;
  819. global_import->is_linked = true;
  820. } else
  821. #endif
  822. {
  823. // TODO: link other env global variables if exists
  824. /* native globals share their initial_values in one module */
  825. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  826. &(global_import->global_data_linked),
  827. sizeof(WASMValue));
  828. }
  829. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  830. } else {
  831. // TODO: link other non-env global variables if exists
  832. // lazy link
  833. if (global_import->is_linked)
  834. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  835. &(global_import->global_data_linked),
  836. sizeof(WASMValue));
  837. }
  838. #endif
  839. }
  840. // global->data_offset = global_data_offset;
  841. global_data_offset += wasm_value_type_size(global->type);
  842. global++;
  843. }
  844. /* instantiate globals from global section */
  845. for (i = 0; i < module->global_count; i++) {
  846. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  847. global->type = module->globals[i].type;
  848. global->is_mutable = module->globals[i].is_mutable;
  849. // global->data_offset = global_data_offset;
  850. global->data = NULL;
  851. global_data_offset += wasm_value_type_size(global->type);
  852. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  853. if (!check_global_init_expr(module, init_expr->u.global_index,
  854. error_buf, error_buf_size)) {
  855. return NULL;
  856. }
  857. bh_memcpy_s(
  858. &(global->initial_value), sizeof(WASMValue),
  859. &(globals[init_expr->u.global_index].initial_value),
  860. sizeof(globals[init_expr->u.global_index].initial_value));
  861. }
  862. #if WASM_ENABLE_REF_TYPES != 0
  863. else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) {
  864. global->initial_value.u32 = (uint32)NULL_REF;
  865. }
  866. #endif
  867. else {
  868. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  869. &(init_expr->u), sizeof(init_expr->u));
  870. }
  871. global++;
  872. }
  873. bh_assert((uint32)(global - globals) == global_count);
  874. *p_global_data_size = global_data_offset;
  875. (void)module_inst;
  876. return globals;
  877. }
  878. /**
  879. * Return export function count in module export section.
  880. */
  881. static void
  882. get_export_count(const WASMModule *module, WASMModuleInstance * module_inst)
  883. {
  884. WASMExport *export = module->exports;
  885. for (uint32 i = 0; i < module->export_count; i++, export++) {
  886. if (export->kind == EXPORT_KIND_FUNC)
  887. module_inst->export_func_count ++;
  888. #if WASM_ENABLE_MULTI_MODULE != 0 || WASM_ENABLE_DYNAMIC_LINKING != 0
  889. else if (export->kind == EXPORT_KIND_TABLE)
  890. module_inst->export_tab_count ++;
  891. else if (export->kind == EXPORT_KIND_MEMORY)
  892. module_inst->export_mem_count ++;
  893. else
  894. module_inst->export_glob_count ++;
  895. #endif
  896. }
  897. }
  898. /**
  899. * Destroy export function instances.
  900. */
  901. static void
  902. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  903. {
  904. if (functions)
  905. wasm_runtime_free(functions);
  906. }
  907. /**
  908. * Instantiate export functions in a module.
  909. */
  910. static WASMExportFuncInstance *
  911. export_functions_instantiate(const WASMModule *module,
  912. WASMModuleInstance *module_inst,
  913. uint32 export_func_count, char *error_buf,
  914. uint32 error_buf_size)
  915. {
  916. WASMExportFuncInstance *export_funcs, *export_func;
  917. WASMExport *export = module->exports;
  918. uint32 i;
  919. uint64 total_size =
  920. sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  921. if (!(export_func = export_funcs =
  922. runtime_malloc(total_size, error_buf, error_buf_size))) {
  923. return NULL;
  924. }
  925. for (i = 0; i < module->export_count; i++, export ++)
  926. if (export->kind == EXPORT_KIND_FUNC) {
  927. export_func->name = export->name;
  928. export_func->function = &module_inst->functions[export->index];
  929. export_func++;
  930. }
  931. bh_assert((uint32)(export_func - export_funcs) == export_func_count);
  932. return export_funcs;
  933. }
  934. #if WASM_ENABLE_MULTI_MODULE != 0 || WASM_ENABLE_DYNAMIC_LINKING != 0
  935. static void
  936. export_globals_deinstantiate(WASMExportGlobInstance *globals)
  937. {
  938. if (globals)
  939. wasm_runtime_free(globals);
  940. }
  941. static WASMExportGlobInstance *
  942. export_globals_instantiate(const WASMModule *module,
  943. WASMModuleInstance *module_inst,
  944. uint32 export_glob_count, char *error_buf,
  945. uint32 error_buf_size)
  946. {
  947. WASMExportGlobInstance *export_globals, *export_global;
  948. WASMExport *export = module->exports;
  949. uint32 i;
  950. uint64 total_size =
  951. sizeof(WASMExportGlobInstance) * (uint64)export_glob_count;
  952. if (!(export_global = export_globals =
  953. runtime_malloc(total_size, error_buf, error_buf_size))) {
  954. return NULL;
  955. }
  956. for (i = 0; i < module->export_count; i++, export ++)
  957. if (export->kind == EXPORT_KIND_GLOBAL) {
  958. export_global->name = export->name;
  959. export_global->global = &module_inst->globals[export->index];
  960. export_global++;
  961. }
  962. bh_assert((uint32)(export_global - export_globals) == export_glob_count);
  963. return export_globals;
  964. }
  965. #endif
  966. static bool
  967. execute_post_inst_function(WASMModuleInstance *module_inst)
  968. {
  969. WASMFunctionInstance *post_inst_func = NULL;
  970. WASMType *post_inst_func_type;
  971. uint32 i;
  972. // https://github.com/emscripten-core/emscripten/issues/13893
  973. for (i = 0; i < module_inst->export_func_count; i++)
  974. if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate") ||
  975. !strcmp(module_inst->export_functions[i].name, "__wasm_call_ctors")) {
  976. post_inst_func = module_inst->export_functions[i].function;
  977. break;
  978. }
  979. if (!post_inst_func)
  980. /* Not found */
  981. return true;
  982. post_inst_func_type = post_inst_func->u.func->func_type;
  983. if (post_inst_func_type->param_count != 0
  984. || post_inst_func_type->result_count != 0)
  985. /* Not a valid function type, ignore it */
  986. return true;
  987. return wasm_create_exec_env_and_call_function(module_inst, post_inst_func,
  988. 0, NULL);
  989. }
  990. #if WASM_ENABLE_BULK_MEMORY != 0
  991. static bool
  992. execute_memory_init_function(WASMModuleInstance *module_inst)
  993. {
  994. WASMFunctionInstance *memory_init_func = NULL;
  995. WASMType *memory_init_func_type;
  996. uint32 i;
  997. for (i = 0; i < module_inst->export_func_count; i++)
  998. if (!strcmp(module_inst->export_functions[i].name,
  999. "__wasm_call_ctors")) {
  1000. memory_init_func = module_inst->export_functions[i].function;
  1001. break;
  1002. }
  1003. if (!memory_init_func)
  1004. /* Not found */
  1005. return true;
  1006. memory_init_func_type = memory_init_func->u.func->func_type;
  1007. if (memory_init_func_type->param_count != 0
  1008. || memory_init_func_type->result_count != 0)
  1009. /* Not a valid function type, ignore it */
  1010. return true;
  1011. return wasm_create_exec_env_and_call_function(module_inst, memory_init_func,
  1012. 0, NULL);
  1013. }
  1014. #endif
  1015. static bool
  1016. execute_start_function(WASMModuleInstance *module_inst)
  1017. {
  1018. WASMFunctionInstance *func = module_inst->start_function;
  1019. if (!func)
  1020. return true;
  1021. bh_assert(!func->is_import_func && func->func_type->param_cell_num == 0
  1022. && func->func_type->ret_cell_num == 0);
  1023. return wasm_create_exec_env_and_call_function(module_inst, func, 0, NULL);
  1024. }
  1025. static bool
  1026. execute_malloc_function(WASMModuleInstance *module_inst,
  1027. WASMFunctionInstance *malloc_func,
  1028. WASMFunctionInstance *retain_func, uint32 size,
  1029. uint32 *p_result)
  1030. {
  1031. uint32 argv[2], argc;
  1032. bool ret;
  1033. argv[0] = size;
  1034. argc = 1;
  1035. /* if __retain is exported, then this module is compiled by
  1036. assemblyscript, the memory should be managed by as's runtime,
  1037. in this case we need to call the retain function after malloc
  1038. the memory */
  1039. if (retain_func) {
  1040. /* the malloc functino from assemblyscript is:
  1041. function __new(size: usize, id: u32)
  1042. id = 0 means this is an ArrayBuffer object */
  1043. argv[1] = 0;
  1044. argc = 2;
  1045. }
  1046. ret = wasm_create_exec_env_and_call_function(module_inst, malloc_func, argc,
  1047. argv);
  1048. if (retain_func && ret) {
  1049. ret = wasm_create_exec_env_and_call_function(module_inst, retain_func,
  1050. 1, argv);
  1051. }
  1052. if (ret)
  1053. *p_result = argv[0];
  1054. return ret;
  1055. }
  1056. static bool
  1057. execute_free_function(WASMModuleInstance *module_inst,
  1058. WASMFunctionInstance *free_func, uint32 offset)
  1059. {
  1060. uint32 argv[2];
  1061. argv[0] = offset;
  1062. return wasm_create_exec_env_and_call_function(module_inst, free_func, 1,
  1063. argv);
  1064. }
  1065. #if WASM_ENABLE_MULTI_MODULE != 0
  1066. static bool
  1067. sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
  1068. uint32 stack_size, uint32 heap_size, char *error_buf,
  1069. uint32 error_buf_size)
  1070. {
  1071. bh_list *sub_module_inst_list = module_inst->sub_module_inst_list;
  1072. WASMRegisteredModule *sub_module_list_node =
  1073. bh_list_first_elem(module->import_module_list);
  1074. while (sub_module_list_node) {
  1075. WASMSubModInstNode *sub_module_inst_list_node;
  1076. WASMModule *sub_module = (WASMModule *)sub_module_list_node->module;
  1077. WASMModuleInstance *sub_module_inst =
  1078. wasm_instantiate(NULL, sub_module, false, stack_size, heap_size,
  1079. error_buf, error_buf_size);
  1080. if (!sub_module_inst) {
  1081. LOG_DEBUG("instantiate %s failed",
  1082. sub_module_list_node->module_name);
  1083. return false;
  1084. }
  1085. sub_module_inst_list_node = runtime_malloc(sizeof(WASMSubModInstNode),
  1086. error_buf, error_buf_size);
  1087. if (!sub_module_inst_list_node) {
  1088. LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d",
  1089. sizeof(WASMSubModInstNode));
  1090. wasm_deinstantiate(sub_module_inst, false);
  1091. return false;
  1092. }
  1093. sub_module_inst_list_node->module_inst = sub_module_inst;
  1094. sub_module_inst_list_node->module_name =
  1095. sub_module_list_node->module_name;
  1096. bh_list_status ret =
  1097. bh_list_insert(sub_module_inst_list, sub_module_inst_list_node);
  1098. bh_assert(BH_LIST_SUCCESS == ret);
  1099. (void)ret;
  1100. sub_module_list_node = bh_list_elem_next(sub_module_list_node);
  1101. }
  1102. return true;
  1103. }
  1104. static void
  1105. sub_module_deinstantiate(WASMModuleInstance *module_inst)
  1106. {
  1107. bh_list *list = module_inst->sub_module_inst_list;
  1108. WASMSubModInstNode *node = bh_list_first_elem(list);
  1109. while (node) {
  1110. WASMSubModInstNode *next_node = bh_list_elem_next(node);
  1111. bh_list_remove(list, node);
  1112. wasm_deinstantiate(node->module_inst, false);
  1113. wasm_runtime_free(node);
  1114. node = next_node;
  1115. }
  1116. }
  1117. #endif
  1118. static bool
  1119. check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf,
  1120. uint32 error_buf_size)
  1121. {
  1122. WASMModule *module = module_inst->module;
  1123. uint32 i;
  1124. for (i = 0; i < module->import_function_count; i++) {
  1125. WASMFunctionImport *func =
  1126. &((module->import_functions + i)->u.function);
  1127. if (!func->func_ptr_linked
  1128. #if WASM_ENABLE_MULTI_MODULE != 0
  1129. && !func->import_func_linked
  1130. #endif
  1131. ) {
  1132. #if WASM_ENABLE_SPEC_TEST != 0
  1133. set_error_buf(error_buf, error_buf_size,
  1134. "unknown import or incompatible import type");
  1135. return false;
  1136. #else
  1137. #if WASM_ENABLE_WAMR_COMPILER == 0
  1138. LOG_WARNING("warning: failed to link import function (%s, %s)",
  1139. func->module_name->str, func->field_name->str);
  1140. #else
  1141. /* do nothing to avoid confused message */
  1142. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1143. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  1144. }
  1145. }
  1146. for (i = 0; i < module->import_global_count; i++) {
  1147. WASMGlobalImport *global = &((module->import_globals + i)->u.global);
  1148. if (!global->is_linked) {
  1149. #if WASM_ENABLE_SPEC_TEST != 0
  1150. set_error_buf(error_buf, error_buf_size,
  1151. "unknown import or incompatible import type");
  1152. return false;
  1153. #else
  1154. #if WASM_ENABLE_WAMR_COMPILER == 0
  1155. LOG_DEBUG("warning: failed to link import global (%s, %s)",
  1156. global->module_name->str, global->field_name->str);
  1157. #else
  1158. /* do nothing to avoid confused message */
  1159. #endif /* WASM_ENABLE_WAMR_COMPILER == 0 */
  1160. #endif /* WASM_ENABLE_SPEC_TEST != 0 */
  1161. }
  1162. }
  1163. return true;
  1164. }
  1165. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1166. static WASMGlobalInstance *
  1167. dependency_globals_instantiate(const WASMModule *module,
  1168. WASMModuleInstance *module_inst,
  1169. uint32 *global_data_size_ptr)
  1170. {
  1171. WASMImport *import;
  1172. uint32 global_data_offset = 0;
  1173. uint32 i, global_count =
  1174. module->import_global_count + module->global_count;
  1175. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  1176. WASMGlobalInstance *globals, *global;
  1177. WASMRuntime * runtime = module->runtime;
  1178. WASMProgramInstance * program = module_inst->program;
  1179. if (!(globals = runtime_malloc(total_size,
  1180. program->error_buf, program->error_buf_size))) {
  1181. return NULL;
  1182. }
  1183. /* instantiate globals from import section */
  1184. global = globals;
  1185. import = module->import_globals;
  1186. for (i = 0; i < module->import_global_count; i++, import++) {
  1187. WASMGlobalImport *global_import = &import->u.global;
  1188. global->type = global_import->type;
  1189. global->is_mutable = global_import->is_mutable;
  1190. global->data = NULL;
  1191. if (global_import->module_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_env)) {
  1192. if (global_import->field_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_var_stack_pointer)) {
  1193. if (!wasm_program_link_sp_wasm_globals(program, global, global_import->field_name)) {
  1194. set_error_buf(program->error_buf, program->error_buf_size,
  1195. "link sp globals failed.");
  1196. return NULL;
  1197. }
  1198. } else if (global_import->field_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_var_memory_base)) {
  1199. //global_import->is_linked = true;
  1200. //global_import->global_data_linked.u32 = module_inst->init_globals.memory_base;
  1201. bh_assert(global->is_mutable == false);
  1202. global->initial_value.u32 = module_inst->init_globals.memory_base;
  1203. } else if (global_import->field_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_var_table_base)) {
  1204. bh_assert(global->is_mutable == false);
  1205. global->initial_value.u32 = module_inst->init_globals.table_base;
  1206. } else {
  1207. // TODO: link other env global variables if exists
  1208. /* native globals share their initial_values in one module */
  1209. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  1210. &(global_import->global_data_linked),
  1211. sizeof(WASMValue));
  1212. }
  1213. } else if (global_import->module_name == CONST_STR_POOL_DESC(runtime, WAMR_CSP_GOT_func)) {
  1214. if (!wasm_program_link_got_globals(program,
  1215. module_inst, global, global_import->field_name)) {
  1216. set_error_buf(program->error_buf, program->error_buf_size,
  1217. "link GOT.func globals failed.");
  1218. return NULL;
  1219. }
  1220. } else {
  1221. // TODO: link other non-env global variables if exists
  1222. // lazy link
  1223. }
  1224. // global->data_offset = global_data_offset;
  1225. global_data_offset += wasm_value_type_size(global->type);
  1226. global++;
  1227. }
  1228. /* instantiate globals from global section */
  1229. for (i = 0; i < module->global_count; i++) {
  1230. InitializerExpression *init_expr = &(module->globals[i].init_expr);
  1231. global->type = module->globals[i].type;
  1232. global->is_mutable = module->globals[i].is_mutable;
  1233. // global->data_offset = global_data_offset;
  1234. global->data = NULL;
  1235. global_data_offset += wasm_value_type_size(global->type);
  1236. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1237. if (!check_global_init_expr(module, init_expr->u.global_index,
  1238. program->error_buf, program->error_buf_size)) {
  1239. return NULL;
  1240. }
  1241. bh_memcpy_s(
  1242. &(global->initial_value), sizeof(WASMValue),
  1243. &(globals[init_expr->u.global_index].initial_value),
  1244. sizeof(globals[init_expr->u.global_index].initial_value));
  1245. }
  1246. #if WASM_ENABLE_REF_TYPES != 0
  1247. else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) {
  1248. global->initial_value.u32 = (uint32)NULL_REF;
  1249. }
  1250. #endif
  1251. else {
  1252. bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
  1253. &(init_expr->u), sizeof(init_expr->u));
  1254. }
  1255. global++;
  1256. }
  1257. bh_assert((uint32)(global - globals) == global_count);
  1258. *global_data_size_ptr = global_data_offset;
  1259. (void)module_inst;
  1260. return globals;
  1261. }
  1262. WASMModuleInstance*
  1263. wasm_instantiate_dependency(WASMModule *module, WASMProgramInstance * program,
  1264. uint32 stack_size, DependencyModuleInitGlobals * init_globals)
  1265. {
  1266. WASMModuleInstance *module_inst;
  1267. WASMGlobalInstance *globals = NULL, *global;
  1268. uint32 global_count, global_data_size = 0, i = 0, table_space_start = 0;
  1269. uint32 base_offset, length;
  1270. uint8 *global_data, *global_data_end;
  1271. /* Allocate the memory */
  1272. if (!(module_inst = wasm_runtime_malloc(sizeof(WASMModuleInstance)))) {
  1273. return NULL;
  1274. }
  1275. memset(module_inst, 0, sizeof(WASMModuleInstance));
  1276. module_inst->module = module;
  1277. module_inst->runtime = wasm_runtime_get_runtime();
  1278. module_inst->program = program;
  1279. module_inst->inst_id = wasm_program_alloc_module_instance_id(program, (WASMModuleInstanceCommon*)module_inst);
  1280. module_inst->local_implicit_dependency_modules_name_hmap = NULL;
  1281. module_inst->exp_ref_cnt = module_inst->imp_ref_cnt = 0;
  1282. if (module_inst->inst_id <= 1)
  1283. goto fail;
  1284. memcpy(&module_inst->init_globals, init_globals, sizeof(DependencyModuleInitGlobals));
  1285. global_count = module->import_global_count + module->global_count;
  1286. module_inst->global_count = global_count;
  1287. bh_assert(module->memory_count == 0 && module->import_memory_count == 1);
  1288. module_inst->memory_count =
  1289. module->import_memory_count;
  1290. module_inst->table_count =
  1291. module->import_table_count + module->table_count;
  1292. module_inst->function_count =
  1293. module->import_function_count + module->function_count;
  1294. /* export */
  1295. get_export_count(module, module_inst);
  1296. // use root module memory to instantiate
  1297. module_inst->memories = dependency_memories_instantiate(module, (WASMModuleInstance*)program->root_module_inst);
  1298. /* Initialize the memory data with data segment section */
  1299. module_inst->default_memory =
  1300. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1301. if (!module_inst->table_count)
  1302. module_inst->table_count = 1;
  1303. module_inst->tables = dependency_tables_instantiate(module, module_inst, (WASMModuleInstance*)program->root_module_inst);
  1304. module_inst->default_table =
  1305. module_inst->table_count ? module_inst->tables[0] : NULL;
  1306. table_space_start = wasm_program_alloc_table_space_by_size(module_inst->inst_id, module_inst->default_table->max_size);
  1307. module_inst->init_globals.table_base = (table_space_start + init_globals->table_alignment - 1) &
  1308. (~(init_globals->table_alignment - 1));
  1309. if (module_inst->function_count > 0) {
  1310. module_inst->functions = functions_instantiate(module, module_inst,
  1311. program->error_buf, program->error_buf_size);
  1312. }
  1313. if (module_inst->export_func_count > 0) {
  1314. module_inst->export_functions = export_functions_instantiate(module, module_inst,
  1315. module_inst->export_func_count,
  1316. program->error_buf, program->error_buf_size);
  1317. }
  1318. if (global_count
  1319. && !(globals = dependency_globals_instantiate(module, module_inst,
  1320. &global_data_size))) {
  1321. goto fail;
  1322. }
  1323. module_inst->globals = globals;
  1324. if (global_count > 0) {
  1325. if (!(module_inst->global_data = runtime_malloc
  1326. (global_data_size, program->error_buf,
  1327. program->error_buf_size))) {
  1328. goto fail;
  1329. }
  1330. }
  1331. if (global_count > 0) {
  1332. /* Initialize the global data */
  1333. global_data = module_inst->global_data;
  1334. global_data_end = global_data + global_data_size;
  1335. global = globals;
  1336. for (i = 0; i < global_count; i++, global++) {
  1337. if (!global->data)
  1338. global->data = global_data;
  1339. switch (global->type) {
  1340. case VALUE_TYPE_I32:
  1341. case VALUE_TYPE_F32:
  1342. #if WASM_ENABLE_REF_TYPES != 0
  1343. case VALUE_TYPE_FUNCREF:
  1344. case VALUE_TYPE_EXTERNREF:
  1345. #endif
  1346. *(int32*)global_data = global->initial_value.i32;
  1347. global_data += sizeof(int32);
  1348. break;
  1349. case VALUE_TYPE_I64:
  1350. case VALUE_TYPE_F64:
  1351. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  1352. &global->initial_value.i64, sizeof(int64));
  1353. global_data += sizeof(int64);
  1354. break;
  1355. default:
  1356. bh_assert(0);
  1357. }
  1358. }
  1359. bh_assert(global_data == global_data_end);
  1360. }
  1361. for (i = 0; i < module->data_seg_count; i++) {
  1362. WASMMemoryInstance *memory = NULL;
  1363. uint8 *memory_data = NULL;
  1364. uint32 memory_size = 0;
  1365. WASMDataSeg *data_seg = &module->data_segments[i];
  1366. #if WASM_ENABLE_BULK_MEMORY != 0
  1367. if (data_seg->is_passive)
  1368. continue;
  1369. #endif
  1370. bh_assert(data_seg->memory_index == 0);
  1371. /* has check it in loader */
  1372. memory = module_inst->memories[data_seg->memory_index];
  1373. bh_assert(memory);
  1374. memory_data = memory->memory_data;
  1375. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1376. bh_assert(memory_data || memory_size == 0);
  1377. bh_assert(data_seg->base_offset.init_expr_type
  1378. == INIT_EXPR_TYPE_I32_CONST
  1379. || data_seg->base_offset.init_expr_type
  1380. == INIT_EXPR_TYPE_GET_GLOBAL);
  1381. if (data_seg->base_offset.init_expr_type
  1382. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1383. if (!check_global_init_expr(module,
  1384. data_seg->base_offset.u.global_index,
  1385. program->error_buf, program->error_buf_size)) {
  1386. goto fail;
  1387. }
  1388. if (!globals
  1389. || globals[data_seg->base_offset.u.global_index].type
  1390. != VALUE_TYPE_I32) {
  1391. set_error_buf(program->error_buf, program->error_buf_size,
  1392. "data segment does not fit");
  1393. goto fail;
  1394. }
  1395. base_offset = globals[data_seg->base_offset.u.global_index]
  1396. .initial_value.i32;
  1397. } else {
  1398. base_offset = (uint32)data_seg->base_offset.u.i32;
  1399. }
  1400. /* check offset */
  1401. if (base_offset > memory_size) {
  1402. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1403. memory_size);
  1404. #if WASM_ENABLE_REF_TYPES != 0
  1405. set_error_buf(program->error_buf, program->error_buf_size,
  1406. "out of bounds memory access");
  1407. #else
  1408. set_error_buf(program->error_buf, program->error_buf_size,
  1409. "data segment does not fit");
  1410. #endif
  1411. goto fail;
  1412. }
  1413. /* check offset + length(could be zero) */
  1414. length = data_seg->data_length;
  1415. if (base_offset + length > memory_size) {
  1416. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1417. base_offset, length, memory_size);
  1418. #if WASM_ENABLE_REF_TYPES != 0
  1419. set_error_buf(program->error_buf, program->error_buf_size,
  1420. "out of bounds memory access");
  1421. #else
  1422. set_error_buf(program->error_buf, program->error_buf_size,
  1423. "data segment does not fit");
  1424. #endif
  1425. goto fail;
  1426. }
  1427. if (memory_data) {
  1428. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1429. data_seg->data, length);
  1430. }
  1431. }
  1432. /* in case there is no table */
  1433. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1434. i++) {
  1435. uint32 table_base = 0;
  1436. WASMTableSeg *table_seg = module->table_segments + i;
  1437. bh_assert(table_seg->table_index == 0);
  1438. /* has check it in loader */
  1439. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1440. bh_assert(table);
  1441. #if WASM_ENABLE_REF_TYPES != 0
  1442. if (table->elem_type != VALUE_TYPE_FUNCREF
  1443. && table->elem_type != VALUE_TYPE_EXTERNREF) {
  1444. set_error_buf(program->error_buf, program->error_buf_size,
  1445. "elements segment does not fit");
  1446. goto fail;
  1447. }
  1448. #endif
  1449. uint32 *table_data = (uint32 *)table->base_addr;
  1450. #if WASM_ENABLE_MULTI_MODULE != 0
  1451. table_data = table->table_inst_linked
  1452. ? (uint32 *)table->table_inst_linked->base_addr
  1453. : table_data;
  1454. #endif
  1455. bh_assert(table_data);
  1456. #if WASM_ENABLE_REF_TYPES != 0
  1457. if (!wasm_elem_is_active(table_seg->mode))
  1458. continue;
  1459. #endif
  1460. /* init vec(funcidx) or vec(expr) */
  1461. bh_assert(
  1462. table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST
  1463. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
  1464. #if WASM_ENABLE_REF_TYPES != 0
  1465. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_FUNCREF_CONST
  1466. || table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST
  1467. #endif
  1468. );
  1469. if (table_seg->base_offset.init_expr_type
  1470. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1471. if (!check_global_init_expr(module,
  1472. table_seg->base_offset.u.global_index,
  1473. program->error_buf, program->error_buf_size)) {
  1474. goto fail;
  1475. }
  1476. if (!globals
  1477. || globals[table_seg->base_offset.u.global_index].type
  1478. != VALUE_TYPE_I32) {
  1479. set_error_buf(program->error_buf, program->error_buf_size,
  1480. "elements segment does not fit");
  1481. goto fail;
  1482. }
  1483. table_base = globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1484. }
  1485. /* check offset since length might negative */
  1486. if (table_base > (module_inst->init_globals.table_base + table->cur_size)) {
  1487. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1488. table_base, module_inst->init_globals.table_base + table->cur_size);
  1489. #if WASM_ENABLE_REF_TYPES != 0
  1490. set_error_buf(program->error_buf, program->error_buf_size,
  1491. "out of bounds table access");
  1492. #else
  1493. set_error_buf(program->error_buf, program->error_buf_size,
  1494. "elements segment does not fit");
  1495. #endif
  1496. goto fail;
  1497. }
  1498. /* check offset + length(could be zero) */
  1499. length = table_seg->function_count;
  1500. if (table_base + length > (module_inst->init_globals.table_base + table->cur_size)) {
  1501. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1502. table_base, length, module_inst->init_globals.table_base + table->cur_size);
  1503. #if WASM_ENABLE_REF_TYPES != 0
  1504. set_error_buf(program->error_buf, program->error_buf_size,
  1505. "out of bounds table access");
  1506. #else
  1507. set_error_buf(program->error_buf, program->error_buf_size,
  1508. "elements segment does not fit");
  1509. #endif
  1510. goto fail;
  1511. }
  1512. /**
  1513. * Check function index in the current module inst for now.
  1514. * will check the linked table inst owner in future.
  1515. * so loader check is enough
  1516. */
  1517. bh_memcpy_s(
  1518. table_data + table_base - module_inst->init_globals.table_base,
  1519. (uint32)((module_inst->init_globals.table_base + table->cur_size - table_base)
  1520. * sizeof(uint32)),
  1521. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1522. }
  1523. /* module instance type */
  1524. module_inst->module_type = Wasm_Module_Bytecode;
  1525. /* Initialize the thread related data */
  1526. if (stack_size == 0)
  1527. stack_size = DEFAULT_WASM_STACK_SIZE;
  1528. #if WASM_ENABLE_SPEC_TEST != 0
  1529. if (stack_size < 48 *1024)
  1530. stack_size = 48 * 1024;
  1531. #endif
  1532. module_inst->default_wasm_stack_size = stack_size;
  1533. if (module->malloc_function != (uint32)-1) {
  1534. module_inst->malloc_function =
  1535. &module_inst->functions[module->malloc_function];
  1536. }
  1537. if (module->free_function != (uint32)-1) {
  1538. module_inst->free_function =
  1539. &module_inst->functions[module->free_function];
  1540. }
  1541. if (module->retain_function != (uint32)-1) {
  1542. module_inst->retain_function =
  1543. &module_inst->functions[module->retain_function];
  1544. }
  1545. if (module->start_function != (uint32)-1) {
  1546. /* TODO: fix start function can be import function issue */
  1547. if (module->start_function >= module->import_function_count)
  1548. module_inst->start_function =
  1549. &module_inst->functions[module->start_function];
  1550. }
  1551. /* Execute __wasm_call_ctors function */
  1552. // Note: global relocs has to be run by each thread.
  1553. if (!execute_post_inst_function(module_inst)
  1554. ||!execute_start_function(module_inst)) {
  1555. set_error_buf(program->error_buf, program->error_buf_size,
  1556. module_inst->cur_exception);
  1557. goto fail;
  1558. }
  1559. return module_inst;
  1560. fail:
  1561. wasm_deinstantiate(module_inst, true);
  1562. return NULL;
  1563. }
  1564. #endif
  1565. bool
  1566. init_globals(WASMModule * module, WASMModuleInstance * module_inst,
  1567. char *error_buf, uint32 error_buf_size)
  1568. {
  1569. uint32 global_count = 0, global_data_size = 0, i;
  1570. WASMGlobalInstance *globals = NULL, *global;
  1571. uint8 *global_data, *global_data_end;
  1572. /* Instantiate global firstly to get the mutable data size */
  1573. global_count = module->import_global_count + module->global_count;
  1574. if (global_count
  1575. && !(globals = globals_instantiate(module, module_inst,
  1576. &global_data_size,
  1577. error_buf, error_buf_size))) {
  1578. return false;
  1579. }
  1580. module_inst->global_count = global_count;
  1581. module_inst->globals = globals;
  1582. if (global_count > 0) {
  1583. if (!(module_inst->global_data = runtime_malloc
  1584. (global_data_size, error_buf, error_buf_size))) {
  1585. return false;
  1586. }
  1587. }
  1588. if (global_count > 0) {
  1589. /* Initialize the global data */
  1590. global_data = module_inst->global_data;
  1591. global_data_end = global_data + global_data_size;
  1592. global = globals;
  1593. for (i = 0; i < global_count; i++, global++) {
  1594. if (!global->data)
  1595. global->data = global_data;
  1596. switch (global->type) {
  1597. case VALUE_TYPE_I32:
  1598. case VALUE_TYPE_F32:
  1599. #if WASM_ENABLE_REF_TYPES != 0
  1600. case VALUE_TYPE_FUNCREF:
  1601. case VALUE_TYPE_EXTERNREF:
  1602. #endif
  1603. *(int32 *)global_data = global->initial_value.i32;
  1604. global_data += sizeof(int32);
  1605. break;
  1606. case VALUE_TYPE_I64:
  1607. case VALUE_TYPE_F64:
  1608. bh_memcpy_s(global_data,
  1609. (uint32)(global_data_end - global_data),
  1610. &global->initial_value.i64, sizeof(int64));
  1611. global_data += sizeof(int64);
  1612. break;
  1613. default:
  1614. bh_assert(0);
  1615. }
  1616. }
  1617. bh_assert(global_data == global_data_end);
  1618. }
  1619. return true;
  1620. }
  1621. /**
  1622. * Instantiate module
  1623. */
  1624. WASMModuleInstance*
  1625. wasm_instantiate(WASMProgramInstance * program,
  1626. WASMModule *module, bool is_sub_inst,
  1627. uint32 stack_size, uint32 heap_size,
  1628. char *error_buf, uint32 error_buf_size)
  1629. {
  1630. WASMModuleInstance *module_inst;
  1631. uint32 i;
  1632. uint32 base_offset, length;
  1633. #if WASM_ENABLE_MULTI_MODULE != 0
  1634. bool ret = false;
  1635. #endif
  1636. if (!module)
  1637. return NULL;
  1638. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1639. if (program) {
  1640. error_buf = program->error_buf;
  1641. error_buf_size = program->error_buf_size;
  1642. }
  1643. #else
  1644. if (module->dylink_section)
  1645. return NULL;
  1646. #endif
  1647. /* Check heap size */
  1648. heap_size = align_uint(heap_size, 8);
  1649. if (heap_size > APP_HEAP_SIZE_MAX)
  1650. heap_size = APP_HEAP_SIZE_MAX;
  1651. /* Allocate the memory */
  1652. if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance),
  1653. error_buf, error_buf_size))) {
  1654. return NULL;
  1655. }
  1656. module_inst->module = module;
  1657. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1658. module_inst->exp_ref_cnt = 0;
  1659. module_inst->imp_ref_cnt = 1; // be helpful to hold the instance in program lifetime.
  1660. module_inst->runtime = wasm_runtime_get_runtime();
  1661. module_inst->local_implicit_dependency_modules_name_hmap = NULL;
  1662. if (program) {
  1663. module_inst->program = program;
  1664. wasm_program_alloc_module_instance_id(program, (WASMModuleInstanceCommon*)module_inst);
  1665. }
  1666. module_inst->init_globals.memory_base = 1024;
  1667. module_inst->init_globals.actual_memory_base = 1024;
  1668. module_inst->init_globals.table_base = 1; // 0 is reserved for NULL
  1669. module_inst->init_globals.stack_pointer = 65536;
  1670. #endif
  1671. #if WASM_ENABLE_MULTI_MODULE != 0
  1672. module_inst->sub_module_inst_list =
  1673. &module_inst->sub_module_inst_list_head;
  1674. ret = sub_module_instantiate(module, module_inst, stack_size, heap_size,
  1675. error_buf, error_buf_size);
  1676. if (!ret) {
  1677. LOG_DEBUG("build a sub module list failed");
  1678. goto fail;
  1679. }
  1680. #endif
  1681. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1682. if (!(module_inst->frames = runtime_malloc((uint64)sizeof(Vector),
  1683. error_buf, error_buf_size))) {
  1684. goto fail;
  1685. }
  1686. #endif
  1687. module_inst->memory_count =
  1688. module->import_memory_count + module->memory_count;
  1689. module_inst->table_count =
  1690. module->import_table_count + module->table_count;
  1691. module_inst->function_count =
  1692. module->import_function_count + module->function_count;
  1693. /* export */
  1694. get_export_count(module, module_inst);
  1695. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1696. if (module->dylink_section) {
  1697. module_inst->init_globals.table_size = module->dylink_section->table_size +
  1698. module_inst->export_func_count;
  1699. }
  1700. #endif
  1701. if (!module->dylink_section) {
  1702. if (!init_globals(module, module_inst, error_buf, error_buf_size))
  1703. goto fail;
  1704. }
  1705. /* Instantiate memories/tables/functions */
  1706. if ((module_inst->memory_count > 0
  1707. && !(module_inst->memories =
  1708. memories_instantiate(module,
  1709. module_inst,
  1710. heap_size, error_buf, error_buf_size)))
  1711. || (module_inst->table_count > 0
  1712. && !(module_inst->tables =
  1713. tables_instantiate(module,
  1714. module_inst,
  1715. error_buf, error_buf_size)))
  1716. || (module_inst->function_count > 0
  1717. && !(module_inst->functions =
  1718. functions_instantiate(module,
  1719. module_inst,
  1720. error_buf, error_buf_size)))
  1721. || (module_inst->export_func_count > 0
  1722. && !(module_inst->export_functions = export_functions_instantiate(
  1723. module, module_inst, module_inst->export_func_count,
  1724. error_buf, error_buf_size)))
  1725. #if WASM_ENABLE_MULTI_MODULE != 0 || WASM_ENABLE_DYNAMIC_LINKING != 0
  1726. || (module_inst->export_glob_count > 0
  1727. && !(module_inst->export_globals = export_globals_instantiate(
  1728. module, module_inst, module_inst->export_glob_count,
  1729. error_buf, error_buf_size)))
  1730. #endif
  1731. ) {
  1732. goto fail;
  1733. }
  1734. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1735. if (module_inst->memory_count)
  1736. module_inst->init_globals.stack_pointer = module_inst->memories[0]->heap_data -
  1737. module_inst->memories[0]->memory_data;
  1738. #endif
  1739. if (module->dylink_section) {
  1740. if (!init_globals(module, module_inst, error_buf, error_buf_size))
  1741. goto fail;
  1742. }
  1743. if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
  1744. goto fail;
  1745. }
  1746. /* Initialize the memory data with data segment section */
  1747. module_inst->default_memory =
  1748. module_inst->memory_count ? module_inst->memories[0] : NULL;
  1749. for (i = 0; i < module->data_seg_count; i++) {
  1750. WASMMemoryInstance *memory = NULL;
  1751. uint8 *memory_data = NULL;
  1752. uint32 memory_size = 0;
  1753. WASMDataSeg *data_seg = &module->data_segments[i];
  1754. #if WASM_ENABLE_BULK_MEMORY != 0
  1755. if (data_seg->is_passive)
  1756. continue;
  1757. #endif
  1758. /* has check it in loader */
  1759. memory = module_inst->memories[data_seg->memory_index];
  1760. bh_assert(memory);
  1761. memory_data = memory->memory_data;
  1762. memory_size = memory->num_bytes_per_page * memory->cur_page_count;
  1763. bh_assert(memory_data || memory_size == 0);
  1764. bh_assert(data_seg->base_offset.init_expr_type
  1765. == INIT_EXPR_TYPE_I32_CONST
  1766. || data_seg->base_offset.init_expr_type
  1767. == INIT_EXPR_TYPE_GET_GLOBAL);
  1768. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  1769. if (!check_global_init_expr(module,
  1770. data_seg->base_offset.u.global_index,
  1771. error_buf, error_buf_size)) {
  1772. goto fail;
  1773. }
  1774. if (!module_inst->globals
  1775. || module_inst->globals[data_seg->base_offset.u.global_index].type
  1776. != VALUE_TYPE_I32) {
  1777. set_error_buf(error_buf, error_buf_size,
  1778. "data segment does not fit");
  1779. goto fail;
  1780. }
  1781. base_offset = module_inst->globals[data_seg->base_offset.u.global_index]
  1782. .initial_value.i32;
  1783. //data_seg->base_offset.u.i32 =
  1784. // module_inst->globals[data_seg->base_offset.u.global_index]
  1785. // .initial_value.i32;
  1786. } else {
  1787. base_offset = (uint32)data_seg->base_offset.u.i32;
  1788. }
  1789. /* check offset */
  1790. if (base_offset > memory_size) {
  1791. LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset,
  1792. memory_size);
  1793. #if WASM_ENABLE_REF_TYPES != 0
  1794. set_error_buf(error_buf, error_buf_size,
  1795. "out of bounds memory access");
  1796. #else
  1797. set_error_buf(error_buf, error_buf_size,
  1798. "data segment does not fit");
  1799. #endif
  1800. goto fail;
  1801. }
  1802. /* check offset + length(could be zero) */
  1803. length = data_seg->data_length;
  1804. if (base_offset + length > memory_size) {
  1805. LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)",
  1806. base_offset, length, memory_size);
  1807. #if WASM_ENABLE_REF_TYPES != 0
  1808. set_error_buf(error_buf, error_buf_size,
  1809. "out of bounds memory access");
  1810. #else
  1811. set_error_buf(error_buf, error_buf_size,
  1812. "data segment does not fit");
  1813. #endif
  1814. goto fail;
  1815. }
  1816. if (memory_data) {
  1817. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  1818. data_seg->data, length);
  1819. }
  1820. }
  1821. /* Initialize the table data with table segment section */
  1822. module_inst->default_table =
  1823. module_inst->table_count ? module_inst->tables[0] : NULL;
  1824. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1825. bh_assert(!module_inst->program ||
  1826. (module_inst->program && module_inst->inst_id == 1));
  1827. //wasm_program_alloc_table_space_by_table(module_inst, module_inst->default_table);
  1828. #endif
  1829. /* in case there is no table */
  1830. for (i = 0; module_inst->table_count > 0 && i < module->table_seg_count;
  1831. i++) {
  1832. WASMTableSeg *table_seg = module->table_segments + i;
  1833. /* has check it in loader */
  1834. WASMTableInstance *table = module_inst->tables[table_seg->table_index];
  1835. bh_assert(table);
  1836. #if WASM_ENABLE_REF_TYPES != 0
  1837. if (table->elem_type != VALUE_TYPE_FUNCREF
  1838. && table->elem_type != VALUE_TYPE_EXTERNREF) {
  1839. set_error_buf(error_buf, error_buf_size,
  1840. "elements segment does not fit");
  1841. goto fail;
  1842. }
  1843. #endif
  1844. uint32 *table_data = (uint32 *)table->base_addr;
  1845. #if WASM_ENABLE_MULTI_MODULE != 0
  1846. table_data = table->table_inst_linked
  1847. ? (uint32 *)table->table_inst_linked->base_addr
  1848. : table_data;
  1849. #endif
  1850. bh_assert(table_data);
  1851. #if WASM_ENABLE_REF_TYPES != 0
  1852. if (!wasm_elem_is_active(table_seg->mode))
  1853. continue;
  1854. #endif
  1855. /* init vec(funcidx) or vec(expr) */
  1856. bh_assert(
  1857. table_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_I32_CONST
  1858. || table_seg->base_offset.init_expr_type
  1859. == INIT_EXPR_TYPE_GET_GLOBAL
  1860. #if WASM_ENABLE_REF_TYPES != 0
  1861. || table_seg->base_offset.init_expr_type
  1862. == INIT_EXPR_TYPE_FUNCREF_CONST
  1863. || table_seg->base_offset.init_expr_type
  1864. == INIT_EXPR_TYPE_REFNULL_CONST
  1865. #endif
  1866. );
  1867. if (table_seg->base_offset.init_expr_type
  1868. == INIT_EXPR_TYPE_GET_GLOBAL) {
  1869. if (!check_global_init_expr(module,
  1870. table_seg->base_offset.u.global_index,
  1871. error_buf, error_buf_size)) {
  1872. goto fail;
  1873. }
  1874. if (!module_inst->globals
  1875. || module_inst->globals[table_seg->base_offset.u.global_index].type
  1876. != VALUE_TYPE_I32) {
  1877. set_error_buf(error_buf, error_buf_size,
  1878. "elements segment does not fit");
  1879. goto fail;
  1880. }
  1881. table_seg->base_offset.u.i32 =
  1882. module_inst->globals[table_seg->base_offset.u.global_index].initial_value.i32;
  1883. }
  1884. /* check offset since length might negative */
  1885. if ((uint32)table_seg->base_offset.u.i32 > table->cur_size) {
  1886. LOG_DEBUG("base_offset(%d) > table->cur_size(%d)",
  1887. table_seg->base_offset.u.i32, table->cur_size);
  1888. #if WASM_ENABLE_REF_TYPES != 0
  1889. set_error_buf(error_buf, error_buf_size,
  1890. "out of bounds table access");
  1891. #else
  1892. set_error_buf(error_buf, error_buf_size,
  1893. "elements segment does not fit");
  1894. #endif
  1895. goto fail;
  1896. }
  1897. /* check offset + length(could be zero) */
  1898. length = table_seg->function_count;
  1899. if ((uint32)table_seg->base_offset.u.i32 + length > table->cur_size) {
  1900. LOG_DEBUG("base_offset(%d) + length(%d)> table->cur_size(%d)",
  1901. table_seg->base_offset.u.i32, length, table->cur_size);
  1902. #if WASM_ENABLE_REF_TYPES != 0
  1903. set_error_buf(error_buf, error_buf_size,
  1904. "out of bounds table access");
  1905. #else
  1906. set_error_buf(error_buf, error_buf_size,
  1907. "elements segment does not fit");
  1908. #endif
  1909. goto fail;
  1910. }
  1911. /**
  1912. * Check function index in the current module inst for now.
  1913. * will check the linked table inst owner in future.
  1914. * so loader check is enough
  1915. */
  1916. bh_memcpy_s(
  1917. table_data + table_seg->base_offset.u.i32,
  1918. (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
  1919. * sizeof(uint32)),
  1920. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  1921. }
  1922. /* module instance type */
  1923. module_inst->module_type = Wasm_Module_Bytecode;
  1924. /* Initialize the thread related data */
  1925. if (stack_size == 0)
  1926. stack_size = DEFAULT_WASM_STACK_SIZE;
  1927. #if WASM_ENABLE_SPEC_TEST != 0
  1928. if (stack_size < 48 * 1024)
  1929. stack_size = 48 * 1024;
  1930. #endif
  1931. module_inst->default_wasm_stack_size = stack_size;
  1932. if (module->malloc_function != (uint32)-1) {
  1933. module_inst->malloc_function =
  1934. &module_inst->functions[module->malloc_function];
  1935. }
  1936. if (module->free_function != (uint32)-1) {
  1937. module_inst->free_function =
  1938. &module_inst->functions[module->free_function];
  1939. }
  1940. if (module->retain_function != (uint32)-1) {
  1941. module_inst->retain_function =
  1942. &module_inst->functions[module->retain_function];
  1943. }
  1944. #if WASM_ENABLE_LIBC_WASI != 0
  1945. /* The sub-instance will get the wasi_ctx from main-instance */
  1946. if (!is_sub_inst) {
  1947. if (!wasm_runtime_init_wasi(
  1948. (WASMModuleInstanceCommon *)module_inst,
  1949. module->wasi_args.dir_list, module->wasi_args.dir_count,
  1950. module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
  1951. module->wasi_args.env, module->wasi_args.env_count,
  1952. module->wasi_args.argv, module->wasi_args.argc,
  1953. module->wasi_args.stdio[0], module->wasi_args.stdio[1],
  1954. module->wasi_args.stdio[2], error_buf, error_buf_size)) {
  1955. goto fail;
  1956. }
  1957. }
  1958. #endif
  1959. if (module->start_function != (uint32)-1) {
  1960. /* TODO: fix start function can be import function issue */
  1961. if (module->start_function >= module->import_function_count)
  1962. module_inst->start_function =
  1963. &module_inst->functions[module->start_function];
  1964. }
  1965. /* Execute __post_instantiate function */
  1966. if (!execute_post_inst_function(module_inst)
  1967. || !execute_start_function(module_inst)) {
  1968. set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
  1969. goto fail;
  1970. }
  1971. #if WASM_ENABLE_BULK_MEMORY != 0
  1972. #if WASM_ENABLE_LIBC_WASI != 0
  1973. if (!module->is_wasi_module) {
  1974. #endif
  1975. /* Only execute the memory init function for main instance because
  1976. the data segments will be dropped once initialized.
  1977. */
  1978. if (!is_sub_inst) {
  1979. if (!execute_memory_init_function(module_inst)) {
  1980. set_error_buf(error_buf, error_buf_size,
  1981. module_inst->cur_exception);
  1982. goto fail;
  1983. }
  1984. }
  1985. #if WASM_ENABLE_LIBC_WASI != 0
  1986. }
  1987. #endif
  1988. #endif
  1989. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1990. if (program) {
  1991. if (!wasm_program_insert_module_inst_by_name(program,
  1992. (WASMModuleInstanceCommon*)module_inst,
  1993. module_inst->module->module_name))
  1994. goto fail;
  1995. }
  1996. #endif
  1997. #if WASM_ENABLE_MEMORY_TRACING != 0
  1998. wasm_runtime_dump_module_inst_mem_consumption(
  1999. (WASMModuleInstanceCommon *)module_inst);
  2000. #endif
  2001. return module_inst;
  2002. fail:
  2003. wasm_deinstantiate(module_inst, false);
  2004. return NULL;
  2005. }
  2006. void
  2007. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
  2008. {
  2009. if (!module_inst)
  2010. return;
  2011. //printf("module %s deinstantiate internal\n", (module_inst)->module->module_name->str);
  2012. #if WASM_ENABLE_MULTI_MODULE != 0
  2013. sub_module_deinstantiate(module_inst);
  2014. #endif
  2015. #if WASM_ENABLE_LIBC_WASI != 0
  2016. /* Destroy wasi resource before freeing app heap, since some fields of
  2017. wasi contex are allocated from app heap, and if app heap is freed,
  2018. these fields will be set to NULL, we cannot free their internal data
  2019. which may allocated from global heap. */
  2020. /* Only destroy wasi ctx in the main module instance */
  2021. if (!is_sub_inst)
  2022. wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst);
  2023. #endif
  2024. if (module_inst->memory_count > 0)
  2025. memories_deinstantiate(module_inst, module_inst->memories,
  2026. module_inst->memory_count);
  2027. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  2028. functions_deinstantiate(module_inst->functions,
  2029. module_inst->function_count);
  2030. globals_deinstantiate(module_inst->globals);
  2031. export_functions_deinstantiate(module_inst->export_functions);
  2032. #if WASM_ENABLE_MULTI_MODULE != 0 || WASM_ENABLE_DYNAMIC_LINKING != 0
  2033. export_globals_deinstantiate(module_inst->export_globals);
  2034. #endif
  2035. if (module_inst->global_data)
  2036. wasm_runtime_free(module_inst->global_data);
  2037. #if WASM_ENABLE_REF_TYPES != 0
  2038. wasm_externref_cleanup((WASMModuleInstanceCommon *)module_inst);
  2039. #endif
  2040. if (module_inst->exec_env_singleton)
  2041. wasm_exec_env_destroy(module_inst->exec_env_singleton);
  2042. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2043. if (module_inst->frames) {
  2044. bh_vector_destroy(module_inst->frames);
  2045. wasm_runtime_free(module_inst->frames);
  2046. module_inst->frames = NULL;
  2047. }
  2048. #endif
  2049. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2050. if (module_inst->local_implicit_dependency_modules_name_hmap) {
  2051. bh_hash_map_destroy(module_inst->local_implicit_dependency_modules_name_hmap);
  2052. }
  2053. #endif
  2054. wasm_runtime_free(module_inst);
  2055. }
  2056. WASMFunctionInstance *
  2057. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  2058. const char *signature)
  2059. {
  2060. uint32 i;
  2061. for (i = 0; i < module_inst->export_func_count; i++)
  2062. if (!strcmp(module_inst->export_functions[i].name, name))
  2063. return module_inst->export_functions[i].function;
  2064. (void)signature;
  2065. return NULL;
  2066. }
  2067. #if WASM_ENABLE_MULTI_MODULE != 0
  2068. WASMGlobalInstance *
  2069. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name)
  2070. {
  2071. uint32 i;
  2072. for (i = 0; i < module_inst->export_glob_count; i++)
  2073. if (!strcmp(module_inst->export_globals[i].name, name))
  2074. return module_inst->export_globals[i].global;
  2075. return NULL;
  2076. }
  2077. WASMMemoryInstance *
  2078. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name)
  2079. {
  2080. /**
  2081. * using a strong assumption that one module instance only has
  2082. * one memory instance
  2083. */
  2084. (void)module_inst->export_memories;
  2085. return module_inst->memories[0];
  2086. }
  2087. WASMTableInstance *
  2088. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
  2089. {
  2090. /**
  2091. * using a strong assumption that one module instance only has
  2092. * one table instance
  2093. */
  2094. (void)module_inst->export_tables;
  2095. return module_inst->tables[0];
  2096. }
  2097. #endif
  2098. static bool
  2099. clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
  2100. {
  2101. #if WASM_ENABLE_LIBC_WASI != 0
  2102. const char *exception = wasm_get_exception(module_inst);
  2103. if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
  2104. /* The "wasi proc exit" exception is thrown by native lib to
  2105. let wasm app exit, which is a normal behavior, we clear
  2106. the exception here. */
  2107. wasm_set_exception(module_inst, NULL);
  2108. return true;
  2109. }
  2110. return false;
  2111. #else
  2112. return false;
  2113. #endif
  2114. }
  2115. bool
  2116. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  2117. unsigned argc, uint32 argv[])
  2118. {
  2119. WASMModuleInstance *module_inst =
  2120. (WASMModuleInstance *)exec_env->module_inst;
  2121. /* set thread handle and stack boundary */
  2122. wasm_exec_env_set_thread_info(exec_env);
  2123. wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
  2124. (void)clear_wasi_proc_exit_exception(module_inst);
  2125. return !wasm_get_exception(module_inst) ? true : false;
  2126. }
  2127. bool
  2128. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  2129. WASMFunctionInstance *func,
  2130. unsigned argc, uint32 argv[])
  2131. {
  2132. WASMExecEnv *exec_env;
  2133. bool ret;
  2134. #if WASM_ENABLE_THREAD_MGR != 0
  2135. WASMExecEnv *existing_exec_env = NULL;
  2136. if (!(existing_exec_env = exec_env = wasm_clusters_search_exec_env(
  2137. (WASMModuleInstanceCommon *)module_inst))) {
  2138. #endif
  2139. if (!(exec_env =
  2140. wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
  2141. module_inst->default_wasm_stack_size))) {
  2142. wasm_set_exception(module_inst, "allocate memory failed");
  2143. return false;
  2144. }
  2145. #if WASM_ENABLE_THREAD_MGR != 0
  2146. }
  2147. #endif
  2148. #if WASM_ENABLE_REF_TYPES != 0
  2149. wasm_runtime_prepare_call_function(exec_env, func);
  2150. #endif
  2151. ret = wasm_call_function(exec_env, func, argc, argv);
  2152. #if WASM_ENABLE_REF_TYPES != 0
  2153. wasm_runtime_finalize_call_function(exec_env, func, ret, argv);
  2154. #endif
  2155. #if WASM_ENABLE_THREAD_MGR != 0
  2156. /* don't destroy the exec_env if it's searched from the cluster */
  2157. if (!existing_exec_env)
  2158. #endif
  2159. wasm_exec_env_destroy(exec_env);
  2160. return ret;
  2161. }
  2162. bool
  2163. wasm_create_exec_env_singleton(WASMModuleInstance *module_inst)
  2164. {
  2165. WASMExecEnv *exec_env =
  2166. wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
  2167. module_inst->default_wasm_stack_size);
  2168. if (exec_env)
  2169. module_inst->exec_env_singleton = exec_env;
  2170. return exec_env ? true : false;
  2171. }
  2172. void
  2173. wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
  2174. {
  2175. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2176. WASMProgramInstance * program = module_inst->program;
  2177. if (program)
  2178. program->exception_inst = (WASMModuleInstanceCommon*)module_inst;
  2179. #endif
  2180. if (exception)
  2181. snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
  2182. "Exception: %s", exception);
  2183. else
  2184. module_inst->cur_exception[0] = '\0';
  2185. }
  2186. const char *
  2187. wasm_get_exception(WASMModuleInstance *module_inst)
  2188. {
  2189. if (module_inst->cur_exception[0] == '\0')
  2190. return NULL;
  2191. else
  2192. return module_inst->cur_exception;
  2193. }
  2194. #if WASM_ENABLE_PERF_PROFILING != 0
  2195. void
  2196. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
  2197. {
  2198. WASMExportFuncInstance *export_func;
  2199. WASMFunctionInstance *func_inst;
  2200. const char *func_name;
  2201. uint32 i, j;
  2202. os_printf("Performance profiler data:\n");
  2203. for (i = 0; i < module_inst->function_count; i++) {
  2204. func_inst = module_inst->functions + i;
  2205. if (func_inst->is_import_func) {
  2206. func_name = func_inst->u.func_import->field_name->str;
  2207. }
  2208. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2209. else if (func_inst->u.func->field_name) {
  2210. func_name = func_inst->u.func->field_name;
  2211. }
  2212. #endif
  2213. else {
  2214. func_name = NULL;
  2215. for (j = 0; j < module_inst->export_func_count; j++) {
  2216. export_func = module_inst->export_functions + j;
  2217. if (export_func->function == func_inst) {
  2218. func_name = export_func->name;
  2219. break;
  2220. }
  2221. }
  2222. }
  2223. if (func_name)
  2224. os_printf(" func %s, execution time: %.3f ms, execution count: %d "
  2225. "times\n",
  2226. func_name,
  2227. module_inst->functions[i].total_exec_time / 1000.0f,
  2228. module_inst->functions[i].total_exec_cnt);
  2229. else
  2230. os_printf(" func %d, execution time: %.3f ms, execution count: %d "
  2231. "times\n",
  2232. i, module_inst->functions[i].total_exec_time / 1000.0f,
  2233. module_inst->functions[i].total_exec_cnt);
  2234. }
  2235. }
  2236. #endif
  2237. uint32
  2238. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  2239. void **p_native_addr)
  2240. {
  2241. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2242. WASMProgramInstance * program = module_inst->program;
  2243. if (program && program->config.import_memop_mode == FROM_ROOT)
  2244. module_inst = (WASMModuleInstance *)program->root_module_inst;
  2245. #endif
  2246. WASMMemoryInstance *memory = module_inst->default_memory;
  2247. uint8 *addr = NULL;
  2248. uint32 offset = 0;
  2249. if (!memory) {
  2250. wasm_set_exception(module_inst, "uninitialized memory");
  2251. return 0;
  2252. }
  2253. if (memory->heap_handle) {
  2254. addr = mem_allocator_malloc(memory->heap_handle, size);
  2255. }
  2256. else if (module_inst->malloc_function && module_inst->free_function) {
  2257. #if WASM_ENABLE_DEBUG_INTERP != 0
  2258. /* TODO: obviously, we can not create debug instance for
  2259. * module malloc here, so, just disable the engine here,
  2260. * it is strange, but we now are lack of ways to indicate
  2261. * which calls should not be debugged. And we have other
  2262. * execute_xxx_function may need to be taken care of
  2263. */
  2264. bool active = wasm_debug_get_engine_active();
  2265. wasm_debug_set_engine_active(false);
  2266. #endif
  2267. if (!execute_malloc_function(module_inst, module_inst->malloc_function,
  2268. module_inst->retain_function, size,
  2269. &offset)) {
  2270. #if WASM_ENABLE_DEBUG_INTERP != 0
  2271. wasm_debug_set_engine_active(active);
  2272. #endif
  2273. return 0;
  2274. }
  2275. #if WASM_ENABLE_DEBUG_INTERP != 0
  2276. wasm_debug_set_engine_active(active);
  2277. #endif
  2278. /* If we use app's malloc function,
  2279. the default memory may be changed while memory growing */
  2280. memory = module_inst->default_memory;
  2281. addr = offset ? memory->memory_data + offset : NULL;
  2282. }
  2283. if (!addr) {
  2284. if (memory->heap_handle
  2285. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2286. wasm_runtime_show_app_heap_corrupted_prompt();
  2287. wasm_set_exception(module_inst, "app heap corrupted");
  2288. }
  2289. else {
  2290. wasm_set_exception(module_inst, "out of memory");
  2291. }
  2292. return 0;
  2293. }
  2294. if (p_native_addr)
  2295. *p_native_addr = addr;
  2296. return (uint32)(addr - memory->memory_data);
  2297. }
  2298. uint32
  2299. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  2300. void **p_native_addr)
  2301. {
  2302. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2303. WASMProgramInstance * program = module_inst->program;
  2304. if (program && program->config.import_memop_mode == FROM_ROOT)
  2305. module_inst = (WASMModuleInstance *)program->root_module_inst;
  2306. #endif
  2307. WASMMemoryInstance *memory = module_inst->default_memory;
  2308. uint8 *addr = NULL;
  2309. if (!memory) {
  2310. wasm_set_exception(module_inst, "uninitialized memory");
  2311. return 0;
  2312. }
  2313. if (memory->heap_handle) {
  2314. addr = mem_allocator_realloc(memory->heap_handle,
  2315. memory->memory_data + ptr, size);
  2316. }
  2317. /* Only support realloc in WAMR's app heap */
  2318. if (!addr) {
  2319. if (memory->heap_handle
  2320. && mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2321. wasm_set_exception(module_inst, "app heap corrupted");
  2322. }
  2323. else {
  2324. wasm_set_exception(module_inst, "out of memory");
  2325. }
  2326. return 0;
  2327. }
  2328. if (p_native_addr)
  2329. *p_native_addr = addr;
  2330. return (uint32)(addr - memory->memory_data);
  2331. }
  2332. void
  2333. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
  2334. {
  2335. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2336. WASMProgramInstance * program = module_inst->program;
  2337. if (program && program->config.import_memop_mode == FROM_ROOT)
  2338. module_inst = (WASMModuleInstance *)program->root_module_inst;
  2339. #endif
  2340. if (ptr) {
  2341. WASMMemoryInstance *memory = module_inst->default_memory;
  2342. uint8 *addr;
  2343. if (!memory) {
  2344. return;
  2345. }
  2346. addr = memory->memory_data + ptr;
  2347. if (memory->heap_handle && memory->heap_data <= addr
  2348. && addr < memory->heap_data_end) {
  2349. mem_allocator_free(memory->heap_handle, addr);
  2350. }
  2351. else if (module_inst->malloc_function && module_inst->free_function
  2352. && memory->memory_data <= addr
  2353. && addr < memory->memory_data_end) {
  2354. #if WASM_ENABLE_DEBUG_INTERP != 0
  2355. /*TODO: obviously, we can not create debug instance for module
  2356. malloc here, so, just disable the engine here, it is strange. the
  2357. wasm's call should be marshed to its own thread */
  2358. bool active = wasm_debug_get_engine_active();
  2359. wasm_debug_set_engine_active(false);
  2360. #endif
  2361. execute_free_function(module_inst, module_inst->free_function, ptr);
  2362. #if WASM_ENABLE_DEBUG_INTERP != 0
  2363. wasm_debug_set_engine_active(active);
  2364. #endif
  2365. }
  2366. }
  2367. }
  2368. uint32
  2369. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  2370. uint32 size)
  2371. {
  2372. char *buffer;
  2373. uint32 buffer_offset =
  2374. wasm_module_malloc(module_inst, size, (void **)&buffer);
  2375. if (buffer_offset != 0) {
  2376. buffer = wasm_addr_app_to_native(module_inst, buffer_offset);
  2377. bh_memcpy_s(buffer, size, src, size);
  2378. }
  2379. return buffer_offset;
  2380. }
  2381. bool
  2382. wasm_validate_app_addr(WASMModuleInstance *module_inst, uint32 app_offset,
  2383. uint32 size)
  2384. {
  2385. WASMMemoryInstance *memory = module_inst->default_memory;
  2386. uint32 memory_data_size;
  2387. if (!memory) {
  2388. goto fail;
  2389. }
  2390. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  2391. /* integer overflow check */
  2392. if (app_offset > UINT32_MAX - size) {
  2393. goto fail;
  2394. }
  2395. if (app_offset + size <= memory_data_size) {
  2396. return true;
  2397. }
  2398. fail:
  2399. wasm_set_exception(module_inst, "out of bounds memory access");
  2400. return false;
  2401. }
  2402. bool
  2403. wasm_validate_native_addr(WASMModuleInstance *module_inst, void *native_ptr,
  2404. uint32 size)
  2405. {
  2406. WASMMemoryInstance *memory = module_inst->default_memory;
  2407. uint8 *addr = (uint8 *)native_ptr;
  2408. if (!memory) {
  2409. goto fail;
  2410. }
  2411. /* integer overflow check */
  2412. if ((uintptr_t)addr > UINTPTR_MAX - size) {
  2413. goto fail;
  2414. }
  2415. if (memory->memory_data <= addr && addr + size <= memory->memory_data_end) {
  2416. return true;
  2417. }
  2418. fail:
  2419. wasm_set_exception(module_inst, "out of bounds memory access");
  2420. return false;
  2421. }
  2422. void *
  2423. wasm_addr_app_to_native(WASMModuleInstance *module_inst, uint32 app_offset)
  2424. {
  2425. WASMMemoryInstance *memory = module_inst->default_memory;
  2426. uint8 *addr;
  2427. if (!memory)
  2428. return NULL;
  2429. addr = memory->memory_data + app_offset;
  2430. if (memory->memory_data <= addr && addr < memory->memory_data_end)
  2431. return addr;
  2432. return NULL;
  2433. }
  2434. uint32
  2435. wasm_addr_native_to_app(WASMModuleInstance *module_inst, void *native_ptr)
  2436. {
  2437. WASMMemoryInstance *memory = module_inst->default_memory;
  2438. uint8 *addr = (uint8 *)native_ptr;
  2439. if (!memory)
  2440. return 0;
  2441. if (memory->memory_data <= addr && addr < memory->memory_data_end)
  2442. return (uint32)(addr - memory->memory_data);
  2443. return 0;
  2444. }
  2445. bool
  2446. wasm_get_app_addr_range(WASMModuleInstance *module_inst, uint32 app_offset,
  2447. uint32 *p_app_start_offset, uint32 *p_app_end_offset)
  2448. {
  2449. WASMMemoryInstance *memory = module_inst->default_memory;
  2450. uint32 memory_data_size;
  2451. if (!memory)
  2452. return false;
  2453. memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
  2454. if (app_offset < memory_data_size) {
  2455. if (p_app_start_offset)
  2456. *p_app_start_offset = 0;
  2457. if (p_app_end_offset)
  2458. *p_app_end_offset = memory_data_size;
  2459. return true;
  2460. }
  2461. return false;
  2462. }
  2463. bool
  2464. wasm_get_native_addr_range(WASMModuleInstance *module_inst, uint8 *native_ptr,
  2465. uint8 **p_native_start_addr,
  2466. uint8 **p_native_end_addr)
  2467. {
  2468. WASMMemoryInstance *memory = module_inst->default_memory;
  2469. uint8 *addr = (uint8 *)native_ptr;
  2470. if (!memory)
  2471. return false;
  2472. if (memory->memory_data <= addr && addr < memory->memory_data_end) {
  2473. if (p_native_start_addr)
  2474. *p_native_start_addr = memory->memory_data;
  2475. if (p_native_end_addr)
  2476. *p_native_end_addr = memory->memory_data_end;
  2477. return true;
  2478. }
  2479. return false;
  2480. }
  2481. bool
  2482. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  2483. {
  2484. WASMMemoryInstance *memory = module->default_memory;
  2485. uint8 *new_memory_data, *memory_data, *heap_data_old;
  2486. uint32 heap_size, total_size_old, total_page_count;
  2487. uint64 total_size;
  2488. bool ret = true;
  2489. if (!memory)
  2490. return false;
  2491. memory_data = memory->memory_data;
  2492. heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
  2493. total_size_old = (uint32)(memory->memory_data_end - memory_data);
  2494. total_page_count = inc_page_count + memory->cur_page_count;
  2495. total_size = memory->num_bytes_per_page * (uint64)total_page_count;
  2496. heap_data_old = memory->heap_data;
  2497. if (inc_page_count <= 0)
  2498. /* No need to enlarge memory */
  2499. return true;
  2500. if (total_page_count < memory->cur_page_count /* integer overflow */
  2501. || total_page_count > memory->max_page_count) {
  2502. return false;
  2503. }
  2504. if (total_size >= UINT32_MAX) {
  2505. return false;
  2506. }
  2507. #if WASM_ENABLE_SHARED_MEMORY != 0
  2508. if (memory->is_shared) {
  2509. /* For shared memory, we have reserved the maximum spaces during
  2510. instantiate, only change the cur_page_count here */
  2511. memory->cur_page_count = total_page_count;
  2512. return true;
  2513. }
  2514. #endif
  2515. if (heap_size > 0) {
  2516. if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
  2517. wasm_runtime_show_app_heap_corrupted_prompt();
  2518. return false;
  2519. }
  2520. }
  2521. if (!(new_memory_data =
  2522. wasm_runtime_realloc(memory_data, (uint32)total_size))) {
  2523. if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
  2524. return false;
  2525. }
  2526. if (memory_data) {
  2527. bh_memcpy_s(new_memory_data, (uint32)total_size, memory_data,
  2528. total_size_old);
  2529. wasm_runtime_free(memory_data);
  2530. }
  2531. }
  2532. memset(new_memory_data + total_size_old, 0,
  2533. (uint32)total_size - total_size_old);
  2534. if (heap_size > 0) {
  2535. if (mem_allocator_migrate(memory->heap_handle,
  2536. (char *)heap_data_old
  2537. + (new_memory_data - memory_data),
  2538. heap_size)
  2539. != 0) {
  2540. /* Don't return here as memory->memory_data is obsolete and
  2541. must be updated to be correctly used later. */
  2542. ret = false;
  2543. }
  2544. }
  2545. memory->memory_data = new_memory_data;
  2546. memory->cur_page_count = total_page_count;
  2547. memory->heap_data = new_memory_data + (heap_data_old - memory_data);
  2548. memory->heap_data_end = memory->heap_data + heap_size;
  2549. memory->memory_data_end =
  2550. memory->memory_data + memory->num_bytes_per_page * total_page_count;
  2551. return ret;
  2552. }
  2553. //#if WASM_ENABLE_REF_TYPES != 0
  2554. bool
  2555. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  2556. uint32 inc_entries, uint32 init_val)
  2557. {
  2558. uint32 entry_count, *new_table_data_start, i;
  2559. WASMTableInstance *table_inst;
  2560. if (!inc_entries) {
  2561. return true;
  2562. }
  2563. bh_assert(table_idx < module_inst->table_count);
  2564. table_inst = wasm_get_table_inst(module_inst, table_idx);
  2565. if (!table_inst) {
  2566. return false;
  2567. }
  2568. if (inc_entries > UINT32_MAX - table_inst->cur_size) {
  2569. return false;
  2570. }
  2571. entry_count = table_inst->cur_size + inc_entries;
  2572. if (entry_count > table_inst->max_size) {
  2573. return false;
  2574. }
  2575. /* fill in */
  2576. new_table_data_start =
  2577. (uint32 *)((uint8 *)table_inst + offsetof(WASMTableInstance, base_addr))
  2578. + table_inst->cur_size;
  2579. for (i = 0; i < inc_entries; ++i) {
  2580. new_table_data_start[i] = init_val;
  2581. }
  2582. table_inst->cur_size = entry_count;
  2583. return true;
  2584. }
  2585. //#endif /* WASM_ENABLE_REF_TYPES != 0 */
  2586. bool
  2587. wasm_call_indirect(WASMExecEnv *exec_env, uint32_t tbl_idx,
  2588. uint32_t element_indices, uint32_t argc, uint32_t argv[])
  2589. {
  2590. WASMModuleInstance *module_inst = NULL;
  2591. WASMTableInstance *table_inst = NULL;
  2592. uint32_t function_indices = 0;
  2593. WASMFunctionInstance *function_inst = NULL;
  2594. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  2595. bh_assert(module_inst);
  2596. table_inst = module_inst->tables[tbl_idx];
  2597. if (!table_inst) {
  2598. wasm_set_exception(module_inst, "unknown table");
  2599. goto got_exception;
  2600. }
  2601. if (element_indices >= table_inst->cur_size) {
  2602. wasm_set_exception(module_inst, "undefined element");
  2603. goto got_exception;
  2604. }
  2605. /**
  2606. * please be aware that table_inst->base_addr may point
  2607. * to another module's table
  2608. **/
  2609. function_indices = ((uint32_t *)table_inst->base_addr)[element_indices];
  2610. if (function_indices == NULL_REF) {
  2611. wasm_set_exception(module_inst, "uninitialized element");
  2612. goto got_exception;
  2613. }
  2614. /**
  2615. * we insist to call functions owned by the module itself
  2616. **/
  2617. if (function_indices >= module_inst->function_count) {
  2618. wasm_set_exception(module_inst, "unknown function");
  2619. goto got_exception;
  2620. }
  2621. function_inst = module_inst->functions + function_indices;
  2622. wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
  2623. (void)clear_wasi_proc_exit_exception(module_inst);
  2624. return !wasm_get_exception(module_inst) ? true : false;
  2625. got_exception:
  2626. return false;
  2627. }
  2628. #if WASM_ENABLE_THREAD_MGR != 0
  2629. bool
  2630. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size)
  2631. {
  2632. WASMModuleInstance *module_inst =
  2633. (WASMModuleInstance *)exec_env->module_inst;
  2634. uint32 stack_top_idx = module_inst->module->aux_stack_top_global_index;
  2635. uint32 data_end = module_inst->module->aux_data_end;
  2636. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2637. bool is_stack_before_data = stack_bottom < data_end ? true : false;
  2638. /* Check the aux stack space, currently we don't allocate space in heap */
  2639. if ((is_stack_before_data && (size > start_offset))
  2640. || ((!is_stack_before_data) && (start_offset - data_end < size)))
  2641. return false;
  2642. if (stack_top_idx != (uint32)-1) {
  2643. /* The aux stack top is a wasm global,
  2644. set the initial value for the global */
  2645. uint8 *global_addr =
  2646. module_inst->globals[stack_top_idx].data;
  2647. // module_inst->global_data +
  2648. // module_inst->globals[stack_top_idx].data_offset;
  2649. *(int32*)global_addr = start_offset;
  2650. /* The aux stack boundary is a constant value,
  2651. set the value to exec_env */
  2652. exec_env->aux_stack_boundary.boundary = start_offset - size;
  2653. exec_env->aux_stack_bottom.bottom = start_offset;
  2654. return true;
  2655. }
  2656. return false;
  2657. }
  2658. bool
  2659. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size)
  2660. {
  2661. WASMModuleInstance *module_inst =
  2662. (WASMModuleInstance *)exec_env->module_inst;
  2663. /* The aux stack information is resolved in loader
  2664. and store in module */
  2665. uint32 stack_bottom = module_inst->module->aux_stack_bottom;
  2666. uint32 total_aux_stack_size = module_inst->module->aux_stack_size;
  2667. if (stack_bottom != 0 && total_aux_stack_size != 0) {
  2668. if (start_offset)
  2669. *start_offset = stack_bottom;
  2670. if (size)
  2671. *size = total_aux_stack_size;
  2672. return true;
  2673. }
  2674. return false;
  2675. }
  2676. #endif
  2677. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  2678. void
  2679. wasm_get_module_mem_consumption(const WASMModule *module,
  2680. WASMModuleMemConsumption *mem_conspn)
  2681. {
  2682. uint32 i, size;
  2683. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2684. mem_conspn->module_struct_size = sizeof(WASMModule);
  2685. mem_conspn->types_size = sizeof(WASMType *) * module->type_count;
  2686. for (i = 0; i < module->type_count; i++) {
  2687. WASMType *type = module->types[i];
  2688. size = offsetof(WASMType, types)
  2689. + sizeof(uint8) * (type->param_count + type->result_count);
  2690. mem_conspn->types_size += size;
  2691. }
  2692. mem_conspn->imports_size = sizeof(WASMImport) * module->import_count;
  2693. mem_conspn->functions_size =
  2694. sizeof(WASMFunction *) * module->function_count;
  2695. for (i = 0; i < module->function_count; i++) {
  2696. WASMFunction *func = module->functions[i];
  2697. WASMType *type = func->func_type;
  2698. size = sizeof(WASMFunction) + func->local_count
  2699. + sizeof(uint16) * (type->param_count + func->local_count);
  2700. #if WASM_ENABLE_FAST_INTERP != 0
  2701. size +=
  2702. func->code_compiled_size + sizeof(uint32) * func->const_cell_num;
  2703. #endif
  2704. mem_conspn->functions_size += size;
  2705. }
  2706. mem_conspn->tables_size = sizeof(WASMTable) * module->table_count;
  2707. mem_conspn->memories_size = sizeof(WASMMemory) * module->memory_count;
  2708. mem_conspn->globals_size = sizeof(WASMGlobal) * module->global_count;
  2709. mem_conspn->exports_size = sizeof(WASMExport) * module->export_count;
  2710. mem_conspn->table_segs_size =
  2711. sizeof(WASMTableSeg) * module->table_seg_count;
  2712. for (i = 0; i < module->table_seg_count; i++) {
  2713. WASMTableSeg *table_seg = &module->table_segments[i];
  2714. mem_conspn->tables_size += sizeof(uint32) * table_seg->function_count;
  2715. }
  2716. mem_conspn->data_segs_size = sizeof(WASMDataSeg *) * module->data_seg_count;
  2717. for (i = 0; i < module->data_seg_count; i++) {
  2718. mem_conspn->data_segs_size += sizeof(WASMDataSeg);
  2719. }
  2720. if (module->const_str_list) {
  2721. StringNode *node = module->const_str_list, *node_next;
  2722. while (node) {
  2723. node_next = node->next;
  2724. mem_conspn->const_strs_size +=
  2725. sizeof(StringNode) + strlen(node->str) + 1;
  2726. node = node_next;
  2727. }
  2728. }
  2729. mem_conspn->total_size += mem_conspn->module_struct_size;
  2730. mem_conspn->total_size += mem_conspn->types_size;
  2731. mem_conspn->total_size += mem_conspn->imports_size;
  2732. mem_conspn->total_size += mem_conspn->functions_size;
  2733. mem_conspn->total_size += mem_conspn->tables_size;
  2734. mem_conspn->total_size += mem_conspn->memories_size;
  2735. mem_conspn->total_size += mem_conspn->globals_size;
  2736. mem_conspn->total_size += mem_conspn->exports_size;
  2737. mem_conspn->total_size += mem_conspn->table_segs_size;
  2738. mem_conspn->total_size += mem_conspn->data_segs_size;
  2739. mem_conspn->total_size += mem_conspn->const_strs_size;
  2740. #if WASM_ENABLE_AOT != 0
  2741. mem_conspn->total_size += mem_conspn->aot_code_size;
  2742. #endif
  2743. }
  2744. void
  2745. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
  2746. WASMModuleInstMemConsumption *mem_conspn)
  2747. {
  2748. uint32 i, size;
  2749. memset(mem_conspn, 0, sizeof(*mem_conspn));
  2750. mem_conspn->module_inst_struct_size = sizeof(WASMModuleInstance);
  2751. mem_conspn->memories_size =
  2752. sizeof(WASMMemoryInstance *) * module_inst->memory_count;
  2753. for (i = 0; i < module_inst->memory_count; i++) {
  2754. WASMMemoryInstance *memory = module_inst->memories[i];
  2755. size = sizeof(WASMMemoryInstance)
  2756. + memory->num_bytes_per_page * memory->cur_page_count;
  2757. mem_conspn->memories_size += size;
  2758. mem_conspn->app_heap_size += memory->heap_data_end - memory->heap_data;
  2759. /* size of app heap structure */
  2760. mem_conspn->memories_size += mem_allocator_get_heap_struct_size();
  2761. }
  2762. mem_conspn->tables_size =
  2763. sizeof(WASMTableInstance *) * module_inst->table_count;
  2764. for (i = 0; i < module_inst->table_count; i++) {
  2765. WASMTableInstance *table = module_inst->tables[i];
  2766. #if WASM_ENABLE_MULTI_MODULE != 0
  2767. if (table->table_inst_linked) {
  2768. size = offsetof(WASMTableInstance, base_addr);
  2769. }
  2770. else
  2771. #endif
  2772. {
  2773. size = offsetof(WASMTableInstance, base_addr)
  2774. + sizeof(uint32) * table->cur_size;
  2775. }
  2776. mem_conspn->tables_size += size;
  2777. }
  2778. mem_conspn->functions_size =
  2779. sizeof(WASMFunctionInstance) * module_inst->function_count;
  2780. mem_conspn->globals_size =
  2781. sizeof(WASMGlobalInstance) * module_inst->global_count;
  2782. if (module_inst->global_count > 0) {
  2783. WASMGlobalInstance *global =
  2784. &module_inst->globals[module_inst->global_count - 1];
  2785. //mem_conspn->globals_size += global->data_offset
  2786. // + wasm_value_type_size(global->type);
  2787. mem_conspn->globals_size += global->data + wasm_value_type_size(global->type) - (uint8 *)module_inst->globals;
  2788. }
  2789. mem_conspn->exports_size =
  2790. sizeof(WASMExportFuncInstance) * module_inst->export_func_count;
  2791. mem_conspn->total_size += mem_conspn->module_inst_struct_size;
  2792. mem_conspn->total_size += mem_conspn->memories_size;
  2793. mem_conspn->total_size += mem_conspn->functions_size;
  2794. mem_conspn->total_size += mem_conspn->tables_size;
  2795. mem_conspn->total_size += mem_conspn->globals_size;
  2796. mem_conspn->total_size += mem_conspn->exports_size;
  2797. }
  2798. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) \
  2799. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  2800. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  2801. void
  2802. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
  2803. {
  2804. WASMModuleInstance *module_inst =
  2805. (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
  2806. WASMInterpFrame *first_frame,
  2807. *cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  2808. uint32 n = 0;
  2809. /* count frames includes a function */
  2810. first_frame = cur_frame;
  2811. while (cur_frame) {
  2812. if (cur_frame->function) {
  2813. n++;
  2814. }
  2815. cur_frame = cur_frame->prev_frame;
  2816. }
  2817. /* release previous stack frames and create new ones */
  2818. if (!bh_vector_destroy(module_inst->frames)
  2819. || !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame))) {
  2820. return;
  2821. }
  2822. cur_frame = first_frame;
  2823. n = 0;
  2824. os_printf("\n");
  2825. while (cur_frame) {
  2826. WASMCApiFrame frame = { 0 };
  2827. WASMFunctionInstance *func_inst = cur_frame->function;
  2828. const char *func_name = NULL;
  2829. const uint8 *func_code_base = NULL;
  2830. if (!func_inst) {
  2831. cur_frame = cur_frame->prev_frame;
  2832. continue;
  2833. }
  2834. /* place holder, will overwrite it in wasm_c_api */
  2835. frame.instance = module_inst;
  2836. frame.module_offset = 0;
  2837. frame.func_index = (uint32)(func_inst - module_inst->functions);
  2838. func_code_base = wasm_get_func_code(func_inst);
  2839. if (!cur_frame->ip || !func_code_base) {
  2840. frame.func_offset = 0;
  2841. }
  2842. else {
  2843. frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
  2844. }
  2845. /* look for the function name */
  2846. if (func_inst->is_import_func) {
  2847. func_name = func_inst->u.func_import->field_name->str;
  2848. }
  2849. else {
  2850. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2851. func_name = func_inst->u.func->field_name;
  2852. #endif
  2853. /* if custom name section is not generated,
  2854. search symbols from export table */
  2855. if (!func_name) {
  2856. uint32 i;
  2857. for (i = 0; i < module_inst->export_func_count; i++) {
  2858. WASMExportFuncInstance *export_func =
  2859. module_inst->export_functions + i;
  2860. if (export_func->function == func_inst) {
  2861. func_name = export_func->name;
  2862. break;
  2863. }
  2864. }
  2865. }
  2866. }
  2867. /* function name not exported, print number instead */
  2868. if (func_name == NULL) {
  2869. os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
  2870. }
  2871. else {
  2872. os_printf("#%02d %s \n", n, func_name);
  2873. }
  2874. /* keep print */
  2875. bh_vector_append(module_inst->frames, &frame);
  2876. cur_frame = cur_frame->prev_frame;
  2877. n++;
  2878. }
  2879. os_printf("\n");
  2880. }
  2881. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */