Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d34997aa4e |
No files matched your search
@@ -4,16 +4,76 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
types, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types, wasm.types.heap;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : puint8;
|
||||||
|
bytesRead : uint8;
|
||||||
|
segCount : uint32;
|
||||||
|
i, j : uint32;
|
||||||
|
memIdx : uint32;
|
||||||
|
offset : uint32;
|
||||||
|
dataSize : uint32;
|
||||||
|
opcode : uint8;
|
||||||
|
tmpU32 : uint32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Data - Size: ');
|
writestring('[wasm.parser] Handle Section: Data - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := puint8(buffer + section_length);
|
||||||
|
|
||||||
|
{ Read segment count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @segCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
for i := 0 to segCount - 1 do begin
|
||||||
|
{ Read memory index }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @memIdx);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Evaluate offset init expression (expect i32.const N, end) }
|
||||||
|
offset := 0;
|
||||||
|
opcode := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
if opcode = $41 then begin { i32.const }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @tmpU32);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
offset := tmpU32;
|
||||||
|
end else begin
|
||||||
|
writestring('[wasm.parser] Warning: unsupported data offset opcode: ');
|
||||||
|
writehexpair(opcode);
|
||||||
|
writestringln(' ');
|
||||||
|
while (pos < bend) and (pos^ <> $0B) do Inc(pos);
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Consume end ($0B) }
|
||||||
|
if (pos < bend) and (pos^ = $0B) then
|
||||||
|
Inc(pos);
|
||||||
|
|
||||||
|
{ Read data byte count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @dataSize);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
writestring('[wasm.parser] Data Segment ');
|
||||||
|
writeintWND(i, 0);
|
||||||
|
writestring(' - Offset: ');
|
||||||
|
writeintWND(offset, 0);
|
||||||
|
writestring(' Size: ');
|
||||||
|
writeintlnWND(dataSize, 0);
|
||||||
|
|
||||||
|
{ Copy bytes into linear memory }
|
||||||
|
for j := 0 to dataSize - 1 do begin
|
||||||
|
wasm.types.heap.write_uint8(offset + j, ctx^.ExecutionState.Memory, pos^);
|
||||||
|
Inc(pos);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -10,10 +10,113 @@ procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContex
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
|
var
|
||||||
|
i : uint32;
|
||||||
|
entry : PWASMGlobalEntry;
|
||||||
|
begin
|
||||||
|
for i := 0 to ctx^.ExecutionState.Globals^.GlobalCount - 1 do begin
|
||||||
|
entry := @ctx^.ExecutionState.Globals^.Globals[i];
|
||||||
|
writestring('[wasm.parser] Global ');
|
||||||
|
writeintWND(i, 0);
|
||||||
|
writestring(' - Type: ');
|
||||||
|
writestring(GetWasmValueTypeString(entry^.ValueType));
|
||||||
|
if entry^.Mutable then
|
||||||
|
writestring(' (mut)')
|
||||||
|
else
|
||||||
|
writestring(' (const)');
|
||||||
|
writestring(' = ');
|
||||||
|
case entry^.ValueType of
|
||||||
|
vti32: writeintlnWND(entry^.Value.i32Value, 0);
|
||||||
|
vti64: writeintlnWND(entry^.Value.i64Value, 0);
|
||||||
|
else
|
||||||
|
writestringln('<value>');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : puint8;
|
||||||
|
bytesRead : uint8;
|
||||||
|
globalCount : uint32;
|
||||||
|
i : uint32;
|
||||||
|
entry : PWASMGlobalEntry;
|
||||||
|
valType : uint8;
|
||||||
|
mutFlag : uint8;
|
||||||
|
opcode : uint8;
|
||||||
|
tmpU32 : uint32;
|
||||||
|
tmpU64 : uint64;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Global - Size: ');
|
writestring('[wasm.parser] Handle Section: Global - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := puint8(buffer + section_length);
|
||||||
|
|
||||||
|
{ Read global count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @globalCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Allocate globals on ExecutionState }
|
||||||
|
ctx^.ExecutionState.Globals := PWASMGlobals(kalloc(sizeof(TWASMGlobals)));
|
||||||
|
ctx^.ExecutionState.Globals^.GlobalCount := globalCount;
|
||||||
|
ctx^.ExecutionState.Globals^.Globals := PWASMGlobalEntry(kalloc(sizeof(TWASMGlobalEntry) * globalCount));
|
||||||
|
|
||||||
|
for i := 0 to globalCount - 1 do begin
|
||||||
|
entry := @ctx^.ExecutionState.Globals^.Globals[i];
|
||||||
|
|
||||||
|
{ Read value type }
|
||||||
|
valType := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
entry^.ValueType := TWasmValueType(valType);
|
||||||
|
|
||||||
|
{ Read mutability }
|
||||||
|
mutFlag := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
entry^.Mutable := (mutFlag = 1);
|
||||||
|
|
||||||
|
{ Initialize value to zero }
|
||||||
|
entry^.Value.i64Value := 0;
|
||||||
|
entry^.Value.ValueType := entry^.ValueType;
|
||||||
|
|
||||||
|
{ Evaluate constant init expression }
|
||||||
|
opcode := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
case opcode of
|
||||||
|
$41: begin { i32.const }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @tmpU32);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
entry^.Value.i32Value := int32(tmpU32);
|
||||||
|
end;
|
||||||
|
$42: begin { i64.const }
|
||||||
|
bytesRead := read_leb128_to_uint64(pos, bend, @tmpU64);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
entry^.Value.i64Value := int64(tmpU64);
|
||||||
|
end;
|
||||||
|
$43: begin { f32.const }
|
||||||
|
entry^.Value.f32Value := pfloat(pos)^;
|
||||||
|
Inc(pos, 4);
|
||||||
|
end;
|
||||||
|
$44: begin { f64.const }
|
||||||
|
entry^.Value.f64Value := pdouble(pos)^;
|
||||||
|
Inc(pos, 8);
|
||||||
|
end;
|
||||||
|
else
|
||||||
|
writestring('[wasm.parser] Warning: unsupported global init opcode: ');
|
||||||
|
writehexpair(opcode);
|
||||||
|
writestringln(' ');
|
||||||
|
{ Skip to end opcode }
|
||||||
|
while (pos < bend) and (pos^ <> $0B) do Inc(pos);
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Consume the end ($0B) opcode }
|
||||||
|
if (pos < bend) and (pos^ = $0B) then
|
||||||
|
Inc(pos);
|
||||||
|
end;
|
||||||
|
|
||||||
|
walk(ctx);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -4,16 +4,81 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
types, lmemorymanager, console, leb128,
|
types, lmemorymanager, console, leb128,
|
||||||
wasm.types;
|
wasm.types, wasm.types.heap;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
procedure walk(ctx : PWASMProcessContext);
|
||||||
|
var
|
||||||
|
i : uint32;
|
||||||
|
mem : PWASMMemoryLimits;
|
||||||
|
begin
|
||||||
|
for i := 0 to ctx^.Sections.MemorySection^.MemoryCount - 1 do begin
|
||||||
|
mem := @ctx^.Sections.MemorySection^.Memories[i];
|
||||||
|
writestring('[wasm.parser] Memory ');
|
||||||
|
writeintWND(i, 0);
|
||||||
|
writestring(' - Initial: ');
|
||||||
|
writeintWND(mem^.InitialPages, 0);
|
||||||
|
if mem^.HasMax then begin
|
||||||
|
writestring(' Max: ');
|
||||||
|
writeintWND(mem^.MaxPages, 0);
|
||||||
|
end;
|
||||||
|
writestringln(' ');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : puint8;
|
||||||
|
bytesRead : uint8;
|
||||||
|
memCount : uint32;
|
||||||
|
i : uint32;
|
||||||
|
mem : PWASMMemoryLimits;
|
||||||
|
flags : uint32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
writestring('[wasm.parser] Handle Section: Memory - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := puint8(buffer + section_length);
|
||||||
|
|
||||||
|
{ Read memory count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @memCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Allocate memory section }
|
||||||
|
ctx^.Sections.MemorySection := PWASMMemorySection(kalloc(sizeof(TWASMMemorySection)));
|
||||||
|
ctx^.Sections.MemorySection^.MemoryCount := memCount;
|
||||||
|
ctx^.Sections.MemorySection^.Memories := PWASMMemoryLimits(kalloc(sizeof(TWASMMemoryLimits) * memCount));
|
||||||
|
|
||||||
|
for i := 0 to memCount - 1 do begin
|
||||||
|
mem := @ctx^.Sections.MemorySection^.Memories[i];
|
||||||
|
|
||||||
|
{ Read limits flags: 0 = no max, 1 = has max }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @flags);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
mem^.HasMax := (flags = 1);
|
||||||
|
|
||||||
|
{ Read initial page count }
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @mem^.InitialPages);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
{ Read max page count if present }
|
||||||
|
if mem^.HasMax then begin
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @mem^.MaxPages);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
end else
|
||||||
|
mem^.MaxPages := $FFFF; { no limit }
|
||||||
|
|
||||||
|
{ Expand heap to match initial pages (heap starts with 1 page) }
|
||||||
|
while ctx^.ExecutionState.Memory^.PageCount < mem^.InitialPages do
|
||||||
|
wasm.types.heap.expand_heap(ctx^.ExecutionState.Memory);
|
||||||
|
end;
|
||||||
|
|
||||||
|
walk(ctx);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -11,9 +11,20 @@ procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContex
|
|||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
procedure handle(buffer: puint8; section_length: uint32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
bytesRead : uint8;
|
||||||
|
funcIdx : uint32;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Start - Size: ');
|
writestring('[wasm.parser] Handle Section: Start - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
{ Read the start function index }
|
||||||
|
bytesRead := read_leb128_to_uint32(buffer, puint8(buffer + section_length), @funcIdx);
|
||||||
|
ctx^.Sections.StartIndex := int32(funcIdx);
|
||||||
|
|
||||||
|
writestring('[wasm.parser] Start Function Index: ');
|
||||||
|
writeintlnWND(funcIdx, 0);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -39,6 +39,13 @@ begin
|
|||||||
ctx^.Sections.FunctionSection:= nil;
|
ctx^.Sections.FunctionSection:= nil;
|
||||||
ctx^.Sections.ExportSection:= nil;
|
ctx^.Sections.ExportSection:= nil;
|
||||||
ctx^.Sections.CodeSection:= nil;
|
ctx^.Sections.CodeSection:= nil;
|
||||||
|
ctx^.Sections.MemorySection:= nil;
|
||||||
|
ctx^.Sections.StartIndex:= -1;
|
||||||
|
|
||||||
|
// Initialize globals (empty until global section is parsed)
|
||||||
|
ctx^.ExecutionState.Globals:= PWASMGlobals(kalloc(sizeof(TWASMGlobals)));
|
||||||
|
ctx^.ExecutionState.Globals^.GlobalCount:= 0;
|
||||||
|
ctx^.ExecutionState.Globals^.Globals:= nil;
|
||||||
|
|
||||||
newContext:= ctx;
|
newContext:= ctx;
|
||||||
end;
|
end;
|
||||||
|
|||||||
@@ -275,11 +275,38 @@ type
|
|||||||
end;
|
end;
|
||||||
PWASMCodeSection = ^TWASMCodeSection;
|
PWASMCodeSection = ^TWASMCodeSection;
|
||||||
|
|
||||||
|
TWASMGlobalEntry = record
|
||||||
|
ValueType : TWasmValueType;
|
||||||
|
Mutable : boolean;
|
||||||
|
Value : TWASMValueEntry;
|
||||||
|
end;
|
||||||
|
PWASMGlobalEntry = ^TWASMGlobalEntry;
|
||||||
|
|
||||||
|
TWASMGlobals = record
|
||||||
|
GlobalCount : uint32;
|
||||||
|
Globals : PWASMGlobalEntry;
|
||||||
|
end;
|
||||||
|
PWASMGlobals = ^TWASMGlobals;
|
||||||
|
|
||||||
|
TWASMMemoryLimits = record
|
||||||
|
HasMax : boolean;
|
||||||
|
InitialPages : uint32;
|
||||||
|
MaxPages : uint32;
|
||||||
|
end;
|
||||||
|
PWASMMemoryLimits = ^TWASMMemoryLimits;
|
||||||
|
|
||||||
|
TWASMMemorySection = record
|
||||||
|
MemoryCount : uint32;
|
||||||
|
Memories : PWASMMemoryLimits;
|
||||||
|
end;
|
||||||
|
PWASMMemorySection = ^TWASMMemorySection;
|
||||||
|
|
||||||
TWASMState = record
|
TWASMState = record
|
||||||
Code : puint8;
|
Code : puint8;
|
||||||
Limit : uint32;
|
Limit : uint32;
|
||||||
Locals : PWASMLocals;
|
Locals : PWASMLocals;
|
||||||
Memory : PWasmHeap;
|
Memory : PWasmHeap;
|
||||||
|
Globals : PWASMGlobals;
|
||||||
Control_Stack : PWASMStack;
|
Control_Stack : PWASMStack;
|
||||||
Operand_Stack : PWASMStack;
|
Operand_Stack : PWASMStack;
|
||||||
IP : uint32;
|
IP : uint32;
|
||||||
@@ -292,6 +319,8 @@ type
|
|||||||
FunctionSection : PWASMFunctionSection;
|
FunctionSection : PWASMFunctionSection;
|
||||||
ExportSection : PWASMExportSection;
|
ExportSection : PWASMExportSection;
|
||||||
CodeSection : PWASMCodeSection;
|
CodeSection : PWASMCodeSection;
|
||||||
|
MemorySection : PWASMMemorySection;
|
||||||
|
StartIndex : int32; { -1 = no start function }
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TWASMProcessContext = record
|
TWASMProcessContext = record
|
||||||
|
|||||||
+963
-108
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user