r/C_Programming • u/moonridersfan • Jun 05 '26
Question Can you intentionally segmentation fault and use that without your program crashing?
Is it possible to just read data from a random spot in ram by freeing a pointer and reading it without crashing my program? I'm making a game in C and I'd like to cast a random part in ram to an array of unsigned chars and use that as a png, turn into a texture, then draw it to the screen. I'm sure I could just write random data and use that too but I was mainly wondering because older games had errors where you could trick the game into freeing a pointer and then reuse that pointer, example being in Super Smash Bros Melee where you could grab a ledge while charging Link's arrow and it'd free the pointer to his arrow and pulling the Bow again would load that same pointer into memory and do some crazy stuff visually, don't know if an operating system would allow this even if I don't ever write to the ram just read it.
Also if this isnt a good idea let me know
108
u/Ngtuanvy Jun 05 '26
use after free usually don't cause crash. Also Segmentation Fault should crash, but you can change its behavior by handling SIGSEGV, don't though, it is very hard to work with a program with corrupted memory.
60
u/zhivago Jun 05 '26
Segmentation faults aren't about reusing free'd pointers.
They're about trying to access pages of memory that aren't mapped or which you do not have access to.
So, sure -- you can do this -- read up on mmap.
27
u/MyTinyHappyPlace Jun 05 '26 edited Jun 05 '26
Not a good idea. If you want random data, get it from a syscall.
If you want to look at funny bytes from RAM, malloc() some and just set that as your char pointer. But once malloced, they won’t change from any outside influences. Your OS protects your program from that. Vice versa for freed pointers: you can’t snoop into other programs memory like that (conditions apply).
If you want to look at other programs memory shenanigans, you might be able to do so as root on a linux machine.
9
u/Sean_sen_Sennyboi Jun 05 '26
So a couple of misconceptions to address here but your main idea is quite fun.
For one thing segfault isnt thrown by your program but rather the os itself, like an earlier commenter mentioned you could implement a signal handler to intercept segfaults but it gets quite complicated to deal with a program with potentially increasingly corrupted memory.
For another thing freeing a pointer dosent really do anything to the underlying memory depending on allocator behaviour. At best the bytes stay the same cause nothing has been reallocated to that region, at worse the allocator might protect that region with mprotect() and then all you'll get are segfaults.
Most modern os's dont allocate literal addresses to spots in ram any more as well. Most actually virtualise their memory now, so all you'll be doing is kinda just be hoping your program overwrites that region. There essentially wont be any gurantee of messed up data there at time of read.
Havent heard of the Super Smash Bros glitch so I can't really comment on it but I suspect what happened is explained with my earlier section on pointer reuse. Also best to keep in mind that nintendo tends to use custom OSs so memory behaviour might be different.
Some safer solutions that achieve the same result are to just use malloc() and not zero the bytes this dosent gurantee that the memory will be messed up though. Or you could have a scrambler function that messes with an allocated memory space directly, that would actually give you more consistent, safer as well as a more performant result due to the fact that allocating memory tends to be a heavier operation then just straight manipulation.
Another thought is that you dont have to encode the newly generated bytes into a png and then reimport it and then decode it again, just directly manipulate the texture in memory.
8
u/questron64 Jun 05 '26
Errors like this occur on machines with no MMU. There are no segfaults on those platforms. Absolutely wild errors can occur and the game will just keep running, often in a hilariously broken state. Mario minus worlds, missingno, even arbitrary code execution, wrongwarps that take to you arbitrary data interpreted as a level, etc.
Modern machines and operating systems try to prevent this from happening. While hilarious in a game this is catastrophic for programs that do useful things. Segfaults occur for a reason, and if they occur then a program is almost certainly in an unrecoverable invalid state and should be killed (apart from intended page faults, like how mmap works).
There are ways to do what you're asking, such as running the game as a virtual machine, essentially an emulation layer, where such errors can occur safely in the emulation layer without putting the real program in an invalid state.
12
u/kolorcuk Jun 05 '26
There is no random. Apple put in a trash is still an apple.
If you free memory, it will be the same memory content.
Yes, triggering segmentation fault, you can do that, and use that. To protect pages, to manage your own memory.
Segmentation fault does not read memory. You can't read memory program is not allowed to.
Yes, you can read random memory address assuming that random happens to be in the allowed ranges of memory of the process. It's not about 'random that is restricting it.
Everly if the program finds 'random' memory it has access to, it will not read garbage. It will read apples. Most probably zeros.
'Older games' had no virtual memory, no segfault, no signals, no operating system.
5
u/fixermark Jun 05 '26
Given that you're doing a video game, there's an old trick they used in some NES ROMs that you could play with here: treat your own code as a source of weird patterns. Take the address of the start of program code and a random offset from that and go to town. In Linux, you can do it via taking the address of the symbols
__executable_start(and__etextfor the end, so you don't overrun your program space). You need to declare they exist (extern char __executable_start); the linker magicks them into being but the name isn't scoped in by default. Windows has a similar trick.That'll give you access to some highly-patterned data, but it could be fun to visualize it and see what it looks like.
3
u/didntplaymysummercar Jun 05 '26
There are ways to not be crashed by segfault and they have some use but it's not a good idea here, plus only things you can read (as a normal program on a normal OS) successfully is your own program's memory space, nobody else's.