| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include "wasm_loader.h"
- #include "wasm.h"
- #include "wasm_native.h"
- #include "wasm_opcode.h"
- #include "wasm_runtime.h"
- #include "wasm_log.h"
- #include "wasm_memory.h"
- #include "wasm_dlfcn.h"
- /* Read a value of given type from the address pointed to by the given
- pointer and increase the pointer to the position just after the
- value being read. */
- #define TEMPLATE_READ_VALUE(Type, p) \
- (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
- static void
- set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
- {
- if (error_buf != NULL)
- snprintf(error_buf, error_buf_size, "%s", string);
- }
- #define CHECK_BUF(buf, buf_end, length) do { \
- if (buf + length > buf_end) { \
- set_error_buf(error_buf, error_buf_size, "unexpected end"); \
- return false; \
- } \
- } while (0)
- static bool
- read_leb(const uint8 *buf, const uint8 *buf_end,
- uint32 *p_offset, uint32 maxbits,
- bool sign, uint64 *p_result,
- char* error_buf, uint32 error_buf_size)
- {
- uint64 result = 0;
- uint32 shift = 0;
- uint32 bcnt = 0;
- uint64 byte;
- while (true) {
- CHECK_BUF(buf, buf_end, 1);
- byte = buf[*p_offset];
- *p_offset += 1;
- result |= ((byte & 0x7f) << shift);
- shift += 7;
- bcnt += 1;
- if ((byte & 0x80) == 0) {
- break;
- }
- }
- if (bcnt > (maxbits + 7 - 1) / 7) {
- set_error_buf(error_buf, error_buf_size,
- "integer representation too long");
- return false;
- }
- if (sign && (shift < maxbits) && (byte & 0x40)) {
- /* Sign extend */
- result |= - ((uint64)1 << shift);
- }
- *p_result = result;
- return true;
- }
- #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
- #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
- #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
- #define read_leb_uint64(p, p_end, res) do { \
- uint32 off = 0; \
- uint64 res64; \
- if (!read_leb(p, p_end, &off, 64, false, &res64, \
- error_buf, error_buf_size)) \
- return false; \
- p += off; \
- res = (uint64)res64; \
- } while (0)
- #define read_leb_int64(p, p_end, res) do { \
- uint32 off = 0; \
- uint64 res64; \
- if (!read_leb(p, p_end, &off, 64, true, &res64, \
- error_buf, error_buf_size)) \
- return false; \
- p += off; \
- res = (int64)res64; \
- } while (0)
- #define read_leb_uint32(p, p_end, res) do { \
- uint32 off = 0; \
- uint64 res64; \
- if (!read_leb(p, p_end, &off, 32, false, &res64, \
- error_buf, error_buf_size)) \
- return false; \
- p += off; \
- res = (uint32)res64; \
- } while (0)
- #define read_leb_int32(p, p_end, res) do { \
- uint32 off = 0; \
- uint64 res64; \
- if (!read_leb(p, p_end, &off, 32, true, &res64, \
- error_buf, error_buf_size)) \
- return false; \
- p += off; \
- res = (uint32)res64; \
- } while (0)
- #define read_leb_uint8(p, p_end, res) do { \
- uint32 off = 0; \
- uint64 res64; \
- if (!read_leb(p, p_end, &off, 7, false, &res64, \
- error_buf, error_buf_size)) \
- return false; \
- p += off; \
- res = (uint32)res64; \
- } while (0)
- static char*
- const_str_set_insert(const uint8 *str, int32 len, WASMModule *module,
- char* error_buf, uint32 error_buf_size)
- {
- HashMap *set = module->const_str_set;
- char *c_str = wasm_malloc(len + 1), *value;
- if (!c_str) {
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: "
- "allocate memory failed.");
- return NULL;
- }
- memcpy(c_str, str, len);
- c_str[len] = '\0';
- if ((value = wasm_hash_map_find(set, c_str))) {
- wasm_free(c_str);
- return value;
- }
- if (!wasm_hash_map_insert(set, c_str, c_str)) {
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: "
- "insert string to hash map failed.");
- wasm_free(c_str);
- return NULL;
- }
- return c_str;
- }
- static bool
- load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
- InitializerExpression *init_expr,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = *p_buf, *p_end = buf_end;
- uint8 flag, end_byte, *p_float;
- uint32 i;
- CHECK_BUF(p, p_end, 1);
- init_expr->init_expr_type = read_uint8(p);
- flag = init_expr->init_expr_type;
- switch (flag) {
- /* i32.const */
- case INIT_EXPR_TYPE_I32_CONST:
- read_leb_int32(p, p_end, init_expr->u.i32);
- break;
- /* i64.const */
- case INIT_EXPR_TYPE_I64_CONST:
- read_leb_int64(p, p_end, init_expr->u.i64);
- break;
- /* f32.const */
- case INIT_EXPR_TYPE_F32_CONST:
- CHECK_BUF(p, p_end, 4);
- p_float = (uint8*)&init_expr->u.f32;
- for (i = 0; i < sizeof(float32); i++)
- *p_float++ = *p++;
- break;
- /* f64.const */
- case INIT_EXPR_TYPE_F64_CONST:
- CHECK_BUF(p, p_end, 8);
- p_float = (uint8*)&init_expr->u.f64;
- for (i = 0; i < sizeof(float64); i++)
- *p_float++ = *p++;
- break;
- /* get_global */
- case INIT_EXPR_TYPE_GET_GLOBAL:
- read_leb_uint32(p, p_end, init_expr->u.global_index);
- break;
- default:
- set_error_buf(error_buf, error_buf_size, "type mismatch");
- return false;
- }
- CHECK_BUF(p, p_end, 1);
- end_byte = read_uint8(p);
- if (end_byte != 0x0b) {
- set_error_buf(error_buf, error_buf_size, "unexpected end");
- return false;
- }
- *p_buf = p;
- return true;
- }
- static bool
- load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end, *p_org;
- uint32 type_count, param_count, result_count, i, j;
- uint8 flag;
- WASMType *type;
- read_leb_uint32(p, p_end, type_count);
- if (type_count) {
- module->type_count = type_count;
- if (!(module->types = wasm_malloc(sizeof(WASMType*) * type_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load type section failed: allocate memory failed.");
- return false;
- }
- memset(module->types, 0, sizeof(WASMType*) * type_count);
- for (i = 0; i < type_count; i++) {
- CHECK_BUF(p, p_end, 1);
- flag = read_uint8(p);
- if (flag != 0x60) {
- set_error_buf(error_buf, error_buf_size,
- "Load type section failed: invalid type flag.");
- return false;
- }
- read_leb_uint32(p, p_end, param_count);
- /* Resolve param count and result count firstly */
- p_org = p;
- CHECK_BUF(p, p_end, param_count);
- p += param_count;
- read_leb_uint32(p, p_end, result_count);
- if (result_count > 1) {
- set_error_buf(error_buf, error_buf_size,
- "Load type section failed: invalid result count.");
- return false;
- }
- CHECK_BUF(p, p_end, result_count);
- p = p_org;
- if (!(type = module->types[i] = wasm_malloc(offsetof(WASMType, types) +
- sizeof(uint8) * (param_count + result_count)))) {
- set_error_buf(error_buf, error_buf_size,
- "Load type section failed: allocate memory failed.");
- return false;
- }
- /* Resolve param types and result types */
- type->param_count = param_count;
- type->result_count = result_count;
- for (j = 0; j < param_count; j++) {
- CHECK_BUF(p, p_end, 1);
- type->types[j] = read_uint8(p);
- }
- read_leb_uint32(p, p_end, result_count);
- for (j = 0; j < result_count; j++) {
- CHECK_BUF(p, p_end, 1);
- type->types[param_count + j] = read_uint8(p);
- }
- }
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load type section failed: invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load type section success.\n");
- return true;
- }
- static bool
- load_table_import(const uint8 **p_buf, const uint8 *buf_end,
- WASMTableImport *table,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = *p_buf, *p_end = buf_end;
- read_leb_uint8(p, p_end, table->elem_type);
- wasm_assert(table->elem_type == TABLE_ELEM_TYPE_ANY_FUNC);
- read_leb_uint32(p, p_end, table->flags);
- read_leb_uint32(p, p_end, table->init_size);
- if (table->flags & 1)
- read_leb_uint32(p, p_end, table->max_size);
- else
- table->max_size = 0x10000;
- *p_buf = p;
- return true;
- }
- static bool
- load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
- WASMMemoryImport *memory,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = *p_buf, *p_end = buf_end;
- uint32 pool_size = bh_memory_pool_size();
- uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
- / NumBytesPerPage;
- read_leb_uint32(p, p_end, memory->flags);
- read_leb_uint32(p, p_end, memory->init_page_count);
- if (memory->flags & 1) {
- read_leb_uint32(p, p_end, memory->max_page_count);
- if (memory->max_page_count > max_page_count)
- memory->max_page_count = max_page_count;
- }
- else
- /* Limit the maximum memory size to max_page_count */
- memory->max_page_count = max_page_count;
- *p_buf = p;
- return true;
- }
- static bool
- load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = *p_buf, *p_end = buf_end;
- read_leb_uint8(p, p_end, table->elem_type);
- wasm_assert(table->elem_type == TABLE_ELEM_TYPE_ANY_FUNC);
- read_leb_uint32(p, p_end, table->flags);
- read_leb_uint32(p, p_end, table->init_size);
- if (table->flags & 1)
- read_leb_uint32(p, p_end, table->max_size);
- else
- table->max_size = 0x10000;
- *p_buf = p;
- return true;
- }
- static bool
- load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = *p_buf, *p_end = buf_end;
- uint32 pool_size = bh_memory_pool_size();
- uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
- / NumBytesPerPage;
- read_leb_uint32(p, p_end, memory->flags);
- read_leb_uint32(p, p_end, memory->init_page_count);
- if (memory->flags & 1) {
- read_leb_uint32(p, p_end, memory->max_page_count);
- if (memory->max_page_count > max_page_count)
- memory->max_page_count = max_page_count;
- }
- else
- /* Limit the maximum memory size to max_page_count */
- memory->max_page_count = max_page_count;
- *p_buf = p;
- return true;
- }
- static void*
- resolve_sym(const char *module_name, const char *field_name)
- {
- void *sym;
- if (strcmp(module_name, "env") != 0)
- return NULL;
- if (field_name[0] == '_'
- && (sym = wasm_dlsym(NULL, field_name + 1)))
- return sym;
- if ((sym = wasm_dlsym(NULL, field_name)))
- return sym;
- return NULL;
- }
- static bool
- load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end, *p_old;
- uint32 import_count, name_len, type_index, i, u32, flags;
- WASMImport *import;
- WASMImport *import_functions = NULL, *import_tables = NULL;
- WASMImport *import_memories = NULL, *import_globals = NULL;
- char *module_name, *field_name;
- uint8 mutable, u8, kind;
- read_leb_uint32(p, p_end, import_count);
- if (import_count) {
- module->import_count = import_count;
- if (!(module->imports = wasm_malloc(sizeof(WASMImport) * import_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load import section failed: allocate memory failed.");
- return false;
- }
- memset(module->imports, 0, sizeof(WASMImport) * import_count);
- p_old = p;
- /* Scan firstly to get import count of each type */
- for (i = 0; i < import_count; i++) {
- /* module name */
- read_leb_uint32(p, p_end, name_len);
- CHECK_BUF(p, p_end, name_len);
- p += name_len;
- /* field name */
- read_leb_uint32(p, p_end, name_len);
- CHECK_BUF(p, p_end, name_len);
- p += name_len;
- read_leb_uint8(p, p_end, kind);
- switch (kind) {
- case IMPORT_KIND_FUNC: /* import function */
- read_leb_uint32(p, p_end, type_index);
- module->import_function_count++;
- break;
- case IMPORT_KIND_TABLE: /* import table */
- read_leb_uint8(p, p_end, u8);
- read_leb_uint32(p, p_end, flags);
- read_leb_uint32(p, p_end, u32);
- if (flags & 1)
- read_leb_uint32(p, p_end, u32);
- module->import_table_count++;
- if (module->import_table_count > 1) {
- set_error_buf(error_buf, error_buf_size, "multiple tables");
- return false;
- }
- break;
- case IMPORT_KIND_MEMORY: /* import memory */
- read_leb_uint32(p, p_end, flags);
- read_leb_uint32(p, p_end, u32);
- if (flags & 1)
- read_leb_uint32(p, p_end, u32);
- module->import_memory_count++;
- if (module->import_memory_count > 1) {
- set_error_buf(error_buf, error_buf_size, "multiple memories");
- return false;
- }
- break;
- case IMPORT_KIND_GLOBAL: /* import global */
- read_leb_uint8(p, p_end, u8);
- read_leb_uint8(p, p_end, u8);
- module->import_global_count++;
- break;
- default:
- set_error_buf(error_buf, error_buf_size,
- "Load import section failed: invalid import type.");
- return false;
- }
- }
- if (module->import_function_count)
- import_functions = module->import_functions = module->imports;
- if (module->import_table_count)
- import_tables = module->import_tables =
- module->imports + module->import_function_count;
- if (module->import_memory_count)
- import_memories = module->import_memories =
- module->imports + module->import_function_count + module->import_table_count;
- if (module->import_global_count)
- import_globals = module->import_globals =
- module->imports + module->import_function_count + module->import_table_count
- + module->import_memory_count;
- p = p_old;
- /* Scan again to read the data */
- for (i = 0; i < import_count; i++) {
- /* load module name */
- read_leb_uint32(p, p_end, name_len);
- CHECK_BUF(p, p_end, name_len);
- if (!(module_name = const_str_set_insert
- (p, name_len, module, error_buf, error_buf_size))) {
- return false;
- }
- p += name_len;
- /* load field name */
- read_leb_uint32(p, p_end, name_len);
- CHECK_BUF(p, p_end, name_len);
- if (!(field_name = const_str_set_insert
- (p, name_len, module, error_buf, error_buf_size))) {
- return false;
- }
- p += name_len;
- read_leb_uint8(p, p_end, kind);
- switch (kind) {
- case IMPORT_KIND_FUNC: /* import function */
- import = import_functions++;
- read_leb_uint32(p, p_end, type_index);
- if (type_index >= module->type_count) {
- set_error_buf(error_buf, error_buf_size,
- "Load import section failed: "
- "invalid function type index.");
- return false;
- }
- import->u.function.func_type = module->types[type_index];
- if (!(import->u.function.func_ptr_linked = wasm_native_func_lookup
- (module_name, field_name))) {
- if (!(import->u.function.func_ptr_linked =
- resolve_sym(module_name, field_name))) {
- if (error_buf != NULL)
- snprintf(error_buf, error_buf_size,
- "Load import section failed: "
- "resolve import function (%s, %s) failed.",
- module_name, field_name);
- return false;
- }
- import->u.function.call_type = CALL_TYPE_C_INTRINSIC;
- break;
- }
- import->u.function.call_type = CALL_TYPE_WRAPPER;
- break;
- case IMPORT_KIND_TABLE: /* import table */
- import = import_tables++;
- if (!load_table_import(&p, p_end, &import->u.table,
- error_buf, error_buf_size))
- return false;
- if (module->import_table_count > 1) {
- set_error_buf(error_buf, error_buf_size, "multiple tables");
- return false;
- }
- break;
- case IMPORT_KIND_MEMORY: /* import memory */
- import = import_memories++;
- if (!load_memory_import(&p, p_end, &import->u.memory,
- error_buf, error_buf_size))
- return false;
- if (module->import_table_count > 1) {
- set_error_buf(error_buf, error_buf_size, "multiple memories");
- return false;
- }
- break;
- case IMPORT_KIND_GLOBAL: /* import global */
- import = import_globals++;
- read_leb_uint8(p, p_end, import->u.global.type);
- read_leb_uint8(p, p_end, mutable);
- import->u.global.is_mutable = mutable & 1 ? true : false;
- if (!(wasm_native_global_lookup(module_name, field_name,
- &import->u.global))) {
- if (error_buf != NULL)
- snprintf(error_buf, error_buf_size,
- "Load import section failed: "
- "resolve import global (%s, %s) failed.",
- module_name, field_name);
- return false;
- }
- break;
- default:
- set_error_buf(error_buf, error_buf_size,
- "Load import section failed: "
- "invalid import type.");
- return false;
- }
- import->kind = kind;
- import->u.names.module_name = module_name;
- import->u.names.field_name = field_name;
- }
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load import section failed: "
- "invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load import section success.\n");
- (void)u8;
- (void)u32;
- return true;
- }
- static bool
- load_function_section(const uint8 *buf, const uint8 *buf_end,
- const uint8 *buf_code, const uint8 *buf_code_end,
- WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
- uint32 func_count, total_size;
- uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
- uint32 local_count, local_set_count, sub_local_count;
- uint8 type;
- WASMFunction *func;
- read_leb_uint32(p, p_end, func_count);
- if (buf_code)
- read_leb_uint32(p_code, buf_code_end, code_count);
- if (func_count != code_count) {
- set_error_buf(error_buf, error_buf_size,
- "function and code section have inconsistent lengths");
- return false;
- }
- if (func_count) {
- module->function_count = func_count;
- if (!(module->functions = wasm_malloc(sizeof(WASMFunction*) * func_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load function section failed: allocate memory failed.");
- return false;
- }
- memset(module->functions, 0, sizeof(WASMFunction*) * func_count);
- for (i = 0; i < func_count; i++) {
- /* Resolve function type */
- read_leb_uint32(p, p_end, type_index);
- if (type_index >= module->type_count) {
- set_error_buf(error_buf, error_buf_size,
- "Load function section failed: "
- "invalid function type index.");
- return false;
- }
- read_leb_uint32(p_code, buf_code_end, code_size);
- if (code_size == 0
- || p_code + code_size > buf_code_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load function section failed: "
- "invalid function code size.");
- return false;
- }
- /* Resolve local set count */
- p_code_end = p_code + code_size;
- local_count = 0;
- read_leb_uint32(p_code, buf_code_end, local_set_count);
- p_code_save = p_code;
- /* Calculate total local count */
- for (j = 0; j < local_set_count; j++) {
- read_leb_uint32(p_code, buf_code_end, sub_local_count);
- if (sub_local_count > UINT32_MAX - local_count) {
- set_error_buf(error_buf, error_buf_size,
- "too many locals");
- return false;
- }
- read_leb_uint8(p_code, buf_code_end, type);
- local_count += sub_local_count;
- }
- /* Alloc memory, layout: function structure + local types */
- code_size = p_code_end - p_code;
- total_size = sizeof(WASMFunction) + local_count;
- if (!(func = module->functions[i] = wasm_malloc(total_size))) {
- set_error_buf(error_buf, error_buf_size,
- "Load function section failed: "
- "allocate memory failed.");
- return false;
- }
- /* Set function type, local count, code size and code body */
- memset(func, 0, total_size);
- func->func_type = module->types[type_index];
- func->local_count = local_count;
- if (local_count > 0)
- func->local_types = (uint8*)func + sizeof(WASMFunction);
- func->code_size = code_size;
- func->code = (uint8*)p_code;
- /* Load each local type */
- p_code = p_code_save;
- local_type_index = 0;
- for (j = 0; j < local_set_count; j++) {
- read_leb_uint32(p_code, buf_code_end, sub_local_count);
- if (local_type_index + sub_local_count <= local_type_index
- || local_type_index + sub_local_count > local_count) {
- set_error_buf(error_buf, error_buf_size,
- "Load function section failed: "
- "invalid local count.");
- return false;
- }
- read_leb_uint8(p_code, buf_code_end, type);
- if (type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32) {
- set_error_buf(error_buf, error_buf_size,
- "Load function section failed: "
- "invalid local type.");
- return false;
- }
- for (k = 0; k < sub_local_count; k++) {
- func->local_types[local_type_index++] = type;
- }
- }
- p_code = p_code_end;
- }
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load function section failed: "
- "invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load function section success.\n");
- return true;
- }
- static bool
- load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- uint32 table_count, i;
- WASMTable *table;
- read_leb_uint32(p, p_end, table_count);
- wasm_assert(table_count == 1);
- if (table_count) {
- if (table_count > 1) {
- set_error_buf(error_buf, error_buf_size, "multiple memories");
- return false;
- }
- module->table_count = table_count;
- if (!(module->tables = wasm_malloc(sizeof(WASMTable) * table_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load table section failed: "
- "allocate memory failed.");
- return false;
- }
- memset(module->tables, 0, sizeof(WASMTable) * table_count);
- /* load each table */
- table = module->tables;
- for (i = 0; i < table_count; i++, table++)
- if (!load_table(&p, p_end, table, error_buf, error_buf_size))
- return false;
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load table section failed: invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load table section success.\n");
- return true;
- }
- static bool
- load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- uint32 memory_count, i;
- WASMMemory *memory;
- read_leb_uint32(p, p_end, memory_count);
- wasm_assert(memory_count == 1);
- if (memory_count) {
- if (memory_count > 1) {
- set_error_buf(error_buf, error_buf_size, "multiple memories");
- return false;
- }
- module->memory_count = memory_count;
- if (!(module->memories = wasm_malloc(sizeof(WASMMemory) * memory_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load memory section failed: "
- "allocate memory failed.");
- return false;
- }
- memset(module->memories, 0, sizeof(WASMMemory) * memory_count);
- /* load each memory */
- memory = module->memories;
- for (i = 0; i < memory_count; i++, memory++)
- if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
- return false;
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load memory section failed: invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load memory section success.\n");
- return true;
- }
- static bool
- load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- uint32 global_count, i;
- WASMGlobal *global;
- read_leb_uint32(p, p_end, global_count);
- if (global_count) {
- module->global_count = global_count;
- if (!(module->globals = wasm_malloc(sizeof(WASMGlobal) * global_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load global section failed: "
- "allocate memory failed.");
- return false;
- }
- memset(module->globals, 0, sizeof(WASMGlobal) * global_count);
- global = module->globals;
- for(i = 0; i < global_count; i++, global++) {
- CHECK_BUF(p, p_end, 1);
- global->type = read_uint8(p);
- CHECK_BUF(p, p_end, 1);
- global->is_mutable = read_bool(p);
- /* initialize expression */
- if (!load_init_expr(&p, p_end, &(global->init_expr), error_buf, error_buf_size))
- return false;
- }
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load global section failed: invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load global section success.\n");
- return true;
- }
- static bool
- load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- uint32 export_count, i, index;
- uint8 str_len;
- WASMExport *export;
- read_leb_uint32(p, p_end, export_count);
- if (export_count) {
- module->export_count = export_count;
- if (!(module->exports = wasm_malloc(sizeof(WASMExport) * export_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load export section failed: "
- "allocate memory failed.");
- return false;
- }
- memset(module->exports, 0, sizeof(WASMExport) * export_count);
- export = module->exports;
- for (i = 0; i < export_count; i++, export++) {
- read_leb_uint32(p, p_end, str_len);
- CHECK_BUF(p, p_end, str_len);
- if (!(export->name = const_str_set_insert(p, str_len, module,
- error_buf, error_buf_size))) {
- return false;
- }
- p += str_len;
- CHECK_BUF(p, p_end, 1);
- export->kind = read_uint8(p);
- read_leb_uint32(p, p_end, index);
- export->index = index;
- switch(export->kind) {
- /*function index*/
- case EXPORT_KIND_FUNC:
- if (index >= module->function_count + module->import_function_count) {
- set_error_buf(error_buf, error_buf_size,
- "Load export section failed: "
- "function index is out of range.");
- return false;
- }
- break;
- /*table index*/
- case EXPORT_KIND_TABLE:
- if (index >= module->table_count + module->import_table_count) {
- set_error_buf(error_buf, error_buf_size,
- "Load export section failed: "
- "table index is out of range.");
- return false;
- }
- break;
- /*memory index*/
- case EXPORT_KIND_MEMORY:
- if (index >= module->memory_count + module->import_memory_count) {
- set_error_buf(error_buf, error_buf_size,
- "Load export section failed: "
- "memory index is out of range.");
- return false;
- }
- break;
- /*global index*/
- case EXPORT_KIND_GLOBAL:
- if (index >= module->global_count + module->import_global_count) {
- set_error_buf(error_buf, error_buf_size,
- "Load export section failed: "
- "global index is out of range.");
- return false;
- }
- break;
- default:
- set_error_buf(error_buf, error_buf_size,
- "Load export section failed: "
- "kind flag is unexpected.");
- return false;
- }
- }
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load export section failed: "
- "invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load export section success.\n");
- return true;
- }
- static bool
- load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- uint32 table_segment_count, i, j, table_index, function_count, function_index;
- WASMTableSeg *table_segment;
- read_leb_uint32(p, p_end, table_segment_count);
- if (table_segment_count) {
- module->table_seg_count = table_segment_count;
- if (!(module->table_segments = wasm_malloc
- (sizeof(WASMTableSeg) * table_segment_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load table segment section failed: "
- "allocate memory failed.");
- return false;
- }
- memset(module->table_segments, 0, sizeof(WASMTableSeg) * table_segment_count);
- table_segment = module->table_segments;
- for (i = 0; i < table_segment_count; i++, table_segment++) {
- read_leb_uint32(p, p_end, table_index);
- table_segment->table_index = table_index;
- /* initialize expression */
- if (!load_init_expr(&p, p_end, &(table_segment->base_offset),
- error_buf, error_buf_size))
- return false;
- read_leb_uint32(p, p_end, function_count);
- table_segment->function_count = function_count;
- if (!(table_segment->func_indexes = (uint32 *)
- wasm_malloc(sizeof(uint32) * function_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load table segment section failed: "
- "allocate memory failed.");
- return false;
- }
- for (j = 0; j < function_count; j++) {
- read_leb_uint32(p, p_end, function_index);
- table_segment->func_indexes[j] = function_index;
- }
- }
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load table segment section failed, "
- "invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load table segment section success.\n");
- return true;
- }
- static bool
- load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
- WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- uint32 data_seg_count, i, mem_index, data_seg_len;
- WASMDataSeg *dataseg;
- InitializerExpression init_expr;
- read_leb_uint32(p, p_end, data_seg_count);
- if (data_seg_count) {
- module->data_seg_count = data_seg_count;
- if (!(module->data_segments =
- wasm_malloc(sizeof(WASMDataSeg*) * data_seg_count))) {
- set_error_buf(error_buf, error_buf_size,
- "Load data segment section failed: "
- "allocate memory failed.");
- return false;
- }
- memset(module->data_segments, 0, sizeof(WASMDataSeg*) * data_seg_count);
- for (i = 0; i < data_seg_count; i++) {
- read_leb_uint32(p, p_end, mem_index);
- if (!load_init_expr(&p, p_end, &init_expr, error_buf, error_buf_size))
- return false;
- read_leb_uint32(p, p_end, data_seg_len);
- if (!(dataseg = module->data_segments[i] =
- wasm_malloc(sizeof(WASMDataSeg)))) {
- set_error_buf(error_buf, error_buf_size,
- "Load data segment section failed: "
- "allocate memory failed.");
- return false;
- }
- memcpy(&dataseg->base_offset, &init_expr, sizeof(init_expr));
- dataseg->memory_index = mem_index;
- dataseg->data_length = data_seg_len;
- CHECK_BUF(p, p_end, data_seg_len);
- dataseg->data = (uint8*)p;
- p += data_seg_len;
- }
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load data segment section failed, "
- "invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load data segment section success.\n");
- return true;
- }
- static bool
- load_code_section(const uint8 *buf, const uint8 *buf_end,
- const uint8 *buf_func,
- const uint8 *buf_func_end,
- WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- const uint8 *p_func = buf_func;
- uint32 func_count = 0, code_count;
- /* code has been loaded in function section, so pass it here, just check
- * whether function and code section have inconsistent lengths */
- read_leb_uint32(p, p_end, code_count);
- if (buf_func)
- read_leb_uint32(p_func, buf_func_end, func_count);
- if (func_count != code_count) {
- set_error_buf(error_buf, error_buf_size,
- "function and code section have inconsistent lengths");
- return false;
- }
- LOG_VERBOSE("Load code segment section success.\n");
- return true;
- }
- static bool
- load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *p = buf, *p_end = buf_end;
- uint32 start_function;
- read_leb_uint32(p, p_end, start_function);
- if (start_function) {
- if (start_function >= module->function_count + module->import_function_count) {
- set_error_buf(error_buf, error_buf_size,
- "Load start section failed: "
- "function index is out of range.");
- return false;
- }
- module->start_function = start_function;
- }
- if (p != p_end) {
- set_error_buf(error_buf, error_buf_size,
- "Load start section failed: "
- "invalid section size.");
- return false;
- }
- LOG_VERBOSE("Load start section success.\n");
- return true;
- }
- static bool
- wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
- char *error_buf, uint32 error_buf_size);
- static bool
- load_from_sections(WASMModule *module, WASMSection *sections,
- char *error_buf, uint32 error_buf_size)
- {
- WASMSection *section = sections;
- const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
- *buf_func = NULL, *buf_func_end = NULL;
- uint32 i;
- /* Find code and function sections if have */
- while (section) {
- if (section->section_type == SECTION_TYPE_CODE) {
- buf_code = section->section_body;
- buf_code_end = buf_code + section->section_body_size;
- }
- else if (section->section_type == SECTION_TYPE_FUNC) {
- buf_func = section->section_body;
- buf_func_end = buf_func + section->section_body_size;
- }
- section = section->next;
- }
- section = sections;
- while (section) {
- buf = section->section_body;
- buf_end = buf + section->section_body_size;
- switch (section->section_type) {
- case SECTION_TYPE_USER:
- /* unsupported user section, ignore it. */
- /* add a check to pass spec test case */
- CHECK_BUF(buf, buf_end, 1);
- break;
- case SECTION_TYPE_TYPE:
- if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_IMPORT:
- if (!load_import_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_FUNC:
- if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
- module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_TABLE:
- if (!load_table_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_MEMORY:
- if (!load_memory_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_GLOBAL:
- if (!load_global_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_EXPORT:
- if (!load_export_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_START:
- if (!load_start_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_ELEM:
- if (!load_table_segment_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_CODE:
- if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
- module, error_buf, error_buf_size))
- return false;
- break;
- case SECTION_TYPE_DATA:
- if (!load_data_segment_section(buf, buf_end, module, error_buf, error_buf_size))
- return false;
- break;
- default:
- set_error_buf(error_buf, error_buf_size, "invalid section id");
- return false;
- }
- section = section->next;
- }
- for (i = 0; i < module->function_count; i++) {
- WASMFunction *func = module->functions[i];
- if (!wasm_loader_prepare_bytecode(module, func, error_buf, error_buf_size))
- return false;
- }
- return true;
- }
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- static uint32
- branch_set_hash(const void *key)
- {
- return ((uintptr_t)key) ^ ((uintptr_t)key >> 16);
- }
- static bool
- branch_set_key_equal(void *start_addr1, void *start_addr2)
- {
- return start_addr1 == start_addr2 ? true : false;
- }
- static void
- branch_set_value_destroy(void *value)
- {
- wasm_free(value);
- }
- #endif
- #if BEIHAI_ENABLE_MEMORY_PROFILING != 0
- static void wasm_loader_free(void *ptr)
- {
- wasm_free(ptr);
- }
- #else
- #define wasm_loader_free wasm_free
- #endif
- static WASMModule*
- create_module(char *error_buf, uint32 error_buf_size)
- {
- WASMModule *module = wasm_malloc(sizeof(WASMModule));
- if (!module) {
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: "
- "allocate memory failed.");
- return NULL;
- }
- memset(module, 0, sizeof(WASMModule));
- /* Set start_function to -1, means no start function */
- module->start_function = (uint32)-1;
- if (!(module->const_str_set = wasm_hash_map_create(32, false,
- (HashFunc)wasm_string_hash,
- (KeyEqualFunc)wasm_string_equal,
- NULL,
- wasm_loader_free)))
- goto fail;
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- if (!(module->branch_set = wasm_hash_map_create(64, true,
- branch_set_hash,
- branch_set_key_equal,
- NULL,
- branch_set_value_destroy)))
- goto fail;
- #endif
- return module;
- fail:
- wasm_loader_unload(module);
- return NULL;
- }
- WASMModule *
- wasm_loader_load_from_sections(WASMSection *section_list,
- char *error_buf, uint32 error_buf_size)
- {
- WASMModule *module = create_module(error_buf, error_buf_size);
- if (!module)
- return NULL;
- if (!load_from_sections(module, section_list, error_buf, error_buf_size)) {
- wasm_loader_unload(module);
- return NULL;
- }
- LOG_VERBOSE("Load module from sections success.\n");
- return module;
- }
- static void
- destroy_sections(WASMSection *section_list)
- {
- WASMSection *section = section_list, *next;
- while (section) {
- next = section->next;
- wasm_free(section);
- section = next;
- }
- }
- static bool
- create_sections(const uint8 *buf, uint32 size,
- WASMSection **p_section_list,
- char *error_buf, uint32 error_buf_size)
- {
- WASMSection *section_list_end = NULL, *section;
- const uint8 *p = buf, *p_end = buf + size/*, *section_body*/;
- uint8 section_type;
- uint32 section_size;
- p += 8;
- while (p < p_end) {
- CHECK_BUF(p, p_end, 1);
- section_type = read_uint8(p);
- if (section_type <= SECTION_TYPE_DATA) {
- read_leb_uint32(p, p_end, section_size);
- CHECK_BUF(p, p_end, section_size);
- if (!(section = wasm_malloc(sizeof(WASMSection)))) {
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: "
- "allocate memory failed.");
- return false;
- }
- memset(section, 0, sizeof(WASMSection));
- section->section_type = section_type;
- section->section_body = p;
- section->section_body_size = section_size;
- if (!*p_section_list)
- *p_section_list = section_list_end = section;
- else {
- section_list_end->next = section;
- section_list_end = section;
- }
- p += section_size;
- }
- else {
- set_error_buf(error_buf, error_buf_size, "invalid section id");
- return false;
- }
- }
- return true;
- }
- static void
- exchange32(uint8* p_data)
- {
- uint8 value = *p_data;
- *p_data = *(p_data + 3);
- *(p_data + 3) = value;
- value = *(p_data + 1);
- *(p_data + 1) = *(p_data + 2);
- *(p_data + 2) = value;
- }
- static union {
- int a;
- char b;
- } __ue = { .a = 1 };
- #define is_little_endian() (__ue.b == 1)
- static bool
- load(const uint8 *buf, uint32 size, WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- const uint8 *buf_end = buf + size;
- const uint8 *p = buf, *p_end = buf_end;
- uint32 magic_number, version;
- WASMSection *section_list = NULL;
- CHECK_BUF(p, p_end, sizeof(uint32));
- magic_number = read_uint32(p);
- if (!is_little_endian())
- exchange32((uint8*)&magic_number);
- if (magic_number != WASM_MAGIC_NUMBER) {
- set_error_buf(error_buf, error_buf_size, "magic header not detected");
- return false;
- }
- CHECK_BUF(p, p_end, sizeof(uint32));
- version = read_uint32(p);
- if (!is_little_endian())
- exchange32((uint8*)&version);
- if (version != WASM_CURRENT_VERSION) {
- set_error_buf(error_buf, error_buf_size, "unknown binary version");
- return false;
- }
- if (!create_sections(buf, size, §ion_list, error_buf, error_buf_size)
- || !load_from_sections(module, section_list, error_buf, error_buf_size)) {
- destroy_sections(section_list);
- return false;
- }
- destroy_sections(section_list);
- return true;
- }
- WASMModule*
- wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
- {
- WASMModule *module = wasm_malloc(sizeof(WASMModule));
- if (!module) {
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: allocate memory failed.");
- return NULL;
- }
- memset(module, 0, sizeof(WASMModule));
- /* Set start_function to -1, means no start function */
- module->start_function = (uint32)-1;
- if (!(module->const_str_set = wasm_hash_map_create(32, false,
- (HashFunc)wasm_string_hash,
- (KeyEqualFunc)wasm_string_equal,
- NULL,
- wasm_loader_free)))
- goto fail;
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- if (!(module->branch_set = wasm_hash_map_create(64, true,
- branch_set_hash,
- branch_set_key_equal,
- NULL,
- branch_set_value_destroy)))
- goto fail;
- #endif
- if (!load(buf, size, module, error_buf, error_buf_size))
- goto fail;
- LOG_VERBOSE("Load module success.\n");
- return module;
- fail:
- wasm_loader_unload(module);
- return NULL;
- }
- void
- wasm_loader_unload(WASMModule *module)
- {
- uint32 i;
- if (!module)
- return;
- if (module->types) {
- for (i = 0; i < module->type_count; i++) {
- if (module->types[i])
- wasm_free(module->types[i]);
- }
- wasm_free(module->types);
- }
- if (module->imports)
- wasm_free(module->imports);
- if (module->functions) {
- for (i = 0; i < module->function_count; i++) {
- if (module->functions[i])
- wasm_free(module->functions[i]);
- }
- wasm_free(module->functions);
- }
- if (module->tables)
- wasm_free(module->tables);
- if (module->memories)
- wasm_free(module->memories);
- if (module->globals)
- wasm_free(module->globals);
- if (module->exports)
- wasm_free(module->exports);
- if (module->table_segments) {
- for (i = 0; i < module->table_seg_count; i++) {
- if (module->table_segments[i].func_indexes)
- wasm_free(module->table_segments[i].func_indexes);
- }
- wasm_free(module->table_segments);
- }
- if (module->data_segments) {
- for (i = 0; i < module->data_seg_count; i++) {
- if (module->data_segments[i])
- wasm_free(module->data_segments[i]);
- }
- wasm_free(module->data_segments);
- }
- if (module->const_str_set)
- wasm_hash_map_destroy(module->const_str_set);
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- if (module->branch_set)
- wasm_hash_map_destroy(module->branch_set);
- #endif
- wasm_free(module);
- }
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- typedef struct block_addr {
- uint8 block_type;
- uint8 *end_addr;
- uint8 *else_addr;
- } block_addr;
- #endif
- bool
- wasm_loader_find_block_addr(WASMModule *module,
- const uint8 *start_addr,
- const uint8 *code_end_addr,
- uint8 block_type,
- uint8 **p_else_addr,
- uint8 **p_end_addr,
- char *error_buf,
- uint32 error_buf_size)
- {
- const uint8 *p = start_addr, *p_end = code_end_addr;
- uint8 *else_addr = NULL;
- uint32 block_nested_depth = 1, count, i, u32, u64;
- uint8 opcode, u8;
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- HashMap *branch_set = module->branch_set;
- block_addr *block;
- if ((block = wasm_hash_map_find(branch_set, (void*)start_addr))) {
- if (block->block_type != block_type)
- return false;
- if (block_type == BLOCK_TYPE_IF) /* if block */
- *p_else_addr = block->else_addr;
- *p_end_addr = block->end_addr;
- return true;
- }
- #else
- BlockAddr block_stack[16] = { 0 }, *block;
- uint32 j, t;
- i = ((uintptr_t)start_addr) ^ ((uintptr_t)start_addr >> 16);
- i = i % BLOCK_ADDR_CACHE_SIZE;
- block = module->block_addr_cache[i];
- for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
- if (block[j].start_addr == start_addr) {
- /* Cache hit */
- *p_else_addr = block[j].else_addr;
- *p_end_addr = block[j].end_addr;
- return true;
- }
- }
- /* Cache unhit */
- block_stack[0].start_addr = start_addr;
- #endif
- while (p < code_end_addr) {
- opcode = *p++;
- switch (opcode) {
- case WASM_OP_UNREACHABLE:
- case WASM_OP_NOP:
- break;
- case WASM_OP_BLOCK:
- case WASM_OP_LOOP:
- case WASM_OP_IF:
- read_leb_uint32(p, p_end, u32); /* blocktype */
- #if WASM_ENABLE_HASH_BLOCK_ADDR == 0
- if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
- block_stack[block_nested_depth].start_addr = p;
- block_stack[block_nested_depth].else_addr = NULL;
- }
- #endif
- block_nested_depth++;
- break;
- case WASM_OP_ELSE:
- if (block_type == BLOCK_TYPE_IF && block_nested_depth == 1)
- else_addr = (uint8*)(p - 1);
- #if WASM_ENABLE_HASH_BLOCK_ADDR == 0
- if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr))
- block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1);
- #endif
- break;
- case WASM_OP_END:
- if (block_nested_depth == 1) {
- if (block_type == BLOCK_TYPE_IF)
- *p_else_addr = else_addr;
- *p_end_addr = (uint8*)(p - 1);
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- if (block_type == BLOCK_TYPE_IF)
- block = wasm_malloc(sizeof(block_addr));
- else
- block = wasm_malloc(offsetof(block_addr, else_addr));
- if (block) {
- block->block_type = block_type;
- if (block_type == BLOCK_TYPE_IF)
- block->else_addr = else_addr;
- block->end_addr = (uint8*)(p - 1);
- if (!wasm_hash_map_insert(branch_set, (void*)start_addr, block))
- wasm_free(block);
- }
- #else
- block_stack[0].end_addr = (uint8*)(p - 1);
- for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
- start_addr = block_stack[t].start_addr;
- if (start_addr) {
- i = ((uintptr_t)start_addr) ^ ((uintptr_t)start_addr >> 16);
- i = i % BLOCK_ADDR_CACHE_SIZE;
- block = module->block_addr_cache[i];
- for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
- if (!block[j].start_addr)
- break;
- if (j == BLOCK_ADDR_CONFLICT_SIZE) {
- memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) *
- sizeof(BlockAddr));
- j = 0;
- }
- block[j].start_addr = block_stack[t].start_addr;
- block[j].else_addr = block_stack[t].else_addr;
- block[j].end_addr = block_stack[t].end_addr;
- }
- else
- break;
- }
- #endif
- return true;
- }
- else {
- block_nested_depth--;
- #if WASM_ENABLE_HASH_BLOCK_ADDR == 0
- if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr))
- block_stack[block_nested_depth].end_addr = (uint8*)(p - 1);
- #endif
- }
- break;
- case WASM_OP_BR:
- case WASM_OP_BR_IF:
- read_leb_uint32(p, p_end, u32); /* labelidx */
- break;
- case WASM_OP_BR_TABLE:
- read_leb_uint32(p, p_end, count); /* lable num */
- for (i = 0; i <= count; i++) /* lableidxs */
- read_leb_uint32(p, p_end, u32);
- break;
- case WASM_OP_RETURN:
- break;
- case WASM_OP_CALL:
- read_leb_uint32(p, p_end, u32); /* funcidx */
- break;
- case WASM_OP_CALL_INDIRECT:
- read_leb_uint32(p, p_end, u32); /* typeidx */
- read_leb_uint8(p, p_end, u8); /* 0x00 */
- break;
- case WASM_OP_DROP:
- case WASM_OP_SELECT:
- case WASM_OP_DROP_32:
- case WASM_OP_DROP_64:
- case WASM_OP_SELECT_32:
- case WASM_OP_SELECT_64:
- break;
- case WASM_OP_GET_LOCAL:
- case WASM_OP_SET_LOCAL:
- case WASM_OP_TEE_LOCAL:
- case WASM_OP_GET_GLOBAL:
- case WASM_OP_SET_GLOBAL:
- read_leb_uint32(p, p_end, u32); /* localidx */
- break;
- case WASM_OP_I32_LOAD:
- case WASM_OP_I64_LOAD:
- case WASM_OP_F32_LOAD:
- case WASM_OP_F64_LOAD:
- case WASM_OP_I32_LOAD8_S:
- case WASM_OP_I32_LOAD8_U:
- case WASM_OP_I32_LOAD16_S:
- case WASM_OP_I32_LOAD16_U:
- case WASM_OP_I64_LOAD8_S:
- case WASM_OP_I64_LOAD8_U:
- case WASM_OP_I64_LOAD16_S:
- case WASM_OP_I64_LOAD16_U:
- case WASM_OP_I64_LOAD32_S:
- case WASM_OP_I64_LOAD32_U:
- case WASM_OP_I32_STORE:
- case WASM_OP_I64_STORE:
- case WASM_OP_F32_STORE:
- case WASM_OP_F64_STORE:
- case WASM_OP_I32_STORE8:
- case WASM_OP_I32_STORE16:
- case WASM_OP_I64_STORE8:
- case WASM_OP_I64_STORE16:
- case WASM_OP_I64_STORE32:
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- break;
- case WASM_OP_MEMORY_SIZE:
- case WASM_OP_MEMORY_GROW:
- read_leb_uint32(p, p_end, u32); /* 0x00 */
- break;
- case WASM_OP_I32_CONST:
- read_leb_uint32(p, p_end, u32);
- break;
- case WASM_OP_I64_CONST:
- read_leb_uint64(p, p_end, u64);
- break;
- case WASM_OP_F32_CONST:
- p += sizeof(float32);
- break;
- case WASM_OP_F64_CONST:
- p += sizeof(float64);
- break;
- case WASM_OP_I32_EQZ:
- case WASM_OP_I32_EQ:
- case WASM_OP_I32_NE:
- case WASM_OP_I32_LT_S:
- case WASM_OP_I32_LT_U:
- case WASM_OP_I32_GT_S:
- case WASM_OP_I32_GT_U:
- case WASM_OP_I32_LE_S:
- case WASM_OP_I32_LE_U:
- case WASM_OP_I32_GE_S:
- case WASM_OP_I32_GE_U:
- case WASM_OP_I64_EQZ:
- case WASM_OP_I64_EQ:
- case WASM_OP_I64_NE:
- case WASM_OP_I64_LT_S:
- case WASM_OP_I64_LT_U:
- case WASM_OP_I64_GT_S:
- case WASM_OP_I64_GT_U:
- case WASM_OP_I64_LE_S:
- case WASM_OP_I64_LE_U:
- case WASM_OP_I64_GE_S:
- case WASM_OP_I64_GE_U:
- case WASM_OP_F32_EQ:
- case WASM_OP_F32_NE:
- case WASM_OP_F32_LT:
- case WASM_OP_F32_GT:
- case WASM_OP_F32_LE:
- case WASM_OP_F32_GE:
- case WASM_OP_F64_EQ:
- case WASM_OP_F64_NE:
- case WASM_OP_F64_LT:
- case WASM_OP_F64_GT:
- case WASM_OP_F64_LE:
- case WASM_OP_F64_GE:
- case WASM_OP_I32_CLZ:
- case WASM_OP_I32_CTZ:
- case WASM_OP_I32_POPCNT:
- case WASM_OP_I32_ADD:
- case WASM_OP_I32_SUB:
- case WASM_OP_I32_MUL:
- case WASM_OP_I32_DIV_S:
- case WASM_OP_I32_DIV_U:
- case WASM_OP_I32_REM_S:
- case WASM_OP_I32_REM_U:
- case WASM_OP_I32_AND:
- case WASM_OP_I32_OR:
- case WASM_OP_I32_XOR:
- case WASM_OP_I32_SHL:
- case WASM_OP_I32_SHR_S:
- case WASM_OP_I32_SHR_U:
- case WASM_OP_I32_ROTL:
- case WASM_OP_I32_ROTR:
- case WASM_OP_I64_CLZ:
- case WASM_OP_I64_CTZ:
- case WASM_OP_I64_POPCNT:
- case WASM_OP_I64_ADD:
- case WASM_OP_I64_SUB:
- case WASM_OP_I64_MUL:
- case WASM_OP_I64_DIV_S:
- case WASM_OP_I64_DIV_U:
- case WASM_OP_I64_REM_S:
- case WASM_OP_I64_REM_U:
- case WASM_OP_I64_AND:
- case WASM_OP_I64_OR:
- case WASM_OP_I64_XOR:
- case WASM_OP_I64_SHL:
- case WASM_OP_I64_SHR_S:
- case WASM_OP_I64_SHR_U:
- case WASM_OP_I64_ROTL:
- case WASM_OP_I64_ROTR:
- case WASM_OP_F32_ABS:
- case WASM_OP_F32_NEG:
- case WASM_OP_F32_CEIL:
- case WASM_OP_F32_FLOOR:
- case WASM_OP_F32_TRUNC:
- case WASM_OP_F32_NEAREST:
- case WASM_OP_F32_SQRT:
- case WASM_OP_F32_ADD:
- case WASM_OP_F32_SUB:
- case WASM_OP_F32_MUL:
- case WASM_OP_F32_DIV:
- case WASM_OP_F32_MIN:
- case WASM_OP_F32_MAX:
- case WASM_OP_F32_COPYSIGN:
- case WASM_OP_F64_ABS:
- case WASM_OP_F64_NEG:
- case WASM_OP_F64_CEIL:
- case WASM_OP_F64_FLOOR:
- case WASM_OP_F64_TRUNC:
- case WASM_OP_F64_NEAREST:
- case WASM_OP_F64_SQRT:
- case WASM_OP_F64_ADD:
- case WASM_OP_F64_SUB:
- case WASM_OP_F64_MUL:
- case WASM_OP_F64_DIV:
- case WASM_OP_F64_MIN:
- case WASM_OP_F64_MAX:
- case WASM_OP_F64_COPYSIGN:
- case WASM_OP_I32_WRAP_I64:
- case WASM_OP_I32_TRUNC_S_F32:
- case WASM_OP_I32_TRUNC_U_F32:
- case WASM_OP_I32_TRUNC_S_F64:
- case WASM_OP_I32_TRUNC_U_F64:
- case WASM_OP_I64_EXTEND_S_I32:
- case WASM_OP_I64_EXTEND_U_I32:
- case WASM_OP_I64_TRUNC_S_F32:
- case WASM_OP_I64_TRUNC_U_F32:
- case WASM_OP_I64_TRUNC_S_F64:
- case WASM_OP_I64_TRUNC_U_F64:
- case WASM_OP_F32_CONVERT_S_I32:
- case WASM_OP_F32_CONVERT_U_I32:
- case WASM_OP_F32_CONVERT_S_I64:
- case WASM_OP_F32_CONVERT_U_I64:
- case WASM_OP_F32_DEMOTE_F64:
- case WASM_OP_F64_CONVERT_S_I32:
- case WASM_OP_F64_CONVERT_U_I32:
- case WASM_OP_F64_CONVERT_S_I64:
- case WASM_OP_F64_CONVERT_U_I64:
- case WASM_OP_F64_PROMOTE_F32:
- case WASM_OP_I32_REINTERPRET_F32:
- case WASM_OP_I64_REINTERPRET_F64:
- case WASM_OP_F32_REINTERPRET_I32:
- case WASM_OP_F64_REINTERPRET_I64:
- break;
- default:
- if (error_buf)
- snprintf(error_buf, error_buf_size,
- "WASM loader find block addr failed: "
- "invalid opcode %02x.", opcode);
- return false;
- }
- }
- (void)u32;
- (void)u64;
- (void)u8;
- return false;
- }
- #define REF_I32 VALUE_TYPE_I32
- #define REF_F32 VALUE_TYPE_F32
- #define REF_I64_1 VALUE_TYPE_I64
- #define REF_I64_2 VALUE_TYPE_I64
- #define REF_F64_1 VALUE_TYPE_F64
- #define REF_F64_2 VALUE_TYPE_F64
- typedef struct BranchBlock {
- uint8 block_type;
- uint8 return_type;
- bool jumped_by_br;
- uint8 *start_addr;
- uint8 *else_addr;
- uint8 *end_addr;
- uint32 stack_cell_num;
- } BranchBlock;
- static void*
- memory_realloc(void *mem_old, uint32 size_old, uint32 size_new)
- {
- uint8 *mem_new;
- wasm_assert(size_new > size_old);
- if ((mem_new = wasm_malloc(size_new))) {
- memcpy(mem_new, mem_old, size_old);
- memset(mem_new + size_old, 0, size_new - size_old);
- wasm_free(mem_old);
- }
- return mem_new;
- }
- #define MEM_REALLOC(mem, size_old, size_new) do { \
- void *mem_new = memory_realloc(mem, size_old, size_new);\
- if (!mem_new) { \
- set_error_buf(error_buf, error_buf_size, \
- "WASM loader prepare bytecode failed: " \
- "allocate memory failed."); \
- goto fail; \
- } \
- mem = mem_new; \
- } while (0)
- static bool
- check_stack_push(uint8 **p_frame_ref_bottom, uint8 **p_frame_ref_boundary,
- uint8 **p_frame_ref, uint32 *p_frame_ref_size,
- uint32 stack_cell_num,
- char *error_buf, uint32 error_buf_size)
- {
- if (*p_frame_ref >= *p_frame_ref_boundary) {
- MEM_REALLOC(*p_frame_ref_bottom, *p_frame_ref_size,
- *p_frame_ref_size + 16);
- *p_frame_ref_size += 16;
- *p_frame_ref_boundary = *p_frame_ref_bottom + *p_frame_ref_size;
- *p_frame_ref = *p_frame_ref_bottom + stack_cell_num;
- }
- return true;
- fail:
- return false;
- }
- #define CHECK_STACK_PUSH() do { \
- if (!check_stack_push(&frame_ref_bottom, &frame_ref_boundary,\
- &frame_ref, &frame_ref_size, \
- stack_cell_num, \
- error_buf, error_buf_size)) \
- goto fail; \
- } while (0)
- static bool
- check_stack_pop(uint8 type, uint8 *frame_ref, uint32 stack_cell_num,
- char *error_buf, uint32 error_buf_size,
- const char *type_str)
- {
- if (((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
- && stack_cell_num < 1)
- || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
- && stack_cell_num < 2)) {
- set_error_buf(error_buf, error_buf_size,
- "type mismatch: expected data but stack was empty");
- return false;
- }
- if ((type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
- || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
- || (type == VALUE_TYPE_I64
- && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2))
- || (type == VALUE_TYPE_F64
- && (*(frame_ref - 2) != REF_F64_1 || *(frame_ref - 1) != REF_F64_2))) {
- if (error_buf != NULL)
- snprintf(error_buf, error_buf_size, "%s%s%s",
- "type mismatch: expected ", type_str, " but got other");
- return false;
- }
- return true;
- }
- #define CHECK_STACK_POP(TYPE, type) do { \
- if (!check_stack_pop(VALUE_TYPE_##TYPE, \
- frame_ref, stack_cell_num, \
- error_buf, error_buf_size, #type)) \
- goto fail; \
- } while (0)
- #define PUSH_I32() do { \
- CHECK_STACK_PUSH(); \
- *frame_ref++ = REF_I32; \
- stack_cell_num++; \
- if (stack_cell_num > max_stack_cell_num) \
- max_stack_cell_num = stack_cell_num; \
- } while (0)
- #define PUSH_F32() do { \
- CHECK_STACK_PUSH(); \
- *frame_ref++ = REF_F32; \
- stack_cell_num++; \
- if (stack_cell_num > max_stack_cell_num) \
- max_stack_cell_num = stack_cell_num; \
- } while (0)
- #define PUSH_I64() do { \
- CHECK_STACK_PUSH(); \
- *frame_ref++ = REF_I64_1; \
- stack_cell_num++; \
- CHECK_STACK_PUSH(); \
- *frame_ref++ = REF_I64_2; \
- stack_cell_num++; \
- if (stack_cell_num > max_stack_cell_num) \
- max_stack_cell_num = stack_cell_num; \
- } while (0)
- #define PUSH_F64() do { \
- CHECK_STACK_PUSH(); \
- *frame_ref++ = REF_F64_1; \
- stack_cell_num++; \
- CHECK_STACK_PUSH(); \
- *frame_ref++ = REF_F64_2; \
- stack_cell_num++; \
- if (stack_cell_num > max_stack_cell_num) \
- max_stack_cell_num = stack_cell_num; \
- } while (0)
- #define POP_I32() do { \
- CHECK_STACK_POP(I32, i32); \
- stack_cell_num--; \
- frame_ref--; \
- } while (0)
- #define POP_I64() do { \
- CHECK_STACK_POP(I64, i64); \
- stack_cell_num -= 2; \
- frame_ref -= 2; \
- } while (0)
- #define POP_F32() do { \
- CHECK_STACK_POP(F32, f32); \
- stack_cell_num--; \
- frame_ref--; \
- } while (0)
- #define POP_F64() do { \
- CHECK_STACK_POP(F64, f64); \
- stack_cell_num -= 2; \
- frame_ref -= 2; \
- } while (0)
- static bool
- push_type(uint8 type, uint8 **p_frame_ref_bottom,
- uint8 **p_frame_ref_boundary,
- uint8 **p_frame_ref, uint32 *p_frame_ref_size,
- uint32 *p_stack_cell_num, uint32 *p_max_stack_cell_num,
- char *error_buf, uint32 error_buf_size)
- {
- uint8 *frame_ref = *p_frame_ref;
- uint32 frame_ref_size = *p_frame_ref_size;
- uint32 max_stack_cell_num = *p_max_stack_cell_num;
- uint32 stack_cell_num = *p_stack_cell_num;
- switch (type) {
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- if (!check_stack_push(p_frame_ref_bottom, p_frame_ref_boundary,
- &frame_ref, &frame_ref_size,
- stack_cell_num,
- error_buf, error_buf_size))
- goto fail;
- *frame_ref++ = type;
- stack_cell_num++;
- if (stack_cell_num > max_stack_cell_num)
- max_stack_cell_num = stack_cell_num;
- case VALUE_TYPE_I32:
- case VALUE_TYPE_F32:
- if (!check_stack_push(p_frame_ref_bottom, p_frame_ref_boundary,
- &frame_ref, &frame_ref_size,
- stack_cell_num,
- error_buf, error_buf_size))
- goto fail;
- *frame_ref++ = type;
- stack_cell_num++;
- if (stack_cell_num > max_stack_cell_num)
- max_stack_cell_num = stack_cell_num;
- break;
- }
- *p_frame_ref = frame_ref;
- *p_frame_ref_size = frame_ref_size;
- *p_max_stack_cell_num = max_stack_cell_num;
- *p_stack_cell_num = stack_cell_num;
- return true;
- fail:
- return false;
- }
- #define PUSH_TYPE(type) do { \
- if (!push_type(type, &frame_ref_bottom, \
- &frame_ref_boundary, \
- &frame_ref, &frame_ref_size, \
- &stack_cell_num, &max_stack_cell_num, \
- error_buf, error_buf_size)) \
- goto fail; \
- } while (0)
- static bool
- pop_type(uint8 type, uint8 **p_frame_ref, uint32 *p_stack_cell_num,
- char *error_buf, uint32 error_buf_size)
- {
- char *type_str[] = { "f64", "f32", "i64", "i32" };
- switch (type) {
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- if (!check_stack_pop(type, *p_frame_ref, *p_stack_cell_num,
- error_buf, error_buf_size,
- type_str[type - VALUE_TYPE_F64]))
- return false;
- *p_frame_ref -= 2;
- *p_stack_cell_num -= 2;
- break;
- case VALUE_TYPE_I32:
- case VALUE_TYPE_F32:
- if (!check_stack_pop(type, *p_frame_ref, *p_stack_cell_num,
- error_buf, error_buf_size,
- type_str[type - VALUE_TYPE_F64]))
- return false;
- *p_frame_ref -= 1;
- *p_stack_cell_num -= 1;
- break;
- }
- return true;
- }
- #define POP_TYPE(type) do { \
- if (!pop_type(type, &frame_ref, &stack_cell_num,\
- error_buf, error_buf_size)) \
- goto fail; \
- } while (0)
- #define CHECK_CSP_PUSH() do { \
- if (frame_csp >= frame_csp_boundary) { \
- MEM_REALLOC(frame_csp_bottom, frame_csp_size, \
- frame_csp_size + 8 * sizeof(BranchBlock));\
- frame_csp_size += 8 * sizeof(BranchBlock); \
- frame_csp_boundary = frame_csp_bottom + \
- frame_csp_size / sizeof(BranchBlock); \
- frame_csp = frame_csp_bottom + csp_num; \
- } \
- } while (0)
- #define CHECK_CSP_POP() do { \
- if (csp_num < 1) { \
- set_error_buf(error_buf, error_buf_size, \
- "type mismatch: expected data but block stack was empty");\
- goto fail; \
- } \
- } while (0)
- #define PUSH_CSP(type, ret_type, _start_addr) do { \
- CHECK_CSP_PUSH(); \
- frame_csp->block_type = type; \
- frame_csp->jumped_by_br = false; \
- frame_csp->return_type = ret_type; \
- frame_csp->start_addr = _start_addr; \
- frame_csp->else_addr = NULL; \
- frame_csp->end_addr = NULL; \
- frame_csp->stack_cell_num = stack_cell_num; \
- frame_csp++; \
- csp_num++; \
- if (csp_num > max_csp_num) \
- max_csp_num = csp_num; \
- } while (0)
- #define POP_CSP() do { \
- CHECK_CSP_POP(); \
- frame_csp--; \
- csp_num--; \
- } while (0)
- #define GET_LOCAL_INDEX_AND_TYPE() do { \
- read_leb_uint32(p, p_end, local_idx); \
- if (local_idx >= param_count + local_count) { \
- set_error_buf(error_buf, error_buf_size, \
- "invalid index: local index out of range"); \
- goto fail; \
- } \
- local_type = local_idx < param_count \
- ? param_types[local_idx] \
- : local_types[local_idx - param_count]; \
- } while (0)
- #define CHECK_BR(depth) do { \
- if (csp_num < depth + 1) { \
- set_error_buf(error_buf, error_buf_size, "type mismatch: " \
- "expected data but block stack was empty"); \
- goto fail; \
- } \
- if ((frame_csp - (depth + 1))->block_type != BLOCK_TYPE_LOOP) { \
- uint8 tmp_ret_type = (frame_csp - (depth + 1))->return_type; \
- if ((tmp_ret_type == VALUE_TYPE_I32 \
- && (stack_cell_num < 1 || *(frame_ref - 1) != REF_I32)) \
- || (tmp_ret_type == VALUE_TYPE_F32 \
- && (stack_cell_num < 1 || *(frame_ref - 1) != REF_F32))\
- || (tmp_ret_type == VALUE_TYPE_I64 \
- && (stack_cell_num < 2 \
- || *(frame_ref - 2) != REF_I64_1 \
- || *(frame_ref - 1) != REF_I64_2)) \
- || (tmp_ret_type == VALUE_TYPE_F64 \
- && (stack_cell_num < 2 \
- || *(frame_ref - 2) != REF_F64_1 \
- || *(frame_ref - 1) != REF_F64_2))) { \
- set_error_buf(error_buf, error_buf_size, "type mismatch: " \
- "expected data but stack was empty or other type"); \
- goto fail; \
- } \
- (frame_csp - (depth + 1))->jumped_by_br = true; \
- } \
- } while (0)
- static bool
- check_memory(WASMModule *module,
- char *error_buf, uint32 error_buf_size)
- {
- if (module->memory_count == 0
- && module->import_memory_count == 0) {
- set_error_buf(error_buf, error_buf_size,
- "load or store in module without default memory");
- return false;
- }
- return true;
- }
- #define CHECK_MEMORY() do { \
- if (!check_memory(module, error_buf, error_buf_size)) \
- goto fail; \
- } while (0)
- static bool
- wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
- char *error_buf, uint32 error_buf_size)
- {
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- block_addr *block;
- #endif
- uint8 *p = func->code, *p_end = func->code + func->code_size;
- uint8 *frame_ref_bottom = NULL, *frame_ref_boundary, *frame_ref;
- BranchBlock *frame_csp_bottom = NULL, *frame_csp_boundary, *frame_csp;
- uint32 param_count, local_count, global_count;
- uint32 max_stack_cell_num = 0, max_csp_num = 0;
- uint32 stack_cell_num = 0, csp_num = 0;
- uint32 frame_ref_size, frame_csp_size;
- uint8 *param_types, ret_type, *local_types, local_type, global_type;
- uint32 count, i, local_idx, global_idx, block_return_type, depth, u32;
- int32 i32, i32_const = 0;
- int64 i64;
- uint8 opcode, u8;
- bool return_value = false, is_i32_const = false;
- global_count = module->import_global_count + module->global_count;
- param_count = func->func_type->param_count;
- param_types = func->func_type->types;
- ret_type = func->func_type->result_count
- ? param_types[param_count] : VALUE_TYPE_VOID;
- local_count = func->local_count;
- local_types = func->local_types;
- frame_ref_size = 32;
- if (!(frame_ref_bottom = frame_ref = wasm_malloc(frame_ref_size))) {
- set_error_buf(error_buf, error_buf_size,
- "WASM loader prepare bytecode failed: "
- "allocate memory failed");
- goto fail;
- }
- memset(frame_ref_bottom, 0, frame_ref_size);
- frame_ref_boundary = frame_ref_bottom + frame_ref_size;
- frame_csp_size = sizeof(BranchBlock) * 8;
- if (!(frame_csp_bottom = frame_csp = wasm_malloc(frame_csp_size))) {
- set_error_buf(error_buf, error_buf_size,
- "WASM loader prepare bytecode failed: "
- "allocate memory failed");
- goto fail;
- }
- memset(frame_csp_bottom, 0, frame_csp_size);
- frame_csp_boundary = frame_csp_bottom + 8;
- PUSH_CSP(BLOCK_TYPE_FUNCTION, ret_type, p);
- (frame_csp - 1)->jumped_by_br = true;
- while (p < p_end) {
- opcode = *p++;
- switch (opcode) {
- case WASM_OP_UNREACHABLE:
- goto handle_op_br;
- case WASM_OP_NOP:
- break;
- case WASM_OP_BLOCK:
- read_leb_uint32(p, p_end, block_return_type);
- PUSH_CSP(BLOCK_TYPE_BLOCK, block_return_type, p);
- break;
- case WASM_OP_LOOP:
- read_leb_uint32(p, p_end, block_return_type);
- PUSH_CSP(BLOCK_TYPE_LOOP, block_return_type, p);
- break;
- case WASM_OP_IF:
- POP_I32();
- read_leb_uint32(p, p_end, block_return_type);
- PUSH_CSP(BLOCK_TYPE_IF, block_return_type, p);
- if (!is_i32_const)
- (frame_csp - 1)->jumped_by_br = true;
- else {
- if (!i32_const) {
- if(!wasm_loader_find_block_addr(module,
- (frame_csp - 1)->start_addr,
- p_end,
- (frame_csp - 1)->block_type,
- &(frame_csp - 1)->else_addr,
- &(frame_csp - 1)->end_addr,
- error_buf, error_buf_size))
- goto fail;
- if ((frame_csp - 1)->else_addr)
- p = (frame_csp - 1)->else_addr;
- else
- p = (frame_csp - 1)->end_addr;
- }
- }
- break;
- case WASM_OP_ELSE:
- if (csp_num < 2) {
- set_error_buf(error_buf, error_buf_size, "invalid else");
- goto fail;
- }
- if ((frame_csp - 1)->block_type != BLOCK_TYPE_IF) {
- set_error_buf(error_buf, error_buf_size, "invalid else");
- goto fail;
- }
- (frame_csp - 1)->else_addr = p - 1;
- stack_cell_num = (frame_csp - 1)->stack_cell_num;
- frame_ref = frame_ref_bottom + stack_cell_num;
- break;
- case WASM_OP_END:
- {
- POP_CSP();
- POP_TYPE(frame_csp->return_type);
- PUSH_TYPE(frame_csp->return_type);
- if (csp_num > 0) {
- frame_csp->end_addr = p - 1;
- #if WASM_ENABLE_HASH_BLOCK_ADDR != 0
- if (wasm_hash_map_find(module->branch_set, (void*)frame_csp->start_addr))
- break;
- if (frame_csp->block_type == BLOCK_TYPE_IF)
- block = wasm_malloc(sizeof(block_addr));
- else
- block = wasm_malloc(offsetof(block_addr, else_addr));
- if (!block) {
- set_error_buf(error_buf, error_buf_size,
- "WASM loader prepare bytecode failed: "
- "allocate memory failed.");
- goto fail;
- }
- block->block_type = frame_csp->block_type;
- if (frame_csp->block_type == BLOCK_TYPE_IF)
- block->else_addr = (void*)frame_csp->else_addr;
- block->end_addr = (void*)frame_csp->end_addr;
- if (!wasm_hash_map_insert(module->branch_set, (void*)frame_csp->start_addr,
- block)) {
- set_error_buf(error_buf, error_buf_size,
- "WASM loader prepare bytecode failed: "
- "allocate memory failed.");
- wasm_free(block);
- goto fail;
- }
- #endif
- }
- break;
- }
- case WASM_OP_BR:
- {
- read_leb_uint32(p, p_end, depth);
- CHECK_BR(depth);
- handle_op_br:
- for (i = 1; i <= csp_num; i++)
- if ((frame_csp - i)->jumped_by_br)
- break;
- block_return_type = (frame_csp - i)->return_type;
- if(!wasm_loader_find_block_addr(module,
- (frame_csp - i)->start_addr,
- p_end,
- (frame_csp - i)->block_type,
- &(frame_csp - i)->else_addr,
- &(frame_csp - i)->end_addr,
- error_buf, error_buf_size))
- goto fail;
- stack_cell_num = (frame_csp - i)->stack_cell_num;
- frame_ref = frame_ref_bottom + stack_cell_num;
- csp_num -= i - 1;
- frame_csp -= i - 1;
- if ((frame_csp - 1)->block_type == BLOCK_TYPE_IF
- && (frame_csp - 1)->else_addr != NULL
- && p <= (frame_csp - 1)->else_addr)
- p = (frame_csp - 1)->else_addr;
- else {
- p = (frame_csp - 1)->end_addr;
- PUSH_TYPE(block_return_type);
- }
- break;
- }
- case WASM_OP_BR_IF:
- read_leb_uint32(p, p_end, depth);
- POP_I32();
- CHECK_BR(depth);
- if (!is_i32_const)
- (frame_csp - (depth + 1))->jumped_by_br = true;
- else {
- if (i32_const)
- goto handle_op_br;
- }
- break;
- case WASM_OP_BR_TABLE:
- {
- read_leb_uint32(p, p_end, count);
- POP_I32();
- /* TODO: check the const */
- for (i = 0; i <= count; i++) {
- read_leb_uint32(p, p_end, depth);
- CHECK_BR(depth);
- }
- goto handle_op_br;
- }
- case WASM_OP_RETURN:
- {
- POP_TYPE(ret_type);
- PUSH_TYPE(ret_type);
- if(!wasm_loader_find_block_addr(module,
- (frame_csp - 1)->start_addr,
- p_end,
- (frame_csp - 1)->block_type,
- &(frame_csp - 1)->else_addr,
- &(frame_csp - 1)->end_addr,
- error_buf, error_buf_size))
- goto fail;
- stack_cell_num = (frame_csp - 1)->stack_cell_num;
- frame_ref = frame_ref_bottom + stack_cell_num;
- if ((frame_csp - 1)->block_type == BLOCK_TYPE_IF
- && p <= (frame_csp - 1)->else_addr) {
- p = (frame_csp - 1)->else_addr;
- }
- else {
- p = (frame_csp - 1)->end_addr;
- PUSH_TYPE((frame_csp - 1)->return_type);
- }
- break;
- }
- case WASM_OP_CALL:
- {
- WASMType *func_type;
- uint32 func_idx;
- int32 idx;
- read_leb_uint32(p, p_end, func_idx);
- if (func_idx >= module->import_function_count + module->function_count) {
- set_error_buf(error_buf, error_buf_size, "function index is overflow");
- goto fail;
- }
- if (func_idx < module->import_function_count)
- func_type = module->import_functions[func_idx].u.function.func_type;
- else
- func_type =
- module->functions[func_idx - module->import_function_count]->func_type;
- for (idx = func_type->param_count - 1; idx >= 0; idx--)
- POP_TYPE(func_type->types[idx]);
- if (func_type->result_count)
- PUSH_TYPE(func_type->types[func_type->param_count]);
- break;
- }
- case WASM_OP_CALL_INDIRECT:
- {
- int32 idx;
- WASMType *func_type;
- uint32 type_idx;
- if (module->table_count == 0
- && module->import_table_count == 0) {
- set_error_buf(error_buf, error_buf_size,
- "call indirect without default table");
- goto fail;
- }
- read_leb_uint32(p, p_end, type_idx);
- /* reserved byte 0x00 */
- if (*p++ != 0x00) {
- set_error_buf(error_buf, error_buf_size,
- "zero flag expected");
- goto fail;
- }
- POP_I32();
- if (type_idx >= module->type_count) {
- set_error_buf(error_buf, error_buf_size,
- "function index is overflow");
- goto fail;
- }
- func_type = module->types[type_idx];
- for (idx = func_type->param_count - 1; idx >= 0; idx--)
- POP_TYPE(func_type->types[idx]);
- PUSH_TYPE(func_type->types[func_type->param_count]);
- break;
- }
- case WASM_OP_DROP:
- {
- if (stack_cell_num <= 0) {
- set_error_buf(error_buf, error_buf_size,
- "invalid drop: stack was empty");
- goto fail;
- }
- if (*(frame_ref - 1) == REF_I32
- || *(frame_ref - 1) == REF_F32) {
- frame_ref--;
- stack_cell_num--;
- *(p - 1) = WASM_OP_DROP_32;
- }
- else {
- if (stack_cell_num <= 1) {
- set_error_buf(error_buf, error_buf_size,
- "invalid drop: stack was empty");
- goto fail;
- }
- frame_ref -= 2;
- stack_cell_num -= 2;
- *(p - 1) = WASM_OP_DROP_64;
- }
- break;
- }
- case WASM_OP_SELECT:
- {
- uint8 ref_type;
- POP_I32();
- if (stack_cell_num <= 0) {
- set_error_buf(error_buf, error_buf_size,
- "invalid drop: stack was empty");
- goto fail;
- }
- switch (*(frame_ref - 1)) {
- case REF_I32:
- case REF_F32:
- *(p - 1) = WASM_OP_SELECT_32;
- break;
- case REF_I64_2:
- case REF_F64_2:
- *(p - 1) = WASM_OP_SELECT_64;
- break;
- }
- ref_type = *(frame_ref - 1);
- POP_TYPE(ref_type);
- POP_TYPE(ref_type);
- PUSH_TYPE(ref_type);
- break;
- }
- case WASM_OP_GET_LOCAL:
- {
- GET_LOCAL_INDEX_AND_TYPE();
- PUSH_TYPE(local_type);
- break;
- }
- case WASM_OP_SET_LOCAL:
- {
- GET_LOCAL_INDEX_AND_TYPE();
- POP_TYPE(local_type);
- break;
- }
- case WASM_OP_TEE_LOCAL:
- {
- GET_LOCAL_INDEX_AND_TYPE();
- POP_TYPE(local_type);
- PUSH_TYPE(local_type);
- break;
- }
- case WASM_OP_GET_GLOBAL:
- {
- read_leb_uint32(p, p_end, global_idx);
- if (global_idx >= global_count) {
- set_error_buf(error_buf, error_buf_size,
- "invalid index: global index out of range");
- goto fail;
- }
- global_type = global_idx < module->import_global_count
- ? module->import_globals[global_idx].u.global.type
- :module->globals[global_idx - module->import_global_count].type;
- PUSH_TYPE(global_type);
- break;
- }
- case WASM_OP_SET_GLOBAL:
- {
- read_leb_uint32(p, p_end, global_idx);
- if (global_idx >= global_count) {
- set_error_buf(error_buf, error_buf_size,
- "invalid index: global index out of range");
- goto fail;
- }
- global_type = global_idx < module->import_global_count
- ? module->import_globals[global_idx].u.global.type
- : module->globals[global_idx - module->import_global_count].type;
- POP_TYPE(global_type);
- break;
- }
- case WASM_OP_I32_LOAD:
- case WASM_OP_I32_LOAD8_S:
- case WASM_OP_I32_LOAD8_U:
- case WASM_OP_I32_LOAD16_S:
- case WASM_OP_I32_LOAD16_U:
- CHECK_MEMORY();
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- POP_I32();
- PUSH_I32();
- break;
- case WASM_OP_I64_LOAD:
- case WASM_OP_I64_LOAD8_S:
- case WASM_OP_I64_LOAD8_U:
- case WASM_OP_I64_LOAD16_S:
- case WASM_OP_I64_LOAD16_U:
- case WASM_OP_I64_LOAD32_S:
- case WASM_OP_I64_LOAD32_U:
- CHECK_MEMORY();
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- POP_I32();
- PUSH_I64();
- break;
- case WASM_OP_F32_LOAD:
- CHECK_MEMORY();
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- POP_I32();
- PUSH_F32();
- break;
- case WASM_OP_F64_LOAD:
- CHECK_MEMORY();
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- POP_I32();
- PUSH_F64();
- break;
- case WASM_OP_I32_STORE:
- case WASM_OP_I32_STORE8:
- case WASM_OP_I32_STORE16:
- CHECK_MEMORY();
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- POP_I32();
- POP_I32();
- break;
- case WASM_OP_I64_STORE:
- case WASM_OP_I64_STORE8:
- case WASM_OP_I64_STORE16:
- case WASM_OP_I64_STORE32:
- CHECK_MEMORY();
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- POP_I64();
- POP_I32();
- break;
- case WASM_OP_F32_STORE:
- CHECK_MEMORY();
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- POP_F32();
- POP_I32();
- break;
- case WASM_OP_F64_STORE:
- CHECK_MEMORY();
- read_leb_uint32(p, p_end, u32); /* align */
- read_leb_uint32(p, p_end, u32); /* offset */
- POP_F64();
- POP_I32();
- break;
- case WASM_OP_MEMORY_SIZE:
- CHECK_MEMORY();
- /* reserved byte 0x00 */
- if (*p++ != 0x00) {
- set_error_buf(error_buf, error_buf_size,
- "zero flag expected");
- goto fail;
- }
- PUSH_I32();
- break;
- case WASM_OP_MEMORY_GROW:
- CHECK_MEMORY();
- /* reserved byte 0x00 */
- if (*p++ != 0x00) {
- set_error_buf(error_buf, error_buf_size,
- "zero flag expected");
- goto fail;
- }
- POP_I32();
- PUSH_I32();
- break;
- case WASM_OP_I32_CONST:
- read_leb_int32(p, p_end, i32_const);
- /* Currently we only track simple I32_CONST opcode. */
- is_i32_const = true;
- PUSH_I32();
- break;
- case WASM_OP_I64_CONST:
- read_leb_int64(p, p_end, i64);
- PUSH_I64();
- break;
- case WASM_OP_F32_CONST:
- p += sizeof(float32);
- PUSH_F32();
- break;
- case WASM_OP_F64_CONST:
- p += sizeof(float64);
- PUSH_F64();
- break;
- case WASM_OP_I32_EQZ:
- POP_I32();
- PUSH_I32();
- break;
- case WASM_OP_I32_EQ:
- case WASM_OP_I32_NE:
- case WASM_OP_I32_LT_S:
- case WASM_OP_I32_LT_U:
- case WASM_OP_I32_GT_S:
- case WASM_OP_I32_GT_U:
- case WASM_OP_I32_LE_S:
- case WASM_OP_I32_LE_U:
- case WASM_OP_I32_GE_S:
- case WASM_OP_I32_GE_U:
- POP_I32();
- POP_I32();
- PUSH_I32();
- break;
- case WASM_OP_I64_EQZ:
- POP_I64();
- PUSH_I32();
- break;
- case WASM_OP_I64_EQ:
- case WASM_OP_I64_NE:
- case WASM_OP_I64_LT_S:
- case WASM_OP_I64_LT_U:
- case WASM_OP_I64_GT_S:
- case WASM_OP_I64_GT_U:
- case WASM_OP_I64_LE_S:
- case WASM_OP_I64_LE_U:
- case WASM_OP_I64_GE_S:
- case WASM_OP_I64_GE_U:
- POP_I64();
- POP_I64();
- PUSH_I32();
- break;
- case WASM_OP_F32_EQ:
- case WASM_OP_F32_NE:
- case WASM_OP_F32_LT:
- case WASM_OP_F32_GT:
- case WASM_OP_F32_LE:
- case WASM_OP_F32_GE:
- POP_F32();
- POP_F32();
- PUSH_I32();
- break;
- case WASM_OP_F64_EQ:
- case WASM_OP_F64_NE:
- case WASM_OP_F64_LT:
- case WASM_OP_F64_GT:
- case WASM_OP_F64_LE:
- case WASM_OP_F64_GE:
- POP_F64();
- POP_F64();
- PUSH_I32();
- break;
- break;
- case WASM_OP_I32_CLZ:
- case WASM_OP_I32_CTZ:
- case WASM_OP_I32_POPCNT:
- POP_I32();
- PUSH_I32();
- break;
- case WASM_OP_I32_ADD:
- case WASM_OP_I32_SUB:
- case WASM_OP_I32_MUL:
- case WASM_OP_I32_DIV_S:
- case WASM_OP_I32_DIV_U:
- case WASM_OP_I32_REM_S:
- case WASM_OP_I32_REM_U:
- case WASM_OP_I32_AND:
- case WASM_OP_I32_OR:
- case WASM_OP_I32_XOR:
- case WASM_OP_I32_SHL:
- case WASM_OP_I32_SHR_S:
- case WASM_OP_I32_SHR_U:
- case WASM_OP_I32_ROTL:
- case WASM_OP_I32_ROTR:
- POP_I32();
- POP_I32();
- PUSH_I32();
- break;
- case WASM_OP_I64_CLZ:
- case WASM_OP_I64_CTZ:
- case WASM_OP_I64_POPCNT:
- POP_I64();
- PUSH_I64();
- break;
- case WASM_OP_I64_ADD:
- case WASM_OP_I64_SUB:
- case WASM_OP_I64_MUL:
- case WASM_OP_I64_DIV_S:
- case WASM_OP_I64_DIV_U:
- case WASM_OP_I64_REM_S:
- case WASM_OP_I64_REM_U:
- case WASM_OP_I64_AND:
- case WASM_OP_I64_OR:
- case WASM_OP_I64_XOR:
- case WASM_OP_I64_SHL:
- case WASM_OP_I64_SHR_S:
- case WASM_OP_I64_SHR_U:
- case WASM_OP_I64_ROTL:
- case WASM_OP_I64_ROTR:
- POP_I64();
- POP_I64();
- PUSH_I64();
- break;
- case WASM_OP_F32_ABS:
- case WASM_OP_F32_NEG:
- case WASM_OP_F32_CEIL:
- case WASM_OP_F32_FLOOR:
- case WASM_OP_F32_TRUNC:
- case WASM_OP_F32_NEAREST:
- case WASM_OP_F32_SQRT:
- POP_F32();
- PUSH_F32();
- break;
- case WASM_OP_F32_ADD:
- case WASM_OP_F32_SUB:
- case WASM_OP_F32_MUL:
- case WASM_OP_F32_DIV:
- case WASM_OP_F32_MIN:
- case WASM_OP_F32_MAX:
- case WASM_OP_F32_COPYSIGN:
- POP_F32();
- POP_F32();
- PUSH_F32();
- break;
- case WASM_OP_F64_ABS:
- case WASM_OP_F64_NEG:
- case WASM_OP_F64_CEIL:
- case WASM_OP_F64_FLOOR:
- case WASM_OP_F64_TRUNC:
- case WASM_OP_F64_NEAREST:
- case WASM_OP_F64_SQRT:
- POP_F64();
- PUSH_F64();
- break;
- case WASM_OP_F64_ADD:
- case WASM_OP_F64_SUB:
- case WASM_OP_F64_MUL:
- case WASM_OP_F64_DIV:
- case WASM_OP_F64_MIN:
- case WASM_OP_F64_MAX:
- case WASM_OP_F64_COPYSIGN:
- POP_F64();
- POP_F64();
- PUSH_F64();
- break;
- case WASM_OP_I32_WRAP_I64:
- POP_I64();
- PUSH_I32();
- break;
- case WASM_OP_I32_TRUNC_S_F32:
- case WASM_OP_I32_TRUNC_U_F32:
- POP_F32();
- PUSH_I32();
- break;
- case WASM_OP_I32_TRUNC_S_F64:
- case WASM_OP_I32_TRUNC_U_F64:
- POP_F64();
- PUSH_I32();
- break;
- case WASM_OP_I64_EXTEND_S_I32:
- case WASM_OP_I64_EXTEND_U_I32:
- POP_I32();
- PUSH_I64();
- break;
- case WASM_OP_I64_TRUNC_S_F32:
- case WASM_OP_I64_TRUNC_U_F32:
- POP_F32();
- PUSH_I64();
- break;
- case WASM_OP_I64_TRUNC_S_F64:
- case WASM_OP_I64_TRUNC_U_F64:
- POP_F64();
- PUSH_I64();
- break;
- case WASM_OP_F32_CONVERT_S_I32:
- case WASM_OP_F32_CONVERT_U_I32:
- POP_I32();
- PUSH_F32();
- break;
- case WASM_OP_F32_CONVERT_S_I64:
- case WASM_OP_F32_CONVERT_U_I64:
- POP_I64();
- PUSH_F32();
- break;
- case WASM_OP_F32_DEMOTE_F64:
- POP_F64();
- PUSH_F32();
- break;
- case WASM_OP_F64_CONVERT_S_I32:
- case WASM_OP_F64_CONVERT_U_I32:
- POP_I32();
- PUSH_F64();
- break;
- case WASM_OP_F64_CONVERT_S_I64:
- case WASM_OP_F64_CONVERT_U_I64:
- POP_I64();
- PUSH_F64();
- break;
- case WASM_OP_F64_PROMOTE_F32:
- POP_F32();
- PUSH_F64();
- break;
- case WASM_OP_I32_REINTERPRET_F32:
- POP_F32();
- PUSH_I32();
- break;
- case WASM_OP_I64_REINTERPRET_F64:
- POP_F64();
- PUSH_I64();
- break;
- case WASM_OP_F32_REINTERPRET_I32:
- POP_I32();
- PUSH_F32();
- break;
- case WASM_OP_F64_REINTERPRET_I64:
- POP_I64();
- PUSH_F64();
- break;
- default:
- if (error_buf != NULL)
- snprintf(error_buf, error_buf_size,
- "WASM module load failed: "
- "invalid opcode %02x.", opcode);
- goto fail;
- }
- if (opcode != WASM_OP_I32_CONST)
- is_i32_const = false;
- }
- if (csp_num > 0) {
- set_error_buf(error_buf, error_buf_size,
- "WASM module load failed: "
- "function body must end with END opcode.");
- goto fail;
- }
- func->max_stack_cell_num = max_stack_cell_num;
- func->max_block_num = max_csp_num;
- return_value = true;
- fail:
- if (frame_ref_bottom)
- wasm_free(frame_ref_bottom);
- if (frame_csp_bottom)
- wasm_free(frame_csp_bottom);
- (void)u8;
- (void)u32;
- (void)i32;
- (void)i64;
- return return_value;
- }
|