Commit b0da742
Gabor Horvath
[cxx-interop] Fix miscompilation for inferred shared references
When a base class is annotated as shared reference we can occasionally
infer that the derived types also need to be shared references.
Unfortunately, we did not generate the correct code for some of those
scenarios. When the reference counted base is not at the offset zero we
need to do offset adjustments before we pass the pointer to the
reference counting functions. We did not do those offset calculations.
I looked into implementing the codegen for the offset calculation
directly in Swift but it needed significantly more work than I
anticipated. We need to invoke the frontend to get the path to the base
class and we also need to deal with virtual inheritance, alignment and
some other considerations.
This PR ends up generating a Clang shim instead and the derived to base
conversion happens in this shim. As a result, we piggy-back on Clang
making all the correct offset calculations. This patch also had to
change how certain aspects of shared references are implemented to be
compatible with this approach:
* Instead of always looking at the base classes to querry the
retain/release operations we now propagate the corresponding
annotations once per types. This also has the beneficial effects that
we traverse the inheritance hierarchy less often.
* To generate the correct diagnostics, I reuse the result of the
refcount operation query.
* We do not want these generated functions to be inherited, so added a
set to exempt them from cloning.
* Tweaked the lookup logic for retain/release a bit as these generated
clang methods are not found by lookup. We rely on looking up the
imported methods instead.
rdar://166227787
rdar://1656350021 parent cef53c6 commit b0da742
File tree
13 files changed
+271
-46
lines changed- include/swift
- AST
- ClangImporter
- lib/ClangImporter
- test/Interop/Cxx/foreign-reference
- Inputs
13 files changed
+271
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
219 | | - | |
| 219 | + | |
220 | 220 | | |
221 | 221 | | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
222 | 226 | | |
223 | 227 | | |
224 | 228 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
650 | 650 | | |
651 | 651 | | |
652 | 652 | | |
| 653 | + | |
653 | 654 | | |
654 | 655 | | |
655 | 656 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
500 | 500 | | |
501 | 501 | | |
502 | 502 | | |
503 | | - | |
| 503 | + | |
504 | 504 | | |
505 | 505 | | |
506 | 506 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6565 | 6565 | | |
6566 | 6566 | | |
6567 | 6567 | | |
| 6568 | + | |
6568 | 6569 | | |
6569 | 6570 | | |
6570 | 6571 | | |
| |||
8033 | 8034 | | |
8034 | 8035 | | |
8035 | 8036 | | |
| 8037 | + | |
| 8038 | + | |
| 8039 | + | |
| 8040 | + | |
| 8041 | + | |
| 8042 | + | |
| 8043 | + | |
| 8044 | + | |
| 8045 | + | |
| 8046 | + | |
8036 | 8047 | | |
8037 | 8048 | | |
8038 | 8049 | | |
| |||
8053 | 8064 | | |
8054 | 8065 | | |
8055 | 8066 | | |
| 8067 | + | |
| 8068 | + | |
| 8069 | + | |
| 8070 | + | |
8056 | 8071 | | |
8057 | 8072 | | |
8058 | 8073 | | |
| |||
8206 | 8221 | | |
8207 | 8222 | | |
8208 | 8223 | | |
8209 | | - | |
| 8224 | + | |
8210 | 8225 | | |
8211 | | - | |
8212 | | - | |
| 8226 | + | |
8213 | 8227 | | |
8214 | | - | |
| 8228 | + | |
| 8229 | + | |
| 8230 | + | |
| 8231 | + | |
| 8232 | + | |
8215 | 8233 | | |
8216 | 8234 | | |
8217 | 8235 | | |
| |||
8242 | 8260 | | |
8243 | 8261 | | |
8244 | 8262 | | |
8245 | | - | |
8246 | | - | |
8247 | | - | |
| 8263 | + | |
| 8264 | + | |
| 8265 | + | |
8248 | 8266 | | |
8249 | 8267 | | |
8250 | 8268 | | |
| |||
8257 | 8275 | | |
8258 | 8276 | | |
8259 | 8277 | | |
8260 | | - | |
8261 | | - | |
8262 | | - | |
8263 | | - | |
8264 | | - | |
| 8278 | + | |
| 8279 | + | |
| 8280 | + | |
| 8281 | + | |
| 8282 | + | |
8265 | 8283 | | |
8266 | 8284 | | |
8267 | 8285 | | |
| |||
8811 | 8829 | | |
8812 | 8830 | | |
8813 | 8831 | | |
8814 | | - | |
8815 | 8832 | | |
8816 | | - | |
8817 | | - | |
8818 | | - | |
| 8833 | + | |
| 8834 | + | |
| 8835 | + | |
8819 | 8836 | | |
8820 | 8837 | | |
8821 | | - | |
8822 | | - | |
8823 | | - | |
8824 | | - | |
8825 | | - | |
8826 | | - | |
8827 | 8838 | | |
8828 | 8839 | | |
8829 | 8840 | | |
| |||
8842 | 8853 | | |
8843 | 8854 | | |
8844 | 8855 | | |
8845 | | - | |
8846 | | - | |
8847 | | - | |
8848 | | - | |
| 8856 | + | |
| 8857 | + | |
8849 | 8858 | | |
8850 | 8859 | | |
8851 | 8860 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2190 | 2190 | | |
2191 | 2191 | | |
2192 | 2192 | | |
| 2193 | + | |
| 2194 | + | |
| 2195 | + | |
| 2196 | + | |
| 2197 | + | |
| 2198 | + | |
| 2199 | + | |
| 2200 | + | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
| 2205 | + | |
| 2206 | + | |
| 2207 | + | |
| 2208 | + | |
| 2209 | + | |
| 2210 | + | |
| 2211 | + | |
| 2212 | + | |
| 2213 | + | |
| 2214 | + | |
| 2215 | + | |
| 2216 | + | |
| 2217 | + | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
2193 | 2226 | | |
2194 | 2227 | | |
2195 | 2228 | | |
| |||
2786 | 2819 | | |
2787 | 2820 | | |
2788 | 2821 | | |
2789 | | - | |
| 2822 | + | |
| 2823 | + | |
| 2824 | + | |
| 2825 | + | |
| 2826 | + | |
2790 | 2827 | | |
2791 | 2828 | | |
2792 | 2829 | | |
| |||
2853 | 2890 | | |
2854 | 2891 | | |
2855 | 2892 | | |
2856 | | - | |
2857 | | - | |
| 2893 | + | |
| 2894 | + | |
| 2895 | + | |
| 2896 | + | |
2858 | 2897 | | |
2859 | 2898 | | |
2860 | 2899 | | |
| |||
2937 | 2976 | | |
2938 | 2977 | | |
2939 | 2978 | | |
2940 | | - | |
2941 | | - | |
2942 | | - | |
2943 | | - | |
2944 | | - | |
2945 | 2979 | | |
2946 | 2980 | | |
2947 | 2981 | | |
| |||
3008 | 3042 | | |
3009 | 3043 | | |
3010 | 3044 | | |
3011 | | - | |
3012 | | - | |
3013 | | - | |
3014 | | - | |
3015 | | - | |
3016 | 3045 | | |
3017 | 3046 | | |
3018 | 3047 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| |||
692 | 693 | | |
693 | 694 | | |
694 | 695 | | |
| 696 | + | |
695 | 697 | | |
696 | 698 | | |
697 | 699 | | |
| |||
714 | 716 | | |
715 | 717 | | |
716 | 718 | | |
| 719 | + | |
| 720 | + | |
717 | 721 | | |
718 | 722 | | |
719 | 723 | | |
| |||
2231 | 2235 | | |
2232 | 2236 | | |
2233 | 2237 | | |
| 2238 | + | |
| 2239 | + | |
| 2240 | + | |
| 2241 | + | |
| 2242 | + | |
2234 | 2243 | | |
2235 | 2244 | | |
2236 | 2245 | | |
| |||
0 commit comments