Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b937e60838 | ||
|
|
255ad4806e | ||
|
|
93dfff358d | ||
|
|
3ed5c0a847 | ||
|
|
ff65a0d5f0 | ||
|
|
76d7d737c0 | ||
|
|
d12ad11746 | ||
|
|
679973c6e9 | ||
|
|
68df3565cb | ||
|
|
862e3254df |
No files matched your search
@@ -1,3 +1,4 @@
|
||||
bin
|
||||
lib
|
||||
AGENTS.md
|
||||
GAP_ANALYSIS.md
|
||||
@@ -8,7 +8,34 @@ set LIB=%ROOT%lib
|
||||
set FPC=C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe
|
||||
|
||||
set TESTFLAGS=
|
||||
set DO_CLEAN=0
|
||||
set DO_RUN=0
|
||||
|
||||
:parse_args
|
||||
if "%1"=="" goto args_done
|
||||
if "%1"=="test" set TESTFLAGS=-dRUN_TESTS
|
||||
if "%1"=="clean" set DO_CLEAN=1
|
||||
if "%1"=="run" set DO_RUN=1
|
||||
if "%1"=="help" (
|
||||
echo Usage: build.bat [options]
|
||||
echo Options:
|
||||
echo test - Build with tests enabled
|
||||
echo clean - Clean build outputs
|
||||
echo run - Run the built executable after building
|
||||
echo help - Show this help message
|
||||
exit /b 0
|
||||
)
|
||||
shift
|
||||
goto parse_args
|
||||
:args_done
|
||||
|
||||
if %DO_CLEAN%==1 (
|
||||
echo Cleaning build outputs...
|
||||
if exist "%BIN%" rd /s /q "%BIN%"
|
||||
if exist "%LIB%" rd /s /q "%LIB%"
|
||||
echo Clean complete.
|
||||
if "%TESTFLAGS%"=="" if %DO_RUN%==0 exit /b 0
|
||||
)
|
||||
|
||||
if not exist "%BIN%" mkdir "%BIN%"
|
||||
if not exist "%LIB%" mkdir "%LIB%"
|
||||
@@ -17,6 +44,7 @@ REM ── Lint ─────────────────────
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File "%ROOT%lint.ps1" -SrcRoot "%SRC%"
|
||||
if %ERRORLEVEL% NEQ 0 exit /b 1
|
||||
|
||||
REM ── Build ────────────────────────────────────────────────────────────
|
||||
"%FPC%" ^
|
||||
%TESTFLAGS% ^
|
||||
-FE"%BIN%" ^
|
||||
@@ -32,6 +60,7 @@ if %ERRORLEVEL% NEQ 0 exit /b 1
|
||||
-Fu"%SRC%\wasm\test\infra" ^
|
||||
-Fu"%SRC%\wasm\types" ^
|
||||
-Fu"%SRC%\wasm\vm" ^
|
||||
-Fu"%SRC%\wasm\vm\opcodes" ^
|
||||
-gw3 ^
|
||||
-o"%BIN%\WASURO.exe" ^
|
||||
"%SRC%\project\WASURO.lpr"
|
||||
@@ -42,3 +71,9 @@ if %ERRORLEVEL% NEQ 0 (
|
||||
)
|
||||
|
||||
echo Build succeeded: %BIN%\WASURO.exe
|
||||
|
||||
REM ── Run ────────────────────────────────────────────────────────────
|
||||
if %DO_RUN%==1 (
|
||||
echo Running WASURO...
|
||||
"%BIN%\WASURO.exe"
|
||||
)
|
||||
@@ -7,7 +7,7 @@ uses
|
||||
|
||||
types,
|
||||
|
||||
wasm.types,
|
||||
wasm.types.context,
|
||||
wasm.parser,
|
||||
wasm.vm, wasm.test.binary.return42,
|
||||
wasm.types.stack
|
||||
|
||||
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
unit wasm.parser.sections.dataCountSection;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
implementation
|
||||
|
||||
uses console, leb128, lmemorymanager, wasm.types.sections;
|
||||
|
||||
{ DataCount section (ID 12): contains a single u32 count of data segments.
|
||||
Pre-allocates the DataSegments array so that bulk memory opcodes
|
||||
can reference segments by index during validation. }
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
var
|
||||
pos, bend : TWASMPUInt8;
|
||||
bytesRead : TWASMUInt8;
|
||||
dataCount : TWASMUInt32;
|
||||
begin
|
||||
writestring('[wasm.parser] Handle Section: DataCount - Size: ');
|
||||
writeintlnWND(section_length, 0);
|
||||
|
||||
pos := buffer;
|
||||
bend := TWASMPUInt8(buffer + section_length);
|
||||
|
||||
bytesRead := read_leb128_to_uint32(pos, bend, @dataCount);
|
||||
Inc(pos, bytesRead);
|
||||
|
||||
writestring('[wasm.parser] Data Count: ');
|
||||
writeintlnWND(dataCount, 0);
|
||||
|
||||
{ Pre-allocate data segment storage with correct count.
|
||||
The actual segment data will be filled when the Data section is parsed. }
|
||||
ctx^.ExecutionState.DataSegments^.SegmentCount := dataCount;
|
||||
if dataCount > 0 then
|
||||
ctx^.ExecutionState.DataSegments^.Segments := PWASMDataSegment(kalloc(dataCount * sizeof(TWASMDataSegment)))
|
||||
else
|
||||
ctx^.ExecutionState.DataSegments^.Segments := nil;
|
||||
end;
|
||||
|
||||
end.
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types.enums, wasm.types.sections, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types.sections, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types.enums, wasm.types.sections, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types, wasm.types.heap;
|
||||
wasm.types.sections, wasm.types.context, wasm.types.heap;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
|
||||
@@ -4,16 +4,85 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types.sections, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
implementation
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
var
|
||||
pos: TWASMPUInt8;
|
||||
buf_end: TWASMPUInt8;
|
||||
bytesRead: TWASMUInt8;
|
||||
table_count, i, j, min_size, max_size: TWASMUInt32;
|
||||
elem_type, limits_flag: TWASMUInt8;
|
||||
tables: PWASMTables;
|
||||
begin
|
||||
writestring('[wasm.parser] Handle Section: Table - Size: ');
|
||||
writeintlnWND(section_length, 0);
|
||||
|
||||
pos := buffer;
|
||||
buf_end := buffer;
|
||||
Inc(buf_end, section_length);
|
||||
|
||||
bytesRead := read_leb128_to_uint32(pos, buf_end, @table_count);
|
||||
Inc(pos, bytesRead);
|
||||
|
||||
writestring('[wasm.parser] Tables: ');
|
||||
writeintlnWND(table_count, 0);
|
||||
|
||||
tables := ctx^.ExecutionState.Tables;
|
||||
tables^.TableCount := table_count;
|
||||
if table_count > 0 then
|
||||
tables^.Tables := PWASMTableInstance(kalloc(sizeof(TWASMTableInstance) * table_count))
|
||||
else
|
||||
tables^.Tables := nil;
|
||||
|
||||
for i := 0 to table_count - 1 do begin
|
||||
elem_type := pos^;
|
||||
Inc(pos);
|
||||
|
||||
limits_flag := pos^;
|
||||
Inc(pos);
|
||||
|
||||
bytesRead := read_leb128_to_uint32(pos, buf_end, @min_size);
|
||||
Inc(pos, bytesRead);
|
||||
|
||||
max_size := 0;
|
||||
if limits_flag = 1 then begin
|
||||
bytesRead := read_leb128_to_uint32(pos, buf_end, @max_size);
|
||||
Inc(pos, bytesRead);
|
||||
end;
|
||||
|
||||
writestring('[wasm.parser] Table ');
|
||||
writeintWND(i, 0);
|
||||
writestring(' - ElemType: $');
|
||||
writeintWND(elem_type, 0);
|
||||
writestring(' Initial: ');
|
||||
writeintWND(min_size, 0);
|
||||
if limits_flag = 1 then begin
|
||||
writestring(' Max: ');
|
||||
writeintWND(max_size, 0);
|
||||
end;
|
||||
writestringln('');
|
||||
|
||||
tables^.Tables[i].ElementType := elem_type;
|
||||
tables^.Tables[i].Size := min_size;
|
||||
tables^.Tables[i].HasMax := (limits_flag = 1);
|
||||
if limits_flag = 1 then
|
||||
tables^.Tables[i].MaxSize := max_size
|
||||
else
|
||||
tables^.Tables[i].MaxSize := 0;
|
||||
|
||||
{ Allocate elements and initialize to $FFFFFFFF (uninitialized) }
|
||||
if min_size > 0 then begin
|
||||
tables^.Tables[i].Elements := TWASMPUInt32(kalloc(sizeof(TWASMUInt32) * min_size));
|
||||
for j := 0 to min_size - 1 do
|
||||
tables^.Tables[i].Elements[j] := $FFFFFFFF;
|
||||
end else
|
||||
tables^.Tables[i].Elements := nil;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types;
|
||||
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
|
||||
|
||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||
wasm.types, wasm.types.heap, wasm.types.stack,
|
||||
wasm.parser.sections;
|
||||
wasm.types.enums, wasm.types.sections, wasm.types.context, wasm.types.heap, wasm.types.stack,
|
||||
wasm.parser.sections, wasm.types.constants;
|
||||
|
||||
function parse(buffer: TWASMPUInt8; buffer_end: TWASMPUInt8) : PWASMProcessContext;
|
||||
|
||||
@@ -36,6 +36,7 @@ begin
|
||||
|
||||
// Initialize the sections
|
||||
ctx^.Sections.TypeSection:= nil;
|
||||
ctx^.Sections.ImportSection:= nil;
|
||||
ctx^.Sections.FunctionSection:= nil;
|
||||
ctx^.Sections.ExportSection:= nil;
|
||||
ctx^.Sections.CodeSection:= nil;
|
||||
@@ -47,6 +48,21 @@ begin
|
||||
ctx^.ExecutionState.Globals^.GlobalCount:= 0;
|
||||
ctx^.ExecutionState.Globals^.Globals:= nil;
|
||||
|
||||
// Initialize tables (empty until table section is parsed)
|
||||
ctx^.ExecutionState.Tables:= PWASMTables(kalloc(sizeof(TWASMTables)));
|
||||
ctx^.ExecutionState.Tables^.TableCount:= 0;
|
||||
ctx^.ExecutionState.Tables^.Tables:= nil;
|
||||
|
||||
// Initialize data segments (empty until data section is parsed)
|
||||
ctx^.ExecutionState.DataSegments:= PWASMDataSegments(kalloc(sizeof(TWASMDataSegments)));
|
||||
ctx^.ExecutionState.DataSegments^.SegmentCount:= 0;
|
||||
ctx^.ExecutionState.DataSegments^.Segments:= nil;
|
||||
|
||||
// Initialize element segments (empty until element section is parsed)
|
||||
ctx^.ExecutionState.ElementSegments:= PWASMElementSegments(kalloc(sizeof(TWASMElementSegments)));
|
||||
ctx^.ExecutionState.ElementSegments^.SegmentCount:= 0;
|
||||
ctx^.ExecutionState.ElementSegments^.Segments:= nil;
|
||||
|
||||
newContext:= ctx;
|
||||
end;
|
||||
|
||||
@@ -66,7 +82,7 @@ begin
|
||||
writestringln('[wasm.parser] Parsing WASM Binary');
|
||||
|
||||
// Check for the WASM Magic
|
||||
if(TWASMPUInt32(pos)^ = wasm.types.WASM_HDR_MAGIC) then begin
|
||||
if(TWASMPUInt32(pos)^ = wasm.types.constants.WASM_HDR_MAGIC) then begin
|
||||
writestringln('[wasm.parser] Binary is valid.');
|
||||
|
||||
// Set binary to valid
|
||||
|
||||
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
wasm.types.builtin,
|
||||
wasm.types,
|
||||
wasm.types.enums, wasm.types.context,
|
||||
wasm.parser.sections.customSection,
|
||||
wasm.parser.sections.typeSection,
|
||||
wasm.parser.sections.importSection,
|
||||
@@ -16,7 +16,8 @@ uses
|
||||
wasm.parser.sections.startSection,
|
||||
wasm.parser.sections.elementSection,
|
||||
wasm.parser.sections.codeSection,
|
||||
wasm.parser.sections.dataSection;
|
||||
wasm.parser.sections.dataSection,
|
||||
wasm.parser.sections.dataCountSection;
|
||||
|
||||
procedure handle(sectionId : TWASMUInt8; buffer : TWASMPUInt8; section_length : TWASMUInt32; ctx : PWASMProcessContext);
|
||||
|
||||
@@ -50,6 +51,8 @@ begin
|
||||
ord(sidCode): wasm.parser.sections.codeSection.handle (buffer, section_length, ctx);
|
||||
// Data Section
|
||||
ord(sidData): wasm.parser.sections.dataSection.handle (buffer, section_length, ctx);
|
||||
// DataCount Section
|
||||
ord(sidDataCount): wasm.parser.sections.dataCountSection.handle (buffer, section_length, ctx);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
unit wasm.parser.validate;
|
||||
|
||||
interface
|
||||
|
||||
uses wasm.types.builtin, wasm.types.context;
|
||||
|
||||
{ Validates a parsed WASM module for structural consistency.
|
||||
Returns true if valid, false otherwise.
|
||||
Sets errorMsg to a description of the first error found. }
|
||||
function validate_module(ctx : PWASMProcessContext; var errorMsg : TWASMPChar) : TWASMBoolean;
|
||||
|
||||
implementation
|
||||
|
||||
uses console;
|
||||
|
||||
function validate_module(ctx : PWASMProcessContext; var errorMsg : TWASMPChar) : TWASMBoolean;
|
||||
var i : TWASMUInt32;
|
||||
begin
|
||||
validate_module := true;
|
||||
|
||||
{ Check 1: Binary must be valid (magic number was correct) }
|
||||
if not ctx^.ValidBinary then begin
|
||||
errorMsg := 'Invalid WASM binary: bad magic number';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
|
||||
{ Check 2: Version must be 1 (MVP) }
|
||||
if ctx^.Version <> 1 then begin
|
||||
errorMsg := 'Unsupported WASM version (expected 1)';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
|
||||
{ Check 3: If function section exists, code section must exist and counts must match }
|
||||
if (ctx^.Sections.FunctionSection <> nil) then begin
|
||||
if (ctx^.Sections.CodeSection = nil) then begin
|
||||
errorMsg := 'Function section present but code section missing';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
if ctx^.Sections.FunctionSection^.FunctionCount <> ctx^.Sections.CodeSection^.CodeCount then begin
|
||||
errorMsg := 'Function count does not match code count';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ Check 4: If code section exists, function section must exist }
|
||||
if (ctx^.Sections.CodeSection <> nil) then begin
|
||||
if (ctx^.Sections.FunctionSection = nil) then begin
|
||||
errorMsg := 'Code section present but function section missing';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ Check 5: Function type indices must be within range of type section }
|
||||
if (ctx^.Sections.FunctionSection <> nil) and (ctx^.Sections.TypeSection <> nil) then begin
|
||||
for i := 0 to ctx^.Sections.FunctionSection^.FunctionCount - 1 do begin
|
||||
if ctx^.Sections.FunctionSection^.Functions[i].Index >= ctx^.Sections.TypeSection^.TypeCount then begin
|
||||
errorMsg := 'Function type index out of range';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ Check 6: Start function index must be within range }
|
||||
if ctx^.Sections.StartIndex >= 0 then begin
|
||||
if (ctx^.Sections.FunctionSection = nil) then begin
|
||||
errorMsg := 'Start function specified but no function section';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
if TWASMUInt32(ctx^.Sections.StartIndex) >= ctx^.Sections.FunctionSection^.FunctionCount then begin
|
||||
errorMsg := 'Start function index out of range';
|
||||
validate_module := false;
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
errorMsg := nil;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -18,7 +18,7 @@ procedure run;
|
||||
implementation
|
||||
|
||||
uses
|
||||
wasm.types.builtin, console, wasm.types, wasm.types.stack,
|
||||
wasm.types.builtin, console, wasm.types.context, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
|
||||
Loaded 20 of 449 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user