+ added the WasmBasicType to TWasmGlobalAsmSymbol. This will help WebAssembly

validation and stack tracking.
This commit is contained in:
Nikolay Nikolov 2024-02-25 08:54:32 +02:00
parent f4b9d51aff
commit ab65c9889b
3 changed files with 40 additions and 10 deletions

View File

@ -46,6 +46,11 @@ uses
{ TWasmGlobalAsmSymbol } { TWasmGlobalAsmSymbol }
TWasmGlobalAsmSymbol = class(TAsmSymbol) TWasmGlobalAsmSymbol = class(TAsmSymbol)
private
FWasmGlobalType: TWasmBasicType;
procedure SetWasmGlobalType(AValue: TWasmBasicType);
public
property WasmGlobalType: TWasmBasicType read FWasmGlobalType write SetWasmGlobalType;
end; end;
{ TWasmValueStack } { TWasmValueStack }
@ -328,7 +333,7 @@ uses
immutable: boolean; immutable: boolean;
is_external: boolean; is_external: boolean;
is_global: boolean; is_global: boolean;
sym : tasmsymbol; sym : TWasmGlobalAsmSymbol;
constructor create(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean); constructor create(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean);
constructor create_local(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef); constructor create_local(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
constructor create_global(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef); constructor create_global(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
@ -385,6 +390,17 @@ uses
function wasm_convert_first_item_to_structured(srclist: TAsmList): tai; forward; function wasm_convert_first_item_to_structured(srclist: TAsmList): tai; forward;
procedure map_structured_asmlist_inner(l: TAsmList; f: TAsmMapFunc; blockstack: twasmstruc_stack); forward; procedure map_structured_asmlist_inner(l: TAsmList; f: TAsmMapFunc; blockstack: twasmstruc_stack); forward;
{ TWasmGlobalAsmSymbol }
procedure TWasmGlobalAsmSymbol.SetWasmGlobalType(AValue: TWasmBasicType);
begin
if FWasmGlobalType=AValue then
Exit;
if FWasmGlobalType<>wbt_Unknown then
Internalerror(2024022503);
FWasmGlobalType:=AValue;
end;
{ TWasmValueStack } { TWasmValueStack }
function TWasmValueStack.GetItems(AIndex: Integer): TWasmBasicType; function TWasmValueStack.GetItems(AIndex: Integer): TWasmBasicType;
@ -1657,7 +1673,8 @@ uses
constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean); constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
begin begin
inherited Create; inherited Create;
sym:=current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AT_WASM_GLOBAL); sym:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AT_WASM_GLOBAL));
sym.WasmGlobalType:=atype;
typ:=ait_globaltype; typ:=ait_globaltype;
globalname:=aglobalname; globalname:=aglobalname;
gtype:=atype; gtype:=atype;
@ -1669,7 +1686,8 @@ uses
constructor tai_globaltype.create_local(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef); constructor tai_globaltype.create_local(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
begin begin
inherited Create; inherited Create;
sym:=current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_LOCAL,AT_WASM_GLOBAL,def); sym:=TWasmGlobalAsmSymbol(current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_LOCAL,AT_WASM_GLOBAL,def));
sym.WasmGlobalType:=atype;
typ:=ait_globaltype; typ:=ait_globaltype;
globalname:=aglobalname; globalname:=aglobalname;
gtype:=atype; gtype:=atype;
@ -1681,7 +1699,8 @@ uses
constructor tai_globaltype.create_global(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef); constructor tai_globaltype.create_global(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
begin begin
inherited Create; inherited Create;
sym:=current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_GLOBAL,AT_WASM_GLOBAL,def); sym:=TWasmGlobalAsmSymbol(current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_GLOBAL,AT_WASM_GLOBAL,def));
sym.WasmGlobalType:=atype;
typ:=ait_globaltype; typ:=ait_globaltype;
globalname:=aglobalname; globalname:=aglobalname;
gtype:=atype; gtype:=atype;

View File

