[compiler-rt] r277536 - [sanitizer] Implement a __asan_default_options() equivalent for Scudo

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 2 15:25:39 PDT 2016


Author: kcc
Date: Tue Aug  2 17:25:38 2016
New Revision: 277536

URL: http://llvm.org/viewvc/llvm-project?rev=277536&view=rev
Log:
[sanitizer] Implement a __asan_default_options() equivalent for Scudo

Summary:
Currently, the Scudo Hardened Allocator only gets its flags via the SCUDO_OPTIONS environment variable.
With this patch, we offer the opportunity for programs to define their own options via __scudo_default_options() which behaves like __asan_default_options() (weak symbol).
A relevant test has been added as well, and the documentation updated accordingly.
I also used this patch as an opportunity to rename a few variables to comply with the LLVM naming scheme, and replaced a use of Report with dieWithMessage for consistency (and to avoid a callback).

Reviewers: llvm-commits, kcc

Differential Revision: https://reviews.llvm.org/D23018

Added:
    compiler-rt/trunk/docs/
    compiler-rt/trunk/projects/
    compiler-rt/trunk/projects/compiler-rt/
    compiler-rt/trunk/projects/compiler-rt/lib/
    compiler-rt/trunk/projects/compiler-rt/lib/scudo/
    compiler-rt/trunk/projects/compiler-rt/test/
    compiler-rt/trunk/projects/compiler-rt/test/scudo/
    compiler-rt/trunk/test/scudo/options.cpp
Modified:
    compiler-rt/trunk/lib/scudo/scudo_allocator.cpp
    compiler-rt/trunk/lib/scudo/scudo_flags.cpp
    compiler-rt/trunk/lib/scudo/scudo_termination.cpp
    compiler-rt/trunk/lib/scudo/scudo_utils.cpp

Modified: compiler-rt/trunk/lib/scudo/scudo_allocator.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_allocator.cpp?rev=277536&r1=277535&r2=277536&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_allocator.cpp (original)
+++ compiler-rt/trunk/lib/scudo/scudo_allocator.cpp Tue Aug  2 17:25:38 2016
@@ -76,7 +76,7 @@ struct UnpackedHeader {
   u64 Offset        : 20; // Offset from the beginning of the backend
                           // allocation to the beginning chunk itself, in
                           // multiples of MinAlignment. See comment about its
-                          // maximum value and test in Initialize.
+                          // maximum value and test in init().
   u64 Unused_1_     : 28;
   u16 Salt          : 16;
 };

Modified: compiler-rt/trunk/lib/scudo/scudo_flags.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_flags.cpp?rev=277536&r1=277535&r2=277536&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_flags.cpp (original)
+++ compiler-rt/trunk/lib/scudo/scudo_flags.cpp Tue Aug  2 17:25:38 2016
@@ -17,9 +17,12 @@
 #include "sanitizer_common/sanitizer_flags.h"
 #include "sanitizer_common/sanitizer_flag_parser.h"
 
+extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
+const char* __scudo_default_options();
+
 namespace __scudo {
 
-Flags scudo_flags_dont_use_directly;  // use via flags().
+Flags ScudoFlags;  // Use via getFlags().
 
 void Flags::setDefaults() {
 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
@@ -34,6 +37,10 @@ static void RegisterScudoFlags(FlagParse
 #undef SCUDO_FLAG
 }
 
+static const char *callGetScudoDefaultOptions() {
+  return (&__scudo_default_options) ? __scudo_default_options() : "";
+}
+
 void initFlags() {
   SetCommonFlagsDefaults();
   {
@@ -45,11 +52,16 @@ void initFlags() {
   Flags *f = getFlags();
   f->setDefaults();
 
-  FlagParser scudo_parser;
-  RegisterScudoFlags(&scudo_parser, f);
-  RegisterCommonFlags(&scudo_parser);
+  FlagParser ScudoParser;
+  RegisterScudoFlags(&ScudoParser, f);
+  RegisterCommonFlags(&ScudoParser);
+
+  // Override from user-specified string.
+  const char *ScudoDefaultOptions = callGetScudoDefaultOptions();
+  ScudoParser.ParseString(ScudoDefaultOptions);
 
-  scudo_parser.ParseString(GetEnv("SCUDO_OPTIONS"));
+  // Override from environment.
+  ScudoParser.ParseString(GetEnv("SCUDO_OPTIONS"));
 
   InitializeCommonFlags();
 
@@ -75,7 +87,7 @@ void initFlags() {
 }
 
 Flags *getFlags() {
-  return &scudo_flags_dont_use_directly;
+  return &ScudoFlags;
 }
 
 }

Modified: compiler-rt/trunk/lib/scudo/scudo_termination.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_termination.cpp?rev=277536&r1=277535&r2=277536&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_termination.cpp (original)
+++ compiler-rt/trunk/lib/scudo/scudo_termination.cpp Tue Aug  2 17:25:38 2016
@@ -13,15 +13,17 @@
 ///
 //===----------------------------------------------------------------------===//
 
+#include "scudo_utils.h"
+
 #include "sanitizer_common/sanitizer_common.h"
 
 namespace __sanitizer {
 
-bool AddDieCallback(DieCallbackType callback) { return true; }
+bool AddDieCallback(DieCallbackType Callback) { return true; }
 
-bool RemoveDieCallback(DieCallbackType callback) { return true; }
+bool RemoveDieCallback(DieCallbackType Callback) { return true; }
 
-void SetUserDieCallback(DieCallbackType callback) {}
+void SetUserDieCallback(DieCallbackType Callback) {}
 
 void NORETURN Die() {
   if (common_flags()->abort_on_error)
@@ -31,11 +33,10 @@ void NORETURN Die() {
 
 void SetCheckFailedCallback(CheckFailedCallbackType callback) {}
 
-void NORETURN CheckFailed(const char *file, int line, const char *cond,
-                          u64 v1, u64 v2) {
-  Report("Sanitizer CHECK failed: %s:%d %s (%lld, %lld)\n", file, line, cond,
-                                                            v1, v2);
-  Die();
+void NORETURN CheckFailed(const char *File, int Line, const char *Condition,
+                          u64 Value1, u64 Value2) {
+  __scudo::dieWithMessage("Scudo CHECK failed: %s:%d %s (%lld, %lld)\n",
+                          File, Line, Condition, Value1, Value2);
 }
 
 } // namespace __sanitizer

Modified: compiler-rt/trunk/lib/scudo/scudo_utils.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_utils.cpp?rev=277536&r1=277535&r2=277536&view=diff
==============================================================================
--- compiler-rt/trunk/lib/scudo/scudo_utils.cpp (original)
+++ compiler-rt/trunk/lib/scudo/scudo_utils.cpp Tue Aug  2 17:25:38 2016
@@ -33,7 +33,7 @@ extern int VSNPrintf(char *buff, int buf
 namespace __scudo {
 
 FORMAT(1, 2)
-void dieWithMessage(const char *Format, ...) {
+void NORETURN dieWithMessage(const char *Format, ...) {
   // Our messages are tiny, 128 characters is more than enough.
   char Message[128];
   va_list Args;

Added: compiler-rt/trunk/test/scudo/options.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/scudo/options.cpp?rev=277536&view=auto
==============================================================================
--- compiler-rt/trunk/test/scudo/options.cpp (added)
+++ compiler-rt/trunk/test/scudo/options.cpp Tue Aug  2 17:25:38 2016
@@ -0,0 +1,25 @@
+// RUN: %clang_scudo %s -o %t
+// RUN:                                              %run %t 2>&1
+// RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=0     %run %t 2>&1
+// RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=1 not %run %t 2>&1 | FileCheck %s
+
+// Tests that the options can be passed using getScudoDefaultOptions, and that
+// the environment ones take precedence over them.
+
+#include <stdlib.h>
+#include <malloc.h>
+
+extern "C" const char* __scudo_default_options() {
+  return "DeallocationTypeMismatch=0";  // Defaults to true in scudo_flags.inc.
+}
+
+int main(int argc, char **argv)
+{
+  int *p = (int *)malloc(16);
+  if (!p)
+    return 1;
+  delete p;
+  return 0;
+}
+
+// CHECK: ERROR: allocation type mismatch on address




More information about the llvm-commits mailing list