• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 syzkaller project authors. All rights reserved.
2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3 
4 // This file is shared between executor and csource package.
5 
6 #include <windows.h>
7 
8 #include "common.h"
9 
10 #if SYZ_EXECUTOR || SYZ_HANDLE_SEGV
install_segv_handler()11 static void install_segv_handler()
12 {
13 }
14 
15 #define NONFAILING(...)                          \
16 	__try {                                  \
17 		__VA_ARGS__;                     \
18 	} __except (EXCEPTION_EXECUTE_HANDLER) { \
19 	}
20 #endif
21 
22 #if SYZ_EXECUTOR || SYZ_THREADED || SYZ_REPEAT && SYZ_EXECUTOR_USES_FORK_SERVER
current_time_ms()23 static uint64 current_time_ms()
24 {
25 	return GetTickCount64();
26 }
27 #endif
28 
29 #if SYZ_EXECUTOR || SYZ_THREADED || SYZ_REPEAT && SYZ_EXECUTOR_USES_FORK_SERVER
sleep_ms(uint64 ms)30 static void sleep_ms(uint64 ms)
31 {
32 	Sleep(ms);
33 }
34 #endif
35 
36 #if SYZ_EXECUTOR || SYZ_THREADED
thread_start(void * (* fn)(void *),void * arg)37 static void thread_start(void* (*fn)(void*), void* arg)
38 {
39 	HANDLE th = CreateThread(NULL, 128 << 10, (LPTHREAD_START_ROUTINE)fn, arg, 0, NULL);
40 	if (th == NULL)
41 		exitf("CreateThread failed");
42 }
43 
44 struct event_t {
45 	CRITICAL_SECTION cs;
46 	CONDITION_VARIABLE cv;
47 	int state;
48 };
49 
event_init(event_t * ev)50 static void event_init(event_t* ev)
51 {
52 	InitializeCriticalSection(&ev->cs);
53 	InitializeConditionVariable(&ev->cv);
54 	ev->state = 0;
55 }
56 
event_reset(event_t * ev)57 static void event_reset(event_t* ev)
58 {
59 	ev->state = 0;
60 }
61 
event_set(event_t * ev)62 static void event_set(event_t* ev)
63 {
64 	EnterCriticalSection(&ev->cs);
65 	if (ev->state)
66 		fail("event already set");
67 	ev->state = 1;
68 	LeaveCriticalSection(&ev->cs);
69 	WakeAllConditionVariable(&ev->cv);
70 }
71 
event_wait(event_t * ev)72 static void event_wait(event_t* ev)
73 {
74 	EnterCriticalSection(&ev->cs);
75 	while (!ev->state)
76 		SleepConditionVariableCS(&ev->cv, &ev->cs, INFINITE);
77 	LeaveCriticalSection(&ev->cs);
78 }
79 
event_isset(event_t * ev)80 static int event_isset(event_t* ev)
81 {
82 	EnterCriticalSection(&ev->cs);
83 	int res = ev->state;
84 	LeaveCriticalSection(&ev->cs);
85 	return res;
86 }
87 
event_timedwait(event_t * ev,uint64 timeout_ms)88 static int event_timedwait(event_t* ev, uint64 timeout_ms)
89 {
90 	EnterCriticalSection(&ev->cs);
91 	uint64 start = current_time_ms();
92 	for (;;) {
93 		if (ev->state)
94 			break;
95 		uint64 now = current_time_ms();
96 		if (now - start > timeout_ms)
97 			break;
98 		SleepConditionVariableCS(&ev->cv, &ev->cs, timeout_ms - (now - start));
99 	}
100 	int res = ev->state;
101 	LeaveCriticalSection(&ev->cs);
102 	return res;
103 }
104 #endif
105 
106 #if SYZ_EXECUTOR || SYZ_SANDBOX_NONE
107 static void loop();
do_sandbox_none(void)108 static int do_sandbox_none(void)
109 {
110 	loop();
111 	doexit(0);
112 }
113 #endif
114 
115 #if SYZ_EXECUTOR
116 #define do_sandbox_setuid() 0
117 #define do_sandbox_namespace() 0
118 #endif
119