Skip to content

Commit

Permalink
introducing NOVMTPATCH conditional
Browse files Browse the repository at this point in the history
- to disable the vmtAutoTable slot runtime patch, replacing it with a slowest algo
- could be used e.g. when in-memory executables can't be patched (e.g. on security constrained systems, or on OS without any mmap support)
- fallback to the regular Rtti.FindType() lookup which is not O(1) and could still be optimized
  • Loading branch information
Arnaud Bouchez committed Aug 8, 2023
1 parent a027c78 commit fb49b06
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 40 deletions.
16 changes: 10 additions & 6 deletions src/core/mormot.core.interfaces.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5427,7 +5427,7 @@ TRttiCustomWrapper = class(TRttiJson);

procedure TInjectableObject.AutoResolve(aRaiseEServiceExceptionIfNotFound: boolean);
var
rtti: TRttiJson;
r: TRttiJson;
n: integer;
p: PPRttiCustomProp;
addr: pointer;
Expand All @@ -5437,12 +5437,16 @@ procedure TInjectableObject.AutoResolve(aRaiseEServiceExceptionIfNotFound: boole
raise EInterfaceResolver.CreateUtf8(
'%.AutoResolve with no prior registration', [self]);
// inlined Rtti.RegisterClass()
rtti := PPointer(PPAnsiChar(self)^ + vmtAutoTable)^;
if (rtti = nil) or
not (rcfAutoCreateFields in rtti.Flags) then
rtti := DoRegisterAutoCreateFields(self);
{$ifdef NOVMTPATCH}
r := pointer(Rtti.FindType(PPointer(PPAnsiChar(self)^ + vmtTypeInfo)^));
{$else}
r := PPointer(PPAnsiChar(self)^ + vmtAutoTable)^;
{$endif NOVMTPATCH}
if (r = nil) or
not (rcfAutoCreateFields in r.Flags) then
r := DoRegisterAutoCreateFields(self);
// resolve all published interface fields
p := pointer(TRttiCustomWrapper(rtti).fAutoCreateInterfaces);
p := pointer(TRttiCustomWrapper(r).fAutoCreateInterfaces);
if p = nil then
exit;
n := PDALen(PAnsiChar(p) - _DALEN)^ + _DAOFF; // length(AutoCreateClasses)
Expand Down
28 changes: 18 additions & 10 deletions src/core/mormot.core.json.pas
Original file line number Diff line number Diff line change
Expand Up @@ -11288,16 +11288,20 @@ function DoRegisterAutoCreateFields(ObjectInstance: TObject): TRttiJson;

procedure AutoCreateFields(ObjectInstance: TObject);
var
rtti: TRttiJson;
r: TRttiJson;
n: integer;
p: PPRttiCustomProp;
begin
// inlined Rtti.RegisterClass()
rtti := PPointer(PPAnsiChar(ObjectInstance)^ + vmtAutoTable)^;
if (rtti = nil) or
not (rcfAutoCreateFields in rtti.Flags) then
rtti := DoRegisterAutoCreateFields(ObjectInstance);
p := pointer(rtti.fAutoCreateClasses);
{$ifdef NOVMTPATCH}
r := pointer(Rtti.FindType(PPointer(PPAnsiChar(ObjectInstance)^ + vmtTypeInfo)^));
{$else}
r := PPointer(PPAnsiChar(ObjectInstance)^ + vmtAutoTable)^;
{$endif NOVMTPATCH}
if (r = nil) or
not (rcfAutoCreateFields in r.Flags) then
r := DoRegisterAutoCreateFields(ObjectInstance);
p := pointer(r.fAutoCreateClasses);
if p = nil then
exit;
// create all published class fields
Expand All @@ -11313,15 +11317,19 @@ procedure AutoCreateFields(ObjectInstance: TObject);

procedure AutoDestroyFields(ObjectInstance: TObject);
var
rtti: TRttiJson;
r: TRttiJson;
n: integer;
p: PPRttiCustomProp;
arr: pointer;
o: TObject;
begin
rtti := PPointer(PPAnsiChar(ObjectInstance)^ + vmtAutoTable)^;
{$ifdef NOVMTPATCH}
r := pointer(Rtti.FindType(PPointer(PPAnsiChar(ObjectInstance)^ + vmtTypeInfo)^));
{$else}
r := PPointer(PPAnsiChar(ObjectInstance)^ + vmtAutoTable)^;
{$endif NOVMTPATCH}
// free all published class fields
p := pointer(rtti.fAutoCreateClasses);
p := pointer(r.fAutoCreateClasses);
if p <> nil then
begin
n := PDALen(PAnsiChar(p) - _DALEN)^ + _DAOFF;
Expand All @@ -11335,7 +11343,7 @@ procedure AutoDestroyFields(ObjectInstance: TObject);
until n = 0;
end;
// release all published T*ObjArray fields
p := pointer(rtti.fAutoCreateObjArrays);
p := pointer(r.fAutoCreateObjArrays);
if p = nil then
exit;
n := PDALen(PAnsiChar(p) - _DALEN)^ + _DAOFF;
Expand Down
14 changes: 13 additions & 1 deletion src/core/mormot.core.log.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4211,7 +4211,11 @@ class function TSynLog.Family: TSynLogFamily;
if result <> nil then
begin
// inlined Rtti.Find(ClassType)
{$ifdef NOVMTPATCH}
result := pointer(Rtti.FindType(PPointer(PAnsiChar(result) + vmtTypeInfo)^));
{$else}
result := PPointer(PAnsiChar(result) + vmtAutoTable)^;
{$endif NOVMTPATCH}
if result <> nil then
// we know TRttiCustom is in the slot, and PrivateSlot as TSynLogFamily
result := TRttiCustom(pointer(result)).PrivateSlot;
Expand All @@ -4229,7 +4233,11 @@ class function TSynLog.Add: TSynLog;
result := pointer(Self);
if result <> nil then
begin
{$ifdef NOVMTPATCH}
P := pointer(Rtti.FindType(PPointer(PAnsiChar(result) + vmtTypeInfo)^));
{$else}
P := PPointer(PAnsiChar(result) + vmtAutoTable)^;
{$endif NOVMTPATCH}
if P <> nil then
begin
// we know TRttiCustom is in the slot, and Private is TSynLogFamily
Expand All @@ -4247,19 +4255,23 @@ class function TSynLog.Add: TSynLog;
class function TSynLog.FamilyCreate: TSynLogFamily;
var
rtticustom: TRttiCustom;
{$ifndef NOVMTPATCH}
vmt: TObject;
{$endif NOVMTPATCH}
begin
// private sub function called from inlined TSynLog.Family / TSynLog.Add
if (self <> nil) and
InheritsFrom(TSynLog) then // paranoid
begin
rtticustom := Rtti.RegisterClass(self);
{$ifndef NOVMTPATCH}
vmt := PPointer(PAnsiChar(self) + vmtAutoTable)^;
if (rtticustom = nil) or
(vmt <> rtticustom) then
// TSynLog.Family / TSynLog.Add expect TRttiCustom in the first slot
raise ESynLogException.CreateUtf8(
'%.FamilyCreate: vmtAutoTable=% not %', [self, vmt, rtticustom]);
{$endif NOVMTPATCH}
Rtti.RegisterSafe.Lock;
try
result := rtticustom.PrivateSlot;
Expand All @@ -4269,7 +4281,7 @@ class function TSynLog.FamilyCreate: TSynLogFamily;
exit
else
raise ESynLogException.CreateUtf8( // paranoid
'%.FamilyCreate: vmtAutoTable=%', [self, result]);
'%.FamilyCreate: PrivateSlot=%', [self, result]);
// create the TSynLogFamily instance associated with this TSynLog class
result := TSynLogFamily.Create(self); // stored in SynLogFamily[]
rtticustom.PrivateSlot := result; // will be owned by this TRttiCustom
Expand Down

0 comments on commit fb49b06

Please sign in to comment.