BOOL factor_vm::windows_stat(vm_char* path) {
BY_HANDLE_FILE_INFORMATION bhfi;
- HANDLE h = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
+ HANDLE h = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (h == INVALID_HANDLE_VALUE) {
// FindFirstFile is the only call that can stat c:\pagefile.sys
WIN32_FIND_DATA st;
- HANDLE h;
-
- if (INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
+ h = FindFirstFile(path, &st);
+ if (h == INVALID_HANDLE_VALUE)
return false;
FindClose(h);
return true;
void sleep_nanos(uint64_t nsec) { Sleep((DWORD)(nsec / 1000000)); }
+#ifndef EXCEPTION_DISPOSITION
typedef enum _EXCEPTION_DISPOSITION {
ExceptionContinueExecution = 0,
ExceptionContinueSearch = 1,
ExceptionNestedException = 2,
ExceptionCollidedUnwind = 3
} EXCEPTION_DISPOSITION;
+#endif
LONG factor_vm::exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,
void* dispatch) {
+ (void)frame;
+ (void)dispatch;
switch (e->ExceptionCode) {
case EXCEPTION_ACCESS_VIOLATION:
- signal_fault_addr = e->ExceptionInformation[1];
- signal_fault_pc = c->EIP;
- verify_memory_protection_error(signal_fault_addr);
+ set_memory_protection_error(e->ExceptionInformation[1], c->EIP);
dispatch_signal_handler((cell*)&c->ESP, (cell*)&c->EIP,
(cell)factor::memory_signal_handler_impl);
break;
// On Unix SIGINT (ctrl-c) automatically interrupts blocking io system
// calls. It doesn't on Windows, so we need to manually send some
// cancellation requests to unblock the thread.
-VOID CALLBACK dummy_cb (ULONG_PTR dwParam) { }
+VOID CALLBACK dummy_cb(ULONG_PTR dwParam) { (void)dwParam; }
// CancelSynchronousIo is not in Windows XP
#if _WIN32_WINNT >= 0x0600
}
}
#else
-static void wake_up_thread(HANDLE thread) {}
+static void wake_up_thread(HANDLE thread) { (void)thread; }
#endif
static BOOL WINAPI ctrl_handler(DWORD dwCtrlType) {
SetConsoleCtrlHandler(factor::ctrl_handler, TRUE);
}
+const int ctrl_break_sleep = 10; /* msec */
+
+static DWORD WINAPI ctrl_break_thread_proc(LPVOID parent_vm) {
+ bool ctrl_break_handled = false;
+ factor_vm* vm = static_cast<factor_vm*>(parent_vm);
+ while (vm->stop_on_ctrl_break) {
+ if (GetAsyncKeyState(VK_CANCEL) >= 0) { /* Ctrl-Break is released. */
+ ctrl_break_handled = false; /* Wait for the next press. */
+ } else if (!ctrl_break_handled) {
+ /* Check if the VM thread has the same Id as the thread Id of the
+ currently active window. Note that thread Id is not a handle. */
+ DWORD fg_thd_id = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
+ if ((fg_thd_id == vm->thread_id) && !vm->fep_p) {
+ vm->enqueue_fep();
+ ctrl_break_handled = true;
+ }
+ }
+ Sleep(ctrl_break_sleep);
+ }
+ return 0;
+}
+
+void factor_vm::primitive_disable_ctrl_break() {
+ stop_on_ctrl_break = false;
+ if (ctrl_break_thread != NULL) {
+ DWORD wait_result = WaitForSingleObject(ctrl_break_thread,
+ 2 * ctrl_break_sleep);
+ if (wait_result != WAIT_OBJECT_0)
+ TerminateThread(ctrl_break_thread, 0);
+ CloseHandle(ctrl_break_thread);
+ ctrl_break_thread = NULL;
+ }
+}
+
+void factor_vm::primitive_enable_ctrl_break() {
+ stop_on_ctrl_break = true;
+ if (ctrl_break_thread == NULL) {
+ DisableProcessWindowsGhosting();
+ ctrl_break_thread = CreateThread(NULL, 0, factor::ctrl_break_thread_proc,
+ static_cast<LPVOID>(this), 0, NULL);
+ SetThreadPriority(ctrl_break_thread, THREAD_PRIORITY_ABOVE_NORMAL);
+ }
+}
+
void lock_console() {}
void unlock_console() {}