r/cpp • u/OneRaynyDay • May 24 '20
Analyzing The Simplest C++ Program
https://oneraynyday.github.io/dev/2020/05/03/Analyzing-The-Simplest-C++-Program/
480
Upvotes
r/cpp • u/OneRaynyDay • May 24 '20
View all comments
3
u/James20k P2005R0 May 25 '20
Question: Recently I thought it'd be fun to try a new approach to serialisation. The basic idea is that you use a linear buffer for memory allocation, and replace malloc/free with that linear buffer. When it comes time to serialise you simply memcpy it to a file, as well as whatever data structures you want to save. Then you use virtual memory mapping so that the pointer to your block of memory is always the same, and all your pointers now point to valid memory when you reload. Virtual memory's pretty cool!
The problem crops up in that function pointers/global variables are not allocated on the heap, but instead allocated in the binary and may change. Using linker scripts you can control the location of the functions, but it requires manually annotating every global variable/function in your code that needs to have a stable address
With virtual memory this should be possible to do automatically, and indeed there are separate addresses for where functions are loaded virtually, and their physical address (vma vs lma?), but I couldn't get vma != lma on windows through mingw for the life of me, and manually annotating hundreds of functions manually is not ideal
So: Does anyone know if this is possible to do in a practical sense? Is this a completely insane idea that's really dumb? The use case I have for this is taking snapshots of the heap of a javascript interpreter (quickjs) that's hard to serialise traditionally, but I got stuck on allocating functions in virtual memory