Issue 233468
Summary [clang][Modules] Reduced BMI: crash in an importer while generating an implicit destructor ("Generating code for declaration 'S::~S'")
Labels
Assignees
Reporter 451891358
    ## Version

clang 23.1.3 (llvmorg-23.1.3, `0d261d1ca552`), official Windows x64 release build (no assertions).

Reproduces with `--target=x86_64-pc-windows-msvc` (the default here), `--target=x86_64-unknown-linux-gnu` and `--target=x86_64-w64-windows-gnu`, with `-std=c++20`, `-std=c++23` and `-std=c++26`, at `-O0` and `-O2`. I have not tested main.

## Reproducer

`a.cppm`:

```c++
export module a;

template <class T> struct Base {
  union { T value; };
  ~Base() { value.~T(); }
};
template <class T> struct Mid : Base<T> {};
export template <class T> struct Wrapper : Mid<T> {};

export struct S {
 Wrapper<int> member;
};
```

`b.cppm`:

```c++
export module b;
import a;

inline void use() { Wrapper<S> w; }
```

`c.cpp`:

```c++
import a;
import b;

void f() { S s; }
```

```sh
clang++ -std=c++20 -c a.cppm -fmodule-output=a.pcm -o a.o
clang++ -std=c++20 -fmodule-file=a=a.pcm -c b.cppm -fmodule-output=b.pcm -o b.o
clang++ -std=c++20 -fmodule-file=a=a.pcm -fmodule-file=b=b.pcm -c c.cpp -o c.o   # crashes
```

## Output

```
Stack dump:
0.	Program arguments: clang++ --target=x86_64-unknown-linux-gnu -std=c++20 -fmodule-file=a=a.pcm -fmodule-file=b=b.pcm -fno-crash-diagnostics -c c.cpp -o c.o
1.	<eof> parser at end of file
2.	Per-file LLVM IR generation
3.	a.cppm:10:15: Generating code for declaration 'S::~S'
Exception Code: 0xC0000005
```

(The compiler path is shortened. `-fno-crash-diagnostics` only suppresses the reproducer files; the crash is the same without it.)

The Windows release binary has no symbols, so I can only provide offsets. On the same binary, the innermost frame and the outer frames are the same for the MSVC and Itanium targets; only the frames in between differ (one frame with the MSVC target, four with the Itanium targets).

## Control experiments

Each row changes one thing in the reproducer above. All rows were checked with the MSVC and Linux targets.

| Variant | Result |
| --- | --- |
| as above | crash |
| only `b.cppm` uses a reduced BMI (`a.cppm` compiled with `-fno-modules-reduced-bmi`) | crash |
| `b.cppm` compiled with `-fno-modules-reduced-bmi` (`a.cppm` reduced or full) | no crash |
| two-phase compilation with `--precompile` (default, `-fmodules-reduced-bmi` or `-fno-modules-reduced-bmi`) | no crash |
| `use()` is not inline | no crash |
| `b.cppm` has no `use()`, or `use()` declares `S s;` instead of `Wrapper<S> w;` | no crash |
| `c.cpp` does not import `b` | no crash |
| `~Base()` has an empty body | no crash |
| `Base` has a plain member `T value;` instead of the anonymous union | no crash |
| `Wrapper` derives from `Base` directly (no `Mid`) | no crash |
| `S` has a `Mid<int>` member instead of `Wrapper<int>` | no crash |
| `c.cpp` declares `Wrapper<S> w;` instead of `S s;` | crash |

## Notes

- I first hit this in a project that uses `import std;` with the MSVC STL. There, `S` had a `std::variant` member, and an intermediate module destroyed `S` inside another `std::variant` (an inline member function that calls `clear()` on a `std::deque<std::variant<S, ...>>`); an importer of both modules then crashed while generating `S::~S`. In a reduction that still used the standard library, `std::optional` triggers it as well. The case above needs no standard library.
- This looks related to, but different from, #231589 (MS ABI only, a virtual destructor's resolved `operator delete`) and #229408 (`extern template`; that one also crashes with full BMIs). It needs no virtual functions and no `extern template`, does not depend on the ABI, and does not reproduce when the intermediate module uses a full BMI. It may be another update dropped by the lazy DeclUpdate handling in reduced BMIs (#175498), but I have not verified that.
- Workaround: compile module interfaces with `-fno-modules-reduced-bmi`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to