OLD | NEW |
(Empty) | |
| 1 //===- PNaClABIVerifyFunctions.cpp - Verify PNaCl ABI rules ---------------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // Verify function-level PNaCl ABI requirements. |
| 11 // |
| 12 // |
| 13 //===----------------------------------------------------------------------===// |
| 14 |
| 15 #include "llvm/Analysis/NaCl/PNaClABIVerifyFunctions.h" |
| 16 #include "llvm/ADT/Twine.h" |
| 17 #include "llvm/Analysis/NaCl.h" |
| 18 #include "llvm/Analysis/NaCl/PNaClABITypeChecker.h" |
| 19 #include "llvm/IR/Function.h" |
| 20 #include "llvm/IR/Instructions.h" |
| 21 #include "llvm/IR/IntrinsicInst.h" |
| 22 #include "llvm/IR/LLVMContext.h" |
| 23 #include "llvm/IR/Metadata.h" |
| 24 #include "llvm/IR/Operator.h" |
| 25 #include "llvm/Support/raw_ostream.h" |
| 26 |
| 27 using namespace llvm; |
| 28 |
| 29 // There's no built-in way to get the name of an MDNode, so use a |
| 30 // string ostream to print it. |
| 31 static std::string getMDNodeString(unsigned Kind, |
| 32 const SmallVectorImpl<StringRef> &MDNames) { |
| 33 std::string MDName; |
| 34 raw_string_ostream N(MDName); |
| 35 if (Kind < MDNames.size()) { |
| 36 N << "!" << MDNames[Kind]; |
| 37 } else { |
| 38 N << "!<unknown kind #" << Kind << ">"; |
| 39 } |
| 40 return N.str(); |
| 41 } |
| 42 |
| 43 PNaClABIVerifyFunctions::~PNaClABIVerifyFunctions() { |
| 44 if (ReporterIsOwned) |
| 45 delete Reporter; |
| 46 } |
| 47 |
| 48 // A valid pointer type is either: |
| 49 // * a pointer to a valid PNaCl scalar type (except i1), or |
| 50 // * a pointer to a valid PNaCl vector type (except i1), or |
| 51 // * a function pointer (with valid argument and return types). |
| 52 // |
| 53 // i1 is disallowed so that all loads and stores are a whole number of |
| 54 // bytes, and so that we do not need to define whether a store of i1 |
| 55 // zero-extends. |
| 56 static bool isValidPointerType(Type *Ty) { |
| 57 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) { |
| 58 if (PtrTy->getAddressSpace() != 0) |
| 59 return false; |
| 60 Type *EltTy = PtrTy->getElementType(); |
| 61 if (PNaClABITypeChecker::isValidScalarType(EltTy) && !EltTy->isIntegerTy(1)) |
| 62 return true; |
| 63 if (PNaClABITypeChecker::isValidVectorType(EltTy) && |
| 64 !cast<VectorType>(EltTy)->getElementType()->isIntegerTy(1)) |
| 65 return true; |
| 66 if (FunctionType *FTy = dyn_cast<FunctionType>(EltTy)) |
| 67 return PNaClABITypeChecker::isValidFunctionType(FTy); |
| 68 } |
| 69 return false; |
| 70 } |
| 71 |
| 72 static bool isIntrinsicFunc(const Value *Val) { |
| 73 if (const Function *F = dyn_cast<Function>(Val)) |
| 74 return F->isIntrinsic(); |
| 75 return false; |
| 76 } |
| 77 |
| 78 // InherentPtrs may be referenced by casts -- PtrToIntInst and |
| 79 // BitCastInst -- that produce NormalizedPtrs. |
| 80 // |
| 81 // InherentPtrs exclude intrinsic functions in order to prevent taking |
| 82 // the address of an intrinsic function. InherentPtrs include |
| 83 // intrinsic calls because some intrinsics return pointer types |
| 84 // (e.g. nacl.read.tp returns i8*). |
| 85 static bool isInherentPtr(const Value *Val) { |
| 86 return isa<AllocaInst>(Val) || |
| 87 (isa<GlobalValue>(Val) && !isIntrinsicFunc(Val)) || |
| 88 isa<IntrinsicInst>(Val); |
| 89 } |
| 90 |
| 91 // NormalizedPtrs may be used where pointer types are required -- for |
| 92 // loads, stores, etc. Note that this excludes ConstantExprs, |
| 93 // ConstantPointerNull and UndefValue. |
| 94 static bool isNormalizedPtr(const Value *Val) { |
| 95 if (!isValidPointerType(Val->getType())) |
| 96 return false; |
| 97 // The bitcast must also be a bitcast of an InherentPtr, but we |
| 98 // check that when visiting the bitcast instruction. |
| 99 return isa<IntToPtrInst>(Val) || isa<BitCastInst>(Val) || isInherentPtr(Val); |
| 100 } |
| 101 |
| 102 static bool isValidScalarOperand(const Value *Val) { |
| 103 // The types of Instructions and Arguments are checked elsewhere |
| 104 // (when visiting the Instruction or the Function). BasicBlocks are |
| 105 // included here because branch instructions have BasicBlock |
| 106 // operands. |
| 107 if (isa<Instruction>(Val) || isa<Argument>(Val) || isa<BasicBlock>(Val)) |
| 108 return true; |
| 109 |
| 110 // Allow some Constants. Note that this excludes ConstantExprs. |
| 111 return PNaClABITypeChecker::isValidScalarType(Val->getType()) && |
| 112 (isa<ConstantInt>(Val) || |
| 113 isa<ConstantFP>(Val) || |
| 114 isa<UndefValue>(Val)); |
| 115 } |
| 116 |
| 117 static bool isValidVectorOperand(const Value *Val) { |
| 118 // The types of Instructions and Arguments are checked elsewhere. |
| 119 if (isa<Instruction>(Val) || isa<Argument>(Val)) |
| 120 return true; |
| 121 // Contrary to scalars, constant vector values aren't allowed on |
| 122 // instructions, except undefined. Constant vectors are loaded from |
| 123 // constant global memory instead, and can be rematerialized as |
| 124 // constants by the backend if need be. |
| 125 return PNaClABITypeChecker::isValidVectorType(Val->getType()) && |
| 126 isa<UndefValue>(Val); |
| 127 } |
| 128 |
| 129 static bool hasAllowedAtomicRMWOperation( |
| 130 const NaCl::AtomicIntrinsics::AtomicIntrinsic *I, const CallInst *Call) { |
| 131 for (size_t P = 0; P != I->NumParams; ++P) { |
| 132 if (I->ParamType[P] != NaCl::AtomicIntrinsics::RMW) |
| 133 continue; |
| 134 |
| 135 const Value *Operation = Call->getOperand(P); |
| 136 if (!Operation) |
| 137 return false; |
| 138 const Constant *C = dyn_cast<Constant>(Operation); |
| 139 if (!C) |
| 140 return false; |
| 141 const APInt &I = C->getUniqueInteger(); |
| 142 if (I.ule(NaCl::AtomicInvalid) || I.uge(NaCl::AtomicNum)) |
| 143 return false; |
| 144 } |
| 145 return true; |
| 146 } |
| 147 |
| 148 static bool |
| 149 hasAllowedAtomicMemoryOrder(const NaCl::AtomicIntrinsics::AtomicIntrinsic *I, |
| 150 const CallInst *Call) { |
| 151 NaCl::MemoryOrder PreviousOrder = NaCl::MemoryOrderInvalid; |
| 152 |
| 153 for (size_t P = 0; P != I->NumParams; ++P) { |
| 154 if (I->ParamType[P] != NaCl::AtomicIntrinsics::Mem) |
| 155 continue; |
| 156 |
| 157 NaCl::MemoryOrder Order = NaCl::MemoryOrderInvalid; |
| 158 if (const Value *MemoryOrderOperand = Call->getOperand(P)) |
| 159 if (const Constant *C = dyn_cast<Constant>(MemoryOrderOperand)) { |
| 160 const APInt &I = C->getUniqueInteger(); |
| 161 if (I.ugt(NaCl::MemoryOrderInvalid) && I.ult(NaCl::MemoryOrderNum)) |
| 162 Order = static_cast<NaCl::MemoryOrder>(I.getLimitedValue()); |
| 163 } |
| 164 if (Order == NaCl::MemoryOrderInvalid) |
| 165 return false; |
| 166 |
| 167 // Validate PNaCl restrictions. |
| 168 switch (Order) { |
| 169 case NaCl::MemoryOrderInvalid: |
| 170 case NaCl::MemoryOrderNum: |
| 171 llvm_unreachable("Invalid memory order"); |
| 172 case NaCl::MemoryOrderRelaxed: |
| 173 case NaCl::MemoryOrderConsume: |
| 174 // TODO(jfb) PNaCl doesn't allow relaxed or consume memory ordering. |
| 175 return false; |
| 176 case NaCl::MemoryOrderAcquire: |
| 177 case NaCl::MemoryOrderRelease: |
| 178 case NaCl::MemoryOrderAcquireRelease: |
| 179 case NaCl::MemoryOrderSequentiallyConsistent: |
| 180 break; // Allowed by PNaCl. |
| 181 } |
| 182 |
| 183 // Validate conformance to the C++11 memory model. |
| 184 switch (I->ID) { |
| 185 default: |
| 186 llvm_unreachable("unexpected atomic operation"); |
| 187 case Intrinsic::nacl_atomic_load: |
| 188 // C++11 [atomics.types.operations.req]: The order argument shall not be |
| 189 // release nor acq_rel. |
| 190 if (Order == NaCl::MemoryOrderRelease || |
| 191 Order == NaCl::MemoryOrderAcquireRelease) |
| 192 return false; |
| 193 break; |
| 194 case Intrinsic::nacl_atomic_store: |
| 195 // C++11 [atomics.types.operations.req]: The order argument shall not be |
| 196 // consume, acquire, nor acq_rel. |
| 197 if (Order == NaCl::MemoryOrderConsume || |
| 198 Order == NaCl::MemoryOrderAcquire || |
| 199 Order == NaCl::MemoryOrderAcquireRelease) |
| 200 return false; |
| 201 break; |
| 202 case Intrinsic::nacl_atomic_rmw: |
| 203 break; // No restriction. |
| 204 case Intrinsic::nacl_atomic_cmpxchg: |
| 205 // C++11 [atomics.types.operations.req]: The failure argument shall not be |
| 206 // release nor acq_rel. The failure argument shall be no stronger than the |
| 207 // success argument. |
| 208 // Where the partial ordering is: |
| 209 // relaxed < consume < acquire < acq_rel < seq_cst |
| 210 // relaxed < release < acq_rel < seq_cst |
| 211 if (PreviousOrder != NaCl::MemoryOrderInvalid) { // Failure ordering. |
| 212 NaCl::MemoryOrder Success = PreviousOrder, Failure = Order; |
| 213 if (Failure == NaCl::MemoryOrderRelease || |
| 214 Failure == NaCl::MemoryOrderAcquireRelease) |
| 215 return false; |
| 216 if ((Success < Failure) || (Success == NaCl::MemoryOrderRelease && |
| 217 Failure != NaCl::MemoryOrderRelaxed)) |
| 218 return false; |
| 219 } |
| 220 break; // Success ordering has no restriction. |
| 221 case Intrinsic::nacl_atomic_fence: |
| 222 case Intrinsic::nacl_atomic_fence_all: |
| 223 break; // No restrictions. |
| 224 } |
| 225 |
| 226 PreviousOrder = Order; |
| 227 } |
| 228 |
| 229 return true; |
| 230 } |
| 231 |
| 232 static bool hasAllowedLockFreeByteSize(const CallInst *Call) { |
| 233 if (!Call->getType()->isIntegerTy()) |
| 234 return false; |
| 235 const Value *Operation = Call->getOperand(0); |
| 236 if (!Operation) |
| 237 return false; |
| 238 const Constant *C = dyn_cast<Constant>(Operation); |
| 239 if (!C) |
| 240 return false; |
| 241 const APInt &I = C->getUniqueInteger(); |
| 242 // PNaCl currently only supports atomics of byte size {1,2,4,8} (which |
| 243 // may or may not be lock-free). These values coincide with |
| 244 // C11/C++11's supported atomic types. |
| 245 if (I == 1 || I == 2 || I == 4 || I == 8) |
| 246 return true; |
| 247 return false; |
| 248 } |
| 249 |
| 250 // Check the instruction's opcode and its operands. The operands may |
| 251 // require opcode-specific checking. |
| 252 // |
| 253 // This returns an error string if the instruction is rejected, or |
| 254 // NULL if the instruction is allowed. |
| 255 const char *PNaClABIVerifyFunctions::checkInstruction(const DataLayout *DL, |
| 256 const Instruction *Inst) { |
| 257 // If the instruction has a single pointer operand, PtrOperandIndex is |
| 258 // set to its operand index. |
| 259 unsigned PtrOperandIndex = -1; |
| 260 |
| 261 // True if we should apply the default operand checks, at the end |
| 262 // of this function. |
| 263 bool ApplyDefaultOperandTypeChecks = true; |
| 264 |
| 265 switch (Inst->getOpcode()) { |
| 266 // Disallowed instructions. Default is to disallow. |
| 267 // We expand GetElementPtr out into arithmetic. |
| 268 case Instruction::GetElementPtr: |
| 269 // VAArg is expanded out by ExpandVarArgs. |
| 270 case Instruction::VAArg: |
| 271 // Zero-cost C++ exception handling is not supported yet. |
| 272 case Instruction::Invoke: |
| 273 case Instruction::LandingPad: |
| 274 case Instruction::Resume: |
| 275 // indirectbr may interfere with streaming |
| 276 case Instruction::IndirectBr: |
| 277 // TODO(jfb) Figure out ShuffleVector. |
| 278 case Instruction::ShuffleVector: |
| 279 // ExtractValue and InsertValue operate on struct values. |
| 280 case Instruction::ExtractValue: |
| 281 case Instruction::InsertValue: |
| 282 // Atomics should become NaCl intrinsics. |
| 283 case Instruction::AtomicCmpXchg: |
| 284 case Instruction::AtomicRMW: |
| 285 case Instruction::Fence: |
| 286 return "bad instruction opcode"; |
| 287 default: |
| 288 return "unknown instruction opcode"; |
| 289 |
| 290 // Terminator instructions |
| 291 case Instruction::Ret: |
| 292 case Instruction::Br: |
| 293 case Instruction::Unreachable: |
| 294 // Binary operations |
| 295 case Instruction::FAdd: |
| 296 case Instruction::FSub: |
| 297 case Instruction::FMul: |
| 298 case Instruction::FDiv: |
| 299 case Instruction::FRem: |
| 300 // Bitwise binary operations |
| 301 case Instruction::And: |
| 302 case Instruction::Or: |
| 303 case Instruction::Xor: |
| 304 // Conversion operations |
| 305 case Instruction::Trunc: |
| 306 case Instruction::ZExt: |
| 307 case Instruction::SExt: |
| 308 case Instruction::FPTrunc: |
| 309 case Instruction::FPExt: |
| 310 case Instruction::FPToUI: |
| 311 case Instruction::FPToSI: |
| 312 case Instruction::UIToFP: |
| 313 case Instruction::SIToFP: |
| 314 // Other operations |
| 315 case Instruction::FCmp: |
| 316 case Instruction::PHI: |
| 317 case Instruction::Select: |
| 318 break; |
| 319 |
| 320 // The following operations are of dubious usefulness on 1-bit |
| 321 // values. Use of the i1 type is disallowed here so that code |
| 322 // generators do not need to support these corner cases. |
| 323 case Instruction::ICmp: |
| 324 // Binary operations |
| 325 case Instruction::Add: |
| 326 case Instruction::Sub: |
| 327 case Instruction::Mul: |
| 328 case Instruction::UDiv: |
| 329 case Instruction::SDiv: |
| 330 case Instruction::URem: |
| 331 case Instruction::SRem: |
| 332 case Instruction::Shl: |
| 333 case Instruction::LShr: |
| 334 case Instruction::AShr: { |
| 335 const Type *Ty = Inst->getOperand(0)->getType(); |
| 336 if (!PNaClABITypeChecker::isValidIntArithmeticType( |
| 337 Inst->getOperand(0)->getType())) { |
| 338 if (Ty->isIntegerTy() || |
| 339 (Ty->isVectorTy() && Ty->getVectorElementType()->isIntegerTy())) { |
| 340 return "Invalid integer arithmetic type"; |
| 341 } else { |
| 342 return "Expects integer arithmetic type"; |
| 343 } |
| 344 } |
| 345 ApplyDefaultOperandTypeChecks = false; |
| 346 break; |
| 347 } |
| 348 |
| 349 // Vector. |
| 350 case Instruction::ExtractElement: |
| 351 case Instruction::InsertElement: { |
| 352 // Insert and extract element are restricted to constant indices |
| 353 // that are in range to prevent undefined behavior. |
| 354 // TODO(kschimpf) Figure out way to put test into pnacl-bcdis? |
| 355 Value *Vec = Inst->getOperand(0); |
| 356 Value *Idx = Inst->getOperand( |
| 357 Instruction::InsertElement == Inst->getOpcode() ? 2 : 1); |
| 358 if (!isa<ConstantInt>(Idx)) |
| 359 return "non-constant vector insert/extract index"; |
| 360 if (!PNaClABIProps::isVectorIndexSafe( |
| 361 cast<ConstantInt>(Idx)->getValue(), |
| 362 cast<VectorType>(Vec->getType())->getNumElements())) { |
| 363 return "out of range vector insert/extract index"; |
| 364 } |
| 365 break; |
| 366 } |
| 367 |
| 368 // Memory accesses. |
| 369 case Instruction::Load: { |
| 370 const LoadInst *Load = cast<LoadInst>(Inst); |
| 371 PtrOperandIndex = Load->getPointerOperandIndex(); |
| 372 if (Load->isAtomic()) |
| 373 return "atomic load"; |
| 374 if (Load->isVolatile()) |
| 375 return "volatile load"; |
| 376 if (!isNormalizedPtr(Inst->getOperand(PtrOperandIndex))) |
| 377 return "bad pointer"; |
| 378 if (!PNaClABIProps:: |
| 379 isAllowedAlignment(DL, Load->getAlignment(), Load->getType())) |
| 380 return "bad alignment"; |
| 381 break; |
| 382 } |
| 383 case Instruction::Store: { |
| 384 const StoreInst *Store = cast<StoreInst>(Inst); |
| 385 PtrOperandIndex = Store->getPointerOperandIndex(); |
| 386 if (Store->isAtomic()) |
| 387 return "atomic store"; |
| 388 if (Store->isVolatile()) |
| 389 return "volatile store"; |
| 390 if (!isNormalizedPtr(Inst->getOperand(PtrOperandIndex))) |
| 391 return "bad pointer"; |
| 392 if (!PNaClABIProps:: |
| 393 isAllowedAlignment(DL, Store->getAlignment(), |
| 394 Store->getValueOperand()->getType())) |
| 395 return "bad alignment"; |
| 396 break; |
| 397 } |
| 398 |
| 399 // Casts. |
| 400 case Instruction::BitCast: |
| 401 if (Inst->getType()->isPointerTy()) { |
| 402 PtrOperandIndex = 0; |
| 403 if (!isInherentPtr(Inst->getOperand(PtrOperandIndex))) |
| 404 return "operand not InherentPtr"; |
| 405 } |
| 406 break; |
| 407 case Instruction::IntToPtr: |
| 408 if (!cast<IntToPtrInst>(Inst)->getSrcTy()->isIntegerTy(32)) |
| 409 return "non-i32 inttoptr"; |
| 410 break; |
| 411 case Instruction::PtrToInt: |
| 412 PtrOperandIndex = 0; |
| 413 if (!isInherentPtr(Inst->getOperand(PtrOperandIndex))) |
| 414 return "operand not InherentPtr"; |
| 415 if (!Inst->getType()->isIntegerTy(32)) |
| 416 return "non-i32 ptrtoint"; |
| 417 break; |
| 418 |
| 419 case Instruction::Alloca: { |
| 420 const AllocaInst *Alloca = cast<AllocaInst>(Inst); |
| 421 if (!PNaClABIProps::isAllocaAllocatedType(Alloca->getAllocatedType())) |
| 422 return "non-i8 alloca"; |
| 423 if (!PNaClABIProps::isAllocaSizeType(Alloca->getArraySize()->getType())) |
| 424 return PNaClABIProps::ExpectedAllocaSizeType(); |
| 425 break; |
| 426 } |
| 427 |
| 428 case Instruction::Call: { |
| 429 const CallInst *Call = cast<CallInst>(Inst); |
| 430 if (Call->isInlineAsm()) |
| 431 return "inline assembly"; |
| 432 if (!Call->getAttributes().isEmpty()) |
| 433 return "bad call attributes"; |
| 434 if (!PNaClABIProps::isValidCallingConv(Call->getCallingConv())) |
| 435 return "bad calling convention"; |
| 436 |
| 437 // Intrinsic calls can have multiple pointer arguments and |
| 438 // metadata arguments, so handle them specially. |
| 439 // TODO(kschimpf) How can we lift this to pnacl-bcdis. |
| 440 if (const IntrinsicInst *Call = dyn_cast<IntrinsicInst>(Inst)) { |
| 441 for (unsigned ArgNum = 0, E = Call->getNumArgOperands(); |
| 442 ArgNum < E; ++ArgNum) { |
| 443 const Value *Arg = Call->getArgOperand(ArgNum); |
| 444 if (!(isValidScalarOperand(Arg) || |
| 445 isValidVectorOperand(Arg) || |
| 446 isNormalizedPtr(Arg) || |
| 447 isa<MDNode>(Arg))) |
| 448 return "bad intrinsic operand"; |
| 449 } |
| 450 |
| 451 // Disallow alignments other than 1 on memcpy() etc., for the |
| 452 // same reason that we disallow them on integer loads and |
| 453 // stores. |
| 454 if (const MemIntrinsic *MemOp = dyn_cast<MemIntrinsic>(Call)) { |
| 455 // Avoid the getAlignment() method here because it aborts if |
| 456 // the alignment argument is not a Constant. |
| 457 Value *AlignArg = MemOp->getArgOperand(3); |
| 458 if (!isa<ConstantInt>(AlignArg) || |
| 459 cast<ConstantInt>(AlignArg)->getZExtValue() != 1) { |
| 460 return "bad alignment"; |
| 461 } |
| 462 } |
| 463 |
| 464 switch (Call->getIntrinsicID()) { |
| 465 default: break; // Other intrinsics don't require checks. |
| 466 // Disallow NaCl atomic intrinsics which don't have valid |
| 467 // constant NaCl::AtomicOperation and NaCl::MemoryOrder |
| 468 // parameters. |
| 469 case Intrinsic::nacl_atomic_load: |
| 470 case Intrinsic::nacl_atomic_store: |
| 471 case Intrinsic::nacl_atomic_rmw: |
| 472 case Intrinsic::nacl_atomic_cmpxchg: |
| 473 case Intrinsic::nacl_atomic_fence: |
| 474 case Intrinsic::nacl_atomic_fence_all: { |
| 475 // All overloads have memory order and RMW operation in the |
| 476 // same parameter, arbitrarily use the I32 overload. |
| 477 Type *T = Type::getInt32Ty( |
| 478 Inst->getParent()->getParent()->getContext()); |
| 479 const NaCl::AtomicIntrinsics::AtomicIntrinsic *I = |
| 480 AtomicIntrinsics->find(Call->getIntrinsicID(), T); |
| 481 if (!I) |
| 482 // All intrinsics have an I32 overload. Failure here means there |
| 483 // is no such intrinsic. |
| 484 return "invalid atomic intrinsic"; |
| 485 if (!hasAllowedAtomicMemoryOrder(I, Call)) |
| 486 return "invalid memory order"; |
| 487 if (!hasAllowedAtomicRMWOperation(I, Call)) |
| 488 return "invalid atomicRMW operation"; |
| 489 } break; |
| 490 // Disallow NaCl atomic_is_lock_free intrinsics which don't |
| 491 // have valid constant size type. |
| 492 case Intrinsic::nacl_atomic_is_lock_free: |
| 493 if (!hasAllowedLockFreeByteSize(Call)) |
| 494 return "invalid atomic lock-free byte size"; |
| 495 break; |
| 496 } |
| 497 |
| 498 // Allow the instruction and skip the later checks. |
| 499 return NULL; |
| 500 } |
| 501 |
| 502 // The callee is the last operand. |
| 503 PtrOperandIndex = Inst->getNumOperands() - 1; |
| 504 if (!isNormalizedPtr(Inst->getOperand(PtrOperandIndex))) |
| 505 return "bad function callee operand"; |
| 506 break; |
| 507 } |
| 508 |
| 509 case Instruction::Switch: { |
| 510 // SwitchInst represents switch cases using array and vector |
| 511 // constants, which we normally reject, so we must check |
| 512 // SwitchInst specially here. |
| 513 const SwitchInst *Switch = cast<SwitchInst>(Inst); |
| 514 if (!isValidScalarOperand(Switch->getCondition())) |
| 515 return "bad switch condition"; |
| 516 const Type *SwitchType = Switch->getCondition()->getType(); |
| 517 if (!PNaClABITypeChecker::isValidSwitchConditionType(SwitchType)) |
| 518 return PNaClABITypeChecker::ExpectedSwitchConditionType(SwitchType); |
| 519 |
| 520 // SwitchInst requires the cases to be ConstantInts, but it |
| 521 // doesn't require their types to be the same as the condition |
| 522 // value, so check all the cases too. |
| 523 for (SwitchInst::ConstCaseIt Case = Switch->case_begin(), |
| 524 E = Switch->case_end(); Case != E; ++Case) { |
| 525 if (!isValidScalarOperand(Case.getCaseValue())) |
| 526 return "bad switch case"; |
| 527 } |
| 528 |
| 529 // Allow the instruction and skip the later checks. |
| 530 return NULL; |
| 531 } |
| 532 } |
| 533 |
| 534 if (ApplyDefaultOperandTypeChecks) { |
| 535 // Check the instruction's operands. We have already checked any |
| 536 // pointer operands. Any remaining operands must be scalars or vectors. |
| 537 for (unsigned OpNum = 0, E = Inst->getNumOperands(); OpNum < E; ++OpNum) { |
| 538 if (OpNum != PtrOperandIndex && |
| 539 !(isValidScalarOperand(Inst->getOperand(OpNum)) || |
| 540 isValidVectorOperand(Inst->getOperand(OpNum)))) |
| 541 return "bad operand"; |
| 542 } |
| 543 } |
| 544 |
| 545 // Check arithmetic attributes. |
| 546 if (const OverflowingBinaryOperator *Op = |
| 547 dyn_cast<OverflowingBinaryOperator>(Inst)) { |
| 548 if (Op->hasNoUnsignedWrap()) |
| 549 return "has \"nuw\" attribute"; |
| 550 if (Op->hasNoSignedWrap()) |
| 551 return "has \"nsw\" attribute"; |
| 552 } |
| 553 if (const PossiblyExactOperator *Op = |
| 554 dyn_cast<PossiblyExactOperator>(Inst)) { |
| 555 if (Op->isExact()) |
| 556 return "has \"exact\" attribute"; |
| 557 } |
| 558 |
| 559 // Allow the instruction. |
| 560 return NULL; |
| 561 } |
| 562 |
| 563 bool PNaClABIVerifyFunctions::runOnFunction(Function &F) { |
| 564 const DataLayout *DL = &getAnalysis<DataLayoutPass>().getDataLayout(); |
| 565 SmallVector<StringRef, 8> MDNames; |
| 566 F.getContext().getMDKindNames(MDNames); |
| 567 |
| 568 for (Function::const_iterator FI = F.begin(), FE = F.end(); |
| 569 FI != FE; ++FI) { |
| 570 for (BasicBlock::const_iterator BBI = FI->begin(), BBE = FI->end(); |
| 571 BBI != BBE; ++BBI) { |
| 572 const Instruction *Inst = BBI; |
| 573 // Check the instruction opcode first. This simplifies testing, |
| 574 // because some instruction opcodes must be rejected out of hand |
| 575 // (regardless of the instruction's result type) and the tests |
| 576 // check the reason for rejection. |
| 577 const char *Error = checkInstruction(DL, BBI); |
| 578 // Check the instruction's result type. |
| 579 bool BadResult = false; |
| 580 if (!Error && !(PNaClABITypeChecker::isValidScalarType(Inst->getType()) || |
| 581 PNaClABITypeChecker::isValidVectorType(Inst->getType()) || |
| 582 isNormalizedPtr(Inst) || |
| 583 isa<AllocaInst>(Inst))) { |
| 584 Error = "bad result type"; |
| 585 BadResult = true; |
| 586 } |
| 587 if (Error) { |
| 588 Reporter->addError() |
| 589 << "Function " << F.getName() << " disallowed: " << Error << ": " |
| 590 << (BadResult ? PNaClABITypeChecker::getTypeName(BBI->getType()) |
| 591 : "") << " " << *BBI << "\n"; |
| 592 } |
| 593 |
| 594 // Check instruction attachment metadata. |
| 595 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; |
| 596 BBI->getAllMetadata(MDForInst); |
| 597 |
| 598 for (unsigned i = 0, e = MDForInst.size(); i != e; i++) { |
| 599 if (!PNaClABIProps::isWhitelistedMetadata(MDForInst[i].first)) { |
| 600 Reporter->addError() |
| 601 << "Function " << F.getName() |
| 602 << " has disallowed instruction metadata: " |
| 603 << getMDNodeString(MDForInst[i].first, MDNames) << "\n"; |
| 604 } |
| 605 } |
| 606 } |
| 607 } |
| 608 |
| 609 Reporter->checkForFatalErrors(); |
| 610 return false; |
| 611 } |
| 612 |
| 613 // This method exists so that the passes can easily be run with opt -analyze. |
| 614 // In this case the default constructor is used and we want to reset the error |
| 615 // messages after each print. |
| 616 void PNaClABIVerifyFunctions::print(llvm::raw_ostream &O, const Module *M) |
| 617 const { |
| 618 Reporter->printErrors(O); |
| 619 Reporter->reset(); |
| 620 } |
| 621 |
| 622 char PNaClABIVerifyFunctions::ID = 0; |
| 623 INITIALIZE_PASS(PNaClABIVerifyFunctions, "verify-pnaclabi-functions", |
| 624 "Verify functions for PNaCl", false, true) |
| 625 |
| 626 FunctionPass *llvm::createPNaClABIVerifyFunctionsPass( |
| 627 PNaClABIErrorReporter *Reporter) { |
| 628 return new PNaClABIVerifyFunctions(Reporter); |
| 629 } |
OLD | NEW |