[PATCH] D129832: [sanitizer] Add "mainfile" prefix to sanitizer special case list

2022-07-15 Thread Fangrui Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG0d5a62faca59: [sanitizer] Add mainfile prefix to 
sanitizer special case list (authored by MaskRay).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D129832/new/

https://reviews.llvm.org/D129832

Files:
  clang/docs/SanitizerSpecialCaseList.rst
  clang/include/clang/Basic/NoSanitizeList.h
  clang/lib/Basic/NoSanitizeList.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/test/CodeGen/sanitize-ignorelist-mainfile.c
  llvm/include/llvm/Support/SpecialCaseList.h

Index: llvm/include/llvm/Support/SpecialCaseList.h
===
--- llvm/include/llvm/Support/SpecialCaseList.h
+++ llvm/include/llvm/Support/SpecialCaseList.h
@@ -19,9 +19,9 @@
 //   prefix:wildcard_expression[=category]
 // If category is not specified, it is assumed to be empty string.
 // Definitions of "prefix" and "category" are sanitizer-specific. For example,
-// sanitizer exclusion support prefixes "src", "fun" and "global".
-// Wildcard expressions define, respectively, source files, functions or
-// globals which shouldn't be instrumented.
+// sanitizer exclusion support prefixes "src", "mainfile", "fun" and "global".
+// Wildcard expressions define, respectively, source files, main files,
+// functions or globals which shouldn't be instrumented.
 // Examples of categories:
 //   "functional": used in DFSan to list functions with pure functional
 // semantics.
@@ -37,6 +37,7 @@
 // type:*Namespace::ClassName*=init
 // src:file_with_tricky_code.cc
 // src:ignore-global-initializers-issues.cc=init
+// mainfile:main_file.cc
 //
 // [dataflow]
 // # Functions with pure functional semantics:
Index: clang/test/CodeGen/sanitize-ignorelist-mainfile.c
===
--- /dev/null
+++ clang/test/CodeGen/sanitize-ignorelist-mainfile.c
@@ -0,0 +1,41 @@
+/// Test mainfile in a sanitizer special case list.
+// RUN: rm -rf %t && split-file %s %t && cd %t
+// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=address,alignment a.c -o - | FileCheck %s --check-prefixes=CHECK,DEFAULT
+// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=address,alignment -fsanitize-ignorelist=a.list a.c -o - | FileCheck %s --check-prefixes=CHECK,IGNORE
+// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=address,alignment -fsanitize-ignorelist=b.list a.c -o - | FileCheck %s --check-prefixes=CHECK,IGNORE
+
+//--- a.list
+mainfile:*a.c
+
+//--- b.list
+[address]
+mainfile:*a.c
+
+[alignment]
+mainfile:*.c
+
+//--- a.h
+int global_h;
+
+static inline int load(int *x) {
+  return *x;
+}
+
+//--- a.c
+#include "a.h"
+
+int global_c;
+
+int foo(void *x) {
+  return load(x);
+}
+
+// DEFAULT: @___asan_gen_{{.*}} = {{.*}} c"global_h\00"
+// DEFAULT: @___asan_gen_{{.*}} = {{.*}} c"global_c\00"
+// IGNORE-NOT:  @___asan_gen_
+
+// CHECK-LABEL: define {{.*}}@load(
+// DEFAULT:   call void @__ubsan_handle_type_mismatch_v1_abort(
+// DEFAULT:   call void @__asan_report_load4(
+// IGNORE-NOT:call void @__ubsan_handle_type_mismatch_v1_abort(
+// IGNORE-NOT:call void @__asan_report_load4(
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -2780,16 +2780,18 @@
   // NoSanitize by function name.
   if (NoSanitizeL.containsFunction(Kind, Fn->getName()))
 return true;
-  // NoSanitize by location.
+  // NoSanitize by location. Check "mainfile" prefix.
+  auto  = Context.getSourceManager();
+  const FileEntry  = *SM.getFileEntryForID(SM.getMainFileID());
+  if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
+return true;
+
+  // Check "src" prefix.
   if (Loc.isValid())
 return NoSanitizeL.containsLocation(Kind, Loc);
   // If location is unknown, this may be a compiler-generated function. Assume
   // it's located in the main file.
-  auto  = Context.getSourceManager();
-  if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
-return NoSanitizeL.containsFile(Kind, MainFile->getName());
-  }
-  return false;
+  return NoSanitizeL.containsFile(Kind, MainFile.getName());
 }
 
 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind,
@@ -2799,8 +2801,13 @@
   const auto  = getContext().getNoSanitizeList();
   if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category))
 return true;
+  auto  = Context.getSourceManager();
+  if (NoSanitizeL.containsMainFile(
+  Kind, SM.getFileEntryForID(SM.getMainFileID())->getName(), Category))
+return true;
   if (NoSanitizeL.containsLocation(Kind, Loc, Category))
 return true;