@ -28,7 +28,7 @@ interface
uses uses
globtype, globtype,
aasmbase,aasmdata, aasmbase,aasmdata,aasmcpu,
symbase,symconst,symtype,symdef,symsym, symbase,symconst,symtype,symdef,symsym,
node, node,
cpubase, hlcgobj, cgbase, cgutils, parabase, wasmdef; cpubase, hlcgobj, cgbase, cgutils, parabase, wasmdef;
@ -55,6 +55,7 @@ uses
(check d.size to determine which one of the two) (check d.size to determine which one of the two)
} }
function is_methodptr_like_type(d:tdef): boolean; function is_methodptr_like_type(d:tdef): boolean;
function RefStackPointerSym: TWasmGlobalAsmSymbol;
public public
fntypelookup : TWasmProcTypeLookup; fntypelookup : TWasmProcTypeLookup;
@ -257,7 +258,7 @@ implementation
uses uses
verbose,cutils,globals,fmodule,constexp, verbose,cutils,globals,fmodule,constexp,
defutil,cpupi, defutil,cpupi,
aasmtai,aasmcpu, aasmtai,
symtable,symcpu, symtable,symcpu,
procinfo,cpuinfo,cgobj,cgcpu,tgobj,tgcpu,paramgr; procinfo,cpuinfo,cgobj,cgcpu,tgobj,tgcpu,paramgr;
@ -317,6 +318,12 @@ implementation
result:=is_8byterecord or is_methodptr or is_nestedprocptr; result:=is_8byterecord or is_methodptr or is_nestedprocptr;
end; end;
function thlcgwasm.RefStackPointerSym: TWasmGlobalAsmSymbol;
begin
result:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL));
result.WasmGlobalType:=wbt_i32;
end;
constructor thlcgwasm.create; constructor thlcgwasm.create;
begin begin
fevalstackheight:=0; fevalstackheight:=0;
@ -2049,7 +2056,7 @@ implementation
g_fingerprint(list); g_fingerprint(list);
list.Concat(taicpu.op_sym(a_global_get,current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL))); list.Concat(taicpu.op_sym(a_global_get,RefStackPointerSym));
incstack(list,1); incstack(list,1);
list.Concat(taicpu.op_ref(a_local_set,pd.base_pointer_ref)); list.Concat(taicpu.op_ref(a_local_set,pd.base_pointer_ref));
decstack(list,1); decstack(list,1);
@ -2065,7 +2072,7 @@ implementation
decstack(list,1); decstack(list,1);
list.Concat(taicpu.op_ref(a_local_get,pd.frame_pointer_ref)); list.Concat(taicpu.op_ref(a_local_get,pd.frame_pointer_ref));
incstack(list,1); incstack(list,1);
list.Concat(taicpu.op_sym(a_global_set,current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL))); list.Concat(taicpu.op_sym(a_global_set,RefStackPointerSym));
decstack(list,1); decstack(list,1);
end; end;
end; end;
@ -2077,7 +2084,7 @@ implementation
pd:=tcpuprocdef(current_procinfo.procdef); pd:=tcpuprocdef(current_procinfo.procdef);
list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref)); list.Concat(taicpu.op_ref(a_local_get,pd.base_pointer_ref));
incstack(list,1); incstack(list,1);
list.Concat(taicpu.op_sym(a_global_set,current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,STACK_POINTER_SYM,AT_WASM_GLOBAL))); list.Concat(taicpu.op_sym(a_global_set,RefStackPointerSym));
decstack(list,1); decstack(list,1);
list.concat(taicpu.op_none(a_return)); list.concat(taicpu.op_none(a_return));

View File

@ -543,8 +543,12 @@ implementation
procedure twasminlinenode.second_tls_get(const SymStr: string); procedure twasminlinenode.second_tls_get(const SymStr: string);
var
sym: TWasmGlobalAsmSymbol;
begin begin
current_asmdata.CurrAsmList.Concat(taicpu.op_sym(a_global_get,current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,SymStr,AT_WASM_GLOBAL))); sym:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,SymStr,AT_WASM_GLOBAL));
sym.WasmGlobalType:=wbt_i32;
current_asmdata.CurrAsmList.Concat(taicpu.op_sym(a_global_get,sym));
thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1); thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
location_reset(location,LOC_REGISTER,def_cgsize(resultdef)); location_reset(location,LOC_REGISTER,def_cgsize(resultdef));