Skip to content
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

anchor vtables of exceptions, helps throwing across DSO boundaries #1941

Closed
wants to merge 1 commit into from

Conversation

dtzWill
Copy link
Member

@dtzWill dtzWill commented Mar 2, 2018

At least it does on my special platform :).

As-is the vtables will be anchored in every translation unit that
includes them, so this should reduce size a bit everywhere.

@shlevy
Copy link
Member

shlevy commented Mar 2, 2018

I think we definitely need some in-code documentation on what these are doing and what problem they solve. Maybe a detailed description in maintainers/ and a pointer there above each declaration?

Copy link
Member

@shlevy shlevy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment.

@edolstra
Copy link
Member

edolstra commented Mar 2, 2018

Hm, I wonder why this hasn't caused problems before. I remember that we had a bug at work with exceptions / RTTI across DSOs, but I can't remember if that was Linux or macOS...

@dtzWill
Copy link
Member Author

dtzWill commented Mar 2, 2018

#1678 anchored nix::Exit, FWIW. But that was due to issues I was seeing as well :).

@dtzWill
Copy link
Member Author

dtzWill commented Mar 6, 2018

clang's -Wweak-vtable helps here, FWIW.

Avoiding weak vtables makes things work more readily, situations that might not work when relying on them re:exceptions/dynamic_cast:

  • dlopen with RTLD_LOCAL
  • Using -fvisibility=hidden
  • Using gcc before 4.5? Or so.
  • Some situations with libc++ (since it doesn't do the string name comparison of type_info that makes duplicate type_info's "work")
  • Various linker flags re:exporting dynamic symbols

Anchoring vtables (no weak vtables) is part of LLVM's coding standard, FWIW, although AFAIK not for these reasons as LLVM avoids both RTTI and exceptions:

https://llvm.org/docs/CodingStandards.html#provide-a-virtual-method-anchor-for-classes-in-headers

So basically anchoring them helps code size a bit and helps avoid the need to ensure things work right, instead of relying on type_info merging we just have one and done.

I'll try to come up with how to best reference/document this in the code.

@shlevy
Copy link
Member

shlevy commented Mar 6, 2018

Plugins rely on RTLD_LOCAL, just FYI

@dtzWill
Copy link
Member Author

dtzWill commented Mar 28, 2018

I'm not sure how well this sort of detail is "documented", but here's a post that seems to describe it well enough: https://marcmutz.wordpress.com/2010/08/04/fun-with-exceptions/ .

That said much of this is implementation-specific with various compilers/toolchains having different behavior and workarounds-- even the approach here is only required and indeed AFAIK only effective due to implementation choices on various platforms and compilers. AFAIK the C++ standard simply requires that whatever the compiler/linker do that there only be one definition (the so-called "one definition rule").

I like the explanation here, hopefully it helps this make sense: https://stackoverflow.com/a/23749273

As for plugins, I'm not sure that RTLD_LOCAL + exceptions will work? Will get back to you.

@dtzWill
Copy link
Member Author

dtzWill commented Mar 29, 2018

Looks like doesn't work with libc++, which makes sense since IIRC it has stricter behavior re:comparing addresses of type_info's. Here's a quick repo I put together for demonstrating/testing this behavior; https://github.com/dtzWill/throw_from_shared .

EDIT: fixed link

@dtzWill
Copy link
Member Author

dtzWill commented Mar 29, 2018

Updated to demonstrate with and without anchor!

@dtzWill dtzWill force-pushed the fix/anchor-vtables-exceptions branch from 4a625a9 to 8154fe2 Compare March 29, 2018 01:35
@shlevy
Copy link
Member

shlevy commented Mar 29, 2018

@dtzWill That link doesn't work for me. What precisely doesn't work with libc++? We can't throw from RTLD_LOCAL DSOs at all?

@shlevy shlevy added the backlog label Apr 1, 2018
@shlevy shlevy self-assigned this Apr 1, 2018
@shlevy
Copy link
Member

shlevy commented Apr 23, 2018

@dtzWill Can you give a bit more information on how this impacts plugins, if at all?

@stale
Copy link

stale bot commented Feb 13, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Feb 13, 2021
@stale
Copy link

stale bot commented Apr 16, 2022

I closed this issue due to inactivity. → More info

@stale stale bot closed this Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants