sanitizer.patch

From: Christian Prochaska <christian.prochaska@genode-labs.com>


---
 sanitizer_common/sanitizer_genode.cc               |  330 ++++++++++++++++++++
 sanitizer_common/sanitizer_internal_defs.h         |    6 
 sanitizer_common/sanitizer_platform.h              |    4 
 sanitizer_common/sanitizer_posix.h                 |    2 
 sanitizer_common/sanitizer_printf.cc               |    2 
 sanitizer_common/sanitizer_stacktrace.h            |    2 
 sanitizer_common/sanitizer_symbolizer_libcdep.cc   |   10 -
 .../sanitizer_symbolizer_posix_libcdep.cc          |   31 +-
 ubsan/ubsan_diag.cc                                |    4 
 9 files changed, 376 insertions(+), 15 deletions(-)
 create mode 100644 sanitizer_common/sanitizer_genode.cc

diff --git a/sanitizer_common/sanitizer_genode.cc b/sanitizer_common/sanitizer_genode.cc
new file mode 100644
index 0000000..cd2bc92
--- /dev/null
+++ b/sanitizer_common/sanitizer_genode.cc
@@ -0,0 +1,330 @@
+/*
+ * \brief  Genode-specific functions from sanitizer_common.h
+ *         and sanitizer_libc.h
+ * \author Christian Prochaska
+ * \date   2018-12-05
+ */
+
+/*
+ * Copyright (C) 2018 Genode Labs GmbH
+ *
+ * This file is part of the Genode OS framework, which is distributed
+ * under the terms of the GNU Affero General Public License version 3.
+ */
+
+#include <base/env.h>
+#include <base/heap.h>
+#include <base/log.h>
+#include <base/buffered_output.h>
+
+#include "sanitizer_common.h"
+#include "sanitizer_file.h"
+#include "sanitizer_platform.h"
+
+using namespace __sanitizer;
+
+
+extern int  genode_atexit(void (*func)(void));
+extern void genode_exit(int) __attribute__((noreturn));
+
+
+static constexpr bool verbose = false;
+
+
+/*
+ * Cannot use 'Genode::Constructible' for the environment, because its
+ * constructor would need to be called before 'sanitizer_init()'.
+ */
+
+static Genode::Env *genode_env;
+
+static Genode::Heap &heap()
+{
+	struct Sanitizer_not_initialized { };
+	if (!genode_env) throw Sanitizer_not_initialized();
+
+	static Genode::Heap instance(genode_env->ram(), genode_env->rm());
+	return instance;
+}
+
+
+void sanitizer_init(Genode::Env &env)
+{
+Genode::log("sanitizer_init()");
+	genode_env = &env;
+}
+
+
+/* sanitizer_common.h */
+
+
+void __sanitizer::Abort()
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	genode_exit(-1);
+}
+
+
+int __sanitizer::Atexit(void (*function)(void))
+{
+	return genode_atexit(function);
+}
+
+
+uptr __sanitizer::GetThreadSelf() {
+  return 0;
+}
+
+
+bool __sanitizer::SupportsColoredOutput(fd_t fd)
+{
+	if (fd == 2)
+		return true;
+	else
+		return false;
+}
+
+
+bool __sanitizer::IsAccessibleMemoryRange(uptr beg, uptr size)
+{
+	if (verbose)
+		Genode::error("sanitizer: ", __func__, " called");
+	return true;
+}
+
+
+void __sanitizer::PrepareForSandboxing(__sanitizer_sandbox_arguments *args)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+}
+
+
+const char *__sanitizer::GetEnv(const char *name)
+{
+	if (verbose)
+		Genode::log("sanitizer: ", __func__,
+		            ": ", Genode::Cstring(name));
+	return nullptr;
+}
+
+
+__sanitizer::BlockingMutex::BlockingMutex()
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	internal_memset(this, 0, sizeof(*this));
+}
+
+
+void __sanitizer::BlockingMutex::Lock()
+{
+	Genode::error("sanitizer: ", __func__, " called");
+}
+
+
+void __sanitizer::BlockingMutex::Unlock()
+{
+	Genode::error("sanitizer: ", __func__, " called");
+}
+
+
+void __sanitizer::BlockingMutex::CheckLocked()
+{
+	Genode::error("sanitizer: ", __func__, " called");
+}
+
+
+uptr __sanitizer::internal_getpid()
+{
+	return 0;
+}
+
+
+uptr __sanitizer::GetPageSize()
+{
+	if (verbose)
+		Genode::warning("sanitizer: ", __func__, " called, returning 4096");
+	return 4096;
+}
+
+
+uptr __sanitizer::ReadBinaryName(char *buf, uptr buf_len)
+{
+	return internal_snprintf(buf, buf_len, "binary");
+}
+
+
+uptr __sanitizer::ReadLongProcessName(char *buf, uptr buf_len)
+{
+	internal_strncpy(buf, "process", buf_len);
+	return internal_strlen(buf);
+}
+
+
+uptr __sanitizer::GetMmapGranularity()
+{
+	return GetPageSize();
+}
+
+
+void *__sanitizer::MmapOrDie(uptr size, const char *mem_type, bool raw_report)
+{
+	size = RoundUpTo(size, GetPageSizeCached());
+
+	void *res = nullptr;
+	heap().alloc(size, &res);
+
+	if (res == nullptr)
+		ReportMmapFailureAndDie(size, mem_type, "allocate", 0, raw_report);
+
+	IncreaseTotalMmap(size);
+	return res;
+}
+
+
+void __sanitizer::UnmapOrDie(void *addr, uptr size)
+{
+	if (!addr || !size) return;
+
+	heap().free(addr, size);
+
+	DecreaseTotalMmap(size);
+}
+
+
+void *__sanitizer::MmapNoReserveOrDie(uptr size, const char *mem_type)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return nullptr;
+}
+
+
+bool __sanitizer::MprotectNoAccess(uptr addr, uptr size)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return false;
+}
+
+
+uptr __sanitizer::GetRSS()
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return 0;
+}
+
+
+fd_t __sanitizer::OpenFile(const char *filename,
+                           FileAccessMode mode,
+                           error_t *errno_p)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return kInvalidFd;
+}
+
+
+void __sanitizer::CloseFile(fd_t fd)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+}
+
+
+bool __sanitizer::ReadFromFile(fd_t fd, void *buff, uptr buff_size,
+                               uptr *bytes_read, error_t *error_p)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return false;
+}
+
+
+bool __sanitizer::WriteToFile(fd_t fd, const void *buff, uptr buff_size,
+                              uptr *bytes_written, error_t *error_p)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return true;
+}
+
+
+bool __sanitizer::RenameFile(const char *oldpath, const char *newpath,
+                             error_t *error_p)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return false;
+}
+
+
+void *__sanitizer::MapWritableFileToMemory(void *addr, uptr size, fd_t fd,
+                                           OFF_T offset)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return nullptr;
+}
+
+
+void __sanitizer::DumpProcessMap()
+{
+	Genode::error("sanitizer: ", __func__, " called");
+}
+
+
+bool __sanitizer::IsPathSeparator(const char c)
+{
+	return c == '/';
+}
+
+
+bool __sanitizer::IsAbsolutePath(const char *path)
+{
+	return path != nullptr && IsPathSeparator(path[0]);
+}
+
+
+void __sanitizer::ReportFile::Write(const char *buffer, uptr length)
+{
+	SpinMutexLock l(mu);
+
+	struct Write_fn
+	{
+		void operator () (char const *s) { Genode::log(Genode::Cstring(s)); }
+	} function { };
+
+	static Genode::Buffered_output<512, Write_fn> buffered_output { function };
+
+	for (uptr i = 0; i < length; i++)
+		buffered_output.out_char(buffer[i]);
+}
+
+
+void __sanitizer::SleepForMillis(int millis)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+}
+
+
+/* sanitizer_libc.h */
+
+
+uptr __sanitizer::internal_sched_yield()
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return 0;
+}
+
+
+uptr __sanitizer::internal_ftruncate(fd_t fd, uptr size)
+{
+	Genode::error("sanitizer: ", __func__, " called");
+	return 0;
+}
+
+
+void __sanitizer::internal__exit(int exitcode)
+{
+	genode_exit(exitcode);
+}
+
+
+bool __sanitizer::internal_iserror(uptr retval, int *rverrno)
+{
+	Genode::warning("sanitizer: ", __func__,
+	                " called, returning false");
+	return false;
+}
diff --git a/sanitizer_common/sanitizer_internal_defs.h b/sanitizer_common/sanitizer_internal_defs.h
index edd6a21..a927498 100644
--- a/sanitizer_common/sanitizer_internal_defs.h
+++ b/sanitizer_common/sanitizer_internal_defs.h
@@ -13,6 +13,8 @@
 
 #include "sanitizer_platform.h"
 
+#include <stddef.h>
+
 #ifndef SANITIZER_DEBUG
 # define SANITIZER_DEBUG 0
 #endif
@@ -152,13 +154,13 @@ typedef uptr OFF_T;
 typedef u64  OFF64_T;
 
 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
-typedef uptr operator_new_size_type;
+typedef size_t operator_new_size_type;
 #else
 # if defined(__s390__) && !defined(__s390x__)
 // Special case: 31-bit s390 has unsigned long as size_t.
 typedef unsigned long operator_new_size_type;
 # else
-typedef u32 operator_new_size_type;
+typedef size_t operator_new_size_type;
 # endif
 #endif
 
diff --git a/sanitizer_common/sanitizer_platform.h b/sanitizer_common/sanitizer_platform.h
index 1eb4d0c..95701c2 100644
--- a/sanitizer_common/sanitizer_platform.h
+++ b/sanitizer_common/sanitizer_platform.h
@@ -11,10 +11,12 @@
 #ifndef SANITIZER_PLATFORM_H
 #define SANITIZER_PLATFORM_H
 
+#if 0
 #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
   !defined(__APPLE__) && !defined(_WIN32) && !defined(__Fuchsia__)
 # error "This operating system is not supported"
 #endif
+#endif
 
 #if defined(__linux__)
 # define SANITIZER_LINUX   1
@@ -90,7 +92,7 @@
 #endif
 
 #define SANITIZER_POSIX \
-  (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_MAC || SANITIZER_NETBSD)
+  (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_MAC || SANITIZER_NETBSD || 1)
 
 #if __LP64__ || defined(_WIN64)
 #  define SANITIZER_WORDSIZE 64
diff --git a/sanitizer_common/sanitizer_posix.h b/sanitizer_common/sanitizer_posix.h
index 9626654..2afbdc0 100644
--- a/sanitizer_common/sanitizer_posix.h
+++ b/sanitizer_common/sanitizer_posix.h
@@ -80,9 +80,11 @@ int my_pthread_attr_getstack(void *attr, void **addr, uptr *size);
 // A routine named real_sigaction() must be implemented by each sanitizer in
 // order for internal_sigaction() to bypass interceptors.
 int internal_sigaction(int signum, const void *act, void *oldact);
+#if 0
 void internal_sigfillset(__sanitizer_sigset_t *set);
 void internal_sigemptyset(__sanitizer_sigset_t *set);
 bool internal_sigismember(__sanitizer_sigset_t *set, int signum);
+#endif
 
 uptr internal_execve(const char *filename, char *const argv[],
                      char *const envp[]);
diff --git a/sanitizer_common/sanitizer_printf.cc b/sanitizer_common/sanitizer_printf.cc
index 1456c76..d6d46b3 100644
--- a/sanitizer_common/sanitizer_printf.cc
+++ b/sanitizer_common/sanitizer_printf.cc
@@ -16,7 +16,9 @@
 #include "sanitizer_flags.h"
 #include "sanitizer_libc.h"
 
