wasm-loader.c 98 KB

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