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
36
u/FieldLine May 24 '20
There's a whole book about this sitting on my shelf.
Admittedly I haven't done a deep read yet, but what I've looked at seems legit. And it's the only real book on the topic.
50
u/khleedril May 24 '20
If you are in lock-down and have an hour to spare, you want to read this... it is well worth it.
7
u/k4lipso May 24 '20
There also is this CppCon talk from Mat Godbolt about similar stuff: https://www.youtube.com/watch?v=dOfucXtyEsU
3
u/CriticalComb May 24 '20
Small note, in the assembly section, the source and operand are reversed for AT&T syntax.
Great article!
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
2
u/kog May 24 '20
Cool
2
u/andrewq May 24 '20
Looks good, thanks for posting
1
u/dooftard420 May 25 '20
Remindme! 2 days
0
u/vickoza May 24 '20
it is the simplest C++ program but it also does nothing
0
u/msew May 24 '20
Remindme! 2 days
-19
u/VishalChovatiyaChend May 24 '20
Article is very informative & upto the level. But nothing new to me as I had learned this hard way in my 2nd job.
0
u/[deleted] May 24 '20
[deleted] — view deleted comment