Skip to content

Commit 29460f7

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent 3e64f8a commit 29460f7

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
@@ -744,6 +744,7 @@ enum AttributeKindCodes {
744744
ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE = 90,
745745
ATTR_KIND_DEAD_ON_UNWIND = 91,
746746
ATTR_KIND_RANGE = 92,
747+
ATTR_KIND_SANITIZE_TYPE = 93,
747748
};
748749

749750
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

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

279+
/// TypeSanitizer is on.
280+
def SanitizeType : EnumAttr<"sanitize_type", [FnAttr]>;
281+
279282
/// MemorySanitizer is on.
280283
def SanitizeMemory : EnumAttr<"sanitize_memory", [FnAttr]>;
281284

@@ -372,6 +375,7 @@ def : CompatRule<"isEqual<SanitizeThreadAttr>">;
372375
def : CompatRule<"isEqual<SanitizeMemoryAttr>">;
373376
def : CompatRule<"isEqual<SanitizeHWAddressAttr>">;
374377
def : CompatRule<"isEqual<SanitizeMemTagAttr>">;
378+
def : CompatRule<"isEqual<SanitizeTypeAttr>">;
375379
def : CompatRule<"isEqual<SafeStackAttr>">;
376380
def : CompatRule<"isEqual<ShadowCallStackAttr>">;
377381
def : CompatRule<"isEqual<UseSampleProfileAttr>">;
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
@@ -371,11 +371,27 @@ static bool isStructPathTBAA(const MDNode *MD) {
371371
return isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
372372
}
373373

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

380396
if (Aliases(LocA.AATags.TBAA, LocB.AATags.TBAA))
381397
return AliasResult::MayAlias;
@@ -425,8 +441,8 @@ MemoryEffects TypeBasedAAResult::getMemoryEffects(const Function *F) {
425441
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
426442
const MemoryLocation &Loc,
427443
AAQueryInfo &AAQI) {
428-
if (!EnableTBAA)
429-
return ModRefInfo::ModRef;
444+
if (!EnableTBAA || usingSanitizeType(Call))
445+
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
430446

431447
if (const MDNode *L = Loc.AATags.TBAA)
432448
if (const MDNode *M = Call->getMetadata(LLVMContext::MD_tbaa))
@@ -439,8 +455,8 @@ ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
439455
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1,
440456
const CallBase *Call2,
441457
AAQueryInfo &AAQI) {
442-
if (!EnableTBAA)
443-
return ModRefInfo::ModRef;
458+
if (!EnableTBAA || usingSanitizeType(Call1))
459+
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
444460

445461
if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa))
446462
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
@@ -2104,6 +2104,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
21042104
return Attribute::SanitizeHWAddress;
21052105
case bitc::ATTR_KIND_SANITIZE_THREAD:
21062106
return Attribute::SanitizeThread;
2107+
case bitc::ATTR_KIND_SANITIZE_TYPE:
2108+
return Attribute::SanitizeType;
21072109
case bitc::ATTR_KIND_SANITIZE_MEMORY:
21082110
return Attribute::SanitizeMemory;
21092111
case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
817817
return bitc::ATTR_KIND_SANITIZE_HWADDRESS;
818818
case Attribute::SanitizeThread:
819819
return bitc::ATTR_KIND_SANITIZE_THREAD;
820+
case Attribute::SanitizeType:
821+
return bitc::ATTR_KIND_SANITIZE_TYPE;
820822
case Attribute::SanitizeMemory:
821823
return bitc::ATTR_KIND_SANITIZE_MEMORY;
822824
case Attribute::SpeculativeLoadHardening:

llvm/lib/CodeGen/ShrinkWrap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
989989
!(MF.getFunction().hasFnAttribute(Attribute::SanitizeAddress) ||
990990
MF.getFunction().hasFnAttribute(Attribute::SanitizeThread) ||
991991
MF.getFunction().hasFnAttribute(Attribute::SanitizeMemory) ||
992+
MF.getFunction().hasFnAttribute(Attribute::SanitizeType) ||
992993
MF.getFunction().hasFnAttribute(Attribute::SanitizeHWAddress));
993994
// If EnableShrinkWrap is set, it takes precedence on whatever the
994995
// 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
@@ -181,6 +181,7 @@
181181
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
182182
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
183183
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
184+
#include "llvm/Transforms/Instrumentation/TypeSanitizer.h"
184185
#include "llvm/Transforms/ObjCARC.h"
185186
#include "llvm/Transforms/Scalar/ADCE.h"
186187
#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
@@ -138,6 +138,7 @@ MODULE_PASS("synthetic-counts-propagation", SyntheticCountsPropagation())
138138
MODULE_PASS("trigger-crash", TriggerCrashPass())
139139
MODULE_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
140140
MODULE_PASS("tsan-module", ModuleThreadSanitizerPass())
141+
MODULE_PASS("tysan-module", ModuleTypeSanitizerPass())
141142
MODULE_PASS("verify", VerifierPass())
142143
MODULE_PASS("view-callgraph", CallGraphViewerPass())
143144
MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass())
@@ -442,6 +443,7 @@ FUNCTION_PASS("tlshoist", TLSVariableHoistPass())
442443
FUNCTION_PASS("transform-warning", WarnMissedTransformationsPass())
443444
FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
444445
FUNCTION_PASS("tsan", ThreadSanitizerPass())
446+
FUNCTION_PASS("tysan", TypeSanitizerPass())
445447
FUNCTION_PASS("typepromotion", TypePromotionPass(TM))
446448
FUNCTION_PASS("unify-loop-exits", UnifyLoopExitsPass())
447449
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
@@ -22,6 +22,7 @@ add_llvm_component_library(LLVMInstrumentation
2222
SanitizerBinaryMetadata.cpp
2323
ValueProfileCollector.cpp
2424
ThreadSanitizer.cpp
25+
TypeSanitizer.cpp
2526
HWAddressSanitizer.cpp
2627

2728
ADDITIONAL_HEADER_DIRS

0 commit comments

Comments
 (0)