From aeabdfbfa833807d4913e82c3316cf717140bf50 Mon Sep 17 00:00:00 2001
From: begasus <begasus@gmail.com>
Date: Sun, 4 Apr 2021 13:49:51 +0000
Subject: Fix missing defines for "#ifndef USEMMAP"

Haiku doesn't provide <sys/syscall.h>

Fix undefind reference to SYS_write

Add -lnetwork to dependencies

diff --git a/GNUmakefile b/GNUmakefile
index ac8fe79..542097a 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -164,7 +164,7 @@ endif
 ifeq "$(shell uname -s)" "Haiku"
   SHMAT_OK=0
   override CFLAGS  += -DUSEMMAP=1 -Wno-error=format -fPIC
-  LDFLAGS += -Wno-deprecated-declarations -lgnu
+  LDFLAGS += -Wno-deprecated-declarations -lgnu -lnetwork
   SPECIAL_PERFORMANCE += -DUSEMMAP=1
 endif
 
diff --git a/instrumentation/afl-compiler-rt.o.c b/instrumentation/afl-compiler-rt.o.c
index f241447..a33b351 100644
--- a/instrumentation/afl-compiler-rt.o.c
+++ b/instrumentation/afl-compiler-rt.o.c
@@ -34,8 +34,10 @@
 #include <errno.h>
 
 #include <sys/mman.h>
-#include <sys/syscall.h>
 #ifndef __HAIKU__
+#include <sys/syscall.h>
+#endif
+#ifndef USEMMAP
   #include <sys/shm.h>
 #endif
 #include <sys/wait.h>
@@ -76,6 +78,10 @@
   #define MAP_INITIAL_SIZE MAP_SIZE
 #endif
 
+#if defined(__HAIKU__)
+  extern ssize_t _kern_write(int fd, off_t pos, const void *buffer,	size_t bufferSize);
+#endif // HAIKU
+
 u8   __afl_area_initial[MAP_INITIAL_SIZE];
 u8 * __afl_area_ptr_dummy = __afl_area_initial;
 u8 * __afl_area_ptr = __afl_area_initial;
@@ -1738,7 +1744,11 @@ static int area_is_valid(void *ptr, size_t len) {
 
   if (unlikely(!ptr || __asan_region_is_poisoned(ptr, len))) { return 0; }
 
-  long r = syscall(SYS_write, __afl_dummy_fd[1], ptr, len);
+  #ifndef __HAIKU__
+    long r = syscall(SYS_write, __afl_dummy_fd[1], ptr, len);
+  #else
+    long r = _kern_write(__afl_dummy_fd[1], -1, ptr, len);
+  #endif // HAIKU
 
   if (r <= 0 || r > len) return 0;
 
diff --git a/utils/afl_network_proxy/afl-network-server.c b/utils/afl_network_proxy/afl-network-server.c
index 0dfae65..60f174e 100644
--- a/utils/afl_network_proxy/afl-network-server.c
+++ b/utils/afl_network_proxy/afl-network-server.c
@@ -45,7 +45,6 @@
 
 #include <sys/wait.h>
 #include <sys/time.h>
-#include <sys/shm.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/resource.h>
@@ -53,7 +52,9 @@
 #include <netinet/ip6.h>
 #include <arpa/inet.h>
 #include <sys/mman.h>
-#include <sys/shm.h>
+#ifndef USEMMAP
+  #include <sys/shm.h>
+#endif
 #include <sys/socket.h>
 #include <netdb.h>
 
-- 
2.30.2


From d45b1bca8d0823aa10e9af24a1cd322ec3a01716 Mon Sep 17 00:00:00 2001
From: begasus <begasus@gmail.com>
Date: Mon, 5 Apr 2021 08:52:46 +0000
Subject: Remove crashing test


diff --git a/test/test-all.sh b/test/test-all.sh
index 8df4bef..5935b76 100755
--- a/test/test-all.sh
+++ b/test/test-all.sh
@@ -10,8 +10,6 @@
 
 . ./test-gcc-plugin.sh
 
-. ./test-libextensions.sh
-
 . ./test-qemu-mode.sh
 
 . ./test-unicorn-mode.sh
-- 
2.30.2


From 298a69e24f1a8ea738438ac7019edc1532ffc674 Mon Sep 17 00:00:00 2001
From: begasus <begasus@gmail.com>
Date: Fri, 9 Apr 2021 17:27:53 +0200
Subject: Fix build on x86_64


diff --git a/GNUmakefile b/GNUmakefile
index 542097a..58278e6 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -166,6 +166,9 @@ ifeq "$(shell uname -s)" "Haiku"
   override CFLAGS  += -DUSEMMAP=1 -Wno-error=format -fPIC
   LDFLAGS += -Wno-deprecated-declarations -lgnu -lnetwork
   SPECIAL_PERFORMANCE += -DUSEMMAP=1
+  ifeq "$(shell uname -m)" "x86_64"
+	AFL_NO_X86=1
+  endif
 endif
 
 AFL_FUZZ_FILES = $(wildcard src/afl-fuzz*.c)
-- 
2.30.2

