Skip to content

Commit 8909a24

Browse files
ldionnetru
authored andcommitted
[libc++] Encode additional ODR-affecting properties in the ABI tag (#69669)
As explained in `__config`, we have an ABI tag that we use to ensure that we don't run into ODR issues when mixing different versions of libc++ in multiple TUs. However, the reasoning behind that extends not only to different versions of libc++, but also to different configurations of the same version of libc++. In fact, we've been aware of this for a while but never really bothered to make the change because ODR issues are often thought to be benign. Well, it turns out that I just spent over an hour banging my head against an issue that boils down to our lack of encoding of some ODR properties in the ABI tag, so here's the patch we should have done a long time ago. For now, the ODR properties we encode in the ABI tag are: - library version - exceptions vs no-exceptions - hardening mode Those are all things that we support different values for on a per-TU basis and they definitely affect ODR in a meaningful way. We can add more properties later as we see fit. (cherry picked from commit bc792a2)
1 parent e9dcc15 commit 8909a24

File tree

3 files changed

+161
-15
lines changed

3 files changed

+161
-15
lines changed

libcxx/include/__config

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@
4545
# define _LIBCPP_CONCAT_IMPL(_X, _Y) _X##_Y
4646
# define _LIBCPP_CONCAT(_X, _Y) _LIBCPP_CONCAT_IMPL(_X, _Y)
4747

48-
// Valid C++ identifier that revs with every libc++ version. This can be used to
49-
// generate identifiers that must be unique for every released libc++ version.
50-
# define _LIBCPP_VERSIONED_IDENTIFIER _LIBCPP_CONCAT(v, _LIBCPP_VERSION)
51-
5248
# if __STDC_HOSTED__ == 0
5349
# define _LIBCPP_FREESTANDING
5450
# endif
@@ -754,22 +750,54 @@ typedef __char32_t char32_t;
754750
# define _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION _LIBCPP_ALWAYS_INLINE
755751
# endif
756752

753+
# if _LIBCPP_ENABLE_HARDENED_MODE
754+
# define _LIBCPP_HARDENING_SIG h
755+
# elif _LIBCPP_ENABLE_ASSERTIONS
756+
# define _LIBCPP_HARDENING_SIG s
757+
# elif _LIBCPP_ENABLE_DEBUG_MODE
758+
# define _LIBCPP_HARDENING_SIG d
759+
# else
760+
# define _LIBCPP_HARDENING_SIG u // for unchecked
761+
# endif
762+
763+
# ifdef _LIBCPP_HAS_NO_EXCEPTIONS
764+
# define _LIBCPP_EXCEPTIONS_SIG n
765+
# else
766+
# define _LIBCPP_EXCEPTIONS_SIG e
767+
# endif
768+
769+
# define _LIBCPP_ODR_SIGNATURE \
770+
_LIBCPP_CONCAT(_LIBCPP_CONCAT(_LIBCPP_HARDENING_SIG, _LIBCPP_EXCEPTIONS_SIG), _LIBCPP_VERSION)
771+
757772
// This macro marks a symbol as being hidden from libc++'s ABI. This is achieved
758773
// on two levels:
759774
// 1. The symbol is given hidden visibility, which ensures that users won't start exporting
760775
// symbols from their dynamic library by means of using the libc++ headers. This ensures
761776
// that those symbols stay private to the dynamic library in which it is defined.
762777
//
763-
// 2. The symbol is given an ABI tag that changes with each version of libc++. This ensures
764-
// that no ODR violation can arise from mixing two TUs compiled with different versions
765-
// of libc++ where we would have changed the definition of a symbol. If the symbols shared
766-
// the same name, the ODR would require that their definitions be token-by-token equivalent,
767-
// which basically prevents us from being able to make any change to any function in our
768-
// headers. Using this ABI tag ensures that the symbol name is "bumped" artificially at
769-
// each release, which lets us change the definition of these symbols at our leisure.
770-
// Note that historically, this has been achieved in various ways, including force-inlining
771-
// all functions or giving internal linkage to all functions. Both these (previous) solutions
772-
// suffer from drawbacks that lead notably to code bloat.
778+
// 2. The symbol is given an ABI tag that encodes the ODR-relevant properties of the library.
779+
// This ensures that no ODR violation can arise from mixing two TUs compiled with different
780+
// versions or configurations of libc++ (such as exceptions vs no-exceptions). Indeed, if the
781+
// program contains two definitions of a function, the ODR requires them to be token-by-token
782+
// equivalent, and the linker is allowed to pick either definition and discard the other one.
783+
//
784+
// For example, if a program contains a copy of `vector::at()` compiled with exceptions enabled
785+
// *and* a copy of `vector::at()` compiled with exceptions disabled (by means of having two TUs
786+
// compiled with different settings), the two definitions are both visible by the linker and they
787+
// have the same name, but they have a meaningfully different implementation (one throws an exception
788+
// and the other aborts the program). This violates the ODR and makes the program ill-formed, and in
789+
// practice what will happen is that the linker will pick one of the definitions at random and will
790+
// discard the other one. This can quite clearly lead to incorrect program behavior.
791+
//
792+
// A similar reasoning holds for many other properties that are ODR-affecting. Essentially any
793+
// property that causes the code of a function to differ from the code in another configuration
794+
// can be considered ODR-affecting. In practice, we don't encode all such properties in the ABI
795+
// tag, but we encode the ones that we think are most important: library version, exceptions, and
796+
// hardening mode.
797+
//
798+
// Note that historically, solving this problem has been achieved in various ways, including
799+
// force-inlining all functions or giving internal linkage to all functions. Both these previous
800+
// solutions suffer from drawbacks that lead notably to code bloat.
773801
//
774802
// Note that we use _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION to ensure that we don't depend
775803
// on _LIBCPP_HIDE_FROM_ABI methods of classes explicitly instantiated in the dynamic library.
@@ -789,7 +817,7 @@ typedef __char32_t char32_t;
789817
# ifndef _LIBCPP_NO_ABI_TAG
790818
# define _LIBCPP_HIDE_FROM_ABI \
791819
_LIBCPP_HIDDEN _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION \
792-
__attribute__((__abi_tag__(_LIBCPP_TOSTRING(_LIBCPP_VERSIONED_IDENTIFIER))))
820+
__attribute__((__abi_tag__(_LIBCPP_TOSTRING(_LIBCPP_ODR_SIGNATURE))))
793821
# else
794822
# define _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDDEN _LIBCPP_EXCLUDE_FROM_EXPLICIT_INSTANTIATION
795823
# endif
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// TODO: Investigate
10+
// XFAIL: msvc
11+
12+
// Test that we encode whether exceptions are supported in an ABI tag to avoid
13+
// ODR violations when linking TUs that have different values for it.
14+
15+
// RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU1 -fno-exceptions -o %t.tu1.o
16+
// RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU2 -fexceptions -o %t.tu2.o
17+
// RUN: %{cxx} %s %{flags} %{compile_flags} -c -DMAIN -o %t.main.o
18+
// RUN: %{cxx} %t.tu1.o %t.tu2.o %t.main.o %{flags} %{link_flags} -o %t.exe
19+
// RUN: %{exec} %t.exe
20+
21+
// -fno-exceptions
22+
#ifdef TU1
23+
# include <__config>
24+
_LIBCPP_HIDE_FROM_ABI inline int f() { return 1; }
25+
int tu1() { return f(); }
26+
#endif // TU1
27+
28+
// -fexceptions
29+
#ifdef TU2
30+
# include <__config>
31+
_LIBCPP_HIDE_FROM_ABI inline int f() { return 2; }
32+
int tu2() { return f(); }
33+
#endif // TU2
34+
35+
#ifdef MAIN
36+
# include <cassert>
37+
38+
int tu1();
39+
int tu2();
40+
41+
int main(int, char**) {
42+
assert(tu1() == 1);
43+
assert(tu2() == 2);
44+
return 0;
45+
}
46+
#endif // MAIN
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// TODO: Remove these UNSUPPORTED lines once we change how hardening is enabled to avoid
10+
// mutually exclusive modes being enabled at the same time.
11+
// UNSUPPORTED: libcpp-has-hardened-mode
12+
// UNSUPPORTED: libcpp-has-debug-mode
13+
// UNSUPPORTED: libcpp-has-assertions
14+
15+
// TODO: Investigate
16+
// XFAIL: msvc
17+
18+
// Test that we encode the hardening mode in an ABI tag to avoid ODR violations
19+
// when linking TUs that have different values for it.
20+
21+
// RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU1 -D_LIBCPP_ENABLE_HARDENED_MODE -o %t.tu1.o
22+
// RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU2 -D_LIBCPP_ENABLE_ASSERTIONS -o %t.tu2.o
23+
// RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU3 -D_LIBCPP_ENABLE_DEBUG_MODE -o %t.tu3.o
24+
// RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU4 -o %t.tu4.o
25+
// RUN: %{cxx} %s %{flags} %{compile_flags} -c -DMAIN -o %t.main.o
26+
// RUN: %{cxx} %t.tu1.o %t.tu2.o %t.tu3.o %t.tu4.o %t.main.o %{flags} %{link_flags} -o %t.exe
27+
// RUN: %{exec} %t.exe
28+
29+
// hardened mode
30+
#ifdef TU1
31+
# include <__config>
32+
_LIBCPP_HIDE_FROM_ABI inline int f() { return 1; }
33+
int tu1() { return f(); }
34+
#endif // TU1
35+
36+
// safe mode
37+
#ifdef TU2
38+
# include <__config>
39+
_LIBCPP_HIDE_FROM_ABI inline int f() { return 2; }
40+
int tu2() { return f(); }
41+
#endif // TU2
42+
43+
// debug mode
44+
#ifdef TU3
45+
# include <__config>
46+
_LIBCPP_HIDE_FROM_ABI inline int f() { return 3; }
47+
int tu3() { return f(); }
48+
#endif // TU3
49+
50+
// unchecked mode
51+
#ifdef TU4
52+
# include <__config>
53+
_LIBCPP_HIDE_FROM_ABI inline int f() { return 4; }
54+
int tu4() { return f(); }
55+
#endif // TU4
56+
57+
#ifdef MAIN
58+
# include <cassert>
59+
60+
int tu1();
61+
int tu2();
62+
int tu3();
63+
int tu4();
64+
65+
int main(int, char**) {
66+
assert(tu1() == 1);
67+
assert(tu2() == 2);
68+
assert(tu3() == 3);
69+
assert(tu4() == 4);
70+
return 0;
71+
}
72+
#endif // MAIN

0 commit comments

Comments
 (0)