Skip to content

Commit fa25c9b

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent e9b8cd0 commit fa25c9b

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
@@ -756,6 +756,7 @@ enum AttributeKindCodes {
756756
ATTR_KIND_RANGE = 92,
757757
ATTR_KIND_SANITIZE_NUMERICAL_STABILITY = 93,
758758
ATTR_KIND_INITIALIZES = 94,
759+
ATTR_KIND_SANITIZE_TYPE = 95,
759760
};
760761

761762
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

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

285+
/// TypeSanitizer is on.
286+
def SanitizeType : EnumAttr<"sanitize_type", [FnAttr]>;
287+
285288
/// MemorySanitizer is on.
286289
def SanitizeMemory : EnumAttr<"sanitize_memory", [FnAttr]>;
287290

@@ -382,6 +385,7 @@ def : CompatRule<"isEqual<SanitizeMemoryAttr>">;
382385
def : CompatRule<"isEqual<SanitizeHWAddressAttr>">;
383386
def : CompatRule<"isEqual<SanitizeMemTagAttr>">;
384387
def : CompatRule<"isEqual<SanitizeNumericalStabilityAttr>">;
388+
def : CompatRule<"isEqual<SanitizeTypeAttr>">;
385389
def : CompatRule<"isEqual<SafeStackAttr>">;
386390
def : CompatRule<"isEqual<ShadowCallStackAttr>">;
387391
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
@@ -2137,6 +2137,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
21372137
return Attribute::SanitizeHWAddress;
21382138
case bitc::ATTR_KIND_SANITIZE_THREAD:
21392139
return Attribute::SanitizeThread;
2140+
case bitc::ATTR_KIND_SANITIZE_TYPE:
2141+
return Attribute::SanitizeType;
21402142
case bitc::ATTR_KIND_SANITIZE_MEMORY:
21412143
return Attribute::SanitizeMemory;
21422144
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
@@ -827,6 +827,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
827827
return bitc::ATTR_KIND_SANITIZE_HWADDRESS;
828828
case Attribute::SanitizeThread:
829829
return bitc::ATTR_KIND_SANITIZE_THREAD;
830+
case Attribute::SanitizeType:
831+
return bitc::ATTR_KIND_SANITIZE_TYPE;
830832
case Attribute::SanitizeMemory:
831833
return bitc::ATTR_KIND_SANITIZE_MEMORY;
832834
case Attribute::SanitizeNumericalStability:

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
@@ -190,6 +190,7 @@
190190
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
191191
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
192192
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
193+
#include "llvm/Transforms/Instrumentation/TypeSanitizer.h"
193194
#include "llvm/Transforms/ObjCARC.h"
194195
#include "llvm/Transforms/Scalar/ADCE.h"
195196
#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
@@ -140,6 +140,7 @@ MODULE_PASS("synthetic-counts-propagation", SyntheticCountsPropagation())
140140
MODULE_PASS("trigger-crash-module", TriggerCrashModulePass())
141141
MODULE_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
142142
MODULE_PASS("tsan-module", ModuleThreadSanitizerPass())
143+
MODULE_PASS("tysan-module", ModuleTypeSanitizerPass())
143144
MODULE_PASS("verify", VerifierPass())
144145
MODULE_PASS("view-callgraph", CallGraphViewerPass())
145146
MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass())
@@ -462,6 +463,7 @@ FUNCTION_PASS("transform-warning", WarnMissedTransformationsPass())
462463
FUNCTION_PASS("trigger-crash-function", TriggerCrashFunctionPass())
463464
FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
464465
FUNCTION_PASS("tsan", ThreadSanitizerPass())
466+
FUNCTION_PASS("tysan", TypeSanitizerPass())
465467
FUNCTION_PASS("typepromotion", TypePromotionPass(TM))
466468
FUNCTION_PASS("unify-loop-exits", UnifyLoopExitsPass())
467469
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
@@ -23,6 +23,7 @@ add_llvm_component_library(LLVMInstrumentation
2323
SanitizerBinaryMetadata.cpp
2424
ValueProfileCollector.cpp
2525
ThreadSanitizer.cpp
26+
TypeSanitizer.cpp
2627
HWAddressSanitizer.cpp
2728

2829
ADDITIONAL_HEADER_DIRS

0 commit comments

Comments
 (0)