aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorm00nbsd <42475391+m00nbsd@users.noreply.github.com>2020-06-11 23:16:52 +0200
committerGitHub <noreply@github.com>2020-06-11 23:16:52 +0200
commit0f23e882fa8ef33d4f44a3fc0a5bba7a32889801 (patch)
tree8a419c6258329fe77cacf0d410cc87501fa9c387
parent1ffa7b1e771dad7944976c721ed61a0762d7b65f (diff)
sys/netbsd: add support for fault injection (#1817)
-rw-r--r--executor/common.h2
-rw-r--r--executor/common_bsd.h49
-rw-r--r--executor/executor_bsd.h1
-rw-r--r--pkg/csource/generated.go51
-rw-r--r--pkg/host/host_netbsd.go8
5 files changed, 109 insertions, 2 deletions
diff --git a/executor/common.h b/executor/common.h
index 9e1c94c07..cb409e763 100644
--- a/executor/common.h
+++ b/executor/common.h
@@ -221,7 +221,7 @@ static void remove_dir(const char* dir)
#endif
#endif
-#if !GOOS_linux
+#if !GOOS_linux && !GOOS_netbsd
#if SYZ_EXECUTOR
static int inject_fault(int nth)
{
diff --git a/executor/common_bsd.h b/executor/common_bsd.h
index a04d86a98..82aef4211 100644
--- a/executor/common_bsd.h
+++ b/executor/common_bsd.h
@@ -12,6 +12,7 @@
#include <sys/syscall.h>
#if GOOS_netbsd
+
#if SYZ_EXECUTOR || __NR_syz_usb_connect
#include "common_usb_netbsd.h"
#endif
@@ -22,6 +23,54 @@ static void setup_usb(void)
fail("failed to chmod /dev/vhci");
}
#endif
+
+#if SYZ_EXECUTOR || SYZ_FAULT
+#include <fcntl.h>
+#include <sys/fault.h>
+static void setup_fault(void)
+{
+ if (chmod("/dev/fault", 0666))
+ fail("failed to chmod /dev/fault");
+}
+static int inject_fault(int nth)
+{
+ struct fault_ioc_enable en;
+ int fd;
+
+ fd = open("/dev/fault", O_RDWR);
+ if (fd == -1)
+ fail("failed to open /dev/fault");
+
+ en.scope = FAULT_SCOPE_LWP;
+ en.mode = FAULT_MODE_NTH;
+ en.nth = nth;
+ if (ioctl(fd, FAULT_IOC_ENABLE, &en) != 0)
+ fail("FAULT_IOC_ENABLE failed with nth=%d", nth);
+
+ return fd;
+}
+#endif
+#if SYZ_EXECUTOR
+static int fault_injected(int fd)
+{
+ struct fault_ioc_getinfo info;
+ struct fault_ioc_disable dis;
+ int res;
+
+ if (ioctl(fd, FAULT_IOC_GETINFO, &info) != 0)
+ fail("FAULT_IOC_GETINFO failed");
+ res = (info.nfaults > 0);
+
+ dis.scope = FAULT_SCOPE_LWP;
+ if (ioctl(fd, FAULT_IOC_DISABLE, &dis) != 0)
+ fail("FAULT_IOC_DISABLE failed");
+
+ close(fd);
+
+ return res;
+}
+#endif
+
#endif
#if GOOS_openbsd
diff --git a/executor/executor_bsd.h b/executor/executor_bsd.h
index c17f37964..a9440acec 100644
--- a/executor/executor_bsd.h
+++ b/executor/executor_bsd.h
@@ -172,6 +172,7 @@ static bool cover_check(uint64 pc)
#define SYZ_HAVE_FEATURES 1
static feature_t features[] = {
{"usb", setup_usb},
+ {"fault", setup_fault},
};
static void setup_machine(void)
diff --git a/pkg/csource/generated.go b/pkg/csource/generated.go
index 6f81981a3..eaa2b714f 100644
--- a/pkg/csource/generated.go
+++ b/pkg/csource/generated.go
@@ -206,7 +206,7 @@ static void remove_dir(const char* dir)
#endif
#endif
-#if !GOOS_linux
+#if !GOOS_linux && !GOOS_netbsd
#if SYZ_EXECUTOR
static int inject_fault(int nth)
{
@@ -410,6 +410,7 @@ void child()
#include <sys/syscall.h>
#if GOOS_netbsd
+
#if SYZ_EXECUTOR || __NR_syz_usb_connect
#include <dev/usb/usb.h>
@@ -1568,6 +1569,54 @@ static void setup_usb(void)
fail("failed to chmod /dev/vhci");
}
#endif
+
+#if SYZ_EXECUTOR || SYZ_FAULT
+#include <fcntl.h>
+#include <sys/fault.h>
+static void setup_fault(void)
+{
+ if (chmod("/dev/fault", 0666))
+ fail("failed to chmod /dev/fault");
+}
+static int inject_fault(int nth)
+{
+ struct fault_ioc_enable en;
+ int fd;
+
+ fd = open("/dev/fault", O_RDWR);
+ if (fd == -1)
+ fail("failed to open /dev/fault");
+
+ en.scope = FAULT_SCOPE_LWP;
+ en.mode = FAULT_MODE_NTH;
+ en.nth = nth;
+ if (ioctl(fd, FAULT_IOC_ENABLE, &en) != 0)
+ fail("FAULT_IOC_ENABLE failed with nth=%d", nth);
+
+ return fd;
+}
+#endif
+#if SYZ_EXECUTOR
+static int fault_injected(int fd)
+{
+ struct fault_ioc_getinfo info;
+ struct fault_ioc_disable dis;
+ int res;
+
+ if (ioctl(fd, FAULT_IOC_GETINFO, &info) != 0)
+ fail("FAULT_IOC_GETINFO failed");
+ res = (info.nfaults > 0);
+
+ dis.scope = FAULT_SCOPE_LWP;
+ if (ioctl(fd, FAULT_IOC_DISABLE, &dis) != 0)
+ fail("FAULT_IOC_DISABLE failed");
+
+ close(fd);
+
+ return res;
+}
+#endif
+
#endif
#if GOOS_openbsd
diff --git a/pkg/host/host_netbsd.go b/pkg/host/host_netbsd.go
index 160929194..dffdec5d7 100644
--- a/pkg/host/host_netbsd.go
+++ b/pkg/host/host_netbsd.go
@@ -23,6 +23,7 @@ func init() {
checkFeature[FeatureComparisons] = unconditionallyEnabled
checkFeature[FeatureUSBEmulation] = checkUSBEmulation
checkFeature[FeatureExtraCoverage] = checkUSBEmulation
+ checkFeature[FeatureFault] = checkFault
}
func checkUSBEmulation() string {
@@ -31,3 +32,10 @@ func checkUSBEmulation() string {
}
return ""
}
+
+func checkFault() string {
+ if err := osutil.IsAccessible("/dev/fault"); err != nil {
+ return err.Error()
+ }
+ return ""
+}