From ca27b3bc5783ce1dbda7f23883d0ddf3729902a8 Mon Sep 17 00:00:00 2001 From: Suraj K Suresh Date: Mon, 28 Sep 2020 14:48:39 +0530 Subject: executor: msvc support syz-executor --- executor/common.h | 20 +++++++++++++++++--- executor/common_fuchsia.h | 9 ++++++--- executor/common_windows.h | 20 +++++++++++++------- executor/executor.cc | 16 ++++++++++------ executor/executor_windows.h | 18 ++++++++++++++++++ 5 files changed, 64 insertions(+), 19 deletions(-) (limited to 'executor') diff --git a/executor/common.h b/executor/common.h index 2e619065f..e054cf434 100644 --- a/executor/common.h +++ b/executor/common.h @@ -17,6 +17,13 @@ #if GOOS_freebsd || GOOS_test && HOSTGOOS_freebsd #include // for htobe*. +#elif GOOS_windows +#define htobe16 _byteswap_ushort +#define htobe32 _byteswap_ulong +#define htobe64 _byteswap_uint64 +#define le16toh(x) x +#define htole16(x) x +typedef signed int ssize_t; #else #include // for htobe*. #endif @@ -30,7 +37,9 @@ #endif #if SYZ_EXECUTOR && !GOOS_linux +#if !GOOS_windows #include +#endif NORETURN void doexit(int status) { _exit(status); @@ -124,13 +133,16 @@ static void install_segv_handler(void) } #define NONFAILING(...) \ - { \ + ({ \ + int ok = 1; \ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \ if (_setjmp(segv_env) == 0) { \ __VA_ARGS__; \ - } \ + } else \ + ok = 0; \ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \ - } + ok; \ + }) #endif #endif @@ -440,11 +452,13 @@ static long syz_execute_func(volatile long text) // from the reach of the random code, otherwise it's known to reach // the output region somehow. The asm block is arch-independent except // for the number of available registers. +#if defined(__GNUC__) volatile long p[8] = {0}; (void)p; #if GOARCH_amd64 asm volatile("" ::"r"(0l), "r"(1l), "r"(2l), "r"(3l), "r"(4l), "r"(5l), "r"(6l), "r"(7l), "r"(8l), "r"(9l), "r"(10l), "r"(11l), "r"(12l), "r"(13l)); +#endif #endif ((void (*)(void))(text))(); return 0; diff --git a/executor/common_fuchsia.h b/executor/common_fuchsia.h index 2d76891da..6576e83f0 100644 --- a/executor/common_fuchsia.h +++ b/executor/common_fuchsia.h @@ -126,13 +126,16 @@ static void install_segv_handler(void) } #define NONFAILING(...) \ - { \ + ({ \ + int ok = 1; \ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \ if (sigsetjmp(segv_env, 0) == 0) { \ __VA_ARGS__; \ - } \ + } else \ + ok = 0; \ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \ - } + ok; \ + }) #endif #if SYZ_EXECUTOR || SYZ_THREADED diff --git a/executor/common_windows.h b/executor/common_windows.h index e9722a360..a29d437b8 100644 --- a/executor/common_windows.h +++ b/executor/common_windows.h @@ -3,20 +3,17 @@ // This file is shared between executor and csource package. +#include // for _chdir +#include // for mktemp #include -#include "common.h" - #if SYZ_EXECUTOR || SYZ_HANDLE_SEGV static void install_segv_handler() { } -#define NONFAILING(...) \ - __try { \ - __VA_ARGS__; \ - } __except (EXCEPTION_EXECUTE_HANDLER) { \ - } +#define NONFAILING(...) \ + ([&]() { __try { __VA_ARGS__; } __except (EXCEPTION_EXECUTE_HANDLER) { return false; } return true; }()) #endif #if SYZ_EXECUTOR || SYZ_THREADED || SYZ_REPEAT && SYZ_EXECUTOR_USES_FORK_SERVER @@ -111,3 +108,12 @@ static int do_sandbox_none(void) return 0; } #endif + +static void use_temporary_dir(void) +{ + char tmpdir_template[] = "./syzkaller.XXXXXX"; + char* tmpdir = mktemp(tmpdir_template); + + CreateDirectory(tmpdir, NULL); + _chdir(tmpdir); +} diff --git a/executor/executor.cc b/executor/executor.cc index 9473b871f..174d4bb15 100644 --- a/executor/executor.cc +++ b/executor/executor.cc @@ -14,7 +14,10 @@ #include #include #include + +#if !GOOS_windows #include +#endif #include "defs.h" @@ -23,12 +26,15 @@ #define NORETURN __attribute__((noreturn)) #define ALIGNED(N) __attribute__((aligned(N))) #define PRINTF(fmt, args) __attribute__((format(printf, fmt, args))) +#define INPUT_DATA_ALIGNMENT 64 << 10 #else // Assuming windows/cl. #define SYSCALLAPI WINAPI #define NORETURN __declspec(noreturn) -#define ALIGNED(N) __declspec(align(N)) +#define INPUT_DATA_ALIGNMENT 4 << 10 +#define ALIGNED(N) __declspec(align(N)) // here we are not aligning the value because of msvc reporting the value as an illegal value #define PRINTF(fmt, args) +#define __thread __declspec(thread) #endif #ifndef GIT_REVISION @@ -172,7 +178,7 @@ static bool collide; uint32 completed; bool is_kernel_64_bit = true; -ALIGNED(64 << 10) +ALIGNED(INPUT_DATA_ALIGNMENT) static char input_data[kMaxInput]; // Checksum kinds. @@ -1233,8 +1239,7 @@ void copyin(char* addr, uint64 val, uint64 size, uint64 bf, uint64 bf_off, uint6 bool copyout(char* addr, uint64 size, uint64* res) { - bool ok = false; - NONFAILING( + return NONFAILING( switch (size) { case 1: *res = *(uint8*)addr; @@ -1250,8 +1255,7 @@ bool copyout(char* addr, uint64 size, uint64* res) break; default: fail("copyout: bad argument size %llu", size); - } __atomic_store_n(&ok, true, __ATOMIC_RELEASE);); - return ok; + }); } uint64 read_arg(uint64** input_posp) diff --git a/executor/executor_windows.h b/executor/executor_windows.h index 1e210826b..bb863c009 100644 --- a/executor/executor_windows.h +++ b/executor/executor_windows.h @@ -6,6 +6,9 @@ #include "nocover.h" +#define read read_win +#define write write_win + static void os_init(int argc, char** argv, void* data, size_t data_size) { if (VirtualAlloc(data, data_size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE) != data) @@ -20,3 +23,18 @@ static intptr_t execute_syscall(const call_t* c, intptr_t a[kMaxArgs]) return -1; } } + +static __inline int read_win(int pipe_id, void* input_data, int data_size) +{ + DWORD dwBytesRead = 0; + ReadFile((HANDLE)_get_osfhandle(pipe_id), input_data, data_size, &dwBytesRead, NULL); + + return (int)dwBytesRead; +} + +static __inline int write_win(int pipe_id, void* input_data, int data_size) +{ + DWORD dwBytesWritten = 0; + WriteFile((HANDLE)_get_osfhandle(pipe_id), input_data, data_size, &dwBytesWritten, NULL); + return (int)dwBytesWritten; +} -- cgit mrf-deployment