From 9fe4bdc5f1037a409e82299f36117030114c7b94 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 20 Jul 2018 20:26:05 +0200 Subject: executor: overhaul Make as much code as possible shared between all OSes. In particular main is now common across all OSes. Make more code shared between executor and csource (in particular, loop function and threaded execution logic). Also make loop and threaded logic shared across all OSes. Make more posix/unix code shared across OSes (e.g. signal handling, pthread creation, etc). Plus other changes along similar lines. Also support test OS in executor (based on portable posix) and add 4 arches that cover all execution modes (fork server/no fork server, shmem/no shmem). This change paves way for testing of executor code and allows to preserve consistency across OSes and executor/csource. --- executor/executor_fuchsia.h | 77 +++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 51 deletions(-) (limited to 'executor/executor_fuchsia.h') diff --git a/executor/executor_fuchsia.h b/executor/executor_fuchsia.h index e49f75b67..6e7dab0b0 100644 --- a/executor/executor_fuchsia.h +++ b/executor/executor_fuchsia.h @@ -1,62 +1,37 @@ -// Copyright 2018 syzkaller project authors. All rights reserved. +// Copyright 2017 syzkaller project authors. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. -// Fuchsia's pthread_cond_timedwait just returns immidiately, so we use simple spin wait. - +#include #include +#include +#include +#include +#include -typedef pthread_t osthread_t; - -void thread_start(osthread_t* t, void* (*fn)(void*), void* arg) -{ - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setstacksize(&attr, 128 << 10); - if (pthread_create(t, &attr, fn, arg)) - exitf("pthread_create failed"); - pthread_attr_destroy(&attr); -} - -struct event_t { - int state; -}; - -void event_init(event_t* ev) -{ - ev->state = 0; -} - -void event_reset(event_t* ev) -{ - ev->state = 0; -} - -void event_set(event_t* ev) -{ - if (ev->state) - fail("event already set"); - __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE); -} - -void event_wait(event_t* ev) -{ - while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) - usleep(200); -} +#include "nocover.h" -bool event_isset(event_t* ev) +static void os_init(int argc, char** argv, void* data, size_t data_size) { - return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE); + if (syz_mmap((size_t)data, data_size) != ZX_OK) + fail("mmap of data segment failed"); } -bool event_timedwait(event_t* ev, uint64 timeout_ms) +static long execute_syscall(const call_t* c, long a[kMaxArgs]) { - uint64 start = current_time_ms(); - for (;;) { - if (__atomic_load_n(&ev->state, __ATOMIC_RELAXED)) - return true; - if (current_time_ms() - start > timeout_ms) - return false; - usleep(200); + long res = c->call(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]); + if (strncmp(c->name, "zx_", 3) == 0) { + // Convert zircon error convention to the libc convention that executor expects. + if (res == ZX_OK || + !strcmp(c->name, "zx_log_read") || + !strcmp(c->name, "zx_clock_get") || + !strcmp(c->name, "zx_ticks_get")) + return 0; + errno = (-res) & 0x7f; + return -1; } + // We cast libc functions to signature returning long, + // as the result int -1 is returned as 0x00000000ffffffff rather than full -1. + if (res == 0xffffffff) + res = (long)-1; + return res; } -- cgit mrf-deployment