From: Bart House Date: Thu, 13 Dec 2018 05:46:14 +0000 (-0800) Subject: Changes to get fuzzer working in NetHackW. X-Git-Tag: nmake-explicit-path~46 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0cd50847fb75a9b9e08a9dbcc362d3aef5653258;p=nethack Changes to get fuzzer working in NetHackW. --- diff --git a/include/extern.h b/include/extern.h index 81b792aa9..c7fd71868 100644 --- a/include/extern.h +++ b/include/extern.h @@ -174,6 +174,7 @@ E boolean NDECL(status_hilite_menu); /* ### cmd.c ### */ E char NDECL(randomkey); +E void FDECL(random_response, (char * buf, int size)); E int NDECL(doconduct); E int NDECL(domonability); E char FDECL(cmd_from_func, (int NDECL((*)))); diff --git a/src/cmd.c b/src/cmd.c index 67170cec7..65bea3080 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -4421,6 +4421,27 @@ randomkey() return c; } +void random_response(char * buf, int size) +{ + int count = 0; + while (1) { + char c = randomkey(); + + if (c == '\n') + break; + + if (c == '\033') { + count = 0; + break; + } + + if (count < size) + buf[count++] = c; + } + + buf[count] = '\0'; +} + int ch2spkeys(c, start, end) char c; diff --git a/sys/winnt/windmain.c b/sys/winnt/windmain.c index d8b7c8056..15ae95d87 100644 --- a/sys/winnt/windmain.c +++ b/sys/winnt/windmain.c @@ -348,7 +348,10 @@ attempt_restore: if (discover) You("are in non-scoring discovery mode."); } - moveloop(resuming); + + // iflags.debug_fuzzer = TRUE; + + moveloop(resuming); nethack_exit(EXIT_SUCCESS); /*NOTREACHED*/ return 0; diff --git a/win/win32/mhdlg.c b/win/win32/mhdlg.c index 6bc7d4eb3..8a158bee9 100644 --- a/win/win32/mhdlg.c +++ b/win/win32/mhdlg.c @@ -25,6 +25,14 @@ INT_PTR CALLBACK GetlinDlgProc(HWND, UINT, WPARAM, LPARAM); int mswin_getlin_window(const char *question, char *result, size_t result_size) { + if (iflags.debug_fuzzer) { + random_response(result, result_size); + if (result[0] != '\0') + return IDOK; + else + return IDCANCEL; + } + INT_PTR ret; struct getlin_data data; diff --git a/win/win32/mhmain.c b/win/win32/mhmain.c index a71534703..a8742b03f 100644 --- a/win/win32/mhmain.c +++ b/win/win32/mhmain.c @@ -529,6 +529,11 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) child = GetNHApp()->windowlist[msg_param->wid].win; } break; + + case MSNH_MSG_RANDOM_INPUT: + nhassert(0); // unexpected + break; + } } diff --git a/win/win32/mhmap.c b/win/win32/mhmap.c index b883cd7bd..18bca2571 100644 --- a/win/win32/mhmap.c +++ b/win/win32/mhmap.c @@ -722,7 +722,11 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) } } break; - } /* end switch(wParam) */ + case MSNH_MSG_RANDOM_INPUT: + nhassert(0); // unexpected + break; + + } /* end switch(wParam) */ } /* on WM_CREATE */ diff --git a/win/win32/mhmenu.c b/win/win32/mhmenu.c index 5e38d0235..34fe28a1a 100644 --- a/win/win32/mhmenu.c +++ b/win/win32/mhmenu.c @@ -687,6 +687,18 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) ZeroMemory(data->menu.prompt, sizeof(data->menu.prompt)); } } break; + + case MSNH_MSG_RANDOM_INPUT: { + char c = randomkey(); + if (c == '\n') + PostMessage(hWnd, WM_COMMAND, MAKELONG(IDOK, 0), 0); + else if (c == '\033') + PostMessage(hWnd, WM_COMMAND, MAKELONG(IDCANCEL, 0), 0); + else + PostMessage(GetDlgItem(hWnd, IDC_TEXT_CONTROL), WM_CHAR, c, 0); + + } break; + } } /*-----------------------------------------------------------------------------*/ diff --git a/win/win32/mhmsg.h b/win/win32/mhmsg.h index 7eb65d0c0..4fd04af01 100644 --- a/win/win32/mhmsg.h +++ b/win/win32/mhmsg.h @@ -21,6 +21,7 @@ #define MSNH_MSG_CARET 110 #define MSNH_MSG_GETTEXT 111 #define MSNH_MSG_UPDATE_STATUS 112 +#define MSNH_MSG_RANDOM_INPUT 113 typedef struct mswin_nhmsg_add_wnd { winid wid; diff --git a/win/win32/mhmsgwnd.c b/win/win32/mhmsgwnd.c index 505b40f36..58df28c63 100644 --- a/win/win32/mhmsgwnd.c +++ b/win/win32/mhmsgwnd.c @@ -416,6 +416,10 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) } } break; + case MSNH_MSG_RANDOM_INPUT: + nhassert(0); // unexpected + break; + } /* switch( wParam ) */ } diff --git a/win/win32/mhrip.c b/win/win32/mhrip.c index 79ad05342..f00004f09 100644 --- a/win/win32/mhrip.c +++ b/win/win32/mhrip.c @@ -282,6 +282,11 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) data->window_text = NULL; break; } + + case MSNH_MSG_RANDOM_INPUT: + nhassert(0); // unexpected + break; + } } diff --git a/win/win32/mhstatus.c b/win/win32/mhstatus.c index d5bfda092..20b09f094 100644 --- a/win/win32/mhstatus.c +++ b/win/win32/mhstatus.c @@ -219,6 +219,11 @@ StatusWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) data->status_lines = msg_data->status_lines; InvalidateRect(hWnd, NULL, TRUE); } break; + + case MSNH_MSG_RANDOM_INPUT: + nhassert(0); // unexpected + break; + } /* end switch( wParam ) { */ } break; diff --git a/win/win32/mhtext.c b/win/win32/mhtext.c index 8c1c2b32f..14092d52f 100644 --- a/win/win32/mhtext.c +++ b/win/win32/mhtext.c @@ -212,6 +212,19 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) _tcscat(data->window_text, TEXT("\r\n")); break; } + + case MSNH_MSG_RANDOM_INPUT: { + char c = randomkey(); + if (c == '\n') + PostMessage(hWnd, WM_COMMAND, MAKELONG(IDOK, 0), 0); + else if (c == '\033') + PostMessage(hWnd, WM_COMMAND, MAKELONG(IDCANCEL, 0), 0); + else + PostMessage(GetDlgItem(hWnd, IDC_TEXT_CONTROL), WM_CHAR, c, 0); + + } + break; + } } diff --git a/win/win32/mswproc.c b/win/win32/mswproc.c index 85f427725..0a6cde13d 100644 --- a/win/win32/mswproc.c +++ b/win/win32/mswproc.c @@ -2102,14 +2102,24 @@ mswin_main_loop() { MSG msg; - while (!mswin_have_input() && GetMessage(&msg, NULL, 0, 0) != 0) { - if (GetNHApp()->regNetHackMode - || !TranslateAccelerator(msg.hwnd, GetNHApp()->hAccelTable, - &msg)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } + while (!mswin_have_input()) { + if (!iflags.debug_fuzzer || PeekMessage(&msg, NULL, 0, 0, FALSE)) { + if(GetMessage(&msg, NULL, 0, 0) != 0) { + if (GetNHApp()->regNetHackMode + || !TranslateAccelerator(msg.hwnd, GetNHApp()->hAccelTable, + &msg)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } else { + /* WM_QUIT */ + break; + } + } else { + nhassert(iflags.debug_fuzzer); + NHEVENT_KBD(randomkey()); + } + } } /* clean up and quit */ @@ -2222,16 +2232,25 @@ mswin_popup_display(HWND hWnd, int *done_indicator) SetFocus(hWnd); /* go into message loop */ - while (IsWindow(hWnd) && (done_indicator == NULL || !*done_indicator) - && GetMessage(&msg, NULL, 0, 0) != 0) { - if (!IsDialogMessage(hWnd, &msg)) { - if (!TranslateAccelerator(msg.hwnd, GetNHApp()->hAccelTable, - &msg)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } - } + while (IsWindow(hWnd) && (done_indicator == NULL || !*done_indicator)) { + if (!iflags.debug_fuzzer || PeekMessage(&msg, NULL, 0, 0, FALSE)) { + if(GetMessage(&msg, NULL, 0, 0) != 0) { + if (!IsDialogMessage(hWnd, &msg)) { + if (!TranslateAccelerator(msg.hwnd, GetNHApp()->hAccelTable, + &msg)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + } else { + /* WM_QUIT */ + break; + } + } else { + nhassert(iflags.debug_fuzzer); + PostMessage(hWnd, WM_MSNH_COMMAND, MSNH_MSG_RANDOM_INPUT, 0); + } + } } void