Skip to content

[UBSan][Ignorelist] Expanding =sanitize to fun. #142074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/NoSanitizeList.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SanitizerSpecialCaseList;
class NoSanitizeList {
std::unique_ptr<SanitizerSpecialCaseList> SSCL;
SourceManager &SM;
bool containsPrefix(SanitizerMask Mask, StringRef Prefix, StringRef Name,
StringRef Category = StringRef()) const;

public:
NoSanitizeList(const std::vector<std::string> &NoSanitizeListPaths,
Expand Down
29 changes: 18 additions & 11 deletions clang/lib/Basic/NoSanitizeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,39 @@ NoSanitizeList::NoSanitizeList(const std::vector<std::string> &NoSanitizePaths,

NoSanitizeList::~NoSanitizeList() = default;

bool NoSanitizeList::containsPrefix(SanitizerMask Mask, StringRef Prefix,
StringRef Name, StringRef Category) const {
std::pair<unsigned, unsigned> NoSan =
SSCL->inSectionBlame(Mask, Prefix, Name, Category);
if (NoSan == llvm::SpecialCaseList::NotFound)
return false;
std::pair<unsigned, unsigned> San =
SSCL->inSectionBlame(Mask, Prefix, Name, "sanitize");
// The statement evaluates to true under the following conditions:
// 1. The string "prefix:*=sanitize" is absent.
// 2. If "prefix:*=sanitize" is present, its (File Index, Line Number) is less
// than that of "prefix:*".
return San == llvm::SpecialCaseList::NotFound || NoSan > San;
}

bool NoSanitizeList::containsGlobal(SanitizerMask Mask, StringRef GlobalName,
StringRef Category) const {
return SSCL->inSection(Mask, "global", GlobalName, Category);
}

bool NoSanitizeList::containsType(SanitizerMask Mask, StringRef MangledTypeName,
StringRef Category) const {
auto NoSan = SSCL->inSectionBlame(Mask, "type", MangledTypeName, Category);
if (NoSan == llvm::SpecialCaseList::NotFound)
return false;
auto San = SSCL->inSectionBlame(Mask, "type", MangledTypeName, "sanitize");
return San == llvm::SpecialCaseList::NotFound || NoSan > San;
return containsPrefix(Mask, "type", MangledTypeName, Category);
}

bool NoSanitizeList::containsFunction(SanitizerMask Mask,
StringRef FunctionName) const {
return SSCL->inSection(Mask, "fun", FunctionName);
return containsPrefix(Mask, "fun", FunctionName);
}

bool NoSanitizeList::containsFile(SanitizerMask Mask, StringRef FileName,
StringRef Category) const {
auto NoSan = SSCL->inSectionBlame(Mask, "src", FileName, Category);
if (NoSan == llvm::SpecialCaseList::NotFound)
return false;
auto San = SSCL->inSectionBlame(Mask, "src", FileName, "sanitize");
return San == llvm::SpecialCaseList::NotFound || NoSan > San;
return containsPrefix(Mask, "src", FileName, Category);
}

bool NoSanitizeList::containsMainFile(SanitizerMask Mask, StringRef FileName,
Expand Down
83 changes: 83 additions & 0 deletions clang/test/CodeGen/ubsan-function-ignorelist.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// RUN: rm -rf %t
// RUN: split-file %s %t

// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-0.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,SANITIZE
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-1.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,IGNORE
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-2.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,SANITIZE
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-3.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,IGNORE
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-4.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,SANITIZE
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-5.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,IGNORE
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-6.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,SANITIZE
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-7.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,IGNORE
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-ignorelist=%t/order-8.ignorelist -emit-llvm %t/test.c -o - | FileCheck %s --check-prefixes=CHECK,SANITIZE


// The same type can appear multiple times within an ignorelist. Any ``=sanitize`` type
// entries enable sanitizer instrumentation, even if it was ignored by entries before.
// If multiple entries match the source, then the latest entry takes the
// precedence.


//--- order-0.ignorelist
fun:add
fun:add=sanitize

//--- order-1.ignorelist
fun:add=sanitize
fun:add

//--- order-2.ignorelist
fun:ad*
fun:add=sanitize

//--- order-3.ignorelist
fun:ad*=sanitize
fun:add

//--- order-4.ignorelist
fun:add
fun:ad*=sanitize

//--- order-5.ignorelist
fun:add=sanitize
fun:ad*

//--- order-6.ignorelist
fun:add
fun:add=sanitize
fun:a*d
fun:*dd=sanitize

//--- order-7.ignorelist
[{unsigned-integer-overflow,signed-integer-overflow}]
fun:*
fun:add=sanitize
fun:a*d
fun:*dd=sanitize
[{unsigned-integer-overflow,signed-integer-overflow}]
fun:*
fun:add
fun:a*d=sanitize
fun:*d

//--- order-8.ignorelist
[{unsigned-integer-overflow,signed-integer-overflow}]
fun:*
fun:add
fun:a*d=sanitize
fun:*dd
[{unsigned-integer-overflow,signed-integer-overflow}]
fun:*
fun:add=sanitize
fun:a*d
fun:*dd=sanitize


//--- test.c
// CHECK-LABEL: define dso_local void @test
void add(int A) {
// IGNORE: %inc = add nsw
// SANITIZE: @llvm.sadd.with.overflow.i32
++A;
}

Loading