Debian Codegraph @

clang/test/Modules/specializations-lazy-load-parentmap-crash.cpp

raw
1 // RUN: rm -rf %t
2 // RUN: mkdir -p %t
3 // RUN: split-file --leading-lines %s %t
4 //
5 // Prepare the BMIs.
6 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-module-interface -o %t/mod_a-part1.pcm %t/mod_a-part1.cppm
7 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-module-interface -o %t/mod_a-part2.pcm %t/mod_a-part2.cppm
8 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-module-interface -o %t/mod_a.pcm %t/mod_a.cppm -fmodule-file=mod_a:part2=%t/mod_a-part2.pcm -fmodule-file=mod_a:part1=%t/mod_a-part1.pcm
9 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-module-interface -o %t/mod_b.pcm %t/mod_b.cppm -fmodule-file=mod_a:part2=%t/mod_a-part2.pcm -fmodule-file=mod_a=%t/mod_a.pcm -fmodule-file=mod_a:part1=%t/mod_a-part1.pcm
10
11 // Trigger the construction of the parent map (which is necessary to trigger the bug this regression test is for) using a sanitized build:
12 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fsanitize=unsigned-integer-overflow -fsanitize-undefined-ignore-overflow-pattern=all -emit-llvm -o %t/ignored %t/test-sanitized-build.cpp -fmodule-file=mod_a:part2=%t/mod_a-part2.pcm -fmodule-file=mod_a=%t/mod_a.pcm -fmodule-file=mod_a:part1=%t/mod_a-part1.pcm -fmodule-file=mod_b=%t/mod_b.pcm
13
14 //--- mod_a-part1.cppm
15 module;
16 namespace mod_a {
17 template <int> struct Important;
18 }
19
20 namespace mod_a {
21 Important<0>& instantiate1();
22 } // namespace mod_a
23 export module mod_a:part1;
24
25 export namespace mod_a {
26 using ::mod_a::instantiate1;
27 }
28
29 //--- mod_a-part2.cppm
30 module;
31 namespace mod_a {
32 template <int> struct Important;
33 }
34
35 namespace mod_a {
36 template <int N> Important<N>& instantiate2();
37 namespace part2InternalInstantiations {
38 // During the construction of the parent map, we iterate over ClassTemplateDecl::specializations() for 'Important'.
39 // After GH119333, the following instantiations get loaded between the call to spec_begin() and spec_end().
40 // This used to invalidate the begin iterator returned by spec_begin() by the time the end iterator is returned.
41 // This is a regression test for that.
42 Important<1> fn1();
43 Important<2> fn2();
44 Important<3> fn3();
45 Important<4> fn4();
46 Important<5> fn5();
47 Important<6> fn6();
48 Important<7> fn7();
49 Important<8> fn8();
50 Important<9> fn9();
51 Important<10> fn10();
52 Important<11> fn11();
53 }
54 } // namespace mod_a
55 export module mod_a:part2;
56
57 export namespace mod_a {
58 using ::mod_a::instantiate2;
59 }
60
61 //--- mod_a.cppm
62 export module mod_a;
63 export import :part1;
64 export import :part2;
65
66 //--- mod_b.cppm
67 export module mod_b;
68 import mod_a;
69
70 void a() {
71 mod_a::instantiate1();
72 mod_a::instantiate2<42>();
73 }
74
75 //--- test-sanitized-build.cpp
76 import mod_b;
77
78 extern void some();
79 void triggerParentMapContextCreationThroughSanitizedBuild(unsigned i) {
80 // This code currently causes UBSan to create the ParentMapContext.
81 // UBSan currently excludes the pattern below to avoid noise, and it relies on ParentMapContext to detect it.
82 while (i--)
83 some();
84 }