Add preliminary NetBSD support in libfuzzer

Summary:
This code already works and passes some number of tests.

There is need to finish remaining sanitizers to get better coverage.

Many tests fail due to overly long file names of executables (>31).
This is a current shortcoming of the NetBSD 8(beta) kernel, as
certain functions can fail (like retrieving file name of executable).

Sponsored by <The NetBSD Foundation>

Reviewers: joerg, kcc, vitalybuka, george.karpenkov

Reviewed By: kcc

Subscribers: mgorny, llvm-commits, #sanitizers

Tags: #sanitizers

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

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/fuzzer@312183 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/FuzzerDefs.h b/FuzzerDefs.h
index 7ea54a9..e8c92ae 100644
--- a/FuzzerDefs.h
+++ b/FuzzerDefs.h
@@ -25,14 +25,22 @@
 #ifdef __linux__
 #define LIBFUZZER_APPLE 0
 #define LIBFUZZER_LINUX 1
+#define LIBFUZZER_NETBSD 0
 #define LIBFUZZER_WINDOWS 0
 #elif __APPLE__
 #define LIBFUZZER_APPLE 1
 #define LIBFUZZER_LINUX 0
+#define LIBFUZZER_NETBSD 0
+#define LIBFUZZER_WINDOWS 0
+#elif __NetBSD__
+#define LIBFUZZER_APPLE 0
+#define LIBFUZZER_LINUX 0
+#define LIBFUZZER_NETBSD 1
 #define LIBFUZZER_WINDOWS 0
 #elif _WIN32
 #define LIBFUZZER_APPLE 0
 #define LIBFUZZER_LINUX 0
+#define LIBFUZZER_NETBSD 0
 #define LIBFUZZER_WINDOWS 1
 #else
 #error "Support for your platform has not been implemented"
@@ -42,7 +50,7 @@
 #  define __has_attribute(x) 0
 #endif
 
-#define LIBFUZZER_POSIX LIBFUZZER_APPLE || LIBFUZZER_LINUX
+#define LIBFUZZER_POSIX (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD)
 
 #ifdef __x86_64
 #  if __has_attribute(target)
diff --git a/FuzzerExtFunctionsWeak.cpp b/FuzzerExtFunctionsWeak.cpp
index 503f039..5056eb8 100644
--- a/FuzzerExtFunctionsWeak.cpp
+++ b/FuzzerExtFunctionsWeak.cpp
@@ -13,7 +13,7 @@
 // to clients right now.
 //===----------------------------------------------------------------------===//
 #include "FuzzerDefs.h"
-#if LIBFUZZER_LINUX
+#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD
 
 #include "FuzzerExtFunctions.h"
 #include "FuzzerIO.h"
@@ -51,4 +51,4 @@
 
 } // namespace fuzzer
 
-#endif // LIBFUZZER_LINUX
+#endif // LIBFUZZER_LINUX || LIBFUZZER_NETBSD
diff --git a/FuzzerExtraCounters.cpp b/FuzzerExtraCounters.cpp
index 07dbe0f..0e7a776 100644
--- a/FuzzerExtraCounters.cpp
+++ b/FuzzerExtraCounters.cpp
@@ -11,7 +11,7 @@
 
 #include "FuzzerDefs.h"
 
-#if LIBFUZZER_LINUX
+#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD
 __attribute__((weak)) extern uint8_t __start___libfuzzer_extra_counters;
 __attribute__((weak)) extern uint8_t __stop___libfuzzer_extra_counters;
 
diff --git a/FuzzerUtilLinux.cpp b/FuzzerUtilLinux.cpp
index dfe7e6f..69d46b5 100644
--- a/FuzzerUtilLinux.cpp
+++ b/FuzzerUtilLinux.cpp
@@ -9,7 +9,7 @@
 // Misc utils for Linux.
 //===----------------------------------------------------------------------===//
 #include "FuzzerDefs.h"
-#if LIBFUZZER_LINUX
+#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD
 
 #include <stdlib.h>
 
@@ -21,4 +21,4 @@
 
 } // namespace fuzzer
 
-#endif // LIBFUZZER_LINUX
+#endif // LIBFUZZER_LINUX || LIBFUZZER_NETBSD
diff --git a/afl/afl_driver.cpp b/afl/afl_driver.cpp
index 15bceb8..f102479 100644
--- a/afl/afl_driver.cpp
+++ b/afl/afl_driver.cpp
@@ -68,9 +68,15 @@
 #ifdef __linux__
 #define LIBFUZZER_LINUX 1
 #define LIBFUZZER_APPLE 0
+#define LIBFUZZER_NETBSD 0
 #elif __APPLE__
 #define LIBFUZZER_LINUX 0
 #define LIBFUZZER_APPLE 1
+#define LIBFUZZER_NETBSD 0
+#elif __NetBSD__
+#define LIBFUZZER_LINUX 0
+#define LIBFUZZER_APPLE 0
+#define LIBFUZZER_NETBSD 1
 #else
 #error "Support for your platform has not been implemented"
 #endif
@@ -119,7 +125,7 @@
   struct rusage usage;
   if (getrusage(RUSAGE_SELF, &usage))
     return 0;
-  if (LIBFUZZER_LINUX) {
+  if (LIBFUZZER_LINUX || LIBFUZZER_NETBSD) {
     // ru_maxrss is in KiB
     return usage.ru_maxrss >> 10;
   } else if (LIBFUZZER_APPLE) {