Skip to content

Commit 201c9e3

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent 04c473b commit 201c9e3

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
@@ -724,6 +724,7 @@ enum AttributeKindCodes {
724724
ATTR_KIND_WRITABLE = 89,
725725
ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE = 90,
726726
ATTR_KIND_DEAD_ON_UNWIND = 91,
727+
ATTR_KIND_SANITIZE_TYPE = 92,
727728
};
728729

729730
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

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

273+
/// TypeSanitizer is on.
274+
def SanitizeType : EnumAttr<"sanitize_type", [FnAttr]>;
275+
273276
/// MemorySanitizer is on.
274277
def SanitizeMemory : EnumAttr<"sanitize_memory", [FnAttr]>;
275278

@@ -354,6 +357,7 @@ def : CompatRule<"isEqual<SanitizeThreadAttr>">;
354357
def : CompatRule<"isEqual<SanitizeMemoryAttr>">;
355358
def : CompatRule<"isEqual<SanitizeHWAddressAttr>">;
356359
def : CompatRule<"isEqual<SanitizeMemTagAttr>">;
360+
def : CompatRule<"isEqual<SanitizeTypeAttr>">;
357361
def : CompatRule<"isEqual<SafeStackAttr>">;
358362
def : CompatRule<"isEqual<ShadowCallStackAttr>">;
359363
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
@@ -2058,6 +2058,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
20582058
return Attribute::SanitizeHWAddress;
20592059
case bitc::ATTR_KIND_SANITIZE_THREAD:
20602060
return Attribute::SanitizeThread;
2061+
case bitc::ATTR_KIND_SANITIZE_TYPE:
2062+
return Attribute::SanitizeType;
20612063
case bitc::ATTR_KIND_SANITIZE_MEMORY:
20622064
return Attribute::SanitizeMemory;
20632065
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
@@ -789,6 +789,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
789789
return bitc::ATTR_KIND_SANITIZE_HWADDRESS;
790790
case Attribute::SanitizeThread:
791791
return bitc::ATTR_KIND_SANITIZE_THREAD;
792+
case Attribute::SanitizeType:
793+
return bitc::ATTR_KIND_SANITIZE_TYPE;
792794
case Attribute::SanitizeMemory:
793795
return bitc::ATTR_KIND_SANITIZE_MEMORY;
794796
case Attribute::SpeculativeLoadHardening:

llvm/lib/CodeGen/ShrinkWrap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
984984
!(MF.getFunction().hasFnAttribute(Attribute::SanitizeAddress) ||
985985
MF.getFunction().hasFnAttribute(Attribute::SanitizeThread) ||
986986
MF.getFunction().hasFnAttribute(Attribute::SanitizeMemory) ||
987+
MF.getFunction().hasFnAttribute(Attribute::SanitizeType) ||
987988
MF.getFunction().hasFnAttribute(Attribute::SanitizeHWAddress));
988989
// If EnableShrinkWrap is set, it takes precedence on whatever the
989990
// 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
@@ -167,6 +167,7 @@
167167
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
168168
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
169169
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
170+
#include "llvm/Transforms/Instrumentation/TypeSanitizer.h"
170171
#include "llvm/Transforms/ObjCARC.h"
171172
#include "llvm/Transforms/Scalar/ADCE.h"
172173
#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())
@@ -417,6 +418,7 @@ FUNCTION_PASS("tlshoist", TLSVariableHoistPass())
417418
FUNCTION_PASS("transform-warning", WarnMissedTransformationsPass())
418419
FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
419420
FUNCTION_PASS("tsan", ThreadSanitizerPass())
421+
FUNCTION_PASS("tysan", TypeSanitizerPass())
420422
FUNCTION_PASS("typepromotion", TypePromotionPass(TM))
421423
FUNCTION_PASS("unify-loop-exits", UnifyLoopExitsPass())
422424
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
@@ -20,6 +20,7 @@ add_llvm_component_library(LLVMInstrumentation
2020
SanitizerBinaryMetadata.cpp
2121
ValueProfileCollector.cpp
2222
ThreadSanitizer.cpp
23+
TypeSanitizer.cpp
2324
HWAddressSanitizer.cpp
2425

2526
ADDITIONAL_HEADER_DIRS

0 commit comments

Comments
 (0)