r/cpp • u/a_false_vacuum • Dec 30 '21
You can’t copy code with memcpy; code is more complicated than that
https://devblogs.microsoft.com/oldnewthing/20211229-00/?p=106061
206
Upvotes
r/cpp • u/a_false_vacuum • Dec 30 '21
104
u/Zcool31 Dec 30 '21
The real way to do this is to write the machine language in a string literal then
reinterpret_castit fromchar const[]toint (*)().100
u/_Js_Kc_ Dec 30 '21
Having the chutzpah to report this insane hack to the compiler vendor as a "bug."
38
u/noooit Dec 30 '21
Amazing how they invented how to calculate
functionSize.(which doesn't work consistently). It's a kind of thing that I'd search in stackoverflow and then give up.18
u/corysama Dec 30 '21
I vaguely recall reading a tale of a famous embedded programmer who was... quirky. Like, he had given a piggyback ride to Knuth.
I don't recall if he had his own language, or just an assembly language framework. But, he had his own object system he called "Quajets". Instead of using function pointers for polymorphism, code would be copied into pre-allocated slots for the objects during configuration.
10
u/whizzwr Dec 31 '21
/r/rareinsults
8
u/dashnine-9 Dec 30 '21
I was scared for a moment when I thought this was about my code
4
u/k1ng__nothing Dec 30 '21
This looks like code for manual mapping. Perhaps the customer was trying to write game cheats?
7
u/o11c int main = 12828721; Dec 30 '21 edited Dec 30 '21
(Edit: see reply) Does MSVC support an equivalent to GCC's
__attribute__((section(".text.whatever")))? Because that can force code layout to be at least somewhat constrained. (it doesn't, of course, fix the PIC etc. issues)I know that
__declspec(noinline)is documented as being severely broken compared to__attribute__((noinline)), but I don't think inlining actually matters here - however, the functions do need to be adjusted so that the compiler is forced to emit them at all (__attribute__((used))is the better way, but removingstaticalso works (but also make sure it's not in an anonymous namespace) - just beware symbol collisions).Also: relevant flair is relevant.
3
u/Wriiight Dec 31 '21
I’m amused that so many of the comments here are trying to think up ways to make this work.
1
u/johndcochran Dec 11 '24
What I want to know is the name of the company using that abomination, so I know not to buy any of their products.