2016-04-22 10:03:59 -07:00
|
|
|
// Copyright 2016 The Crashpad Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tlhelp32.h>
|
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/logging.h"
|
2017-03-24 18:06:08 -04:00
|
|
|
#include "base/strings/stringprintf.h"
|
2016-04-22 10:03:59 -07:00
|
|
|
#include "client/crashpad_client.h"
|
2017-04-03 13:53:11 -04:00
|
|
|
#include "test/test_paths.h"
|
2016-04-22 10:03:59 -07:00
|
|
|
#include "test/win/child_launcher.h"
|
|
|
|
#include "util/file/file_io.h"
|
|
|
|
#include "util/win/scoped_handle.h"
|
|
|
|
#include "util/win/xp_compat.h"
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace test {
|
|
|
|
namespace {
|
|
|
|
|
2017-03-24 18:06:08 -04:00
|
|
|
constexpr DWORD kCrashAndDumpTargetExitCode = 0xdeadbea7;
|
|
|
|
|
2016-04-22 10:03:59 -07:00
|
|
|
bool CrashAndDumpTarget(const CrashpadClient& client, HANDLE process) {
|
|
|
|
DWORD target_pid = GetProcessId(process);
|
|
|
|
|
2017-03-24 18:06:08 -04:00
|
|
|
ScopedFileHANDLE thread_snap(CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0));
|
|
|
|
if (!thread_snap.is_valid()) {
|
|
|
|
PLOG(ERROR) << "CreateToolhelp32Snapshot";
|
2016-04-22 10:03:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
THREADENTRY32 te32;
|
|
|
|
te32.dwSize = sizeof(THREADENTRY32);
|
|
|
|
if (!Thread32First(thread_snap.get(), &te32)) {
|
2017-03-24 18:06:08 -04:00
|
|
|
PLOG(ERROR) << "Thread32First";
|
2016-04-22 10:03:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (te32.th32OwnerProcessID == target_pid) {
|
2016-11-03 11:35:19 -07:00
|
|
|
// We set the thread priority of "Thread1" to a non-default value before
|
|
|
|
// going to sleep. Dump and blame this thread. For an explanation of
|
|
|
|
// "9", see
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100.aspx.
|
|
|
|
if (te32.tpBasePri == 9) {
|
2016-04-22 10:03:59 -07:00
|
|
|
ScopedKernelHANDLE thread(
|
|
|
|
OpenThread(kXPThreadAllAccess, false, te32.th32ThreadID));
|
2017-03-24 18:06:08 -04:00
|
|
|
if (!thread.is_valid()) {
|
|
|
|
PLOG(ERROR) << "OpenThread";
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-22 10:03:59 -07:00
|
|
|
if (!client.DumpAndCrashTargetProcess(
|
2017-03-24 18:06:08 -04:00
|
|
|
process, thread.get(), kCrashAndDumpTargetExitCode)) {
|
2016-04-22 10:03:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (Thread32Next(thread_snap.get(), &te32));
|
|
|
|
|
2017-03-24 18:06:08 -04:00
|
|
|
LOG(ERROR) << "target not found";
|
2016-04-22 10:03:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CrashOtherProgram(int argc, wchar_t* argv[]) {
|
|
|
|
CrashpadClient client;
|
|
|
|
|
|
|
|
if (argc == 2 || argc == 3) {
|
|
|
|
if (!client.SetHandlerIPCPipe(argv[1])) {
|
2017-03-24 18:06:08 -04:00
|
|
|
LOG(ERROR) << "SetHandlerIPCPipe";
|
2016-04-22 10:03:59 -07:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Usage: %ls <server_pipe_name> [noexception]\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch another process that hangs.
|
2017-04-03 13:53:11 -04:00
|
|
|
base::FilePath test_executable = TestPaths::Executable();
|
2016-04-22 10:03:59 -07:00
|
|
|
std::wstring child_test_executable =
|
|
|
|
test_executable.DirName().Append(L"hanging_program.exe").value();
|
|
|
|
ChildLauncher child(child_test_executable, argv[1]);
|
|
|
|
child.Start();
|
|
|
|
|
|
|
|
// Wait until it's ready.
|
|
|
|
char c;
|
Make file_io reads more rational and predictable
ReadFile() attempted to continue reading after a short read. In most
cases, this is fine. However, ReadFile() would keep trying to fill a
partially-filled buffer until experiencing a 0-length read(), signaling
end-of-file. For certain weird file descriptors like terminal input, EOF
is an ephemeral condition, and attempting to read beyond EOF doesn’t
actually return 0 (EOF) provided that they remain open, it will block
waiting for more input. Consequently, ReadFile() and anything based on
ReadFile() had an undocumented and quirky interface, which was that any
short read that it returned (not an underlying short read) actually
indicated EOF.
This facet of ReadFile() was unexpected, so it’s being removed. The new
behavior is that ReadFile() will return an underlying short read. The
behavior of FileReaderInterface::Read() is updated in accordance with
this change.
Upon experiencing a short read, the caller can determine the best
action. Most callers were already prepared for this behavior. Outside of
util/file, only crashpad_database_util properly implemented EOF
detection according to previous semantics, and adapting it to new
semantics is trivial.
Callers who require an exact-length read can use the new
ReadFileExactly(), or the newly renamed LoggingReadFileExactly() or
CheckedReadFileExactly(). These functions will retry following a short
read. The renamed functions were previously called LoggingReadFile() and
CheckedReadFile(), but those names implied that they were simply
wrapping ReadFile(), which is not the case. They wrapped ReadFile() and
further, insisted on a full read. Since ReadFile()’s semantics are now
changing but these functions’ are not, they’re now even more distinct
from ReadFile(), and must be renamed to avoid confusion.
Test: *
Change-Id: I06b77e0d6ad8719bd2eb67dab93a8740542dd908
Reviewed-on: https://chromium-review.googlesource.com/456676
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-03-16 13:36:38 -04:00
|
|
|
if (!LoggingReadFileExactly(child.stdout_read_handle(), &c, sizeof(c)) ||
|
|
|
|
c != ' ') {
|
2016-04-22 10:03:59 -07:00
|
|
|
LOG(ERROR) << "failed child communication";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-03-24 18:06:08 -04:00
|
|
|
DWORD expect_exit_code;
|
2016-04-22 10:03:59 -07:00
|
|
|
if (argc == 3 && wcscmp(argv[2], L"noexception") == 0) {
|
2017-03-24 18:06:08 -04:00
|
|
|
expect_exit_code = CrashpadClient::kTriggeredExceptionCode;
|
|
|
|
if (!client.DumpAndCrashTargetProcess(child.process_handle(), 0, 0))
|
|
|
|
return EXIT_FAILURE;
|
2016-04-22 10:03:59 -07:00
|
|
|
} else {
|
2017-03-24 18:06:08 -04:00
|
|
|
expect_exit_code = kCrashAndDumpTargetExitCode;
|
|
|
|
if (!CrashAndDumpTarget(client, child.process_handle())) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD exit_code = child.WaitForExit();
|
|
|
|
if (exit_code != expect_exit_code) {
|
|
|
|
LOG(ERROR) << base::StringPrintf(
|
|
|
|
"incorrect exit code, expected 0x%x, observed 0x%x",
|
|
|
|
expect_exit_code,
|
|
|
|
exit_code);
|
|
|
|
return EXIT_FAILURE;
|
2016-04-22 10:03:59 -07:00
|
|
|
}
|
|
|
|
|
2017-03-24 18:06:08 -04:00
|
|
|
return EXIT_SUCCESS;
|
2016-04-22 10:03:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace test
|
|
|
|
} // namespace crashpad
|
|
|
|
|
|
|
|
int wmain(int argc, wchar_t* argv[]) {
|
|
|
|
return crashpad::test::CrashOtherProgram(argc, argv);
|
|
|
|
}
|