wasm_loader.c 106 KB

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