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/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.
12
13
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
2
u/OneRaynyDay May 25 '20
Ooh this man’s name is thrown around a lot in the finance industry. I use his compiler explorer tool for a lot of sandboxing. I just watched it and he talks a lot about the static initialization process which is great but he glossed over the g++ details :( but it’s an hour long talk so for what he covered I really enjoyed.
3
u/CriticalComb May 24 '20
Small note, in the assembly section, the source and operand are reversed for AT&T syntax.
Great article!
1
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/mttd May 25 '20
Just to throw in a related idea (or a set of ideas)--you may want to look into checkpointing and perhaps recoverable virtual memory.
Some references:
Checkpointing and Its Applications (1995): http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.89.9840&rep=rep1&type=pdf
Lightweight Recoverable Virtual Memory (1994): https://people.eecs.berkeley.edu/~brewer/cs262/lrvm.pdf, http://www.coda.cs.cmu.edu/doc/html/rvm_manual-1.html
Continuous checkpointing: joining the checkpointing with virtual memory paging (1997): https://ir.nctu.edu.tw/bitstream/11536/338/1/A1997XW08300007.pdf
2
2
1
0
u/vickoza May 24 '20
it is the simplest C++ program but it also does nothing
20
u/ShakaUVM i+++ ++i+i[arr] May 24 '20
it is the simplest C++ program but it also does nothing
Not true. It does nothing successfully.
It's like the UNIX true command.
5
u/chugga_fan May 24 '20
Said command has like, a thousand or two lines of code, surprisingly.
1
u/conflicted_panda May 25 '20
Are you counting includes? Without them GNU true is about 70, as far as I see.
And true on old school unixes was/is 0 bytes, I hear.
2
u/chugga_fan May 25 '20
Like 1/3rd of the program is in other files, even still, 78 lines just to return EXIT_SUCCESS is a lot.
2
u/conflicted_panda May 25 '20
It also handles --help/--version (along with localization, apparently). And I honestly don't think that trading off 78 lines of code for, err, uniformity of interface of your utilities is that absurd.
1
1
2
0
u/msew May 24 '20
Remindme! 2 days
1
u/RevRagnarok May 24 '20
I prefer to email my work address now that gmail finally has queued email. "Tuesday morning at 8" ;)
1
u/RemindMeBot May 24 '20 edited May 25 '20
I will be messaging you in 1 day on 2020-05-26 19:37:02 UTC to remind you of this link
2 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback
-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.
9
0
May 24 '20
[deleted]
20
u/OneRaynyDay May 24 '20
Good question! You actually don't need to allocate registers, but that means you'll be writing your variable's values directly into memory. This is really slow and compiler writers try their best to not let things "spill out" of registers and into memory.
So basically, ya but u want program go fast
2
u/slaymaker1907 Jun 21 '20
It’s not the slowest thing as long as you still hit the L1 cache. x86 has so few general purpose registers that it is hard to avoid spilling into memory.
2
u/paulstelian97 Jun 23 '20
16 registers, including stack pointer and stack frame pointer but I think excluding the instruction pointer?
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.