Discussion:
[fpc-devel] Quick question on VMT / writeable or protected memory
Martin Frb via fpc-devel
2021-04-13 23:22:16 UTC
Permalink
Opening a can of worms here. But leaving all pitfalls aside.

If one wanted to write to the VMT of a class, would that be possible?
Would the VMT be stored in write-able memory, once the exe is loaded?

Thanks
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/c
Jonas Maebe via fpc-devel
2021-04-14 06:40:00 UTC
Permalink
Post by Martin Frb via fpc-devel
Opening a can of worms here. But leaving all pitfalls aside.
If one wanted to write to the VMT of a class, would that be possible?
Would the VMT be stored in write-able memory, once the exe is loaded?
It depends on the platforms. Most platforms support read-only
relocatable memory, so they can make it permanently read-only after the
(dynamic) linker has relocated everything. I don't know which ones do/don't.


Jonas
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailm
Alfred via fpc-devel
2021-04-14 15:49:07 UTC
Permalink
The mORMot[1] sources could give you some info.
During runtime, it patches the exe-memory to redirect function calls.
Hard part was to get around the W^X memory protection on some BSD's.

Look at:

procedure PatchCode(Old,New: pointer; Size: integer; Backup: pointer;
inside SynCommons.pas

function TInterfaceFactory.GetMethodsVirtualTable: pointer;
inside mORMot.pas

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mail
Jonas Maebe via fpc-devel
2021-04-14 16:33:31 UTC
Permalink
Post by Alfred via fpc-devel
The mORMot[1] sources could give you some info.
During runtime, it patches the exe-memory to redirect function calls.
Hard part was to get around the W^X memory protection on some BSD's.
procedure PatchCode(Old,New: pointer; Size: integer; Backup: pointer;
inside SynCommons.pas
function TInterfaceFactory.GetMethodsVirtualTable: pointer;
inside mORMot.pas
Please don't use that kind of hacks. It also breaks in case WPO is used
and some calls get devirtualised at compile time.


Jonas
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lis
Michael Van Canneyt via fpc-devel
2021-04-14 16:48:50 UTC
Permalink
Post by Jonas Maebe via fpc-devel
Post by Alfred via fpc-devel
The mORMot[1] sources could give you some info.
During runtime, it patches the exe-memory to redirect function calls.
Hard part was to get around the W^X memory protection on some BSD's.
procedure PatchCode(Old,New: pointer; Size: integer; Backup: pointer;
inside SynCommons.pas
function TInterfaceFactory.GetMethodsVirtualTable: pointer;
inside mORMot.pas
Please don't use that kind of hacks. It also breaks in case WPO is used
and some calls get devirtualised at compile time.
This is mORMot. It consists solely of hacks :)

Michael.
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc

Loading...