aot_loader.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_runtime.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "aot_reloc.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "../common/wasm_native.h"
  11. #include "../compilation/aot.h"
  12. #if WASM_ENABLE_JIT != 0
  13. #include "../compilation/aot_llvm.h"
  14. #include "../interpreter/wasm_loader.h"
  15. #endif
  16. #if WASM_ENABLE_DEBUG_AOT != 0
  17. #include "debug/elf_parser.h"
  18. #include "debug/jit_debug.h"
  19. #endif
  20. #define YMM_PLT_PREFIX "__ymm@"
  21. #define XMM_PLT_PREFIX "__xmm@"
  22. #define REAL_PLT_PREFIX "__real@"
  23. static void
  24. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  25. {
  26. if (error_buf != NULL) {
  27. snprintf(error_buf, error_buf_size, "AOT module load failed: %s",
  28. string);
  29. }
  30. }
  31. static void
  32. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  33. {
  34. va_list args;
  35. char buf[128];
  36. if (error_buf != NULL) {
  37. va_start(args, format);
  38. vsnprintf(buf, sizeof(buf), format, args);
  39. va_end(args);
  40. snprintf(error_buf, error_buf_size, "AOT module load failed: %s", buf);
  41. }
  42. }
  43. #define exchange_uint8(p_data) (void)0
  44. static void
  45. exchange_uint16(uint8 *p_data)
  46. {
  47. uint8 value = *p_data;
  48. *p_data = *(p_data + 1);
  49. *(p_data + 1) = value;
  50. }
  51. static void
  52. exchange_uint32(uint8 *p_data)
  53. {
  54. uint8 value = *p_data;
  55. *p_data = *(p_data + 3);
  56. *(p_data + 3) = value;
  57. value = *(p_data + 1);
  58. *(p_data + 1) = *(p_data + 2);
  59. *(p_data + 2) = value;
  60. }
  61. static void
  62. exchange_uint64(uint8 *pData)
  63. {
  64. uint32 value;
  65. value = *(uint32 *)pData;
  66. *(uint32 *)pData = *(uint32 *)(pData + 4);
  67. *(uint32 *)(pData + 4) = value;
  68. exchange_uint32(pData);
  69. exchange_uint32(pData + 4);
  70. }
  71. static union {
  72. int a;
  73. char b;
  74. } __ue = { .a = 1 };
  75. #define is_little_endian() (__ue.b == 1)
  76. static bool
  77. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  78. char *error_buf, uint32 error_buf_size)
  79. {
  80. if ((uintptr_t)buf + length < (uintptr_t)buf
  81. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  82. set_error_buf(error_buf, error_buf_size, "unexpect end");
  83. return false;
  84. }
  85. return true;
  86. }
  87. #define CHECK_BUF(buf, buf_end, length) \
  88. do { \
  89. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  90. goto fail; \
  91. } \
  92. } while (0)
  93. static uint8 *
  94. align_ptr(const uint8 *p, uint32 b)
  95. {
  96. uintptr_t v = (uintptr_t)p;
  97. uintptr_t m = b - 1;
  98. return (uint8 *)((v + m) & ~m);
  99. }
  100. static inline uint64
  101. GET_U64_FROM_ADDR(uint32 *addr)
  102. {
  103. union {
  104. uint64 val;
  105. uint32 parts[2];
  106. } u;
  107. u.parts[0] = addr[0];
  108. u.parts[1] = addr[1];
  109. return u.val;
  110. }
  111. #define TEMPLATE_READ(p, p_end, res, type) \
  112. do { \
  113. if (sizeof(type) != sizeof(uint64)) \
  114. p = (uint8 *)align_ptr(p, sizeof(type)); \
  115. else \
  116. /* align 4 bytes if type is uint64 */ \
  117. p = (uint8 *)align_ptr(p, sizeof(uint32)); \
  118. CHECK_BUF(p, p_end, sizeof(type)); \
  119. if (sizeof(type) != sizeof(uint64)) \
  120. res = *(type *)p; \
  121. else \
  122. res = (type)GET_U64_FROM_ADDR((uint32 *)p); \
  123. if (!is_little_endian()) \
  124. exchange_##type((uint8 *)&res); \
  125. p += sizeof(type); \
  126. } while (0)
  127. #define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8)
  128. #define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16)
  129. #define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32)
  130. #define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64)
  131. #define read_byte_array(p, p_end, addr, len) \
  132. do { \
  133. CHECK_BUF(p, p_end, len); \
  134. bh_memcpy_s(addr, len, p, len); \
  135. p += len; \
  136. } while (0)
  137. #define read_string(p, p_end, str) \
  138. do { \
  139. if (!(str = load_string((uint8 **)&p, p_end, module, \
  140. is_load_from_file_buf, error_buf, \
  141. error_buf_size))) \
  142. goto fail; \
  143. } while (0)
  144. /* Legal values for bin_type */
  145. #define BIN_TYPE_ELF32L 0 /* 32-bit little endian */
  146. #define BIN_TYPE_ELF32B 1 /* 32-bit big endian */
  147. #define BIN_TYPE_ELF64L 2 /* 64-bit little endian */
  148. #define BIN_TYPE_ELF64B 3 /* 64-bit big endian */
  149. #define BIN_TYPE_COFF32 4 /* 32-bit little endian */
  150. #define BIN_TYPE_COFF64 6 /* 64-bit little endian */
  151. /* Legal values for e_type (object file type). */
  152. #define E_TYPE_NONE 0 /* No file type */
  153. #define E_TYPE_REL 1 /* Relocatable file */
  154. #define E_TYPE_EXEC 2 /* Executable file */
  155. #define E_TYPE_DYN 3 /* Shared object file */
  156. #define E_TYPE_XIP 4 /* eXecute In Place file */
  157. /* Legal values for e_machine (architecture). */
  158. #define E_MACHINE_386 3 /* Intel 80386 */
  159. #define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */
  160. #define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
  161. #define E_MACHINE_ARM 40 /* ARM/Thumb */
  162. #define E_MACHINE_AARCH64 183 /* AArch64 */
  163. #define E_MACHINE_ARC 45 /* Argonaut RISC Core */
  164. #define E_MACHINE_IA_64 50 /* Intel Merced */
  165. #define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
  166. #define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */
  167. #define E_MACHINE_ARC_COMPACT 93 /* ARC International ARCompact */
  168. #define E_MACHINE_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */
  169. #define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
  170. #define E_MACHINE_RISCV 243 /* RISC-V 32/64 */
  171. #define E_MACHINE_WIN_I386 0x14c /* Windows i386 architecture */
  172. #define E_MACHINE_WIN_X86_64 0x8664 /* Windows x86-64 architecture */
  173. /* Legal values for e_version */
  174. #define E_VERSION_CURRENT 1 /* Current version */
  175. static void *
  176. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  177. {
  178. void *mem;
  179. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  180. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  181. return NULL;
  182. }
  183. memset(mem, 0, (uint32)size);
  184. return mem;
  185. }
  186. static char *
  187. const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
  188. char *error_buf, uint32 error_buf_size)
  189. {
  190. HashMap *set = module->const_str_set;
  191. char *c_str, *value;
  192. /* Create const string set if it isn't created */
  193. if (!set
  194. && !(set = module->const_str_set = bh_hash_map_create(
  195. 32, false, (HashFunc)wasm_string_hash,
  196. (KeyEqualFunc)wasm_string_equal, NULL, wasm_runtime_free))) {
  197. set_error_buf(error_buf, error_buf_size,
  198. "create const string set failed");
  199. return NULL;
  200. }
  201. /* Lookup const string set, use the string if found */
  202. if (!(c_str = loader_malloc((uint32)len + 1, error_buf, error_buf_size))) {
  203. return NULL;
  204. }
  205. bh_memcpy_s(c_str, (uint32)(len + 1), str, (uint32)len);
  206. c_str[len] = '\0';
  207. if ((value = bh_hash_map_find(set, c_str))) {
  208. wasm_runtime_free(c_str);
  209. return value;
  210. }
  211. if (!bh_hash_map_insert(set, c_str, c_str)) {
  212. set_error_buf(error_buf, error_buf_size,
  213. "insert string to hash map failed");
  214. wasm_runtime_free(c_str);
  215. return NULL;
  216. }
  217. return c_str;
  218. }
  219. static char *
  220. load_string(uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  221. bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size)
  222. {
  223. uint8 *p = *p_buf;
  224. const uint8 *p_end = buf_end;
  225. char *str;
  226. uint16 str_len;
  227. read_uint16(p, p_end, str_len);
  228. CHECK_BUF(p, p_end, str_len);
  229. if (str_len == 0) {
  230. str = "";
  231. }
  232. else if (p[str_len - 1] == '\0') {
  233. /* The string is terminated with '\0', use it directly */
  234. str = (char *)p;
  235. }
  236. else if (is_load_from_file_buf) {
  237. /* As the file buffer can be referred to after loading,
  238. we use the 2 bytes of size to adjust the string:
  239. move string 2 byte backward and then append '\0' */
  240. str = (char *)(p - 2);
  241. bh_memmove_s(str, (uint32)(str_len + 1), p, (uint32)str_len);
  242. str[str_len] = '\0';
  243. }
  244. else {
  245. /* Load from sections, the file buffer cannot be reffered to
  246. after loading, we must create another string and insert it
  247. into const string set */
  248. if (!(str = const_str_set_insert((uint8 *)p, str_len, module, error_buf,
  249. error_buf_size))) {
  250. goto fail;
  251. }
  252. }
  253. p += str_len;
  254. *p_buf = p;
  255. return str;
  256. fail:
  257. return NULL;
  258. }
  259. static bool
  260. get_aot_file_target(AOTTargetInfo *target_info, char *target_buf,
  261. uint32 target_buf_size, char *error_buf,
  262. uint32 error_buf_size)
  263. {
  264. char *machine_type = NULL;
  265. switch (target_info->e_machine) {
  266. case E_MACHINE_X86_64:
  267. case E_MACHINE_WIN_X86_64:
  268. machine_type = "x86_64";
  269. break;
  270. case E_MACHINE_386:
  271. case E_MACHINE_WIN_I386:
  272. machine_type = "i386";
  273. break;
  274. case E_MACHINE_ARM:
  275. case E_MACHINE_AARCH64:
  276. machine_type = target_info->arch;
  277. break;
  278. case E_MACHINE_MIPS:
  279. machine_type = "mips";
  280. break;
  281. case E_MACHINE_XTENSA:
  282. machine_type = "xtensa";
  283. break;
  284. case E_MACHINE_RISCV:
  285. machine_type = "riscv";
  286. break;
  287. case E_MACHINE_ARC_COMPACT:
  288. case E_MACHINE_ARC_COMPACT2:
  289. machine_type = "arc";
  290. break;
  291. default:
  292. set_error_buf_v(error_buf, error_buf_size,
  293. "unknown machine type %d", target_info->e_machine);
  294. return false;
  295. }
  296. if (strncmp(target_info->arch, machine_type, strlen(machine_type))) {
  297. set_error_buf_v(
  298. error_buf, error_buf_size,
  299. "machine type (%s) isn't consistent with target type (%s)",
  300. machine_type, target_info->arch);
  301. return false;
  302. }
  303. snprintf(target_buf, target_buf_size, "%s", target_info->arch);
  304. return true;
  305. }
  306. static bool
  307. check_machine_info(AOTTargetInfo *target_info, char *error_buf,
  308. uint32 error_buf_size)
  309. {
  310. char target_expected[32], target_got[32];
  311. get_current_target(target_expected, sizeof(target_expected));
  312. if (!get_aot_file_target(target_info, target_got, sizeof(target_got),
  313. error_buf, error_buf_size))
  314. return false;
  315. if (strncmp(target_expected, target_got, strlen(target_expected))) {
  316. set_error_buf_v(error_buf, error_buf_size,
  317. "invalid target type, expected %s but got %s",
  318. target_expected, target_got);
  319. return false;
  320. }
  321. return true;
  322. }
  323. static bool
  324. load_target_info_section(const uint8 *buf, const uint8 *buf_end,
  325. AOTModule *module, char *error_buf,
  326. uint32 error_buf_size)
  327. {
  328. AOTTargetInfo target_info;
  329. const uint8 *p = buf, *p_end = buf_end;
  330. bool is_target_little_endian, is_target_64_bit;
  331. read_uint16(p, p_end, target_info.bin_type);
  332. read_uint16(p, p_end, target_info.abi_type);
  333. read_uint16(p, p_end, target_info.e_type);
  334. read_uint16(p, p_end, target_info.e_machine);
  335. read_uint32(p, p_end, target_info.e_version);
  336. read_uint32(p, p_end, target_info.e_flags);
  337. read_uint32(p, p_end, target_info.reserved);
  338. read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch));
  339. if (p != buf_end) {
  340. set_error_buf(error_buf, error_buf_size, "invalid section size");
  341. return false;
  342. }
  343. /* Check target endian type */
  344. is_target_little_endian = target_info.bin_type & 1 ? false : true;
  345. if (is_little_endian() != is_target_little_endian) {
  346. set_error_buf_v(error_buf, error_buf_size,
  347. "invalid target endian type, expected %s but got %s",
  348. is_little_endian() ? "little endian" : "big endian",
  349. is_target_little_endian ? "little endian"
  350. : "big endian");
  351. return false;
  352. }
  353. /* Check target bit width */
  354. is_target_64_bit = target_info.bin_type & 2 ? true : false;
  355. if ((sizeof(void *) == 8 ? true : false) != is_target_64_bit) {
  356. set_error_buf_v(error_buf, error_buf_size,
  357. "invalid target bit width, expected %s but got %s",
  358. sizeof(void *) == 8 ? "64-bit" : "32-bit",
  359. is_target_64_bit ? "64-bit" : "32-bit");
  360. return false;
  361. }
  362. /* Check target elf file type */
  363. if (target_info.e_type != E_TYPE_REL && target_info.e_type != E_TYPE_XIP) {
  364. set_error_buf(error_buf, error_buf_size,
  365. "invalid object file type, "
  366. "expected relocatable or XIP file type but got others");
  367. return false;
  368. }
  369. /* Check machine info */
  370. if (!check_machine_info(&target_info, error_buf, error_buf_size)) {
  371. return false;
  372. }
  373. if (target_info.e_version != E_VERSION_CURRENT) {
  374. set_error_buf(error_buf, error_buf_size, "invalid elf file version");
  375. return false;
  376. }
  377. return true;
  378. fail:
  379. return false;
  380. }
  381. static void *
  382. get_native_symbol_by_name(const char *name)
  383. {
  384. void *func = NULL;
  385. uint32 symnum = 0;
  386. SymbolMap *sym = NULL;
  387. sym = get_target_symbol_map(&symnum);
  388. while (symnum--) {
  389. if (strcmp(sym->symbol_name, name) == 0) {
  390. func = sym->symbol_addr;
  391. break;
  392. }
  393. sym++;
  394. }
  395. return func;
  396. }
  397. static bool
  398. str2uint32(const char *buf, uint32 *p_res);
  399. static bool
  400. str2uint64(const char *buf, uint64 *p_res);
  401. static bool
  402. load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
  403. AOTModule *module, bool is_load_from_file_buf,
  404. char *error_buf, uint32 error_buf_size)
  405. {
  406. const uint8 *p = buf, *p_end = buf_end;
  407. uint32 cnt;
  408. int32 i;
  409. const char *symbol;
  410. read_uint32(p, p_end, cnt);
  411. if (cnt > 0) {
  412. module->native_symbol_list = wasm_runtime_malloc(cnt * sizeof(void *));
  413. if (module->native_symbol_list == NULL) {
  414. set_error_buf(error_buf, error_buf_size,
  415. "malloc native symbol list failed");
  416. goto fail;
  417. }
  418. for (i = cnt - 1; i >= 0; i--) {
  419. read_string(p, p_end, symbol);
  420. if (!strncmp(symbol, "f32#", 4) || !strncmp(symbol, "i32#", 4)) {
  421. uint32 u32;
  422. /* Resolve the raw int bits of f32 const */
  423. if (!str2uint32(symbol + 4, &u32)) {
  424. set_error_buf_v(error_buf, error_buf_size,
  425. "resolve symbol %s failed", symbol);
  426. goto fail;
  427. }
  428. *(uint32 *)(&module->native_symbol_list[i]) = u32;
  429. }
  430. else if (!strncmp(symbol, "f64#", 4)
  431. || !strncmp(symbol, "i64#", 4)) {
  432. uint64 u64;
  433. /* Resolve the raw int bits of f64 const */
  434. if (!str2uint64(symbol + 4, &u64)) {
  435. set_error_buf_v(error_buf, error_buf_size,
  436. "resolve symbol %s failed", symbol);
  437. goto fail;
  438. }
  439. *(uint64 *)(&module->native_symbol_list[i]) = u64;
  440. }
  441. else if (!strncmp(symbol, "__ignore", 8)) {
  442. /* Padding bytes to make f64 on 8-byte aligned address,
  443. or it is the second 32-bit slot in 32-bit system */
  444. continue;
  445. }
  446. else {
  447. module->native_symbol_list[i] =
  448. get_native_symbol_by_name(symbol);
  449. if (module->native_symbol_list[i] == NULL) {
  450. set_error_buf_v(error_buf, error_buf_size,
  451. "missing native symbol: %s", symbol);
  452. goto fail;
  453. }
  454. }
  455. }
  456. }
  457. return true;
  458. fail:
  459. return false;
  460. }
  461. static bool
  462. load_name_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  463. bool is_load_from_file_buf, char *error_buf,
  464. uint32 error_buf_size)
  465. {
  466. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  467. const uint8 *p = buf, *p_end = buf_end;
  468. uint32 *aux_func_indexes;
  469. const char **aux_func_names;
  470. uint32 name_type, subsection_size;
  471. uint32 previous_name_type = 0;
  472. uint32 num_func_name;
  473. uint32 func_index;
  474. uint32 previous_func_index = ~0U;
  475. uint32 name_index;
  476. int i = 0;
  477. uint32 name_len;
  478. uint64 size;
  479. if (p >= p_end) {
  480. set_error_buf(error_buf, error_buf_size, "unexpected end");
  481. return false;
  482. }
  483. read_uint32(p, p_end, name_len);
  484. if (name_len != 4 || p + name_len > p_end) {
  485. set_error_buf(error_buf, error_buf_size, "unexpected end");
  486. return false;
  487. }
  488. if (memcmp(p, "name", 4) != 0) {
  489. set_error_buf(error_buf, error_buf_size, "invalid custom name section");
  490. return false;
  491. }
  492. p += name_len;
  493. while (p < p_end) {
  494. read_uint32(p, p_end, name_type);
  495. if (i != 0) {
  496. if (name_type == previous_name_type) {
  497. set_error_buf(error_buf, error_buf_size,
  498. "duplicate sub-section");
  499. return false;
  500. }
  501. if (name_type < previous_name_type) {
  502. set_error_buf(error_buf, error_buf_size,
  503. "out-of-order sub-section");
  504. return false;
  505. }
  506. }
  507. previous_name_type = name_type;
  508. read_uint32(p, p_end, subsection_size);
  509. CHECK_BUF(p, p_end, subsection_size);
  510. switch (name_type) {
  511. case SUB_SECTION_TYPE_FUNC:
  512. if (subsection_size) {
  513. read_uint32(p, p_end, num_func_name);
  514. if (num_func_name
  515. > module->import_func_count + module->func_count) {
  516. set_error_buf(error_buf, error_buf_size,
  517. "function name count out of bounds");
  518. return false;
  519. }
  520. module->aux_func_name_count = num_func_name;
  521. /* Allocate memory */
  522. size = sizeof(uint32) * (uint64)module->aux_func_name_count;
  523. if (!(aux_func_indexes = module->aux_func_indexes =
  524. loader_malloc(size, error_buf, error_buf_size))) {
  525. return false;
  526. }
  527. size =
  528. sizeof(char **) * (uint64)module->aux_func_name_count;
  529. if (!(aux_func_names = module->aux_func_names =
  530. loader_malloc(size, error_buf, error_buf_size))) {
  531. return false;
  532. }
  533. for (name_index = 0; name_index < num_func_name;
  534. name_index++) {
  535. read_uint32(p, p_end, func_index);
  536. if (name_index != 0
  537. && func_index == previous_func_index) {
  538. set_error_buf(error_buf, error_buf_size,
  539. "duplicate function name");
  540. return false;
  541. }
  542. if (name_index != 0
  543. && func_index < previous_func_index) {
  544. set_error_buf(error_buf, error_buf_size,
  545. "out-of-order function index ");
  546. return false;
  547. }
  548. if (func_index
  549. >= module->import_func_count + module->func_count) {
  550. set_error_buf(error_buf, error_buf_size,
  551. "function index out of bounds");
  552. return false;
  553. }
  554. previous_func_index = func_index;
  555. *(aux_func_indexes + name_index) = func_index;
  556. read_string(p, p_end, *(aux_func_names + name_index));
  557. #if 0
  558. LOG_DEBUG("func_index %u -> aux_func_name = %s\n",
  559. func_index, *(aux_func_names + name_index));
  560. #endif
  561. }
  562. }
  563. break;
  564. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  565. */
  566. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  567. default:
  568. p = p + subsection_size;
  569. break;
  570. }
  571. i++;
  572. }
  573. return true;
  574. fail:
  575. return false;
  576. #else
  577. return true;
  578. #endif /* WASM_ENABLE_CUSTOM_NAME_SECTION != 0 */
  579. }
  580. static bool
  581. load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  582. bool is_load_from_file_buf, char *error_buf,
  583. uint32 error_buf_size)
  584. {
  585. const uint8 *p = buf, *p_end = buf_end;
  586. uint32 sub_section_type;
  587. read_uint32(p, p_end, sub_section_type);
  588. buf = p;
  589. switch (sub_section_type) {
  590. case AOT_CUSTOM_SECTION_NATIVE_SYMBOL:
  591. if (!load_native_symbol_section(buf, buf_end, module,
  592. is_load_from_file_buf, error_buf,
  593. error_buf_size))
  594. goto fail;
  595. break;
  596. case AOT_CUSTOM_SECTION_NAME:
  597. if (!load_name_section(buf, buf_end, module, is_load_from_file_buf,
  598. error_buf, error_buf_size))
  599. goto fail;
  600. break;
  601. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  602. case AOT_CUSTOM_SECTION_RAW:
  603. {
  604. const char *section_name;
  605. WASMCustomSection *section;
  606. if (p >= p_end) {
  607. set_error_buf(error_buf, error_buf_size, "unexpected end");
  608. goto fail;
  609. }
  610. read_string(p, p_end, section_name);
  611. section = loader_malloc(sizeof(WASMCustomSection), error_buf,
  612. error_buf_size);
  613. if (!section) {
  614. goto fail;
  615. }
  616. section->name_addr = (char *)section_name;
  617. section->name_len = strlen(section_name);
  618. section->content_addr = (uint8 *)p;
  619. section->content_len = p_end - p;
  620. section->next = module->custom_section_list;
  621. module->custom_section_list = section;
  622. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  623. break;
  624. }
  625. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
  626. default:
  627. break;
  628. }
  629. return true;
  630. fail:
  631. return false;
  632. }
  633. static void
  634. destroy_import_memories(AOTImportMemory *import_memories, bool is_jit_mode)
  635. {
  636. if (!is_jit_mode)
  637. wasm_runtime_free(import_memories);
  638. }
  639. static void
  640. destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count,
  641. bool is_jit_mode)
  642. {
  643. if (!is_jit_mode) {
  644. uint32 i;
  645. for (i = 0; i < count; i++)
  646. if (data_list[i])
  647. wasm_runtime_free(data_list[i]);
  648. wasm_runtime_free(data_list);
  649. }
  650. }
  651. static bool
  652. load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  653. AOTModule *module, char *error_buf,
  654. uint32 error_buf_size)
  655. {
  656. const uint8 *buf = *p_buf;
  657. AOTMemInitData **data_list;
  658. uint64 size;
  659. uint32 i;
  660. /* Allocate memory */
  661. size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
  662. if (!(module->mem_init_data_list = data_list =
  663. loader_malloc(size, error_buf, error_buf_size))) {
  664. return false;
  665. }
  666. /* Create each memory data segment */
  667. for (i = 0; i < module->mem_init_data_count; i++) {
  668. uint32 init_expr_type, byte_count;
  669. uint64 init_expr_value;
  670. uint32 is_passive;
  671. uint32 memory_index;
  672. read_uint32(buf, buf_end, is_passive);
  673. read_uint32(buf, buf_end, memory_index);
  674. read_uint32(buf, buf_end, init_expr_type);
  675. read_uint64(buf, buf_end, init_expr_value);
  676. read_uint32(buf, buf_end, byte_count);
  677. size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count;
  678. if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
  679. return false;
  680. }
  681. #if WASM_ENABLE_BULK_MEMORY != 0
  682. /* is_passive and memory_index is only used in bulk memory mode */
  683. data_list[i]->is_passive = (bool)is_passive;
  684. data_list[i]->memory_index = memory_index;
  685. #endif
  686. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  687. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  688. data_list[i]->byte_count = byte_count;
  689. read_byte_array(buf, buf_end, data_list[i]->bytes,
  690. data_list[i]->byte_count);
  691. }
  692. *p_buf = buf;
  693. return true;
  694. fail:
  695. return false;
  696. }
  697. static bool
  698. load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  699. char *error_buf, uint32 error_buf_size)
  700. {
  701. uint32 i;
  702. uint64 total_size;
  703. const uint8 *buf = *p_buf;
  704. read_uint32(buf, buf_end, module->import_memory_count);
  705. /* We don't support import_memory_count > 0 currently */
  706. bh_assert(module->import_memory_count == 0);
  707. read_uint32(buf, buf_end, module->memory_count);
  708. total_size = sizeof(AOTMemory) * (uint64)module->memory_count;
  709. if (!(module->memories =
  710. loader_malloc(total_size, error_buf, error_buf_size))) {
  711. return false;
  712. }
  713. for (i = 0; i < module->memory_count; i++) {
  714. read_uint32(buf, buf_end, module->memories[i].memory_flags);
  715. read_uint32(buf, buf_end, module->memories[i].num_bytes_per_page);
  716. read_uint32(buf, buf_end, module->memories[i].mem_init_page_count);
  717. read_uint32(buf, buf_end, module->memories[i].mem_max_page_count);
  718. }
  719. read_uint32(buf, buf_end, module->mem_init_data_count);
  720. /* load memory init data list */
  721. if (module->mem_init_data_count > 0
  722. && !load_mem_init_data_list(&buf, buf_end, module, error_buf,
  723. error_buf_size))
  724. return false;
  725. *p_buf = buf;
  726. return true;
  727. fail:
  728. return false;
  729. }
  730. static void
  731. destroy_import_tables(AOTImportTable *import_tables, bool is_jit_mode)
  732. {
  733. if (!is_jit_mode)
  734. wasm_runtime_free(import_tables);
  735. }
  736. static void
  737. destroy_tables(AOTTable *tables, bool is_jit_mode)
  738. {
  739. if (!is_jit_mode)
  740. wasm_runtime_free(tables);
  741. }
  742. static void
  743. destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count,
  744. bool is_jit_mode)
  745. {
  746. if (!is_jit_mode) {
  747. uint32 i;
  748. for (i = 0; i < count; i++)
  749. if (data_list[i])
  750. wasm_runtime_free(data_list[i]);
  751. wasm_runtime_free(data_list);
  752. }
  753. }
  754. static bool
  755. load_import_table_list(const uint8 **p_buf, const uint8 *buf_end,
  756. AOTModule *module, char *error_buf,
  757. uint32 error_buf_size)
  758. {
  759. const uint8 *buf = *p_buf;
  760. AOTImportTable *import_table;
  761. uint64 size;
  762. uint32 i, possible_grow;
  763. /* Allocate memory */
  764. size = sizeof(AOTImportTable) * (uint64)module->import_table_count;
  765. if (!(module->import_tables = import_table =
  766. loader_malloc(size, error_buf, error_buf_size))) {
  767. return false;
  768. }
  769. /* keep sync with aot_emit_table_info() aot_emit_aot_file */
  770. for (i = 0; i < module->import_table_count; i++, import_table++) {
  771. read_uint32(buf, buf_end, import_table->elem_type);
  772. read_uint32(buf, buf_end, import_table->table_init_size);
  773. read_uint32(buf, buf_end, import_table->table_max_size);
  774. read_uint32(buf, buf_end, possible_grow);
  775. import_table->possible_grow = (possible_grow & 0x1);
  776. }
  777. *p_buf = buf;
  778. return true;
  779. fail:
  780. return false;
  781. }
  782. static bool
  783. load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  784. char *error_buf, uint32 error_buf_size)
  785. {
  786. const uint8 *buf = *p_buf;
  787. AOTTable *table;
  788. uint64 size;
  789. uint32 i, possible_grow;
  790. /* Allocate memory */
  791. size = sizeof(AOTTable) * (uint64)module->table_count;
  792. if (!(module->tables = table =
  793. loader_malloc(size, error_buf, error_buf_size))) {
  794. return false;
  795. }
  796. /* Create each table data segment */
  797. for (i = 0; i < module->table_count; i++, table++) {
  798. read_uint32(buf, buf_end, table->elem_type);
  799. read_uint32(buf, buf_end, table->table_flags);
  800. read_uint32(buf, buf_end, table->table_init_size);
  801. read_uint32(buf, buf_end, table->table_max_size);
  802. read_uint32(buf, buf_end, possible_grow);
  803. table->possible_grow = (possible_grow & 0x1);
  804. }
  805. *p_buf = buf;
  806. return true;
  807. fail:
  808. return false;
  809. }
  810. static bool
  811. load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
  812. AOTModule *module, char *error_buf,
  813. uint32 error_buf_size)
  814. {
  815. const uint8 *buf = *p_buf;
  816. AOTTableInitData **data_list;
  817. uint64 size;
  818. uint32 i;
  819. /* Allocate memory */
  820. size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count;
  821. if (!(module->table_init_data_list = data_list =
  822. loader_malloc(size, error_buf, error_buf_size))) {
  823. return false;
  824. }
  825. /* Create each table data segment */
  826. for (i = 0; i < module->table_init_data_count; i++) {
  827. uint32 mode, elem_type;
  828. uint32 table_index, init_expr_type, func_index_count;
  829. uint64 init_expr_value, size1;
  830. read_uint32(buf, buf_end, mode);
  831. read_uint32(buf, buf_end, elem_type);
  832. read_uint32(buf, buf_end, table_index);
  833. read_uint32(buf, buf_end, init_expr_type);
  834. read_uint64(buf, buf_end, init_expr_value);
  835. read_uint32(buf, buf_end, func_index_count);
  836. size1 = sizeof(uint32) * (uint64)func_index_count;
  837. size = offsetof(AOTTableInitData, func_indexes) + size1;
  838. if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
  839. return false;
  840. }
  841. data_list[i]->mode = mode;
  842. data_list[i]->elem_type = elem_type;
  843. data_list[i]->is_dropped = false;
  844. data_list[i]->table_index = table_index;
  845. data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
  846. data_list[i]->offset.u.i64 = (int64)init_expr_value;
  847. data_list[i]->func_index_count = func_index_count;
  848. read_byte_array(buf, buf_end, data_list[i]->func_indexes,
  849. (uint32)size1);
  850. }
  851. *p_buf = buf;
  852. return true;
  853. fail:
  854. return false;
  855. }
  856. static bool
  857. load_table_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  858. char *error_buf, uint32 error_buf_size)
  859. {
  860. const uint8 *buf = *p_buf;
  861. read_uint32(buf, buf_end, module->import_table_count);
  862. if (module->import_table_count > 0
  863. && !load_import_table_list(&buf, buf_end, module, error_buf,
  864. error_buf_size))
  865. return false;
  866. read_uint32(buf, buf_end, module->table_count);
  867. if (module->table_count > 0
  868. && !load_table_list(&buf, buf_end, module, error_buf, error_buf_size))
  869. return false;
  870. read_uint32(buf, buf_end, module->table_init_data_count);
  871. /* load table init data list */
  872. if (module->table_init_data_count > 0
  873. && !load_table_init_data_list(&buf, buf_end, module, error_buf,
  874. error_buf_size))
  875. return false;
  876. *p_buf = buf;
  877. return true;
  878. fail:
  879. return false;
  880. }
  881. static void
  882. destroy_func_types(AOTFuncType **func_types, uint32 count, bool is_jit_mode)
  883. {
  884. if (!is_jit_mode) {
  885. uint32 i;
  886. for (i = 0; i < count; i++)
  887. if (func_types[i])
  888. wasm_runtime_free(func_types[i]);
  889. wasm_runtime_free(func_types);
  890. }
  891. }
  892. static bool
  893. load_func_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  894. char *error_buf, uint32 error_buf_size)
  895. {
  896. const uint8 *buf = *p_buf;
  897. AOTFuncType **func_types;
  898. uint64 size;
  899. uint32 i;
  900. /* Allocate memory */
  901. size = sizeof(AOTFuncType *) * (uint64)module->func_type_count;
  902. if (!(module->func_types = func_types =
  903. loader_malloc(size, error_buf, error_buf_size))) {
  904. return false;
  905. }
  906. /* Create each function type */
  907. for (i = 0; i < module->func_type_count; i++) {
  908. uint32 param_count, result_count;
  909. uint32 param_cell_num, ret_cell_num;
  910. uint64 size1;
  911. read_uint32(buf, buf_end, param_count);
  912. read_uint32(buf, buf_end, result_count);
  913. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  914. set_error_buf(error_buf, error_buf_size,
  915. "param count or result count too large");
  916. return false;
  917. }
  918. size1 = (uint64)param_count + (uint64)result_count;
  919. size = offsetof(AOTFuncType, types) + size1;
  920. if (!(func_types[i] = loader_malloc(size, error_buf, error_buf_size))) {
  921. return false;
  922. }
  923. func_types[i]->param_count = (uint16)param_count;
  924. func_types[i]->result_count = (uint16)result_count;
  925. read_byte_array(buf, buf_end, func_types[i]->types, (uint32)size1);
  926. param_cell_num = wasm_get_cell_num(func_types[i]->types, param_count);
  927. ret_cell_num =
  928. wasm_get_cell_num(func_types[i]->types + param_count, result_count);
  929. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  930. set_error_buf(error_buf, error_buf_size,
  931. "param count or result count too large");
  932. return false;
  933. }
  934. func_types[i]->param_cell_num = (uint16)param_cell_num;
  935. func_types[i]->ret_cell_num = (uint16)ret_cell_num;
  936. }
  937. *p_buf = buf;
  938. return true;
  939. fail:
  940. return false;
  941. }
  942. static bool
  943. load_func_type_info(const uint8 **p_buf, const uint8 *buf_end,
  944. AOTModule *module, char *error_buf, uint32 error_buf_size)
  945. {
  946. const uint8 *buf = *p_buf;
  947. read_uint32(buf, buf_end, module->func_type_count);
  948. /* load function type */
  949. if (module->func_type_count > 0
  950. && !load_func_types(&buf, buf_end, module, error_buf, error_buf_size))
  951. return false;
  952. *p_buf = buf;
  953. return true;
  954. fail:
  955. return false;
  956. }
  957. static void
  958. destroy_import_globals(AOTImportGlobal *import_globals, bool is_jit_mode)
  959. {
  960. if (!is_jit_mode)
  961. wasm_runtime_free(import_globals);
  962. }
  963. static bool
  964. load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
  965. AOTModule *module, bool is_load_from_file_buf,
  966. char *error_buf, uint32 error_buf_size)
  967. {
  968. const uint8 *buf = *p_buf;
  969. AOTImportGlobal *import_globals;
  970. uint64 size;
  971. uint32 i, data_offset = 0;
  972. #if WASM_ENABLE_LIBC_BUILTIN != 0
  973. WASMGlobalImport tmp_global;
  974. #endif
  975. /* Allocate memory */
  976. size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
  977. if (!(module->import_globals = import_globals =
  978. loader_malloc(size, error_buf, error_buf_size))) {
  979. return false;
  980. }
  981. /* Create each import global */
  982. for (i = 0; i < module->import_global_count; i++) {
  983. buf = (uint8 *)align_ptr(buf, 2);
  984. read_uint8(buf, buf_end, import_globals[i].type);
  985. read_uint8(buf, buf_end, import_globals[i].is_mutable);
  986. read_string(buf, buf_end, import_globals[i].module_name);
  987. read_string(buf, buf_end, import_globals[i].global_name);
  988. #if WASM_ENABLE_LIBC_BUILTIN != 0
  989. if (wasm_native_lookup_libc_builtin_global(
  990. import_globals[i].module_name, import_globals[i].global_name,
  991. &tmp_global)) {
  992. if (tmp_global.type != import_globals[i].type
  993. || tmp_global.is_mutable != import_globals[i].is_mutable) {
  994. set_error_buf(error_buf, error_buf_size,
  995. "incompatible import type");
  996. return false;
  997. }
  998. import_globals[i].global_data_linked =
  999. tmp_global.global_data_linked;
  1000. }
  1001. #endif
  1002. import_globals[i].size = wasm_value_type_size(import_globals[i].type);
  1003. import_globals[i].data_offset = data_offset;
  1004. data_offset += import_globals[i].size;
  1005. module->global_data_size += import_globals[i].size;
  1006. }
  1007. *p_buf = buf;
  1008. return true;
  1009. fail:
  1010. return false;
  1011. }
  1012. static bool
  1013. load_import_global_info(const uint8 **p_buf, const uint8 *buf_end,
  1014. AOTModule *module, bool is_load_from_file_buf,
  1015. char *error_buf, uint32 error_buf_size)
  1016. {
  1017. const uint8 *buf = *p_buf;
  1018. read_uint32(buf, buf_end, module->import_global_count);
  1019. /* load import globals */
  1020. if (module->import_global_count > 0
  1021. && !load_import_globals(&buf, buf_end, module, is_load_from_file_buf,
  1022. error_buf, error_buf_size))
  1023. return false;
  1024. *p_buf = buf;
  1025. return true;
  1026. fail:
  1027. return false;
  1028. }
  1029. static void
  1030. destroy_globals(AOTGlobal *globals, bool is_jit_mode)
  1031. {
  1032. if (!is_jit_mode)
  1033. wasm_runtime_free(globals);
  1034. }
  1035. static bool
  1036. load_globals(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1037. char *error_buf, uint32 error_buf_size)
  1038. {
  1039. const uint8 *buf = *p_buf;
  1040. AOTGlobal *globals;
  1041. uint64 size;
  1042. uint32 i, data_offset = 0;
  1043. AOTImportGlobal *last_import_global;
  1044. /* Allocate memory */
  1045. size = sizeof(AOTGlobal) * (uint64)module->global_count;
  1046. if (!(module->globals = globals =
  1047. loader_malloc(size, error_buf, error_buf_size))) {
  1048. return false;
  1049. }
  1050. if (module->import_global_count > 0) {
  1051. last_import_global =
  1052. &module->import_globals[module->import_global_count - 1];
  1053. data_offset =
  1054. last_import_global->data_offset + last_import_global->size;
  1055. }
  1056. /* Create each global */
  1057. for (i = 0; i < module->global_count; i++) {
  1058. uint16 init_expr_type;
  1059. read_uint8(buf, buf_end, globals[i].type);
  1060. read_uint8(buf, buf_end, globals[i].is_mutable);
  1061. read_uint16(buf, buf_end, init_expr_type);
  1062. if (init_expr_type != INIT_EXPR_TYPE_V128_CONST) {
  1063. read_uint64(buf, buf_end, globals[i].init_expr.u.i64);
  1064. }
  1065. else {
  1066. uint64 *i64x2 = (uint64 *)globals[i].init_expr.u.v128.i64x2;
  1067. CHECK_BUF(buf, buf_end, sizeof(uint64) * 2);
  1068. wasm_runtime_read_v128(buf, &i64x2[0], &i64x2[1]);
  1069. buf += sizeof(uint64) * 2;
  1070. }
  1071. globals[i].init_expr.init_expr_type = (uint8)init_expr_type;
  1072. globals[i].size = wasm_value_type_size(globals[i].type);
  1073. globals[i].data_offset = data_offset;
  1074. data_offset += globals[i].size;
  1075. module->global_data_size += globals[i].size;
  1076. }
  1077. *p_buf = buf;
  1078. return true;
  1079. fail:
  1080. return false;
  1081. }
  1082. static bool
  1083. load_global_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1084. char *error_buf, uint32 error_buf_size)
  1085. {
  1086. const uint8 *buf = *p_buf;
  1087. read_uint32(buf, buf_end, module->global_count);
  1088. /* load globals */
  1089. if (module->global_count > 0
  1090. && !load_globals(&buf, buf_end, module, error_buf, error_buf_size))
  1091. return false;
  1092. *p_buf = buf;
  1093. return true;
  1094. fail:
  1095. return false;
  1096. }
  1097. static void
  1098. destroy_import_funcs(AOTImportFunc *import_funcs, bool is_jit_mode)
  1099. {
  1100. if (!is_jit_mode)
  1101. wasm_runtime_free(import_funcs);
  1102. }
  1103. static bool
  1104. load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1105. bool is_load_from_file_buf, char *error_buf,
  1106. uint32 error_buf_size)
  1107. {
  1108. const char *module_name, *field_name;
  1109. const uint8 *buf = *p_buf;
  1110. AOTImportFunc *import_funcs;
  1111. uint64 size;
  1112. uint32 i;
  1113. /* Allocate memory */
  1114. size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
  1115. if (!(module->import_funcs = import_funcs =
  1116. loader_malloc(size, error_buf, error_buf_size))) {
  1117. return false;
  1118. }
  1119. /* Create each import func */
  1120. for (i = 0; i < module->import_func_count; i++) {
  1121. read_uint16(buf, buf_end, import_funcs[i].func_type_index);
  1122. if (import_funcs[i].func_type_index >= module->func_type_count) {
  1123. set_error_buf(error_buf, error_buf_size, "unknown type");
  1124. return false;
  1125. }
  1126. import_funcs[i].func_type =
  1127. module->func_types[import_funcs[i].func_type_index];
  1128. read_string(buf, buf_end, import_funcs[i].module_name);
  1129. read_string(buf, buf_end, import_funcs[i].func_name);
  1130. module_name = import_funcs[i].module_name;
  1131. field_name = import_funcs[i].func_name;
  1132. import_funcs[i].func_ptr_linked = wasm_native_resolve_symbol(
  1133. module_name, field_name, import_funcs[i].func_type,
  1134. &import_funcs[i].signature, &import_funcs[i].attachment,
  1135. &import_funcs[i].call_conv_raw);
  1136. #if WASM_ENABLE_LIBC_WASI != 0
  1137. if (!strcmp(import_funcs[i].module_name, "wasi_unstable")
  1138. || !strcmp(import_funcs[i].module_name, "wasi_snapshot_preview1"))
  1139. module->import_wasi_api = true;
  1140. #endif
  1141. }
  1142. *p_buf = buf;
  1143. return true;
  1144. fail:
  1145. return false;
  1146. }
  1147. static bool
  1148. load_import_func_info(const uint8 **p_buf, const uint8 *buf_end,
  1149. AOTModule *module, bool is_load_from_file_buf,
  1150. char *error_buf, uint32 error_buf_size)
  1151. {
  1152. const uint8 *buf = *p_buf;
  1153. read_uint32(buf, buf_end, module->import_func_count);
  1154. /* load import funcs */
  1155. if (module->import_func_count > 0
  1156. && !load_import_funcs(&buf, buf_end, module, is_load_from_file_buf,
  1157. error_buf, error_buf_size))
  1158. return false;
  1159. *p_buf = buf;
  1160. return true;
  1161. fail:
  1162. return false;
  1163. }
  1164. static void
  1165. destroy_object_data_sections(AOTObjectDataSection *data_sections,
  1166. uint32 data_section_count)
  1167. {
  1168. uint32 i;
  1169. AOTObjectDataSection *data_section = data_sections;
  1170. for (i = 0; i < data_section_count; i++, data_section++)
  1171. if (data_section->data)
  1172. os_munmap(data_section->data, data_section->size);
  1173. wasm_runtime_free(data_sections);
  1174. }
  1175. static bool
  1176. load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
  1177. AOTModule *module, bool is_load_from_file_buf,
  1178. char *error_buf, uint32 error_buf_size)
  1179. {
  1180. const uint8 *buf = *p_buf;
  1181. AOTObjectDataSection *data_sections;
  1182. uint64 size;
  1183. uint32 i;
  1184. /* Allocate memory */
  1185. size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count;
  1186. if (!(module->data_sections = data_sections =
  1187. loader_malloc(size, error_buf, error_buf_size))) {
  1188. return false;
  1189. }
  1190. /* Create each data section */
  1191. for (i = 0; i < module->data_section_count; i++) {
  1192. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE;
  1193. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1194. || defined(BUILD_TARGET_RISCV64_LP64D) \
  1195. || defined(BUILD_TARGET_RISCV64_LP64)
  1196. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1197. relocation for R_X86_64_32/32S/PC32 */
  1198. int map_flags = MMAP_MAP_32BIT;
  1199. #else
  1200. int map_flags = MMAP_MAP_NONE;
  1201. #endif
  1202. read_string(buf, buf_end, data_sections[i].name);
  1203. read_uint32(buf, buf_end, data_sections[i].size);
  1204. /* Allocate memory for data */
  1205. if (data_sections[i].size > 0
  1206. && !(data_sections[i].data = os_mmap(NULL, data_sections[i].size,
  1207. map_prot, map_flags))) {
  1208. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  1209. return false;
  1210. }
  1211. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1212. #if !defined(BH_PLATFORM_LINUX_SGX) && !defined(BH_PLATFORM_WINDOWS) \
  1213. && !defined(BH_PLATFORM_DARWIN)
  1214. /* address must be in the first 2 Gigabytes of
  1215. the process address space */
  1216. bh_assert((uintptr_t)data_sections[i].data < INT32_MAX);
  1217. #endif
  1218. #endif
  1219. read_byte_array(buf, buf_end, data_sections[i].data,
  1220. data_sections[i].size);
  1221. }
  1222. *p_buf = buf;
  1223. return true;
  1224. fail:
  1225. return false;
  1226. }
  1227. static bool
  1228. load_object_data_sections_info(const uint8 **p_buf, const uint8 *buf_end,
  1229. AOTModule *module, bool is_load_from_file_buf,
  1230. char *error_buf, uint32 error_buf_size)
  1231. {
  1232. const uint8 *buf = *p_buf;
  1233. read_uint32(buf, buf_end, module->data_section_count);
  1234. /* load object data sections */
  1235. if (module->data_section_count > 0
  1236. && !load_object_data_sections(&buf, buf_end, module,
  1237. is_load_from_file_buf, error_buf,
  1238. error_buf_size))
  1239. return false;
  1240. *p_buf = buf;
  1241. return true;
  1242. fail:
  1243. return false;
  1244. }
  1245. static bool
  1246. load_init_data_section(const uint8 *buf, const uint8 *buf_end,
  1247. AOTModule *module, bool is_load_from_file_buf,
  1248. char *error_buf, uint32 error_buf_size)
  1249. {
  1250. const uint8 *p = buf, *p_end = buf_end;
  1251. if (!load_memory_info(&p, p_end, module, error_buf, error_buf_size)
  1252. || !load_table_info(&p, p_end, module, error_buf, error_buf_size)
  1253. || !load_func_type_info(&p, p_end, module, error_buf, error_buf_size)
  1254. || !load_import_global_info(&p, p_end, module, is_load_from_file_buf,
  1255. error_buf, error_buf_size)
  1256. || !load_global_info(&p, p_end, module, error_buf, error_buf_size)
  1257. || !load_import_func_info(&p, p_end, module, is_load_from_file_buf,
  1258. error_buf, error_buf_size))
  1259. return false;
  1260. /* load function count and start function index */
  1261. read_uint32(p, p_end, module->func_count);
  1262. read_uint32(p, p_end, module->start_func_index);
  1263. /* check start function index */
  1264. if (module->start_func_index != (uint32)-1
  1265. && (module->start_func_index
  1266. >= module->import_func_count + module->func_count)) {
  1267. set_error_buf(error_buf, error_buf_size,
  1268. "invalid start function index");
  1269. return false;
  1270. }
  1271. read_uint32(p, p_end, module->aux_data_end_global_index);
  1272. read_uint32(p, p_end, module->aux_data_end);
  1273. read_uint32(p, p_end, module->aux_heap_base_global_index);
  1274. read_uint32(p, p_end, module->aux_heap_base);
  1275. read_uint32(p, p_end, module->aux_stack_top_global_index);
  1276. read_uint32(p, p_end, module->aux_stack_bottom);
  1277. read_uint32(p, p_end, module->aux_stack_size);
  1278. if (!load_object_data_sections_info(&p, p_end, module,
  1279. is_load_from_file_buf, error_buf,
  1280. error_buf_size))
  1281. return false;
  1282. if (p != p_end) {
  1283. set_error_buf(error_buf, error_buf_size,
  1284. "invalid init data section size");
  1285. return false;
  1286. }
  1287. return true;
  1288. fail:
  1289. return false;
  1290. }
  1291. static bool
  1292. load_text_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1293. char *error_buf, uint32 error_buf_size)
  1294. {
  1295. uint8 *plt_base;
  1296. if (module->func_count > 0 && buf_end == buf) {
  1297. set_error_buf(error_buf, error_buf_size, "invalid code size");
  1298. return false;
  1299. }
  1300. /* The layout is: literal size + literal + code (with plt table) */
  1301. read_uint32(buf, buf_end, module->literal_size);
  1302. /* literal data is at beginning of the text section */
  1303. module->literal = (uint8 *)buf;
  1304. module->code = (void *)(buf + module->literal_size);
  1305. module->code_size = (uint32)(buf_end - (uint8 *)module->code);
  1306. #if WASM_ENABLE_DEBUG_AOT != 0
  1307. module->elf_size = module->code_size;
  1308. if (is_ELF(module->code)) {
  1309. /* Now code points to an ELF object, we pull it down to .text section */
  1310. uint64 offset;
  1311. uint64 size;
  1312. char *code_buf = module->code;
  1313. module->elf_hdr = code_buf;
  1314. if (!get_text_section(code_buf, &offset, &size)) {
  1315. set_error_buf(error_buf, error_buf_size,
  1316. "get text section of ELF failed");
  1317. return false;
  1318. }
  1319. module->code = code_buf + offset;
  1320. module->code_size -= (uint32)offset;
  1321. }
  1322. #endif
  1323. if ((module->code_size > 0) && !module->is_indirect_mode) {
  1324. plt_base = (uint8 *)buf_end - get_plt_table_size();
  1325. init_plt_table(plt_base);
  1326. }
  1327. return true;
  1328. fail:
  1329. return false;
  1330. }
  1331. static bool
  1332. load_function_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1333. char *error_buf, uint32 error_buf_size)
  1334. {
  1335. const uint8 *p = buf, *p_end = buf_end;
  1336. uint32 i;
  1337. uint64 size, text_offset;
  1338. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1339. RUNTIME_FUNCTION *rtl_func_table;
  1340. AOTUnwindInfo *unwind_info;
  1341. uint32 unwind_info_offset = module->code_size - sizeof(AOTUnwindInfo);
  1342. uint32 unwind_code_offset = unwind_info_offset - PLT_ITEM_SIZE;
  1343. #endif
  1344. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1345. unwind_info = (AOTUnwindInfo *)((uint8 *)module->code + module->code_size
  1346. - sizeof(AOTUnwindInfo));
  1347. unwind_info->Version = 1;
  1348. unwind_info->Flags = UNW_FLAG_NHANDLER;
  1349. *(uint32 *)&unwind_info->UnwindCode[0] = unwind_code_offset;
  1350. size = sizeof(RUNTIME_FUNCTION) * (uint64)module->func_count;
  1351. if (size > 0
  1352. && !(rtl_func_table = module->rtl_func_table =
  1353. loader_malloc(size, error_buf, error_buf_size))) {
  1354. return false;
  1355. }
  1356. #endif
  1357. size = sizeof(void *) * (uint64)module->func_count;
  1358. if (size > 0
  1359. && !(module->func_ptrs =
  1360. loader_malloc(size, error_buf, error_buf_size))) {
  1361. return false;
  1362. }
  1363. for (i = 0; i < module->func_count; i++) {
  1364. if (sizeof(void *) == 8) {
  1365. read_uint64(p, p_end, text_offset);
  1366. }
  1367. else {
  1368. uint32 text_offset32;
  1369. read_uint32(p, p_end, text_offset32);
  1370. text_offset = text_offset32;
  1371. }
  1372. if (text_offset >= module->code_size) {
  1373. set_error_buf(error_buf, error_buf_size,
  1374. "invalid function code offset");
  1375. return false;
  1376. }
  1377. module->func_ptrs[i] = (uint8 *)module->code + text_offset;
  1378. #if defined(BUILD_TARGET_THUMB) || defined(BUILD_TARGET_THUMB_VFP)
  1379. /* bits[0] of thumb function address must be 1 */
  1380. module->func_ptrs[i] = (void *)((uintptr_t)module->func_ptrs[i] | 1);
  1381. #endif
  1382. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1383. rtl_func_table[i].BeginAddress = (DWORD)text_offset;
  1384. if (i > 0) {
  1385. rtl_func_table[i - 1].EndAddress = rtl_func_table[i].BeginAddress;
  1386. }
  1387. rtl_func_table[i].UnwindInfoAddress = (DWORD)unwind_info_offset;
  1388. #endif
  1389. }
  1390. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  1391. if (module->func_count > 0) {
  1392. uint32 plt_table_size =
  1393. module->is_indirect_mode ? 0 : get_plt_table_size();
  1394. rtl_func_table[module->func_count - 1].EndAddress =
  1395. (DWORD)(module->code_size - plt_table_size);
  1396. if (!RtlAddFunctionTable(rtl_func_table, module->func_count,
  1397. (DWORD64)(uintptr_t)module->code)) {
  1398. set_error_buf(error_buf, error_buf_size,
  1399. "add dynamic function table failed");
  1400. return false;
  1401. }
  1402. module->rtl_func_table_registered = true;
  1403. }
  1404. #endif
  1405. /* Set start function when function pointers are resolved */
  1406. if (module->start_func_index != (uint32)-1) {
  1407. if (module->start_func_index >= module->import_func_count)
  1408. module->start_function =
  1409. module->func_ptrs[module->start_func_index
  1410. - module->import_func_count];
  1411. else
  1412. /* TODO: fix start function can be import function issue */
  1413. module->start_function = NULL;
  1414. }
  1415. else {
  1416. module->start_function = NULL;
  1417. }
  1418. size = sizeof(uint32) * (uint64)module->func_count;
  1419. if (size > 0
  1420. && !(module->func_type_indexes =
  1421. loader_malloc(size, error_buf, error_buf_size))) {
  1422. return false;
  1423. }
  1424. for (i = 0; i < module->func_count; i++) {
  1425. read_uint32(p, p_end, module->func_type_indexes[i]);
  1426. if (module->func_type_indexes[i] >= module->func_type_count) {
  1427. set_error_buf(error_buf, error_buf_size, "unknown type");
  1428. return false;
  1429. }
  1430. }
  1431. if (p != buf_end) {
  1432. set_error_buf(error_buf, error_buf_size,
  1433. "invalid function section size");
  1434. return false;
  1435. }
  1436. return true;
  1437. fail:
  1438. return false;
  1439. }
  1440. static void
  1441. destroy_exports(AOTExport *exports, bool is_jit_mode)
  1442. {
  1443. if (!is_jit_mode)
  1444. wasm_runtime_free(exports);
  1445. }
  1446. static bool
  1447. load_exports(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
  1448. bool is_load_from_file_buf, char *error_buf, uint32 error_buf_size)
  1449. {
  1450. const uint8 *buf = *p_buf;
  1451. AOTExport *exports;
  1452. uint64 size;
  1453. uint32 i;
  1454. /* Allocate memory */
  1455. size = sizeof(AOTExport) * (uint64)module->export_count;
  1456. if (!(module->exports = exports =
  1457. loader_malloc(size, error_buf, error_buf_size))) {
  1458. return false;
  1459. }
  1460. /* Create each export */
  1461. for (i = 0; i < module->export_count; i++) {
  1462. read_uint32(buf, buf_end, exports[i].index);
  1463. read_uint8(buf, buf_end, exports[i].kind);
  1464. read_string(buf, buf_end, exports[i].name);
  1465. #if 0 /* TODO: check kind and index */
  1466. if (export_funcs[i].index >=
  1467. module->func_count + module->import_func_count) {
  1468. set_error_buf(error_buf, error_buf_size,
  1469. "function index is out of range");
  1470. return false;
  1471. }
  1472. #endif
  1473. }
  1474. *p_buf = buf;
  1475. return true;
  1476. fail:
  1477. return false;
  1478. }
  1479. static bool
  1480. load_export_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
  1481. bool is_load_from_file_buf, char *error_buf,
  1482. uint32 error_buf_size)
  1483. {
  1484. const uint8 *p = buf, *p_end = buf_end;
  1485. /* load export functions */
  1486. read_uint32(p, p_end, module->export_count);
  1487. if (module->export_count > 0
  1488. && !load_exports(&p, p_end, module, is_load_from_file_buf, error_buf,
  1489. error_buf_size))
  1490. return false;
  1491. if (p != p_end) {
  1492. set_error_buf(error_buf, error_buf_size, "invalid export section size");
  1493. return false;
  1494. }
  1495. return true;
  1496. fail:
  1497. return false;
  1498. }
  1499. static void *
  1500. get_data_section_addr(AOTModule *module, const char *section_name,
  1501. uint32 *p_data_size)
  1502. {
  1503. uint32 i;
  1504. AOTObjectDataSection *data_section = module->data_sections;
  1505. for (i = 0; i < module->data_section_count; i++, data_section++) {
  1506. if (!strcmp(data_section->name, section_name)) {
  1507. if (p_data_size)
  1508. *p_data_size = data_section->size;
  1509. return data_section->data;
  1510. }
  1511. }
  1512. return NULL;
  1513. }
  1514. static void *
  1515. resolve_target_sym(const char *symbol, int32 *p_index)
  1516. {
  1517. uint32 i, num = 0;
  1518. SymbolMap *target_sym_map;
  1519. if (!(target_sym_map = get_target_symbol_map(&num)))
  1520. return NULL;
  1521. for (i = 0; i < num; i++) {
  1522. if (!strcmp(target_sym_map[i].symbol_name, symbol)
  1523. #if defined(_WIN32) || defined(_WIN32_)
  1524. /* In Win32, the symbol name of function added by
  1525. LLVMAddFunction() is prefixed by '_', ignore it */
  1526. || (strlen(symbol) > 1 && symbol[0] == '_'
  1527. && !strcmp(target_sym_map[i].symbol_name, symbol + 1))
  1528. #endif
  1529. ) {
  1530. *p_index = (int32)i;
  1531. return target_sym_map[i].symbol_addr;
  1532. }
  1533. }
  1534. return NULL;
  1535. }
  1536. static bool
  1537. is_literal_relocation(const char *reloc_sec_name)
  1538. {
  1539. return !strcmp(reloc_sec_name, ".rela.literal");
  1540. }
  1541. static bool
  1542. str2uint32(const char *buf, uint32 *p_res)
  1543. {
  1544. uint32 res = 0, val;
  1545. const char *buf_end = buf + 8;
  1546. char ch;
  1547. while (buf < buf_end) {
  1548. ch = *buf++;
  1549. if (ch >= '0' && ch <= '9')
  1550. val = ch - '0';
  1551. else if (ch >= 'a' && ch <= 'f')
  1552. val = ch - 'a' + 0xA;
  1553. else if (ch >= 'A' && ch <= 'F')
  1554. val = ch - 'A' + 0xA;
  1555. else
  1556. return false;
  1557. res = (res << 4) | val;
  1558. }
  1559. *p_res = res;
  1560. return true;
  1561. }
  1562. static bool
  1563. str2uint64(const char *buf, uint64 *p_res)
  1564. {
  1565. uint64 res = 0, val;
  1566. const char *buf_end = buf + 16;
  1567. char ch;
  1568. while (buf < buf_end) {
  1569. ch = *buf++;
  1570. if (ch >= '0' && ch <= '9')
  1571. val = ch - '0';
  1572. else if (ch >= 'a' && ch <= 'f')
  1573. val = ch - 'a' + 0xA;
  1574. else if (ch >= 'A' && ch <= 'F')
  1575. val = ch - 'A' + 0xA;
  1576. else
  1577. return false;
  1578. res = (res << 4) | val;
  1579. }
  1580. *p_res = res;
  1581. return true;
  1582. }
  1583. static bool
  1584. do_text_relocation(AOTModule *module, AOTRelocationGroup *group,
  1585. char *error_buf, uint32 error_buf_size)
  1586. {
  1587. bool is_literal = is_literal_relocation(group->section_name);
  1588. uint8 *aot_text = is_literal ? module->literal : module->code;
  1589. uint32 aot_text_size =
  1590. is_literal ? module->literal_size : module->code_size;
  1591. uint32 i, func_index, symbol_len;
  1592. #if defined(BH_PLATFORM_WINDOWS)
  1593. uint32 ymm_plt_index = 0, xmm_plt_index = 0;
  1594. uint32 real_plt_index = 0, float_plt_index = 0, j;
  1595. #endif
  1596. char symbol_buf[128] = { 0 }, *symbol, *p;
  1597. void *symbol_addr;
  1598. AOTRelocation *relocation = group->relocations;
  1599. if (group->relocation_count > 0 && !aot_text) {
  1600. set_error_buf(error_buf, error_buf_size,
  1601. "invalid text relocation count");
  1602. return false;
  1603. }
  1604. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1605. int32 symbol_index = -1;
  1606. symbol_len = (uint32)strlen(relocation->symbol_name);
  1607. if (symbol_len + 1 <= sizeof(symbol_buf))
  1608. symbol = symbol_buf;
  1609. else {
  1610. if (!(symbol = loader_malloc(symbol_len + 1, error_buf,
  1611. error_buf_size))) {
  1612. return false;
  1613. }
  1614. }
  1615. bh_memcpy_s(symbol, symbol_len, relocation->symbol_name, symbol_len);
  1616. symbol[symbol_len] = '\0';
  1617. if (!strncmp(symbol, AOT_FUNC_PREFIX, strlen(AOT_FUNC_PREFIX))) {
  1618. p = symbol + strlen(AOT_FUNC_PREFIX);
  1619. if (*p == '\0'
  1620. || (func_index = (uint32)atoi(p)) > module->func_count) {
  1621. set_error_buf_v(error_buf, error_buf_size,
  1622. "invalid import symbol %s", symbol);
  1623. goto check_symbol_fail;
  1624. }
  1625. symbol_addr = module->func_ptrs[func_index];
  1626. }
  1627. else if (!strcmp(symbol, ".text")) {
  1628. symbol_addr = module->code;
  1629. }
  1630. else if (!strcmp(symbol, ".data") || !strcmp(symbol, ".sdata")
  1631. || !strcmp(symbol, ".rdata")
  1632. || !strcmp(symbol, ".rodata")
  1633. /* ".rodata.cst4/8/16/.." */
  1634. || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))
  1635. /* ".rodata.strn.m" */
  1636. || !strncmp(symbol, ".rodata.str", strlen(".rodata.str"))) {
  1637. symbol_addr = get_data_section_addr(module, symbol, NULL);
  1638. if (!symbol_addr) {
  1639. set_error_buf_v(error_buf, error_buf_size,
  1640. "invalid data section (%s)", symbol);
  1641. goto check_symbol_fail;
  1642. }
  1643. }
  1644. else if (!strcmp(symbol, ".literal")) {
  1645. symbol_addr = module->literal;
  1646. }
  1647. #if defined(BH_PLATFORM_WINDOWS)
  1648. /* Relocation for symbols which start with "__ymm@", "__xmm@" or
  1649. "__real@" and end with the ymm value, xmm value or real value.
  1650. In Windows PE file, the data is stored in some individual ".rdata"
  1651. sections. We simply create extra plt data, parse the values from
  1652. the symbols and stored them into the extra plt data. */
  1653. else if (!strcmp(group->section_name, ".text")
  1654. && !strncmp(symbol, YMM_PLT_PREFIX, strlen(YMM_PLT_PREFIX))
  1655. && strlen(symbol) == strlen(YMM_PLT_PREFIX) + 64) {
  1656. char ymm_buf[17] = { 0 };
  1657. symbol_addr = module->extra_plt_data + ymm_plt_index * 32;
  1658. for (j = 0; j < 4; j++) {
  1659. bh_memcpy_s(ymm_buf, sizeof(ymm_buf),
  1660. symbol + strlen(YMM_PLT_PREFIX) + 48 - 16 * j, 16);
  1661. if (!str2uint64(ymm_buf,
  1662. (uint64 *)((uint8 *)symbol_addr + 8 * j))) {
  1663. set_error_buf_v(error_buf, error_buf_size,
  1664. "resolve symbol %s failed", symbol);
  1665. goto check_symbol_fail;
  1666. }
  1667. }
  1668. ymm_plt_index++;
  1669. }
  1670. else if (!strcmp(group->section_name, ".text")
  1671. && !strncmp(symbol, XMM_PLT_PREFIX, strlen(XMM_PLT_PREFIX))
  1672. && strlen(symbol) == strlen(XMM_PLT_PREFIX) + 32) {
  1673. char xmm_buf[17] = { 0 };
  1674. symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32
  1675. + xmm_plt_index * 16;
  1676. for (j = 0; j < 2; j++) {
  1677. bh_memcpy_s(xmm_buf, sizeof(xmm_buf),
  1678. symbol + strlen(XMM_PLT_PREFIX) + 16 - 16 * j, 16);
  1679. if (!str2uint64(xmm_buf,
  1680. (uint64 *)((uint8 *)symbol_addr + 8 * j))) {
  1681. set_error_buf_v(error_buf, error_buf_size,
  1682. "resolve symbol %s failed", symbol);
  1683. goto check_symbol_fail;
  1684. }
  1685. }
  1686. xmm_plt_index++;
  1687. }
  1688. else if (!strcmp(group->section_name, ".text")
  1689. && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))
  1690. && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 16) {
  1691. char real_buf[17] = { 0 };
  1692. symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32
  1693. + module->xmm_plt_count * 16 + real_plt_index * 8;
  1694. bh_memcpy_s(real_buf, sizeof(real_buf),
  1695. symbol + strlen(REAL_PLT_PREFIX), 16);
  1696. if (!str2uint64(real_buf, (uint64 *)symbol_addr)) {
  1697. set_error_buf_v(error_buf, error_buf_size,
  1698. "resolve symbol %s failed", symbol);
  1699. goto check_symbol_fail;
  1700. }
  1701. real_plt_index++;
  1702. }
  1703. else if (!strcmp(group->section_name, ".text")
  1704. && !strncmp(symbol, REAL_PLT_PREFIX, strlen(REAL_PLT_PREFIX))
  1705. && strlen(symbol) == strlen(REAL_PLT_PREFIX) + 8) {
  1706. char float_buf[9] = { 0 };
  1707. symbol_addr = module->extra_plt_data + module->ymm_plt_count * 32
  1708. + module->xmm_plt_count * 16
  1709. + module->real_plt_count * 8 + float_plt_index * 4;
  1710. bh_memcpy_s(float_buf, sizeof(float_buf),
  1711. symbol + strlen(REAL_PLT_PREFIX), 8);
  1712. if (!str2uint32(float_buf, (uint32 *)symbol_addr)) {
  1713. set_error_buf_v(error_buf, error_buf_size,
  1714. "resolve symbol %s failed", symbol);
  1715. goto check_symbol_fail;
  1716. }
  1717. float_plt_index++;
  1718. }
  1719. #endif /* end of defined(BH_PLATFORM_WINDOWS) */
  1720. else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) {
  1721. set_error_buf_v(error_buf, error_buf_size,
  1722. "resolve symbol %s failed", symbol);
  1723. goto check_symbol_fail;
  1724. }
  1725. if (symbol != symbol_buf)
  1726. wasm_runtime_free(symbol);
  1727. if (!apply_relocation(
  1728. module, aot_text, aot_text_size, relocation->relocation_offset,
  1729. relocation->relocation_addend, relocation->relocation_type,
  1730. symbol_addr, symbol_index, error_buf, error_buf_size))
  1731. return false;
  1732. }
  1733. return true;
  1734. check_symbol_fail:
  1735. if (symbol != symbol_buf)
  1736. wasm_runtime_free(symbol);
  1737. return false;
  1738. }
  1739. static bool
  1740. do_data_relocation(AOTModule *module, AOTRelocationGroup *group,
  1741. char *error_buf, uint32 error_buf_size)
  1742. {
  1743. uint8 *data_addr;
  1744. uint32 data_size = 0, i;
  1745. AOTRelocation *relocation = group->relocations;
  1746. void *symbol_addr;
  1747. char *symbol, *data_section_name;
  1748. if (!strncmp(group->section_name, ".rela.", 6)) {
  1749. data_section_name = group->section_name + strlen(".rela");
  1750. }
  1751. else if (!strncmp(group->section_name, ".rel.", 5)) {
  1752. data_section_name = group->section_name + strlen(".rel");
  1753. }
  1754. else if (!strcmp(group->section_name, ".rdata")) {
  1755. data_section_name = group->section_name;
  1756. }
  1757. else {
  1758. set_error_buf(error_buf, error_buf_size,
  1759. "invalid data relocation section name");
  1760. return false;
  1761. }
  1762. data_addr = get_data_section_addr(module, data_section_name, &data_size);
  1763. if (group->relocation_count > 0 && !data_addr) {
  1764. set_error_buf(error_buf, error_buf_size,
  1765. "invalid data relocation count");
  1766. return false;
  1767. }
  1768. for (i = 0; i < group->relocation_count; i++, relocation++) {
  1769. symbol = relocation->symbol_name;
  1770. if (!strcmp(symbol, ".text")) {
  1771. symbol_addr = module->code;
  1772. }
  1773. else {
  1774. set_error_buf_v(error_buf, error_buf_size,
  1775. "invalid relocation symbol %s", symbol);
  1776. return false;
  1777. }
  1778. if (!apply_relocation(
  1779. module, data_addr, data_size, relocation->relocation_offset,
  1780. relocation->relocation_addend, relocation->relocation_type,
  1781. symbol_addr, -1, error_buf, error_buf_size))
  1782. return false;
  1783. }
  1784. return true;
  1785. }
  1786. static bool
  1787. validate_symbol_table(uint8 *buf, uint8 *buf_end, uint32 *offsets, uint32 count,
  1788. char *error_buf, uint32 error_buf_size)
  1789. {
  1790. uint32 i, str_len_addr = 0;
  1791. uint16 str_len;
  1792. for (i = 0; i < count; i++) {
  1793. if (offsets[i] != str_len_addr)
  1794. return false;
  1795. read_uint16(buf, buf_end, str_len);
  1796. str_len_addr += (uint32)sizeof(uint16) + str_len;
  1797. str_len_addr = align_uint(str_len_addr, 2);
  1798. buf += str_len;
  1799. buf = (uint8 *)align_ptr(buf, 2);
  1800. }
  1801. if (buf == buf_end)
  1802. return true;
  1803. fail:
  1804. return false;
  1805. }
  1806. static bool
  1807. load_relocation_section(const uint8 *buf, const uint8 *buf_end,
  1808. AOTModule *module, bool is_load_from_file_buf,
  1809. char *error_buf, uint32 error_buf_size)
  1810. {
  1811. AOTRelocationGroup *groups = NULL, *group;
  1812. uint32 symbol_count = 0;
  1813. uint32 group_count = 0, i, j;
  1814. uint64 size;
  1815. uint32 *symbol_offsets, total_string_len;
  1816. uint8 *symbol_buf, *symbol_buf_end;
  1817. int map_prot, map_flags;
  1818. bool ret = false;
  1819. char **symbols = NULL;
  1820. read_uint32(buf, buf_end, symbol_count);
  1821. symbol_offsets = (uint32 *)buf;
  1822. for (i = 0; i < symbol_count; i++) {
  1823. CHECK_BUF(buf, buf_end, sizeof(uint32));
  1824. buf += sizeof(uint32);
  1825. }
  1826. read_uint32(buf, buf_end, total_string_len);
  1827. symbol_buf = (uint8 *)buf;
  1828. symbol_buf_end = symbol_buf + total_string_len;
  1829. if (!validate_symbol_table(symbol_buf, symbol_buf_end, symbol_offsets,
  1830. symbol_count, error_buf, error_buf_size)) {
  1831. set_error_buf(error_buf, error_buf_size,
  1832. "validate symbol table failed");
  1833. goto fail;
  1834. }
  1835. if (symbol_count > 0) {
  1836. symbols = loader_malloc((uint64)sizeof(*symbols) * symbol_count,
  1837. error_buf, error_buf_size);
  1838. if (symbols == NULL) {
  1839. goto fail;
  1840. }
  1841. }
  1842. #if defined(BH_PLATFORM_WINDOWS)
  1843. buf = symbol_buf_end;
  1844. read_uint32(buf, buf_end, group_count);
  1845. for (i = 0; i < group_count; i++) {
  1846. uint32 name_index, relocation_count;
  1847. uint16 group_name_len;
  1848. uint8 *group_name;
  1849. /* section name address is 4 bytes aligned. */
  1850. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  1851. read_uint32(buf, buf_end, name_index);
  1852. if (name_index >= symbol_count) {
  1853. set_error_buf(error_buf, error_buf_size,
  1854. "symbol index out of range");
  1855. goto fail;
  1856. }
  1857. group_name = symbol_buf + symbol_offsets[name_index];
  1858. group_name_len = *(uint16 *)group_name;
  1859. group_name += sizeof(uint16);
  1860. read_uint32(buf, buf_end, relocation_count);
  1861. for (j = 0; j < relocation_count; j++) {
  1862. AOTRelocation relocation = { 0 };
  1863. uint32 symbol_index, offset32;
  1864. int32 addend32;
  1865. uint16 symbol_name_len;
  1866. uint8 *symbol_name;
  1867. if (sizeof(void *) == 8) {
  1868. read_uint64(buf, buf_end, relocation.relocation_offset);
  1869. read_uint64(buf, buf_end, relocation.relocation_addend);
  1870. }
  1871. else {
  1872. read_uint32(buf, buf_end, offset32);
  1873. relocation.relocation_offset = (uint64)offset32;
  1874. read_uint32(buf, buf_end, addend32);
  1875. relocation.relocation_addend = (int64)addend32;
  1876. }
  1877. read_uint32(buf, buf_end, relocation.relocation_type);
  1878. read_uint32(buf, buf_end, symbol_index);
  1879. if (symbol_index >= symbol_count) {
  1880. set_error_buf(error_buf, error_buf_size,
  1881. "symbol index out of range");
  1882. goto fail;
  1883. }
  1884. symbol_name = symbol_buf + symbol_offsets[symbol_index];
  1885. symbol_name_len = *(uint16 *)symbol_name;
  1886. symbol_name += sizeof(uint16);
  1887. char group_name_buf[128] = { 0 };
  1888. char symbol_name_buf[128] = { 0 };
  1889. memcpy(group_name_buf, group_name, group_name_len);
  1890. memcpy(symbol_name_buf, symbol_name, symbol_name_len);
  1891. if ((group_name_len == strlen(".text")
  1892. || (module->is_indirect_mode
  1893. && group_name_len == strlen(".text") + 1))
  1894. && !strncmp(group_name, ".text", strlen(".text"))) {
  1895. if ((symbol_name_len == strlen(YMM_PLT_PREFIX) + 64
  1896. || (module->is_indirect_mode
  1897. && symbol_name_len == strlen(YMM_PLT_PREFIX) + 64 + 1))
  1898. && !strncmp(symbol_name, YMM_PLT_PREFIX,
  1899. strlen(YMM_PLT_PREFIX))) {
  1900. module->ymm_plt_count++;
  1901. }
  1902. else if ((symbol_name_len == strlen(XMM_PLT_PREFIX) + 32
  1903. || (module->is_indirect_mode
  1904. && symbol_name_len
  1905. == strlen(XMM_PLT_PREFIX) + 32 + 1))
  1906. && !strncmp(symbol_name, XMM_PLT_PREFIX,
  1907. strlen(XMM_PLT_PREFIX))) {
  1908. module->xmm_plt_count++;
  1909. }
  1910. else if ((symbol_name_len == strlen(REAL_PLT_PREFIX) + 16
  1911. || (module->is_indirect_mode
  1912. && symbol_name_len
  1913. == strlen(REAL_PLT_PREFIX) + 16 + 1))
  1914. && !strncmp(symbol_name, REAL_PLT_PREFIX,
  1915. strlen(REAL_PLT_PREFIX))) {
  1916. module->real_plt_count++;
  1917. }
  1918. else if ((symbol_name_len >= strlen(REAL_PLT_PREFIX) + 8
  1919. || (module->is_indirect_mode
  1920. && symbol_name_len
  1921. == strlen(REAL_PLT_PREFIX) + 8 + 1))
  1922. && !strncmp(symbol_name, REAL_PLT_PREFIX,
  1923. strlen(REAL_PLT_PREFIX))) {
  1924. module->float_plt_count++;
  1925. }
  1926. }
  1927. }
  1928. }
  1929. /* Allocate memory for extra plt data */
  1930. size = sizeof(uint64) * 4 * module->ymm_plt_count
  1931. + sizeof(uint64) * 2 * module->xmm_plt_count
  1932. + sizeof(uint64) * module->real_plt_count
  1933. + sizeof(uint32) * module->float_plt_count;
  1934. if (size > 0) {
  1935. map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  1936. /* aot code and data in x86_64 must be in range 0 to 2G due to
  1937. relocation for R_X86_64_32/32S/PC32 */
  1938. map_flags = MMAP_MAP_32BIT;
  1939. if (size > UINT32_MAX
  1940. || !(module->extra_plt_data =
  1941. os_mmap(NULL, (uint32)size, map_prot, map_flags))) {
  1942. set_error_buf(error_buf, error_buf_size, "mmap memory failed");
  1943. goto fail;
  1944. }
  1945. module->extra_plt_data_size = (uint32)size;
  1946. }
  1947. #endif /* end of defined(BH_PLATFORM_WINDOWS) */
  1948. buf = symbol_buf_end;
  1949. read_uint32(buf, buf_end, group_count);
  1950. /* Allocate memory for relocation groups */
  1951. size = sizeof(AOTRelocationGroup) * (uint64)group_count;
  1952. if (size > 0
  1953. && !(groups = loader_malloc(size, error_buf, error_buf_size))) {
  1954. goto fail;
  1955. }
  1956. /* Load each relocation group */
  1957. for (i = 0, group = groups; i < group_count; i++, group++) {
  1958. AOTRelocation *relocation;
  1959. uint32 name_index;
  1960. /* section name address is 4 bytes aligned. */
  1961. buf = (uint8 *)align_ptr(buf, sizeof(uint32));
  1962. read_uint32(buf, buf_end, name_index);
  1963. if (name_index >= symbol_count) {
  1964. set_error_buf(error_buf, error_buf_size,
  1965. "symbol index out of range");
  1966. goto fail;
  1967. }
  1968. if (symbols[name_index] == NULL) {
  1969. uint8 *name_addr = symbol_buf + symbol_offsets[name_index];
  1970. read_string(name_addr, buf_end, symbols[name_index]);
  1971. }
  1972. group->section_name = symbols[name_index];
  1973. read_uint32(buf, buf_end, group->relocation_count);
  1974. /* Allocate memory for relocations */
  1975. size = sizeof(AOTRelocation) * (uint64)group->relocation_count;
  1976. if (!(group->relocations = relocation =
  1977. loader_malloc(size, error_buf, error_buf_size))) {
  1978. ret = false;
  1979. goto fail;
  1980. }
  1981. /* Load each relocation */
  1982. for (j = 0; j < group->relocation_count; j++, relocation++) {
  1983. uint32 symbol_index;
  1984. if (sizeof(void *) == 8) {
  1985. read_uint64(buf, buf_end, relocation->relocation_offset);
  1986. read_uint64(buf, buf_end, relocation->relocation_addend);
  1987. }
  1988. else {
  1989. uint32 offset32, addend32;
  1990. read_uint32(buf, buf_end, offset32);
  1991. relocation->relocation_offset = (uint64)offset32;
  1992. read_uint32(buf, buf_end, addend32);
  1993. relocation->relocation_addend = (uint64)addend32;
  1994. }
  1995. read_uint32(buf, buf_end, relocation->relocation_type);
  1996. read_uint32(buf, buf_end, symbol_index);
  1997. if (symbol_index >= symbol_count) {
  1998. set_error_buf(error_buf, error_buf_size,
  1999. "symbol index out of range");
  2000. goto fail;
  2001. }
  2002. if (symbols[symbol_index] == NULL) {
  2003. uint8 *symbol_addr = symbol_buf + symbol_offsets[symbol_index];
  2004. read_string(symbol_addr, buf_end, symbols[symbol_index]);
  2005. }
  2006. relocation->symbol_name = symbols[symbol_index];
  2007. }
  2008. if (!strcmp(group->section_name, ".rel.text")
  2009. || !strcmp(group->section_name, ".rela.text")
  2010. || !strcmp(group->section_name, ".rela.literal")
  2011. #ifdef BH_PLATFORM_WINDOWS
  2012. || !strcmp(group->section_name, ".text")
  2013. #endif
  2014. ) {
  2015. #if !defined(BH_PLATFORM_LINUX) && !defined(BH_PLATFORM_LINUX_SGX) \
  2016. && !defined(BH_PLATFORM_DARWIN) && !defined(BH_PLATFORM_WINDOWS)
  2017. if (module->is_indirect_mode) {
  2018. set_error_buf(error_buf, error_buf_size,
  2019. "cannot apply relocation to text section "
  2020. "for aot file generated with "
  2021. "\"--enable-indirect-mode\" flag");
  2022. goto fail;
  2023. }
  2024. #endif
  2025. if (!do_text_relocation(module, group, error_buf, error_buf_size))
  2026. goto fail;
  2027. }
  2028. else {
  2029. if (!do_data_relocation(module, group, error_buf, error_buf_size))
  2030. goto fail;
  2031. }
  2032. }
  2033. /* Set read only for AOT code and some data sections */
  2034. map_prot = MMAP_PROT_READ | MMAP_PROT_EXEC;
  2035. if (module->code) {
  2036. /* The layout is: literal size + literal + code (with plt table) */
  2037. uint8 *mmap_addr = module->literal - sizeof(uint32);
  2038. uint32 total_size =
  2039. sizeof(uint32) + module->literal_size + module->code_size;
  2040. os_mprotect(mmap_addr, total_size, map_prot);
  2041. }
  2042. map_prot = MMAP_PROT_READ;
  2043. #if defined(BH_PLATFORM_WINDOWS)
  2044. if (module->extra_plt_data) {
  2045. os_mprotect(module->extra_plt_data, module->extra_plt_data_size,
  2046. map_prot);
  2047. }
  2048. #endif
  2049. for (i = 0; i < module->data_section_count; i++) {
  2050. AOTObjectDataSection *data_section = module->data_sections + i;
  2051. if (!strcmp(data_section->name, ".rdata")
  2052. || !strcmp(data_section->name, ".rodata")
  2053. /* ".rodata.cst4/8/16/.." */
  2054. || !strncmp(data_section->name, ".rodata.cst",
  2055. strlen(".rodata.cst"))
  2056. /* ".rodata.strn.m" */
  2057. || !strncmp(data_section->name, ".rodata.str",
  2058. strlen(".rodata.str"))) {
  2059. os_mprotect(data_section->data, data_section->size, map_prot);
  2060. }
  2061. }
  2062. ret = true;
  2063. fail:
  2064. if (symbols) {
  2065. wasm_runtime_free(symbols);
  2066. }
  2067. if (groups) {
  2068. for (i = 0, group = groups; i < group_count; i++, group++)
  2069. if (group->relocations)
  2070. wasm_runtime_free(group->relocations);
  2071. wasm_runtime_free(groups);
  2072. }
  2073. (void)map_flags;
  2074. return ret;
  2075. }
  2076. static bool
  2077. load_from_sections(AOTModule *module, AOTSection *sections,
  2078. bool is_load_from_file_buf, char *error_buf,
  2079. uint32 error_buf_size)
  2080. {
  2081. AOTSection *section = sections;
  2082. const uint8 *buf, *buf_end;
  2083. uint32 last_section_type = (uint32)-1, section_type;
  2084. uint32 i, func_index, func_type_index;
  2085. AOTFuncType *func_type;
  2086. AOTExport *exports;
  2087. while (section) {
  2088. buf = section->section_body;
  2089. buf_end = buf + section->section_body_size;
  2090. /* Check sections */
  2091. section_type = (uint32)section->section_type;
  2092. if ((last_section_type == (uint32)-1
  2093. && section_type != AOT_SECTION_TYPE_TARGET_INFO)
  2094. || (last_section_type != (uint32)-1
  2095. && (section_type != last_section_type + 1
  2096. && section_type != AOT_SECTION_TYPE_CUSTOM))) {
  2097. set_error_buf(error_buf, error_buf_size, "invalid section order");
  2098. return false;
  2099. }
  2100. last_section_type = section_type;
  2101. switch (section_type) {
  2102. case AOT_SECTION_TYPE_TARGET_INFO:
  2103. if (!load_target_info_section(buf, buf_end, module, error_buf,
  2104. error_buf_size))
  2105. return false;
  2106. break;
  2107. case AOT_SECTION_TYPE_INIT_DATA:
  2108. if (!load_init_data_section(buf, buf_end, module,
  2109. is_load_from_file_buf, error_buf,
  2110. error_buf_size))
  2111. return false;
  2112. break;
  2113. case AOT_SECTION_TYPE_TEXT:
  2114. if (!load_text_section(buf, buf_end, module, error_buf,
  2115. error_buf_size))
  2116. return false;
  2117. break;
  2118. case AOT_SECTION_TYPE_FUNCTION:
  2119. if (!load_function_section(buf, buf_end, module, error_buf,
  2120. error_buf_size))
  2121. return false;
  2122. break;
  2123. case AOT_SECTION_TYPE_EXPORT:
  2124. if (!load_export_section(buf, buf_end, module,
  2125. is_load_from_file_buf, error_buf,
  2126. error_buf_size))
  2127. return false;
  2128. break;
  2129. case AOT_SECTION_TYPE_RELOCATION:
  2130. if (!load_relocation_section(buf, buf_end, module,
  2131. is_load_from_file_buf, error_buf,
  2132. error_buf_size))
  2133. return false;
  2134. break;
  2135. case AOT_SECTION_TYPE_CUSTOM:
  2136. if (!load_custom_section(buf, buf_end, module,
  2137. is_load_from_file_buf, error_buf,
  2138. error_buf_size))
  2139. return false;
  2140. break;
  2141. default:
  2142. set_error_buf(error_buf, error_buf_size,
  2143. "invalid aot section type");
  2144. return false;
  2145. }
  2146. section = section->next;
  2147. }
  2148. if (last_section_type != AOT_SECTION_TYPE_RELOCATION
  2149. && last_section_type != AOT_SECTION_TYPE_CUSTOM) {
  2150. set_error_buf(error_buf, error_buf_size, "section missing");
  2151. return false;
  2152. }
  2153. /* Resolve malloc and free function */
  2154. module->malloc_func_index = (uint32)-1;
  2155. module->free_func_index = (uint32)-1;
  2156. module->retain_func_index = (uint32)-1;
  2157. exports = module->exports;
  2158. for (i = 0; i < module->export_count; i++) {
  2159. if (exports[i].kind == EXPORT_KIND_FUNC
  2160. && exports[i].index >= module->import_func_count) {
  2161. if (!strcmp(exports[i].name, "malloc")) {
  2162. func_index = exports[i].index - module->import_func_count;
  2163. func_type_index = module->func_type_indexes[func_index];
  2164. func_type = module->func_types[func_type_index];
  2165. if (func_type->param_count == 1 && func_type->result_count == 1
  2166. && func_type->types[0] == VALUE_TYPE_I32
  2167. && func_type->types[1] == VALUE_TYPE_I32) {
  2168. bh_assert(module->malloc_func_index == (uint32)-1);
  2169. module->malloc_func_index = func_index;
  2170. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2171. exports[i].name, exports[i].index);
  2172. }
  2173. }
  2174. else if (!strcmp(exports[i].name, "__new")) {
  2175. func_index = exports[i].index - module->import_func_count;
  2176. func_type_index = module->func_type_indexes[func_index];
  2177. func_type = module->func_types[func_type_index];
  2178. if (func_type->param_count == 2 && func_type->result_count == 1
  2179. && func_type->types[0] == VALUE_TYPE_I32
  2180. && func_type->types[1] == VALUE_TYPE_I32
  2181. && func_type->types[2] == VALUE_TYPE_I32) {
  2182. uint32 j;
  2183. WASMExport *export_tmp;
  2184. bh_assert(module->malloc_func_index == (uint32)-1);
  2185. module->malloc_func_index = func_index;
  2186. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2187. exports[i].name, exports[i].index);
  2188. /* resolve retain function.
  2189. If not find, reset malloc function index */
  2190. export_tmp = module->exports;
  2191. for (j = 0; j < module->export_count; j++, export_tmp++) {
  2192. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  2193. && (!strcmp(export_tmp->name, "__retain")
  2194. || !strcmp(export_tmp->name, "__pin"))) {
  2195. func_index =
  2196. export_tmp->index - module->import_func_count;
  2197. func_type_index =
  2198. module->func_type_indexes[func_index];
  2199. func_type = module->func_types[func_type_index];
  2200. if (func_type->param_count == 1
  2201. && func_type->result_count == 1
  2202. && func_type->types[0] == VALUE_TYPE_I32
  2203. && func_type->types[1] == VALUE_TYPE_I32) {
  2204. bh_assert(module->retain_func_index
  2205. == (uint32)-1);
  2206. module->retain_func_index = export_tmp->index;
  2207. LOG_VERBOSE("Found retain function, name: %s, "
  2208. "index: %u",
  2209. export_tmp->name,
  2210. export_tmp->index);
  2211. break;
  2212. }
  2213. }
  2214. }
  2215. if (j == module->export_count) {
  2216. module->malloc_func_index = (uint32)-1;
  2217. LOG_VERBOSE("Can't find retain function,"
  2218. "reset malloc function index to -1");
  2219. }
  2220. }
  2221. }
  2222. else if ((!strcmp(exports[i].name, "free"))
  2223. || (!strcmp(exports[i].name, "__release"))
  2224. || (!strcmp(exports[i].name, "__unpin"))) {
  2225. func_index = exports[i].index - module->import_func_count;
  2226. func_type_index = module->func_type_indexes[func_index];
  2227. func_type = module->func_types[func_type_index];
  2228. if (func_type->param_count == 1 && func_type->result_count == 0
  2229. && func_type->types[0] == VALUE_TYPE_I32) {
  2230. bh_assert(module->free_func_index == (uint32)-1);
  2231. module->free_func_index = func_index;
  2232. LOG_VERBOSE("Found free function, name: %s, index: %u",
  2233. exports[i].name, exports[i].index);
  2234. }
  2235. }
  2236. }
  2237. }
  2238. /* Flush data cache before executing AOT code,
  2239. * otherwise unpredictable behavior can occur. */
  2240. os_dcache_flush();
  2241. #if WASM_ENABLE_MEMORY_TRACING != 0
  2242. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  2243. #endif
  2244. #if WASM_ENABLE_DEBUG_AOT != 0
  2245. if (!jit_code_entry_create(module->elf_hdr, module->elf_size)) {
  2246. set_error_buf(error_buf, error_buf_size,
  2247. "create jit code entry failed");
  2248. return false;
  2249. }
  2250. #endif
  2251. return true;
  2252. }
  2253. static AOTModule *
  2254. create_module(char *error_buf, uint32 error_buf_size)
  2255. {
  2256. AOTModule *module =
  2257. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size);
  2258. if (!module) {
  2259. return NULL;
  2260. }
  2261. module->module_type = Wasm_Module_AoT;
  2262. return module;
  2263. }
  2264. AOTModule *
  2265. aot_load_from_sections(AOTSection *section_list, char *error_buf,
  2266. uint32 error_buf_size)
  2267. {
  2268. AOTModule *module = create_module(error_buf, error_buf_size);
  2269. if (!module)
  2270. return NULL;
  2271. if (!load_from_sections(module, section_list, false, error_buf,
  2272. error_buf_size)) {
  2273. aot_unload(module);
  2274. return NULL;
  2275. }
  2276. LOG_VERBOSE("Load module from sections success.\n");
  2277. return module;
  2278. }
  2279. static void
  2280. destroy_sections(AOTSection *section_list, bool destroy_aot_text)
  2281. {
  2282. AOTSection *section = section_list, *next;
  2283. while (section) {
  2284. next = section->next;
  2285. if (destroy_aot_text && section->section_type == AOT_SECTION_TYPE_TEXT
  2286. && section->section_body)
  2287. os_munmap((uint8 *)section->section_body,
  2288. section->section_body_size);
  2289. wasm_runtime_free(section);
  2290. section = next;
  2291. }
  2292. }
  2293. static bool
  2294. resolve_execute_mode(const uint8 *buf, uint32 size, bool *p_mode,
  2295. char *error_buf, uint32 error_buf_size)
  2296. {
  2297. const uint8 *p = buf, *p_end = buf + size;
  2298. uint32 section_type;
  2299. uint32 section_size = 0;
  2300. uint16 e_type = 0;
  2301. p += 8;
  2302. while (p < p_end) {
  2303. read_uint32(p, p_end, section_type);
  2304. if (section_type <= AOT_SECTION_TYPE_SIGANATURE
  2305. || section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  2306. read_uint32(p, p_end, section_size);
  2307. CHECK_BUF(p, p_end, section_size);
  2308. if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
  2309. p += 4;
  2310. read_uint16(p, p_end, e_type);
  2311. if (e_type == E_TYPE_XIP) {
  2312. *p_mode = true;
  2313. }
  2314. else {
  2315. *p_mode = false;
  2316. }
  2317. break;
  2318. }
  2319. }
  2320. else if (section_type > AOT_SECTION_TYPE_SIGANATURE) {
  2321. set_error_buf(error_buf, error_buf_size,
  2322. "resolve execute mode failed");
  2323. break;
  2324. }
  2325. p += section_size;
  2326. }
  2327. return true;
  2328. fail:
  2329. return false;
  2330. }
  2331. static bool
  2332. create_sections(AOTModule *module, const uint8 *buf, uint32 size,
  2333. AOTSection **p_section_list, char *error_buf,
  2334. uint32 error_buf_size)
  2335. {
  2336. AOTSection *section_list = NULL, *section_list_end = NULL, *section;
  2337. const uint8 *p = buf, *p_end = buf + size;
  2338. bool destroy_aot_text = false;
  2339. bool is_indirect_mode = false;
  2340. uint32 section_type;
  2341. uint32 section_size;
  2342. uint64 total_size;
  2343. uint8 *aot_text;
  2344. if (!resolve_execute_mode(buf, size, &is_indirect_mode, error_buf,
  2345. error_buf_size)) {
  2346. goto fail;
  2347. }
  2348. module->is_indirect_mode = is_indirect_mode;
  2349. p += 8;
  2350. while (p < p_end) {
  2351. read_uint32(p, p_end, section_type);
  2352. if (section_type < AOT_SECTION_TYPE_SIGANATURE
  2353. || section_type == AOT_SECTION_TYPE_CUSTOM) {
  2354. read_uint32(p, p_end, section_size);
  2355. CHECK_BUF(p, p_end, section_size);
  2356. if (!(section = loader_malloc(sizeof(AOTSection), error_buf,
  2357. error_buf_size))) {
  2358. goto fail;
  2359. }
  2360. memset(section, 0, sizeof(AOTSection));
  2361. section->section_type = (int32)section_type;
  2362. section->section_body = (uint8 *)p;
  2363. section->section_body_size = section_size;
  2364. if (section_type == AOT_SECTION_TYPE_TEXT) {
  2365. if ((section_size > 0) && !module->is_indirect_mode) {
  2366. int map_prot =
  2367. MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  2368. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  2369. || defined(BUILD_TARGET_RISCV64_LP64D) \
  2370. || defined(BUILD_TARGET_RISCV64_LP64)
  2371. /* aot code and data in x86_64 must be in range 0 to 2G due
  2372. to relocation for R_X86_64_32/32S/PC32 */
  2373. int map_flags = MMAP_MAP_32BIT;
  2374. #else
  2375. int map_flags = MMAP_MAP_NONE;
  2376. #endif
  2377. total_size =
  2378. (uint64)section_size + aot_get_plt_table_size();
  2379. total_size = (total_size + 3) & ~((uint64)3);
  2380. if (total_size >= UINT32_MAX
  2381. || !(aot_text = os_mmap(NULL, (uint32)total_size,
  2382. map_prot, map_flags))) {
  2383. wasm_runtime_free(section);
  2384. set_error_buf(error_buf, error_buf_size,
  2385. "mmap memory failed");
  2386. goto fail;
  2387. }
  2388. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  2389. #if !defined(BH_PLATFORM_LINUX_SGX) && !defined(BH_PLATFORM_WINDOWS) \
  2390. && !defined(BH_PLATFORM_DARWIN)
  2391. /* address must be in the first 2 Gigabytes of
  2392. the process address space */
  2393. bh_assert((uintptr_t)aot_text < INT32_MAX);
  2394. #endif
  2395. #endif
  2396. bh_memcpy_s(aot_text, (uint32)total_size,
  2397. section->section_body, (uint32)section_size);
  2398. section->section_body = aot_text;
  2399. destroy_aot_text = true;
  2400. if ((uint32)total_size > section->section_body_size) {
  2401. memset(aot_text + (uint32)section_size, 0,
  2402. (uint32)total_size - section_size);
  2403. section->section_body_size = (uint32)total_size;
  2404. }
  2405. }
  2406. }
  2407. if (!section_list)
  2408. section_list = section_list_end = section;
  2409. else {
  2410. section_list_end->next = section;
  2411. section_list_end = section;
  2412. }
  2413. p += section_size;
  2414. }
  2415. else {
  2416. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2417. goto fail;
  2418. }
  2419. }
  2420. if (!section_list) {
  2421. set_error_buf(error_buf, error_buf_size, "create section list failed");
  2422. return false;
  2423. }
  2424. *p_section_list = section_list;
  2425. return true;
  2426. fail:
  2427. if (section_list)
  2428. destroy_sections(section_list, destroy_aot_text);
  2429. return false;
  2430. }
  2431. static bool
  2432. load(const uint8 *buf, uint32 size, AOTModule *module, char *error_buf,
  2433. uint32 error_buf_size)
  2434. {
  2435. const uint8 *buf_end = buf + size;
  2436. const uint8 *p = buf, *p_end = buf_end;
  2437. uint32 magic_number, version;
  2438. AOTSection *section_list = NULL;
  2439. bool ret;
  2440. read_uint32(p, p_end, magic_number);
  2441. if (magic_number != AOT_MAGIC_NUMBER) {
  2442. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  2443. return false;
  2444. }
  2445. read_uint32(p, p_end, version);
  2446. if (version != AOT_CURRENT_VERSION) {
  2447. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  2448. return false;
  2449. }
  2450. if (!create_sections(module, buf, size, &section_list, error_buf,
  2451. error_buf_size))
  2452. return false;
  2453. ret = load_from_sections(module, section_list, true, error_buf,
  2454. error_buf_size);
  2455. if (!ret) {
  2456. /* If load_from_sections() fails, then aot text is destroyed
  2457. in destroy_sections() */
  2458. destroy_sections(section_list, module->is_indirect_mode ? false : true);
  2459. /* aot_unload() won't destroy aot text again */
  2460. module->code = NULL;
  2461. }
  2462. else {
  2463. /* If load_from_sections() succeeds, then aot text is set to
  2464. module->code and will be destroyed in aot_unload() */
  2465. destroy_sections(section_list, false);
  2466. }
  2467. return ret;
  2468. fail:
  2469. return false;
  2470. }
  2471. AOTModule *
  2472. aot_load_from_aot_file(const uint8 *buf, uint32 size, char *error_buf,
  2473. uint32 error_buf_size)
  2474. {
  2475. AOTModule *module = create_module(error_buf, error_buf_size);
  2476. if (!module)
  2477. return NULL;
  2478. if (!load(buf, size, module, error_buf, error_buf_size)) {
  2479. aot_unload(module);
  2480. return NULL;
  2481. }
  2482. LOG_VERBOSE("Load module success.\n");
  2483. return module;
  2484. }
  2485. #if WASM_ENABLE_JIT != 0
  2486. #if WASM_ENABLE_LAZY_JIT != 0
  2487. /* Orc JIT thread arguments */
  2488. typedef struct OrcJitThreadArg {
  2489. AOTCompData *comp_data;
  2490. AOTCompContext *comp_ctx;
  2491. AOTModule *module;
  2492. int32 group_idx;
  2493. int32 group_stride;
  2494. } OrcJitThreadArg;
  2495. static bool orcjit_stop_compiling = false;
  2496. static korp_tid orcjit_threads[WASM_LAZY_JIT_COMPILE_THREAD_NUM];
  2497. static OrcJitThreadArg orcjit_thread_args[WASM_LAZY_JIT_COMPILE_THREAD_NUM];
  2498. static void *
  2499. orcjit_thread_callback(void *arg)
  2500. {
  2501. LLVMErrorRef error;
  2502. LLVMOrcJITTargetAddress func_addr = 0;
  2503. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  2504. AOTCompData *comp_data = thread_arg->comp_data;
  2505. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  2506. AOTModule *module = thread_arg->module;
  2507. char func_name[32];
  2508. int32 i;
  2509. /* Compile wasm functions of this group */
  2510. for (i = thread_arg->group_idx; i < (int32)comp_data->func_count;
  2511. i += thread_arg->group_stride) {
  2512. if (!module->func_ptrs[i]) {
  2513. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2514. if ((error = LLVMOrcLLJITLookup(comp_ctx->orc_lazyjit, &func_addr,
  2515. func_name))) {
  2516. char *err_msg = LLVMGetErrorMessage(error);
  2517. os_printf("failed to compile orc jit function: %s", err_msg);
  2518. LLVMDisposeErrorMessage(err_msg);
  2519. break;
  2520. }
  2521. /**
  2522. * No need to lock the func_ptr[func_idx] here as it is basic
  2523. * data type, the load/store for it can be finished by one cpu
  2524. * instruction, and there can be only one cpu instruction
  2525. * loading/storing at the same time.
  2526. */
  2527. module->func_ptrs[i] = (void *)func_addr;
  2528. }
  2529. if (orcjit_stop_compiling) {
  2530. break;
  2531. }
  2532. }
  2533. /* Try to compile functions that haven't been compiled by other threads */
  2534. for (i = (int32)comp_data->func_count - 1; i > 0; i--) {
  2535. if (orcjit_stop_compiling) {
  2536. break;
  2537. }
  2538. if (!module->func_ptrs[i]) {
  2539. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2540. if ((error = LLVMOrcLLJITLookup(comp_ctx->orc_lazyjit, &func_addr,
  2541. func_name))) {
  2542. char *err_msg = LLVMGetErrorMessage(error);
  2543. os_printf("failed to compile orc jit function: %s", err_msg);
  2544. LLVMDisposeErrorMessage(err_msg);
  2545. break;
  2546. }
  2547. module->func_ptrs[i] = (void *)func_addr;
  2548. }
  2549. }
  2550. return NULL;
  2551. }
  2552. static void
  2553. orcjit_stop_compile_threads()
  2554. {
  2555. uint32 i;
  2556. orcjit_stop_compiling = true;
  2557. for (i = 0; i < WASM_LAZY_JIT_COMPILE_THREAD_NUM; i++) {
  2558. os_thread_join(orcjit_threads[i], NULL);
  2559. }
  2560. }
  2561. #endif
  2562. static AOTModule *
  2563. aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
  2564. char *error_buf, uint32 error_buf_size)
  2565. {
  2566. uint32 i;
  2567. uint64 size;
  2568. char func_name[32];
  2569. AOTModule *module;
  2570. /* Allocate memory for module */
  2571. if (!(module =
  2572. loader_malloc(sizeof(AOTModule), error_buf, error_buf_size))) {
  2573. return NULL;
  2574. }
  2575. module->module_type = Wasm_Module_AoT;
  2576. module->import_memory_count = comp_data->import_memory_count;
  2577. module->import_memories = comp_data->import_memories;
  2578. module->memory_count = comp_data->memory_count;
  2579. if (module->memory_count) {
  2580. size = sizeof(AOTMemory) * (uint64)module->memory_count;
  2581. if (!(module->memories =
  2582. loader_malloc(size, error_buf, error_buf_size))) {
  2583. goto fail1;
  2584. }
  2585. bh_memcpy_s(module->memories, (uint32)size, comp_data->memories,
  2586. (uint32)size);
  2587. }
  2588. module->mem_init_data_list = comp_data->mem_init_data_list;
  2589. module->mem_init_data_count = comp_data->mem_init_data_count;
  2590. module->import_table_count = comp_data->import_table_count;
  2591. module->import_tables = comp_data->import_tables;
  2592. module->table_count = comp_data->table_count;
  2593. module->tables = comp_data->tables;
  2594. module->table_init_data_list = comp_data->table_init_data_list;
  2595. module->table_init_data_count = comp_data->table_init_data_count;
  2596. module->func_type_count = comp_data->func_type_count;
  2597. module->func_types = comp_data->func_types;
  2598. module->import_global_count = comp_data->import_global_count;
  2599. module->import_globals = comp_data->import_globals;
  2600. module->global_count = comp_data->global_count;
  2601. module->globals = comp_data->globals;
  2602. module->global_count = comp_data->global_count;
  2603. module->globals = comp_data->globals;
  2604. module->global_data_size = comp_data->global_data_size;
  2605. module->import_func_count = comp_data->import_func_count;
  2606. module->import_funcs = comp_data->import_funcs;
  2607. module->func_count = comp_data->func_count;
  2608. /* Allocate memory for function pointers */
  2609. size = (uint64)module->func_count * sizeof(void *);
  2610. if (size > 0
  2611. && !(module->func_ptrs =
  2612. loader_malloc(size, error_buf, error_buf_size))) {
  2613. goto fail2;
  2614. }
  2615. #if WASM_ENABLE_LAZY_JIT != 0
  2616. /* Create threads to compile the wasm functions */
  2617. for (i = 0; i < WASM_LAZY_JIT_COMPILE_THREAD_NUM; i++) {
  2618. orcjit_thread_args[i].comp_data = comp_data;
  2619. orcjit_thread_args[i].comp_ctx = comp_ctx;
  2620. orcjit_thread_args[i].module = module;
  2621. orcjit_thread_args[i].group_idx = (int32)i;
  2622. orcjit_thread_args[i].group_stride = WASM_LAZY_JIT_COMPILE_THREAD_NUM;
  2623. if (os_thread_create(&orcjit_threads[i], orcjit_thread_callback,
  2624. (void *)&orcjit_thread_args[i],
  2625. APP_THREAD_STACK_SIZE_DEFAULT)
  2626. != 0) {
  2627. uint32 j;
  2628. set_error_buf(error_buf, error_buf_size,
  2629. "create orcjit compile thread failed");
  2630. /* Terminate the threads created */
  2631. orcjit_stop_compiling = true;
  2632. for (j = 0; j < i; j++) {
  2633. os_thread_join(orcjit_threads[j], NULL);
  2634. }
  2635. goto fail3;
  2636. }
  2637. }
  2638. #else
  2639. /* Resolve function addresses */
  2640. bh_assert(comp_ctx->exec_engine);
  2641. for (i = 0; i < comp_data->func_count; i++) {
  2642. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2643. if (!(module->func_ptrs[i] = (void *)LLVMGetFunctionAddress(
  2644. comp_ctx->exec_engine, func_name))) {
  2645. set_error_buf(error_buf, error_buf_size,
  2646. "get function address failed");
  2647. goto fail3;
  2648. }
  2649. }
  2650. #endif /* WASM_ENABLE_LAZY_JIT != 0 */
  2651. /* Allocation memory for function type indexes */
  2652. size = (uint64)module->func_count * sizeof(uint32);
  2653. if (size > 0
  2654. && !(module->func_type_indexes =
  2655. loader_malloc(size, error_buf, error_buf_size))) {
  2656. goto fail4;
  2657. }
  2658. for (i = 0; i < comp_data->func_count; i++)
  2659. module->func_type_indexes[i] = comp_data->funcs[i]->func_type_index;
  2660. module->export_count = comp_data->wasm_module->export_count;
  2661. module->exports = comp_data->wasm_module->exports;
  2662. module->start_func_index = comp_data->start_func_index;
  2663. if (comp_data->start_func_index != (uint32)-1) {
  2664. bh_assert(comp_data->start_func_index
  2665. < module->import_func_count + module->func_count);
  2666. /* TODO: fix issue that start func cannot be import func */
  2667. if (comp_data->start_func_index >= module->import_func_count) {
  2668. #if WASM_ENABLE_LAZY_JIT != 0
  2669. if (!module->func_ptrs[comp_data->start_func_index
  2670. - module->import_func_count]) {
  2671. LLVMErrorRef error;
  2672. LLVMOrcJITTargetAddress func_addr = 0;
  2673. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  2674. comp_data->start_func_index
  2675. - module->import_func_count);
  2676. if ((error = LLVMOrcLLJITLookup(comp_ctx->orc_lazyjit,
  2677. &func_addr, func_name))) {
  2678. char *err_msg = LLVMGetErrorMessage(error);
  2679. set_error_buf_v(error_buf, error_buf_size,
  2680. "failed to compile orc jit function: %s",
  2681. err_msg);
  2682. LLVMDisposeErrorMessage(err_msg);
  2683. goto fail5;
  2684. }
  2685. module->func_ptrs[comp_data->start_func_index
  2686. - module->import_func_count] =
  2687. (void *)func_addr;
  2688. }
  2689. #endif
  2690. module->start_function =
  2691. module->func_ptrs[comp_data->start_func_index
  2692. - module->import_func_count];
  2693. }
  2694. }
  2695. module->malloc_func_index = comp_data->malloc_func_index;
  2696. module->free_func_index = comp_data->free_func_index;
  2697. module->retain_func_index = comp_data->retain_func_index;
  2698. module->aux_data_end_global_index = comp_data->aux_data_end_global_index;
  2699. module->aux_data_end = comp_data->aux_data_end;
  2700. module->aux_heap_base_global_index = comp_data->aux_heap_base_global_index;
  2701. module->aux_heap_base = comp_data->aux_heap_base;
  2702. module->aux_stack_top_global_index = comp_data->aux_stack_top_global_index;
  2703. module->aux_stack_bottom = comp_data->aux_stack_bottom;
  2704. module->aux_stack_size = comp_data->aux_stack_size;
  2705. module->code = NULL;
  2706. module->code_size = 0;
  2707. module->is_jit_mode = true;
  2708. module->wasm_module = comp_data->wasm_module;
  2709. module->comp_ctx = comp_ctx;
  2710. module->comp_data = comp_data;
  2711. #if WASM_ENABLE_LIBC_WASI != 0
  2712. module->import_wasi_api = comp_data->wasm_module->import_wasi_api;
  2713. #endif
  2714. return module;
  2715. #if WASM_ENABLE_LAZY_JIT != 0
  2716. fail5:
  2717. if (module->func_type_indexes)
  2718. wasm_runtime_free(module->func_type_indexes);
  2719. #endif
  2720. fail4:
  2721. #if WASM_ENABLE_LAZY_JIT != 0
  2722. /* Terminate all threads before free module->func_ptrs */
  2723. orcjit_stop_compile_threads();
  2724. #endif
  2725. fail3:
  2726. if (module->func_ptrs)
  2727. wasm_runtime_free(module->func_ptrs);
  2728. fail2:
  2729. if (module->memory_count > 0)
  2730. wasm_runtime_free(module->memories);
  2731. fail1:
  2732. wasm_runtime_free(module);
  2733. return NULL;
  2734. }
  2735. AOTModule *
  2736. aot_convert_wasm_module(WASMModule *wasm_module, char *error_buf,
  2737. uint32 error_buf_size)
  2738. {
  2739. AOTCompData *comp_data;
  2740. AOTCompContext *comp_ctx;
  2741. AOTModule *aot_module;
  2742. AOTCompOption option = { 0 };
  2743. char *aot_last_error;
  2744. comp_data = aot_create_comp_data(wasm_module);
  2745. if (!comp_data) {
  2746. aot_last_error = aot_get_last_error();
  2747. bh_assert(aot_last_error != NULL);
  2748. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2749. return NULL;
  2750. }
  2751. option.is_jit_mode = true;
  2752. option.opt_level = 3;
  2753. option.size_level = 3;
  2754. #if WASM_ENABLE_BULK_MEMORY != 0
  2755. option.enable_bulk_memory = true;
  2756. #endif
  2757. #if WASM_ENABLE_THREAD_MGR != 0
  2758. option.enable_thread_mgr = true;
  2759. #endif
  2760. #if WASM_ENABLE_TAIL_CALL != 0
  2761. option.enable_tail_call = true;
  2762. #endif
  2763. #if WASM_ENABLE_SIMD != 0
  2764. option.enable_simd = true;
  2765. #endif
  2766. #if WASM_ENABLE_REF_TYPES != 0
  2767. option.enable_ref_types = true;
  2768. #endif
  2769. option.enable_aux_stack_check = true;
  2770. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  2771. option.enable_aux_stack_frame = true;
  2772. #endif
  2773. comp_ctx = aot_create_comp_context(comp_data, &option);
  2774. if (!comp_ctx) {
  2775. aot_last_error = aot_get_last_error();
  2776. bh_assert(aot_last_error != NULL);
  2777. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2778. goto fail1;
  2779. }
  2780. if (!aot_compile_wasm(comp_ctx)) {
  2781. aot_last_error = aot_get_last_error();
  2782. bh_assert(aot_last_error != NULL);
  2783. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2784. goto fail2;
  2785. }
  2786. aot_module =
  2787. aot_load_from_comp_data(comp_data, comp_ctx, error_buf, error_buf_size);
  2788. if (!aot_module) {
  2789. goto fail2;
  2790. }
  2791. return aot_module;
  2792. fail2:
  2793. aot_destroy_comp_context(comp_ctx);
  2794. fail1:
  2795. aot_destroy_comp_data(comp_data);
  2796. return NULL;
  2797. }
  2798. #endif
  2799. void
  2800. aot_unload(AOTModule *module)
  2801. {
  2802. #if WASM_ENABLE_JIT != 0
  2803. #if WASM_ENABLE_LAZY_JIT != 0
  2804. orcjit_stop_compile_threads();
  2805. #endif
  2806. if (module->comp_data)
  2807. aot_destroy_comp_data(module->comp_data);
  2808. if (module->comp_ctx)
  2809. aot_destroy_comp_context(module->comp_ctx);
  2810. if (module->wasm_module)
  2811. wasm_loader_unload(module->wasm_module);
  2812. #endif
  2813. if (module->import_memories)
  2814. destroy_import_memories(module->import_memories, module->is_jit_mode);
  2815. if (module->memories)
  2816. wasm_runtime_free(module->memories);
  2817. if (module->mem_init_data_list)
  2818. destroy_mem_init_data_list(module->mem_init_data_list,
  2819. module->mem_init_data_count,
  2820. module->is_jit_mode);
  2821. if (module->native_symbol_list)
  2822. wasm_runtime_free(module->native_symbol_list);
  2823. if (module->import_tables)
  2824. destroy_import_tables(module->import_tables, module->is_jit_mode);
  2825. if (module->tables)
  2826. destroy_tables(module->tables, module->is_jit_mode);
  2827. if (module->table_init_data_list)
  2828. destroy_table_init_data_list(module->table_init_data_list,
  2829. module->table_init_data_count,
  2830. module->is_jit_mode);
  2831. if (module->func_types)
  2832. destroy_func_types(module->func_types, module->func_type_count,
  2833. module->is_jit_mode);
  2834. if (module->import_globals)
  2835. destroy_import_globals(module->import_globals, module->is_jit_mode);
  2836. if (module->globals)
  2837. destroy_globals(module->globals, module->is_jit_mode);
  2838. if (module->import_funcs)
  2839. destroy_import_funcs(module->import_funcs, module->is_jit_mode);
  2840. if (module->exports)
  2841. destroy_exports(module->exports, module->is_jit_mode);
  2842. if (module->func_type_indexes)
  2843. wasm_runtime_free(module->func_type_indexes);
  2844. if (module->func_ptrs)
  2845. wasm_runtime_free(module->func_ptrs);
  2846. if (module->const_str_set)
  2847. bh_hash_map_destroy(module->const_str_set);
  2848. if (module->code && !module->is_indirect_mode) {
  2849. /* The layout is: literal size + literal + code (with plt table) */
  2850. uint8 *mmap_addr = module->literal - sizeof(uint32);
  2851. uint32 total_size =
  2852. sizeof(uint32) + module->literal_size + module->code_size;
  2853. os_munmap(mmap_addr, total_size);
  2854. }
  2855. #if defined(BH_PLATFORM_WINDOWS)
  2856. if (module->extra_plt_data) {
  2857. os_munmap(module->extra_plt_data, module->extra_plt_data_size);
  2858. }
  2859. #endif
  2860. #if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)
  2861. if (module->rtl_func_table) {
  2862. if (module->rtl_func_table_registered)
  2863. RtlDeleteFunctionTable(module->rtl_func_table);
  2864. wasm_runtime_free(module->rtl_func_table);
  2865. }
  2866. #endif
  2867. if (module->data_sections)
  2868. destroy_object_data_sections(module->data_sections,
  2869. module->data_section_count);
  2870. #if WASM_ENABLE_DEBUG_AOT != 0
  2871. jit_code_entry_destroy(module->elf_hdr);
  2872. #endif
  2873. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2874. if (module->aux_func_indexes) {
  2875. wasm_runtime_free(module->aux_func_indexes);
  2876. }
  2877. if (module->aux_func_names) {
  2878. wasm_runtime_free((void *)module->aux_func_names);
  2879. }
  2880. #endif
  2881. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  2882. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  2883. #endif
  2884. wasm_runtime_free(module);
  2885. }
  2886. uint32
  2887. aot_get_plt_table_size()
  2888. {
  2889. return get_plt_table_size();
  2890. }
  2891. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  2892. const uint8 *
  2893. aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len)
  2894. {
  2895. WASMCustomSection *section = module->custom_section_list;
  2896. while (section) {
  2897. if (strcmp(section->name_addr, name) == 0) {
  2898. if (len) {
  2899. *len = section->content_len;
  2900. }
  2901. return section->content_addr;
  2902. }
  2903. section = section->next;
  2904. }
  2905. return NULL;
  2906. }
  2907. #endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION */