Make lsan-force-options and lsan-build-env more usable. Factor out common
authorIvan Baravy <i.baravy@samsung.com>
Mon, 27 Feb 2017 08:07:23 +0000 (11:07 +0300)
committerIvan Baravy <i.baravy@samsung.com>
Mon, 27 Feb 2017 08:07:23 +0000 (11:07 +0300)
code from {A, UB}San to sanitizer_common code.

Add -fno-omit-frame-pointer -U_FORTIFY_SOURCE to lsan-force-options because
LSan heavily relies on frame pointers. Disable LSan in QEMU builds through
LSAN_OPTIONS file. Refactor Tizen specific patches in ASan and UBSan.

libsanitizer/asan/asan_flags.cc
libsanitizer/asan/asan_rtl.cc
libsanitizer/lsan/lsan.cc
libsanitizer/sanitizer_common/sanitizer_common.cc
libsanitizer/sanitizer_common/sanitizer_common.h
libsanitizer/sanitizer_common/sanitizer_libc.cc
libsanitizer/sanitizer_common/sanitizer_libc.h
libsanitizer/ubsan/ubsan_flags.cc

index db257c4..4f87cd2 100644 (file)
@@ -105,21 +105,11 @@ void InitializeFlags() {
   ubsan_parser.ParseString(ubsan_default_options);
 #endif
 
-  // Read from command line.
-  const char *options = GetEnv("ASAN_OPTIONS");
-  // If environment variable ASAN_OPTIONS is not set use predefined file to
-  // init options.
-  bool getFlagsFromFile = options == 0;
-  char *asanFlags = 0;
-  uptr len = GetPageSizeCached();
-  uptr read_len = 0;
-  if (getFlagsFromFile) {
-    ReadFileToBuffer("/ASAN_OPTIONS", &asanFlags, &len, &read_len,
-                     GetPageSizeCached());
-    options = asanFlags;
-  }
-
+  int mmaped = 0;
+  const char *options =
+      GetRuntimeOptions("ASAN_OPTIONS", "/ASAN_OPTIONS", &mmaped);
   asan_parser.ParseString(options);
+
 #if CAN_SANITIZE_LEAKS
   lsan_parser.ParseString(GetEnv("LSAN_OPTIONS"));
 #endif
@@ -179,9 +169,8 @@ void InitializeFlags() {
     f->quarantine_size_mb = kDefaultQuarantineSizeMb;
   }
 
-  if (getFlagsFromFile)
-    UnmapOrDie(asanFlags, len);
-
+  if (mmaped)
+    UnmapOrDie((void *)options, GetPageSizeCached());
 }
 
 }  // namespace __asan
index 7f51016..d4dfc1d 100644 (file)
@@ -400,25 +400,11 @@ static void PrintAddressSpaceLayout() {
           kHighShadowBeg > kMidMemEnd);
 }
 
-extern "C"
-int mount(const char *source, const char *target, const char *filesystemtype,
-          unsigned long mountflags, const void *data);
-
 static void MaybeDisableUlimit() {
   if (!AddressSpaceIsUnlimited())
     SetAddressSpaceUnlimited();
 }
 
-bool MaybeMountProcFS() {
-  if (FileExists("/proc/self/maps")) return true;
-
-  if(0 != mount("proc", "/proc", "proc", 0, 0)) {
-    Report("Failed to mount /proc, Asan is likely to fail\n");
-    return false;
-  }
-  return true;
-}
-
 static void AsanInitInternal() {
   if (LIKELY(asan_inited)) return;
   MaybeMountProcFS();
index d544dfa..8d20bb6 100644 (file)
@@ -15,6 +15,7 @@
 #include "sanitizer_common/sanitizer_flags.h"
 #include "sanitizer_common/sanitizer_flag_parser.h"
 #include "sanitizer_common/sanitizer_stacktrace.h"
+#include "sanitizer_common/sanitizer_libc.h"
 #include "lsan_allocator.h"
 #include "lsan_common.h"
 #include "lsan_thread.h"
@@ -67,13 +68,20 @@ static void InitializeFlags() {
   RegisterLsanFlags(&parser, f);
   RegisterCommonFlags(&parser);
 
-  parser.ParseString(GetEnv("LSAN_OPTIONS"));
+  int mmaped = 0;
+  const char *options =
+      GetRuntimeOptions("LSAN_OPTIONS", "/LSAN_OPTIONS", &mmaped);
+  parser.ParseString(options);
 
   SetVerbosity(common_flags()->verbosity);
 
   if (Verbosity()) ReportUnrecognizedFlags();
 
   if (common_flags()->help) parser.PrintFlagDescriptions();
+  __sanitizer_set_report_path(common_flags()->log_path);
+
+  if (mmaped)
+    UnmapOrDie((void *)options, GetPageSizeCached());
 }
 
 extern "C" void __lsan_init() {
index 7c3f55d..5df61c2 100644 (file)
@@ -461,6 +461,24 @@ void PrintCmdline() {
   Printf("\n\n");
 }
 
+const char *GetRuntimeOptions(const char *env_option, const char *filename,
+                              int *mmaped) {
+  // Read from command line.
+  const char *options = GetEnv(env_option);
+  // If environment variable {A, L, UB}SAN_OPTIONS is not set use predefined
+  // file to init options.
+  bool getFlagsFromFile = options == 0;
+  char *flags = 0;
+  uptr len = GetPageSizeCached();
+  uptr read_len = 0;
+  if (getFlagsFromFile) {
+    ReadFileToBuffer(filename, &flags, &len, &read_len, GetPageSizeCached());
+    *mmaped = 1;
+    options = flags;
+  }
+  return options;
+}
+
 } // namespace __sanitizer
 
 using namespace __sanitizer;  // NOLINT
index 1550154..0ac1cd1 100644 (file)
@@ -712,6 +712,9 @@ struct SignalContext {
 
 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
 
+const char *GetRuntimeOptions(const char *env_option, const char *filename,
+                              int *mmaped);
+
 }  // namespace __sanitizer
 
 inline void *operator new(__sanitizer::operator_new_size_type size,
index 05fdca6..d3d9cfe 100644 (file)
@@ -258,4 +258,15 @@ bool mem_is_zero(const char *beg, uptr size) {
   return all == 0;
 }
 
+bool MaybeMountProcFS() {
+  if (FileExists("/proc/self/maps"))
+    return true;
+
+  if (0 != mount("proc", "/proc", "proc", 0, 0)) {
+    Report("Failed to mount /proc, %s is likely to fail\n", SanitizerToolName);
+    return false;
+  }
+  return true;
+}
+
 } // namespace __sanitizer
index 1b3f8ed..f9f5820 100644 (file)
@@ -76,6 +76,12 @@ uptr internal_sched_yield();
 // Error handling
 bool internal_iserror(uptr retval, int *rverrno = nullptr);
 
+extern "C" int mount(const char *source, const char *target,
+                     const char *filesystemtype, unsigned long mountflags,
+                     const void *data);
+
+bool MaybeMountProcFS();
+
 } // namespace __sanitizer
 
 #endif // SANITIZER_LIBC_H
index f3496d3..d94da27 100644 (file)
@@ -56,20 +56,13 @@ void InitializeFlags() {
   // Override from user-specified string.
   parser.ParseString(MaybeCallUbsanDefaultOptions());
 
-  // Read from command line.
-  const char *options = GetEnv("UBSAN_OPTIONS");
-  char *ubsanFlags = 0;
-  uptr len = GetPageSizeCached();
-  uptr read_len = 0;
-  bool getFlagsFromFile = ubsanFlags == 0;
-  // Override from predefined file if exists.
-  if (getFlagsFromFile) {
-    ReadFileToBuffer("/UBSAN_OPTIONS", &ubsanFlags, &len, &read_len,
-                     GetPageSizeCached());
-    options = ubsanFlags;
-  }
+  int mmaped = 0;
+  const char *options =
+      GetRuntimeOptions("UBSAN_OPTIONS", "/UBSAN_OPTIONS", &mmaped);
   parser.ParseString(options);
-  UnmapOrDie(ubsanFlags, len);
+
+  if (mmaped)
+    UnmapOrDie((void *)options, GetPageSizeCached());
   SetVerbosity(common_flags()->verbosity);
   if (Verbosity()) ReportUnrecognizedFlags();