Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2021-04

Quick question on VMT / writeable or protected memory

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin Frb

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

Reply by email

Jonas Maebe
to Martin Frb
Martin Frb wrote
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

Reply by email

Alfred
to Martin Frb: Opening a can of worms here. But leaving all pitfalls aside. If one wanted to write to…

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

Reply by email

Jonas Maebe
to Alfred
Alfred wrote 11 lines
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

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

Reply by email

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 15 lines
On 14/04/2021 17:49, Alfred via fpc-devel wrote: > 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 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.

Reply by email