| Issue |
235320
|
| Summary |
[clang] SIGSEGV emitting implicit destructor when a module holds (transitively) a class defined with a qualified-id at global scope in another module interface
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
Anthony-Gaudino
|
## Clang crashes with SIGSEGV emitting an implicit destructor when a module holds (transitively) a class defined with a qualified-id at global scope in another module interface
### Version
```
clang version 23.1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
```
### Reproducer (3 files, 32 lines total)
`m.cppm` — class forward-declared in an `export namespace` block, then defined with a qualified-id at global scope:
```cpp
export module m;
import std;
export namespace m {
class Def;
} // namespace m
class m::Def {
std::shared_ptr<int> state_;
public:
Def() = default;
};
```
`sf.cppm` — a holder module; `Outer` has a user-provided constructor and holds the class indirectly through `Inner`:
```cpp
export module sf;
import m;
export namespace w {
struct Inner {
m::Def v;
};
class Outer {
public:
Outer() {}
Inner inner;
};
} // namespace w
```
`use.cpp` — a plain consumer that constructs `Outer`:
```cpp
import sf;
int main() {
w::Outer o;
}
```
### Build commands
```sh
# Any prebuilt `std` BMI works; e.g. with libstdc++ headers:
clang++ -std=c++26 -x c++-module --precompile /usr/include/c++/16/bits/std.cc -o std.pcm
clang++ -std=c++26 -fmodule-file=std=std.pcm -c m.cppm -o m.o
clang++ -std=c++26 -fmodule-file=std=std.pcm -fmodule-file=m=m.pcm -c sf.cppm -o sf.o
clang++ -std=c++26 -fmodule-file=std=std.pcm -fmodule-file=m=m.pcm -fmodule-file=sf=sf.pcm -c use.cpp -o use.o # SIGSEGV here
```
Bare `-std=c++26` suffices; no `-fno-rtti`, `-fms-extensions`, `-g` or other flags are needed. The `std` BMI may itself be built with either `--precompile` or `-c`; what matters is that `sf.cppm` is built with `-c` (see below).
### Crash
```
3. sf.cppm:6:8: Generating code for declaration 'w::Inner::~Inner'
#5 clang::Decl::castFromDeclContext(clang::DeclContext const*) const
#6 0x...1983a5f
#7 clang::CodeGen::CodeGenFunction::GetVTTParameter(clang::GlobalDecl, bool, bool)
#8 0x...18acc96
#9 clang::CodeGen::CodeGenFunction::destroyCXXObject(...)
...
#13 clang::CodeGen::CodeGenFunction::EmitDestructorBody(...)
```
Neither `Def`, `Inner` nor `Outer` has virtual bases (or virtual functions at all), so the `GetVTTParameter` frame looks like it operates on a corrupted `CXXRecordDecl` (`castFromDeclContext` segfaults). A closely related variant (class in a module partition, module-interface consumer) crashes in the reader instead: `VisitFieldDecl` -> `ASTReader::GetType` -> `ASTContext::getTagType`/`getTypeInfoImpl` while laying out the record, suggesting the `-c`-built holder BMI mis-records the field type.
### The nested equivalent compiles cleanly
Defining the class inside the namespace block (everything else identical):
```cpp
export module mn;
import std;
export namespace mn {
class Def {
std::shared_ptr<int> state_;
public:
Def() = default;
};
} // namespace mn
```
plus the same holder/consumer shape builds and links with exit 0. So the qualified-id definition form is what produces the corrupt BMI.
### `-c` vs `--precompile` observation
Building `sf.cppm` with `--precompile` instead of `-c` produces a BMI whose consumers build cleanly; rebuilding the identical `sf.cppm` with `-c` (same flags, same inputs) produces a BMI whose consumers segfault. Likewise the corruption requires the holder (`Outer`/`Inner`) to live in an imported module BMI: defining `Outer`/`Inner` directly in the consumer TU is clean, as is putting `Def` and the holder in the same module. A defaulted `Outer` constructor, a `Def` without a declared default constructor, a direct (non-`Inner`) member, or a non-`shared_ptr` member type (e.g. `int`, `std::string`, `std::vector<int>`, `std::unique_ptr<int>`, hand-rolled templates/classes, including ones with base classes or virtual destructors) are all clean; `std::shared_ptr`, `std::weak_ptr`, `std::function` and `std::optional<std::string>` members all crash.
Original context: this pattern (forward-declare in `export namespace ultra::audio { ... }`, define `class ultra::audio::Voice final { ... }` at global scope) crashed our codebase's build while compiling a consumer of a module holding `Sound`+`Voice` members (`~Typewriter` codegen, same `GetVTTParameter`/`castFromDeclContext` stack). We keep the nested form in-tree; this report is only about the compiler crash.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs