wasm_loader.c 98 KB

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