+#if 0
 #include <stdio.h>
+#endif
 #include <stdarg.h>
 
 #if SANITIZER_WINDOWS && defined(_MSC_VER) && _MSC_VER < 1800 &&               \
diff --git a/sanitizer_common/sanitizer_stacktrace.h b/sanitizer_common/sanitizer_stacktrace.h
index 31e99f6..47c7204 100644
--- a/sanitizer_common/sanitizer_stacktrace.h
+++ b/sanitizer_common/sanitizer_stacktrace.h
@@ -31,7 +31,7 @@ static const u32 kStackTraceMax = 256;
 #if SANITIZER_MAC
 # define SANITIZER_CAN_SLOW_UNWIND 0
 #else
-# define SANITIZER_CAN_SLOW_UNWIND 1
+# define SANITIZER_CAN_SLOW_UNWIND 0
 #endif
 
 struct StackTrace {
diff --git a/sanitizer_common/sanitizer_symbolizer_libcdep.cc b/sanitizer_common/sanitizer_symbolizer_libcdep.cc
index df93441..fd65874 100644
--- a/sanitizer_common/sanitizer_symbolizer_libcdep.cc
+++ b/sanitizer_common/sanitizer_symbolizer_libcdep.cc
@@ -9,6 +9,8 @@
 // run-time libraries.
 //===----------------------------------------------------------------------===//
 
+#include <base/log.h>
+
 #include "sanitizer_allocator_internal.h"
 #include "sanitizer_internal_defs.h"
 #include "sanitizer_symbolizer_internal.h"
@@ -167,7 +169,7 @@ void Symbolizer::RefreshModules() {
   RAW_CHECK(modules_.size() > 0);
   modules_fresh_ = true;
 }
-
+#if 0
 static const LoadedModule *SearchForModule(const ListOfModules &modules,
                                            uptr address) {
   for (uptr i = 0; i < modules.size(); i++) {
@@ -177,8 +179,11 @@ static const LoadedModule *SearchForModule(const ListOfModules &modules,
   }
   return nullptr;
 }
-
+#endif
 const LoadedModule *Symbolizer::FindModuleForAddress(uptr address) {
+  Genode::error("sanitizer: ", __func__, " called");
+  return 0;
+#if 0
   bool modules_were_reloaded = false;
   if (!modules_fresh_) {
     RefreshModules();
@@ -202,6 +207,7 @@ const LoadedModule *Symbolizer::FindModuleForAddress(uptr address) {
     module = SearchForModule(fallback_modules_, address);
   }
   return module;
+#endif
 }
 
 // For now we assume the following protocol:
diff --git a/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc b/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc
index afd6bbe..9a34d2f 100644
--- a/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc
+++ b/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc
@@ -10,6 +10,9 @@
 // POSIX-specific implementation of symbolizer parts.
 //===----------------------------------------------------------------------===//
 
+#include <base/log.h>
+#include <stddef.h>
+
 #include "sanitizer_platform.h"
 #if SANITIZER_POSIX
 #include "sanitizer_allocator_internal.h"
@@ -25,13 +28,14 @@
 #include "sanitizer_symbolizer_libbacktrace.h"
 #include "sanitizer_symbolizer_mac.h"
 
+#if 0
 #include <dlfcn.h>   // for dlsym()
 #include <errno.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <sys/wait.h>
 #include <unistd.h>
-
+#endif
 #if SANITIZER_MAC
 #include <util.h>  // for forkpty()
 #endif  // SANITIZER_MAC
@@ -61,7 +65,7 @@ const char *DemangleCXXABI(const char *name) {
 
   return name;
 }
-
+#if 0
 // As of now, there are no headers for the Swift runtime. Once they are
 // present, we will weakly link since we do not require Swift runtime to be
 // linked.
@@ -92,14 +96,16 @@ const char *DemangleSwift(const char *name) {
 
   return nullptr;
 }
-
+#endif
 const char *DemangleSwiftAndCXX(const char *name) {
   if (!name) return nullptr;
+#if 0
   if (const char *swift_demangled_name = DemangleSwift(name))
     return swift_demangled_name;
+#endif
   return DemangleCXXABI(name);
 }
-
+#if 0
 static bool CreateTwoHighNumberedPipes(int *infd_, int *outfd_) {
   int *infd = NULL;
   int *outfd = NULL;
@@ -139,8 +145,12 @@ static bool CreateTwoHighNumberedPipes(int *infd_, int *outfd_) {
   outfd_[1] = outfd[1];
   return true;
 }
-
+#endif
 bool SymbolizerProcess::StartSymbolizerSubprocess() {
+  Genode::error("sanitizer: ", __func__, " called");
+  return false;
+
+#if 0
   if (!FileExists(path_)) {
     if (!reported_invalid_path_) {
       Report("WARNING: invalid path to external symbolizer!\n");
@@ -246,6 +256,7 @@ bool SymbolizerProcess::StartSymbolizerSubprocess() {
   }
 
   return true;
+#endif
 }
 
 class Addr2LineProcess : public SymbolizerProcess {
@@ -443,7 +454,7 @@ const char *Symbolizer::PlatformDemangle(const char *name) {
 }
 
 void Symbolizer::PlatformPrepareForSandboxing() {}
-
+#if 0
 static SymbolizerTool *ChooseExternalSymbolizer(LowLevelAllocator *allocator) {
   const char *path = common_flags()->external_symbolizer_path;
   const char *binary_name = path ? StripModuleName(path) : "";
@@ -491,7 +502,7 @@ static SymbolizerTool *ChooseExternalSymbolizer(LowLevelAllocator *allocator) {
   }
   return nullptr;
 }
-
+#endif
 static void ChooseSymbolizerTools(IntrusiveList<SymbolizerTool> *list,
                                   LowLevelAllocator *allocator) {
   if (!common_flags()->symbolize) {
@@ -510,11 +521,11 @@ static void ChooseSymbolizerTools(IntrusiveList<SymbolizerTool> *list,
     list->push_back(tool);
     return;
   }
-
+#if 0
   if (SymbolizerTool *tool = ChooseExternalSymbolizer(allocator)) {
     list->push_back(tool);
   }
-
+#endif
 #if SANITIZER_MAC
   VReport(2, "Using dladdr symbolizer.\n");
   list->push_back(new(*allocator) DlAddrSymbolizer());
@@ -530,7 +541,9 @@ Symbolizer *Symbolizer::PlatformInit() {
 
 void Symbolizer::LateInitialize() {
   Symbolizer::GetOrInit();
+#if 0
   InitializeSwiftDemangler();
+#endif
 }
 
 }  // namespace __sanitizer
diff --git a/ubsan/ubsan_diag.cc b/ubsan/ubsan_diag.cc
index 978d966..7d38a9b 100644
--- a/ubsan/ubsan_diag.cc
+++ b/ubsan/ubsan_diag.cc
@@ -20,7 +20,9 @@
 #include "sanitizer_common/sanitizer_stacktrace_printer.h"
 #include "sanitizer_common/sanitizer_suppressions.h"
 #include "sanitizer_common/sanitizer_symbolizer.h"
+#if 0
 #include <stdio.h>
+#endif
 
 using namespace __ubsan;
 
@@ -209,8 +211,10 @@ static void RenderText(InternalScopedString *Buffer, const char *Message,
 #if SANITIZER_WINDOWS
       sprintf_s(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
 #else
+#if 0
       snprintf(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
 #endif
+#endif
       Buffer->append("%s", FloatBuffer);
       break;
     }
