LowerTypeTests: Implement importing of type identifiers.

To import a type identifier we read the summary and create external
references to the symbols defined when exporting.

Differential Revision: https://reviews.llvm.org/D28546

llvm-svn: 292654
This commit is contained in:
Peter Collingbourne 2017-01-20 21:49:34 +00:00
parent df0a9a0897
commit f04a390099
3 changed files with 285 additions and 2 deletions

View File

@ -295,6 +295,8 @@ class LowerTypeTestsModule {
Function *WeakInitializerFn = nullptr;
void exportTypeId(StringRef TypeId, const TypeIdLowering &TIL);
TypeIdLowering importTypeId(StringRef TypeId);
void importTypeTest(CallInst *CI);
BitSetInfo
buildBitSet(Metadata *TypeId,
@ -499,10 +501,12 @@ Value *LowerTypeTestsModule::createBitSetTest(IRBuilder<> &B,
return createMaskedBitTest(B, TIL.InlineBits, BitOffset);
} else {
Constant *ByteArray = TIL.TheByteArray;
if (!LinkerSubsectionsViaSymbols && AvoidReuse) {
if (!LinkerSubsectionsViaSymbols && AvoidReuse &&
Action != SummaryAction::Import) {
// Each use of the byte array uses a different alias. This makes the
// backend less likely to reuse previously computed byte array addresses,
// improving the security of the CFI mechanism based on this pass.
// This won't work when importing because TheByteArray is external.
ByteArray = GlobalAlias::create(Int8Ty, 0, GlobalValue::PrivateLinkage,
"bits_use", ByteArray, &M);
}
@ -598,7 +602,7 @@ Value *LowerTypeTestsModule::lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
IntPtrTy));
Value *BitOffset = B.CreateOr(OffsetSHR, OffsetSHL);
Constant *BitSizeConst = ConstantExpr::getZExt(TIL.SizeM1, IntPtrTy);
Constant *BitSizeConst = ConstantExpr::getZExtOrBitCast(TIL.SizeM1, IntPtrTy);
Value *OffsetInRange = B.CreateICmpULE(BitOffset, BitSizeConst);
// If the bit set is all ones, testing against it is unnecessary.
@ -735,6 +739,78 @@ void LowerTypeTestsModule::exportTypeId(StringRef TypeId,
ConstantExpr::getIntToPtr(TIL.InlineBits, Int8PtrTy));
}
LowerTypeTestsModule::TypeIdLowering
LowerTypeTestsModule::importTypeId(StringRef TypeId) {
TypeTestResolution &TTRes = Summary->getTypeIdSummary(TypeId).TTRes;
TypeIdLowering TIL;
TIL.TheKind = TTRes.TheKind;
auto ImportGlobal = [&](StringRef Name, unsigned AbsWidth) {
unsigned PtrWidth = IntPtrTy->getBitWidth();
Constant *C =
M.getOrInsertGlobal(("__typeid_" + TypeId + "_" + Name).str(), Int8Ty);
auto *GV = dyn_cast<GlobalVariable>(C);
// We only need to set metadata if the global is newly created, in which
// case it would not have hidden visibility.
if (!GV || GV->getVisibility() == GlobalValue::HiddenVisibility)
return C;
GV->setVisibility(GlobalValue::HiddenVisibility);
auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
auto *T = IntegerType::get(M.getContext(), PtrWidth);
auto *MinC = ConstantAsMetadata::get(ConstantInt::get(T, Min));
auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(T, Max));
GV->setMetadata(LLVMContext::MD_absolute_symbol,
MDNode::get(M.getContext(), {MinC, MaxC}));
};
if (AbsWidth == PtrWidth)
SetAbsRange(~0ull, ~0ull); // Full set.
else if (AbsWidth)
SetAbsRange(0, 1ull << AbsWidth);
return C;
};
if (TIL.TheKind != TypeTestResolution::Unsat)
TIL.OffsetedGlobal = ImportGlobal("global_addr", 0);
if (TIL.TheKind == TypeTestResolution::ByteArray ||
TIL.TheKind == TypeTestResolution::Inline ||
TIL.TheKind == TypeTestResolution::AllOnes) {
TIL.AlignLog2 = ConstantExpr::getPtrToInt(ImportGlobal("align", 8), Int8Ty);
TIL.SizeM1 = ConstantExpr::getPtrToInt(
ImportGlobal("size_m1", TTRes.SizeM1BitWidth), IntPtrTy);
}
if (TIL.TheKind == TypeTestResolution::ByteArray) {
TIL.TheByteArray = ImportGlobal("byte_array", 0);
TIL.BitMask = ImportGlobal("bit_mask", 8);
}
if (TIL.TheKind == TypeTestResolution::Inline)
TIL.InlineBits = ConstantExpr::getPtrToInt(
ImportGlobal("inline_bits", 1 << TTRes.SizeM1BitWidth),
TTRes.SizeM1BitWidth <= 5 ? Int32Ty : Int64Ty);
return TIL;
}
void LowerTypeTestsModule::importTypeTest(CallInst *CI) {
auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
if (!TypeIdMDVal)
report_fatal_error("Second argument of llvm.type.test must be metadata");
auto TypeIdStr = dyn_cast<MDString>(TypeIdMDVal->getMetadata());
if (!TypeIdStr)
report_fatal_error(
"Second argument of llvm.type.test must be a metadata string");
TypeIdLowering TIL = importTypeId(TypeIdStr->getString());
Value *Lowered = lowerTypeTestCall(TypeIdStr, CI, TIL);
CI->replaceAllUsesWith(Lowered);
CI->eraseFromParent();
}
void LowerTypeTestsModule::lowerTypeTestCalls(
ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
@ -1272,6 +1348,12 @@ bool LowerTypeTestsModule::lower() {
Action != SummaryAction::Export)
return false;
if (Action == SummaryAction::Import) {
for (const Use &U : TypeTestFunc->uses())
importTypeTest(cast<CallInst>(U.getUser()));
return true;
}
// Equivalence class set containing type identifiers and the globals that
// reference them. This is used to partition the set of type identifiers in
// the module into disjoint sets.

View File

@ -0,0 +1,31 @@
---
TypeIdMap:
allones7:
TTRes:
Kind: AllOnes
SizeM1BitWidth: 7
allones32:
TTRes:
Kind: AllOnes
SizeM1BitWidth: 32
bytearray7:
TTRes:
Kind: ByteArray
SizeM1BitWidth: 7
bytearray32:
TTRes:
Kind: ByteArray
SizeM1BitWidth: 32
inline5:
TTRes:
Kind: Inline
SizeM1BitWidth: 5
inline6:
TTRes:
Kind: Inline
SizeM1BitWidth: 6
single:
TTRes:
Kind: Single
SizeM1BitWidth: 0
...

View File

@ -0,0 +1,170 @@
; RUN: opt -S -lowertypetests -lowertypetests-summary-action=import -lowertypetests-read-summary=%S/Inputs/import.yaml < %s | FileCheck %s
target datalayout = "e-p:64:64"
declare i1 @llvm.type.test(i8* %ptr, metadata %bitset) nounwind readnone
; CHECK-DAG: @__typeid_single_global_addr = external hidden global i8
; CHECK-DAG: @__typeid_inline6_global_addr = external hidden global i8
; CHECK-DAG: @__typeid_inline6_align = external hidden global i8, !absolute_symbol !0
; CHECK-DAG: @__typeid_inline6_size_m1 = external hidden global i8, !absolute_symbol !1
; CHECK-DAG: @__typeid_inline6_inline_bits = external hidden global i8, !absolute_symbol !2
; CHECK-DAG: @__typeid_inline5_global_addr = external hidden global i8
; CHECK-DAG: @__typeid_inline5_align = external hidden global i8, !absolute_symbol !0
; CHECK-DAG: @__typeid_inline5_size_m1 = external hidden global i8, !absolute_symbol !3
; CHECK-DAG: @__typeid_inline5_inline_bits = external hidden global i8, !absolute_symbol !4
; CHECK-DAG: @__typeid_bytearray32_global_addr = external hidden global i8
; CHECK-DAG: @__typeid_bytearray32_align = external hidden global i8, !absolute_symbol !0
; CHECK-DAG: @__typeid_bytearray32_size_m1 = external hidden global i8, !absolute_symbol !4
; CHECK-DAG: @__typeid_bytearray32_byte_array = external hidden global i8
; CHECK-DAG: @__typeid_bytearray32_bit_mask = external hidden global i8, !absolute_symbol !0
; CHECK-DAG: @__typeid_bytearray7_global_addr = external hidden global i8
; CHECK-DAG: @__typeid_bytearray7_align = external hidden global i8, !absolute_symbol !0
; CHECK-DAG: @__typeid_bytearray7_size_m1 = external hidden global i8, !absolute_symbol !5
; CHECK-DAG: @__typeid_bytearray7_byte_array = external hidden global i8
; CHECK-DAG: @__typeid_bytearray7_bit_mask = external hidden global i8, !absolute_symbol !0
; CHECK-DAG: @__typeid_allones32_global_addr = external hidden global i8
; CHECK-DAG: @__typeid_allones32_align = external hidden global i8, !absolute_symbol !0
; CHECK-DAG: @__typeid_allones32_size_m1 = external hidden global i8, !absolute_symbol !4
; CHECK-DAG: @__typeid_allones7_global_addr = external hidden global i8
; CHECK-DAG: @__typeid_allones7_align = external hidden global i8, !absolute_symbol !0
; CHECK-DAG: @__typeid_allones7_size_m1 = external hidden global i8, !absolute_symbol !5
; CHECK: define i1 @allones7(i8* [[p:%.*]])
define i1 @allones7(i8* %p) {
; CHECK-NEXT: [[pi:%.*]] = ptrtoint i8* [[p]] to i64
; CHECK-NEXT: [[sub:%.*]] = sub i64 [[pi]], ptrtoint (i8* @__typeid_allones7_global_addr to i64)
; CHECK-NEXT: [[lshr:%.*]] = lshr i64 [[sub]], zext (i8 ptrtoint (i8* @__typeid_allones7_align to i8) to i64)
; CHECK-NEXT: [[shl:%.*]] = shl i64 [[sub]], zext (i8 sub (i8 64, i8 ptrtoint (i8* @__typeid_allones7_align to i8)) to i64)
; CHECK-NEXT: [[or:%.*]] = or i64 [[lshr]], [[shl]]
; CHECK-NEXT: [[ule:%.*]] = icmp ule i64 [[or]], ptrtoint (i8* @__typeid_allones7_size_m1 to i64)
; CHECK-NEXT: ret i1 [[ule]]
%x = call i1 @llvm.type.test(i8* %p, metadata !"allones7")
ret i1 %x
}
; CHECK: define i1 @allones32(i8* [[p:%.*]])
define i1 @allones32(i8* %p) {
; CHECK-NEXT: [[pi:%.*]] = ptrtoint i8* [[p]] to i64
; CHECK-NEXT: [[sub:%.*]] = sub i64 [[pi]], ptrtoint (i8* @__typeid_allones32_global_addr to i64)
; CHECK-NEXT: [[lshr:%.*]] = lshr i64 [[sub]], zext (i8 ptrtoint (i8* @__typeid_allones32_align to i8) to i64)
; CHECK-NEXT: [[shl:%.*]] = shl i64 [[sub]], zext (i8 sub (i8 64, i8 ptrtoint (i8* @__typeid_allones32_align to i8)) to i64)
; CHECK-NEXT: [[or:%.*]] = or i64 [[lshr]], [[shl]]
; CHECK-NEXT: [[ule:%.*]] = icmp ule i64 [[or]], ptrtoint (i8* @__typeid_allones32_size_m1 to i64)
; CHECK-NEXT: ret i1 [[ule]]
%x = call i1 @llvm.type.test(i8* %p, metadata !"allones32")
ret i1 %x
}
; CHECK: define i1 @bytearray7(i8* [[p:%.*]])
define i1 @bytearray7(i8* %p) {
; CHECK-NEXT: [[pi:%.*]] = ptrtoint i8* [[p]] to i64
; CHECK-NEXT: [[sub:%.*]] = sub i64 [[pi]], ptrtoint (i8* @__typeid_bytearray7_global_addr to i64)
; CHECK-NEXT: [[lshr:%.*]] = lshr i64 [[sub]], zext (i8 ptrtoint (i8* @__typeid_bytearray7_align to i8) to i64)
; CHECK-NEXT: [[shl:%.*]] = shl i64 [[sub]], zext (i8 sub (i8 64, i8 ptrtoint (i8* @__typeid_bytearray7_align to i8)) to i64)
; CHECK-NEXT: [[or:%.*]] = or i64 [[lshr]], [[shl]]
; CHECK-NEXT: [[ule:%.*]] = icmp ule i64 [[or]], ptrtoint (i8* @__typeid_bytearray7_size_m1 to i64)
; CHECK-NEXT: br i1 [[ule]], label %[[t:.*]], label %[[f:.*]]
; CHECK: [[t]]:
; CHECK-NEXT: [[gep:%.*]] = getelementptr i8, i8* @__typeid_bytearray7_byte_array, i64 [[or]]
; CHECK-NEXT: [[load:%.*]] = load i8, i8* [[gep]]
; CHECK-NEXT: [[and:%.*]] = and i8 [[load]], ptrtoint (i8* @__typeid_bytearray7_bit_mask to i8)
; CHECK-NEXT: [[ne:%.*]] = icmp ne i8 [[and]], 0
; CHECK-NEXT: br label %[[f]]
; CHECK: [[f]]:
; CHECK-NEXT: [[phi:%.*]] = phi i1 [ false, %0 ], [ [[ne]], %[[t]] ]
; CHECK-NEXT: ret i1 [[phi]]
%x = call i1 @llvm.type.test(i8* %p, metadata !"bytearray7")
ret i1 %x
}
; CHECK: define i1 @bytearray32(i8* [[p:%.*]])
define i1 @bytearray32(i8* %p) {
; CHECK-NEXT: [[pi:%.*]] = ptrtoint i8* [[p]] to i64
; CHECK-NEXT: [[sub:%.*]] = sub i64 [[pi]], ptrtoint (i8* @__typeid_bytearray32_global_addr to i64)
; CHECK-NEXT: [[lshr:%.*]] = lshr i64 [[sub]], zext (i8 ptrtoint (i8* @__typeid_bytearray32_align to i8) to i64)
; CHECK-NEXT: [[shl:%.*]] = shl i64 [[sub]], zext (i8 sub (i8 64, i8 ptrtoint (i8* @__typeid_bytearray32_align to i8)) to i64)
; CHECK-NEXT: [[or:%.*]] = or i64 [[lshr]], [[shl]]
; CHECK-NEXT: [[ule:%.*]] = icmp ule i64 [[or]], ptrtoint (i8* @__typeid_bytearray32_size_m1 to i64)
; CHECK-NEXT: br i1 [[ule]], label %[[t:.*]], label %[[f:.*]]
; CHECK: [[t]]:
; CHECK-NEXT: [[gep:%.*]] = getelementptr i8, i8* @__typeid_bytearray32_byte_array, i64 [[or]]
; CHECK-NEXT: [[load:%.*]] = load i8, i8* [[gep]]
; CHECK-NEXT: [[and:%.*]] = and i8 [[load]], ptrtoint (i8* @__typeid_bytearray32_bit_mask to i8)
; CHECK-NEXT: [[ne:%.*]] = icmp ne i8 [[and]], 0
; CHECK-NEXT: br label %[[f]]
; CHECK: [[f]]:
; CHECK-NEXT: [[phi:%.*]] = phi i1 [ false, %0 ], [ [[ne]], %[[t]] ]
; CHECK-NEXT: ret i1 [[phi]]
%x = call i1 @llvm.type.test(i8* %p, metadata !"bytearray32")
ret i1 %x
}
; CHECK: define i1 @inline5(i8* [[p:%.*]])
define i1 @inline5(i8* %p) {
; CHECK-NEXT: [[pi:%.*]] = ptrtoint i8* [[p]] to i64
; CHECK-NEXT: [[sub:%.*]] = sub i64 [[pi]], ptrtoint (i8* @__typeid_inline5_global_addr to i64)
; CHECK-NEXT: [[lshr:%.*]] = lshr i64 [[sub]], zext (i8 ptrtoint (i8* @__typeid_inline5_align to i8) to i64)
; CHECK-NEXT: [[shl:%.*]] = shl i64 [[sub]], zext (i8 sub (i8 64, i8 ptrtoint (i8* @__typeid_inline5_align to i8)) to i64)
; CHECK-NEXT: [[or:%.*]] = or i64 [[lshr]], [[shl]]
; CHECK-NEXT: [[ule:%.*]] = icmp ule i64 [[or]], ptrtoint (i8* @__typeid_inline5_size_m1 to i64)
; CHECK-NEXT: br i1 [[ule]], label %[[t:.*]], label %[[f:.*]]
; CHECK: [[t]]:
; CHECK-NEXT: [[trunc:%.*]] = trunc i64 [[or]] to i32
; CHECK-NEXT: [[and:%.*]] = and i32 [[trunc]], 31
; CHECK-NEXT: [[shl2:%.*]] = shl i32 1, [[and]]
; CHECK-NEXT: [[and2:%.*]] = and i32 ptrtoint (i8* @__typeid_inline5_inline_bits to i32), [[shl2]]
; CHECK-NEXT: [[ne:%.*]] = icmp ne i32 [[and2]], 0
; CHECK-NEXT: br label %[[f]]
; CHECK: [[f]]:
; CHECK-NEXT: [[phi:%.*]] = phi i1 [ false, %0 ], [ [[ne]], %[[t]] ]
; CHECK-NEXT: ret i1 [[phi]]
%x = call i1 @llvm.type.test(i8* %p, metadata !"inline5")
ret i1 %x
}
; CHECK: define i1 @inline6(i8* [[p:%.*]])
define i1 @inline6(i8* %p) {
; CHECK-NEXT: [[pi:%.*]] = ptrtoint i8* [[p]] to i64
; CHECK-NEXT: [[sub:%.*]] = sub i64 [[pi]], ptrtoint (i8* @__typeid_inline6_global_addr to i64)
; CHECK-NEXT: [[lshr:%.*]] = lshr i64 [[sub]], zext (i8 ptrtoint (i8* @__typeid_inline6_align to i8) to i64)
; CHECK-NEXT: [[shl:%.*]] = shl i64 [[sub]], zext (i8 sub (i8 64, i8 ptrtoint (i8* @__typeid_inline6_align to i8)) to i64)
; CHECK-NEXT: [[or:%.*]] = or i64 [[lshr]], [[shl]]
; CHECK-NEXT: [[ule:%.*]] = icmp ule i64 [[or]], ptrtoint (i8* @__typeid_inline6_size_m1 to i64)
; CHECK-NEXT: br i1 [[ule]], label %[[t:.*]], label %[[f:.*]]
; CHECK: [[t]]:
; CHECK-NEXT: [[and:%.*]] = and i64 [[or]], 63
; CHECK-NEXT: [[shl2:%.*]] = shl i64 1, [[and]]
; CHECK-NEXT: [[and2:%.*]] = and i64 ptrtoint (i8* @__typeid_inline6_inline_bits to i64), [[shl2]]
; CHECK-NEXT: [[ne:%.*]] = icmp ne i64 [[and2]], 0
; CHECK-NEXT: br label %[[f]]
; CHECK: [[f]]:
; CHECK-NEXT: [[phi:%.*]] = phi i1 [ false, %0 ], [ [[ne]], %[[t]] ]
; CHECK-NEXT: ret i1 [[phi]]
%x = call i1 @llvm.type.test(i8* %p, metadata !"inline6")
ret i1 %x
}
; CHECK: define i1 @single(i8* [[p:%.*]])
define i1 @single(i8* %p) {
; CHECK-NEXT: [[pi:%.*]] = ptrtoint i8* [[p]] to i64
; CHECK-NEXT: [[eq:%.*]] = icmp eq i64 [[pi]], ptrtoint (i8* @__typeid_single_global_addr to i64)
; CHECK-NEXT: ret i1 [[eq]]
%x = call i1 @llvm.type.test(i8* %p, metadata !"single")
ret i1 %x
}
; CHECK: !0 = !{i64 0, i64 256}
; CHECK: !1 = !{i64 0, i64 64}
; CHECK: !2 = !{i64 -1, i64 -1}
; CHECK: !3 = !{i64 0, i64 32}
; CHECK: !4 = !{i64 0, i64 4294967296}
; CHECK: !5 = !{i64 0, i64 128}