+
   // Check global type.
   if (!Ty.isNull()) {
 // Drill down the array types: if global variable of a fixed type is
Index: 

[PATCH] D129832: [sanitizer] Add "mainfile" prefix to sanitizer special case list

2022-07-15 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay updated this revision to Diff 445051.
MaskRay retitled this revision from "[sanitizer] Add "mainsrc" prefix to 
sanitizer special case list" to "[sanitizer] Add "mainfile" prefix to sanitizer 
special case list".
MaskRay edited the summary of this revision.
MaskRay added a comment.

mainsrc => mainfile

Clang names the file "main file". CC1 has options like -main-file-name.
Using "mainfile" instead of "mainsrc" avoids introducing new terminology.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D129832/new/

https://reviews.llvm.org/D129832

Files:
  clang/docs/SanitizerSpecialCaseList.rst
  clang/include/clang/Basic/NoSanitizeList.h
  clang/lib/Basic/NoSanitizeList.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/test/CodeGen/sanitize-ignorelist-mainfile.c
  llvm/include/llvm/Support/SpecialCaseList.h

Index: llvm/include/llvm/Support/SpecialCaseList.h
===
--- llvm/include/llvm/Support/SpecialCaseList.h
+++ llvm/include/llvm/Support/SpecialCaseList.h
@@ -19,9 +19,9 @@
 //   prefix:wildcard_expression[=category]
 // If category is not specified, it is assumed to be empty string.
 // Definitions of "prefix" and "category" are sanitizer-specific. For example,
-// sanitizer exclusion support prefixes "src", "fun" and "global".
-// Wildcard expressions define, respectively, source files, functions or
-// globals which shouldn't be instrumented.
+// sanitizer exclusion support prefixes "src", "mainfile", "fun" and "global".
+// Wildcard expressions define, respectively, source files, main files,
+// functions or globals which shouldn't be instrumented.
 // Examples of categories:
 //   "functional": used in DFSan to list functions with pure functional
 // semantics.
@@ -37,6 +37,7 @@
 // type:*Namespace::ClassName*=init
 // src:file_with_tricky_code.cc
 // src:ignore-global-initializers-issues.cc=init
+// mainfile:main_file.cc
 //
 // [dataflow]
 // # Functions with pure functional semantics:
Index: clang/test/CodeGen/sanitize-ignorelist-mainfile.c
===
--- /dev/null
+++ clang/test/CodeGen/sanitize-ignorelist-mainfile.c
@@ -0,0 +1,41 @@
+/// Test mainfile in a sanitizer special case list.
+// RUN: rm -rf %t && split-file %s %t && cd %t
+// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=address,alignment a.c -o - | FileCheck %s --check-prefixes=CHECK,DEFAULT
+// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=address,alignment -fsanitize-ignorelist=a.list a.c -o - | FileCheck %s --check-prefixes=CHECK,IGNORE
+// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=address,alignment -fsanitize-ignorelist=b.list a.c -o - | FileCheck %s --check-prefixes=CHECK,IGNORE
+
+//--- a.list
+mainfile:*a.c
+
+//--- b.list
+[address]
+mainfile:*a.c
+
+[alignment]
+mainfile:*.c
+
+//--- a.h
+int global_h;
+
+static inline int load(int *x) {
+  return *x;
+}
+
+//--- a.c
+#include "a.h"
+
+int global_c;
+
+int foo(void *x) {
+  return load(x);
+}
+
+// DEFAULT: @___asan_gen_{{.*}} = {{.*}} c"global_h\00"
+// DEFAULT: @___asan_gen_{{.*}} = {{.*}} c"global_c\00"
+// IGNORE-NOT:  @___asan_gen_
+
+// CHECK-LABEL: define {{.*}}@load(
+// DEFAULT:   call void @__ubsan_handle_type_mismatch_v1_abort(
+// DEFAULT:   call void @__asan_report_load4(
+// IGNORE-NOT:call void @__ubsan_handle_type_mismatch_v1_abort(
+// IGNORE-NOT:call void @__asan_report_load4(
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -2780,16 +2780,18 @@
   // NoSanitize by function name.
   if (NoSanitizeL.containsFunction(Kind, Fn->getName()))
 return true;
-  // NoSanitize by location.
+  // NoSanitize by location. Check "mainfile" prefix.
+  auto  = Context.getSourceManager();
+  const FileEntry  = *SM.getFileEntryForID(SM.getMainFileID());
+  if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
+return true;
+
+  // Check "src" prefix.
   if (Loc.isValid())
 return NoSanitizeL.containsLocation(Kind, Loc);
   // If location is unknown, this may be a compiler-generated function. Assume
   // it's located in the main file.
-  auto  = Context.getSourceManager();
-  if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
-return NoSanitizeL.containsFile(Kind, MainFile->getName());
-  }
-  return false;
+  return NoSanitizeL.containsFile(Kind, MainFile.getName());
 }
 
 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind,
@@ -2799,8 +2801,13 @@
   const auto  = getContext().getNoSanitizeList();
   if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category))
 return true;
+  auto  = Context.getSourceManager();
+  if (NoSanitizeL.containsMainFile(
+  Kind, SM.getFileEntryForID(SM.getMainFileID())->getName(), Category))
+return true;