LLVM 23.0.0git
NumericalStabilitySanitizer.cpp
Go to the documentation of this file.
1//===-- NumericalStabilitySanitizer.cpp -----------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the instrumentation pass for the numerical sanitizer.
10// Conceptually the pass injects shadow computations using higher precision
11// types and inserts consistency checks. For details see the paper
12// https://arxiv.org/abs/2102.12782.
13//
14//===----------------------------------------------------------------------===//
15
17
18#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/Statistic.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/Function.h"
26#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/Intrinsics.h"
29#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/MDBuilder.h"
31#include "llvm/IR/Metadata.h"
32#include "llvm/IR/Module.h"
33#include "llvm/IR/Type.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/Regex.h"
42
43#include <cstdint>
44
45using namespace llvm;
46
47#define DEBUG_TYPE "nsan"
48
49STATISTIC(NumInstrumentedFTLoads,
50 "Number of instrumented floating-point loads");
51
52STATISTIC(NumInstrumentedFTCalls,
53 "Number of instrumented floating-point calls");
54STATISTIC(NumInstrumentedFTRets,
55 "Number of instrumented floating-point returns");
56STATISTIC(NumInstrumentedFTStores,
57 "Number of instrumented floating-point stores");
58STATISTIC(NumInstrumentedNonFTStores,
59 "Number of instrumented non floating-point stores");
61 NumInstrumentedNonFTMemcpyStores,
62 "Number of instrumented non floating-point stores with memcpy semantics");
63STATISTIC(NumInstrumentedFCmp, "Number of instrumented fcmps");
64
65// Using smaller shadow types types can help improve speed. For example, `dlq`
66// is 3x slower to 5x faster in opt mode and 2-6x faster in dbg mode compared to
67// `dqq`.
69 "nsan-shadow-type-mapping", cl::init("dqq"),
70 cl::desc("One shadow type id for each of `float`, `double`, `long double`. "
71 "`d`,`l`,`q`,`e` mean double, x86_fp80, fp128 (quad) and "
72 "ppc_fp128 (extended double) respectively. The default is to "
73 "shadow `float` as `double`, and `double` and `x86_fp80` as "
74 "`fp128`"),
76
77static cl::opt<bool>
78 ClInstrumentFCmp("nsan-instrument-fcmp", cl::init(true),
79 cl::desc("Instrument floating-point comparisons"),
81
83 "check-functions-filter",
84 cl::desc("Only emit checks for arguments of functions "
85 "whose names match the given regular expression"),
86 cl::value_desc("regex"));
87
89 "nsan-truncate-fcmp-eq", cl::init(true),
91 "This flag controls the behaviour of fcmp equality comparisons."
92 "For equality comparisons such as `x == 0.0f`, we can perform the "
93 "shadow check in the shadow (`x_shadow == 0.0) == (x == 0.0f)`) or app "
94 " domain (`(trunc(x_shadow) == 0.0f) == (x == 0.0f)`). This helps "
95 "catch the case when `x_shadow` is accurate enough (and therefore "
96 "close enough to zero) so that `trunc(x_shadow)` is zero even though "
97 "both `x` and `x_shadow` are not"),
99
100// When there is external, uninstrumented code writing to memory, the shadow
101// memory can get out of sync with the application memory. Enabling this flag
102// emits consistency checks for loads to catch this situation.
103// When everything is instrumented, this is not strictly necessary because any
104// load should have a corresponding store, but can help debug cases when the
105// framework did a bad job at tracking shadow memory modifications by failing on
106// load rather than store.
107// TODO: provide a way to resume computations from the FT value when the load
108// is inconsistent. This ensures that further computations are not polluted.
109static cl::opt<bool> ClCheckLoads("nsan-check-loads",
110 cl::desc("Check floating-point load"),
111 cl::Hidden);
112
113static cl::opt<bool> ClCheckStores("nsan-check-stores", cl::init(true),
114 cl::desc("Check floating-point stores"),
115 cl::Hidden);
116
117static cl::opt<bool> ClCheckRet("nsan-check-ret", cl::init(true),
118 cl::desc("Check floating-point return values"),
119 cl::Hidden);
120
121// LLVM may store constant floats as bitcasted ints.
122// It's not really necessary to shadow such stores,
123// if the shadow value is unknown the framework will re-extend it on load
124// anyway. Moreover, because of size collisions (e.g. bf16 vs f16) it is
125// impossible to determine the floating-point type based on the size.
126// However, for debugging purposes it can be useful to model such stores.
128 "nsan-propagate-non-ft-const-stores-as-ft",
129 cl::desc(
130 "Propagate non floating-point const stores as floating point values."
131 "For debugging purposes only"),
132 cl::Hidden);
133
134constexpr StringLiteral kNsanModuleCtorName("nsan.module_ctor");
135constexpr StringLiteral kNsanInitName("__nsan_init");
136
137// The following values must be kept in sync with the runtime.
138constexpr int kShadowScale = 2;
139constexpr int kMaxVectorWidth = 8;
140constexpr int kMaxNumArgs = 128;
141constexpr int kMaxShadowTypeSizeBytes = 16; // fp128
142
143namespace {
144
145// Defines the characteristics (type id, type, and floating-point semantics)
146// attached for all possible shadow types.
147class ShadowTypeConfig {
148public:
149 static std::unique_ptr<ShadowTypeConfig> fromNsanTypeId(char TypeId);
150
151 // The LLVM Type corresponding to the shadow type.
152 virtual Type *getType(LLVMContext &Context) const = 0;
153
154 // The nsan type id of the shadow type (`d`, `l`, `q`, ...).
155 virtual char getNsanTypeId() const = 0;
156
157 virtual ~ShadowTypeConfig() = default;
158};
159
160template <char NsanTypeId>
161class ShadowTypeConfigImpl : public ShadowTypeConfig {
162public:
163 char getNsanTypeId() const override { return NsanTypeId; }
164 static constexpr char kNsanTypeId = NsanTypeId;
165};
166
167// `double` (`d`) shadow type.
168class F64ShadowConfig : public ShadowTypeConfigImpl<'d'> {
169 Type *getType(LLVMContext &Context) const override {
170 return Type::getDoubleTy(Context);
171 }
172};
173
174// `x86_fp80` (`l`) shadow type: X86 long double.
175class F80ShadowConfig : public ShadowTypeConfigImpl<'l'> {
176 Type *getType(LLVMContext &Context) const override {
177 return Type::getX86_FP80Ty(Context);
178 }
179};
180
181// `fp128` (`q`) shadow type.
182class F128ShadowConfig : public ShadowTypeConfigImpl<'q'> {
183 Type *getType(LLVMContext &Context) const override {
184 return Type::getFP128Ty(Context);
185 }
186};
187
188// `ppc_fp128` (`e`) shadow type: IBM extended double with 106 bits of mantissa.
189class PPC128ShadowConfig : public ShadowTypeConfigImpl<'e'> {
190 Type *getType(LLVMContext &Context) const override {
191 return Type::getPPC_FP128Ty(Context);
192 }
193};
194
195// Creates a ShadowTypeConfig given its type id.
196std::unique_ptr<ShadowTypeConfig>
197ShadowTypeConfig::fromNsanTypeId(const char TypeId) {
198 switch (TypeId) {
199 case F64ShadowConfig::kNsanTypeId:
200 return std::make_unique<F64ShadowConfig>();
201 case F80ShadowConfig::kNsanTypeId:
202 return std::make_unique<F80ShadowConfig>();
203 case F128ShadowConfig::kNsanTypeId:
204 return std::make_unique<F128ShadowConfig>();
205 case PPC128ShadowConfig::kNsanTypeId:
206 return std::make_unique<PPC128ShadowConfig>();
207 }
208 report_fatal_error("nsan: invalid shadow type id '" + Twine(TypeId) + "'");
209}
210
211// An enum corresponding to shadow value types. Used as indices in arrays, so
212// not an `enum class`.
213enum FTValueType { kFloat, kDouble, kLongDouble, kNumValueTypes };
214
215// If `FT` corresponds to a primitive FTValueType, return it.
216static std::optional<FTValueType> ftValueTypeFromType(Type *FT) {
217 if (FT->isFloatTy())
218 return kFloat;
219 if (FT->isDoubleTy())
220 return kDouble;
221 if (FT->isX86_FP80Ty())
222 return kLongDouble;
223 return {};
224}
225
226// Returns the LLVM type for an FTValueType.
227static Type *typeFromFTValueType(FTValueType VT, LLVMContext &Context) {
228 switch (VT) {
229 case kFloat:
230 return Type::getFloatTy(Context);
231 case kDouble:
232 return Type::getDoubleTy(Context);
233 case kLongDouble:
234 return Type::getX86_FP80Ty(Context);
235 case kNumValueTypes:
236 return nullptr;
237 }
238 llvm_unreachable("Unhandled FTValueType enum");
239}
240
241// Returns the type name for an FTValueType.
242static const char *typeNameFromFTValueType(FTValueType VT) {
243 switch (VT) {
244 case kFloat:
245 return "float";
246 case kDouble:
247 return "double";
248 case kLongDouble:
249 return "longdouble";
250 case kNumValueTypes:
251 return nullptr;
252 }
253 llvm_unreachable("Unhandled FTValueType enum");
254}
255
256// A specific mapping configuration of application type to shadow type for nsan
257// (see -nsan-shadow-mapping flag).
258class MappingConfig {
259public:
260 explicit MappingConfig(LLVMContext &C) : Context(C) {
261 if (ClShadowMapping.size() != 3)
262 report_fatal_error("Invalid nsan mapping: " + Twine(ClShadowMapping));
263 unsigned ShadowTypeSizeBits[kNumValueTypes];
264 for (int VT = 0; VT < kNumValueTypes; ++VT) {
265 auto Config = ShadowTypeConfig::fromNsanTypeId(ClShadowMapping[VT]);
266 if (!Config)
267 report_fatal_error("Failed to get ShadowTypeConfig for " +
269 const unsigned AppTypeSize =
270 typeFromFTValueType(static_cast<FTValueType>(VT), Context)
271 ->getScalarSizeInBits();
272 const unsigned ShadowTypeSize =
273 Config->getType(Context)->getScalarSizeInBits();
274 // Check that the shadow type size is at most kShadowScale times the
275 // application type size, so that shadow memory compoutations are valid.
276 if (ShadowTypeSize > kShadowScale * AppTypeSize)
277 report_fatal_error("Invalid nsan mapping f" + Twine(AppTypeSize) +
278 "->f" + Twine(ShadowTypeSize) +
279 ": The shadow type size should be at most " +
281 " times the application type size");
282 ShadowTypeSizeBits[VT] = ShadowTypeSize;
283 Configs[VT] = std::move(Config);
284 }
285
286 // Check that the mapping is monotonous. This is required because if one
287 // does an fpextend of `float->long double` in application code, nsan is
288 // going to do an fpextend of `shadow(float) -> shadow(long double)` in
289 // shadow code. This will fail in `qql` mode, since nsan would be
290 // fpextending `f128->long`, which is invalid.
291 // TODO: Relax this.
292 if (ShadowTypeSizeBits[kFloat] > ShadowTypeSizeBits[kDouble] ||
293 ShadowTypeSizeBits[kDouble] > ShadowTypeSizeBits[kLongDouble])
294 report_fatal_error("Invalid nsan mapping: { float->f" +
295 Twine(ShadowTypeSizeBits[kFloat]) + "; double->f" +
296 Twine(ShadowTypeSizeBits[kDouble]) +
297 "; long double->f" +
298 Twine(ShadowTypeSizeBits[kLongDouble]) + " }");
299 }
300
301 const ShadowTypeConfig &byValueType(FTValueType VT) const {
302 assert(VT < FTValueType::kNumValueTypes && "invalid value type");
303 return *Configs[VT];
304 }
305
306 // Returns the extended shadow type for a given application type.
307 Type *getExtendedFPType(Type *FT) const {
308 if (const auto VT = ftValueTypeFromType(FT))
309 return Configs[*VT]->getType(Context);
310 if (FT->isVectorTy()) {
311 auto *VecTy = cast<VectorType>(FT);
312 // TODO: add support for scalable vector types.
313 if (VecTy->isScalableTy())
314 return nullptr;
315 Type *ExtendedScalar = getExtendedFPType(VecTy->getElementType());
316 return ExtendedScalar
317 ? VectorType::get(ExtendedScalar, VecTy->getElementCount())
318 : nullptr;
319 }
320 return nullptr;
321 }
322
323private:
324 LLVMContext &Context;
325 std::unique_ptr<ShadowTypeConfig> Configs[FTValueType::kNumValueTypes];
326};
327
328// The memory extents of a type specifies how many elements of a given
329// FTValueType needs to be stored when storing this type.
330struct MemoryExtents {
331 FTValueType ValueType;
332 uint64_t NumElts;
333};
334
335static MemoryExtents getMemoryExtentsOrDie(Type *FT) {
336 if (const auto VT = ftValueTypeFromType(FT))
337 return {*VT, 1};
338 if (auto *VecTy = dyn_cast<VectorType>(FT)) {
339 const auto ScalarExtents = getMemoryExtentsOrDie(VecTy->getElementType());
340 return {ScalarExtents.ValueType,
341 ScalarExtents.NumElts * VecTy->getElementCount().getFixedValue()};
342 }
343 llvm_unreachable("invalid value type");
344}
345
346// The location of a check. Passed as parameters to runtime checking functions.
347class CheckLoc {
348public:
349 // Creates a location that references an application memory location.
350 static CheckLoc makeStore(Value *Address) {
351 CheckLoc Result(kStore);
352 Result.Address = Address;
353 return Result;
354 }
355 static CheckLoc makeLoad(Value *Address) {
356 CheckLoc Result(kLoad);
357 Result.Address = Address;
358 return Result;
359 }
360
361 // Creates a location that references an argument, given by id.
362 static CheckLoc makeArg(int ArgId) {
363 CheckLoc Result(kArg);
364 Result.ArgId = ArgId;
365 return Result;
366 }
367
368 // Creates a location that references the return value of a function.
369 static CheckLoc makeRet() { return CheckLoc(kRet); }
370
371 // Creates a location that references a vector insert.
372 static CheckLoc makeInsert() { return CheckLoc(kInsert); }
373
374 // Returns the CheckType of location this refers to, as an integer-typed LLVM
375 // IR value.
376 Value *getType(LLVMContext &C) const {
377 return ConstantInt::get(Type::getInt32Ty(C), static_cast<int>(CheckTy));
378 }
379
380 // Returns a CheckType-specific value representing details of the location
381 // (e.g. application address for loads or stores), as an `IntptrTy`-typed LLVM
382 // IR value.
383 Value *getValue(Type *IntptrTy, IRBuilder<> &Builder) const {
384 switch (CheckTy) {
385 case kUnknown:
386 llvm_unreachable("unknown type");
387 case kRet:
388 case kInsert:
389 return ConstantInt::get(IntptrTy, 0);
390 case kArg:
391 return ConstantInt::get(IntptrTy, ArgId);
392 case kLoad:
393 case kStore:
394 return Builder.CreatePtrToInt(Address, IntptrTy);
395 }
396 llvm_unreachable("Unhandled CheckType enum");
397 }
398
399private:
400 // Must be kept in sync with the runtime,
401 // see compiler-rt/lib/nsan/nsan_stats.h
402 enum CheckType {
403 kUnknown = 0,
404 kRet,
405 kArg,
406 kLoad,
407 kStore,
408 kInsert,
409 };
410 explicit CheckLoc(CheckType CheckTy) : CheckTy(CheckTy) {}
411
412 Value *Address = nullptr;
413 const CheckType CheckTy;
414 int ArgId = -1;
415};
416
417// A map of LLVM IR values to shadow LLVM IR values.
418class ValueToShadowMap {
419public:
420 explicit ValueToShadowMap(const MappingConfig &Config) : Config(Config) {}
421
422 ValueToShadowMap(const ValueToShadowMap &) = delete;
423 ValueToShadowMap &operator=(const ValueToShadowMap &) = delete;
424
425 // Sets the shadow value for a value. Asserts that the value does not already
426 // have a value.
427 void setShadow(Value &V, Value &Shadow) {
428 [[maybe_unused]] const bool Inserted = Map.try_emplace(&V, &Shadow).second;
429 LLVM_DEBUG({
430 if (!Inserted) {
431 if (auto *I = dyn_cast<Instruction>(&V))
432 errs() << I->getFunction()->getName() << ": ";
433 errs() << "duplicate shadow (" << &V << "): ";
434 V.dump();
435 }
436 });
437 assert(Inserted && "duplicate shadow");
438 }
439
440 // Returns true if the value already has a shadow (including if the value is a
441 // constant). If true, calling getShadow() is valid.
442 bool hasShadow(Value *V) const { return isa<Constant>(V) || Map.contains(V); }
443
444 // Returns the shadow value for a given value. Asserts that the value has
445 // a shadow value. Lazily creates shadows for constant values.
446 Value *getShadow(Value *V) const {
447 if (Constant *C = dyn_cast<Constant>(V))
448 return getShadowConstant(C);
449 return Map.find(V)->second;
450 }
451
452 bool empty() const { return Map.empty(); }
453
454private:
455 // Extends a constant application value to its shadow counterpart.
456 APFloat extendConstantFP(APFloat CV, const fltSemantics &To) const {
457 bool LosesInfo = false;
458 CV.convert(To, APFloatBase::rmTowardZero, &LosesInfo);
459 return CV;
460 }
461
462 // Returns the shadow constant for the given application constant.
463 Constant *getShadowConstant(Constant *C) const {
465 return UndefValue::get(Config.getExtendedFPType(U->getType()));
466 }
467 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
468 // Floating-point constants.
469 Type *Ty = Config.getExtendedFPType(CFP->getType());
470 return ConstantFP::get(
471 Ty, extendConstantFP(CFP->getValueAPF(),
472 Ty->getScalarType()->getFltSemantics()));
473 }
474 // Vector, array, or aggregate constants.
475 if (C->getType()->isVectorTy()) {
477 for (int I = 0, E = cast<VectorType>(C->getType())
478 ->getElementCount()
479 .getFixedValue();
480 I < E; ++I)
481 Elements.push_back(getShadowConstant(C->getAggregateElement(I)));
482 return ConstantVector::get(Elements);
483 }
484 llvm_unreachable("unimplemented");
485 }
486
487 const MappingConfig &Config;
489};
490
491class NsanMemOpFn {
492public:
493 NsanMemOpFn(Module &M, ArrayRef<StringRef> Sized, StringRef Fallback,
494 size_t NumArgs);
495 FunctionCallee getFunctionFor(uint64_t MemOpSize) const;
496 FunctionCallee getFallback() const;
497
498private:
500 size_t NumSizedFuncs;
501};
502
503NsanMemOpFn::NsanMemOpFn(Module &M, ArrayRef<StringRef> Sized,
504 StringRef Fallback, size_t NumArgs) {
505 LLVMContext &Ctx = M.getContext();
506 AttributeList Attr;
507 Attr = Attr.addFnAttribute(Ctx, Attribute::NoUnwind);
508 Type *PtrTy = PointerType::getUnqual(Ctx);
509 Type *VoidTy = Type::getVoidTy(Ctx);
510 IntegerType *IntptrTy = M.getDataLayout().getIntPtrType(Ctx);
511 FunctionType *SizedFnTy = nullptr;
512
513 NumSizedFuncs = Sized.size();
514
515 // First entry is fallback function
516 if (NumArgs == 3) {
517 Funcs.push_back(
518 M.getOrInsertFunction(Fallback, Attr, VoidTy, PtrTy, PtrTy, IntptrTy));
519 SizedFnTy = FunctionType::get(VoidTy, {PtrTy, PtrTy}, false);
520 } else if (NumArgs == 2) {
521 Funcs.push_back(
522 M.getOrInsertFunction(Fallback, Attr, VoidTy, PtrTy, IntptrTy));
523 SizedFnTy = FunctionType::get(VoidTy, {PtrTy}, false);
524 } else {
525 llvm_unreachable("Unexpected value of sized functions arguments");
526 }
527
528 for (size_t i = 0; i < NumSizedFuncs; ++i)
529 Funcs.push_back(M.getOrInsertFunction(Sized[i], SizedFnTy, Attr));
530}
531
532FunctionCallee NsanMemOpFn::getFunctionFor(uint64_t MemOpSize) const {
533 // Now `getFunctionFor` operates on `Funcs` of size 4 (at least) and the
534 // following code assumes that the number of functions in `Func` is sufficient
535 assert(NumSizedFuncs >= 3 && "Unexpected number of sized functions");
536
537 size_t Idx =
538 MemOpSize == 4 ? 1 : (MemOpSize == 8 ? 2 : (MemOpSize == 16 ? 3 : 0));
539
540 return Funcs[Idx];
541}
542
543FunctionCallee NsanMemOpFn::getFallback() const { return Funcs[0]; }
544
545/// Instantiating NumericalStabilitySanitizer inserts the nsan runtime library
546/// API function declarations into the module if they don't exist already.
547/// Instantiating ensures the __nsan_init function is in the list of global
548/// constructors for the module.
549class NumericalStabilitySanitizer {
550public:
551 NumericalStabilitySanitizer(Module &M);
552 bool sanitizeFunction(Function &F, const TargetLibraryInfo &TLI);
553
554private:
555 bool instrumentMemIntrinsic(MemIntrinsic *MI);
556 void maybeAddSuffixForNsanInterface(CallBase *CI);
557 bool addrPointsToConstantData(Value *Addr);
558 void maybeCreateShadowValue(Instruction &Root, const TargetLibraryInfo &TLI,
559 ValueToShadowMap &Map);
560 Value *createShadowValueWithOperandsAvailable(Instruction &Inst,
561 const TargetLibraryInfo &TLI,
562 const ValueToShadowMap &Map);
563 PHINode *maybeCreateShadowPhi(PHINode &Phi, const TargetLibraryInfo &TLI);
564 void createShadowArguments(Function &F, const TargetLibraryInfo &TLI,
565 ValueToShadowMap &Map);
566
567 void populateShadowStack(CallBase &CI, const TargetLibraryInfo &TLI,
568 const ValueToShadowMap &Map);
569
570 void propagateShadowValues(Instruction &Inst, const TargetLibraryInfo &TLI,
571 const ValueToShadowMap &Map);
572 Value *emitCheck(Value *V, Value *ShadowV, IRBuilder<> &Builder,
573 CheckLoc Loc);
574 Value *emitCheckInternal(Value *V, Value *ShadowV, IRBuilder<> &Builder,
575 CheckLoc Loc);
576 void emitFCmpCheck(FCmpInst &FCmp, const ValueToShadowMap &Map);
577
578 // Value creation handlers.
579 Value *handleLoad(LoadInst &Load, Type *VT, Type *ExtendedVT);
580 Value *handleCallBase(CallBase &Call, Type *VT, Type *ExtendedVT,
581 const TargetLibraryInfo &TLI,
582 const ValueToShadowMap &Map, IRBuilder<> &Builder);
583 Value *maybeHandleKnownCallBase(CallBase &Call, Type *VT, Type *ExtendedVT,
584 const TargetLibraryInfo &TLI,
585 const ValueToShadowMap &Map,
586 IRBuilder<> &Builder);
587 Value *handleTrunc(const FPTruncInst &Trunc, Type *VT, Type *ExtendedVT,
588 const ValueToShadowMap &Map, IRBuilder<> &Builder);
589 Value *handleExt(const FPExtInst &Ext, Type *VT, Type *ExtendedVT,
590 const ValueToShadowMap &Map, IRBuilder<> &Builder);
591
592 // Value propagation handlers.
593 void propagateFTStore(StoreInst &Store, Type *VT, Type *ExtendedVT,
594 const ValueToShadowMap &Map);
595 void propagateNonFTStore(StoreInst &Store, Type *VT,
596 const ValueToShadowMap &Map);
597
598 const DataLayout &DL;
599 LLVMContext &Context;
600 MappingConfig Config;
601 IntegerType *IntptrTy = nullptr;
602
603 // TODO: Use std::array instead?
604 FunctionCallee NsanGetShadowPtrForStore[FTValueType::kNumValueTypes] = {};
605 FunctionCallee NsanGetShadowPtrForLoad[FTValueType::kNumValueTypes] = {};
606 FunctionCallee NsanCheckValue[FTValueType::kNumValueTypes] = {};
607 FunctionCallee NsanFCmpFail[FTValueType::kNumValueTypes] = {};
608
609 NsanMemOpFn NsanCopyFns;
610 NsanMemOpFn NsanSetUnknownFns;
611
612 FunctionCallee NsanGetRawShadowTypePtr;
613 FunctionCallee NsanGetRawShadowPtr;
614 GlobalValue *NsanShadowRetTag = nullptr;
615
616 Type *NsanShadowRetType = nullptr;
617 GlobalValue *NsanShadowRetPtr = nullptr;
618
619 GlobalValue *NsanShadowArgsTag = nullptr;
620
621 Type *NsanShadowArgsType = nullptr;
622 GlobalValue *NsanShadowArgsPtr = nullptr;
623
624 std::optional<Regex> CheckFunctionsFilter;
625};
626} // end anonymous namespace
627
628PreservedAnalyses
631 M, kNsanModuleCtorName, kNsanInitName, /*InitArgTypes=*/{},
632 /*InitArgs=*/{},
633 // This callback is invoked when the functions are created the first
634 // time. Hook them into the global ctors list in that case:
635 [&](Function *Ctor, FunctionCallee) { appendToGlobalCtors(M, Ctor, 0); });
636
637 NumericalStabilitySanitizer Nsan(M);
638 auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
639 for (Function &F : M)
640 Nsan.sanitizeFunction(F, FAM.getResult<TargetLibraryAnalysis>(F));
641
643}
644
645static GlobalValue *createThreadLocalGV(const char *Name, Module &M, Type *Ty) {
646 return M.getOrInsertGlobal(Name, Ty, [&M, Ty, Name] {
647 return new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage,
648 nullptr, Name, nullptr,
650 });
651}
652
653NumericalStabilitySanitizer::NumericalStabilitySanitizer(Module &M)
654 : DL(M.getDataLayout()), Context(M.getContext()), Config(Context),
655 NsanCopyFns(M, {"__nsan_copy_4", "__nsan_copy_8", "__nsan_copy_16"},
656 "__nsan_copy_values", /*NumArgs=*/3),
657 NsanSetUnknownFns(M,
658 {"__nsan_set_value_unknown_4",
659 "__nsan_set_value_unknown_8",
660 "__nsan_set_value_unknown_16"},
661 "__nsan_set_value_unknown", /*NumArgs=*/2) {
662 IntptrTy = DL.getIntPtrType(Context);
663 Type *PtrTy = PointerType::getUnqual(Context);
664 Type *Int32Ty = Type::getInt32Ty(Context);
665 Type *Int1Ty = Type::getInt1Ty(Context);
666 Type *VoidTy = Type::getVoidTy(Context);
667
668 AttributeList Attr;
669 Attr = Attr.addFnAttribute(Context, Attribute::NoUnwind);
670 // Initialize the runtime values (functions and global variables).
671 for (int I = 0; I < kNumValueTypes; ++I) {
672 const FTValueType VT = static_cast<FTValueType>(I);
673 const char *VTName = typeNameFromFTValueType(VT);
674 Type *VTTy = typeFromFTValueType(VT, Context);
675
676 // Load/store.
677 const std::string GetterPrefix =
678 std::string("__nsan_get_shadow_ptr_for_") + VTName;
679 NsanGetShadowPtrForStore[VT] = M.getOrInsertFunction(
680 GetterPrefix + "_store", Attr, PtrTy, PtrTy, IntptrTy);
681 NsanGetShadowPtrForLoad[VT] = M.getOrInsertFunction(
682 GetterPrefix + "_load", Attr, PtrTy, PtrTy, IntptrTy);
683
684 // Check.
685 const auto &ShadowConfig = Config.byValueType(VT);
686 Type *ShadowTy = ShadowConfig.getType(Context);
687 NsanCheckValue[VT] =
688 M.getOrInsertFunction(std::string("__nsan_internal_check_") + VTName +
689 "_" + ShadowConfig.getNsanTypeId(),
690 Attr, Int32Ty, VTTy, ShadowTy, Int32Ty, IntptrTy);
691 NsanFCmpFail[VT] = M.getOrInsertFunction(
692 std::string("__nsan_fcmp_fail_") + VTName + "_" +
693 ShadowConfig.getNsanTypeId(),
694 Attr, VoidTy, VTTy, VTTy, ShadowTy, ShadowTy, Int32Ty, Int1Ty, Int1Ty);
695 }
696
697 // TODO: Add attributes nofree, nosync, readnone, readonly,
698 NsanGetRawShadowTypePtr = M.getOrInsertFunction(
699 "__nsan_internal_get_raw_shadow_type_ptr", Attr, PtrTy, PtrTy);
700 NsanGetRawShadowPtr = M.getOrInsertFunction(
701 "__nsan_internal_get_raw_shadow_ptr", Attr, PtrTy, PtrTy);
702
703 NsanShadowRetTag = createThreadLocalGV("__nsan_shadow_ret_tag", M, IntptrTy);
704
705 NsanShadowRetType = ArrayType::get(Type::getInt8Ty(Context),
707 NsanShadowRetPtr =
708 createThreadLocalGV("__nsan_shadow_ret_ptr", M, NsanShadowRetType);
709
710 NsanShadowArgsTag =
711 createThreadLocalGV("__nsan_shadow_args_tag", M, IntptrTy);
712
713 NsanShadowArgsType =
716
717 NsanShadowArgsPtr =
718 createThreadLocalGV("__nsan_shadow_args_ptr", M, NsanShadowArgsType);
719
720 if (!ClCheckFunctionsFilter.empty()) {
722 std::string RegexError;
723 assert(R.isValid(RegexError));
724 CheckFunctionsFilter = std::move(R);
725 }
726}
727
728// Returns true if the given LLVM Value points to constant data (typically, a
729// global variable reference).
730bool NumericalStabilitySanitizer::addrPointsToConstantData(Value *Addr) {
731 // If this is a GEP, just analyze its pointer operand.
732 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
733 Addr = GEP->getPointerOperand();
734
735 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr))
736 return GV->isConstant();
737 return false;
738}
739
740// This instruments the function entry to create shadow arguments.
741// Pseudocode:
742// if (this_fn_ptr == __nsan_shadow_args_tag) {
743// s(arg0) = LOAD<sizeof(arg0)>(__nsan_shadow_args);
744// s(arg1) = LOAD<sizeof(arg1)>(__nsan_shadow_args + sizeof(arg0));
745// ...
746// __nsan_shadow_args_tag = 0;
747// } else {
748// s(arg0) = fext(arg0);
749// s(arg1) = fext(arg1);
750// ...
751// }
752void NumericalStabilitySanitizer::createShadowArguments(
753 Function &F, const TargetLibraryInfo &TLI, ValueToShadowMap &Map) {
754 assert(!F.getIntrinsicID() && "found a definition of an intrinsic");
755
756 // Do not bother if there are no FP args.
757 if (all_of(F.args(), [this](const Argument &Arg) {
758 return Config.getExtendedFPType(Arg.getType()) == nullptr;
759 }))
760 return;
761
762 IRBuilder<> Builder(&F.getEntryBlock(), F.getEntryBlock().getFirstNonPHIIt());
763 // The function has shadow args if the shadow args tag matches the function
764 // address.
765 Value *HasShadowArgs = Builder.CreateICmpEQ(
766 Builder.CreateLoad(IntptrTy, NsanShadowArgsTag, /*isVolatile=*/false),
767 Builder.CreatePtrToInt(&F, IntptrTy));
768
769 unsigned ShadowArgsOffsetBytes = 0;
770 for (Argument &Arg : F.args()) {
771 Type *VT = Arg.getType();
772 Type *ExtendedVT = Config.getExtendedFPType(VT);
773 if (ExtendedVT == nullptr)
774 continue; // Not an FT value.
775 Value *L = Builder.CreateAlignedLoad(
776 ExtendedVT,
777 Builder.CreateConstGEP2_64(NsanShadowArgsType, NsanShadowArgsPtr, 0,
778 ShadowArgsOffsetBytes),
779 Align(1), /*isVolatile=*/false);
780 Value *Shadow = Builder.CreateSelect(HasShadowArgs, L,
781 Builder.CreateFPExt(&Arg, ExtendedVT));
782 Map.setShadow(Arg, *Shadow);
783 TypeSize SlotSize = DL.getTypeStoreSize(ExtendedVT);
784 assert(!SlotSize.isScalable() && "unsupported");
785 ShadowArgsOffsetBytes += SlotSize;
786 }
787 Builder.CreateStore(ConstantInt::get(IntptrTy, 0), NsanShadowArgsTag);
788}
789
790// Returns true if the instrumentation should emit code to check arguments
791// before a function call.
792static bool shouldCheckArgs(CallBase &CI, const TargetLibraryInfo &TLI,
793 const std::optional<Regex> &CheckFunctionsFilter) {
794
795 Function *Fn = CI.getCalledFunction();
796
797 if (CheckFunctionsFilter) {
798 // Skip checking args of indirect calls.
799 if (Fn == nullptr)
800 return false;
801 if (CheckFunctionsFilter->match(Fn->getName()))
802 return true;
803 return false;
804 }
805
806 if (Fn == nullptr)
807 return true; // Always check args of indirect calls.
808
809 // Never check nsan functions, the user called them for a reason.
810 if (Fn->getName().starts_with("__nsan_"))
811 return false;
812
813 const auto ID = Fn->getIntrinsicID();
814 LibFunc LFunc = LibFunc::NotLibFunc;
815 // Always check args of unknown functions.
816 if (ID == Intrinsic::ID() && !TLI.getLibFunc(*Fn, LFunc))
817 return true;
818
819 // Do not check args of an `fabs` call that is used for a comparison.
820 // This is typically used for `fabs(a-b) < tolerance`, where what matters is
821 // the result of the comparison, which is already caught be the fcmp checks.
822 if (ID == Intrinsic::fabs || LFunc == LibFunc_fabsf ||
823 LFunc == LibFunc_fabs || LFunc == LibFunc_fabsl)
824 for (const auto &U : CI.users())
825 if (isa<CmpInst>(U))
826 return false;
827
828 return true; // Default is check.
829}
830
831// Populates the shadow call stack (which contains shadow values for every
832// floating-point parameter to the function).
833void NumericalStabilitySanitizer::populateShadowStack(
834 CallBase &CI, const TargetLibraryInfo &TLI, const ValueToShadowMap &Map) {
835 // Do not create a shadow stack for inline asm.
836 if (CI.isInlineAsm())
837 return;
838
839 // Do not bother if there are no FP args.
840 if (all_of(CI.operands(), [this](const Value *Arg) {
841 return Config.getExtendedFPType(Arg->getType()) == nullptr;
842 }))
843 return;
844
845 IRBuilder<> Builder(&CI);
846 SmallVector<Value *, 8> ArgShadows;
847 const bool ShouldCheckArgs = shouldCheckArgs(CI, TLI, CheckFunctionsFilter);
848 for (auto [ArgIdx, Arg] : enumerate(CI.operands())) {
849 if (Config.getExtendedFPType(Arg->getType()) == nullptr)
850 continue; // Not an FT value.
851 Value *ArgShadow = Map.getShadow(Arg);
852 ArgShadows.push_back(ShouldCheckArgs ? emitCheck(Arg, ArgShadow, Builder,
853 CheckLoc::makeArg(ArgIdx))
854 : ArgShadow);
855 }
856
857 // Do not create shadow stacks for intrinsics/known lib funcs.
858 if (Function *Fn = CI.getCalledFunction()) {
859 LibFunc LFunc;
860 if (Fn->isIntrinsic() || TLI.getLibFunc(*Fn, LFunc))
861 return;
862 }
863
864 // Set the shadow stack tag.
865 Builder.CreateStore(CI.getCalledOperand(), NsanShadowArgsTag);
866 TypeSize ShadowArgsOffsetBytes = TypeSize::getFixed(0);
867
868 unsigned ShadowArgId = 0;
869 for (const Value *Arg : CI.operands()) {
870 Type *VT = Arg->getType();
871 Type *ExtendedVT = Config.getExtendedFPType(VT);
872 if (ExtendedVT == nullptr)
873 continue; // Not an FT value.
874 Builder.CreateAlignedStore(
875 ArgShadows[ShadowArgId++],
876 Builder.CreateConstGEP2_64(NsanShadowArgsType, NsanShadowArgsPtr, 0,
877 ShadowArgsOffsetBytes),
878 Align(1), /*isVolatile=*/false);
879 TypeSize SlotSize = DL.getTypeStoreSize(ExtendedVT);
880 assert(!SlotSize.isScalable() && "unsupported");
881 ShadowArgsOffsetBytes += SlotSize;
882 }
883}
884
885// Internal part of emitCheck(). Returns a value that indicates whether
886// computation should continue with the shadow or resume by re-fextending the
887// value.
888enum class ContinuationType { // Keep in sync with runtime.
891};
892
893Value *NumericalStabilitySanitizer::emitCheckInternal(Value *V, Value *ShadowV,
894 IRBuilder<> &Builder,
895 CheckLoc Loc) {
896 // Do not emit checks for constant values, this is redundant.
897 if (isa<Constant>(V))
898 return ConstantInt::get(
899 Builder.getInt32Ty(),
900 static_cast<int>(ContinuationType::ContinueWithShadow));
901
902 Type *Ty = V->getType();
903 if (const auto VT = ftValueTypeFromType(Ty))
904 return Builder.CreateCall(
905 NsanCheckValue[*VT],
906 {V, ShadowV, Loc.getType(Context), Loc.getValue(IntptrTy, Builder)});
907
908 if (Ty->isVectorTy()) {
909 auto *VecTy = cast<VectorType>(Ty);
910 // We currently skip scalable vector types in MappingConfig,
911 // thus we should not encounter any such types here.
912 assert(!VecTy->isScalableTy() &&
913 "Scalable vector types are not supported yet");
914 Value *CheckResult = nullptr;
915 for (int I = 0, E = VecTy->getElementCount().getFixedValue(); I < E; ++I) {
916 // We resume if any element resumes. Another option would be to create a
917 // vector shuffle with the array of ContinueWithShadow, but that is too
918 // complex.
919 Value *ExtractV = Builder.CreateExtractElement(V, I);
920 Value *ExtractShadowV = Builder.CreateExtractElement(ShadowV, I);
921 Value *ComponentCheckResult =
922 emitCheckInternal(ExtractV, ExtractShadowV, Builder, Loc);
923 CheckResult = CheckResult
924 ? Builder.CreateOr(CheckResult, ComponentCheckResult)
925 : ComponentCheckResult;
926 }
927 return CheckResult;
928 }
929 if (Ty->isArrayTy()) {
930 Value *CheckResult = nullptr;
931 for (auto I : seq(Ty->getArrayNumElements())) {
932 Value *ExtractV = Builder.CreateExtractElement(V, I);
933 Value *ExtractShadowV = Builder.CreateExtractElement(ShadowV, I);
934 Value *ComponentCheckResult =
935 emitCheckInternal(ExtractV, ExtractShadowV, Builder, Loc);
936 CheckResult = CheckResult
937 ? Builder.CreateOr(CheckResult, ComponentCheckResult)
938 : ComponentCheckResult;
939 }
940 return CheckResult;
941 }
942 if (Ty->isStructTy()) {
943 Value *CheckResult = nullptr;
944 for (auto I : seq(Ty->getStructNumElements())) {
945 if (Config.getExtendedFPType(Ty->getStructElementType(I)) == nullptr)
946 continue; // Only check FT values.
947 Value *ExtractV = Builder.CreateExtractValue(V, I);
948 Value *ExtractShadowV = Builder.CreateExtractElement(ShadowV, I);
949 Value *ComponentCheckResult =
950 emitCheckInternal(ExtractV, ExtractShadowV, Builder, Loc);
951 CheckResult = CheckResult
952 ? Builder.CreateOr(CheckResult, ComponentCheckResult)
953 : ComponentCheckResult;
954 }
955 if (!CheckResult)
956 return ConstantInt::get(
957 Builder.getInt32Ty(),
958 static_cast<int>(ContinuationType::ContinueWithShadow));
959 return CheckResult;
960 }
961
962 llvm_unreachable("not implemented");
963}
964
965// Inserts a runtime check of V against its shadow value ShadowV.
966// We check values whenever they escape: on return, call, stores, and
967// insertvalue.
968// Returns the shadow value that should be used to continue the computations,
969// depending on the answer from the runtime.
970// TODO: Should we check on select ? phi ?
971Value *NumericalStabilitySanitizer::emitCheck(Value *V, Value *ShadowV,
972 IRBuilder<> &Builder,
973 CheckLoc Loc) {
974 // Do not emit checks for constant values, this is redundant.
975 if (isa<Constant>(V))
976 return ShadowV;
977
978 if (Instruction *Inst = dyn_cast<Instruction>(V)) {
979 Function *F = Inst->getFunction();
980 if (CheckFunctionsFilter && !CheckFunctionsFilter->match(F->getName())) {
981 return ShadowV;
982 }
983 }
984
985 Value *CheckResult = emitCheckInternal(V, ShadowV, Builder, Loc);
986 Value *ICmpEQ = Builder.CreateICmpEQ(
987 CheckResult,
988 ConstantInt::get(Builder.getInt32Ty(),
989 static_cast<int>(ContinuationType::ResumeFromValue)));
990 return Builder.CreateSelect(
991 ICmpEQ, Builder.CreateFPExt(V, Config.getExtendedFPType(V->getType())),
992 ShadowV);
993}
994
995// Inserts a check that fcmp on shadow values are consistent with that on base
996// values.
997void NumericalStabilitySanitizer::emitFCmpCheck(FCmpInst &FCmp,
998 const ValueToShadowMap &Map) {
999 if (!ClInstrumentFCmp)
1000 return;
1001
1002 Function *F = FCmp.getFunction();
1003 if (CheckFunctionsFilter && !CheckFunctionsFilter->match(F->getName()))
1004 return;
1005
1006 Value *LHS = FCmp.getOperand(0);
1007 if (Config.getExtendedFPType(LHS->getType()) == nullptr)
1008 return;
1009 Value *RHS = FCmp.getOperand(1);
1010
1011 // Split the basic block. On mismatch, we'll jump to the new basic block with
1012 // a call to the runtime for error reporting.
1013 BasicBlock *FCmpBB = FCmp.getParent();
1014 BasicBlock *NextBB = FCmpBB->splitBasicBlock(FCmp.getNextNode());
1015 // Remove the newly created terminator unconditional branch.
1016 FCmpBB->back().eraseFromParent();
1017 BasicBlock *FailBB =
1018 BasicBlock::Create(Context, "", FCmpBB->getParent(), NextBB);
1019
1020 // Create the shadow fcmp and comparison between the fcmps.
1021 IRBuilder<> FCmpBuilder(FCmpBB);
1022 FCmpBuilder.SetCurrentDebugLocation(FCmp.getDebugLoc());
1023 Value *ShadowLHS = Map.getShadow(LHS);
1024 Value *ShadowRHS = Map.getShadow(RHS);
1025 // See comment on ClTruncateFCmpEq.
1026 if (FCmp.isEquality() && ClTruncateFCmpEq) {
1027 Type *Ty = ShadowLHS->getType();
1028 ShadowLHS = FCmpBuilder.CreateFPExt(
1029 FCmpBuilder.CreateFPTrunc(ShadowLHS, LHS->getType()), Ty);
1030 ShadowRHS = FCmpBuilder.CreateFPExt(
1031 FCmpBuilder.CreateFPTrunc(ShadowRHS, RHS->getType()), Ty);
1032 }
1033 Value *ShadowFCmp =
1034 FCmpBuilder.CreateFCmp(FCmp.getPredicate(), ShadowLHS, ShadowRHS);
1035 Value *OriginalAndShadowFcmpMatch =
1036 FCmpBuilder.CreateICmpEQ(&FCmp, ShadowFCmp);
1037
1038 if (OriginalAndShadowFcmpMatch->getType()->isVectorTy()) {
1039 // If we have a vector type, `OriginalAndShadowFcmpMatch` is a vector of i1,
1040 // where an element is true if the corresponding elements in original and
1041 // shadow are the same. We want all elements to be 1.
1042 OriginalAndShadowFcmpMatch =
1043 FCmpBuilder.CreateAndReduce(OriginalAndShadowFcmpMatch);
1044 }
1045
1046 // Use MDBuilder(*C).createLikelyBranchWeights() because "match" is the common
1047 // case.
1048 FCmpBuilder.CreateCondBr(OriginalAndShadowFcmpMatch, NextBB, FailBB,
1049 MDBuilder(Context).createLikelyBranchWeights());
1050
1051 // Fill in FailBB.
1052 IRBuilder<> FailBuilder(FailBB);
1053 FailBuilder.SetCurrentDebugLocation(FCmp.getDebugLoc());
1054
1055 const auto EmitFailCall = [this, &FCmp, &FCmpBuilder,
1056 &FailBuilder](Value *L, Value *R, Value *ShadowL,
1057 Value *ShadowR, Value *Result,
1058 Value *ShadowResult) {
1059 Type *FT = L->getType();
1060 FunctionCallee *Callee = nullptr;
1061 if (FT->isFloatTy()) {
1062 Callee = &(NsanFCmpFail[kFloat]);
1063 } else if (FT->isDoubleTy()) {
1064 Callee = &(NsanFCmpFail[kDouble]);
1065 } else if (FT->isX86_FP80Ty()) {
1066 // TODO: make NsanFCmpFailLongDouble work.
1067 Callee = &(NsanFCmpFail[kDouble]);
1068 L = FailBuilder.CreateFPTrunc(L, Type::getDoubleTy(Context));
1069 R = FailBuilder.CreateFPTrunc(L, Type::getDoubleTy(Context));
1070 } else {
1071 llvm_unreachable("not implemented");
1072 }
1073 FailBuilder.CreateCall(*Callee, {L, R, ShadowL, ShadowR,
1074 ConstantInt::get(FCmpBuilder.getInt32Ty(),
1075 FCmp.getPredicate()),
1076 Result, ShadowResult});
1077 };
1078 if (LHS->getType()->isVectorTy()) {
1079 for (int I = 0, E = cast<VectorType>(LHS->getType())
1080 ->getElementCount()
1081 .getFixedValue();
1082 I < E; ++I) {
1083 Value *ExtractLHS = FailBuilder.CreateExtractElement(LHS, I);
1084 Value *ExtractRHS = FailBuilder.CreateExtractElement(RHS, I);
1085 Value *ExtractShaodwLHS = FailBuilder.CreateExtractElement(ShadowLHS, I);
1086 Value *ExtractShaodwRHS = FailBuilder.CreateExtractElement(ShadowRHS, I);
1087 Value *ExtractFCmp = FailBuilder.CreateExtractElement(&FCmp, I);
1088 Value *ExtractShadowFCmp =
1089 FailBuilder.CreateExtractElement(ShadowFCmp, I);
1090 EmitFailCall(ExtractLHS, ExtractRHS, ExtractShaodwLHS, ExtractShaodwRHS,
1091 ExtractFCmp, ExtractShadowFCmp);
1092 }
1093 } else {
1094 EmitFailCall(LHS, RHS, ShadowLHS, ShadowRHS, &FCmp, ShadowFCmp);
1095 }
1096 FailBuilder.CreateBr(NextBB);
1097
1098 ++NumInstrumentedFCmp;
1099}
1100
1101// Creates a shadow phi value for any phi that defines a value of FT type.
1102PHINode *NumericalStabilitySanitizer::maybeCreateShadowPhi(
1103 PHINode &Phi, const TargetLibraryInfo &TLI) {
1104 Type *VT = Phi.getType();
1105 Type *ExtendedVT = Config.getExtendedFPType(VT);
1106 if (ExtendedVT == nullptr)
1107 return nullptr; // Not an FT value.
1108 // The phi operands are shadow values and are not available when the phi is
1109 // created. They will be populated in a final phase, once all shadow values
1110 // have been created.
1111 PHINode *Shadow = PHINode::Create(ExtendedVT, Phi.getNumIncomingValues());
1112 Shadow->insertAfter(Phi.getIterator());
1113 return Shadow;
1114}
1115
1116Value *NumericalStabilitySanitizer::handleLoad(LoadInst &Load, Type *VT,
1117 Type *ExtendedVT) {
1118 IRBuilder<> Builder(Load.getNextNode());
1119 Builder.SetCurrentDebugLocation(Load.getDebugLoc());
1120 if (addrPointsToConstantData(Load.getPointerOperand())) {
1121 // No need to look into the shadow memory, the value is a constant. Just
1122 // convert from FT to 2FT.
1123 return Builder.CreateFPExt(&Load, ExtendedVT);
1124 }
1125
1126 // if (%shadowptr == &)
1127 // %shadow = fpext %v
1128 // else
1129 // %shadow = load (ptrcast %shadow_ptr))
1130 // Considered options here:
1131 // - Have `NsanGetShadowPtrForLoad` return a fixed address
1132 // &__nsan_unknown_value_shadow_address that is valid to load from, and
1133 // use a select. This has the advantage that the generated IR is simpler.
1134 // - Have `NsanGetShadowPtrForLoad` return nullptr. Because `select` does
1135 // not short-circuit, dereferencing the returned pointer is no longer an
1136 // option, have to split and create a separate basic block. This has the
1137 // advantage of being easier to debug because it crashes if we ever mess
1138 // up.
1139
1140 const auto Extents = getMemoryExtentsOrDie(VT);
1141 Value *ShadowPtr = Builder.CreateCall(
1142 NsanGetShadowPtrForLoad[Extents.ValueType],
1143 {Load.getPointerOperand(), ConstantInt::get(IntptrTy, Extents.NumElts)});
1144 ++NumInstrumentedFTLoads;
1145
1146 // Split the basic block.
1147 BasicBlock *LoadBB = Load.getParent();
1148 BasicBlock *NextBB = LoadBB->splitBasicBlock(Builder.GetInsertPoint());
1149 // Create the two options for creating the shadow value.
1150 BasicBlock *ShadowLoadBB =
1151 BasicBlock::Create(Context, "", LoadBB->getParent(), NextBB);
1152 BasicBlock *FExtBB =
1153 BasicBlock::Create(Context, "", LoadBB->getParent(), NextBB);
1154
1155 // Replace the newly created terminator unconditional branch by a conditional
1156 // branch to one of the options.
1157 {
1158 LoadBB->back().eraseFromParent();
1159 IRBuilder<> LoadBBBuilder(LoadBB); // The old builder has been invalidated.
1160 LoadBBBuilder.SetCurrentDebugLocation(Load.getDebugLoc());
1161 LoadBBBuilder.CreateCondBr(LoadBBBuilder.CreateIsNull(ShadowPtr), FExtBB,
1162 ShadowLoadBB);
1163 }
1164
1165 // Fill in ShadowLoadBB.
1166 IRBuilder<> ShadowLoadBBBuilder(ShadowLoadBB);
1167 ShadowLoadBBBuilder.SetCurrentDebugLocation(Load.getDebugLoc());
1168 Value *ShadowLoad = ShadowLoadBBBuilder.CreateAlignedLoad(
1169 ExtendedVT, ShadowPtr, Align(1), Load.isVolatile());
1170 if (ClCheckLoads) {
1171 ShadowLoad = emitCheck(&Load, ShadowLoad, ShadowLoadBBBuilder,
1172 CheckLoc::makeLoad(Load.getPointerOperand()));
1173 }
1174 ShadowLoadBBBuilder.CreateBr(NextBB);
1175
1176 // Fill in FExtBB.
1177 IRBuilder<> FExtBBBuilder(FExtBB);
1178 FExtBBBuilder.SetCurrentDebugLocation(Load.getDebugLoc());
1179 Value *FExt = FExtBBBuilder.CreateFPExt(&Load, ExtendedVT);
1180 FExtBBBuilder.CreateBr(NextBB);
1181
1182 // The shadow value come from any of the options.
1183 IRBuilder<> NextBBBuilder(&*NextBB->begin());
1184 NextBBBuilder.SetCurrentDebugLocation(Load.getDebugLoc());
1185 PHINode *ShadowPhi = NextBBBuilder.CreatePHI(ExtendedVT, 2);
1186 ShadowPhi->addIncoming(ShadowLoad, ShadowLoadBB);
1187 ShadowPhi->addIncoming(FExt, FExtBB);
1188 return ShadowPhi;
1189}
1190
1191Value *NumericalStabilitySanitizer::handleTrunc(const FPTruncInst &Trunc,
1192 Type *VT, Type *ExtendedVT,
1193 const ValueToShadowMap &Map,
1194 IRBuilder<> &Builder) {
1195 Value *OrigSource = Trunc.getOperand(0);
1196 Type *OrigSourceTy = OrigSource->getType();
1197 Type *ExtendedSourceTy = Config.getExtendedFPType(OrigSourceTy);
1198
1199 // When truncating:
1200 // - (A) If the source has a shadow, we truncate from the shadow, else we
1201 // truncate from the original source.
1202 // - (B) If the shadow of the source is larger than the shadow of the dest,
1203 // we still need a truncate. Else, the shadow of the source is the same
1204 // type as the shadow of the dest (because mappings are non-decreasing), so
1205 // we don't need to emit a truncate.
1206 // Examples,
1207 // with a mapping of {f32->f64;f64->f80;f80->f128}
1208 // fptrunc double %1 to float -> fptrunc x86_fp80 s(%1) to double
1209 // fptrunc x86_fp80 %1 to float -> fptrunc fp128 s(%1) to double
1210 // fptrunc fp128 %1 to float -> fptrunc fp128 %1 to double
1211 // fptrunc x86_fp80 %1 to double -> x86_fp80 s(%1)
1212 // fptrunc fp128 %1 to double -> fptrunc fp128 %1 to x86_fp80
1213 // fptrunc fp128 %1 to x86_fp80 -> fp128 %1
1214 // with a mapping of {f32->f64;f64->f128;f80->f128}
1215 // fptrunc double %1 to float -> fptrunc fp128 s(%1) to double
1216 // fptrunc x86_fp80 %1 to float -> fptrunc fp128 s(%1) to double
1217 // fptrunc fp128 %1 to float -> fptrunc fp128 %1 to double
1218 // fptrunc x86_fp80 %1 to double -> fp128 %1
1219 // fptrunc fp128 %1 to double -> fp128 %1
1220 // fptrunc fp128 %1 to x86_fp80 -> fp128 %1
1221 // with a mapping of {f32->f32;f64->f32;f80->f64}
1222 // fptrunc double %1 to float -> float s(%1)
1223 // fptrunc x86_fp80 %1 to float -> fptrunc double s(%1) to float
1224 // fptrunc fp128 %1 to float -> fptrunc fp128 %1 to float
1225 // fptrunc x86_fp80 %1 to double -> fptrunc double s(%1) to float
1226 // fptrunc fp128 %1 to double -> fptrunc fp128 %1 to float
1227 // fptrunc fp128 %1 to x86_fp80 -> fptrunc fp128 %1 to double
1228
1229 // See (A) above.
1230 Value *Source = ExtendedSourceTy ? Map.getShadow(OrigSource) : OrigSource;
1231 Type *SourceTy = ExtendedSourceTy ? ExtendedSourceTy : OrigSourceTy;
1232 // See (B) above.
1233 if (SourceTy == ExtendedVT)
1234 return Source;
1235
1236 return Builder.CreateFPTrunc(Source, ExtendedVT);
1237}
1238
1239Value *NumericalStabilitySanitizer::handleExt(const FPExtInst &Ext, Type *VT,
1240 Type *ExtendedVT,
1241 const ValueToShadowMap &Map,
1242 IRBuilder<> &Builder) {
1243 Value *OrigSource = Ext.getOperand(0);
1244 Type *OrigSourceTy = OrigSource->getType();
1245 Type *ExtendedSourceTy = Config.getExtendedFPType(OrigSourceTy);
1246 // When extending:
1247 // - (A) If the source has a shadow, we extend from the shadow, else we
1248 // extend from the original source.
1249 // - (B) If the shadow of the dest is larger than the shadow of the source,
1250 // we still need an extend. Else, the shadow of the source is the same
1251 // type as the shadow of the dest (because mappings are non-decreasing), so
1252 // we don't need to emit an extend.
1253 // Examples,
1254 // with a mapping of {f32->f64;f64->f80;f80->f128}
1255 // fpext half %1 to float -> fpext half %1 to double
1256 // fpext half %1 to double -> fpext half %1 to x86_fp80
1257 // fpext half %1 to x86_fp80 -> fpext half %1 to fp128
1258 // fpext float %1 to double -> double s(%1)
1259 // fpext float %1 to x86_fp80 -> fpext double s(%1) to fp128
1260 // fpext double %1 to x86_fp80 -> fpext x86_fp80 s(%1) to fp128
1261 // with a mapping of {f32->f64;f64->f128;f80->f128}
1262 // fpext half %1 to float -> fpext half %1 to double
1263 // fpext half %1 to double -> fpext half %1 to fp128
1264 // fpext half %1 to x86_fp80 -> fpext half %1 to fp128
1265 // fpext float %1 to double -> fpext double s(%1) to fp128
1266 // fpext float %1 to x86_fp80 -> fpext double s(%1) to fp128
1267 // fpext double %1 to x86_fp80 -> fp128 s(%1)
1268 // with a mapping of {f32->f32;f64->f32;f80->f64}
1269 // fpext half %1 to float -> fpext half %1 to float
1270 // fpext half %1 to double -> fpext half %1 to float
1271 // fpext half %1 to x86_fp80 -> fpext half %1 to double
1272 // fpext float %1 to double -> s(%1)
1273 // fpext float %1 to x86_fp80 -> fpext float s(%1) to double
1274 // fpext double %1 to x86_fp80 -> fpext float s(%1) to double
1275
1276 // See (A) above.
1277 Value *Source = ExtendedSourceTy ? Map.getShadow(OrigSource) : OrigSource;
1278 Type *SourceTy = ExtendedSourceTy ? ExtendedSourceTy : OrigSourceTy;
1279 // See (B) above.
1280 if (SourceTy == ExtendedVT)
1281 return Source;
1282
1283 return Builder.CreateFPExt(Source, ExtendedVT);
1284}
1285
1286namespace {
1287// TODO: This should be tablegen-ed.
1288struct KnownIntrinsic {
1289 struct WidenedIntrinsic {
1290 const char *NarrowName;
1291 Intrinsic::ID ID; // wide id.
1292 using FnTypeFactory = FunctionType *(*)(LLVMContext &);
1293 FnTypeFactory MakeFnTy;
1294 };
1295
1296 static const char *get(LibFunc LFunc);
1297
1298 // Given an intrinsic with an `FT` argument, try to find a wider intrinsic
1299 // that applies the same operation on the shadow argument.
1300 // Options are:
1301 // - pass in the ID and full function type,
1302 // - pass in the name, which includes the function type through mangling.
1303 static const WidenedIntrinsic *widen(StringRef Name);
1304
1305private:
1306 struct LFEntry {
1307 LibFunc LFunc;
1308 const char *IntrinsicName;
1309 };
1310 static const LFEntry kLibfuncIntrinsics[];
1311
1312 static const WidenedIntrinsic kWidenedIntrinsics[];
1313};
1314} // namespace
1315
1319
1324
1329
1335
1340
1346
1353
1360
1361const KnownIntrinsic::WidenedIntrinsic KnownIntrinsic::kWidenedIntrinsics[] = {
1362 // TODO: Right now we ignore vector intrinsics.
1363 // This is hard because we have to model the semantics of the intrinsics,
1364 // e.g. llvm.x86.sse2.min.sd means extract first element, min, insert back.
1365 // Intrinsics that take any non-vector FT types:
1366 // NOTE: Right now because of
1367 // https://github.com/llvm/llvm-project/issues/44744
1368 // for f128 we need to use makeX86FP80X86FP80 (go to a lower precision and
1369 // come back).
1370 {"llvm.sqrt.f32", Intrinsic::sqrt, makeDoubleDouble},
1371 {"llvm.sqrt.f64", Intrinsic::sqrt, makeX86FP80X86FP80},
1372 {"llvm.sqrt.f80", Intrinsic::sqrt, makeX86FP80X86FP80},
1373 {"llvm.powi.f32", Intrinsic::powi, makeDoubleDoubleI32},
1374 {"llvm.powi.f64", Intrinsic::powi, makeX86FP80X86FP80I32},
1375 {"llvm.powi.f80", Intrinsic::powi, makeX86FP80X86FP80I32},
1376 {"llvm.sin.f32", Intrinsic::sin, makeDoubleDouble},
1377 {"llvm.sin.f64", Intrinsic::sin, makeX86FP80X86FP80},
1378 {"llvm.sin.f80", Intrinsic::sin, makeX86FP80X86FP80},
1379 {"llvm.cos.f32", Intrinsic::cos, makeDoubleDouble},
1380 {"llvm.cos.f64", Intrinsic::cos, makeX86FP80X86FP80},
1381 {"llvm.cos.f80", Intrinsic::cos, makeX86FP80X86FP80},
1382 {"llvm.pow.f32", Intrinsic::pow, makeDoubleDoubleDouble},
1383 {"llvm.pow.f64", Intrinsic::pow, makeX86FP80X86FP80X86FP80},
1384 {"llvm.pow.f80", Intrinsic::pow, makeX86FP80X86FP80X86FP80},
1385 {"llvm.exp.f32", Intrinsic::exp, makeDoubleDouble},
1386 {"llvm.exp.f64", Intrinsic::exp, makeX86FP80X86FP80},
1387 {"llvm.exp.f80", Intrinsic::exp, makeX86FP80X86FP80},
1388 {"llvm.exp2.f32", Intrinsic::exp2, makeDoubleDouble},
1389 {"llvm.exp2.f64", Intrinsic::exp2, makeX86FP80X86FP80},
1390 {"llvm.exp2.f80", Intrinsic::exp2, makeX86FP80X86FP80},
1391 {"llvm.log.f32", Intrinsic::log, makeDoubleDouble},
1392 {"llvm.log.f64", Intrinsic::log, makeX86FP80X86FP80},
1393 {"llvm.log.f80", Intrinsic::log, makeX86FP80X86FP80},
1394 {"llvm.log10.f32", Intrinsic::log10, makeDoubleDouble},
1395 {"llvm.log10.f64", Intrinsic::log10, makeX86FP80X86FP80},
1396 {"llvm.log10.f80", Intrinsic::log10, makeX86FP80X86FP80},
1397 {"llvm.log2.f32", Intrinsic::log2, makeDoubleDouble},
1398 {"llvm.log2.f64", Intrinsic::log2, makeX86FP80X86FP80},
1399 {"llvm.log2.f80", Intrinsic::log2, makeX86FP80X86FP80},
1400 {"llvm.fma.f32", Intrinsic::fma, makeDoubleDoubleDoubleDouble},
1401 {"llvm.fma.f64", Intrinsic::fma, makeX86FP80X86FP80X86FP80X86FP80},
1402 {"llvm.fma.f80", Intrinsic::fma, makeX86FP80X86FP80X86FP80X86FP80},
1403 {"llvm.fmuladd.f32", Intrinsic::fmuladd, makeDoubleDoubleDoubleDouble},
1404 {"llvm.fmuladd.f64", Intrinsic::fmuladd, makeX86FP80X86FP80X86FP80X86FP80},
1405 {"llvm.fmuladd.f80", Intrinsic::fmuladd, makeX86FP80X86FP80X86FP80X86FP80},
1406 {"llvm.fabs.f32", Intrinsic::fabs, makeDoubleDouble},
1407 {"llvm.fabs.f64", Intrinsic::fabs, makeX86FP80X86FP80},
1408 {"llvm.fabs.f80", Intrinsic::fabs, makeX86FP80X86FP80},
1409 {"llvm.minnum.f32", Intrinsic::minnum, makeDoubleDoubleDouble},
1410 {"llvm.minnum.f64", Intrinsic::minnum, makeX86FP80X86FP80X86FP80},
1411 {"llvm.minnum.f80", Intrinsic::minnum, makeX86FP80X86FP80X86FP80},
1412 {"llvm.maxnum.f32", Intrinsic::maxnum, makeDoubleDoubleDouble},
1413 {"llvm.maxnum.f64", Intrinsic::maxnum, makeX86FP80X86FP80X86FP80},
1414 {"llvm.maxnum.f80", Intrinsic::maxnum, makeX86FP80X86FP80X86FP80},
1415 {"llvm.minimum.f32", Intrinsic::minimum, makeDoubleDoubleDouble},
1416 {"llvm.minimum.f64", Intrinsic::minimum, makeX86FP80X86FP80X86FP80},
1417 {"llvm.minimum.f80", Intrinsic::minimum, makeX86FP80X86FP80X86FP80},
1418 {"llvm.maximum.f32", Intrinsic::maximum, makeDoubleDoubleDouble},
1419 {"llvm.maximum.f64", Intrinsic::maximum, makeX86FP80X86FP80X86FP80},
1420 {"llvm.maximum.f80", Intrinsic::maximum, makeX86FP80X86FP80X86FP80},
1421 {"llvm.minimumnum.f32", Intrinsic::minimumnum, makeDoubleDoubleDouble},
1422 {"llvm.minimumnum.f64", Intrinsic::minimumnum, makeX86FP80X86FP80X86FP80},
1423 {"llvm.minimumnum.f80", Intrinsic::minimumnum, makeX86FP80X86FP80X86FP80},
1424 {"llvm.maximumnum.f32", Intrinsic::maximumnum, makeDoubleDoubleDouble},
1425 {"llvm.maximumnum.f64", Intrinsic::maximumnum, makeX86FP80X86FP80X86FP80},
1426 {"llvm.maximumnum.f80", Intrinsic::maximumnum, makeX86FP80X86FP80X86FP80},
1427 {"llvm.copysign.f32", Intrinsic::copysign, makeDoubleDoubleDouble},
1428 {"llvm.copysign.f64", Intrinsic::copysign, makeX86FP80X86FP80X86FP80},
1429 {"llvm.copysign.f80", Intrinsic::copysign, makeX86FP80X86FP80X86FP80},
1430 {"llvm.floor.f32", Intrinsic::floor, makeDoubleDouble},
1431 {"llvm.floor.f64", Intrinsic::floor, makeX86FP80X86FP80},
1432 {"llvm.floor.f80", Intrinsic::floor, makeX86FP80X86FP80},
1433 {"llvm.ceil.f32", Intrinsic::ceil, makeDoubleDouble},
1434 {"llvm.ceil.f64", Intrinsic::ceil, makeX86FP80X86FP80},
1435 {"llvm.ceil.f80", Intrinsic::ceil, makeX86FP80X86FP80},
1436 {"llvm.trunc.f32", Intrinsic::trunc, makeDoubleDouble},
1437 {"llvm.trunc.f64", Intrinsic::trunc, makeX86FP80X86FP80},
1438 {"llvm.trunc.f80", Intrinsic::trunc, makeX86FP80X86FP80},
1439 {"llvm.rint.f32", Intrinsic::rint, makeDoubleDouble},
1440 {"llvm.rint.f64", Intrinsic::rint, makeX86FP80X86FP80},
1441 {"llvm.rint.f80", Intrinsic::rint, makeX86FP80X86FP80},
1442 {"llvm.nearbyint.f32", Intrinsic::nearbyint, makeDoubleDouble},
1443 {"llvm.nearbyint.f64", Intrinsic::nearbyint, makeX86FP80X86FP80},
1444 {"llvm.nearbyint.f80", Intrinsic::nearbyint, makeX86FP80X86FP80},
1445 {"llvm.round.f32", Intrinsic::round, makeDoubleDouble},
1446 {"llvm.round.f64", Intrinsic::round, makeX86FP80X86FP80},
1447 {"llvm.round.f80", Intrinsic::round, makeX86FP80X86FP80},
1448};
1449
1450const KnownIntrinsic::LFEntry KnownIntrinsic::kLibfuncIntrinsics[] = {
1451 {LibFunc_sqrtf, "llvm.sqrt.f32"},
1452 {LibFunc_sqrt, "llvm.sqrt.f64"},
1453 {LibFunc_sqrtl, "llvm.sqrt.f80"},
1454 {LibFunc_sinf, "llvm.sin.f32"},
1455 {LibFunc_sin, "llvm.sin.f64"},
1456 {LibFunc_sinl, "llvm.sin.f80"},
1457 {LibFunc_cosf, "llvm.cos.f32"},
1458 {LibFunc_cos, "llvm.cos.f64"},
1459 {LibFunc_cosl, "llvm.cos.f80"},
1460 {LibFunc_powf, "llvm.pow.f32"},
1461 {LibFunc_pow, "llvm.pow.f64"},
1462 {LibFunc_powl, "llvm.pow.f80"},
1463 {LibFunc_expf, "llvm.exp.f32"},
1464 {LibFunc_exp, "llvm.exp.f64"},
1465 {LibFunc_expl, "llvm.exp.f80"},
1466 {LibFunc_exp2f, "llvm.exp2.f32"},
1467 {LibFunc_exp2, "llvm.exp2.f64"},
1468 {LibFunc_exp2l, "llvm.exp2.f80"},
1469 {LibFunc_logf, "llvm.log.f32"},
1470 {LibFunc_log, "llvm.log.f64"},
1471 {LibFunc_logl, "llvm.log.f80"},
1472 {LibFunc_log10f, "llvm.log10.f32"},
1473 {LibFunc_log10, "llvm.log10.f64"},
1474 {LibFunc_log10l, "llvm.log10.f80"},
1475 {LibFunc_log2f, "llvm.log2.f32"},
1476 {LibFunc_log2, "llvm.log2.f64"},
1477 {LibFunc_log2l, "llvm.log2.f80"},
1478 {LibFunc_fabsf, "llvm.fabs.f32"},
1479 {LibFunc_fabs, "llvm.fabs.f64"},
1480 {LibFunc_fabsl, "llvm.fabs.f80"},
1481 {LibFunc_copysignf, "llvm.copysign.f32"},
1482 {LibFunc_copysign, "llvm.copysign.f64"},
1483 {LibFunc_copysignl, "llvm.copysign.f80"},
1484 {LibFunc_floorf, "llvm.floor.f32"},
1485 {LibFunc_floor, "llvm.floor.f64"},
1486 {LibFunc_floorl, "llvm.floor.f80"},
1487 {LibFunc_fmaxf, "llvm.maxnum.f32"},
1488 {LibFunc_fmax, "llvm.maxnum.f64"},
1489 {LibFunc_fmaxl, "llvm.maxnum.f80"},
1490 {LibFunc_fminf, "llvm.minnum.f32"},
1491 {LibFunc_fmin, "llvm.minnum.f64"},
1492 {LibFunc_fminl, "llvm.minnum.f80"},
1493 {LibFunc_fmaximum_numf, "llvm.maximumnum.f32"},
1494 {LibFunc_fmaximum_num, "llvm.maximumnum.f64"},
1495 {LibFunc_fmaximum_numl, "llvm.maximumnum.f80"},
1496 {LibFunc_fminimum_numf, "llvm.minimumnum.f32"},
1497 {LibFunc_fminimum_num, "llvm.minimumnum.f64"},
1498 {LibFunc_fminimum_numl, "llvm.minimumnum.f80"},
1499 {LibFunc_ceilf, "llvm.ceil.f32"},
1500 {LibFunc_ceil, "llvm.ceil.f64"},
1501 {LibFunc_ceill, "llvm.ceil.f80"},
1502 {LibFunc_truncf, "llvm.trunc.f32"},
1503 {LibFunc_trunc, "llvm.trunc.f64"},
1504 {LibFunc_truncl, "llvm.trunc.f80"},
1505 {LibFunc_rintf, "llvm.rint.f32"},
1506 {LibFunc_rint, "llvm.rint.f64"},
1507 {LibFunc_rintl, "llvm.rint.f80"},
1508 {LibFunc_nearbyintf, "llvm.nearbyint.f32"},
1509 {LibFunc_nearbyint, "llvm.nearbyint.f64"},
1510 {LibFunc_nearbyintl, "llvm.nearbyint.f80"},
1511 {LibFunc_roundf, "llvm.round.f32"},
1512 {LibFunc_round, "llvm.round.f64"},
1513 {LibFunc_roundl, "llvm.round.f80"},
1514};
1515
1516const char *KnownIntrinsic::get(LibFunc LFunc) {
1517 for (const auto &E : kLibfuncIntrinsics) {
1518 if (E.LFunc == LFunc)
1519 return E.IntrinsicName;
1520 }
1521 return nullptr;
1522}
1523
1524const KnownIntrinsic::WidenedIntrinsic *KnownIntrinsic::widen(StringRef Name) {
1525 for (const auto &E : kWidenedIntrinsics) {
1526 if (E.NarrowName == Name)
1527 return &E;
1528 }
1529 return nullptr;
1530}
1531
1532// Returns the name of the LLVM intrinsic corresponding to the given function.
1533static const char *getIntrinsicFromLibfunc(Function &Fn, Type *VT,
1534 const TargetLibraryInfo &TLI) {
1535 LibFunc LFunc;
1536 if (!TLI.getLibFunc(Fn, LFunc))
1537 return nullptr;
1538
1539 if (const char *Name = KnownIntrinsic::get(LFunc))
1540 return Name;
1541
1542 LLVM_DEBUG(errs() << "TODO: LibFunc: " << TLI.getName(LFunc) << "\n");
1543 return nullptr;
1544}
1545
1546// Try to handle a known function call.
1547Value *NumericalStabilitySanitizer::maybeHandleKnownCallBase(
1548 CallBase &Call, Type *VT, Type *ExtendedVT, const TargetLibraryInfo &TLI,
1549 const ValueToShadowMap &Map, IRBuilder<> &Builder) {
1551 if (Fn == nullptr)
1552 return nullptr;
1553
1554 Intrinsic::ID WidenedId = Intrinsic::ID();
1555 FunctionType *WidenedFnTy = nullptr;
1556 if (const auto ID = Fn->getIntrinsicID()) {
1557 const auto *Widened = KnownIntrinsic::widen(Fn->getName());
1558 if (Widened) {
1559 WidenedId = Widened->ID;
1560 WidenedFnTy = Widened->MakeFnTy(Context);
1561 } else {
1562 // If we don't know how to widen the intrinsic, we have no choice but to
1563 // call the non-wide version on a truncated shadow and extend again
1564 // afterwards.
1565 WidenedId = ID;
1566 WidenedFnTy = Fn->getFunctionType();
1567 }
1568 } else if (const char *Name = getIntrinsicFromLibfunc(*Fn, VT, TLI)) {
1569 // We might have a call to a library function that we can replace with a
1570 // wider Intrinsic.
1571 const auto *Widened = KnownIntrinsic::widen(Name);
1572 assert(Widened && "make sure KnownIntrinsic entries are consistent");
1573 WidenedId = Widened->ID;
1574 WidenedFnTy = Widened->MakeFnTy(Context);
1575 } else {
1576 // This is not a known library function or intrinsic.
1577 return nullptr;
1578 }
1579
1580 // Check that the widened intrinsic is valid.
1582 getIntrinsicInfoTableEntries(WidenedId, Table);
1585 [[maybe_unused]] Intrinsic::MatchIntrinsicTypesResult MatchResult =
1586 Intrinsic::matchIntrinsicSignature(WidenedFnTy, TableRef, ArgTys);
1588 "invalid widened intrinsic");
1589 // For known intrinsic functions, we create a second call to the same
1590 // intrinsic with a different type.
1591 SmallVector<Value *, 4> Args;
1592 // The last operand is the intrinsic itself, skip it.
1593 for (unsigned I = 0, E = Call.getNumOperands() - 1; I < E; ++I) {
1594 Value *Arg = Call.getOperand(I);
1595 Type *OrigArgTy = Arg->getType();
1596 Type *IntrinsicArgTy = WidenedFnTy->getParamType(I);
1597 if (OrigArgTy == IntrinsicArgTy) {
1598 Args.push_back(Arg); // The arg is passed as is.
1599 continue;
1600 }
1601 Type *ShadowArgTy = Config.getExtendedFPType(Arg->getType());
1602 assert(ShadowArgTy &&
1603 "don't know how to get the shadow value for a non-FT");
1604 Value *Shadow = Map.getShadow(Arg);
1605 if (ShadowArgTy == IntrinsicArgTy) {
1606 // The shadow is the right type for the intrinsic.
1607 assert(Shadow->getType() == ShadowArgTy);
1608 Args.push_back(Shadow);
1609 continue;
1610 }
1611 // There is no intrinsic with his level of precision, truncate the shadow.
1612 Args.push_back(Builder.CreateFPTrunc(Shadow, IntrinsicArgTy));
1613 }
1614 Value *IntrinsicCall = Builder.CreateIntrinsic(WidenedId, ArgTys, Args);
1615 return WidenedFnTy->getReturnType() == ExtendedVT
1616 ? IntrinsicCall
1617 : Builder.CreateFPExt(IntrinsicCall, ExtendedVT);
1618}
1619
1620// Handle a CallBase, i.e. a function call, an inline asm sequence, or an
1621// invoke.
1622Value *NumericalStabilitySanitizer::handleCallBase(CallBase &Call, Type *VT,
1623 Type *ExtendedVT,
1624 const TargetLibraryInfo &TLI,
1625 const ValueToShadowMap &Map,
1626 IRBuilder<> &Builder) {
1627 // We cannot look inside inline asm, just expand the result again.
1628 if (Call.isInlineAsm())
1629 return Builder.CreateFPExt(&Call, ExtendedVT);
1630
1631 // Intrinsics and library functions (e.g. sin, exp) are handled
1632 // specifically, because we know their semantics and can do better than
1633 // blindly calling them (e.g. compute the sinus in the actual shadow domain).
1634 if (Value *V =
1635 maybeHandleKnownCallBase(Call, VT, ExtendedVT, TLI, Map, Builder))
1636 return V;
1637
1638 // If the return tag matches that of the called function, read the extended
1639 // return value from the shadow ret ptr. Else, just extend the return value.
1640 Value *L =
1641 Builder.CreateLoad(IntptrTy, NsanShadowRetTag, /*isVolatile=*/false);
1642 Value *HasShadowRet = Builder.CreateICmpEQ(
1643 L, Builder.CreatePtrToInt(Call.getCalledOperand(), IntptrTy));
1644
1645 Value *ShadowRetVal = Builder.CreateLoad(
1646 ExtendedVT,
1647 Builder.CreateConstGEP2_64(NsanShadowRetType, NsanShadowRetPtr, 0, 0),
1648 /*isVolatile=*/false);
1649 Value *Shadow = Builder.CreateSelect(HasShadowRet, ShadowRetVal,
1650 Builder.CreateFPExt(&Call, ExtendedVT));
1651 ++NumInstrumentedFTCalls;
1652 return Shadow;
1653}
1654
1655// Creates a shadow value for the given FT value. At that point all operands are
1656// guaranteed to be available.
1657Value *NumericalStabilitySanitizer::createShadowValueWithOperandsAvailable(
1658 Instruction &Inst, const TargetLibraryInfo &TLI,
1659 const ValueToShadowMap &Map) {
1660 Type *VT = Inst.getType();
1661 Type *ExtendedVT = Config.getExtendedFPType(VT);
1662 assert(ExtendedVT != nullptr && "trying to create a shadow for a non-FT");
1663
1664 if (auto *Load = dyn_cast<LoadInst>(&Inst))
1665 return handleLoad(*Load, VT, ExtendedVT);
1666
1667 if (auto *Call = dyn_cast<CallInst>(&Inst)) {
1668 // Insert after the call.
1669 BasicBlock::iterator It(Inst);
1670 IRBuilder<> Builder(Call->getParent(), ++It);
1672 return handleCallBase(*Call, VT, ExtendedVT, TLI, Map, Builder);
1673 }
1674
1675 if (auto *Invoke = dyn_cast<InvokeInst>(&Inst)) {
1676 // The Invoke terminates the basic block, create a new basic block in
1677 // between the successful invoke and the next block.
1678 BasicBlock *InvokeBB = Invoke->getParent();
1679 BasicBlock *NextBB = Invoke->getNormalDest();
1680 BasicBlock *NewBB =
1681 BasicBlock::Create(Context, "", NextBB->getParent(), NextBB);
1682 Inst.replaceSuccessorWith(NextBB, NewBB);
1683
1684 IRBuilder<> Builder(NewBB);
1685 Builder.SetCurrentDebugLocation(Invoke->getDebugLoc());
1686 Value *Shadow = handleCallBase(*Invoke, VT, ExtendedVT, TLI, Map, Builder);
1687 Builder.CreateBr(NextBB);
1688 NewBB->replaceSuccessorsPhiUsesWith(InvokeBB, NewBB);
1689 return Shadow;
1690 }
1691
1692 IRBuilder<> Builder(Inst.getNextNode());
1693 Builder.SetCurrentDebugLocation(Inst.getDebugLoc());
1694
1695 if (auto *Trunc = dyn_cast<FPTruncInst>(&Inst))
1696 return handleTrunc(*Trunc, VT, ExtendedVT, Map, Builder);
1697 if (auto *Ext = dyn_cast<FPExtInst>(&Inst))
1698 return handleExt(*Ext, VT, ExtendedVT, Map, Builder);
1699
1700 if (auto *UnaryOp = dyn_cast<UnaryOperator>(&Inst))
1701 return Builder.CreateUnOp(UnaryOp->getOpcode(),
1702 Map.getShadow(UnaryOp->getOperand(0)));
1703
1704 if (auto *BinOp = dyn_cast<BinaryOperator>(&Inst))
1705 return Builder.CreateBinOp(BinOp->getOpcode(),
1706 Map.getShadow(BinOp->getOperand(0)),
1707 Map.getShadow(BinOp->getOperand(1)));
1708
1709 if (isa<UIToFPInst>(&Inst) || isa<SIToFPInst>(&Inst)) {
1710 auto *Cast = cast<CastInst>(&Inst);
1711 return Builder.CreateCast(Cast->getOpcode(), Cast->getOperand(0),
1712 ExtendedVT);
1713 }
1714
1715 if (auto *S = dyn_cast<SelectInst>(&Inst))
1716 return Builder.CreateSelect(S->getCondition(),
1717 Map.getShadow(S->getTrueValue()),
1718 Map.getShadow(S->getFalseValue()));
1719
1720 if (auto *Freeze = dyn_cast<FreezeInst>(&Inst))
1721 return Builder.CreateFreeze(Map.getShadow(Freeze->getOperand(0)));
1722
1723 if (auto *Extract = dyn_cast<ExtractElementInst>(&Inst))
1724 return Builder.CreateExtractElement(
1725 Map.getShadow(Extract->getVectorOperand()), Extract->getIndexOperand());
1726
1727 if (auto *Insert = dyn_cast<InsertElementInst>(&Inst))
1728 return Builder.CreateInsertElement(Map.getShadow(Insert->getOperand(0)),
1729 Map.getShadow(Insert->getOperand(1)),
1730 Insert->getOperand(2));
1731
1732 if (auto *Shuffle = dyn_cast<ShuffleVectorInst>(&Inst))
1733 return Builder.CreateShuffleVector(Map.getShadow(Shuffle->getOperand(0)),
1734 Map.getShadow(Shuffle->getOperand(1)),
1735 Shuffle->getShuffleMask());
1736 // TODO: We could make aggregate object first class citizens. For now we
1737 // just extend the extracted value.
1738 if (auto *Extract = dyn_cast<ExtractValueInst>(&Inst))
1739 return Builder.CreateFPExt(Extract, ExtendedVT);
1740
1741 if (auto *BC = dyn_cast<BitCastInst>(&Inst))
1742 return Builder.CreateFPExt(BC, ExtendedVT);
1743
1744 report_fatal_error("Unimplemented support for " +
1745 Twine(Inst.getOpcodeName()));
1746}
1747
1748// Creates a shadow value for an instruction that defines a value of FT type.
1749// FT operands that do not already have shadow values are created recursively.
1750// The DFS is guaranteed to not loop as phis and arguments already have
1751// shadows.
1752void NumericalStabilitySanitizer::maybeCreateShadowValue(
1753 Instruction &Root, const TargetLibraryInfo &TLI, ValueToShadowMap &Map) {
1754 Type *VT = Root.getType();
1755 Type *ExtendedVT = Config.getExtendedFPType(VT);
1756 if (ExtendedVT == nullptr)
1757 return; // Not an FT value.
1758
1759 if (Map.hasShadow(&Root))
1760 return; // Shadow already exists.
1761
1762 assert(!isa<PHINode>(Root) && "phi nodes should already have shadows");
1763
1764 std::vector<Instruction *> DfsStack(1, &Root);
1765 while (!DfsStack.empty()) {
1766 // Ensure that all operands to the instruction have shadows before
1767 // proceeding.
1768 Instruction *I = DfsStack.back();
1769 // The shadow for the instruction might have been created deeper in the DFS,
1770 // see `forward_use_with_two_uses` test.
1771 if (Map.hasShadow(I)) {
1772 DfsStack.pop_back();
1773 continue;
1774 }
1775
1776 bool MissingShadow = false;
1777 for (Value *Op : I->operands()) {
1778 Type *VT = Op->getType();
1779 if (!Config.getExtendedFPType(VT))
1780 continue; // Not an FT value.
1781 if (Map.hasShadow(Op))
1782 continue; // Shadow is already available.
1783 MissingShadow = true;
1784 DfsStack.push_back(cast<Instruction>(Op));
1785 }
1786 if (MissingShadow)
1787 continue; // Process operands and come back to this instruction later.
1788
1789 // All operands have shadows. Create a shadow for the current value.
1790 Value *Shadow = createShadowValueWithOperandsAvailable(*I, TLI, Map);
1791 Map.setShadow(*I, *Shadow);
1792 DfsStack.pop_back();
1793 }
1794}
1795
1796// A floating-point store needs its value and type written to shadow memory.
1797void NumericalStabilitySanitizer::propagateFTStore(
1798 StoreInst &Store, Type *VT, Type *ExtendedVT, const ValueToShadowMap &Map) {
1799 Value *StoredValue = Store.getValueOperand();
1800 IRBuilder<> Builder(&Store);
1801 Builder.SetCurrentDebugLocation(Store.getDebugLoc());
1802 const auto Extents = getMemoryExtentsOrDie(VT);
1803 Value *ShadowPtr = Builder.CreateCall(
1804 NsanGetShadowPtrForStore[Extents.ValueType],
1805 {Store.getPointerOperand(), ConstantInt::get(IntptrTy, Extents.NumElts)});
1806
1807 Value *StoredShadow = Map.getShadow(StoredValue);
1808 if (!Store.getParent()->getParent()->hasOptNone()) {
1809 // Only check stores when optimizing, because non-optimized code generates
1810 // too many stores to the stack, creating false positives.
1811 if (ClCheckStores) {
1812 StoredShadow = emitCheck(StoredValue, StoredShadow, Builder,
1813 CheckLoc::makeStore(Store.getPointerOperand()));
1814 ++NumInstrumentedFTStores;
1815 }
1816 }
1817
1818 Builder.CreateAlignedStore(StoredShadow, ShadowPtr, Align(1),
1819 Store.isVolatile());
1820}
1821
1822// A non-ft store needs to invalidate shadow memory. Exceptions are:
1823// - memory transfers of floating-point data through other pointer types (llvm
1824// optimization passes transform `*(float*)a = *(float*)b` into
1825// `*(i32*)a = *(i32*)b` ). These have the same semantics as memcpy.
1826// - Writes of FT-sized constants. LLVM likes to do float stores as bitcasted
1827// ints. Note that this is not really necessary because if the value is
1828// unknown the framework will re-extend it on load anyway. It just felt
1829// easier to debug tests with vectors of FTs.
1830void NumericalStabilitySanitizer::propagateNonFTStore(
1831 StoreInst &Store, Type *VT, const ValueToShadowMap &Map) {
1832 Value *PtrOp = Store.getPointerOperand();
1833 IRBuilder<> Builder(Store.getNextNode());
1834 Builder.SetCurrentDebugLocation(Store.getDebugLoc());
1835 Value *Dst = PtrOp;
1836 TypeSize SlotSize = DL.getTypeStoreSize(VT);
1837 assert(!SlotSize.isScalable() && "unsupported");
1838 const auto LoadSizeBytes = SlotSize.getFixedValue();
1839 Value *ValueSize = Constant::getIntegerValue(
1840 IntptrTy, APInt(IntptrTy->getPrimitiveSizeInBits(), LoadSizeBytes));
1841
1842 ++NumInstrumentedNonFTStores;
1843 Value *StoredValue = Store.getValueOperand();
1844 if (LoadInst *Load = dyn_cast<LoadInst>(StoredValue)) {
1845 // TODO: Handle the case when the value is from a phi.
1846 // This is a memory transfer with memcpy semantics. Copy the type and
1847 // value from the source. Note that we cannot use __nsan_copy_values()
1848 // here, because that will not work when there is a write to memory in
1849 // between the load and the store, e.g. in the case of a swap.
1850 Type *ShadowTypeIntTy = Type::getIntNTy(Context, 8 * LoadSizeBytes);
1851 Type *ShadowValueIntTy =
1852 Type::getIntNTy(Context, 8 * kShadowScale * LoadSizeBytes);
1853 IRBuilder<> LoadBuilder(Load->getNextNode());
1854 Builder.SetCurrentDebugLocation(Store.getDebugLoc());
1855 Value *LoadSrc = Load->getPointerOperand();
1856 // Read the shadow type and value at load time. The type has the same size
1857 // as the FT value, the value has twice its size.
1858 // TODO: cache them to avoid re-creating them when a load is used by
1859 // several stores. Maybe create them like the FT shadows when a load is
1860 // encountered.
1861 Value *RawShadowType = LoadBuilder.CreateAlignedLoad(
1862 ShadowTypeIntTy,
1863 LoadBuilder.CreateCall(NsanGetRawShadowTypePtr, {LoadSrc}), Align(1),
1864 /*isVolatile=*/false);
1865 Value *RawShadowValue = LoadBuilder.CreateAlignedLoad(
1866 ShadowValueIntTy,
1867 LoadBuilder.CreateCall(NsanGetRawShadowPtr, {LoadSrc}), Align(1),
1868 /*isVolatile=*/false);
1869
1870 // Write back the shadow type and value at store time.
1871 Builder.CreateAlignedStore(
1872 RawShadowType, Builder.CreateCall(NsanGetRawShadowTypePtr, {Dst}),
1873 Align(1),
1874 /*isVolatile=*/false);
1875 Builder.CreateAlignedStore(RawShadowValue,
1876 Builder.CreateCall(NsanGetRawShadowPtr, {Dst}),
1877 Align(1),
1878 /*isVolatile=*/false);
1879
1880 ++NumInstrumentedNonFTMemcpyStores;
1881 return;
1882 }
1883 // ClPropagateNonFTConstStoresAsFT is by default false.
1884 if (Constant *C; ClPropagateNonFTConstStoresAsFT &&
1885 (C = dyn_cast<Constant>(StoredValue))) {
1886 // This might be a fp constant stored as an int. Bitcast and store if it has
1887 // appropriate size.
1888 Type *BitcastTy = nullptr; // The FT type to bitcast to.
1889 if (auto *CInt = dyn_cast<ConstantInt>(C)) {
1890 switch (CInt->getType()->getScalarSizeInBits()) {
1891 case 32:
1892 BitcastTy = Type::getFloatTy(Context);
1893 break;
1894 case 64:
1895 BitcastTy = Type::getDoubleTy(Context);
1896 break;
1897 case 80:
1898 BitcastTy = Type::getX86_FP80Ty(Context);
1899 break;
1900 default:
1901 break;
1902 }
1903 } else if (auto *CDV = dyn_cast<ConstantDataVector>(C)) {
1904 const int NumElements =
1905 cast<VectorType>(CDV->getType())->getElementCount().getFixedValue();
1906 switch (CDV->getType()->getScalarSizeInBits()) {
1907 case 32:
1908 BitcastTy =
1909 VectorType::get(Type::getFloatTy(Context), NumElements, false);
1910 break;
1911 case 64:
1912 BitcastTy =
1913 VectorType::get(Type::getDoubleTy(Context), NumElements, false);
1914 break;
1915 case 80:
1916 BitcastTy =
1917 VectorType::get(Type::getX86_FP80Ty(Context), NumElements, false);
1918 break;
1919 default:
1920 break;
1921 }
1922 }
1923 if (BitcastTy) {
1924 const MemoryExtents Extents = getMemoryExtentsOrDie(BitcastTy);
1925 Value *ShadowPtr = Builder.CreateCall(
1926 NsanGetShadowPtrForStore[Extents.ValueType],
1927 {PtrOp, ConstantInt::get(IntptrTy, Extents.NumElts)});
1928 // Bitcast the integer value to the appropriate FT type and extend to 2FT.
1929 Type *ExtVT = Config.getExtendedFPType(BitcastTy);
1930 Value *Shadow =
1931 Builder.CreateFPExt(Builder.CreateBitCast(C, BitcastTy), ExtVT);
1932 Builder.CreateAlignedStore(Shadow, ShadowPtr, Align(1),
1933 Store.isVolatile());
1934 return;
1935 }
1936 }
1937 // All other stores just reset the shadow value to unknown.
1938 Builder.CreateCall(NsanSetUnknownFns.getFallback(), {Dst, ValueSize});
1939}
1940
1941void NumericalStabilitySanitizer::propagateShadowValues(
1942 Instruction &Inst, const TargetLibraryInfo &TLI,
1943 const ValueToShadowMap &Map) {
1944 if (auto *Store = dyn_cast<StoreInst>(&Inst)) {
1945 Value *StoredValue = Store->getValueOperand();
1946 Type *VT = StoredValue->getType();
1947 Type *ExtendedVT = Config.getExtendedFPType(VT);
1948 if (ExtendedVT == nullptr)
1949 return propagateNonFTStore(*Store, VT, Map);
1950 return propagateFTStore(*Store, VT, ExtendedVT, Map);
1951 }
1952
1953 if (auto *FCmp = dyn_cast<FCmpInst>(&Inst)) {
1954 emitFCmpCheck(*FCmp, Map);
1955 return;
1956 }
1957
1958 if (auto *CB = dyn_cast<CallBase>(&Inst)) {
1959 maybeAddSuffixForNsanInterface(CB);
1960 if (CallInst *CI = dyn_cast<CallInst>(&Inst))
1962 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&Inst)) {
1963 instrumentMemIntrinsic(MI);
1964 return;
1965 }
1966 populateShadowStack(*CB, TLI, Map);
1967 return;
1968 }
1969
1970 if (auto *RetInst = dyn_cast<ReturnInst>(&Inst)) {
1971 if (!ClCheckRet)
1972 return;
1973
1974 Value *RV = RetInst->getReturnValue();
1975 if (RV == nullptr)
1976 return; // This is a `ret void`.
1977 Type *VT = RV->getType();
1978 Type *ExtendedVT = Config.getExtendedFPType(VT);
1979 if (ExtendedVT == nullptr)
1980 return; // Not an FT ret.
1981 Value *RVShadow = Map.getShadow(RV);
1982 IRBuilder<> Builder(RetInst);
1983
1984 RVShadow = emitCheck(RV, RVShadow, Builder, CheckLoc::makeRet());
1985 ++NumInstrumentedFTRets;
1986 // Store tag.
1987 Value *FnAddr =
1988 Builder.CreatePtrToInt(Inst.getParent()->getParent(), IntptrTy);
1989 Builder.CreateStore(FnAddr, NsanShadowRetTag);
1990 // Store value.
1991 Value *ShadowRetValPtr =
1992 Builder.CreateConstGEP2_64(NsanShadowRetType, NsanShadowRetPtr, 0, 0);
1993 Builder.CreateStore(RVShadow, ShadowRetValPtr);
1994 return;
1995 }
1996
1997 if (InsertValueInst *Insert = dyn_cast<InsertValueInst>(&Inst)) {
1998 Value *V = Insert->getOperand(1);
1999 Type *VT = V->getType();
2000 Type *ExtendedVT = Config.getExtendedFPType(VT);
2001 if (ExtendedVT == nullptr)
2002 return;
2003 IRBuilder<> Builder(Insert);
2004 emitCheck(V, Map.getShadow(V), Builder, CheckLoc::makeInsert());
2005 return;
2006 }
2007}
2008
2009// Moves fast math flags from the function to individual instructions, and
2010// removes the attribute from the function.
2011// TODO: Make this controllable with a flag.
2013 std::vector<Instruction *> &Instructions) {
2014 FastMathFlags FMF;
2015#define MOVE_FLAG(attr, setter) \
2016 if (F.getFnAttribute(attr).getValueAsString() == "true") { \
2017 F.removeFnAttr(attr); \
2018 FMF.set##setter(); \
2019 }
2020 MOVE_FLAG("no-signed-zeros-fp-math", NoSignedZeros)
2021#undef MOVE_FLAG
2022
2023 for (Instruction *I : Instructions)
2025 I->setFastMathFlags(FMF);
2026}
2027
2028bool NumericalStabilitySanitizer::sanitizeFunction(
2029 Function &F, const TargetLibraryInfo &TLI) {
2030 if (!F.hasFnAttribute(Attribute::SanitizeNumericalStability) ||
2031 F.isDeclaration())
2032 return false;
2033
2034 // This is required to prevent instrumenting call to __nsan_init from within
2035 // the module constructor.
2036 if (F.getName() == kNsanModuleCtorName)
2037 return false;
2038
2039 // The instrumentation maintains:
2040 // - for each IR value `v` of floating-point (or vector floating-point) type
2041 // FT, a shadow IR value `s(v)` with twice the precision 2FT (e.g.
2042 // double for float and f128 for double).
2043 // - A shadow memory, which stores `s(v)` for any `v` that has been stored,
2044 // along with a shadow memory tag, which stores whether the value in the
2045 // corresponding shadow memory is valid. Note that this might be
2046 // incorrect if a non-instrumented function stores to memory, or if
2047 // memory is stored to through a char pointer.
2048 // - A shadow stack, which holds `s(v)` for any floating-point argument `v`
2049 // of a call to an instrumented function. This allows
2050 // instrumented functions to retrieve the shadow values for their
2051 // arguments.
2052 // Because instrumented functions can be called from non-instrumented
2053 // functions, the stack needs to include a tag so that the instrumented
2054 // function knows whether shadow values are available for their
2055 // parameters (i.e. whether is was called by an instrumented function).
2056 // When shadow arguments are not available, they have to be recreated by
2057 // extending the precision of the non-shadow arguments to the non-shadow
2058 // value. Non-instrumented functions do not modify (or even know about) the
2059 // shadow stack. The shadow stack pointer is __nsan_shadow_args. The shadow
2060 // stack tag is __nsan_shadow_args_tag. The tag is any unique identifier
2061 // for the function (we use the address of the function). Both variables
2062 // are thread local.
2063 // Example:
2064 // calls shadow stack tag shadow stack
2065 // =======================================================================
2066 // non_instrumented_1() 0 0
2067 // |
2068 // v
2069 // instrumented_2(float a) 0 0
2070 // |
2071 // v
2072 // instrumented_3(float b, double c) &instrumented_3 s(b),s(c)
2073 // |
2074 // v
2075 // instrumented_4(float d) &instrumented_4 s(d)
2076 // |
2077 // v
2078 // non_instrumented_5(float e) &non_instrumented_5 s(e)
2079 // |
2080 // v
2081 // instrumented_6(float f) &non_instrumented_5 s(e)
2082 //
2083 // On entry, instrumented_2 checks whether the tag corresponds to its
2084 // function ptr.
2085 // Note that functions reset the tag to 0 after reading shadow parameters.
2086 // This ensures that the function does not erroneously read invalid data if
2087 // called twice in the same stack, once from an instrumented function and
2088 // once from an uninstrumented one. For example, in the following example,
2089 // resetting the tag in (A) ensures that (B) does not reuse the same the
2090 // shadow arguments (which would be incorrect).
2091 // instrumented_1(float a)
2092 // |
2093 // v
2094 // instrumented_2(float b) (A)
2095 // |
2096 // v
2097 // non_instrumented_3()
2098 // |
2099 // v
2100 // instrumented_2(float b) (B)
2101 //
2102 // - A shadow return slot. Any function that returns a floating-point value
2103 // places a shadow return value in __nsan_shadow_ret_val. Again, because
2104 // we might be calling non-instrumented functions, this value is guarded
2105 // by __nsan_shadow_ret_tag marker indicating which instrumented function
2106 // placed the value in __nsan_shadow_ret_val, so that the caller can check
2107 // that this corresponds to the callee. Both variables are thread local.
2108 //
2109 // For example, in the following example, the instrumentation in
2110 // `instrumented_1` rejects the shadow return value from `instrumented_3`
2111 // because is is not tagged as expected (`&instrumented_3` instead of
2112 // `non_instrumented_2`):
2113 //
2114 // instrumented_1()
2115 // |
2116 // v
2117 // float non_instrumented_2()
2118 // |
2119 // v
2120 // float instrumented_3()
2121 //
2122 // Calls of known math functions (sin, cos, exp, ...) are duplicated to call
2123 // their overload on the shadow type.
2124
2125 // Collect all instructions before processing, as creating shadow values
2126 // creates new instructions inside the function.
2127 std::vector<Instruction *> OriginalInstructions;
2128 for (BasicBlock &BB : F)
2129 for (Instruction &Inst : BB)
2130 OriginalInstructions.emplace_back(&Inst);
2131
2132 moveFastMathFlags(F, OriginalInstructions);
2133 ValueToShadowMap ValueToShadow(Config);
2134
2135 // In the first pass, we create shadow values for all FT function arguments
2136 // and all phis. This ensures that the DFS of the next pass does not have
2137 // any loops.
2138 std::vector<PHINode *> OriginalPhis;
2139 createShadowArguments(F, TLI, ValueToShadow);
2140 for (Instruction *I : OriginalInstructions) {
2141 if (PHINode *Phi = dyn_cast<PHINode>(I)) {
2142 if (PHINode *Shadow = maybeCreateShadowPhi(*Phi, TLI)) {
2143 OriginalPhis.push_back(Phi);
2144 ValueToShadow.setShadow(*Phi, *Shadow);
2145 }
2146 }
2147 }
2148
2149 // Create shadow values for all instructions creating FT values.
2150 for (Instruction *I : OriginalInstructions)
2151 maybeCreateShadowValue(*I, TLI, ValueToShadow);
2152
2153 // Propagate shadow values across stores, calls and rets.
2154 for (Instruction *I : OriginalInstructions)
2155 propagateShadowValues(*I, TLI, ValueToShadow);
2156
2157 // The last pass populates shadow phis with shadow values.
2158 for (PHINode *Phi : OriginalPhis) {
2159 PHINode *ShadowPhi = cast<PHINode>(ValueToShadow.getShadow(Phi));
2160 for (unsigned I : seq(Phi->getNumOperands())) {
2161 Value *V = Phi->getOperand(I);
2162 Value *Shadow = ValueToShadow.getShadow(V);
2163 BasicBlock *IncomingBB = Phi->getIncomingBlock(I);
2164 // For some instructions (e.g. invoke), we create the shadow in a separate
2165 // block, different from the block where the original value is created.
2166 // In that case, the shadow phi might need to refer to this block instead
2167 // of the original block.
2168 // Note that this can only happen for instructions as constant shadows are
2169 // always created in the same block.
2170 ShadowPhi->addIncoming(Shadow, IncomingBB);
2171 }
2172 }
2173
2174 return !ValueToShadow.empty();
2175}
2176
2178 uint64_t OpSize = 0;
2179 if (Constant *C = dyn_cast<Constant>(V)) {
2180 auto *CInt = dyn_cast<ConstantInt>(C);
2181 if (CInt && CInt->getValue().getBitWidth() <= 64)
2182 OpSize = CInt->getValue().getZExtValue();
2183 }
2184
2185 return OpSize;
2186}
2187
2188// Instrument the memory intrinsics so that they properly modify the shadow
2189// memory.
2190bool NumericalStabilitySanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
2191 IRBuilder<> Builder(MI);
2192 if (auto *M = dyn_cast<MemSetInst>(MI)) {
2193 FunctionCallee SetUnknownFn =
2194 NsanSetUnknownFns.getFunctionFor(GetMemOpSize(M->getArgOperand(2)));
2195 if (SetUnknownFn.getFunctionType()->getNumParams() == 1)
2196 Builder.CreateCall(SetUnknownFn, {/*Address=*/M->getArgOperand(0)});
2197 else
2198 Builder.CreateCall(SetUnknownFn,
2199 {/*Address=*/M->getArgOperand(0),
2200 /*Size=*/Builder.CreateIntCast(M->getArgOperand(2),
2201 IntptrTy, false)});
2202
2203 } else if (auto *M = dyn_cast<MemTransferInst>(MI)) {
2204 FunctionCallee CopyFn =
2205 NsanCopyFns.getFunctionFor(GetMemOpSize(M->getArgOperand(2)));
2206
2207 if (CopyFn.getFunctionType()->getNumParams() == 2)
2208 Builder.CreateCall(CopyFn, {/*Destination=*/M->getArgOperand(0),
2209 /*Source=*/M->getArgOperand(1)});
2210 else
2211 Builder.CreateCall(CopyFn, {/*Destination=*/M->getArgOperand(0),
2212 /*Source=*/M->getArgOperand(1),
2213 /*Size=*/
2214 Builder.CreateIntCast(M->getArgOperand(2),
2215 IntptrTy, false)});
2216 }
2217 return false;
2218}
2219
2220void NumericalStabilitySanitizer::maybeAddSuffixForNsanInterface(CallBase *CI) {
2221 Function *Fn = CI->getCalledFunction();
2222 if (Fn == nullptr)
2223 return;
2224
2225 if (!Fn->getName().starts_with("__nsan_"))
2226 return;
2227
2228 if (Fn->getName() == "__nsan_dump_shadow_mem") {
2229 assert(CI->arg_size() == 4 &&
2230 "invalid prototype for __nsan_dump_shadow_mem");
2231 // __nsan_dump_shadow_mem requires an extra parameter with the dynamic
2232 // configuration:
2233 // (shadow_type_id_for_long_double << 16) | (shadow_type_id_for_double << 8)
2234 // | shadow_type_id_for_double
2235 const uint64_t shadow_value_type_ids =
2236 (static_cast<size_t>(Config.byValueType(kLongDouble).getNsanTypeId())
2237 << 16) |
2238 (static_cast<size_t>(Config.byValueType(kDouble).getNsanTypeId())
2239 << 8) |
2240 static_cast<size_t>(Config.byValueType(kFloat).getNsanTypeId());
2241 CI->setArgOperand(3, ConstantInt::get(IntptrTy, shadow_value_type_ids));
2242 }
2243}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ArrayRef< TableEntry > TableRef
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
Hexagon Common GEP
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
static GlobalValue * createThreadLocalGV(const char *Name, Module &M, Type *Ty)
static FunctionType * makeDoubleDouble(LLVMContext &C)
constexpr int kMaxVectorWidth
static cl::opt< bool > ClCheckRet("nsan-check-ret", cl::init(true), cl::desc("Check floating-point return values"), cl::Hidden)
static bool shouldCheckArgs(CallBase &CI, const TargetLibraryInfo &TLI, const std::optional< Regex > &CheckFunctionsFilter)
static cl::opt< bool > ClCheckStores("nsan-check-stores", cl::init(true), cl::desc("Check floating-point stores"), cl::Hidden)
static FunctionType * makeX86FP80X86FP80(LLVMContext &C)
#define MOVE_FLAG(attr, setter)
static FunctionType * makeX86FP80X86FP80I32(LLVMContext &C)
constexpr int kMaxNumArgs
constexpr int kMaxShadowTypeSizeBytes
static const char * getIntrinsicFromLibfunc(Function &Fn, Type *VT, const TargetLibraryInfo &TLI)
static FunctionType * makeX86FP80X86FP80X86FP80(LLVMContext &C)
static FunctionType * makeX86FP80X86FP80X86FP80X86FP80(LLVMContext &C)
static uint64_t GetMemOpSize(Value *V)
constexpr StringLiteral kNsanInitName("__nsan_init")
static FunctionType * makeDoubleDoubleDouble(LLVMContext &C)
constexpr int kShadowScale
static cl::opt< bool > ClCheckLoads("nsan-check-loads", cl::desc("Check floating-point load"), cl::Hidden)
constexpr StringLiteral kNsanModuleCtorName("nsan.module_ctor")
static cl::opt< bool > ClPropagateNonFTConstStoresAsFT("nsan-propagate-non-ft-const-stores-as-ft", cl::desc("Propagate non floating-point const stores as floating point values." "For debugging purposes only"), cl::Hidden)
static cl::opt< std::string > ClShadowMapping("nsan-shadow-type-mapping", cl::init("dqq"), cl::desc("One shadow type id for each of `float`, `double`, `long double`. " "`d`,`l`,`q`,`e` mean double, x86_fp80, fp128 (quad) and " "ppc_fp128 (extended double) respectively. The default is to " "shadow `float` as `double`, and `double` and `x86_fp80` as " "`fp128`"), cl::Hidden)
static FunctionType * makeDoubleDoubleDoubleDouble(LLVMContext &C)
static cl::opt< bool > ClTruncateFCmpEq("nsan-truncate-fcmp-eq", cl::init(true), cl::desc("This flag controls the behaviour of fcmp equality comparisons." "For equality comparisons such as `x == 0.0f`, we can perform the " "shadow check in the shadow (`x_shadow == 0.0) == (x == 0.0f)`) or app " " domain (`(trunc(x_shadow) == 0.0f) == (x == 0.0f)`). This helps " "catch the case when `x_shadow` is accurate enough (and therefore " "close enough to zero) so that `trunc(x_shadow)` is zero even though " "both `x` and `x_shadow` are not"), cl::Hidden)
static cl::opt< std::string > ClCheckFunctionsFilter("check-functions-filter", cl::desc("Only emit checks for arguments of functions " "whose names match the given regular expression"), cl::value_desc("regex"))
static FunctionType * makeDoubleDoubleI32(LLVMContext &C)
static void moveFastMathFlags(Function &F, std::vector< Instruction * > &Instructions)
static cl::opt< bool > ClInstrumentFCmp("nsan-instrument-fcmp", cl::init(true), cl::desc("Instrument floating-point comparisons"), cl::Hidden)
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
Value * RHS
Value * LHS
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5890
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM_ABI void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New)
Update all phi nodes in this basic block's successors to refer to basic block New instead of basic bl...
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:449
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
const Instruction & back() const
Definition BasicBlock.h:474
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
bool isInlineAsm() const
Check if this call is an inline asm statement.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Value * getCalledOperand() const
void setArgOperand(unsigned i, Value *v)
unsigned arg_size() const
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:765
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:256
bool empty() const
Definition DenseMap.h:109
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:169
This instruction compares its operands according to the predicate given to the constructor.
static bool isEquality(Predicate Pred)
This class represents an extension of floating point types.
This class represents a truncation of floating point types.
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
FunctionType * getFunctionType()
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:211
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:246
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition IRBuilder.h:2584
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition IRBuilder.h:2572
Value * CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2157
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition IRBuilder.h:2631
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, const Twine &Name="")
Definition IRBuilder.h:2023
BasicBlock::iterator GetInsertPoint() const
Definition IRBuilder.h:202
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition IRBuilder.h:2650
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition IRBuilder.h:579
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Definition IRBuilder.h:2233
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition IRBuilder.h:247
Value * CreateUnOp(Instruction::UnaryOps Opc, Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:1839
UncondBrInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Definition IRBuilder.h:1217
LLVM_ABI CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2331
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2199
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition IRBuilder.h:1877
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition IRBuilder.h:2606
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition IRBuilder.h:1890
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2189
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2510
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:1734
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition IRBuilder.h:2272
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile=false)
Definition IRBuilder.h:1913
Value * CreateFPExt(Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2172
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition IRBuilder.h:1599
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2811
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI void replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB)
Replace specified successor OldBB to point at the provided block.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
const char * getOpcodeName() const
LLVM_ABI void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction.
Class to represent integer types.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
This is the common base class for memset/memcpy/memmove.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:882
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
StringRef getName(LibFunc F) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI Type * getStructElementType(unsigned N) const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:290
bool isX86_FP80Ty() const
Return true if this is x86 long double.
Definition Type.h:161
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:281
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:313
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
static LLVM_ABI Type * getPPC_FP128Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI Type * getFP128Ty(LLVMContext &C)
Definition Type.cpp:295
LLVM_ABI unsigned getStructNumElements() const
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:311
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:278
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:201
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:310
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:291
static LLVM_ABI Type * getX86_FP80Ty(LLVMContext &C)
Definition Type.cpp:294
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:290
'undef' values are things that do not have specified contents.
Definition Constants.h:1606
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
op_range operands()
Definition User.h:267
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
iterator_range< user_iterator > users()
Definition Value.h:427
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
const ParentTy * getParent() const
Definition ilist_node.h:34
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI MatchIntrinsicTypesResult matchIntrinsicSignature(FunctionType *FTy, ArrayRef< IITDescriptor > &Infos, SmallVectorImpl< Type * > &ArgTys)
Match the specified function type with the type constraints specified by the .td file.
LLVM_ABI void getIntrinsicInfoTableEntries(ID id, SmallVectorImpl< IITDescriptor > &T)
Return the IIT table descriptor for the specified intrinsic into an array of IITDescriptors.
initializer< Ty > init(const Ty &Val)
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
Context & getContext() const
Definition BasicBlock.h:99
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI std::pair< Function *, FunctionCallee > getOrCreateSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, function_ref< void(Function *, FunctionCallee)> FunctionsCreatedCallback, StringRef VersionCheckName=StringRef(), bool Weak=false)
Creates sanitizer constructor function lazily.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305
LLVM_ABI void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI, const TargetLibraryInfo *TLI)
Given a CallInst, check if it calls a string function known to CodeGen, and mark it with NoBuiltin if...
Definition Local.cpp:3889
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)