Skip to content

Commit f40e32a

Browse files
committed
Update Wasm3
1 parent f27a974 commit f40e32a

13 files changed

+116
-74
lines changed

src/m3_compile.c

+42-31
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ M3Result AllocateSlots (IM3Compilation o, u16 * o_slot, u8 i_type)
292292

293293
M3Result AllocateConstantSlots (IM3Compilation o, u16 * o_slot, u8 i_type)
294294
{
295-
return AllocateSlotsWithinRange (o, o_slot, i_type, o->slotFirstConstIndex, o->slotFirstDynamicIndex);
295+
u16 maxTableIndex = o->slotFirstConstIndex + d_m3MaxConstantTableSize;
296+
return AllocateSlotsWithinRange (o, o_slot, i_type, o->slotFirstConstIndex, M3_MIN(o->slotFirstDynamicIndex, maxTableIndex));
296297
}
297298

298299

@@ -1116,10 +1117,10 @@ _ (Read_u8 (& opcode, & o->wasm, o->wasmEnd)); m3log (compile, d_i
11161117

11171118
//printf("Extended opcode: 0x%x\n", i_opcode);
11181119

1119-
const M3OpInfo* opinfo = GetOpInfo (i_opcode);
1120-
_throwif (m3Err_unknownOpcode, not opinfo);
1120+
IM3OpInfo opInfo = GetOpInfo (i_opcode);
1121+
_throwif (m3Err_unknownOpcode, not opInfo);
11211122

1122-
M3Compiler compiler = opinfo->compiler;
1123+
M3Compiler compiler = opInfo->compiler;
11231124
_throwif (m3Err_noCompiler, not compiler);
11241125

11251126
_ ((* compiler) (o, i_opcode));
@@ -2011,74 +2012,75 @@ M3Result Compile_Operator (IM3Compilation o, m3opcode_t i_opcode)
20112012
{
20122013
M3Result result;
20132014

2014-
const M3OpInfo * op = GetOpInfo(i_opcode);
2015+
IM3OpInfo opInfo = GetOpInfo (i_opcode);
2016+
_throwif (m3Err_unknownOpcode, not opInfo);
20152017

2016-
IM3Operation operation;
2018+
IM3Operation op;
20172019

20182020
// This preserve is for for FP compare operations.
20192021
// either need additional slot destination operations or the
20202022
// easy fix, move _r0 out of the way.
20212023
// moving out the way might be the optimal solution most often?
20222024
// otherwise, the _r0 reg can get buried down in the stack
20232025
// and be idle & wasted for a moment.
2024-
if (IsFpType (GetStackTopType (o)) and IsIntType (op->type))
2026+
if (IsFpType (GetStackTopType (o)) and IsIntType (opInfo->type))
20252027
{
2026-
_ (PreserveRegisterIfOccupied (o, op->type));
2028+
_ (PreserveRegisterIfOccupied (o, opInfo->type));
20272029
}
20282030

2029-
if (op->stackOffset == 0)
2031+
if (opInfo->stackOffset == 0)
20302032
{
20312033
if (IsStackTopInRegister (o))
20322034
{
2033-
operation = op->operations [0]; // _s
2035+
op = opInfo->operations [0]; // _s
20342036
}
20352037
else
20362038
{
2037-
_ (PreserveRegisterIfOccupied (o, op->type));
2038-
operation = op->operations [1]; // _r
2039+
_ (PreserveRegisterIfOccupied (o, opInfo->type));
2040+
op = opInfo->operations [1]; // _r
20392041
}
20402042
}
20412043
else
20422044
{
20432045
if (IsStackTopInRegister (o))
20442046
{
2045-
operation = op->operations [0]; // _rs
2047+
op = opInfo->operations [0]; // _rs
20462048

20472049
if (IsStackTopMinus1InRegister (o))
20482050
{ d_m3Assert (i_opcode == 0x38 or i_opcode == 0x39);
2049-
operation = op->operations [3]; // _rr for fp.store
2051+
op = opInfo->operations [3]; // _rr for fp.store
20502052
}
20512053
}
20522054
else if (IsStackTopMinus1InRegister (o))
20532055
{
2054-
operation = op->operations [1]; // _sr
2056+
op = opInfo->operations [1]; // _sr
20552057

2056-
if (not operation) // must be commutative, then
2057-
operation = op->operations [0];
2058+
if (not op) // must be commutative, then
2059+
op = opInfo->operations [0];
20582060
}
20592061
else
20602062
{
2061-
_ (PreserveRegisterIfOccupied (o, op->type)); // _ss
2062-
operation = op->operations [2];
2063+
_ (PreserveRegisterIfOccupied (o, opInfo->type)); // _ss
2064+
op = opInfo->operations [2];
20632065
}
20642066
}
20652067

2066-
if (operation)
2068+
if (op)
20672069
{
2068-
_ (EmitOp (o, operation));
2070+
_ (EmitOp (o, op));
20692071

20702072
_ (EmitSlotNumOfStackTopAndPop (o));
20712073

2072-
if (op->stackOffset < 0)
2074+
if (opInfo->stackOffset < 0)
20732075
_ (EmitSlotNumOfStackTopAndPop (o));
20742076

2075-
if (op->type != c_m3Type_none)
2076-
_ (PushRegister (o, op->type));
2077+
if (opInfo->type != c_m3Type_none)
2078+
_ (PushRegister (o, opInfo->type));
20772079
}
20782080
else
20792081
{
20802082
# ifdef DEBUG
2081-
result = ErrorCompile ("no operation found for opcode", o, "'%s'", op->name);
2083+
result = ErrorCompile ("no operation found for opcode", o, "'%s'", opInfo->name);
20822084
# else
20832085
result = ErrorCompile ("no operation found for opcode", o, "%x", i_opcode);
20842086
# endif
@@ -2093,7 +2095,9 @@ M3Result Compile_Convert (IM3Compilation o, m3opcode_t i_opcode)
20932095
{
20942096
M3Result result = m3Err_none;
20952097

2096-
const M3OpInfo * opInfo = GetOpInfo(i_opcode);
2098+
_try {
2099+
IM3OpInfo opInfo = GetOpInfo (i_opcode);
2100+
_throwif (m3Err_unknownOpcode, not opInfo);
20972101

20982102
bool destInSlot = IsRegisterTypeAllocated (o, opInfo->type);
20992103
bool sourceInSlot = IsStackTopInSlot (o);
@@ -2108,6 +2112,7 @@ _ (PushAllocatedSlotAndEmit (o, opInfo->type))
21082112
else
21092113
_ (PushRegister (o, opInfo->type))
21102114

2115+
}
21112116
_catch: return result;
21122117
}
21132118

@@ -2122,9 +2127,10 @@ _try {
21222127
_ (ReadLEB_u32 (& alignHint, & o->wasm, o->wasmEnd));
21232128
_ (ReadLEB_u32 (& memoryOffset, & o->wasm, o->wasmEnd));
21242129
m3log (compile, d_indent " (offset = %d)", get_indention_string (o), memoryOffset);
2125-
const M3OpInfo * op = GetOpInfo(i_opcode);
2130+
IM3OpInfo opInfo = GetOpInfo (i_opcode);
2131+
_throwif (m3Err_unknownOpcode, not opInfo);
21262132

2127-
if (IsFpType (op->type))
2133+
if (IsFpType (opInfo->type))
21282134
_ (PreserveRegisterIfOccupied (o, c_m3Type_f64));
21292135

21302136
_ (Compile_Operator (o, i_opcode));
@@ -2439,7 +2445,8 @@ const M3OpInfo c_operationsFC [] =
24392445
# endif
24402446
};
24412447

2442-
const M3OpInfo* GetOpInfo (m3opcode_t opcode)
2448+
2449+
IM3OpInfo GetOpInfo (m3opcode_t opcode)
24432450
{
24442451
switch (opcode >> 8) {
24452452
case 0x00:
@@ -2480,7 +2487,7 @@ _ (Read_opcode (& opcode, & o->wasm, o->wasmEnd)); log_opco
24802487
}
24812488
}
24822489

2483-
IM3OpInfo opinfo = GetOpInfo(opcode);
2490+
IM3OpInfo opinfo = GetOpInfo (opcode);
24842491

24852492
if (opinfo == NULL)
24862493
_throw (ErrorCompile (m3Err_unknownOpcode, o, "opcode '%x' not available", opcode));
@@ -2648,6 +2655,7 @@ M3Result CompileLocals (IM3Compilation o)
26482655
{
26492656
M3Result result;
26502657

2658+
u32 numLocals = 0;
26512659
u32 numLocalBlocks;
26522660
_ (ReadLEB_u32 (& numLocalBlocks, & o->wasm, o->wasmEnd));
26532661

@@ -2660,11 +2668,14 @@ _ (ReadLEB_u32 (& numLocalBlocks, & o->wasm, o->wasmEnd));
26602668
_ (ReadLEB_u32 (& varCount, & o->wasm, o->wasmEnd));
26612669
_ (ReadLEB_i7 (& waType, & o->wasm, o->wasmEnd));
26622670
_ (NormalizeType (& localType, waType));
2663-
m3log (compile, "pushing locals. count: %d; type: %s", varCount, c_waTypes [localType]);
2671+
numLocals += varCount; m3log (compile, "pushing locals. count: %d; type: %s", varCount, c_waTypes [localType]);
26642672
while (varCount--)
26652673
_ (PushAllocatedSlot (o, localType));
26662674
}
26672675

2676+
if (o->function)
2677+
o->function->numLocals = numLocals;
2678+
26682679
_catch: return result;
26692680
}
26702681

src/m3_compile.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ M3OpInfo;
140140

141141
typedef const M3OpInfo * IM3OpInfo;
142142

143-
extern const M3OpInfo* GetOpInfo(m3opcode_t opcode);
143+
IM3OpInfo GetOpInfo (m3opcode_t opcode);
144144

145145
// TODO: This helper should be removed, when MultiValue is implemented
146146
static inline

src/m3_config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# endif
3434

3535
# ifndef d_m3MaxConstantTableSize
36-
# define d_m3MaxConstantTableSize 1024
36+
# define d_m3MaxConstantTableSize 120
3737
# endif
3838

3939
# ifndef d_m3MaxDuplicateFunctionImpl

src/m3_config_platforms.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
# define M3_NO_UBSAN
7272
# else
7373
# define M3_WEAK __attribute__((weak))
74-
# define M3_NO_UBSAN __attribute__((no_sanitize("undefined")))
74+
# define M3_NO_UBSAN //__attribute__((no_sanitize("undefined")))
7575
# endif
7676

7777
# ifndef M3_MIN

src/m3_core.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ M3Result ReadLEB_i64 (i64 * o_value, bytes_t * io_bytes, cbytes_t
245245
M3Result Read_utf8 (cstr_t * o_utf8, bytes_t * io_bytes, cbytes_t i_end);
246246

247247
cstr_t SPrintValue (void * i_value, u8 i_type);
248-
size_t SPrintArg (char * o_string, size_t i_stringBufferSize, m3stack_t i_sp, u8 i_type);
248+
size_t SPrintArg (char * o_string, size_t i_stringBufferSize, voidptr_t i_sp, u8 i_type);
249249

250250
void ReportError (IM3Runtime io_runtime, IM3Module i_module, IM3Function i_function, ccstr_t i_errorMessage, ccstr_t i_file, u32 i_lineNum);
251251

src/m3_env.c

+44-22
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ void Environment_AddFuncType (IM3Environment i_environment, IM3FuncType * io_f
107107
* io_funcType = newType;
108108
}
109109

110+
110111
IM3CodePage RemoveCodePageOfCapacity (M3CodePage ** io_list, u32 i_minimumLineCount)
111112
{
112113
IM3CodePage prev = NULL;
@@ -194,6 +195,7 @@ void * m3_GetUserData (IM3Runtime i_runtime)
194195
return i_runtime ? i_runtime->userdata : NULL;
195196
}
196197

198+
197199
void * ForEachModule (IM3Runtime i_runtime, ModuleVisitor i_visitor, void * i_info)
198200
{
199201
void * r = NULL;
@@ -444,6 +446,8 @@ M3Result InitDataSegments (M3Memory * io_memory, IM3Module io_module)
444446
{
445447
M3Result result = m3Err_none;
446448

449+
_throwif ("unallocated linear memory", !(io_memory->mallocated));
450+
447451
for (u32 i = 0; i < io_module->numDataSegments; ++i)
448452
{
449453
M3DataSegment * segment = & io_module->dataSegments [i];
@@ -454,8 +458,6 @@ _ (EvaluateExpression (io_module, & segmentOffset, c_m3Type_i32, & start,
454458

455459
m3log (runtime, "loading data segment: %d; size: %d; offset: %d", i, segment->size, segmentOffset);
456460

457-
_throwif ("unallocated linear memory", !(io_memory->mallocated));
458-
459461
if (segmentOffset >= 0 && (size_t)(segmentOffset) + segment->size <= io_memory->mallocated->length)
460462
{
461463
u8 * dest = m3MemData (io_memory->mallocated) + segmentOffset;
@@ -490,14 +492,18 @@ _ (EvaluateExpression (io_module, & offset, c_m3Type_i32, & bytes, end
490492
u32 numElements;
491493
_ (ReadLEB_u32 (& numElements, & bytes, end));
492494

493-
size_t endElement = (size_t)(numElements) + offset;
495+
size_t endElement = (size_t) numElements + offset;
494496
_throwif ("table overflow", endElement > d_m3MaxSaneTableSize);
495497

496-
io_module->table0 = m3_ReallocArray (IM3Function, io_module->table0, endElement, io_module->table0Size);
498+
// is there any requirement that elements must be in increasing sequence?
499+
// make sure the table isn't shrunk.
500+
if (endElement > io_module->table0Size)
501+
{
502+
io_module->table0 = m3_ReallocArray (IM3Function, io_module->table0, endElement, io_module->table0Size);
503+
io_module->table0Size = (u32) endElement;
504+
}
497505
_throwifnull(io_module->table0);
498506

499-
io_module->table0Size = (u32) endElement;
500-
501507
for (u32 e = 0; e < numElements; ++e)
502508
{
503509
u32 functionIndex;
@@ -513,6 +519,22 @@ _ (ReadLEB_u32 (& functionIndex, & bytes, end));
513519
_catch: return result;
514520
}
515521

522+
M3Result m3_CompileModule (IM3Module io_module)
523+
{
524+
M3Result result = m3Err_none;
525+
526+
for (u32 i = 0; i < io_module->numFunctions; ++i)
527+
{
528+
IM3Function f = & io_module->functions [i];
529+
if (f->wasm and not f->compiled)
530+
{
531+
_ (CompileFunction (f));
532+
}
533+
}
534+
535+
_catch: return result;
536+
}
537+
516538
M3Result m3_RunStart (IM3Module io_module)
517539
{
518540
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
@@ -779,6 +801,16 @@ M3Result m3_CallV (IM3Function i_function, ...)
779801
return r;
780802
}
781803

804+
static
805+
void ReportNativeStackUsage ()
806+
{
807+
# if d_m3LogNativeStack
808+
int stackUsed = m3StackGetMax();
809+
fprintf (stderr, "Native stack used: %d\n", stackUsed);
810+
# endif
811+
}
812+
813+
782814
M3Result m3_CallVL (IM3Function i_function, va_list i_args)
783815
{
784816
IM3Runtime runtime = i_function->module->runtime;
@@ -808,14 +840,10 @@ M3Result m3_CallVL (IM3Function i_function, va_list i_args)
808840
}
809841
m3StackCheckInit();
810842
M3Result r = (M3Result) Call (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
843+
ReportNativeStackUsage ();
811844

812845
runtime->lastCalled = r ? NULL : i_function;
813846

814-
#if d_m3LogNativeStack
815-
int stackUsed = m3StackGetMax();
816-
fprintf (stderr, "Native stack used: %d\n", stackUsed);
817-
#endif
818-
819847
return r;
820848
}
821849

@@ -852,13 +880,10 @@ M3Result m3_Call (IM3Function i_function, uint32_t i_argc, const void * i_argp
852880

853881
m3StackCheckInit();
854882
M3Result r = (M3Result) Call (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
883+
ReportNativeStackUsage ();
855884

856885
runtime->lastCalled = r ? NULL : i_function;
857886

858-
#if d_m3LogNativeStack
859-
int stackUsed = m3StackGetMax();
860-
fprintf (stderr, "Native stack used: %d\n", stackUsed);
861-
#endif
862887

863888
return r;
864889
}
@@ -896,14 +921,10 @@ M3Result m3_CallArgv (IM3Function i_function, uint32_t i_argc, const char * i_
896921

897922
m3StackCheckInit();
898923
M3Result r = (M3Result) Call (i_function->compiled, (m3stack_t)(runtime->stack), runtime->memory.mallocated, d_m3OpDefaultArgs);
924+
ReportNativeStackUsage ();
899925

900926
runtime->lastCalled = r ? NULL : i_function;
901927

902-
#if d_m3LogNativeStack
903-
int stackUsed = m3StackGetMax();
904-
fprintf (stderr, "Native stack used: %d\n", stackUsed);
905-
#endif
906-
907928
return r;
908929
}
909930

@@ -1090,8 +1111,7 @@ void m3_ResetErrorInfo (IM3Runtime i_runtime)
10901111

10911112
uint8_t * m3_GetMemory (IM3Runtime i_runtime, uint32_t * o_memorySizeInBytes, uint32_t i_memoryIndex)
10921113
{
1093-
uint8_t * memory = NULL;
1094-
d_m3Assert (i_memoryIndex == 0);
1114+
uint8_t * memory = NULL; d_m3Assert (i_memoryIndex == 0);
10951115

10961116
if (i_runtime)
10971117
{
@@ -1107,11 +1127,13 @@ uint8_t * m3_GetMemory (IM3Runtime i_runtime, uint32_t * o_memorySizeInBytes,
11071127
return memory;
11081128
}
11091129

1130+
11101131
uint32_t m3_GetMemorySize (IM3Runtime i_runtime)
11111132
{
11121133
return i_runtime->memory.mallocated->length;
11131134
}
11141135

1136+
11151137
M3BacktraceInfo * m3_GetBacktrace (IM3Runtime i_runtime)
11161138
{
11171139
# if d_m3RecordBacktraces

0 commit comments

Comments
 (0)