wasm_loader.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "wasm.h"
  7. #include "wasm_native.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_log.h"
  11. #include "wasm_memory.h"
  12. #include "wasm_dlfcn.h"
  13. #include "bh_common.h"
  14. /* Read a value of given type from the address pointed to by the given
  15. pointer and increase the pointer to the position just after the
  16. value being read. */
  17. #define TEMPLATE_READ_VALUE(Type, p) \
  18. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  19. static void
  20. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  21. {
  22. if (error_buf != NULL)
  23. snprintf(error_buf, error_buf_size, "%s", string);
  24. }
  25. #define CHECK_BUF(buf, buf_end, length) do { \
  26. if (buf + length > buf_end) { \
  27. set_error_buf(error_buf, error_buf_size, "unexpected end"); \
  28. return false; \
  29. } \
  30. } while (0)
  31. static bool
  32. read_leb(const uint8 *buf, const uint8 *buf_end,
  33. uint32 *p_offset, uint32 maxbits,
  34. bool sign, uint64 *p_result,
  35. char* error_buf, uint32 error_buf_size)
  36. {
  37. uint64 result = 0;
  38. uint32 shift = 0;
  39. uint32 bcnt = 0;
  40. uint64 byte;
  41. while (true) {
  42. CHECK_BUF(buf, buf_end, 1);
  43. byte = buf[*p_offset];
  44. *p_offset += 1;
  45. result |= ((byte & 0x7f) << shift);
  46. shift += 7;
  47. bcnt += 1;
  48. if ((byte & 0x80) == 0) {
  49. break;
  50. }
  51. }
  52. if (bcnt > (maxbits + 7 - 1) / 7) {
  53. set_error_buf(error_buf, error_buf_size,
  54. "integer representation too long");
  55. return false;
  56. }
  57. if (sign && (shift < maxbits) && (byte & 0x40)) {
  58. /* Sign extend */
  59. result |= - ((uint64)1 << shift);
  60. }
  61. *p_result = result;
  62. return true;
  63. }
  64. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  65. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  66. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  67. #define read_leb_uint64(p, p_end, res) do { \
  68. uint32 off = 0; \
  69. uint64 res64; \
  70. if (!read_leb(p, p_end, &off, 64, false, &res64, \
  71. error_buf, error_buf_size)) \
  72. return false; \
  73. p += off; \
  74. res = (uint64)res64; \
  75. } while (0)
  76. #define read_leb_int64(p, p_end, res) do { \
  77. uint32 off = 0; \
  78. uint64 res64; \
  79. if (!read_leb(p, p_end, &off, 64, true, &res64, \
  80. error_buf, error_buf_size)) \
  81. return false; \
  82. p += off; \
  83. res = (int64)res64; \
  84. } while (0)
  85. #define read_leb_uint32(p, p_end, res) do { \
  86. uint32 off = 0; \
  87. uint64 res64; \
  88. if (!read_leb(p, p_end, &off, 32, false, &res64, \
  89. error_buf, error_buf_size)) \
  90. return false; \
  91. p += off; \
  92. res = (uint32)res64; \
  93. } while (0)
  94. #define read_leb_int32(p, p_end, res) do { \
  95. uint32 off = 0; \
  96. uint64 res64; \
  97. if (!read_leb(p, p_end, &off, 32, true, &res64, \
  98. error_buf, error_buf_size)) \
  99. return false; \
  100. p += off; \
  101. res = (int32)res64; \
  102. } while (0)
  103. #define read_leb_uint8(p, p_end, res) do { \
  104. uint32 off = 0; \
  105. uint64 res64; \
  106. if (!read_leb(p, p_end, &off, 7, false, &res64, \
  107. error_buf, error_buf_size)) \
  108. return false; \
  109. p += off; \
  110. res = (uint8)res64; \
  111. } while (0)
  112. static char*
  113. const_str_set_insert(const uint8 *str, uint32 len, WASMModule *module,
  114. char* error_buf, uint32 error_buf_size)
  115. {
  116. HashMap *set = module->const_str_set;
  117. char *c_str = wasm_malloc(len + 1), *value;
  118. if (!c_str) {
  119. set_error_buf(error_buf, error_buf_size,
  120. "WASM module load failed: "
  121. "allocate memory failed.");
  122. return NULL;
  123. }
  124. bh_memcpy_s(c_str, len + 1, str, len);
  125. c_str[len] = '\0';
  126. if ((value = wasm_hash_map_find(set, c_str))) {
  127. wasm_free(c_str);
  128. return value;
  129. }
  130. if (!wasm_hash_map_insert(set, c_str, c_str)) {
  131. set_error_buf(error_buf, error_buf_size,
  132. "WASM module load failed: "
  133. "insert string to hash map failed.");
  134. wasm_free(c_str);
  135. return NULL;
  136. }
  137. return c_str;
  138. }
  139. static bool
  140. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  141. InitializerExpression *init_expr,
  142. char *error_buf, uint32 error_buf_size)
  143. {
  144. const uint8 *p = *p_buf, *p_end = buf_end;
  145. uint8 flag, end_byte, *p_float;
  146. uint32 i;
  147. CHECK_BUF(p, p_end, 1);
  148. init_expr->init_expr_type = read_uint8(p);
  149. flag = init_expr->init_expr_type;
  150. switch (flag) {
  151. /* i32.const */
  152. case INIT_EXPR_TYPE_I32_CONST:
  153. read_leb_int32(p, p_end, init_expr->u.i32);
  154. break;
  155. /* i64.const */
  156. case INIT_EXPR_TYPE_I64_CONST:
  157. read_leb_int64(p, p_end, init_expr->u.i64);
  158. break;
  159. /* f32.const */
  160. case INIT_EXPR_TYPE_F32_CONST:
  161. CHECK_BUF(p, p_end, 4);
  162. p_float = (uint8*)&init_expr->u.f32;
  163. for (i = 0; i < sizeof(float32); i++)
  164. *p_float++ = *p++;
  165. break;
  166. /* f64.const */
  167. case INIT_EXPR_TYPE_F64_CONST:
  168. CHECK_BUF(p, p_end, 8);
  169. p_float = (uint8*)&init_expr->u.f64;
  170. for (i = 0; i < sizeof(float64); i++)
  171. *p_float++ = *p++;
  172. break;
  173. /* get_global */
  174. case INIT_EXPR_TYPE_GET_GLOBAL:
  175. read_leb_uint32(p, p_end, init_expr->u.global_index);
  176. break;
  177. default:
  178. set_error_buf(error_buf, error_buf_size, "type mismatch");
  179. return false;
  180. }
  181. CHECK_BUF(p, p_end, 1);
  182. end_byte = read_uint8(p);
  183. if (end_byte != 0x0b) {
  184. set_error_buf(error_buf, error_buf_size, "unexpected end");
  185. return false;
  186. }
  187. *p_buf = p;
  188. return true;
  189. }
  190. static bool
  191. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  192. char *error_buf, uint32 error_buf_size)
  193. {
  194. const uint8 *p = buf, *p_end = buf_end, *p_org;
  195. uint32 type_count, param_count, result_count, i, j;
  196. uint64 total_size;
  197. uint8 flag;
  198. WASMType *type;
  199. read_leb_uint32(p, p_end, type_count);
  200. if (type_count) {
  201. module->type_count = type_count;
  202. total_size = sizeof(WASMType*) * (uint64)type_count;
  203. if (total_size >= UINT32_MAX
  204. || !(module->types = wasm_malloc((uint32)total_size))) {
  205. set_error_buf(error_buf, error_buf_size,
  206. "Load type section failed: allocate memory failed.");
  207. return false;
  208. }
  209. memset(module->types, 0, (uint32)total_size);
  210. for (i = 0; i < type_count; i++) {
  211. CHECK_BUF(p, p_end, 1);
  212. flag = read_uint8(p);
  213. if (flag != 0x60) {
  214. set_error_buf(error_buf, error_buf_size,
  215. "Load type section failed: invalid type flag.");
  216. return false;
  217. }
  218. read_leb_uint32(p, p_end, param_count);
  219. /* Resolve param count and result count firstly */
  220. p_org = p;
  221. CHECK_BUF(p, p_end, param_count);
  222. p += param_count;
  223. read_leb_uint32(p, p_end, result_count);
  224. if (result_count > 1) {
  225. set_error_buf(error_buf, error_buf_size,
  226. "Load type section failed: invalid result count.");
  227. return false;
  228. }
  229. CHECK_BUF(p, p_end, result_count);
  230. p = p_org;
  231. total_size = offsetof(WASMType, types) +
  232. sizeof(uint8) * (uint64)(param_count + result_count);
  233. if (total_size >= UINT32_MAX
  234. || !(type = module->types[i] = wasm_malloc((uint32)total_size))) {
  235. set_error_buf(error_buf, error_buf_size,
  236. "Load type section failed: allocate memory failed.");
  237. return false;
  238. }
  239. /* Resolve param types and result types */
  240. type->param_count = param_count;
  241. type->result_count = result_count;
  242. for (j = 0; j < param_count; j++) {
  243. CHECK_BUF(p, p_end, 1);
  244. type->types[j] = read_uint8(p);
  245. }
  246. read_leb_uint32(p, p_end, result_count);
  247. for (j = 0; j < result_count; j++) {
  248. CHECK_BUF(p, p_end, 1);
  249. type->types[param_count + j] = read_uint8(p);
  250. }
  251. }
  252. }
  253. if (p != p_end) {
  254. set_error_buf(error_buf, error_buf_size,
  255. "Load type section failed: invalid section size.");
  256. return false;
  257. }
  258. LOG_VERBOSE("Load type section success.\n");
  259. return true;
  260. }
  261. static bool
  262. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  263. WASMTableImport *table,
  264. char *error_buf, uint32 error_buf_size)
  265. {
  266. const uint8 *p = *p_buf, *p_end = buf_end;
  267. read_leb_uint8(p, p_end, table->elem_type);
  268. wasm_assert(table->elem_type == TABLE_ELEM_TYPE_ANY_FUNC);
  269. read_leb_uint32(p, p_end, table->flags);
  270. read_leb_uint32(p, p_end, table->init_size);
  271. if (table->flags & 1)
  272. read_leb_uint32(p, p_end, table->max_size);
  273. else
  274. table->max_size = 0x10000;
  275. *p_buf = p;
  276. return true;
  277. }
  278. static bool
  279. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  280. WASMMemoryImport *memory,
  281. char *error_buf, uint32 error_buf_size)
  282. {
  283. const uint8 *p = *p_buf, *p_end = buf_end;
  284. uint32 pool_size = bh_memory_pool_size();
  285. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  286. / NumBytesPerPage;
  287. read_leb_uint32(p, p_end, memory->flags);
  288. read_leb_uint32(p, p_end, memory->init_page_count);
  289. if (memory->flags & 1) {
  290. read_leb_uint32(p, p_end, memory->max_page_count);
  291. if (memory->max_page_count > max_page_count)
  292. memory->max_page_count = max_page_count;
  293. }
  294. else
  295. /* Limit the maximum memory size to max_page_count */
  296. memory->max_page_count = max_page_count;
  297. *p_buf = p;
  298. return true;
  299. }
  300. static bool
  301. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  302. char *error_buf, uint32 error_buf_size)
  303. {
  304. const uint8 *p = *p_buf, *p_end = buf_end;
  305. read_leb_uint8(p, p_end, table->elem_type);
  306. wasm_assert(table->elem_type == TABLE_ELEM_TYPE_ANY_FUNC);
  307. read_leb_uint32(p, p_end, table->flags);
  308. read_leb_uint32(p, p_end, table->init_size);
  309. if (table->flags & 1)
  310. read_leb_uint32(p, p_end, table->max_size);
  311. else
  312. table->max_size = 0x10000;
  313. *p_buf = p;
  314. return true;
  315. }
  316. static bool
  317. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  318. char *error_buf, uint32 error_buf_size)
  319. {
  320. const uint8 *p = *p_buf, *p_end = buf_end;
  321. uint32 pool_size = bh_memory_pool_size();
  322. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  323. / NumBytesPerPage;
  324. read_leb_uint32(p, p_end, memory->flags);
  325. read_leb_uint32(p, p_end, memory->init_page_count);
  326. if (memory->flags & 1) {
  327. read_leb_uint32(p, p_end, memory->max_page_count);
  328. if (memory->max_page_count > max_page_count)
  329. memory->max_page_count = max_page_count;
  330. }
  331. else
  332. /* Limit the maximum memory size to max_page_count */
  333. memory->max_page_count = max_page_count;
  334. *p_buf = p;
  335. return true;
  336. }
  337. static void*
  338. resolve_sym(const char *module_name, const char *field_name)
  339. {
  340. void *sym;
  341. if (strcmp(module_name, "env") != 0)
  342. return NULL;
  343. if (field_name[0] == '_'
  344. && (sym = wasm_dlsym(NULL, field_name + 1)))
  345. return sym;
  346. if ((sym = wasm_dlsym(NULL, field_name)))
  347. return sym;
  348. return NULL;
  349. }
  350. static bool
  351. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  352. char *error_buf, uint32 error_buf_size)
  353. {
  354. const uint8 *p = buf, *p_end = buf_end, *p_old;
  355. uint32 import_count, name_len, type_index, i, u32, flags;
  356. uint64 total_size;
  357. WASMImport *import;
  358. WASMImport *import_functions = NULL, *import_tables = NULL;
  359. WASMImport *import_memories = NULL, *import_globals = NULL;
  360. char *module_name, *field_name;
  361. uint8 mutable, u8, kind;
  362. read_leb_uint32(p, p_end, import_count);
  363. if (import_count) {
  364. module->import_count = import_count;
  365. total_size = sizeof(WASMImport) * (uint64)import_count;
  366. if (total_size >= UINT32_MAX
  367. || !(module->imports = wasm_malloc((uint32)total_size))) {
  368. set_error_buf(error_buf, error_buf_size,
  369. "Load import section failed: allocate memory failed.");
  370. return false;
  371. }
  372. memset(module->imports, 0, (uint32)total_size);
  373. p_old = p;
  374. /* Scan firstly to get import count of each type */
  375. for (i = 0; i < import_count; i++) {
  376. /* module name */
  377. read_leb_uint32(p, p_end, name_len);
  378. CHECK_BUF(p, p_end, name_len);
  379. p += name_len;
  380. /* field name */
  381. read_leb_uint32(p, p_end, name_len);
  382. CHECK_BUF(p, p_end, name_len);
  383. p += name_len;
  384. read_leb_uint8(p, p_end, kind);
  385. switch (kind) {
  386. case IMPORT_KIND_FUNC: /* import function */
  387. read_leb_uint32(p, p_end, type_index);
  388. module->import_function_count++;
  389. break;
  390. case IMPORT_KIND_TABLE: /* import table */
  391. read_leb_uint8(p, p_end, u8);
  392. read_leb_uint32(p, p_end, flags);
  393. read_leb_uint32(p, p_end, u32);
  394. if (flags & 1)
  395. read_leb_uint32(p, p_end, u32);
  396. module->import_table_count++;
  397. if (module->import_table_count > 1) {
  398. set_error_buf(error_buf, error_buf_size, "multiple tables");
  399. return false;
  400. }
  401. break;
  402. case IMPORT_KIND_MEMORY: /* import memory */
  403. read_leb_uint32(p, p_end, flags);
  404. read_leb_uint32(p, p_end, u32);
  405. if (flags & 1)
  406. read_leb_uint32(p, p_end, u32);
  407. module->import_memory_count++;
  408. if (module->import_memory_count > 1) {
  409. set_error_buf(error_buf, error_buf_size, "multiple memories");
  410. return false;
  411. }
  412. break;
  413. case IMPORT_KIND_GLOBAL: /* import global */
  414. read_leb_uint8(p, p_end, u8);
  415. read_leb_uint8(p, p_end, u8);
  416. module->import_global_count++;
  417. break;
  418. default:
  419. set_error_buf(error_buf, error_buf_size,
  420. "Load import section failed: invalid import type.");
  421. return false;
  422. }
  423. }
  424. if (module->import_function_count)
  425. import_functions = module->import_functions = module->imports;
  426. if (module->import_table_count)
  427. import_tables = module->import_tables =
  428. module->imports + module->import_function_count;
  429. if (module->import_memory_count)
  430. import_memories = module->import_memories =
  431. module->imports + module->import_function_count + module->import_table_count;
  432. if (module->import_global_count)
  433. import_globals = module->import_globals =
  434. module->imports + module->import_function_count + module->import_table_count
  435. + module->import_memory_count;
  436. p = p_old;
  437. /* Scan again to read the data */
  438. for (i = 0; i < import_count; i++) {
  439. /* load module name */
  440. read_leb_uint32(p, p_end, name_len);
  441. CHECK_BUF(p, p_end, name_len);
  442. if (!(module_name = const_str_set_insert
  443. (p, name_len, module, error_buf, error_buf_size))) {
  444. return false;
  445. }
  446. p += name_len;
  447. /* load field name */
  448. read_leb_uint32(p, p_end, name_len);
  449. CHECK_BUF(p, p_end, name_len);
  450. if (!(field_name = const_str_set_insert
  451. (p, name_len, module, error_buf, error_buf_size))) {
  452. return false;
  453. }
  454. p += name_len;
  455. read_leb_uint8(p, p_end, kind);
  456. switch (kind) {
  457. case IMPORT_KIND_FUNC: /* import function */
  458. wasm_assert(import_functions);
  459. import = import_functions++;
  460. read_leb_uint32(p, p_end, type_index);
  461. if (type_index >= module->type_count) {
  462. set_error_buf(error_buf, error_buf_size,
  463. "Load import section failed: "
  464. "invalid function type index.");
  465. return false;
  466. }
  467. import->u.function.func_type = module->types[type_index];
  468. if (!(import->u.function.func_ptr_linked = wasm_native_func_lookup
  469. (module_name, field_name))) {
  470. if (!(import->u.function.func_ptr_linked =
  471. resolve_sym(module_name, field_name))) {
  472. if (error_buf != NULL)
  473. snprintf(error_buf, error_buf_size,
  474. "Load import section failed: "
  475. "resolve import function (%s, %s) failed.",
  476. module_name, field_name);
  477. return false;
  478. }
  479. import->u.function.call_type = CALL_TYPE_C_INTRINSIC;
  480. break;
  481. }
  482. import->u.function.call_type = CALL_TYPE_WRAPPER;
  483. break;
  484. case IMPORT_KIND_TABLE: /* import table */
  485. wasm_assert(import_tables);
  486. import = import_tables++;
  487. if (!load_table_import(&p, p_end, &import->u.table,
  488. error_buf, error_buf_size))
  489. return false;
  490. if (module->import_table_count > 1) {
  491. set_error_buf(error_buf, error_buf_size, "multiple tables");
  492. return false;
  493. }
  494. break;
  495. case IMPORT_KIND_MEMORY: /* import memory */
  496. wasm_assert(import_memories);
  497. import = import_memories++;
  498. if (!load_memory_import(&p, p_end, &import->u.memory,
  499. error_buf, error_buf_size))
  500. return false;
  501. if (module->import_table_count > 1) {
  502. set_error_buf(error_buf, error_buf_size, "multiple memories");
  503. return false;
  504. }
  505. break;
  506. case IMPORT_KIND_GLOBAL: /* import global */
  507. wasm_assert(import_globals);
  508. import = import_globals++;
  509. read_leb_uint8(p, p_end, import->u.global.type);
  510. read_leb_uint8(p, p_end, mutable);
  511. import->u.global.is_mutable = mutable & 1 ? true : false;
  512. if (!(wasm_native_global_lookup(module_name, field_name,
  513. &import->u.global))) {
  514. if (error_buf != NULL)
  515. snprintf(error_buf, error_buf_size,
  516. "Load import section failed: "
  517. "resolve import global (%s, %s) failed.",
  518. module_name, field_name);
  519. return false;
  520. }
  521. break;
  522. default:
  523. set_error_buf(error_buf, error_buf_size,
  524. "Load import section failed: "
  525. "invalid import type.");
  526. return false;
  527. }
  528. import->kind = kind;
  529. import->u.names.module_name = module_name;
  530. import->u.names.field_name = field_name;
  531. }
  532. #if WASM_ENABLE_WASI != 0
  533. import = module->import_functions;
  534. for (i = 0; i < module->import_function_count; i++, import++) {
  535. if (!strcmp(import->u.names.module_name, "wasi_unstable")) {
  536. module->is_wasi_module = true;
  537. break;
  538. }
  539. }
  540. #endif
  541. }
  542. if (p != p_end) {
  543. set_error_buf(error_buf, error_buf_size,
  544. "Load import section failed: "
  545. "invalid section size.");
  546. return false;
  547. }
  548. LOG_VERBOSE("Load import section success.\n");
  549. (void)u8;
  550. (void)u32;
  551. return true;
  552. }
  553. static bool
  554. load_function_section(const uint8 *buf, const uint8 *buf_end,
  555. const uint8 *buf_code, const uint8 *buf_code_end,
  556. WASMModule *module,
  557. char *error_buf, uint32 error_buf_size)
  558. {
  559. const uint8 *p = buf, *p_end = buf_end;
  560. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  561. uint32 func_count;
  562. uint64 total_size;
  563. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  564. uint32 local_count, local_set_count, sub_local_count;
  565. uint8 type;
  566. WASMFunction *func;
  567. read_leb_uint32(p, p_end, func_count);
  568. if (buf_code)
  569. read_leb_uint32(p_code, buf_code_end, code_count);
  570. if (func_count != code_count) {
  571. set_error_buf(error_buf, error_buf_size,
  572. "function and code section have inconsistent lengths");
  573. return false;
  574. }
  575. if (func_count) {
  576. module->function_count = func_count;
  577. total_size = sizeof(WASMFunction*) * (uint64)func_count;
  578. if (total_size >= UINT32_MAX
  579. || !(module->functions = wasm_malloc((uint32)total_size))) {
  580. set_error_buf(error_buf, error_buf_size,
  581. "Load function section failed: allocate memory failed.");
  582. return false;
  583. }
  584. memset(module->functions, 0, (uint32)total_size);
  585. for (i = 0; i < func_count; i++) {
  586. /* Resolve function type */
  587. read_leb_uint32(p, p_end, type_index);
  588. if (type_index >= module->type_count) {
  589. set_error_buf(error_buf, error_buf_size,
  590. "Load function section failed: "
  591. "invalid function type index.");
  592. return false;
  593. }
  594. read_leb_uint32(p_code, buf_code_end, code_size);
  595. if (code_size == 0
  596. || p_code + code_size > buf_code_end) {
  597. set_error_buf(error_buf, error_buf_size,
  598. "Load function section failed: "
  599. "invalid function code size.");
  600. return false;
  601. }
  602. /* Resolve local set count */
  603. p_code_end = p_code + code_size;
  604. local_count = 0;
  605. read_leb_uint32(p_code, buf_code_end, local_set_count);
  606. p_code_save = p_code;
  607. /* Calculate total local count */
  608. for (j = 0; j < local_set_count; j++) {
  609. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  610. if (sub_local_count > UINT32_MAX - local_count) {
  611. set_error_buf(error_buf, error_buf_size,
  612. "too many locals");
  613. return false;
  614. }
  615. read_leb_uint8(p_code, buf_code_end, type);
  616. local_count += sub_local_count;
  617. }
  618. /* Alloc memory, layout: function structure + local types */
  619. code_size = (uint32)(p_code_end - p_code);
  620. total_size = sizeof(WASMFunction) + (uint64)local_count;
  621. if (total_size >= UINT32_MAX
  622. || !(func = module->functions[i] = wasm_malloc((uint32)total_size))) {
  623. set_error_buf(error_buf, error_buf_size,
  624. "Load function section failed: "
  625. "allocate memory failed.");
  626. return false;
  627. }
  628. /* Set function type, local count, code size and code body */
  629. memset(func, 0, (uint32)total_size);
  630. func->func_type = module->types[type_index];
  631. func->local_count = local_count;
  632. if (local_count > 0)
  633. func->local_types = (uint8*)func + sizeof(WASMFunction);
  634. func->code_size = code_size;
  635. func->code = (uint8*)p_code;
  636. /* Load each local type */
  637. p_code = p_code_save;
  638. local_type_index = 0;
  639. for (j = 0; j < local_set_count; j++) {
  640. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  641. if (local_type_index + sub_local_count <= local_type_index
  642. || local_type_index + sub_local_count > local_count) {
  643. set_error_buf(error_buf, error_buf_size,
  644. "Load function section failed: "
  645. "invalid local count.");
  646. return false;
  647. }
  648. read_leb_uint8(p_code, buf_code_end, type);
  649. if (type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32) {
  650. set_error_buf(error_buf, error_buf_size,
  651. "Load function section failed: "
  652. "invalid local type.");
  653. return false;
  654. }
  655. for (k = 0; k < sub_local_count; k++) {
  656. func->local_types[local_type_index++] = type;
  657. }
  658. }
  659. p_code = p_code_end;
  660. }
  661. }
  662. if (p != p_end) {
  663. set_error_buf(error_buf, error_buf_size,
  664. "Load function section failed: "
  665. "invalid section size.");
  666. return false;
  667. }
  668. LOG_VERBOSE("Load function section success.\n");
  669. return true;
  670. }
  671. static bool
  672. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  673. char *error_buf, uint32 error_buf_size)
  674. {
  675. const uint8 *p = buf, *p_end = buf_end;
  676. uint32 table_count, i;
  677. uint64 total_size;
  678. WASMTable *table;
  679. read_leb_uint32(p, p_end, table_count);
  680. wasm_assert(table_count == 1);
  681. if (table_count) {
  682. if (table_count > 1) {
  683. set_error_buf(error_buf, error_buf_size, "multiple memories");
  684. return false;
  685. }
  686. module->table_count = table_count;
  687. total_size = sizeof(WASMTable) * (uint64)table_count;
  688. if (total_size >= UINT32_MAX
  689. || !(module->tables = wasm_malloc((uint32)total_size))) {
  690. set_error_buf(error_buf, error_buf_size,
  691. "Load table section failed: "
  692. "allocate memory failed.");
  693. return false;
  694. }
  695. memset(module->tables, 0, (uint32)total_size);
  696. /* load each table */
  697. table = module->tables;
  698. for (i = 0; i < table_count; i++, table++)
  699. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  700. return false;
  701. }
  702. if (p != p_end) {
  703. set_error_buf(error_buf, error_buf_size,
  704. "Load table section failed: invalid section size.");
  705. return false;
  706. }
  707. LOG_VERBOSE("Load table section success.\n");
  708. return true;
  709. }
  710. static bool
  711. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  712. char *error_buf, uint32 error_buf_size)
  713. {
  714. const uint8 *p = buf, *p_end = buf_end;
  715. uint32 memory_count, i;
  716. uint64 total_size;
  717. WASMMemory *memory;
  718. read_leb_uint32(p, p_end, memory_count);
  719. wasm_assert(memory_count == 1);
  720. if (memory_count) {
  721. if (memory_count > 1) {
  722. set_error_buf(error_buf, error_buf_size, "multiple memories");
  723. return false;
  724. }
  725. module->memory_count = memory_count;
  726. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  727. if (total_size >= UINT32_MAX
  728. || !(module->memories = wasm_malloc((uint32)total_size))) {
  729. set_error_buf(error_buf, error_buf_size,
  730. "Load memory section failed: "
  731. "allocate memory failed.");
  732. return false;
  733. }
  734. memset(module->memories, 0, (uint32)total_size);
  735. /* load each memory */
  736. memory = module->memories;
  737. for (i = 0; i < memory_count; i++, memory++)
  738. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  739. return false;
  740. }
  741. if (p != p_end) {
  742. set_error_buf(error_buf, error_buf_size,
  743. "Load memory section failed: invalid section size.");
  744. return false;
  745. }
  746. LOG_VERBOSE("Load memory section success.\n");
  747. return true;
  748. }
  749. static bool
  750. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  751. char *error_buf, uint32 error_buf_size)
  752. {
  753. const uint8 *p = buf, *p_end = buf_end;
  754. uint32 global_count, i;
  755. uint64 total_size;
  756. WASMGlobal *global;
  757. read_leb_uint32(p, p_end, global_count);
  758. if (global_count) {
  759. module->global_count = global_count;
  760. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  761. if (total_size >= UINT32_MAX
  762. || !(module->globals = wasm_malloc((uint32)total_size))) {
  763. set_error_buf(error_buf, error_buf_size,
  764. "Load global section failed: "
  765. "allocate memory failed.");
  766. return false;
  767. }
  768. memset(module->globals, 0, (uint32)total_size);
  769. global = module->globals;
  770. for(i = 0; i < global_count; i++, global++) {
  771. CHECK_BUF(p, p_end, 1);
  772. global->type = read_uint8(p);
  773. CHECK_BUF(p, p_end, 1);
  774. global->is_mutable = read_bool(p);
  775. /* initialize expression */
  776. if (!load_init_expr(&p, p_end, &(global->init_expr), error_buf, error_buf_size))
  777. return false;
  778. }
  779. }
  780. if (p != p_end) {
  781. set_error_buf(error_buf, error_buf_size,
  782. "Load global section failed: invalid section size.");
  783. return false;
  784. }
  785. LOG_VERBOSE("Load global section success.\n");
  786. return true;
  787. }
  788. static bool
  789. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  790. char *error_buf, uint32 error_buf_size)
  791. {
  792. const uint8 *p = buf, *p_end = buf_end;
  793. uint32 export_count, i, index;
  794. uint64 total_size;
  795. uint32 str_len;
  796. WASMExport *export;
  797. read_leb_uint32(p, p_end, export_count);
  798. if (export_count) {
  799. module->export_count = export_count;
  800. total_size = sizeof(WASMExport) * (uint64)export_count;
  801. if (total_size >= UINT32_MAX
  802. || !(module->exports = wasm_malloc((uint32)total_size))) {
  803. set_error_buf(error_buf, error_buf_size,
  804. "Load export section failed: "
  805. "allocate memory failed.");
  806. return false;
  807. }
  808. memset(module->exports, 0, (uint32)total_size);
  809. export = module->exports;
  810. for (i = 0; i < export_count; i++, export++) {
  811. read_leb_uint32(p, p_end, str_len);
  812. CHECK_BUF(p, p_end, str_len);
  813. if (!(export->name = const_str_set_insert(p, str_len, module,
  814. error_buf, error_buf_size))) {
  815. return false;
  816. }
  817. p += str_len;
  818. CHECK_BUF(p, p_end, 1);
  819. export->kind = read_uint8(p);
  820. read_leb_uint32(p, p_end, index);
  821. export->index = index;
  822. switch(export->kind) {
  823. /*function index*/
  824. case EXPORT_KIND_FUNC:
  825. if (index >= module->function_count + module->import_function_count) {
  826. set_error_buf(error_buf, error_buf_size,
  827. "Load export section failed: "
  828. "function index is out of range.");
  829. return false;
  830. }
  831. break;
  832. /*table index*/
  833. case EXPORT_KIND_TABLE:
  834. if (index >= module->table_count + module->import_table_count) {
  835. set_error_buf(error_buf, error_buf_size,
  836. "Load export section failed: "
  837. "table index is out of range.");
  838. return false;
  839. }
  840. break;
  841. /*memory index*/
  842. case EXPORT_KIND_MEMORY:
  843. if (index >= module->memory_count + module->import_memory_count) {
  844. set_error_buf(error_buf, error_buf_size,
  845. "Load export section failed: "
  846. "memory index is out of range.");
  847. return false;
  848. }
  849. break;
  850. /*global index*/
  851. case EXPORT_KIND_GLOBAL:
  852. if (index >= module->global_count + module->import_global_count) {
  853. set_error_buf(error_buf, error_buf_size,
  854. "Load export section failed: "
  855. "global index is out of range.");
  856. return false;
  857. }
  858. break;
  859. default:
  860. set_error_buf(error_buf, error_buf_size,
  861. "Load export section failed: "
  862. "kind flag is unexpected.");
  863. return false;
  864. }
  865. }
  866. }
  867. if (p != p_end) {
  868. set_error_buf(error_buf, error_buf_size,
  869. "Load export section failed: "
  870. "invalid section size.");
  871. return false;
  872. }
  873. LOG_VERBOSE("Load export section success.\n");
  874. return true;
  875. }
  876. static bool
  877. load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  878. char *error_buf, uint32 error_buf_size)
  879. {
  880. const uint8 *p = buf, *p_end = buf_end;
  881. uint32 table_segment_count, i, j, table_index, function_count, function_index;
  882. uint64 total_size;
  883. WASMTableSeg *table_segment;
  884. read_leb_uint32(p, p_end, table_segment_count);
  885. if (table_segment_count) {
  886. module->table_seg_count = table_segment_count;
  887. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  888. if (total_size >= UINT32_MAX
  889. || !(module->table_segments = wasm_malloc((uint32)total_size))) {
  890. set_error_buf(error_buf, error_buf_size,
  891. "Load table segment section failed: "
  892. "allocate memory failed.");
  893. return false;
  894. }
  895. memset(module->table_segments, 0, (uint32)total_size);
  896. table_segment = module->table_segments;
  897. for (i = 0; i < table_segment_count; i++, table_segment++) {
  898. read_leb_uint32(p, p_end, table_index);
  899. table_segment->table_index = table_index;
  900. /* initialize expression */
  901. if (!load_init_expr(&p, p_end, &(table_segment->base_offset),
  902. error_buf, error_buf_size))
  903. return false;
  904. read_leb_uint32(p, p_end, function_count);
  905. table_segment->function_count = function_count;
  906. total_size = sizeof(uint32) * (uint64)function_count;
  907. if (total_size >= UINT32_MAX
  908. || !(table_segment->func_indexes = (uint32 *)
  909. wasm_malloc((uint32)total_size))) {
  910. set_error_buf(error_buf, error_buf_size,
  911. "Load table segment section failed: "
  912. "allocate memory failed.");
  913. return false;
  914. }
  915. for (j = 0; j < function_count; j++) {
  916. read_leb_uint32(p, p_end, function_index);
  917. table_segment->func_indexes[j] = function_index;
  918. }
  919. }
  920. }
  921. if (p != p_end) {
  922. set_error_buf(error_buf, error_buf_size,
  923. "Load table segment section failed, "
  924. "invalid section size.");
  925. return false;
  926. }
  927. LOG_VERBOSE("Load table segment section success.\n");
  928. return true;
  929. }
  930. static bool
  931. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  932. WASMModule *module,
  933. char *error_buf, uint32 error_buf_size)
  934. {
  935. const uint8 *p = buf, *p_end = buf_end;
  936. uint32 data_seg_count, i, mem_index, data_seg_len;
  937. uint64 total_size;
  938. WASMDataSeg *dataseg;
  939. InitializerExpression init_expr;
  940. read_leb_uint32(p, p_end, data_seg_count);
  941. if (data_seg_count) {
  942. module->data_seg_count = data_seg_count;
  943. total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count;
  944. if (total_size >= UINT32_MAX
  945. || !(module->data_segments = wasm_malloc((uint32)total_size))) {
  946. set_error_buf(error_buf, error_buf_size,
  947. "Load data segment section failed: "
  948. "allocate memory failed.");
  949. return false;
  950. }
  951. memset(module->data_segments, 0, (uint32)total_size);
  952. for (i = 0; i < data_seg_count; i++) {
  953. read_leb_uint32(p, p_end, mem_index);
  954. if (!load_init_expr(&p, p_end, &init_expr, error_buf, error_buf_size))
  955. return false;
  956. read_leb_uint32(p, p_end, data_seg_len);
  957. if (!(dataseg = module->data_segments[i] =
  958. wasm_malloc((uint32)sizeof(WASMDataSeg)))) {
  959. set_error_buf(error_buf, error_buf_size,
  960. "Load data segment section failed: "
  961. "allocate memory failed.");
  962. return false;
  963. }
  964. bh_memcpy_s(&dataseg->base_offset, sizeof(InitializerExpression),
  965. &init_expr, sizeof(InitializerExpression));
  966. dataseg->memory_index = mem_index;
  967. dataseg->data_length = data_seg_len;
  968. CHECK_BUF(p, p_end, data_seg_len);
  969. dataseg->data = (uint8*)p;
  970. p += data_seg_len;
  971. }
  972. }
  973. if (p != p_end) {
  974. set_error_buf(error_buf, error_buf_size,
  975. "Load data segment section failed, "
  976. "invalid section size.");
  977. return false;
  978. }
  979. LOG_VERBOSE("Load data segment section success.\n");
  980. return true;
  981. }
  982. static bool
  983. load_code_section(const uint8 *buf, const uint8 *buf_end,
  984. const uint8 *buf_func,
  985. const uint8 *buf_func_end,
  986. WASMModule *module,
  987. char *error_buf, uint32 error_buf_size)
  988. {
  989. const uint8 *p = buf, *p_end = buf_end;
  990. const uint8 *p_func = buf_func;
  991. uint32 func_count = 0, code_count;
  992. /* code has been loaded in function section, so pass it here, just check
  993. * whether function and code section have inconsistent lengths */
  994. read_leb_uint32(p, p_end, code_count);
  995. if (buf_func)
  996. read_leb_uint32(p_func, buf_func_end, func_count);
  997. if (func_count != code_count) {
  998. set_error_buf(error_buf, error_buf_size,
  999. "function and code section have inconsistent lengths");
  1000. return false;
  1001. }
  1002. LOG_VERBOSE("Load code segment section success.\n");
  1003. return true;
  1004. }
  1005. static bool
  1006. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1007. char *error_buf, uint32 error_buf_size)
  1008. {
  1009. const uint8 *p = buf, *p_end = buf_end;
  1010. uint32 start_function;
  1011. read_leb_uint32(p, p_end, start_function);
  1012. if (start_function) {
  1013. if (start_function >= module->function_count + module->import_function_count) {
  1014. set_error_buf(error_buf, error_buf_size,
  1015. "Load start section failed: "
  1016. "function index is out of range.");
  1017. return false;
  1018. }
  1019. module->start_function = start_function;
  1020. }
  1021. if (p != p_end) {
  1022. set_error_buf(error_buf, error_buf_size,
  1023. "Load start section failed: "
  1024. "invalid section size.");
  1025. return false;
  1026. }
  1027. LOG_VERBOSE("Load start section success.\n");
  1028. return true;
  1029. }
  1030. static bool
  1031. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  1032. char *error_buf, uint32 error_buf_size);
  1033. static bool
  1034. load_from_sections(WASMModule *module, WASMSection *sections,
  1035. char *error_buf, uint32 error_buf_size)
  1036. {
  1037. WASMSection *section = sections;
  1038. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  1039. *buf_func = NULL, *buf_func_end = NULL;
  1040. uint32 i;
  1041. /* Find code and function sections if have */
  1042. while (section) {
  1043. if (section->section_type == SECTION_TYPE_CODE) {
  1044. buf_code = section->section_body;
  1045. buf_code_end = buf_code + section->section_body_size;
  1046. }
  1047. else if (section->section_type == SECTION_TYPE_FUNC) {
  1048. buf_func = section->section_body;
  1049. buf_func_end = buf_func + section->section_body_size;
  1050. }
  1051. section = section->next;
  1052. }
  1053. section = sections;
  1054. while (section) {
  1055. buf = section->section_body;
  1056. buf_end = buf + section->section_body_size;
  1057. switch (section->section_type) {
  1058. case SECTION_TYPE_USER:
  1059. /* unsupported user section, ignore it. */
  1060. /* add a check to pass spec test case */
  1061. CHECK_BUF(buf, buf_end, 1);
  1062. break;
  1063. case SECTION_TYPE_TYPE:
  1064. if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size))
  1065. return false;
  1066. break;
  1067. case SECTION_TYPE_IMPORT:
  1068. if (!load_import_section(buf, buf_end, module, error_buf, error_buf_size))
  1069. return false;
  1070. break;
  1071. case SECTION_TYPE_FUNC:
  1072. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  1073. module, error_buf, error_buf_size))
  1074. return false;
  1075. break;
  1076. case SECTION_TYPE_TABLE:
  1077. if (!load_table_section(buf, buf_end, module, error_buf, error_buf_size))
  1078. return false;
  1079. break;
  1080. case SECTION_TYPE_MEMORY:
  1081. if (!load_memory_section(buf, buf_end, module, error_buf, error_buf_size))
  1082. return false;
  1083. break;
  1084. case SECTION_TYPE_GLOBAL:
  1085. if (!load_global_section(buf, buf_end, module, error_buf, error_buf_size))
  1086. return false;
  1087. break;
  1088. case SECTION_TYPE_EXPORT:
  1089. if (!load_export_section(buf, buf_end, module, error_buf, error_buf_size))
  1090. return false;
  1091. break;
  1092. case SECTION_TYPE_START:
  1093. if (!load_start_section(buf, buf_end, module, error_buf, error_buf_size))
  1094. return false;
  1095. break;
  1096. case SECTION_TYPE_ELEM:
  1097. if (!load_table_segment_section(buf, buf_end, module, error_buf, error_buf_size))
  1098. return false;
  1099. break;
  1100. case SECTION_TYPE_CODE:
  1101. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  1102. module, error_buf, error_buf_size))
  1103. return false;
  1104. break;
  1105. case SECTION_TYPE_DATA:
  1106. if (!load_data_segment_section(buf, buf_end, module, error_buf, error_buf_size))
  1107. return false;
  1108. break;
  1109. default:
  1110. set_error_buf(error_buf, error_buf_size, "invalid section id");
  1111. return false;
  1112. }
  1113. section = section->next;
  1114. }
  1115. for (i = 0; i < module->function_count; i++) {
  1116. WASMFunction *func = module->functions[i];
  1117. if (!wasm_loader_prepare_bytecode(module, func, error_buf, error_buf_size))
  1118. return false;
  1119. }
  1120. return true;
  1121. }
  1122. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  1123. static uint32
  1124. branch_set_hash(const void *key)
  1125. {
  1126. return ((uintptr_t)key) ^ ((uintptr_t)key >> 16);
  1127. }
  1128. static bool
  1129. branch_set_key_equal(void *start_addr1, void *start_addr2)
  1130. {
  1131. return start_addr1 == start_addr2 ? true : false;
  1132. }
  1133. static void
  1134. branch_set_value_destroy(void *value)
  1135. {
  1136. wasm_free(value);
  1137. }
  1138. #endif
  1139. #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
  1140. static void wasm_loader_free(void *ptr)
  1141. {
  1142. wasm_free(ptr);
  1143. }
  1144. #else
  1145. #define wasm_loader_free wasm_free
  1146. #endif
  1147. static WASMModule*
  1148. create_module(char *error_buf, uint32 error_buf_size)
  1149. {
  1150. WASMModule *module = wasm_malloc(sizeof(WASMModule));
  1151. if (!module) {
  1152. set_error_buf(error_buf, error_buf_size,
  1153. "WASM module load failed: "
  1154. "allocate memory failed.");
  1155. return NULL;
  1156. }
  1157. memset(module, 0, sizeof(WASMModule));
  1158. /* Set start_function to -1, means no start function */
  1159. module->start_function = (uint32)-1;
  1160. if (!(module->const_str_set = wasm_hash_map_create(32, false,
  1161. (HashFunc)wasm_string_hash,
  1162. (KeyEqualFunc)wasm_string_equal,
  1163. NULL,
  1164. wasm_loader_free)))
  1165. goto fail;
  1166. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  1167. if (!(module->branch_set = wasm_hash_map_create(64, true,
  1168. branch_set_hash,
  1169. branch_set_key_equal,
  1170. NULL,
  1171. branch_set_value_destroy)))
  1172. goto fail;
  1173. #endif
  1174. return module;
  1175. fail:
  1176. wasm_loader_unload(module);
  1177. return NULL;
  1178. }
  1179. WASMModule *
  1180. wasm_loader_load_from_sections(WASMSection *section_list,
  1181. char *error_buf, uint32 error_buf_size)
  1182. {
  1183. WASMModule *module = create_module(error_buf, error_buf_size);
  1184. if (!module)
  1185. return NULL;
  1186. if (!load_from_sections(module, section_list, error_buf, error_buf_size)) {
  1187. wasm_loader_unload(module);
  1188. return NULL;
  1189. }
  1190. LOG_VERBOSE("Load module from sections success.\n");
  1191. return module;
  1192. }
  1193. static void
  1194. destroy_sections(WASMSection *section_list)
  1195. {
  1196. WASMSection *section = section_list, *next;
  1197. while (section) {
  1198. next = section->next;
  1199. wasm_free(section);
  1200. section = next;
  1201. }
  1202. }
  1203. static bool
  1204. create_sections(const uint8 *buf, uint32 size,
  1205. WASMSection **p_section_list,
  1206. char *error_buf, uint32 error_buf_size)
  1207. {
  1208. WASMSection *section_list_end = NULL, *section;
  1209. const uint8 *p = buf, *p_end = buf + size/*, *section_body*/;
  1210. uint8 section_type;
  1211. uint32 section_size;
  1212. wasm_assert(!*p_section_list);
  1213. p += 8;
  1214. while (p < p_end) {
  1215. CHECK_BUF(p, p_end, 1);
  1216. section_type = read_uint8(p);
  1217. if (section_type <= SECTION_TYPE_DATA) {
  1218. read_leb_uint32(p, p_end, section_size);
  1219. CHECK_BUF(p, p_end, section_size);
  1220. if (!(section = wasm_malloc(sizeof(WASMSection)))) {
  1221. set_error_buf(error_buf, error_buf_size,
  1222. "WASM module load failed: "
  1223. "allocate memory failed.");
  1224. return false;
  1225. }
  1226. memset(section, 0, sizeof(WASMSection));
  1227. section->section_type = section_type;
  1228. section->section_body = p;
  1229. section->section_body_size = section_size;
  1230. if (!*p_section_list)
  1231. *p_section_list = section_list_end = section;
  1232. else {
  1233. section_list_end->next = section;
  1234. section_list_end = section;
  1235. }
  1236. p += section_size;
  1237. }
  1238. else {
  1239. set_error_buf(error_buf, error_buf_size, "invalid section id");
  1240. return false;
  1241. }
  1242. }
  1243. return true;
  1244. }
  1245. static void
  1246. exchange32(uint8* p_data)
  1247. {
  1248. uint8 value = *p_data;
  1249. *p_data = *(p_data + 3);
  1250. *(p_data + 3) = value;
  1251. value = *(p_data + 1);
  1252. *(p_data + 1) = *(p_data + 2);
  1253. *(p_data + 2) = value;
  1254. }
  1255. static union {
  1256. int a;
  1257. char b;
  1258. } __ue = { .a = 1 };
  1259. #define is_little_endian() (__ue.b == 1)
  1260. static bool
  1261. load(const uint8 *buf, uint32 size, WASMModule *module,
  1262. char *error_buf, uint32 error_buf_size)
  1263. {
  1264. const uint8 *buf_end = buf + size;
  1265. const uint8 *p = buf, *p_end = buf_end;
  1266. uint32 magic_number, version;
  1267. WASMSection *section_list = NULL;
  1268. CHECK_BUF(p, p_end, sizeof(uint32));
  1269. magic_number = read_uint32(p);
  1270. if (!is_little_endian())
  1271. exchange32((uint8*)&magic_number);
  1272. if (magic_number != WASM_MAGIC_NUMBER) {
  1273. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  1274. return false;
  1275. }
  1276. CHECK_BUF(p, p_end, sizeof(uint32));
  1277. version = read_uint32(p);
  1278. if (!is_little_endian())
  1279. exchange32((uint8*)&version);
  1280. if (version != WASM_CURRENT_VERSION) {
  1281. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  1282. return false;
  1283. }
  1284. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  1285. || !load_from_sections(module, section_list, error_buf, error_buf_size)) {
  1286. destroy_sections(section_list);
  1287. return false;
  1288. }
  1289. destroy_sections(section_list);
  1290. return true;
  1291. }
  1292. WASMModule*
  1293. wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
  1294. {
  1295. WASMModule *module = wasm_malloc(sizeof(WASMModule));
  1296. if (!module) {
  1297. set_error_buf(error_buf, error_buf_size,
  1298. "WASM module load failed: allocate memory failed.");
  1299. return NULL;
  1300. }
  1301. memset(module, 0, sizeof(WASMModule));
  1302. /* Set start_function to -1, means no start function */
  1303. module->start_function = (uint32)-1;
  1304. if (!(module->const_str_set = wasm_hash_map_create(32, false,
  1305. (HashFunc)wasm_string_hash,
  1306. (KeyEqualFunc)wasm_string_equal,
  1307. NULL,
  1308. wasm_loader_free)))
  1309. goto fail;
  1310. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  1311. if (!(module->branch_set = wasm_hash_map_create(64, true,
  1312. branch_set_hash,
  1313. branch_set_key_equal,
  1314. NULL,
  1315. branch_set_value_destroy)))
  1316. goto fail;
  1317. #endif
  1318. if (!load(buf, size, module, error_buf, error_buf_size))
  1319. goto fail;
  1320. LOG_VERBOSE("Load module success.\n");
  1321. return module;
  1322. fail:
  1323. wasm_loader_unload(module);
  1324. return NULL;
  1325. }
  1326. void
  1327. wasm_loader_unload(WASMModule *module)
  1328. {
  1329. uint32 i;
  1330. if (!module)
  1331. return;
  1332. if (module->types) {
  1333. for (i = 0; i < module->type_count; i++) {
  1334. if (module->types[i])
  1335. wasm_free(module->types[i]);
  1336. }
  1337. wasm_free(module->types);
  1338. }
  1339. if (module->imports)
  1340. wasm_free(module->imports);
  1341. if (module->functions) {
  1342. for (i = 0; i < module->function_count; i++) {
  1343. if (module->functions[i])
  1344. wasm_free(module->functions[i]);
  1345. }
  1346. wasm_free(module->functions);
  1347. }
  1348. if (module->tables)
  1349. wasm_free(module->tables);
  1350. if (module->memories)
  1351. wasm_free(module->memories);
  1352. if (module->globals)
  1353. wasm_free(module->globals);
  1354. if (module->exports)
  1355. wasm_free(module->exports);
  1356. if (module->table_segments) {
  1357. for (i = 0; i < module->table_seg_count; i++) {
  1358. if (module->table_segments[i].func_indexes)
  1359. wasm_free(module->table_segments[i].func_indexes);
  1360. }
  1361. wasm_free(module->table_segments);
  1362. }
  1363. if (module->data_segments) {
  1364. for (i = 0; i < module->data_seg_count; i++) {
  1365. if (module->data_segments[i])
  1366. wasm_free(module->data_segments[i]);
  1367. }
  1368. wasm_free(module->data_segments);
  1369. }
  1370. if (module->const_str_set)
  1371. wasm_hash_map_destroy(module->const_str_set);
  1372. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  1373. if (module->branch_set)
  1374. wasm_hash_map_destroy(module->branch_set);
  1375. #endif
  1376. wasm_free(module);
  1377. }
  1378. #if WASM_ENABLE_WASI != 0
  1379. void
  1380. wasm_runtime_set_wasi_args(WASMModule *module,
  1381. const char *dir_list[], uint32 dir_count,
  1382. const char *map_dir_list[], uint32 map_dir_count,
  1383. const char *env_list[], uint32 env_count,
  1384. const char *argv[], uint32 argc)
  1385. {
  1386. module->wasi_args.dir_list = dir_list;
  1387. module->wasi_args.dir_count = dir_count;
  1388. module->wasi_args.map_dir_list = map_dir_list;
  1389. module->wasi_args.map_dir_count = map_dir_count;
  1390. module->wasi_args.env = env_list;
  1391. module->wasi_args.env_count = env_count;
  1392. module->wasi_args.argv = argv;
  1393. module->wasi_args.argc = argc;
  1394. }
  1395. #endif
  1396. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  1397. typedef struct block_addr {
  1398. uint8 block_type;
  1399. uint8 *end_addr;
  1400. uint8 *else_addr;
  1401. } block_addr;
  1402. #endif
  1403. bool
  1404. wasm_loader_find_block_addr(WASMModule *module,
  1405. const uint8 *start_addr,
  1406. const uint8 *code_end_addr,
  1407. uint8 block_type,
  1408. uint8 **p_else_addr,
  1409. uint8 **p_end_addr,
  1410. char *error_buf,
  1411. uint32 error_buf_size)
  1412. {
  1413. const uint8 *p = start_addr, *p_end = code_end_addr;
  1414. uint8 *else_addr = NULL;
  1415. uint32 block_nested_depth = 1, count, i, u32;
  1416. uint64 u64;
  1417. uint8 opcode, u8;
  1418. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  1419. HashMap *branch_set = module->branch_set;
  1420. block_addr *block;
  1421. if ((block = wasm_hash_map_find(branch_set, (void*)start_addr))) {
  1422. if (block->block_type != block_type)
  1423. return false;
  1424. if (block_type == BLOCK_TYPE_IF) /* if block */
  1425. *p_else_addr = block->else_addr;
  1426. *p_end_addr = block->end_addr;
  1427. return true;
  1428. }
  1429. #else
  1430. BlockAddr block_stack[16] = { 0 }, *block;
  1431. uint32 j, t;
  1432. i = (uint32)(((uintptr_t)start_addr) ^ ((uintptr_t)start_addr >> 16));
  1433. i = i % BLOCK_ADDR_CACHE_SIZE;
  1434. block = module->block_addr_cache[i];
  1435. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  1436. if (block[j].start_addr == start_addr) {
  1437. /* Cache hit */
  1438. *p_else_addr = block[j].else_addr;
  1439. *p_end_addr = block[j].end_addr;
  1440. return true;
  1441. }
  1442. }
  1443. /* Cache unhit */
  1444. block_stack[0].start_addr = start_addr;
  1445. #endif
  1446. while (p < code_end_addr) {
  1447. opcode = *p++;
  1448. switch (opcode) {
  1449. case WASM_OP_UNREACHABLE:
  1450. case WASM_OP_NOP:
  1451. break;
  1452. case WASM_OP_BLOCK:
  1453. case WASM_OP_LOOP:
  1454. case WASM_OP_IF:
  1455. read_leb_uint8(p, p_end, u8); /* blocktype */
  1456. #if WASM_ENABLE_HASH_BLOCK_ADDR == 0
  1457. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  1458. block_stack[block_nested_depth].start_addr = p;
  1459. block_stack[block_nested_depth].else_addr = NULL;
  1460. }
  1461. #endif
  1462. block_nested_depth++;
  1463. break;
  1464. case WASM_OP_ELSE:
  1465. if (block_type == BLOCK_TYPE_IF && block_nested_depth == 1)
  1466. else_addr = (uint8*)(p - 1);
  1467. #if WASM_ENABLE_HASH_BLOCK_ADDR == 0
  1468. if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr))
  1469. block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1);
  1470. #endif
  1471. break;
  1472. case WASM_OP_END:
  1473. if (block_nested_depth == 1) {
  1474. if (block_type == BLOCK_TYPE_IF)
  1475. *p_else_addr = else_addr;
  1476. *p_end_addr = (uint8*)(p - 1);
  1477. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  1478. if (block_type == BLOCK_TYPE_IF)
  1479. block = wasm_malloc(sizeof(block_addr));
  1480. else
  1481. block = wasm_malloc(offsetof(block_addr, else_addr));
  1482. if (block) {
  1483. block->block_type = block_type;
  1484. if (block_type == BLOCK_TYPE_IF)
  1485. block->else_addr = else_addr;
  1486. block->end_addr = (uint8*)(p - 1);
  1487. if (!wasm_hash_map_insert(branch_set, (void*)start_addr, block))
  1488. wasm_free(block);
  1489. }
  1490. #else
  1491. block_stack[0].end_addr = (uint8*)(p - 1);
  1492. for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
  1493. start_addr = block_stack[t].start_addr;
  1494. if (start_addr) {
  1495. i = (uint32)(((uintptr_t)start_addr) ^ ((uintptr_t)start_addr >> 16));
  1496. i = i % BLOCK_ADDR_CACHE_SIZE;
  1497. block = module->block_addr_cache[i];
  1498. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  1499. if (!block[j].start_addr)
  1500. break;
  1501. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  1502. memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) *
  1503. sizeof(BlockAddr));
  1504. j = 0;
  1505. }
  1506. block[j].start_addr = block_stack[t].start_addr;
  1507. block[j].else_addr = block_stack[t].else_addr;
  1508. block[j].end_addr = block_stack[t].end_addr;
  1509. }
  1510. else
  1511. break;
  1512. }
  1513. #endif
  1514. return true;
  1515. }
  1516. else {
  1517. block_nested_depth--;
  1518. #if WASM_ENABLE_HASH_BLOCK_ADDR == 0
  1519. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr))
  1520. block_stack[block_nested_depth].end_addr = (uint8*)(p - 1);
  1521. #endif
  1522. }
  1523. break;
  1524. case WASM_OP_BR:
  1525. case WASM_OP_BR_IF:
  1526. read_leb_uint32(p, p_end, u32); /* labelidx */
  1527. break;
  1528. case WASM_OP_BR_TABLE:
  1529. read_leb_uint32(p, p_end, count); /* lable num */
  1530. for (i = 0; i <= count; i++) /* lableidxs */
  1531. read_leb_uint32(p, p_end, u32);
  1532. break;
  1533. case WASM_OP_RETURN:
  1534. break;
  1535. case WASM_OP_CALL:
  1536. read_leb_uint32(p, p_end, u32); /* funcidx */
  1537. break;
  1538. case WASM_OP_CALL_INDIRECT:
  1539. read_leb_uint32(p, p_end, u32); /* typeidx */
  1540. read_leb_uint8(p, p_end, u8); /* 0x00 */
  1541. break;
  1542. case WASM_OP_DROP:
  1543. case WASM_OP_SELECT:
  1544. case WASM_OP_DROP_32:
  1545. case WASM_OP_DROP_64:
  1546. case WASM_OP_SELECT_32:
  1547. case WASM_OP_SELECT_64:
  1548. break;
  1549. case WASM_OP_GET_LOCAL:
  1550. case WASM_OP_SET_LOCAL:
  1551. case WASM_OP_TEE_LOCAL:
  1552. case WASM_OP_GET_GLOBAL:
  1553. case WASM_OP_SET_GLOBAL:
  1554. read_leb_uint32(p, p_end, u32); /* localidx */
  1555. break;
  1556. case WASM_OP_I32_LOAD:
  1557. case WASM_OP_I64_LOAD:
  1558. case WASM_OP_F32_LOAD:
  1559. case WASM_OP_F64_LOAD:
  1560. case WASM_OP_I32_LOAD8_S:
  1561. case WASM_OP_I32_LOAD8_U:
  1562. case WASM_OP_I32_LOAD16_S:
  1563. case WASM_OP_I32_LOAD16_U:
  1564. case WASM_OP_I64_LOAD8_S:
  1565. case WASM_OP_I64_LOAD8_U:
  1566. case WASM_OP_I64_LOAD16_S:
  1567. case WASM_OP_I64_LOAD16_U:
  1568. case WASM_OP_I64_LOAD32_S:
  1569. case WASM_OP_I64_LOAD32_U:
  1570. case WASM_OP_I32_STORE:
  1571. case WASM_OP_I64_STORE:
  1572. case WASM_OP_F32_STORE:
  1573. case WASM_OP_F64_STORE:
  1574. case WASM_OP_I32_STORE8:
  1575. case WASM_OP_I32_STORE16:
  1576. case WASM_OP_I64_STORE8:
  1577. case WASM_OP_I64_STORE16:
  1578. case WASM_OP_I64_STORE32:
  1579. read_leb_uint32(p, p_end, u32); /* align */
  1580. read_leb_uint32(p, p_end, u32); /* offset */
  1581. break;
  1582. case WASM_OP_MEMORY_SIZE:
  1583. case WASM_OP_MEMORY_GROW:
  1584. read_leb_uint32(p, p_end, u32); /* 0x00 */
  1585. break;
  1586. case WASM_OP_I32_CONST:
  1587. read_leb_uint32(p, p_end, u32);
  1588. break;
  1589. case WASM_OP_I64_CONST:
  1590. read_leb_uint64(p, p_end, u64);
  1591. break;
  1592. case WASM_OP_F32_CONST:
  1593. p += sizeof(float32);
  1594. break;
  1595. case WASM_OP_F64_CONST:
  1596. p += sizeof(float64);
  1597. break;
  1598. case WASM_OP_I32_EQZ:
  1599. case WASM_OP_I32_EQ:
  1600. case WASM_OP_I32_NE:
  1601. case WASM_OP_I32_LT_S:
  1602. case WASM_OP_I32_LT_U:
  1603. case WASM_OP_I32_GT_S:
  1604. case WASM_OP_I32_GT_U:
  1605. case WASM_OP_I32_LE_S:
  1606. case WASM_OP_I32_LE_U:
  1607. case WASM_OP_I32_GE_S:
  1608. case WASM_OP_I32_GE_U:
  1609. case WASM_OP_I64_EQZ:
  1610. case WASM_OP_I64_EQ:
  1611. case WASM_OP_I64_NE:
  1612. case WASM_OP_I64_LT_S:
  1613. case WASM_OP_I64_LT_U:
  1614. case WASM_OP_I64_GT_S:
  1615. case WASM_OP_I64_GT_U:
  1616. case WASM_OP_I64_LE_S:
  1617. case WASM_OP_I64_LE_U:
  1618. case WASM_OP_I64_GE_S:
  1619. case WASM_OP_I64_GE_U:
  1620. case WASM_OP_F32_EQ:
  1621. case WASM_OP_F32_NE:
  1622. case WASM_OP_F32_LT:
  1623. case WASM_OP_F32_GT:
  1624. case WASM_OP_F32_LE:
  1625. case WASM_OP_F32_GE:
  1626. case WASM_OP_F64_EQ:
  1627. case WASM_OP_F64_NE:
  1628. case WASM_OP_F64_LT:
  1629. case WASM_OP_F64_GT:
  1630. case WASM_OP_F64_LE:
  1631. case WASM_OP_F64_GE:
  1632. case WASM_OP_I32_CLZ:
  1633. case WASM_OP_I32_CTZ:
  1634. case WASM_OP_I32_POPCNT:
  1635. case WASM_OP_I32_ADD:
  1636. case WASM_OP_I32_SUB:
  1637. case WASM_OP_I32_MUL:
  1638. case WASM_OP_I32_DIV_S:
  1639. case WASM_OP_I32_DIV_U:
  1640. case WASM_OP_I32_REM_S:
  1641. case WASM_OP_I32_REM_U:
  1642. case WASM_OP_I32_AND:
  1643. case WASM_OP_I32_OR:
  1644. case WASM_OP_I32_XOR:
  1645. case WASM_OP_I32_SHL:
  1646. case WASM_OP_I32_SHR_S:
  1647. case WASM_OP_I32_SHR_U:
  1648. case WASM_OP_I32_ROTL:
  1649. case WASM_OP_I32_ROTR:
  1650. case WASM_OP_I64_CLZ:
  1651. case WASM_OP_I64_CTZ:
  1652. case WASM_OP_I64_POPCNT:
  1653. case WASM_OP_I64_ADD:
  1654. case WASM_OP_I64_SUB:
  1655. case WASM_OP_I64_MUL:
  1656. case WASM_OP_I64_DIV_S:
  1657. case WASM_OP_I64_DIV_U:
  1658. case WASM_OP_I64_REM_S:
  1659. case WASM_OP_I64_REM_U:
  1660. case WASM_OP_I64_AND:
  1661. case WASM_OP_I64_OR:
  1662. case WASM_OP_I64_XOR:
  1663. case WASM_OP_I64_SHL:
  1664. case WASM_OP_I64_SHR_S:
  1665. case WASM_OP_I64_SHR_U:
  1666. case WASM_OP_I64_ROTL:
  1667. case WASM_OP_I64_ROTR:
  1668. case WASM_OP_F32_ABS:
  1669. case WASM_OP_F32_NEG:
  1670. case WASM_OP_F32_CEIL:
  1671. case WASM_OP_F32_FLOOR:
  1672. case WASM_OP_F32_TRUNC:
  1673. case WASM_OP_F32_NEAREST:
  1674. case WASM_OP_F32_SQRT:
  1675. case WASM_OP_F32_ADD:
  1676. case WASM_OP_F32_SUB:
  1677. case WASM_OP_F32_MUL:
  1678. case WASM_OP_F32_DIV:
  1679. case WASM_OP_F32_MIN:
  1680. case WASM_OP_F32_MAX:
  1681. case WASM_OP_F32_COPYSIGN:
  1682. case WASM_OP_F64_ABS:
  1683. case WASM_OP_F64_NEG:
  1684. case WASM_OP_F64_CEIL:
  1685. case WASM_OP_F64_FLOOR:
  1686. case WASM_OP_F64_TRUNC:
  1687. case WASM_OP_F64_NEAREST:
  1688. case WASM_OP_F64_SQRT:
  1689. case WASM_OP_F64_ADD:
  1690. case WASM_OP_F64_SUB:
  1691. case WASM_OP_F64_MUL:
  1692. case WASM_OP_F64_DIV:
  1693. case WASM_OP_F64_MIN:
  1694. case WASM_OP_F64_MAX:
  1695. case WASM_OP_F64_COPYSIGN:
  1696. case WASM_OP_I32_WRAP_I64:
  1697. case WASM_OP_I32_TRUNC_S_F32:
  1698. case WASM_OP_I32_TRUNC_U_F32:
  1699. case WASM_OP_I32_TRUNC_S_F64:
  1700. case WASM_OP_I32_TRUNC_U_F64:
  1701. case WASM_OP_I64_EXTEND_S_I32:
  1702. case WASM_OP_I64_EXTEND_U_I32:
  1703. case WASM_OP_I64_TRUNC_S_F32:
  1704. case WASM_OP_I64_TRUNC_U_F32:
  1705. case WASM_OP_I64_TRUNC_S_F64:
  1706. case WASM_OP_I64_TRUNC_U_F64:
  1707. case WASM_OP_F32_CONVERT_S_I32:
  1708. case WASM_OP_F32_CONVERT_U_I32:
  1709. case WASM_OP_F32_CONVERT_S_I64:
  1710. case WASM_OP_F32_CONVERT_U_I64:
  1711. case WASM_OP_F32_DEMOTE_F64:
  1712. case WASM_OP_F64_CONVERT_S_I32:
  1713. case WASM_OP_F64_CONVERT_U_I32:
  1714. case WASM_OP_F64_CONVERT_S_I64:
  1715. case WASM_OP_F64_CONVERT_U_I64:
  1716. case WASM_OP_F64_PROMOTE_F32:
  1717. case WASM_OP_I32_REINTERPRET_F32:
  1718. case WASM_OP_I64_REINTERPRET_F64:
  1719. case WASM_OP_F32_REINTERPRET_I32:
  1720. case WASM_OP_F64_REINTERPRET_I64:
  1721. break;
  1722. default:
  1723. if (error_buf)
  1724. snprintf(error_buf, error_buf_size,
  1725. "WASM loader find block addr failed: "
  1726. "invalid opcode %02x.", opcode);
  1727. return false;
  1728. }
  1729. }
  1730. (void)u32;
  1731. (void)u64;
  1732. (void)u8;
  1733. return false;
  1734. }
  1735. #define REF_I32 VALUE_TYPE_I32
  1736. #define REF_F32 VALUE_TYPE_F32
  1737. #define REF_I64_1 VALUE_TYPE_I64
  1738. #define REF_I64_2 VALUE_TYPE_I64
  1739. #define REF_F64_1 VALUE_TYPE_F64
  1740. #define REF_F64_2 VALUE_TYPE_F64
  1741. typedef struct BranchBlock {
  1742. uint8 block_type;
  1743. uint8 return_type;
  1744. bool jumped_by_br;
  1745. uint8 *start_addr;
  1746. uint8 *else_addr;
  1747. uint8 *end_addr;
  1748. uint32 stack_cell_num;
  1749. } BranchBlock;
  1750. static void*
  1751. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new)
  1752. {
  1753. uint8 *mem_new;
  1754. wasm_assert(size_new > size_old);
  1755. if ((mem_new = wasm_malloc(size_new))) {
  1756. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  1757. memset(mem_new + size_old, 0, size_new - size_old);
  1758. wasm_free(mem_old);
  1759. }
  1760. return mem_new;
  1761. }
  1762. #define MEM_REALLOC(mem, size_old, size_new) do { \
  1763. void *mem_new = memory_realloc(mem, size_old, size_new);\
  1764. if (!mem_new) { \
  1765. set_error_buf(error_buf, error_buf_size, \
  1766. "WASM loader prepare bytecode failed: " \
  1767. "allocate memory failed."); \
  1768. goto fail; \
  1769. } \
  1770. mem = mem_new; \
  1771. } while (0)
  1772. static bool
  1773. check_stack_push(uint8 **p_frame_ref_bottom, uint8 **p_frame_ref_boundary,
  1774. uint8 **p_frame_ref, uint32 *p_frame_ref_size,
  1775. uint32 stack_cell_num,
  1776. char *error_buf, uint32 error_buf_size)
  1777. {
  1778. if (*p_frame_ref >= *p_frame_ref_boundary) {
  1779. MEM_REALLOC(*p_frame_ref_bottom, *p_frame_ref_size,
  1780. *p_frame_ref_size + 16);
  1781. *p_frame_ref_size += 16;
  1782. *p_frame_ref_boundary = *p_frame_ref_bottom + *p_frame_ref_size;
  1783. *p_frame_ref = *p_frame_ref_bottom + stack_cell_num;
  1784. }
  1785. return true;
  1786. fail:
  1787. return false;
  1788. }
  1789. #define CHECK_STACK_PUSH() do { \
  1790. if (!check_stack_push(&frame_ref_bottom, &frame_ref_boundary,\
  1791. &frame_ref, &frame_ref_size, \
  1792. stack_cell_num, \
  1793. error_buf, error_buf_size)) \
  1794. goto fail; \
  1795. } while (0)
  1796. static bool
  1797. check_stack_pop(uint8 type, uint8 *frame_ref, uint32 stack_cell_num,
  1798. char *error_buf, uint32 error_buf_size,
  1799. const char *type_str)
  1800. {
  1801. if (((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  1802. && stack_cell_num < 1)
  1803. || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  1804. && stack_cell_num < 2)) {
  1805. set_error_buf(error_buf, error_buf_size,
  1806. "type mismatch: expected data but stack was empty");
  1807. return false;
  1808. }
  1809. if ((type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  1810. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  1811. || (type == VALUE_TYPE_I64
  1812. && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2))
  1813. || (type == VALUE_TYPE_F64
  1814. && (*(frame_ref - 2) != REF_F64_1 || *(frame_ref - 1) != REF_F64_2))) {
  1815. if (error_buf != NULL)
  1816. snprintf(error_buf, error_buf_size, "%s%s%s",
  1817. "type mismatch: expected ", type_str, " but got other");
  1818. return false;
  1819. }
  1820. return true;
  1821. }
  1822. #define CHECK_STACK_POP(TYPE, type) do { \
  1823. if (!check_stack_pop(VALUE_TYPE_##TYPE, \
  1824. frame_ref, stack_cell_num, \
  1825. error_buf, error_buf_size, #type)) \
  1826. goto fail; \
  1827. } while (0)
  1828. #define PUSH_I32() do { \
  1829. CHECK_STACK_PUSH(); \
  1830. *frame_ref++ = REF_I32; \
  1831. stack_cell_num++; \
  1832. if (stack_cell_num > max_stack_cell_num) \
  1833. max_stack_cell_num = stack_cell_num; \
  1834. } while (0)
  1835. #define PUSH_F32() do { \
  1836. CHECK_STACK_PUSH(); \
  1837. *frame_ref++ = REF_F32; \
  1838. stack_cell_num++; \
  1839. if (stack_cell_num > max_stack_cell_num) \
  1840. max_stack_cell_num = stack_cell_num; \
  1841. } while (0)
  1842. #define PUSH_I64() do { \
  1843. CHECK_STACK_PUSH(); \
  1844. *frame_ref++ = REF_I64_1; \
  1845. stack_cell_num++; \
  1846. CHECK_STACK_PUSH(); \
  1847. *frame_ref++ = REF_I64_2; \
  1848. stack_cell_num++; \
  1849. if (stack_cell_num > max_stack_cell_num) \
  1850. max_stack_cell_num = stack_cell_num; \
  1851. } while (0)
  1852. #define PUSH_F64() do { \
  1853. CHECK_STACK_PUSH(); \
  1854. *frame_ref++ = REF_F64_1; \
  1855. stack_cell_num++; \
  1856. CHECK_STACK_PUSH(); \
  1857. *frame_ref++ = REF_F64_2; \
  1858. stack_cell_num++; \
  1859. if (stack_cell_num > max_stack_cell_num) \
  1860. max_stack_cell_num = stack_cell_num; \
  1861. } while (0)
  1862. #define POP_I32() do { \
  1863. CHECK_STACK_POP(I32, i32); \
  1864. stack_cell_num--; \
  1865. frame_ref--; \
  1866. } while (0)
  1867. #define POP_I64() do { \
  1868. CHECK_STACK_POP(I64, i64); \
  1869. stack_cell_num -= 2; \
  1870. frame_ref -= 2; \
  1871. } while (0)
  1872. #define POP_F32() do { \
  1873. CHECK_STACK_POP(F32, f32); \
  1874. stack_cell_num--; \
  1875. frame_ref--; \
  1876. } while (0)
  1877. #define POP_F64() do { \
  1878. CHECK_STACK_POP(F64, f64); \
  1879. stack_cell_num -= 2; \
  1880. frame_ref -= 2; \
  1881. } while (0)
  1882. static bool
  1883. push_type(uint8 type, uint8 **p_frame_ref_bottom,
  1884. uint8 **p_frame_ref_boundary,
  1885. uint8 **p_frame_ref, uint32 *p_frame_ref_size,
  1886. uint32 *p_stack_cell_num, uint32 *p_max_stack_cell_num,
  1887. char *error_buf, uint32 error_buf_size)
  1888. {
  1889. uint8 *frame_ref = *p_frame_ref;
  1890. uint32 frame_ref_size = *p_frame_ref_size;
  1891. uint32 max_stack_cell_num = *p_max_stack_cell_num;
  1892. uint32 stack_cell_num = *p_stack_cell_num;
  1893. switch (type) {
  1894. case VALUE_TYPE_I64:
  1895. case VALUE_TYPE_F64:
  1896. if (!check_stack_push(p_frame_ref_bottom, p_frame_ref_boundary,
  1897. &frame_ref, &frame_ref_size,
  1898. stack_cell_num,
  1899. error_buf, error_buf_size))
  1900. goto fail;
  1901. *frame_ref++ = type;
  1902. stack_cell_num++;
  1903. if (stack_cell_num > max_stack_cell_num)
  1904. max_stack_cell_num = stack_cell_num;
  1905. case VALUE_TYPE_I32:
  1906. case VALUE_TYPE_F32:
  1907. if (!check_stack_push(p_frame_ref_bottom, p_frame_ref_boundary,
  1908. &frame_ref, &frame_ref_size,
  1909. stack_cell_num,
  1910. error_buf, error_buf_size))
  1911. goto fail;
  1912. *frame_ref++ = type;
  1913. stack_cell_num++;
  1914. if (stack_cell_num > max_stack_cell_num)
  1915. max_stack_cell_num = stack_cell_num;
  1916. break;
  1917. }
  1918. *p_frame_ref = frame_ref;
  1919. *p_frame_ref_size = frame_ref_size;
  1920. *p_max_stack_cell_num = max_stack_cell_num;
  1921. *p_stack_cell_num = stack_cell_num;
  1922. return true;
  1923. fail:
  1924. return false;
  1925. }
  1926. #define PUSH_TYPE(type) do { \
  1927. if (!push_type(type, &frame_ref_bottom, \
  1928. &frame_ref_boundary, \
  1929. &frame_ref, &frame_ref_size, \
  1930. &stack_cell_num, &max_stack_cell_num, \
  1931. error_buf, error_buf_size)) \
  1932. goto fail; \
  1933. } while (0)
  1934. static bool
  1935. pop_type(uint8 type, uint8 **p_frame_ref, uint32 *p_stack_cell_num,
  1936. char *error_buf, uint32 error_buf_size)
  1937. {
  1938. char *type_str[] = { "f64", "f32", "i64", "i32" };
  1939. switch (type) {
  1940. case VALUE_TYPE_I64:
  1941. case VALUE_TYPE_F64:
  1942. if (!check_stack_pop(type, *p_frame_ref, *p_stack_cell_num,
  1943. error_buf, error_buf_size,
  1944. type_str[type - VALUE_TYPE_F64]))
  1945. return false;
  1946. *p_frame_ref -= 2;
  1947. *p_stack_cell_num -= 2;
  1948. break;
  1949. case VALUE_TYPE_I32:
  1950. case VALUE_TYPE_F32:
  1951. if (!check_stack_pop(type, *p_frame_ref, *p_stack_cell_num,
  1952. error_buf, error_buf_size,
  1953. type_str[type - VALUE_TYPE_F64]))
  1954. return false;
  1955. *p_frame_ref -= 1;
  1956. *p_stack_cell_num -= 1;
  1957. break;
  1958. }
  1959. return true;
  1960. }
  1961. #define POP_TYPE(type) do { \
  1962. if (!pop_type(type, &frame_ref, &stack_cell_num,\
  1963. error_buf, error_buf_size)) \
  1964. goto fail; \
  1965. } while (0)
  1966. #define CHECK_CSP_PUSH() do { \
  1967. if (frame_csp >= frame_csp_boundary) { \
  1968. MEM_REALLOC(frame_csp_bottom, frame_csp_size, \
  1969. (uint32)(frame_csp_size \
  1970. + 8 * sizeof(BranchBlock))); \
  1971. frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  1972. frame_csp_boundary = frame_csp_bottom + \
  1973. frame_csp_size / sizeof(BranchBlock); \
  1974. frame_csp = frame_csp_bottom + csp_num; \
  1975. } \
  1976. } while (0)
  1977. #define CHECK_CSP_POP() do { \
  1978. if (csp_num < 1) { \
  1979. set_error_buf(error_buf, error_buf_size, \
  1980. "type mismatch: expected data but block stack was empty");\
  1981. goto fail; \
  1982. } \
  1983. } while (0)
  1984. #define PUSH_CSP(type, ret_type, _start_addr) do { \
  1985. CHECK_CSP_PUSH(); \
  1986. frame_csp->block_type = type; \
  1987. frame_csp->jumped_by_br = false; \
  1988. frame_csp->return_type = ret_type; \
  1989. frame_csp->start_addr = _start_addr; \
  1990. frame_csp->else_addr = NULL; \
  1991. frame_csp->end_addr = NULL; \
  1992. frame_csp->stack_cell_num = stack_cell_num; \
  1993. frame_csp++; \
  1994. csp_num++; \
  1995. if (csp_num > max_csp_num) \
  1996. max_csp_num = csp_num; \
  1997. } while (0)
  1998. #define POP_CSP() do { \
  1999. CHECK_CSP_POP(); \
  2000. frame_csp--; \
  2001. csp_num--; \
  2002. } while (0)
  2003. #define GET_LOCAL_INDEX_AND_TYPE() do { \
  2004. read_leb_uint32(p, p_end, local_idx); \
  2005. if (local_idx >= param_count + local_count) { \
  2006. set_error_buf(error_buf, error_buf_size, \
  2007. "invalid index: local index out of range"); \
  2008. goto fail; \
  2009. } \
  2010. local_type = local_idx < param_count \
  2011. ? param_types[local_idx] \
  2012. : local_types[local_idx - param_count]; \
  2013. } while (0)
  2014. #define CHECK_BR(depth) do { \
  2015. if (csp_num < depth + 1) { \
  2016. set_error_buf(error_buf, error_buf_size, "type mismatch: " \
  2017. "expected data but block stack was empty"); \
  2018. goto fail; \
  2019. } \
  2020. if ((frame_csp - (depth + 1))->block_type != BLOCK_TYPE_LOOP) { \
  2021. uint8 tmp_ret_type = (frame_csp - (depth + 1))->return_type; \
  2022. if ((tmp_ret_type == VALUE_TYPE_I32 \
  2023. && (stack_cell_num < 1 || *(frame_ref - 1) != REF_I32)) \
  2024. || (tmp_ret_type == VALUE_TYPE_F32 \
  2025. && (stack_cell_num < 1 || *(frame_ref - 1) != REF_F32))\
  2026. || (tmp_ret_type == VALUE_TYPE_I64 \
  2027. && (stack_cell_num < 2 \
  2028. || *(frame_ref - 2) != REF_I64_1 \
  2029. || *(frame_ref - 1) != REF_I64_2)) \
  2030. || (tmp_ret_type == VALUE_TYPE_F64 \
  2031. && (stack_cell_num < 2 \
  2032. || *(frame_ref - 2) != REF_F64_1 \
  2033. || *(frame_ref - 1) != REF_F64_2))) { \
  2034. set_error_buf(error_buf, error_buf_size, "type mismatch: " \
  2035. "expected data but stack was empty or other type"); \
  2036. goto fail; \
  2037. } \
  2038. (frame_csp - (depth + 1))->jumped_by_br = true; \
  2039. } \
  2040. } while (0)
  2041. static bool
  2042. check_memory(WASMModule *module,
  2043. char *error_buf, uint32 error_buf_size)
  2044. {
  2045. if (module->memory_count == 0
  2046. && module->import_memory_count == 0) {
  2047. set_error_buf(error_buf, error_buf_size,
  2048. "load or store in module without default memory");
  2049. return false;
  2050. }
  2051. return true;
  2052. }
  2053. #define CHECK_MEMORY() do { \
  2054. if (!check_memory(module, error_buf, error_buf_size)) \
  2055. goto fail; \
  2056. } while (0)
  2057. static bool
  2058. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2059. char *error_buf, uint32 error_buf_size)
  2060. {
  2061. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  2062. block_addr *block;
  2063. #endif
  2064. uint8 *p = func->code, *p_end = func->code + func->code_size;
  2065. uint8 *frame_ref_bottom = NULL, *frame_ref_boundary, *frame_ref;
  2066. BranchBlock *frame_csp_bottom = NULL, *frame_csp_boundary, *frame_csp;
  2067. uint32 param_count, local_count, global_count;
  2068. uint32 max_stack_cell_num = 0, max_csp_num = 0;
  2069. uint32 stack_cell_num = 0, csp_num = 0;
  2070. uint32 frame_ref_size, frame_csp_size;
  2071. uint8 *param_types, ret_type, *local_types, local_type, global_type;
  2072. uint32 count, i, local_idx, global_idx, depth, u32;
  2073. int32 i32, i32_const = 0;
  2074. int64 i64;
  2075. uint8 opcode, u8, block_return_type;
  2076. bool return_value = false, is_i32_const = false;
  2077. global_count = module->import_global_count + module->global_count;
  2078. param_count = func->func_type->param_count;
  2079. param_types = func->func_type->types;
  2080. ret_type = func->func_type->result_count
  2081. ? param_types[param_count] : VALUE_TYPE_VOID;
  2082. local_count = func->local_count;
  2083. local_types = func->local_types;
  2084. frame_ref_size = 32;
  2085. if (!(frame_ref_bottom = frame_ref = wasm_malloc(frame_ref_size))) {
  2086. set_error_buf(error_buf, error_buf_size,
  2087. "WASM loader prepare bytecode failed: "
  2088. "allocate memory failed");
  2089. goto fail;
  2090. }
  2091. memset(frame_ref_bottom, 0, frame_ref_size);
  2092. frame_ref_boundary = frame_ref_bottom + frame_ref_size;
  2093. frame_csp_size = sizeof(BranchBlock) * 8;
  2094. if (!(frame_csp_bottom = frame_csp = wasm_malloc(frame_csp_size))) {
  2095. set_error_buf(error_buf, error_buf_size,
  2096. "WASM loader prepare bytecode failed: "
  2097. "allocate memory failed");
  2098. goto fail;
  2099. }
  2100. memset(frame_csp_bottom, 0, frame_csp_size);
  2101. frame_csp_boundary = frame_csp_bottom + 8;
  2102. PUSH_CSP(BLOCK_TYPE_FUNCTION, ret_type, p);
  2103. (frame_csp - 1)->jumped_by_br = true;
  2104. while (p < p_end) {
  2105. opcode = *p++;
  2106. switch (opcode) {
  2107. case WASM_OP_UNREACHABLE:
  2108. goto handle_op_br;
  2109. case WASM_OP_NOP:
  2110. break;
  2111. case WASM_OP_BLOCK:
  2112. read_leb_uint8(p, p_end, block_return_type);
  2113. PUSH_CSP(BLOCK_TYPE_BLOCK, block_return_type, p);
  2114. break;
  2115. case WASM_OP_LOOP:
  2116. read_leb_uint8(p, p_end, block_return_type);
  2117. PUSH_CSP(BLOCK_TYPE_LOOP, block_return_type, p);
  2118. break;
  2119. case WASM_OP_IF:
  2120. POP_I32();
  2121. read_leb_uint8(p, p_end, block_return_type);
  2122. PUSH_CSP(BLOCK_TYPE_IF, block_return_type, p);
  2123. if (!is_i32_const)
  2124. (frame_csp - 1)->jumped_by_br = true;
  2125. else {
  2126. if (!i32_const) {
  2127. if(!wasm_loader_find_block_addr(module,
  2128. (frame_csp - 1)->start_addr,
  2129. p_end,
  2130. (frame_csp - 1)->block_type,
  2131. &(frame_csp - 1)->else_addr,
  2132. &(frame_csp - 1)->end_addr,
  2133. error_buf, error_buf_size))
  2134. goto fail;
  2135. if ((frame_csp - 1)->else_addr)
  2136. p = (frame_csp - 1)->else_addr;
  2137. else
  2138. p = (frame_csp - 1)->end_addr;
  2139. }
  2140. }
  2141. break;
  2142. case WASM_OP_ELSE:
  2143. if (csp_num < 2) {
  2144. set_error_buf(error_buf, error_buf_size, "invalid else");
  2145. goto fail;
  2146. }
  2147. if ((frame_csp - 1)->block_type != BLOCK_TYPE_IF) {
  2148. set_error_buf(error_buf, error_buf_size, "invalid else");
  2149. goto fail;
  2150. }
  2151. (frame_csp - 1)->else_addr = p - 1;
  2152. stack_cell_num = (frame_csp - 1)->stack_cell_num;
  2153. frame_ref = frame_ref_bottom + stack_cell_num;
  2154. break;
  2155. case WASM_OP_END:
  2156. {
  2157. POP_CSP();
  2158. POP_TYPE(frame_csp->return_type);
  2159. PUSH_TYPE(frame_csp->return_type);
  2160. if (csp_num > 0) {
  2161. frame_csp->end_addr = p - 1;
  2162. #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
  2163. if (wasm_hash_map_find(module->branch_set, (void*)frame_csp->start_addr))
  2164. break;
  2165. if (frame_csp->block_type == BLOCK_TYPE_IF)
  2166. block = wasm_malloc(sizeof(block_addr));
  2167. else
  2168. block = wasm_malloc(offsetof(block_addr, else_addr));
  2169. if (!block) {
  2170. set_error_buf(error_buf, error_buf_size,
  2171. "WASM loader prepare bytecode failed: "
  2172. "allocate memory failed.");
  2173. goto fail;
  2174. }
  2175. block->block_type = frame_csp->block_type;
  2176. if (frame_csp->block_type == BLOCK_TYPE_IF)
  2177. block->else_addr = (void*)frame_csp->else_addr;
  2178. block->end_addr = (void*)frame_csp->end_addr;
  2179. if (!wasm_hash_map_insert(module->branch_set, (void*)frame_csp->start_addr,
  2180. block)) {
  2181. set_error_buf(error_buf, error_buf_size,
  2182. "WASM loader prepare bytecode failed: "
  2183. "allocate memory failed.");
  2184. wasm_free(block);
  2185. goto fail;
  2186. }
  2187. #endif
  2188. }
  2189. break;
  2190. }
  2191. case WASM_OP_BR:
  2192. {
  2193. read_leb_uint32(p, p_end, depth);
  2194. CHECK_BR(depth);
  2195. handle_op_br:
  2196. for (i = 1; i <= csp_num; i++)
  2197. if ((frame_csp - i)->jumped_by_br)
  2198. break;
  2199. block_return_type = (frame_csp - i)->return_type;
  2200. if(!wasm_loader_find_block_addr(module,
  2201. (frame_csp - i)->start_addr,
  2202. p_end,
  2203. (frame_csp - i)->block_type,
  2204. &(frame_csp - i)->else_addr,
  2205. &(frame_csp - i)->end_addr,
  2206. error_buf, error_buf_size))
  2207. goto fail;
  2208. stack_cell_num = (frame_csp - i)->stack_cell_num;
  2209. frame_ref = frame_ref_bottom + stack_cell_num;
  2210. csp_num -= i - 1;
  2211. frame_csp -= i - 1;
  2212. if ((frame_csp - 1)->block_type == BLOCK_TYPE_IF
  2213. && (frame_csp - 1)->else_addr != NULL
  2214. && p <= (frame_csp - 1)->else_addr)
  2215. p = (frame_csp - 1)->else_addr;
  2216. else {
  2217. p = (frame_csp - 1)->end_addr;
  2218. PUSH_TYPE(block_return_type);
  2219. }
  2220. break;
  2221. }
  2222. case WASM_OP_BR_IF:
  2223. read_leb_uint32(p, p_end, depth);
  2224. POP_I32();
  2225. CHECK_BR(depth);
  2226. if (!is_i32_const)
  2227. (frame_csp - (depth + 1))->jumped_by_br = true;
  2228. else {
  2229. if (i32_const)
  2230. goto handle_op_br;
  2231. }
  2232. break;
  2233. case WASM_OP_BR_TABLE:
  2234. {
  2235. read_leb_uint32(p, p_end, count);
  2236. POP_I32();
  2237. /* TODO: check the const */
  2238. for (i = 0; i <= count; i++) {
  2239. read_leb_uint32(p, p_end, depth);
  2240. CHECK_BR(depth);
  2241. }
  2242. goto handle_op_br;
  2243. }
  2244. case WASM_OP_RETURN:
  2245. {
  2246. POP_TYPE(ret_type);
  2247. PUSH_TYPE(ret_type);
  2248. if(!wasm_loader_find_block_addr(module,
  2249. (frame_csp - 1)->start_addr,
  2250. p_end,
  2251. (frame_csp - 1)->block_type,
  2252. &(frame_csp - 1)->else_addr,
  2253. &(frame_csp - 1)->end_addr,
  2254. error_buf, error_buf_size))
  2255. goto fail;
  2256. stack_cell_num = (frame_csp - 1)->stack_cell_num;
  2257. frame_ref = frame_ref_bottom + stack_cell_num;
  2258. if ((frame_csp - 1)->block_type == BLOCK_TYPE_IF
  2259. && p <= (frame_csp - 1)->else_addr) {
  2260. p = (frame_csp - 1)->else_addr;
  2261. }
  2262. else {
  2263. p = (frame_csp - 1)->end_addr;
  2264. PUSH_TYPE((frame_csp - 1)->return_type);
  2265. }
  2266. break;
  2267. }
  2268. case WASM_OP_CALL:
  2269. {
  2270. WASMType *func_type;
  2271. uint32 func_idx;
  2272. int32 idx;
  2273. read_leb_uint32(p, p_end, func_idx);
  2274. if (func_idx >= module->import_function_count + module->function_count) {
  2275. set_error_buf(error_buf, error_buf_size, "function index is overflow");
  2276. goto fail;
  2277. }
  2278. if (func_idx < module->import_function_count)
  2279. func_type = module->import_functions[func_idx].u.function.func_type;
  2280. else
  2281. func_type =
  2282. module->functions[func_idx - module->import_function_count]->func_type;
  2283. if (func_type->param_count > 0) {
  2284. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--)
  2285. POP_TYPE(func_type->types[idx]);
  2286. }
  2287. if (func_type->result_count)
  2288. PUSH_TYPE(func_type->types[func_type->param_count]);
  2289. break;
  2290. }
  2291. case WASM_OP_CALL_INDIRECT:
  2292. {
  2293. int32 idx;
  2294. WASMType *func_type;
  2295. uint32 type_idx;
  2296. if (module->table_count == 0
  2297. && module->import_table_count == 0) {
  2298. set_error_buf(error_buf, error_buf_size,
  2299. "call indirect without default table");
  2300. goto fail;
  2301. }
  2302. read_leb_uint32(p, p_end, type_idx);
  2303. /* reserved byte 0x00 */
  2304. if (*p++ != 0x00) {
  2305. set_error_buf(error_buf, error_buf_size,
  2306. "zero flag expected");
  2307. goto fail;
  2308. }
  2309. POP_I32();
  2310. if (type_idx >= module->type_count) {
  2311. set_error_buf(error_buf, error_buf_size,
  2312. "function index is overflow");
  2313. goto fail;
  2314. }
  2315. func_type = module->types[type_idx];
  2316. if (func_type->param_count > 0) {
  2317. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--)
  2318. POP_TYPE(func_type->types[idx]);
  2319. }
  2320. PUSH_TYPE(func_type->types[func_type->param_count]);
  2321. break;
  2322. }
  2323. case WASM_OP_DROP:
  2324. {
  2325. if (stack_cell_num <= 0) {
  2326. set_error_buf(error_buf, error_buf_size,
  2327. "invalid drop: stack was empty");
  2328. goto fail;
  2329. }
  2330. if (*(frame_ref - 1) == REF_I32
  2331. || *(frame_ref - 1) == REF_F32) {
  2332. frame_ref--;
  2333. stack_cell_num--;
  2334. *(p - 1) = WASM_OP_DROP_32;
  2335. }
  2336. else {
  2337. if (stack_cell_num <= 1) {
  2338. set_error_buf(error_buf, error_buf_size,
  2339. "invalid drop: stack was empty");
  2340. goto fail;
  2341. }
  2342. frame_ref -= 2;
  2343. stack_cell_num -= 2;
  2344. *(p - 1) = WASM_OP_DROP_64;
  2345. }
  2346. break;
  2347. }
  2348. case WASM_OP_SELECT:
  2349. {
  2350. uint8 ref_type;
  2351. POP_I32();
  2352. if (stack_cell_num <= 0) {
  2353. set_error_buf(error_buf, error_buf_size,
  2354. "invalid drop: stack was empty");
  2355. goto fail;
  2356. }
  2357. switch (*(frame_ref - 1)) {
  2358. case REF_I32:
  2359. case REF_F32:
  2360. *(p - 1) = WASM_OP_SELECT_32;
  2361. break;
  2362. case REF_I64_2:
  2363. case REF_F64_2:
  2364. *(p - 1) = WASM_OP_SELECT_64;
  2365. break;
  2366. }
  2367. ref_type = *(frame_ref - 1);
  2368. POP_TYPE(ref_type);
  2369. POP_TYPE(ref_type);
  2370. PUSH_TYPE(ref_type);
  2371. break;
  2372. }
  2373. case WASM_OP_GET_LOCAL:
  2374. {
  2375. GET_LOCAL_INDEX_AND_TYPE();
  2376. PUSH_TYPE(local_type);
  2377. break;
  2378. }
  2379. case WASM_OP_SET_LOCAL:
  2380. {
  2381. GET_LOCAL_INDEX_AND_TYPE();
  2382. POP_TYPE(local_type);
  2383. break;
  2384. }
  2385. case WASM_OP_TEE_LOCAL:
  2386. {
  2387. GET_LOCAL_INDEX_AND_TYPE();
  2388. POP_TYPE(local_type);
  2389. PUSH_TYPE(local_type);
  2390. break;
  2391. }
  2392. case WASM_OP_GET_GLOBAL:
  2393. {
  2394. read_leb_uint32(p, p_end, global_idx);
  2395. if (global_idx >= global_count) {
  2396. set_error_buf(error_buf, error_buf_size,
  2397. "invalid index: global index out of range");
  2398. goto fail;
  2399. }
  2400. global_type = global_idx < module->import_global_count
  2401. ? module->import_globals[global_idx].u.global.type
  2402. :module->globals[global_idx - module->import_global_count].type;
  2403. PUSH_TYPE(global_type);
  2404. break;
  2405. }
  2406. case WASM_OP_SET_GLOBAL:
  2407. {
  2408. read_leb_uint32(p, p_end, global_idx);
  2409. if (global_idx >= global_count) {
  2410. set_error_buf(error_buf, error_buf_size,
  2411. "invalid index: global index out of range");
  2412. goto fail;
  2413. }
  2414. global_type = global_idx < module->import_global_count
  2415. ? module->import_globals[global_idx].u.global.type
  2416. : module->globals[global_idx - module->import_global_count].type;
  2417. POP_TYPE(global_type);
  2418. break;
  2419. }
  2420. case WASM_OP_I32_LOAD:
  2421. case WASM_OP_I32_LOAD8_S:
  2422. case WASM_OP_I32_LOAD8_U:
  2423. case WASM_OP_I32_LOAD16_S:
  2424. case WASM_OP_I32_LOAD16_U:
  2425. CHECK_MEMORY();
  2426. read_leb_uint32(p, p_end, u32); /* align */
  2427. read_leb_uint32(p, p_end, u32); /* offset */
  2428. POP_I32();
  2429. PUSH_I32();
  2430. break;
  2431. case WASM_OP_I64_LOAD:
  2432. case WASM_OP_I64_LOAD8_S:
  2433. case WASM_OP_I64_LOAD8_U:
  2434. case WASM_OP_I64_LOAD16_S:
  2435. case WASM_OP_I64_LOAD16_U:
  2436. case WASM_OP_I64_LOAD32_S:
  2437. case WASM_OP_I64_LOAD32_U:
  2438. CHECK_MEMORY();
  2439. read_leb_uint32(p, p_end, u32); /* align */
  2440. read_leb_uint32(p, p_end, u32); /* offset */
  2441. POP_I32();
  2442. PUSH_I64();
  2443. break;
  2444. case WASM_OP_F32_LOAD:
  2445. CHECK_MEMORY();
  2446. read_leb_uint32(p, p_end, u32); /* align */
  2447. read_leb_uint32(p, p_end, u32); /* offset */
  2448. POP_I32();
  2449. PUSH_F32();
  2450. break;
  2451. case WASM_OP_F64_LOAD:
  2452. CHECK_MEMORY();
  2453. read_leb_uint32(p, p_end, u32); /* align */
  2454. read_leb_uint32(p, p_end, u32); /* offset */
  2455. POP_I32();
  2456. PUSH_F64();
  2457. break;
  2458. case WASM_OP_I32_STORE:
  2459. case WASM_OP_I32_STORE8:
  2460. case WASM_OP_I32_STORE16:
  2461. CHECK_MEMORY();
  2462. read_leb_uint32(p, p_end, u32); /* align */
  2463. read_leb_uint32(p, p_end, u32); /* offset */
  2464. POP_I32();
  2465. POP_I32();
  2466. break;
  2467. case WASM_OP_I64_STORE:
  2468. case WASM_OP_I64_STORE8:
  2469. case WASM_OP_I64_STORE16:
  2470. case WASM_OP_I64_STORE32:
  2471. CHECK_MEMORY();
  2472. read_leb_uint32(p, p_end, u32); /* align */
  2473. read_leb_uint32(p, p_end, u32); /* offset */
  2474. POP_I64();
  2475. POP_I32();
  2476. break;
  2477. case WASM_OP_F32_STORE:
  2478. CHECK_MEMORY();
  2479. read_leb_uint32(p, p_end, u32); /* align */
  2480. read_leb_uint32(p, p_end, u32); /* offset */
  2481. POP_F32();
  2482. POP_I32();
  2483. break;
  2484. case WASM_OP_F64_STORE:
  2485. CHECK_MEMORY();
  2486. read_leb_uint32(p, p_end, u32); /* align */
  2487. read_leb_uint32(p, p_end, u32); /* offset */
  2488. POP_F64();
  2489. POP_I32();
  2490. break;
  2491. case WASM_OP_MEMORY_SIZE:
  2492. CHECK_MEMORY();
  2493. /* reserved byte 0x00 */
  2494. if (*p++ != 0x00) {
  2495. set_error_buf(error_buf, error_buf_size,
  2496. "zero flag expected");
  2497. goto fail;
  2498. }
  2499. PUSH_I32();
  2500. break;
  2501. case WASM_OP_MEMORY_GROW:
  2502. CHECK_MEMORY();
  2503. /* reserved byte 0x00 */
  2504. if (*p++ != 0x00) {
  2505. set_error_buf(error_buf, error_buf_size,
  2506. "zero flag expected");
  2507. goto fail;
  2508. }
  2509. POP_I32();
  2510. PUSH_I32();
  2511. break;
  2512. case WASM_OP_I32_CONST:
  2513. read_leb_int32(p, p_end, i32_const);
  2514. /* Currently we only track simple I32_CONST opcode. */
  2515. is_i32_const = true;
  2516. PUSH_I32();
  2517. break;
  2518. case WASM_OP_I64_CONST:
  2519. read_leb_int64(p, p_end, i64);
  2520. PUSH_I64();
  2521. break;
  2522. case WASM_OP_F32_CONST:
  2523. p += sizeof(float32);
  2524. PUSH_F32();
  2525. break;
  2526. case WASM_OP_F64_CONST:
  2527. p += sizeof(float64);
  2528. PUSH_F64();
  2529. break;
  2530. case WASM_OP_I32_EQZ:
  2531. POP_I32();
  2532. PUSH_I32();
  2533. break;
  2534. case WASM_OP_I32_EQ:
  2535. case WASM_OP_I32_NE:
  2536. case WASM_OP_I32_LT_S:
  2537. case WASM_OP_I32_LT_U:
  2538. case WASM_OP_I32_GT_S:
  2539. case WASM_OP_I32_GT_U:
  2540. case WASM_OP_I32_LE_S:
  2541. case WASM_OP_I32_LE_U:
  2542. case WASM_OP_I32_GE_S:
  2543. case WASM_OP_I32_GE_U:
  2544. POP_I32();
  2545. POP_I32();
  2546. PUSH_I32();
  2547. break;
  2548. case WASM_OP_I64_EQZ:
  2549. POP_I64();
  2550. PUSH_I32();
  2551. break;
  2552. case WASM_OP_I64_EQ:
  2553. case WASM_OP_I64_NE:
  2554. case WASM_OP_I64_LT_S:
  2555. case WASM_OP_I64_LT_U:
  2556. case WASM_OP_I64_GT_S:
  2557. case WASM_OP_I64_GT_U:
  2558. case WASM_OP_I64_LE_S:
  2559. case WASM_OP_I64_LE_U:
  2560. case WASM_OP_I64_GE_S:
  2561. case WASM_OP_I64_GE_U:
  2562. POP_I64();
  2563. POP_I64();
  2564. PUSH_I32();
  2565. break;
  2566. case WASM_OP_F32_EQ:
  2567. case WASM_OP_F32_NE:
  2568. case WASM_OP_F32_LT:
  2569. case WASM_OP_F32_GT:
  2570. case WASM_OP_F32_LE:
  2571. case WASM_OP_F32_GE:
  2572. POP_F32();
  2573. POP_F32();
  2574. PUSH_I32();
  2575. break;
  2576. case WASM_OP_F64_EQ:
  2577. case WASM_OP_F64_NE:
  2578. case WASM_OP_F64_LT:
  2579. case WASM_OP_F64_GT:
  2580. case WASM_OP_F64_LE:
  2581. case WASM_OP_F64_GE:
  2582. POP_F64();
  2583. POP_F64();
  2584. PUSH_I32();
  2585. break;
  2586. break;
  2587. case WASM_OP_I32_CLZ:
  2588. case WASM_OP_I32_CTZ:
  2589. case WASM_OP_I32_POPCNT:
  2590. POP_I32();
  2591. PUSH_I32();
  2592. break;
  2593. case WASM_OP_I32_ADD:
  2594. case WASM_OP_I32_SUB:
  2595. case WASM_OP_I32_MUL:
  2596. case WASM_OP_I32_DIV_S:
  2597. case WASM_OP_I32_DIV_U:
  2598. case WASM_OP_I32_REM_S:
  2599. case WASM_OP_I32_REM_U:
  2600. case WASM_OP_I32_AND:
  2601. case WASM_OP_I32_OR:
  2602. case WASM_OP_I32_XOR:
  2603. case WASM_OP_I32_SHL:
  2604. case WASM_OP_I32_SHR_S:
  2605. case WASM_OP_I32_SHR_U:
  2606. case WASM_OP_I32_ROTL:
  2607. case WASM_OP_I32_ROTR:
  2608. POP_I32();
  2609. POP_I32();
  2610. PUSH_I32();
  2611. break;
  2612. case WASM_OP_I64_CLZ:
  2613. case WASM_OP_I64_CTZ:
  2614. case WASM_OP_I64_POPCNT:
  2615. POP_I64();
  2616. PUSH_I64();
  2617. break;
  2618. case WASM_OP_I64_ADD:
  2619. case WASM_OP_I64_SUB:
  2620. case WASM_OP_I64_MUL:
  2621. case WASM_OP_I64_DIV_S:
  2622. case WASM_OP_I64_DIV_U:
  2623. case WASM_OP_I64_REM_S:
  2624. case WASM_OP_I64_REM_U:
  2625. case WASM_OP_I64_AND:
  2626. case WASM_OP_I64_OR:
  2627. case WASM_OP_I64_XOR:
  2628. case WASM_OP_I64_SHL:
  2629. case WASM_OP_I64_SHR_S:
  2630. case WASM_OP_I64_SHR_U:
  2631. case WASM_OP_I64_ROTL:
  2632. case WASM_OP_I64_ROTR:
  2633. POP_I64();
  2634. POP_I64();
  2635. PUSH_I64();
  2636. break;
  2637. case WASM_OP_F32_ABS:
  2638. case WASM_OP_F32_NEG:
  2639. case WASM_OP_F32_CEIL:
  2640. case WASM_OP_F32_FLOOR:
  2641. case WASM_OP_F32_TRUNC:
  2642. case WASM_OP_F32_NEAREST:
  2643. case WASM_OP_F32_SQRT:
  2644. POP_F32();
  2645. PUSH_F32();
  2646. break;
  2647. case WASM_OP_F32_ADD:
  2648. case WASM_OP_F32_SUB:
  2649. case WASM_OP_F32_MUL:
  2650. case WASM_OP_F32_DIV:
  2651. case WASM_OP_F32_MIN:
  2652. case WASM_OP_F32_MAX:
  2653. case WASM_OP_F32_COPYSIGN:
  2654. POP_F32();
  2655. POP_F32();
  2656. PUSH_F32();
  2657. break;
  2658. case WASM_OP_F64_ABS:
  2659. case WASM_OP_F64_NEG:
  2660. case WASM_OP_F64_CEIL:
  2661. case WASM_OP_F64_FLOOR:
  2662. case WASM_OP_F64_TRUNC:
  2663. case WASM_OP_F64_NEAREST:
  2664. case WASM_OP_F64_SQRT:
  2665. POP_F64();
  2666. PUSH_F64();
  2667. break;
  2668. case WASM_OP_F64_ADD:
  2669. case WASM_OP_F64_SUB:
  2670. case WASM_OP_F64_MUL:
  2671. case WASM_OP_F64_DIV:
  2672. case WASM_OP_F64_MIN:
  2673. case WASM_OP_F64_MAX:
  2674. case WASM_OP_F64_COPYSIGN:
  2675. POP_F64();
  2676. POP_F64();
  2677. PUSH_F64();
  2678. break;
  2679. case WASM_OP_I32_WRAP_I64:
  2680. POP_I64();
  2681. PUSH_I32();
  2682. break;
  2683. case WASM_OP_I32_TRUNC_S_F32:
  2684. case WASM_OP_I32_TRUNC_U_F32:
  2685. POP_F32();
  2686. PUSH_I32();
  2687. break;
  2688. case WASM_OP_I32_TRUNC_S_F64:
  2689. case WASM_OP_I32_TRUNC_U_F64:
  2690. POP_F64();
  2691. PUSH_I32();
  2692. break;
  2693. case WASM_OP_I64_EXTEND_S_I32:
  2694. case WASM_OP_I64_EXTEND_U_I32:
  2695. POP_I32();
  2696. PUSH_I64();
  2697. break;
  2698. case WASM_OP_I64_TRUNC_S_F32:
  2699. case WASM_OP_I64_TRUNC_U_F32:
  2700. POP_F32();
  2701. PUSH_I64();
  2702. break;
  2703. case WASM_OP_I64_TRUNC_S_F64:
  2704. case WASM_OP_I64_TRUNC_U_F64:
  2705. POP_F64();
  2706. PUSH_I64();
  2707. break;
  2708. case WASM_OP_F32_CONVERT_S_I32:
  2709. case WASM_OP_F32_CONVERT_U_I32:
  2710. POP_I32();
  2711. PUSH_F32();
  2712. break;
  2713. case WASM_OP_F32_CONVERT_S_I64:
  2714. case WASM_OP_F32_CONVERT_U_I64:
  2715. POP_I64();
  2716. PUSH_F32();
  2717. break;
  2718. case WASM_OP_F32_DEMOTE_F64:
  2719. POP_F64();
  2720. PUSH_F32();
  2721. break;
  2722. case WASM_OP_F64_CONVERT_S_I32:
  2723. case WASM_OP_F64_CONVERT_U_I32:
  2724. POP_I32();
  2725. PUSH_F64();
  2726. break;
  2727. case WASM_OP_F64_CONVERT_S_I64:
  2728. case WASM_OP_F64_CONVERT_U_I64:
  2729. POP_I64();
  2730. PUSH_F64();
  2731. break;
  2732. case WASM_OP_F64_PROMOTE_F32:
  2733. POP_F32();
  2734. PUSH_F64();
  2735. break;
  2736. case WASM_OP_I32_REINTERPRET_F32:
  2737. POP_F32();
  2738. PUSH_I32();
  2739. break;
  2740. case WASM_OP_I64_REINTERPRET_F64:
  2741. POP_F64();
  2742. PUSH_I64();
  2743. break;
  2744. case WASM_OP_F32_REINTERPRET_I32:
  2745. POP_I32();
  2746. PUSH_F32();
  2747. break;
  2748. case WASM_OP_F64_REINTERPRET_I64:
  2749. POP_I64();
  2750. PUSH_F64();
  2751. break;
  2752. default:
  2753. if (error_buf != NULL)
  2754. snprintf(error_buf, error_buf_size,
  2755. "WASM module load failed: "
  2756. "invalid opcode %02x.", opcode);
  2757. goto fail;
  2758. }
  2759. if (opcode != WASM_OP_I32_CONST)
  2760. is_i32_const = false;
  2761. }
  2762. if (csp_num > 0) {
  2763. set_error_buf(error_buf, error_buf_size,
  2764. "WASM module load failed: "
  2765. "function body must end with END opcode.");
  2766. goto fail;
  2767. }
  2768. func->max_stack_cell_num = max_stack_cell_num;
  2769. func->max_block_num = max_csp_num;
  2770. return_value = true;
  2771. fail:
  2772. if (frame_ref_bottom)
  2773. wasm_free(frame_ref_bottom);
  2774. if (frame_csp_bottom)
  2775. wasm_free(frame_csp_bottom);
  2776. (void)u8;
  2777. (void)u32;
  2778. (void)i32;
  2779. (void)i64;
  2780. return return_value;
  2781. }