129inline int RunChild(
const std::vector<HookTestCase>& cases) {
131 DWORD length = GetModuleFileNameA(NULL, dllPath,
MAX_PATH);
132 char* lastSlash = (length != 0 && length <
MAX_PATH) ? strrchr(dllPath,
'\\') : NULL;
133 if (lastSlash == NULL) {
134 fprintf(stderr,
" unexpected executable path\n");
137 *(lastSlash + 1) =
'\0';
140 if (LoadLibraryA(dllPath) == NULL) {
141 fprintf(stderr,
" failed to load %s (error %lu)\n", dllPath, GetLastError());
150 for (
size_t i = 0; i < cases.size(); i++) {
151 std::string marker =
kCaseMarker + std::to_string(i);
152 OutputDebugStringA(marker.c_str());
153 cases[i].Run(context);
164inline bool CaptureChild(
const char* exePath,
size_t caseCount, std::vector<std::vector<std::string>>& messages) {
165 messages.assign(caseCount, {});
168 sprintf_s(commandLine,
"\"%s\" --child", exePath);
172 SECURITY_ATTRIBUTES inherit{
sizeof(inherit), NULL, TRUE };
173 HANDLE nul = CreateFileA(
"NUL", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &inherit, OPEN_EXISTING, 0, NULL);
174 STARTUPINFOA si{
sizeof(si) };
175 si.dwFlags = STARTF_USESTDHANDLES;
176 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
178 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
180 PROCESS_INFORMATION pi{};
181 BOOL started = CreateProcessA(NULL, commandLine, NULL, NULL, TRUE, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi);
182 DWORD startError = GetLastError();
183 if (nul != INVALID_HANDLE_VALUE) {
187 printf(
"failed to start the test child (error %lu)\n", startError);
193 size_t currentCase = SIZE_MAX;
197 if (!WaitForDebugEvent(&event, INFINITE)) {
202 DWORD continueStatus = DBG_CONTINUE;
203 switch (event.dwDebugEventCode) {
204 case OUTPUT_DEBUG_STRING_EVENT: {
207 SIZE_T toRead =
event.u.DebugString.nDebugStringLength;
208 if (toRead >
sizeof(text) - 1) {
209 toRead =
sizeof(text) - 1;
211 SIZE_T bytesRead = 0;
212 ReadProcessMemory(pi.hProcess, event.u.DebugString.lpDebugStringData, text, toRead, &bytesRead);
213 text[bytesRead] =
'\0';
215 std::string line(text);
216 while (!line.empty() && (line.back() ==
'\n' || line.back() ==
'\r' || line.back() ==
'\0')) {
221 currentCase = strtoul(line.c_str() + strlen(
kCaseMarker), NULL, 10);
225 currentCase = SIZE_MAX;
228 if (currentCase < caseCount) {
229 messages[currentCase].push_back(line.substr(strlen(
kMessagePrefix)));
240 case EXCEPTION_DEBUG_EVENT:
241 if (event.u.Exception.ExceptionRecord.ExceptionCode != EXCEPTION_BREAKPOINT) {
242 if (!event.u.Exception.dwFirstChance) {
243 printf(
"test child crashed with exception 0x%08lx\n", event.u.Exception.ExceptionRecord.ExceptionCode);
246 continueStatus = DBG_EXCEPTION_NOT_HANDLED;
249 case CREATE_PROCESS_DEBUG_EVENT:
250 if (event.u.CreateProcessInfo.hFile != NULL) {
251 CloseHandle(event.u.CreateProcessInfo.hFile);
254 case LOAD_DLL_DEBUG_EVENT:
255 if (event.u.LoadDll.hFile != NULL) {
256 CloseHandle(event.u.LoadDll.hFile);
259 case EXIT_PROCESS_DEBUG_EVENT:
260 exitCode =
event.u.ExitProcess.dwExitCode;
264 ContinueDebugEvent(event.dwProcessId, event.dwThreadId, continueStatus);
265 if (event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) {
270 CloseHandle(pi.hThread);
271 CloseHandle(pi.hProcess);
274 printf(
"test child exited with code %lu\n", exitCode);