-
Notifications
You must be signed in to change notification settings - Fork 397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Static Polymorphic Construction: A Flaw in Extensible Classes #1797
Comments
This problem isn't specific to our extensible class mechanism, is it? It's just a dangerous corner of any kind of static polymorphism in C++ (e.g. the more well known CRTP form) ? |
Correct. |
We had some discussions within IBM on possible solutions and workarounds to this pattern (and more broadly in the context of calling a virtual function of a class from within a constructor of that class as @mgaudet referenced above). @rwy0717, would you care to summarize those discussions here as a starting point so that the community has insight and input on the solution please? |
As part of the Centre of Academic studies, a research group (myself, @snadi , @rwy0717 , @xliang6 , @nbhuiyan , @mgaudet ) is working on changing the variability implementation in OMR to use dynamic polymorphism. While applying that through PRs in order to test if there will be any degradation in the runtime performance, I started virtualizing functions in the Specifically, we got Java to crash when virtualizing these 2 functions due to the reason mentioned above:
More specifically, the constructor calls We wanted to check if anyone here has a suggested solution to fix this in a hypothetical variability implementation (that we are applying) that uses dynamic polymorphism. The solution we have in mind so far is: Please let us know about any other solutions you might have in mind. |
While running UBSan across the compiler codebase, an error was pointed out to me that is a flaw in the way that we've implemented extensible classes.
The issue is the static-polymorphic equivalent to "When my base class’s constructor calls a virtual function on its this object, why doesn’t my derived class’s override of that virtual function get invoked?"
Specifically: If
self()
is invoked inside of a constructor, we have created a situation where we are calling a method on an un-constructed object.We can see exactly this happening in the compiler codebase. For example,
https://github.com/eclipse/omr/blob/8595d1c3b98b623cf696f9e1cb8a8ae1c94e0925/compiler/codegen/OMRCodeGenerator.cpp#L203
is a call to
self()
inside the construction ofOMR::CodeGenerator
. This downcasts to the concrete type; however, the downcast is detected by UBSan to be invalid at the point in time it occurs:Fortuitiously, I suspect we've yet to be bitten in a bad way by this.
I think the correct answer here might be to
self()
in constructors, allowing the natural (and safe) binding to occur.The text was updated successfully, but these errors were encountered: