Skip to content

Extended RTTI Bug

I'm using Free Pascal Compiler version 3.3.1-13996-ga8b4c0772c [2024/01/10] for x86_64

I was testing the new Extended RTTI stuff and tried to test it with a class that contains functions. It gave an access violation error.

Project RttiParse raised exception class 'External: ACCESS VIOLATION' with message:
Access violation reading from address $00000001004FA172.

 In file 'typinfo.pp' at line 4657:
Result := PVmtMethodExEntry(Tail);

And this here's a small PoC.

Program RttiParse;

{$mode OBJFPC}{$H+}{$M+}
{$Modeswitch advancedrecords}
{$RTTI EXPLICIT PROPERTIES([vcPrivate,vcProtected,vcPublic,vcPublished])}
{$RTTI EXPLICIT FIELDS([vcPrivate,vcProtected,vcPublic,vcPublished])}
{$RTTI EXPLICIT METHODS([vcPrivate,vcProtected,vcPublic,vcPublished])}

Uses
  SysUtils,
  TypInfo;

Type
  { TUser }

  TUser = Class
  Private
    FName: string;
    FID: Integer;

  Public
    Procedure PrintName;
    Function Concat(S1, S2: string): string;
    Constructor Create;
    Destructor Destroy; Override;
  Published
    Property ID: Integer read FID write FID;
    Property Name: string read FName write FName;
  End;

  { TUser }

  Function TUser.Concat(S1, S2: string): string;
  Begin
    Result := S1 + S2;
  End;

  Constructor TUser.Create;
  Begin
    FName := 'TUser.Name: ' + WChar($20A1) + ' ' + WChar($2211);
  End;

  Destructor TUser.Destroy;
  Begin
    WriteLn('TUser.Destroy');
    Inherited;
  End;

  Procedure TUser.PrintName;
  Begin
    WriteLn('User Name : ', Name);
  End;

Var
  I: Integer;
  Count: LongInt;
  Method: PVmtMethodExEntry;
  Methods: PExtendedMethodInfoTable;
Begin
  Count := GetMethodList(TUser, Methods, []);
  If Methods <> nil Then
  begin
    For I := 0 To Pred(Count) Do
    Begin
      Method := Methods^[I];
      WriteLn(Format('%-10s - %d', [Method^.Name, Method^.Kind]));
    End;
    Freemem(Methods);
  end;
End.

If we remove Function Concat(S1, S2: string): string; the GetMethodList works fine. But still accessing the Destroy Method info gives an access violation error.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information