1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
//===--- TypeLayoutVerifier.cpp -------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines a generator that produces code to verify that IRGen's
// static assumptions about data layout for a Swift type correspond to the
// runtime's understanding of data layout.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsIRGen.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/Types.h"
#include "swift/SIL/SILModule.h"
#include "EnumPayload.h"
#include "IRGenDebugInfo.h"
#include "IRGenFunction.h"
#include "IRGenModule.h"
#include "GenOpaque.h"
#include "GenType.h"
#include "FixedTypeInfo.h"
using namespace swift;
using namespace irgen;
IRGenTypeVerifierFunction::IRGenTypeVerifierFunction(IRGenModule &IGM,
llvm::Function *f)
: IRGenFunction(IGM, f),
VerifierFn(IGM.getVerifyTypeLayoutAttributeFunctionPointer()) {
// Verifier functions are always artificial.
if (IGM.DebugInfo)
IGM.DebugInfo->emitArtificialFunction(*this, f);
}
void
IRGenTypeVerifierFunction::emit(ArrayRef<CanType> formalTypes) {
auto getSizeConstant = [&](Size sz) -> llvm::Constant * {
return llvm::ConstantInt::get(IGM.SizeTy, sz.getValue());
};
auto getAlignmentMaskConstant = [&](Alignment a) -> llvm::Constant * {
return llvm::ConstantInt::get(IGM.SizeTy, a.getValue() - 1);
};
auto getBoolConstant = [&](bool b) -> llvm::Constant * {
return llvm::ConstantInt::get(IGM.Int1Ty, b);
};
SmallString<20> numberBuf;
for (auto formalType : formalTypes) {
// Runtime type metadata always represents the maximal abstraction level of
// the type.
auto maxAbstraction = AbstractionPattern::getOpaque();
auto layoutType = IGM.getLoweredType(maxAbstraction, formalType);
auto &ti = getTypeInfo(layoutType);
auto metadata = emitTypeMetadataRef(formalType);
// Check type metrics for fixed-layout types.
// If there's no fixed type info, we rely on the runtime for type metrics,
// so there's no compile-time values to validate against.
if (auto *fixedTI = dyn_cast<FixedTypeInfo>(&ti)) {
// Check that the fixed layout matches the runtime layout.
verifyValues(metadata,
emitLoadOfSize(*this, layoutType),
getSizeConstant(fixedTI->getFixedSize()),
"size");
verifyValues(metadata,
emitLoadOfAlignmentMask(*this, layoutType),
getAlignmentMaskConstant(fixedTI->getFixedAlignment()),
"alignment mask");
verifyValues(metadata,
emitLoadOfStride(*this, layoutType),
getSizeConstant(fixedTI->getFixedStride()),
"stride");
verifyValues(metadata,
emitLoadOfIsInline(*this, layoutType),
getBoolConstant(fixedTI->getFixedPacking(IGM)
== FixedPacking::OffsetZero),
"is-inline bit");
verifyValues(metadata,
emitLoadOfIsTriviallyDestroyable(*this, layoutType),
getBoolConstant(fixedTI->isTriviallyDestroyable(ResilienceExpansion::Maximal)),
"is-trivially-destructible bit");
verifyValues(metadata,
emitLoadOfIsBitwiseTakable(*this, layoutType),
getBoolConstant(fixedTI->isBitwiseTakable(ResilienceExpansion::Maximal)),
"is-bitwise-takable bit");
unsigned xiCount = fixedTI->getFixedExtraInhabitantCount(IGM);
verifyValues(metadata,
emitLoadOfExtraInhabitantCount(*this, layoutType),
IGM.getInt32(xiCount),
"extra inhabitant count");
// Check extra inhabitants.
if (xiCount > 0) {
// Verify that the extra inhabitant representations are consistent.
// TODO: Update for EnumPayload implementation changes.
auto xiBuf = createAlloca(fixedTI->getStorageType(),
fixedTI->getFixedAlignment(),
"extra-inhabitant");
auto fixedXIBuf = createAlloca(fixedTI->getStorageType(),
fixedTI->getFixedAlignment(),
"extra-inhabitant");
auto xiOpaque = Builder.CreateElementBitCast(xiBuf, IGM.OpaqueTy);
auto fixedXIOpaque =
Builder.CreateElementBitCast(fixedXIBuf, IGM.OpaqueTy);
auto xiMask = fixedTI->getFixedExtraInhabitantMask(IGM);
auto xiSchema = EnumPayloadSchema(xiMask.getBitWidth());
auto maxXiCount = std::min(xiCount, 256u);
auto numCases = llvm::ConstantInt::get(IGM.Int32Ty, maxXiCount);
// TODO: Randomize the set of extra inhabitants we check.
unsigned bits = fixedTI->getFixedSize().getValueInBits();
for (unsigned i = 0, e = maxXiCount; i < e; ++i) {
// Initialize the buffer with junk, to help ensure we're insensitive to
// insignificant bits.
// TODO: Randomize the filler.
Builder.CreateMemSet(xiBuf.getAddress(),
llvm::ConstantInt::get(IGM.Int8Ty, 0x5A),
fixedTI->getFixedSize().getValue(),
llvm::MaybeAlign(fixedTI->getFixedAlignment().getValue()));
// Ask the runtime to store an extra inhabitant.
auto tag = llvm::ConstantInt::get(IGM.Int32Ty, i+1);
emitStoreEnumTagSinglePayloadCall(*this, layoutType, tag,
numCases, xiOpaque);
// Compare the stored extra inhabitant against the fixed extra
// inhabitant pattern.
auto fixedXIValue
= fixedTI->getFixedExtraInhabitantValue(IGM, bits, i);
auto fixedXIPayload =
EnumPayload::fromBitPattern(IGM, fixedXIValue,
xiSchema);
fixedXIPayload.store(*this, fixedXIBuf);
auto runtimeXIPayload = EnumPayload::load(*this, xiBuf, xiSchema);
runtimeXIPayload.emitApplyAndMask(*this, xiMask);
runtimeXIPayload.store(*this, xiBuf);
numberBuf.clear();
{
llvm::raw_svector_ostream os(numberBuf);
os << i;
}
verifyBuffers(metadata, xiBuf, fixedXIBuf, fixedTI->getFixedSize(),
llvm::Twine("stored extra inhabitant ") + numberBuf.str());
// Now ask the runtime to identify the fixed extra inhabitant value.
// Mask in junk to make sure the runtime correctly ignores it.
// TODO: Randomize the filler.
auto xiFill = ~APInt(fixedXIValue.getBitWidth(), 0);
xiFill &= ~xiMask;
fixedXIValue |= xiFill;
auto maskedXIPayload = EnumPayload::fromBitPattern(IGM,
fixedXIValue, xiSchema);
maskedXIPayload.store(*this, fixedXIBuf);
auto runtimeTag =
emitGetEnumTagSinglePayloadCall(*this, layoutType, numCases,
fixedXIOpaque);
verifyValues(metadata,
runtimeTag, tag,
llvm::Twine("extra inhabitant tag calculation ")
+ numberBuf.str());
}
}
}
ti.verify(*this, metadata, layoutType);
}
Builder.CreateRetVoid();
}
void
IRGenTypeVerifierFunction::verifyValues(llvm::Value *typeMetadata,
llvm::Value *runtimeVal,
llvm::Value *staticVal,
const llvm::Twine &description) {
assert(runtimeVal->getType() == staticVal->getType());
// Get or create buffers for the arguments.
VerifierArgumentBuffers bufs;
auto foundBufs = VerifierArgBufs.find(runtimeVal->getType());
if (foundBufs != VerifierArgBufs.end()) {
bufs = foundBufs->second;
} else {
Address runtimeBuf = createAlloca(runtimeVal->getType(),
IGM.getPointerAlignment(),
"runtime");
Address staticBuf = createAlloca(staticVal->getType(),
IGM.getPointerAlignment(),
"static");
bufs = {runtimeBuf, staticBuf};
VerifierArgBufs[runtimeVal->getType()] = bufs;
}
Builder.CreateStore(runtimeVal, bufs.runtimeBuf);
Builder.CreateStore(staticVal, bufs.staticBuf);
auto runtimePtr = Builder.CreateBitCast(bufs.runtimeBuf.getAddress(),
IGM.Int8PtrTy);
auto staticPtr = Builder.CreateBitCast(bufs.staticBuf.getAddress(),
IGM.Int8PtrTy);
auto count = llvm::ConstantInt::get(IGM.SizeTy,
IGM.DataLayout.getTypeStoreSize(runtimeVal->getType()));
auto msg
= IGM.getAddrOfGlobalString(description.str());
Builder.CreateCall(
VerifierFn, {typeMetadata, runtimePtr, staticPtr, count, msg});
}
void
IRGenTypeVerifierFunction::verifyBuffers(llvm::Value *typeMetadata,
Address runtimeBuf,
Address staticBuf,
Size size,
const llvm::Twine &description) {
auto runtimePtr = Builder.CreateBitCast(runtimeBuf.getAddress(),
IGM.Int8PtrTy);
auto staticPtr = Builder.CreateBitCast(staticBuf.getAddress(),
IGM.Int8PtrTy);
auto count = llvm::ConstantInt::get(IGM.SizeTy,
size.getValue());
auto msg
= IGM.getAddrOfGlobalString(description.str());
Builder.CreateCall(
VerifierFn, {typeMetadata, runtimePtr, staticPtr, count, msg});
}
void IRGenModule::emitTypeVerifier() {
// Look up the types to verify.
SmallVector<CanType, 4> TypesToVerify;
for (auto name : IRGen.Opts.VerifyTypeLayoutNames) {
// Look up the name in the module.
SmallVector<ValueDecl*, 1> lookup;
swift::ModuleDecl *M = getSwiftModule();
M->lookupMember(lookup, M, DeclName(Context.getIdentifier(name)),
Identifier());
if (lookup.empty()) {
Context.Diags.diagnose(SourceLoc(), diag::type_to_verify_not_found,
name);
continue;
}
TypeDecl *typeDecl = nullptr;
for (auto decl : lookup) {
if (auto td = dyn_cast<TypeDecl>(decl)) {
if (typeDecl) {
Context.Diags.diagnose(SourceLoc(), diag::type_to_verify_ambiguous,
name);
goto next;
}
typeDecl = td;
break;
}
}
if (!typeDecl) {
Context.Diags.diagnose(SourceLoc(), diag::type_to_verify_not_found, name);
continue;
}
{
auto type = typeDecl->getDeclaredInterfaceType();
if (type->hasTypeParameter()) {
Context.Diags.diagnose(SourceLoc(), diag::type_to_verify_dependent,
name);
continue;
}
TypesToVerify.push_back(type->getCanonicalType());
}
next:;
}
if (TypesToVerify.empty())
return;
// Find the entry point.
SILFunction *EntryPoint = getSILModule().lookUpFunction(
getSILModule().getASTContext().getEntryPointFunctionName());
if (!EntryPoint)
return;
llvm::Function *EntryFunction = Module.getFunction(EntryPoint->getName());
if (!EntryFunction)
return;
// Create a new function to contain our logic.
auto fnTy = llvm::FunctionType::get(VoidTy, /*varArg*/ false);
auto VerifierFunction = llvm::Function::Create(fnTy,
llvm::GlobalValue::PrivateLinkage,
"type_verifier",
getModule());
VerifierFunction->setAttributes(constructInitialAttributes());
// Insert a call into the entry function.
{
llvm::BasicBlock *EntryBB = &EntryFunction->getEntryBlock();
llvm::BasicBlock::iterator IP = EntryBB->getFirstInsertionPt();
IRBuilder Builder(getLLVMContext(), DebugInfo != nullptr);
Builder.llvm::IRBuilderBase::SetInsertPoint(EntryBB, IP);
if (DebugInfo)
DebugInfo->setEntryPointLoc(Builder);
Builder.CreateCall(fnTy, VerifierFunction, {});
}
IRGenTypeVerifierFunction VerifierIGF(*this, VerifierFunction);
VerifierIGF.emit(TypesToVerify);
}
|