Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix colliding WinEH TypeDescriptors for exceptions with the same TypeInfo_Class name #3614

Merged
merged 2 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gen/irstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ struct IRState {

// MS C++ compatible type descriptors
llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap;
llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> TypeDescriptorMap;
llvm::DenseMap<ClassDeclaration *, llvm::GlobalVariable *> TypeDescriptorMap;

// Target for dcompute. If not nullptr, it owns this.
DComputeTarget *dcomputetarget = nullptr;
Expand Down
4 changes: 1 addition & 3 deletions gen/mangling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ std::string getIRMangledVarName(std::string baseMangle, LINK link) {
return gABI->mangleVariableForLLVM(std::move(baseMangle), link);
}

namespace {
std::string getIRMangledAggregateName(AggregateDeclaration *ad,
const char *suffix) {
const char *suffix) {
std::string ret = "_D";

OutBuffer mangleBuf;
Expand All @@ -154,7 +153,6 @@ std::string getIRMangledAggregateName(AggregateDeclaration *ad,

return getIRMangledVarName(std::move(ret), LINKd);
}
}

std::string getIRMangledInitSymbolName(AggregateDeclaration *aggrdecl) {
return getIRMangledAggregateName(aggrdecl, "6__initZ");
Expand Down
3 changes: 3 additions & 0 deletions gen/mangling.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ std::string getIRMangledName(VarDeclaration *vd);
std::string getIRMangledFuncName(std::string baseMangle, LINK link);
std::string getIRMangledVarName(std::string baseMangle, LINK link);

std::string getIRMangledAggregateName(AggregateDeclaration *aggrdecl,
const char *suffix = nullptr);
std::string getIRMangledInitSymbolName(AggregateDeclaration *aggrdecl);
std::string getIRMangledVTableSymbolName(AggregateDeclaration *aggrdecl);
std::string getIRMangledClassInfoSymbolName(AggregateDeclaration *aggrdecl);
std::string getIRMangledInterfaceInfosSymbolName(ClassDeclaration *cd);

std::string getIRMangledModuleInfoSymbolName(Module *module);
std::string getIRMangledModuleRefSymbolName(const char *moduleMangle);
14 changes: 8 additions & 6 deletions gen/ms-cxx-helper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//===-- ms-cxx-helper.cpp -------------------------------------------------===//
//
// LDC the LLVM D compiler
// LDC the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
Expand All @@ -13,6 +13,7 @@
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/mangling.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/CFG.h"
Expand Down Expand Up @@ -162,15 +163,16 @@ llvm::GlobalVariable *getTypeDescriptor(IRState &irs, ClassDeclaration *cd) {
/*isConstant=*/true);
}

auto classInfoPtr = getIrAggr(cd, true)->getClassInfoSymbol();
llvm::GlobalVariable *&Var = irs.TypeDescriptorMap[classInfoPtr];
llvm::GlobalVariable *&Var = irs.TypeDescriptorMap[cd];
if (Var)
return Var;

auto classInfoPtr = getIrAggr(cd, true)->getClassInfoSymbol();

// first character skipped in debugger output, so we add 'D' as prefix
std::string TypeNameString = "D";
TypeNameString.append(cd->toPrettyChars());
std::string TypeDescName = TypeNameString + "@TypeDescriptor";
const auto TypeNameString = (llvm::Twine("D") + cd->toPrettyChars()).str();

const auto TypeDescName = getIRMangledAggregateName(cd, "@TypeDescriptor");

// Declare and initialize the TypeDescriptor.
llvm::Constant *Fields[] = {
Expand Down
7 changes: 1 addition & 6 deletions gen/trycatchfinally.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "dmd/errors.h"
#include "dmd/expression.h"
#include "dmd/mangle.h"
#include "dmd/statement.h"
#include "dmd/target.h"
#include "gen/classes.h"
Expand Down Expand Up @@ -165,12 +164,8 @@ void TryCatchScope::emitCatchBodies(IRState &irs, llvm::Value *ehPtrSlot) {
// Wrap std::type_info pointers inside a __cpp_type_info_ptr class
// instance so that the personality routine may differentiate C++ catch
// clauses from D ones.
OutBuffer wrapperMangleBuf;
wrapperMangleBuf.writestring("_D");
mangleToBuffer(p.cd, &wrapperMangleBuf);
wrapperMangleBuf.printf("%d%s", 18, "_cpp_type_info_ptr");
const auto wrapperMangle =
getIRMangledVarName(wrapperMangleBuf.peekChars(), LINKd);
getIRMangledAggregateName(p.cd, "18_cpp_type_info_ptr");

ci = irs.module.getGlobalVariable(wrapperMangle);
if (!ci) {
Expand Down
28 changes: 28 additions & 0 deletions tests/compilable/gh3501.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Tests EH with two exception types with the same `TypeInfo_Class.name`
// (due to template args not being fully qualified).

// RUN: %ldc -c %s

void foo(T)()
{
// name: gh3501.foo!(S).foo.MyException
static class MyException : Exception
{
this() { super(null); }
}

try { throw new MyException(); }
catch (MyException) {}
}

void bar1()
{
struct S {}
foo!S();
}

void bar2()
{
struct S {}
foo!S();
}