github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/executor/common_windows.h (about)

     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 <direct.h> // for _chdir
     7  #include <io.h> // for mktemp
     8  #include <windows.h>
     9  
    10  #if SYZ_EXECUTOR || SYZ_HANDLE_SEGV
    11  static void install_segv_handler()
    12  {
    13  }
    14  
    15  #define NONFAILING(...) \
    16  	([&]() { __try { __VA_ARGS__; } __except (EXCEPTION_EXECUTE_HANDLER) { return false; } return true; }())
    17  #endif
    18  
    19  #if SYZ_EXECUTOR || SYZ_THREADED || SYZ_REPEAT && SYZ_EXECUTOR_USES_FORK_SERVER
    20  static uint64 current_time_ms()
    21  {
    22  	return GetTickCount64();
    23  }
    24  #endif
    25  
    26  #if SYZ_EXECUTOR || SYZ_THREADED || SYZ_REPEAT && SYZ_EXECUTOR_USES_FORK_SERVER
    27  static void sleep_ms(uint64 ms)
    28  {
    29  	Sleep(ms);
    30  }
    31  #endif
    32  
    33  #if SYZ_EXECUTOR || SYZ_THREADED
    34  static void thread_start(void* (*fn)(void*), void* arg)
    35  {
    36  	HANDLE th = CreateThread(NULL, 128 << 10, (LPTHREAD_START_ROUTINE)fn, arg, 0, NULL);
    37  	if (th == NULL)
    38  		exitf("CreateThread failed");
    39  }
    40  
    41  struct event_t {
    42  	CRITICAL_SECTION cs;
    43  	CONDITION_VARIABLE cv;
    44  	int state;
    45  };
    46  
    47  static void event_init(event_t* ev)
    48  {
    49  	InitializeCriticalSection(&ev->cs);
    50  	InitializeConditionVariable(&ev->cv);
    51  	ev->state = 0;
    52  }
    53  
    54  static void event_reset(event_t* ev)
    55  {
    56  	ev->state = 0;
    57  }
    58  
    59  static void event_set(event_t* ev)
    60  {
    61  	EnterCriticalSection(&ev->cs);
    62  	if (ev->state)
    63  		exitf("event already set");
    64  	ev->state = 1;
    65  	LeaveCriticalSection(&ev->cs);
    66  	WakeAllConditionVariable(&ev->cv);
    67  }
    68  
    69  static void event_wait(event_t* ev)
    70  {
    71  	EnterCriticalSection(&ev->cs);
    72  	while (!ev->state)
    73  		SleepConditionVariableCS(&ev->cv, &ev->cs, INFINITE);
    74  	LeaveCriticalSection(&ev->cs);
    75  }
    76  
    77  static int event_isset(event_t* ev)
    78  {
    79  	EnterCriticalSection(&ev->cs);
    80  	int res = ev->state;
    81  	LeaveCriticalSection(&ev->cs);
    82  	return res;
    83  }
    84  
    85  static int event_timedwait(event_t* ev, uint64 timeout_ms)
    86  {
    87  	EnterCriticalSection(&ev->cs);
    88  	uint64 start = current_time_ms();
    89  	for (;;) {
    90  		if (ev->state)
    91  			break;
    92  		uint64 now = current_time_ms();
    93  		if (now - start > timeout_ms)
    94  			break;
    95  		SleepConditionVariableCS(&ev->cv, &ev->cs, timeout_ms - (now - start));
    96  	}
    97  	int res = ev->state;
    98  	LeaveCriticalSection(&ev->cs);
    99  	return res;
   100  }
   101  #endif
   102  
   103  #if SYZ_EXECUTOR || SYZ_SANDBOX_NONE
   104  static void loop();
   105  static int do_sandbox_none(void)
   106  {
   107  	loop();
   108  	return 0;
   109  }
   110  #endif
   111  
   112  static void use_temporary_dir(void)
   113  {
   114  	char tmpdir_template[] = "./syzkaller.XXXXXX";
   115  	char* tmpdir = mktemp(tmpdir_template);
   116  
   117  	CreateDirectory(tmpdir, NULL);
   118  	_chdir(tmpdir);
   119  }