Skip to content

Commit 4c3ac21

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent 0e7f187 commit 4c3ac21

File tree

24 files changed

+2137
-6
lines changed

24 files changed

+2137
-6
lines changed

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ enum AttributeKindCodes {
787787
ATTR_KIND_CORO_ELIDE_SAFE = 98,
788788
ATTR_KIND_NO_EXT = 99,
789789
ATTR_KIND_NO_DIVERGENCE_SOURCE = 100,
790+
ATTR_KIND_SANITIZE_TYPE = 101,
790791
};
791792

792793
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ def SanitizeAddress : EnumAttr<"sanitize_address", IntersectPreserve, [FnAttr]>;
317317
/// ThreadSanitizer is on.
318318
def SanitizeThread : EnumAttr<"sanitize_thread", IntersectPreserve, [FnAttr]>;
319319

320+
/// TypeSanitizer is on.
321+
def SanitizeType : EnumAttr<"sanitize_type", [FnAttr]>;
322+
320323
/// MemorySanitizer is on.
321324
def SanitizeMemory : EnumAttr<"sanitize_memory", IntersectPreserve, [FnAttr]>;
322325

@@ -425,6 +428,7 @@ class CompatRuleStrAttr<string F, string Attr> : CompatRule<F> {
425428

426429
def : CompatRule<"isEqual<SanitizeAddressAttr>">;
427430
def : CompatRule<"isEqual<SanitizeThreadAttr>">;
431+
def : CompatRule<"isEqual<SanitizeTypeAttr>">;
428432
def : CompatRule<"isEqual<SanitizeMemoryAttr>">;
429433
def : CompatRule<"isEqual<SanitizeHWAddressAttr>">;
430434
def : CompatRule<"isEqual<SanitizeMemTagAttr>">;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===- Transforms/Instrumentation/TypeSanitizer.h - TySan Pass -----------===//
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 defines the type sanitizer pass.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_TYPESANITIZER_H
14+
#define LLVM_TRANSFORMS_INSTRUMENTATION_TYPESANITIZER_H
15+
16+
#include "llvm/IR/PassManager.h"
17+
18+
namespace llvm {
19+
class Function;
20+
class FunctionPass;
21+
class Module;
22+
23+
/// A function pass for tysan instrumentation.
24+
struct TypeSanitizerPass : public PassInfoMixin<TypeSanitizerPass> {
25+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
26+
static bool isRequired() { return true; }
27+
};
28+
29+
/// A module pass for tysan instrumentation.
30+
///
31+
/// Create ctor and init functions.
32+
struct ModuleTypeSanitizerPass : public PassInfoMixin<ModuleTypeSanitizerPass> {
33+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
34+
static bool isRequired() { return true; }
35+
};
36+
37+
} // namespace llvm
38+
#endif /* LLVM_TRANSFORMS_INSTRUMENTATION_TYPESANITIZER_H */

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,27 @@ static bool isStructPathTBAA(const MDNode *MD) {
372372
return isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
373373
}
374374

375+
// When using the TypeSanitizer, don't use TBAA information for alias analysis.
376+
// This might cause us to remove memory accesses that we need to verify at
377+
// runtime.
378+
static bool usingSanitizeType(const Value *V) {
379+
const Function *F;
380+
381+
if (auto *I = dyn_cast<Instruction>(V))
382+
F = I->getParent()->getParent();
383+
else if (auto *A = dyn_cast<Argument>(V))
384+
F = A->getParent();
385+
else
386+
return false;
387+
388+
return F->hasFnAttribute(Attribute::SanitizeType);
389+
}
390+
375391
AliasResult TypeBasedAAResult::alias(const MemoryLocation &LocA,
376392
const MemoryLocation &LocB,
377393
AAQueryInfo &AAQI, const Instruction *) {
378-
if (!EnableTBAA)
379-
return AliasResult::MayAlias;
394+
if (!EnableTBAA || usingSanitizeType(LocA.Ptr) || usingSanitizeType(LocB.Ptr))
395+
return AAResultBase::alias(LocA, LocB, AAQI, nullptr);
380396

381397
if (Aliases(LocA.AATags.TBAA, LocB.AATags.TBAA))
382398
return AliasResult::MayAlias;
@@ -426,8 +442,8 @@ MemoryEffects TypeBasedAAResult::getMemoryEffects(const Function *F) {
426442
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
427443
const MemoryLocation &Loc,
428444
AAQueryInfo &AAQI) {
429-
if (!EnableTBAA)
430-
return ModRefInfo::ModRef;
445+
if (!EnableTBAA || usingSanitizeType(Call))
446+
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
431447

432448
if (const MDNode *L = Loc.AATags.TBAA)
433449
if (const MDNode *M = Call->getMetadata(LLVMContext::MD_tbaa))
@@ -440,8 +456,8 @@ ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
440456
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1,
441457
const CallBase *Call2,
442458
AAQueryInfo &AAQI) {
443-
if (!EnableTBAA)
444-
return ModRefInfo::ModRef;
459+
if (!EnableTBAA || usingSanitizeType(Call1))
460+
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
445461

446462
if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa))
447463
if (const MDNode *M2 = Call2->getMetadata(LLVMContext::MD_tbaa))

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
21922192
return Attribute::SanitizeHWAddress;
21932193
case bitc::ATTR_KIND_SANITIZE_THREAD:
21942194
return Attribute::SanitizeThread;
2195+
case bitc::ATTR_KIND_SANITIZE_TYPE:
2196+
return Attribute::SanitizeType;
21952197
case bitc::ATTR_KIND_SANITIZE_MEMORY:
21962198
return Attribute::SanitizeMemory;
21972199
case bitc::ATTR_KIND_SANITIZE_NUMERICAL_STABILITY:

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
851851
return bitc::ATTR_KIND_SANITIZE_HWADDRESS;
852852
case Attribute::SanitizeThread:
853853
return bitc::ATTR_KIND_SANITIZE_THREAD;
854+
case Attribute::SanitizeType:
855+
return bitc::ATTR_KIND_SANITIZE_TYPE;
854856
case Attribute::SanitizeMemory:
855857
return bitc::ATTR_KIND_SANITIZE_MEMORY;
856858
case Attribute::SanitizeNumericalStability:

llvm/lib/CodeGen/ShrinkWrap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
986986
!(MF.getFunction().hasFnAttribute(Attribute::SanitizeAddress) ||
987987
MF.getFunction().hasFnAttribute(Attribute::SanitizeThread) ||
988988
MF.getFunction().hasFnAttribute(Attribute::SanitizeMemory) ||
989+
MF.getFunction().hasFnAttribute(Attribute::SanitizeType) ||
989990
MF.getFunction().hasFnAttribute(Attribute::SanitizeHWAddress));
990991
// If EnableShrinkWrap is set, it takes precedence on whatever the
991992
// target sets. The rational is that we assume we want to test

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
226226
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
227227
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
228+
#include "llvm/Transforms/Instrumentation/TypeSanitizer.h"
228229
#include "llvm/Transforms/ObjCARC.h"
229230
#include "llvm/Transforms/Scalar/ADCE.h"
230231
#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ MODULE_PASS("strip-nonlinetable-debuginfo", StripNonLineTableDebugInfoPass())
155155
MODULE_PASS("trigger-crash-module", TriggerCrashModulePass())
156156
MODULE_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
157157
MODULE_PASS("tsan-module", ModuleThreadSanitizerPass())
158+
MODULE_PASS("tysan-module", ModuleTypeSanitizerPass())
158159
MODULE_PASS("verify", VerifierPass())
159160
MODULE_PASS("view-callgraph", CallGraphViewerPass())
160161
MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass())
@@ -478,6 +479,7 @@ FUNCTION_PASS("transform-warning", WarnMissedTransformationsPass())
478479
FUNCTION_PASS("trigger-crash-function", TriggerCrashFunctionPass())
479480
FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
480481
FUNCTION_PASS("tsan", ThreadSanitizerPass())
482+
FUNCTION_PASS("tysan", TypeSanitizerPass())
481483
FUNCTION_PASS("typepromotion", TypePromotionPass(TM))
482484
FUNCTION_PASS("unify-loop-exits", UnifyLoopExitsPass())
483485
FUNCTION_PASS("vector-combine", VectorCombinePass())

llvm/lib/Transforms/Instrumentation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_llvm_component_library(LLVMInstrumentation
2424
SanitizerBinaryMetadata.cpp
2525
ValueProfileCollector.cpp
2626
ThreadSanitizer.cpp
27+
TypeSanitizer.cpp
2728
HWAddressSanitizer.cpp
2829
RealtimeSanitizer.cpp
2930

0 commit comments

Comments
 (0)