News for hackers. Stuff that matters.
Re:Overriding C++ virtual functions at run time by jcelerier on Sunday August 09, 2020 @05:42AM (#24098435)
> The C++ standard does not specify how virtual functions should be implemented. In practice, however, compilers generate a virtual function table and place a pointer to it as the first member of a class.

wishful thinking: https://gcc.godbolt.org/z/qWEe9r

Re:Overriding C++ virtual functions at run time by badsectoracula on Sunday August 09, 2020 @06:19AM (#24098567)
Not sure what you are trying to show, the object still has a vtable and is placed as the first member (and in your example, only) of the class, so that quote is correct.

Obviously if you enable optimizations and one of those optimizations is avoiding the virtual call when the compiler thinks it isn't necessary, then sure you wont get a virtual call everywhere.

But if your code is relying on implementation assumptions like having a vtable at the start of a class, then it should also make sure that this assumption holds by not trying to work around it (e.g. via final) and using compiler options that control that optimization (e.g. GCC has -fno-devirtualize).

It doesn't make much sense to both try and take advantage of implementation details and work against taking advantage of implementation details at the same time.

Re:Overriding C++ virtual functions at run time by MaulingMonkey on Sunday August 09, 2020 @09:42AM (#24099585)
The article talks about vtable patching in scenarios where you might not be able to recompile the original target, which limits your ability to add unusual flags like -fno-devirtualize, or remove common optimization flags like -O3.

Which doesn't make the technique completely useless, but raising this "obvious" important caveat - that it's likely to be an imperfect patch on it's own - when the article completely fails to do so, is worthwhile. I promise you there's C++ programmers out there who weren't aware of how aggressive optimizers can be.

Re:Overriding C++ virtual functions at run time by badsectoracula on Sunday August 09, 2020 @05:57PM (#24103286)
The scenario is just an example of where it could be useful, but what you describe isn't something that is unique to patching the vtable - it is something that can happen with any form of function hotpatching, e.g. a library might use its own functions and some of those might be inlined by the compiler, so if you try to hotpatch one of those functions not all uses of that function will be replaced with your own.

This is something that you should have in mind when hotpatching in general, but that doesn't make hotpatching any less useful.

Re:Overriding C++ virtual functions at run time by jcelerier on Sunday August 09, 2020 @04:33PM (#24102629)
> Not sure what you are trying to show, the object still has a vtable and is placed as the first member (and in your example, only) of the class, so that quote is correct.

I would not say that it is correct - the "object"'s actual existence is only through the interpretation that is made of it by the functions that work on your bytes. There's no struct definition in compiled code, only functions that do something with memory at a given offset. I won't go as far as to say that "objects" don't exist once your code leaves C++'s abstract machine to enter the compiled code world... but quite close.

With that said, 4 out of these 5 functions completely (and rightfully) disregard the vtable so if you're relying on that behaviour to be in place consistently to, say, fix a security issue in a given binary... you're in for some surprises.

Re:Overriding C++ virtual functions at run time by badsectoracula on Sunday August 09, 2020 @05:55PM (#24103268)
With "the object" i meant the bytes in memory that make up an object without taking optimizations into account which can ignore parts of it (since, as i wrote, those can interfere with what you are trying to do).

And yes, the compiler made 4 out of these 5 functions to disregard the vtable, but this is again something the compiler did that you have control over - if you are trying to take advantage of such implementation specific assumptions, you wont realistically use optimizations that break these assumptions nor write code that do not follow them.

The only place where this can break is if a library uses its own functions, those functions are inlined in some places and you want to hotpatch them. But that is an issue with hotpatching 3rd party code you have no control over in general regardless of language (it can happen in C too, for example), not just with vtable hotpatching.

Re:Overriding C++ virtual functions at run time by jcelerier on Monday August 10, 2020 @03:00AM (#24106393)
> this is again something the compiler did that you have control over

I've rarely if ever heard of patching vtables in cases where you have access to the code - it's always been about fixing a binary you cannot recompile with some LD_PRELOAD trick or similar

Re:Overriding C++ virtual functions at run time by leni536 on Sunday August 09, 2020 @07:01AM (#24098756)
In other words if your program relies on undefined behavior then make sure you look at your compiler's documentation and then you might be able to make it implementation defined.
Re:Overriding C++ virtual functions at run time by formerly_proven on Sunday August 09, 2020 @09:20AM (#24099438)
The C++ standard is written in such a way that a vtable as the first member of a polymorphic class instance is the obvious way to satisfy the standard's demands. I don't think there is any mainstream C++ implementation that doesn't use vtables, although some older C++ compilers used a slightly different layout.

The fact that inside a single CU an optimizing compiler can determine the targets of polymorphic dispatch statically is unrelated, because most usage scenarios (more than one CU, external modules) preclude such optimizations anyway.

vtables aren't undefined, they are implementation defined and part of the C++ ABI. As soon as you expose something through the C++ ABI the compiler has to use the ABI's definitions.

Re:Overriding C++ virtual functions at run time by qppo on Sunday August 09, 2020 @01:42PM (#24101311)
People might bring up that the only ABI that is "stable" is Itanium, while MSVC has historically broken ABI stability quite regularly until fairly recently (2017, iirc?).

That said, so much software on Windows relies on vtable layout that breaking their particular implementation in the ABI would be a massive breaking change, so it's unlikely that it will happen.

Re:Overriding C++ virtual functions at run time by gpderetta on Sunday August 09, 2020 @05:06PM (#24102903)
I'm not a Windows programmer,but IIRC vtable layout is very thightly tied to COM so I doubt that MS has broken or will break this specific bit of the ABI anytime soon.
Re:Overriding C++ virtual functions at run time by leni536 on Monday August 10, 2020 @03:49AM (#24106659)
The layout of objects, including dynamic objects, is implementation defined.

Accessing them through reinterpret_cast is however undefined. Don't expect that compilers won't screw you over this.

Re:Overriding C++ virtual functions at run time by topiolli on Sunday August 09, 2020 @06:15AM (#24098549)
Cool. Is this due to "final" and the fact that the compiler can figure out the target at compile time?
Re:Overriding C++ virtual functions at run time by MaulingMonkey on Sunday August 09, 2020 @09:33AM (#24099529)
One call site (the one that takes bar&) relies on the final to devirtualize. The other call sites merely rely on the compiler being able to determine the exact type involved at compile time to devirtualize.