Mem problems fix
This commit is contained in:
@@ -21,21 +21,25 @@ type
|
|||||||
public
|
public
|
||||||
constructor Create(p:Pointer);
|
constructor Create(p:Pointer);
|
||||||
function Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;override;
|
function Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;override;
|
||||||
|
destructor Destroy;override;
|
||||||
end;
|
end;
|
||||||
{TRTBFasmVar=class(TRTBVar)
|
TRTBFasmVar=class(TRTBVar)
|
||||||
protected
|
protected
|
||||||
function SetVal(Val:TValue);virtual;abstract;
|
p:pointer;
|
||||||
function GetVal:TValue;virtual;abstract;
|
&Type:TRTBType;
|
||||||
|
procedure SetVal(Val:TValue);override;
|
||||||
|
//function GetVal:TValue;override;
|
||||||
public
|
public
|
||||||
property Val:TValue read GetVal write SetVal;
|
constructor Create(p:pointer;&Type:TRTBType);
|
||||||
end;}
|
destructor Destroy;override;
|
||||||
|
end;
|
||||||
private
|
private
|
||||||
p:Pointer;
|
p:Pointer;
|
||||||
sb:NativeUInt;
|
sb:NativeUInt;
|
||||||
funcs:TDictionary<string,NativeUInt>;
|
funcs:TDictionary<string,NativeUInt>;
|
||||||
regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>;
|
regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>;
|
||||||
function GetFuntion(Name:string):TRTBFunc;override;
|
function GetFuntion(Name:string):TRTBFunc;override;
|
||||||
{function GetVar(Name:string):TRTBVar;override;}
|
function GetVar(Name:string):TRTBVar;override;
|
||||||
public
|
public
|
||||||
constructor Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>;regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>);
|
constructor Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>;regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>);
|
||||||
destructor Destroy;override;
|
destructor Destroy;override;
|
||||||
@@ -109,7 +113,30 @@ end;
|
|||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
(*constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.TRTBFasmLibFunc.Create(p:Pointer);
|
destructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.TRTBFasmFunc.Destroy;
|
||||||
|
begin
|
||||||
|
p:=nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.TRTBFasmVar.SetVal(Val:TValue);
|
||||||
|
begin
|
||||||
|
if Val.TypeInfo=&Type then
|
||||||
|
Val.ExtractRawData(p);
|
||||||
|
end;
|
||||||
|
|
||||||
|
constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.TRTBFasmVar.Create(p:pointer;&Type:TRTBType);
|
||||||
|
begin
|
||||||
|
Self.p:=p;
|
||||||
|
Self.&Type:=&Type;
|
||||||
|
end;
|
||||||
|
|
||||||
|
destructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.TRTBFasmVar.Destroy;
|
||||||
|
begin
|
||||||
|
p:=nil;
|
||||||
|
&Type:=nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.TRTBFasmLibFunc.Create(p:Pointer);
|
||||||
begin
|
begin
|
||||||
Self.p:=p;
|
Self.p:=p;
|
||||||
end;
|
end;
|
||||||
@@ -122,21 +149,24 @@ end;
|
|||||||
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.GetFuntion(Name:string):TRTBFunc;
|
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.GetFuntion(Name:string):TRTBFunc;
|
||||||
begin
|
begin
|
||||||
Result:=TRTBFasmLibFunc.Create(GetProcAddress(Lib,pwidechar(Name)));
|
Result:=TRTBFasmLibFunc.Create(GetProcAddress(Lib,pwidechar(Name)));
|
||||||
end;*)
|
end;}
|
||||||
|
|
||||||
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.GetFuntion(Name:string):TRTBFunc;
|
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.GetFuntion(Name:string):TRTBFunc;
|
||||||
begin
|
begin
|
||||||
Result:=TRTBFasmFunc.Create(Pointer(PNativeUInt(NativeUInt(funcs.Items[Name])+NativeUInt(p))^+NativeUInt(p)));
|
Result:=TRTBFasmFunc.Create(PPointer(NativeUInt(funcs.Items[Name])+NativeUInt(p))^);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.GetVar(Name:string):TRTBVar;
|
||||||
|
begin
|
||||||
|
Result:=TRTBFasmVar.Create(PPointer(NativeUInt(regvars.Items[Name].Key)+NativeUInt(p))^,regvars.Items[Name].Value)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>;regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>);
|
constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>;regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>);
|
||||||
begin
|
begin
|
||||||
Self.p:=VirtualAlloc(nil,sb,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
|
Self.p:=p;
|
||||||
CopyMemory(Self.p,p,sb);
|
|
||||||
Self.sb:=sb;
|
Self.sb:=sb;
|
||||||
Self.funcs:=funcs;
|
Self.funcs:=funcs;
|
||||||
Self.regvars:=regvars;
|
Self.regvars:=regvars;
|
||||||
FreeMem(p);
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Destroy;
|
destructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Destroy;
|
||||||
@@ -222,11 +252,12 @@ function TRTBFasmCompiler.TRTBFasmSource.Compilate:TRTBModule;
|
|||||||
var
|
var
|
||||||
templib,pointerDecl,PreDecl:string;
|
templib,pointerDecl,PreDecl:string;
|
||||||
Res:TFasmResult;
|
Res:TFasmResult;
|
||||||
i,base:NativeUInt;
|
i,base,sb:NativeUInt;
|
||||||
FuncDict:TDictionary<string,NativeUInt>;
|
FuncDict:TDictionary<string,NativeUInt>;
|
||||||
RegVarDict:TDictionary<string,TPair<NativeUInt,PTypeInfo>>;
|
RegVarDict:TDictionary<string,TPair<NativeUInt,PTypeInfo>>;
|
||||||
|
p:pointer;
|
||||||
begin
|
begin
|
||||||
case SizeOf(pointer) of
|
case SizeOf(pointer)of
|
||||||
2:pointerDecl:='dw ';
|
2:pointerDecl:='dw ';
|
||||||
4:pointerDecl:='dd ';
|
4:pointerDecl:='dd ';
|
||||||
8:pointerDecl:='dq ';
|
8:pointerDecl:='dq ';
|
||||||
@@ -249,10 +280,21 @@ with regvars do
|
|||||||
PreDecl:=PreDecl+pointerDecl+Items[i].Key+sLineBreak;
|
PreDecl:=PreDecl+pointerDecl+Items[i].Key+sLineBreak;
|
||||||
RegVarDict.Add(Items[i].Key,TPair<NativeUInt,PTypeInfo>.Create(base+i*SizeOf(pointer),Items[i].Value));
|
RegVarDict.Add(Items[i].Key,TPair<NativeUInt,PTypeInfo>.Create(base+i*SizeOf(pointer),Items[i].Value));
|
||||||
end;
|
end;
|
||||||
Res:=FasmAssemble(PreDecl+Text+GetIncLibs,(Compiler as TRTBFasmCompiler).CompilerMem,(Compiler as TRTBFasmCompiler).MaxSteps);
|
sb:=(Compiler as TRTBFasmCompiler).CompilerMem;
|
||||||
|
p:=VirtualAlloc(nil,sb,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
|
||||||
|
Res:=FasmAssemble('org '+NativeUint(p).ToString+
|
||||||
|
sLineBreak+PreDecl+Text+GetIncLibs,sb,(Compiler as TRTBFasmCompiler).MaxSteps);
|
||||||
if Res.Error<>FASM_OK then
|
if Res.Error<>FASM_OK then
|
||||||
|
begin
|
||||||
|
VirtualFree(p,sb,MEM_RELEASE);
|
||||||
raise Exception.Create(Res.OutStr);
|
raise Exception.Create(Res.OutStr);
|
||||||
Result:=TRTBFasmModule.Create(Res.OutData,Res.sb,FuncDict,RegVarDict);
|
end;
|
||||||
|
//VirtualFree(p,sb,MEM_RELEASE);
|
||||||
|
VirtualAlloc(p,Res.sb,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
|
||||||
|
CopyMemory(p,Res.OutData,Res.sb);
|
||||||
|
FreeMem(Res.OutData);
|
||||||
|
Result:=TRTBFasmModule.Create(p,Res.sb,FuncDict,RegVarDict);
|
||||||
|
p:=nil;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TRTBFasmCompiler.TRTBFasmSource.Destroy;
|
destructor TRTBFasmCompiler.TRTBFasmSource.Destroy;
|
||||||
|
|||||||
@@ -22,19 +22,29 @@ var
|
|||||||
Fasm:TRTBFasmCompiler;
|
Fasm:TRTBFasmCompiler;
|
||||||
Src:TRTBSource;
|
Src:TRTBSource;
|
||||||
Module:TRTBModule;
|
Module:TRTBModule;
|
||||||
Func1:TRTBFunc;
|
Func1,Func2:TRTBFunc;
|
||||||
|
Var1:TRTBVar;
|
||||||
begin
|
begin
|
||||||
Fasm:=TRTBFasmCompiler.Create('..\..\..\lib\FasmOnDelphi\fasmw172\fasm');
|
Fasm:=TRTBFasmCompiler.Create('..\..\..\lib\FasmOnDelphi\fasmw172\fasm');
|
||||||
Src:=Fasm.GenNewSrc;
|
Src:=Fasm.GenNewSrc;
|
||||||
Src.Text:='use32'+sLineBreak+'main:'+sLineBreak+'pop ecx'+sLineBreak+'pop eax'+sLineBreak+'jmp ecx'+sLineBreak+'Pmain dd 0';
|
Src.Text:='use32'+sLineBreak+'main:'+sLineBreak+'pop ecx'+sLineBreak+'pop eax'+sLineBreak+'jmp ecx'+sLineBreak+'Pmain dd 0'+
|
||||||
|
sLineBreak+'varmain:'+sLineBreak+'mov eax,[Pmain]'+sLineBreak+'ret';
|
||||||
Src.RegisterFunction('','main');
|
Src.RegisterFunction('','main');
|
||||||
Src.Register('','Pmain',TypeInfo(pointer));
|
Src.RegisterFunction('','varmain');
|
||||||
|
Src.Register('','Pmain',TypeInfo(integer));
|
||||||
Module:=Src.Compilate;
|
Module:=Src.Compilate;
|
||||||
Func1:=Module.Funtion['main'];
|
Func1:=Module.Funtion['main'];
|
||||||
|
Func2:=Module.Funtion['varmain'];
|
||||||
if 454<>Func1.Call(TypeInfo(integer),[454],CRTBCallTypeStdCall).AsInteger then
|
if 454<>Func1.Call(TypeInfo(integer),[454],CRTBCallTypeStdCall).AsInteger then
|
||||||
begin
|
begin
|
||||||
raise Exception.Create('Error in test1');
|
raise Exception.Create('Error in test1');
|
||||||
end;
|
end;
|
||||||
|
Var1:=Module.&Var['Pmain'];
|
||||||
|
Var1.Val:=454;
|
||||||
|
if 454<>Func2.Call(TypeInfo(integer),[],CRTBCallTypeStdCall).AsInteger then
|
||||||
|
begin
|
||||||
|
raise Exception.Create('Error in test1');
|
||||||
|
end;
|
||||||
FreeAndNil(Func1);
|
FreeAndNil(Func1);
|
||||||
FreeAndNil(Src);
|
FreeAndNil(Src);
|
||||||
FreeAndNil(Fasm);
|
FreeAndNil(Fasm);
|
||||||
|
|||||||
Reference in New Issue
Block a user