From 4924e414e9d0016a2c294a344a95ecb6d355f7f7 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Mon, 12 Apr 2021 00:13:57 +0300 Subject: [PATCH] Cleanup --- platforms/arduino/platformio.ini | 32 ++++++++----------------------- platforms/bluepill/platformio.ini | 4 ++++ platforms/esp8266/platformio.ini | 4 ++++ platforms/hifive1/platformio.ini | 22 +++++++-------------- source/m3_bind.c | 4 ++-- source/m3_compile.c | 24 +++++++++++------------ source/m3_core.h | 6 ++++++ source/m3_env.c | 16 ++++++++-------- source/m3_env.h | 5 ++--- source/m3_function.c | 28 +++++++++++++-------------- source/m3_function.h | 10 +++++----- source/m3_info.c | 2 +- source/m3_parse.c | 14 ++++++++++++++ source/wasm3.h | 6 +++--- 14 files changed, 90 insertions(+), 87 deletions(-) diff --git a/platforms/arduino/platformio.ini b/platforms/arduino/platformio.ini index 1d96d00..dceda85 100644 --- a/platforms/arduino/platformio.ini +++ b/platforms/arduino/platformio.ini @@ -23,11 +23,17 @@ default_envs = maix #titiva TODO: undefined reference to `_exit' +[env] +framework = arduino +monitor_speed = 115200 + +src_filter = + +<*> + - + [env:mkr1000] platform = atmelsam board = mkr1000USB -framework = arduino -monitor_speed = 115200 src_build_flags = -O3 -Wfatal-errors @@ -37,8 +43,6 @@ src_build_flags = [env:due] platform = atmelsam board = due -framework = arduino -monitor_speed = 115200 src_build_flags = -O3 -Wfatal-errors @@ -47,8 +51,6 @@ src_build_flags = [env:mega1284] platform = atmelavr board = wildfirev3 -framework = arduino -monitor_speed = 115200 src_build_flags = -Dd_m3CodePageAlignSize=512 @@ -58,8 +60,6 @@ src_build_flags = [env:tinyBLE] platform = nordicnrf51 board = seeedTinyBLE -framework = arduino -monitor_speed = 115200 src_build_flags = -O3 -Wfatal-errors @@ -68,8 +68,6 @@ src_build_flags = [env:blenano] platform = nordicnrf51 board = redBearLabBLENano -framework = arduino -monitor_speed = 115200 src_build_flags = -O3 -Wfatal-errors @@ -78,8 +76,6 @@ src_build_flags = [env:blenano2] platform = nordicnrf52 board = redbear_blenano2 -framework = arduino -monitor_speed = 115200 src_build_flags = -O3 -Wfatal-errors @@ -88,9 +84,7 @@ src_build_flags = [env:teensy31] platform = teensy board = teensy31 -framework = arduino upload_protocol = teensy-cli -monitor_speed = 115200 src_build_flags = -O3 -Wfatal-errors @@ -98,12 +92,10 @@ src_build_flags = [env:bluepill-mapple] platform = ststm32 -framework = arduino board = bluepill_f103c8_128k board_build.core = maple upload_protocol = stlink #upload_protocol = dfu -monitor_speed = 115200 src_build_flags = -Dd_m3FixedHeap=8192 @@ -113,9 +105,7 @@ src_build_flags = [env:bluepill] platform = ststm32 board = bluepill_f103c8 -framework = arduino upload_protocol = stlink -monitor_speed = 115200 src_build_flags = -Dd_m3FixedHeap=8192 @@ -125,8 +115,6 @@ src_build_flags = [env:az3166] platform = ststm32 board = mxchip_az3166 -framework = arduino -monitor_speed = 115200 src_build_flags = -O3 -Wfatal-errors @@ -135,8 +123,6 @@ src_build_flags = [env:maix] platform = kendryte210 board = sipeed-maix-one-dock -framework = arduino -monitor_speed = 115200 src_build_flags = -O3 -Wfatal-errors @@ -145,8 +131,6 @@ src_build_flags = [env:titiva] platform = titiva board = lptm4c1294ncpdt -framework = arduino -monitor_speed = 115200 src_build_flags = -DLED_BUILTIN=13 diff --git a/platforms/bluepill/platformio.ini b/platforms/bluepill/platformio.ini index 2bcd597..39beed6 100644 --- a/platforms/bluepill/platformio.ini +++ b/platforms/bluepill/platformio.ini @@ -5,6 +5,10 @@ framework = stm32cube upload_protocol = stlink lib_deps = jeeh +src_filter = + +<*> + - + src_build_flags = -Dd_m3FixedHeap=8192 -Os -Wfatal-errors diff --git a/platforms/esp8266/platformio.ini b/platforms/esp8266/platformio.ini index 8c0359b..5e1b0d8 100644 --- a/platforms/esp8266/platformio.ini +++ b/platforms/esp8266/platformio.ini @@ -17,6 +17,10 @@ upload_speed = 460800 board_build.f_cpu = 160000000L +src_filter = + +<*> + - + src_build_flags = -Dd_m3FixedHeap=8192 -O3 -Wfatal-errors diff --git a/platforms/hifive1/platformio.ini b/platforms/hifive1/platformio.ini index 17b9db0..50b1d46 100644 --- a/platforms/hifive1/platformio.ini +++ b/platforms/hifive1/platformio.ini @@ -11,14 +11,17 @@ [platformio] default_envs = hifive1-revb -[env:hifive1] +[env] platform = sifive framework = freedom-e-sdk -board = hifive1 monitor_speed = 115200 board_build.f_cpu = 320000000L +src_filter = + +<*> + - + src_build_flags = -Dd_m3FixedHeap=8192 -Os -Wfatal-errors @@ -26,19 +29,8 @@ src_build_flags = build_flags = -lm +[env:hifive1] +board = hifive1 [env:hifive1-revb] -platform = sifive -framework = freedom-e-sdk board = hifive1-revb -monitor_speed = 115200 - -board_build.f_cpu = 320000000L - -src_build_flags = - -Dd_m3FixedHeap=8192 - -Os -Wfatal-errors - -flto - -build_flags = -lm - diff --git a/source/m3_bind.c b/source/m3_bind.c index 7bc74b7..c8191a5 100644 --- a/source/m3_bind.c +++ b/source/m3_bind.c @@ -45,11 +45,11 @@ _try { // assume min signature is "()" _throwif (m3Err_malformedFunctionSignature, maxNumTypes < 2); maxNumTypes -= 2; - + _throwif (m3Err_tooManyArgsRets, maxNumTypes > d_m3MaxSaneFunctionArgRetCount); _ (AllocFuncType (& funcType, (u32) maxNumTypes)); - + u8 * typelist = funcType->types; bool parsingRets = true; diff --git a/source/m3_compile.c b/source/m3_compile.c index b5b1d65..bd3740c 100644 --- a/source/m3_compile.c +++ b/source/m3_compile.c @@ -659,7 +659,7 @@ _ (PushAllocatedSlotAndEmit (o, i_type)); u32 * constant = (u32 *) & o->constants [constTableIndex]; * constant = (u32) i_word; } - + _ (Push (o, i_type, slot)); o->slotMaxConstIndex = M3_MAX (slot + numRequiredSlots, o->slotMaxConstIndex); @@ -911,7 +911,7 @@ _ (IncrementSlotUsageCount (o, * o_preservedSlotIndex)); M3Result GetBlockScope (IM3Compilation o, IM3CompilationScope * o_scope, i32 i_depth) { M3Result result = m3Err_none; - + IM3CompilationScope scope = & o->block; while (i_depth--) @@ -1034,7 +1034,7 @@ M3Result Compile_End (IM3Compilation o, m3opcode_t i_opcode) if (o->block.depth == 0) { u8 type = GetSingleRetType (o->block.type); - + u32 numReturns = GetFuncTypeNumReturns (o->block.type); if (numReturns) @@ -1177,7 +1177,7 @@ _ (ReadLEB_u32 (& globalIndex, & o->wasm, o->wasmEnd)); if (o->module->globals) { M3Global * global = & o->module->globals [globalIndex]; - + _ ((i_opcode == 0x23) ? Compile_GetGlobal (o, global) : Compile_SetGlobal (o, global)); } else _throw (ErrorCompile (m3Err_globalMemoryNotAllocated, o, "module '%s' is missing global memory", o->module->name)); @@ -1399,7 +1399,7 @@ _ (Pop (o)); if (i_type->numRets) { u8 type = GetSingleRetType (i_type); - + _ (Push (o, type, topSlot)); u16 numSlots = GetTypeNumSlots (type); @@ -1550,7 +1550,7 @@ _ (NormalizeType (&valueType, type)); m3log M3Result PreserveArgsAndLocals (IM3Compilation o) { M3Result result = m3Err_none; - + if (o->stackIndex > o->stackFirstDynamicIndex) { u32 numArgsAndLocals = GetFunctionNumArgsAndLocals (o->function); @@ -2232,7 +2232,7 @@ _ (Compile_Operator (o, opcode)); if (opcode == c_waOp_end or opcode == c_waOp_else) break; } - + _catch: return result; } @@ -2245,7 +2245,7 @@ M3Result ValidateBlockEnd (IM3Compilation o, bool * o_copyStackTopToRegister) * o_copyStackTopToRegister = false; u8 valueType = GetSingleRetType (o->block.type); - + if (valueType != c_m3Type_none) { if (IsStackPolymorphic (o)) @@ -2355,9 +2355,9 @@ M3Result Compile_ReserveConstants (IM3Compilation o) u16 cappedConstantSlots = M3_MIN (numConstantSlots, d_m3MaxConstantTableSize); AlignSlotIndexToType (& cappedConstantSlots, c_m3Type_i64); m3log (compile, "estimated constant slots: %d; reserved: %d", numConstantSlots, (u32) cappedConstantSlots) - + o->slotFirstDynamicIndex = o->slotFirstConstIndex + cappedConstantSlots; - + if (o->slotFirstDynamicIndex >= d_m3MaxFunctionSlots) _throw (m3Err_functionStackOverflow); @@ -2404,7 +2404,7 @@ _ (AcquireCompilationCodePage (o, & o->page)); // all args & returns are 64-bit aligned, so use 2 slots for a d_m3Use32BitSlots=1 build const u16 ioSlotCount = sizeof (u64) / sizeof (m3slot_t); - + u16 numRetSlots = GetFunctionNumReturns (o->function) * ioSlotCount; for (u16 i = 0; i < numRetSlots; ++i) @@ -2425,7 +2425,7 @@ _ (PushAllocatedSlot (o, type)); } o->slotMaxAllocatedIndexPlusOne = o->function->numRetAndArgSlots = o->slotFirstLocalIndex = o->slotFirstDynamicIndex; - + _ (CompileLocals (o)); u16 maxSlot = GetMaxUsedSlotPlusOne (o); diff --git a/source/m3_core.h b/source/m3_core.h index dde8d8f..ef06459 100644 --- a/source/m3_core.h +++ b/source/m3_core.h @@ -158,6 +158,12 @@ M3CodePageHeader; #define d_m3Reg0SlotAlias 30000 #define d_m3Fp0SlotAlias 30001 +#define d_m3MaxSaneTypesCount 100000 +#define d_m3MaxSaneFunctionsCount 100000 +#define d_m3MaxSaneImportsCount 10000 +#define d_m3MaxSaneExportsCount 10000 +#define d_m3MaxSaneGlobalsCount 100000 +#define d_m3MaxSaneDataSegments 100000 #define d_m3MaxSaneUtf8Length 2000 #define d_m3MaxSaneFunctionArgRetCount 1000 // still insane, but whatever diff --git a/source/m3_env.c b/source/m3_env.c index 93e05bd..4087fb4 100644 --- a/source/m3_env.c +++ b/source/m3_env.c @@ -502,7 +502,7 @@ _ (ReadLEB_u32 (& numElements, & bytes, end)); io_module->table0 = m3_ReallocArray (IM3Function, io_module->table0, endElement, io_module->table0Size); _throwifnull(io_module->table0); - + _throwif ("table overflow", endElement > UINT_MAX) io_module->table0Size = (u32) endElement; @@ -674,7 +674,7 @@ void * v_FindFunction (IM3Module i_module, const char * const i_name) M3Result m3_FindFunction (IM3Function * o_function, IM3Runtime i_runtime, const char * const i_functionName) { M3Result result = m3Err_none; d_m3Assert (o_function and i_runtime and i_functionName); - + IM3Function function = NULL; if (not i_runtime->modules) { @@ -689,7 +689,7 @@ M3Result m3_FindFunction (IM3Function * o_function, IM3Runtime i_runtime, cons { _ (Compile_Function (function)) } - + // Check if start function needs to be called if (function->module->startFunction) { @@ -701,7 +701,7 @@ _ (m3_RunStart (function->module)) _catch: if (result) function = NULL; - + * o_function = function; return result; @@ -760,9 +760,9 @@ u8 * GetStackPointerForArgs (IM3Function i_function) IM3FuncType ftype = i_function->funcType; u16 numReturnSlots = ftype->numRets; - + stack += numReturnSlots; - + return (u8 *) stack; } @@ -790,7 +790,7 @@ M3Result m3_CallVL (IM3Function i_function, va_list i_args) # endif u8* s = GetStackPointerForArgs (i_function); - + for (u32 i = 0; i < ftype->numArgs; ++i) { switch (d_FuncArgType(ftype, i)) { @@ -918,7 +918,7 @@ M3Result m3_GetResults (IM3Function i_function, uint32_t i_retc, const void * } u8* s = (u8*) runtime->stack; - + for (u32 i = 0; i < ftype->numRets; ++i) { switch (d_FuncRetType(ftype, i)) { diff --git a/source/m3_env.h b/source/m3_env.h index 6794a27..9d3a12f 100644 --- a/source/m3_env.h +++ b/source/m3_env.h @@ -50,9 +50,6 @@ typedef struct M3DataSegment } M3DataSegment; - -void FreeImportInfo (M3ImportInfo * i_info); - //--------------------------------------------------------------------------------------------------------------------------------- typedef struct M3Global @@ -128,6 +125,8 @@ M3Result Module_AddGlobal (IM3Module io_module, IM M3Result Module_AddFunction (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo /* can be null */); IM3Function Module_GetFunction (IM3Module i_module, u32 i_functionIndex); +void FreeImportInfo (M3ImportInfo * i_info); + //--------------------------------------------------------------------------------------------------------------------------------- typedef struct M3Environment diff --git a/source/m3_function.c b/source/m3_function.c index 6f128d3..d262361 100644 --- a/source/m3_function.c +++ b/source/m3_function.c @@ -6,7 +6,7 @@ // #include "m3_function.h" - +#include "m3_env.h" M3Result AllocFuncType (IM3FuncType * o_functionType, u32 i_numTypes) @@ -34,17 +34,17 @@ u16 GetFuncTypeNumReturns (const IM3FuncType i_funcType) u8 GetFuncTypeReturnType (const IM3FuncType i_funcType, u16 i_index) { - u8 type = c_m3Type_unknown; - - if (i_funcType) - { - if (i_index < i_funcType->numRets) - { - type = i_funcType->types [i_index]; - } - } - - return type; + u8 type = c_m3Type_unknown; + + if (i_funcType) + { + if (i_index < i_funcType->numRets) + { + type = i_funcType->types [i_index]; + } + } + + return type; } @@ -172,7 +172,7 @@ u8 GetFunctionArgType (IM3Function i_function, u32 i_index) if (i_index < GetFunctionNumArgs (i_function)) { u32 numReturns = i_function->funcType->numRets; - + type = i_function->funcType->types [numReturns + i_index]; } @@ -196,7 +196,7 @@ u16 GetFunctionNumReturns (IM3Function i_function) u8 GetFunctionReturnType (const IM3Function i_function, u16 i_index) { - return i_function ? GetFuncTypeReturnType (i_function->funcType, i_index) : c_m3Type_unknown; + return i_function ? GetFuncTypeReturnType (i_function->funcType, i_index) : c_m3Type_unknown; } diff --git a/source/m3_function.h b/source/m3_function.h index a3d39d6..c9f756b 100644 --- a/source/m3_function.h +++ b/source/m3_function.h @@ -30,7 +30,7 @@ typedef M3FuncType * IM3FuncType; M3Result AllocFuncType (IM3FuncType * o_functionType, u32 i_numTypes); bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB); u16 GetFuncTypeNumReturns (const IM3FuncType i_funcType); -u8 GetFuncTypeReturnType (const IM3FuncType i_funcType, u16 i_index); +u8 GetFuncTypeReturnType (const IM3FuncType i_funcType, u16 i_index); //--------------------------------------------------------------------------------------------------------------------------------- @@ -62,7 +62,7 @@ typedef struct M3Function # if d_m3EnableStrace >= 2 || d_m3LogCompile u32 index; # endif - + u16 maxStackSlots; u16 numRetSlots; @@ -71,10 +71,10 @@ typedef struct M3Function u16 numLocals; // not including args u16 numLocalBytes; - bool ownsWasmCode; + bool ownsWasmCode; - u16 numConstantBytes; - void * constants; + u16 numConstantBytes; + void * constants; } M3Function; diff --git a/source/m3_info.c b/source/m3_info.c index cc53124..0b1a9aa 100644 --- a/source/m3_info.c +++ b/source/m3_info.c @@ -26,7 +26,7 @@ void m3_PrintM3Info () // printf (" sizeof M3CodePage : %zu bytes (%d slots) \n", sizeof (M3CodePage), c_m3CodePageNumSlots); printf (" sizeof M3MemPage : %u bytes \n", d_m3MemPageSize); printf (" sizeof M3Compilation : %zu bytes \n", sizeof (M3Compilation)); - printf (" sizeof M3Function : %zu bytes \n", sizeof (M3Function)); + printf (" sizeof M3Function : %zu bytes \n", sizeof (M3Function)); printf ("----------------------------------------------------------------\n\n"); } diff --git a/source/m3_parse.c b/source/m3_parse.c index 16450da..d11f3d3 100644 --- a/source/m3_parse.c +++ b/source/m3_parse.c @@ -46,6 +46,8 @@ _try { u32 numTypes; _ (ReadLEB_u32 (& numTypes, & i_bytes, i_end)); m3log (parse, "** Type [%d]", numTypes); + _throwif("too many types", numTypes > d_m3MaxSaneTypesCount); + if (numTypes) { // table of IM3FuncType (that point to the actual M3FuncType struct in the Environment) @@ -123,6 +125,8 @@ M3Result ParseSection_Function (IM3Module io_module, bytes_t i_bytes, cbytes_t u32 numFunctions; _ (ReadLEB_u32 (& numFunctions, & i_bytes, i_end)); m3log (parse, "** Function [%d]", numFunctions); + _throwif("too many functions", numFunctions > d_m3MaxSaneFunctionsCount); + for (u32 i = 0; i < numFunctions; ++i) { u32 funcTypeIndex; @@ -144,6 +148,8 @@ M3Result ParseSection_Import (IM3Module io_module, bytes_t i_bytes, cbytes_t i u32 numImports; _ (ReadLEB_u32 (& numImports, & i_bytes, i_end)); m3log (parse, "** Import [%d]", numImports); + _throwif("too many imports", numImports > d_m3MaxSaneImportsCount); + for (u32 i = 0; i < numImports; ++i) { u8 importKind; @@ -215,6 +221,8 @@ M3Result ParseSection_Export (IM3Module io_module, bytes_t i_bytes, cbytes_t u32 numExports; _ (ReadLEB_u32 (& numExports, & i_bytes, i_end)); m3log (parse, "** Export [%d]", numExports); + _throwif("too many exports", numExports > d_m3MaxSaneExportsCount); + for (u32 i = 0; i < numExports; ++i) { const char * utf8; @@ -377,6 +385,8 @@ M3Result ParseSection_Data (M3Module * io_module, bytes_t i_bytes, cbytes_t i_ u32 numDataSegments; _ (ReadLEB_u32 (& numDataSegments, & i_bytes, i_end)); m3log (parse, "** Data [%d]", numDataSegments); + _throwif("too many data segments", numDataSegments > d_m3MaxSaneDataSegments); + io_module->dataSegments = m3_AllocArray (M3DataSegment, numDataSegments); _throwifnull(io_module->dataSegments); io_module->numDataSegments = numDataSegments; @@ -429,6 +439,8 @@ M3Result ParseSection_Global (M3Module * io_module, bytes_t i_bytes, cbytes_t u32 numGlobals; _ (ReadLEB_u32 (& numGlobals, & i_bytes, i_end)); m3log (parse, "** Global [%d]", numGlobals); + _throwif("too many globals", numGlobals > d_m3MaxSaneGlobalsCount); + for (u32 i = 0; i < numGlobals; ++i) { i8 waType; @@ -478,6 +490,8 @@ _ (ReadLEB_u32 (& payloadLength, & i_bytes, i_end)); u32 numNames; _ (ReadLEB_u32 (& numNames, & i_bytes, i_end)); + _throwif("too many names", numNames > d_m3MaxSaneFunctionsCount); + for (u32 i = 0; i < numNames; ++i) { u32 index; diff --git a/source/wasm3.h b/source/wasm3.h index 9d3b324..c1097cb 100644 --- a/source/wasm3.h +++ b/source/wasm3.h @@ -226,9 +226,9 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow") // Calling m3_RunStart is optional M3Result m3_RunStart (IM3Module i_module); - // Arguments and return values are passed in and out through the stack pointer _sp. - // Placeholder return value slots are first and arguments after. So, the first argument is at _sp [numReturns] - // Return values should be written into _sp [0] to _sp [num_returns - 1] + // Arguments and return values are passed in and out through the stack pointer _sp. + // Placeholder return value slots are first and arguments after. So, the first argument is at _sp [numReturns] + // Return values should be written into _sp [0] to _sp [num_returns - 1] typedef const void * (* M3RawCall) (IM3Runtime runtime, IM3ImportContext _ctx, uint64_t * _sp, void * _mem); M3Result m3_LinkRawFunction (IM3Module io_module,