VirtualProtect usage

monday

Expert
Joined
Jun 23, 2014
Messages
1,126
Reaction score
151
Hi, do you know some specific practical examples where reading/writing to some memory region (storing some value like ammo/HP/armor) in GTA-SA / SAMP would result in a crash if the memory protection wouldn't be previously set to "PAGE_EXECUTE_READWRITE" ?

Also, let's assume that there's some mod that is overwritting some memory (e.g. ammo/HP/armor), not using "VirtualProtect", and seemingly working well. Is there a risk, that in some circumstances the game itself will suddenly change the protection of that memory region (to which our mod writes to), resulting in a crash?
Or if the mod works properly once (without using "VirtualProtect") then it will never crash due to lack of "VirtualProtect" in future?
 
Joined
Feb 18, 2005
Messages
2,963
Reaction score
267
Hi, do you know some specific practical examples where reading/writing to some memory region (storing some value like ammo/HP/armor) in GTA-SA / SAMP would result in a crash if the memory protection wouldn't be previously set to "PAGE_EXECUTE_READWRITE" ?

Read-only segments, like .text(contains executable instructions) or .rdata(contains static constants). Since these are read-only by default, modifying code inside these segments(hooking, byte patching, changing value of constants) without write access for the region would result in an access violation error.

Also, let's assume that there's some mod that is overwritting some memory (e.g. ammo/HP/armor), not using "VirtualProtect", and seemingly working well. Is there a risk, that in some circumstances the game itself will suddenly change the protection of that memory region (to which our mod writes to), resulting in a crash?

The game(GTA) doesn't and won't use VirtualProtect, other mods like sa-mp which do a lot of hooking and patching, would use it plenty.
Basically, if the game writes to the same data you want to write too, then you can, without any need for a VirtualProtect call.
 
Top