2015-05-28 14:41:32 -07:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2018-12-20 20:44:15 -08:00
|
|
|
#include "snapshot/win/module_snapshot_win.h"
|
2015-05-28 14:41:32 -07:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-09-22 10:37:11 -07:00
|
|
|
#include "base/files/file_path.h"
|
2015-05-28 14:41:32 -07:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2016-01-06 12:22:50 -05:00
|
|
|
#include "build/build_config.h"
|
2015-05-28 14:41:32 -07:00
|
|
|
#include "client/crashpad_info.h"
|
|
|
|
#include "client/simple_string_dictionary.h"
|
|
|
|
#include "gtest/gtest.h"
|
2017-10-31 17:21:03 -04:00
|
|
|
#include "snapshot/annotation_snapshot.h"
|
2015-05-28 14:41:32 -07:00
|
|
|
#include "snapshot/win/pe_image_reader.h"
|
|
|
|
#include "snapshot/win/process_reader_win.h"
|
2017-04-03 13:53:11 -04:00
|
|
|
#include "test/test_paths.h"
|
2015-09-22 10:37:11 -07:00
|
|
|
#include "test/win/child_launcher.h"
|
2015-05-28 14:41:32 -07:00
|
|
|
#include "util/file/file_io.h"
|
|
|
|
#include "util/win/process_info.h"
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace test {
|
|
|
|
namespace {
|
|
|
|
|
2015-07-31 12:31:58 -04:00
|
|
|
enum TestType {
|
|
|
|
// Don't crash, just test the CrashpadInfo interface.
|
|
|
|
kDontCrash = 0,
|
2015-05-28 14:41:32 -07:00
|
|
|
|
2015-07-31 12:31:58 -04:00
|
|
|
// The child process should crash by __debugbreak().
|
|
|
|
kCrashDebugBreak,
|
|
|
|
};
|
2015-05-28 14:41:32 -07:00
|
|
|
|
2017-11-01 10:37:01 -04:00
|
|
|
void TestAnnotationsOnCrash(TestType type,
|
|
|
|
TestPaths::Architecture architecture) {
|
2015-09-22 10:37:11 -07:00
|
|
|
// Spawn a child process, passing it the pipe name to connect to.
|
2017-11-01 10:37:01 -04:00
|
|
|
base::FilePath child_test_executable =
|
|
|
|
TestPaths::BuildArtifact(L"snapshot",
|
|
|
|
L"annotations",
|
|
|
|
TestPaths::FileType::kExecutable,
|
|
|
|
architecture);
|
2015-09-22 10:37:11 -07:00
|
|
|
ChildLauncher child(child_test_executable, L"");
|
2017-04-19 13:38:26 -04:00
|
|
|
ASSERT_NO_FATAL_FAILURE(child.Start());
|
2015-09-22 10:37:11 -07:00
|
|
|
|
|
|
|
// Wait for the child process to indicate that it's done setting up its
|
|
|
|
// annotations via the CrashpadInfo interface.
|
|
|
|
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
|
|
|
CheckedReadFileExactly(child.stdout_read_handle(), &c, sizeof(c));
|
2015-09-22 10:37:11 -07:00
|
|
|
|
|
|
|
ProcessReaderWin process_reader;
|
|
|
|
ASSERT_TRUE(process_reader.Initialize(child.process_handle(),
|
|
|
|
ProcessSuspensionState::kRunning));
|
|
|
|
|
2017-10-31 17:21:03 -04:00
|
|
|
// Read all the kinds of annotations referenced from the CrashpadInfo
|
|
|
|
// structure.
|
2015-09-22 10:37:11 -07:00
|
|
|
const std::vector<ProcessInfo::Module>& modules = process_reader.Modules();
|
|
|
|
std::map<std::string, std::string> all_annotations_simple_map;
|
2017-10-31 17:21:03 -04:00
|
|
|
std::vector<AnnotationSnapshot> all_annotation_objects;
|
2015-09-22 10:37:11 -07:00
|
|
|
for (const ProcessInfo::Module& module : modules) {
|
2018-12-20 20:44:15 -08:00
|
|
|
internal::ModuleSnapshotWin module_snapshot;
|
|
|
|
module_snapshot.Initialize(&process_reader, module);
|
2017-10-31 17:21:03 -04:00
|
|
|
|
2015-09-22 10:37:11 -07:00
|
|
|
std::map<std::string, std::string> module_annotations_simple_map =
|
2018-12-20 20:44:15 -08:00
|
|
|
module_snapshot.AnnotationsSimpleMap();
|
2015-09-22 10:37:11 -07:00
|
|
|
all_annotations_simple_map.insert(module_annotations_simple_map.begin(),
|
|
|
|
module_annotations_simple_map.end());
|
2017-10-31 17:21:03 -04:00
|
|
|
|
2018-12-20 20:44:15 -08:00
|
|
|
auto module_annotations_list = module_snapshot.AnnotationObjects();
|
2017-10-31 17:21:03 -04:00
|
|
|
all_annotation_objects.insert(all_annotation_objects.end(),
|
|
|
|
module_annotations_list.begin(),
|
|
|
|
module_annotations_list.end());
|
2015-05-28 14:41:32 -07:00
|
|
|
}
|
|
|
|
|
2017-10-31 17:21:03 -04:00
|
|
|
// Verify the "simple map" annotations.
|
2015-09-22 10:37:11 -07:00
|
|
|
EXPECT_GE(all_annotations_simple_map.size(), 5u);
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
EXPECT_EQ(all_annotations_simple_map["#TEST# pad"], "crash");
|
|
|
|
EXPECT_EQ(all_annotations_simple_map["#TEST# key"], "value");
|
|
|
|
EXPECT_EQ(all_annotations_simple_map["#TEST# x"], "y");
|
|
|
|
EXPECT_EQ(all_annotations_simple_map["#TEST# longer"], "shorter");
|
|
|
|
EXPECT_EQ(all_annotations_simple_map["#TEST# empty_value"], "");
|
2015-09-22 10:37:11 -07:00
|
|
|
|
2017-10-31 17:21:03 -04:00
|
|
|
// Verify the typed annotation objects.
|
2017-11-01 20:27:19 -04:00
|
|
|
EXPECT_EQ(all_annotation_objects.size(), 3u);
|
2017-10-31 17:21:03 -04:00
|
|
|
bool saw_same_name_3 = false, saw_same_name_4 = false;
|
|
|
|
for (const auto& annotation : all_annotation_objects) {
|
|
|
|
EXPECT_EQ(annotation.type,
|
|
|
|
static_cast<uint16_t>(Annotation::Type::kString));
|
|
|
|
std::string value(reinterpret_cast<const char*>(annotation.value.data()),
|
|
|
|
annotation.value.size());
|
|
|
|
|
|
|
|
if (annotation.name == "#TEST# one") {
|
|
|
|
EXPECT_EQ(value, "moocow");
|
|
|
|
} else if (annotation.name == "#TEST# same-name") {
|
|
|
|
if (value == "same-name 3") {
|
|
|
|
EXPECT_FALSE(saw_same_name_3);
|
|
|
|
saw_same_name_3 = true;
|
|
|
|
} else if (value == "same-name 4") {
|
|
|
|
EXPECT_FALSE(saw_same_name_4);
|
|
|
|
saw_same_name_4 = true;
|
|
|
|
} else {
|
|
|
|
ADD_FAILURE() << "unexpected annotation value " << value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ADD_FAILURE() << "unexpected annotation " << annotation.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 10:37:11 -07:00
|
|
|
// Tell the child process to continue.
|
|
|
|
DWORD expected_exit_code;
|
|
|
|
switch (type) {
|
|
|
|
case kDontCrash:
|
|
|
|
c = ' ';
|
|
|
|
expected_exit_code = 0;
|
|
|
|
break;
|
|
|
|
case kCrashDebugBreak:
|
|
|
|
c = 'd';
|
|
|
|
expected_exit_code = STATUS_BREAKPOINT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAIL();
|
2015-05-28 14:41:32 -07:00
|
|
|
}
|
2015-09-22 10:37:11 -07:00
|
|
|
CheckedWriteFile(child.stdin_write_handle(), &c, sizeof(c));
|
2015-05-28 14:41:32 -07:00
|
|
|
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
EXPECT_EQ(child.WaitForExit(), expected_exit_code);
|
2015-09-22 10:37:11 -07:00
|
|
|
}
|
2015-05-28 14:41:32 -07:00
|
|
|
|
2018-12-20 20:44:15 -08:00
|
|
|
TEST(ModuleSnapshotWinTest, DontCrash) {
|
2017-11-01 10:37:01 -04:00
|
|
|
TestAnnotationsOnCrash(kDontCrash, TestPaths::Architecture::kDefault);
|
2015-05-28 14:41:32 -07:00
|
|
|
}
|
|
|
|
|
2018-12-20 20:44:15 -08:00
|
|
|
TEST(ModuleSnapshotWinTest, CrashDebugBreak) {
|
2017-11-01 10:37:01 -04:00
|
|
|
TestAnnotationsOnCrash(kCrashDebugBreak, TestPaths::Architecture::kDefault);
|
2015-09-22 10:37:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(ARCH_CPU_64_BITS)
|
2018-12-20 20:44:15 -08:00
|
|
|
TEST(ModuleSnapshotWinTest, DontCrashWOW64) {
|
2017-11-01 10:37:01 -04:00
|
|
|
if (!TestPaths::Has32BitBuildArtifacts()) {
|
2019-10-11 12:07:21 -04:00
|
|
|
GTEST_SKIP();
|
win: Dynamically disable WoW64 tests absent explicit 32-bit build output
Rather than having the 64-bit build assume that it lives in
out\{Debug,Release}_x64 and that it can find 32-bit build output in
out\{Debug,Release}, require the location of 32-bit build output to be
provided explicitly via the CRASHPAD_TEST_32_BIT_OUTPUT environment
variable. If this variable is not set, 64-bit tests that require 32-bit
test build output will dynamically disable themselves at runtime.
In order for this to work, a new DISABLED_TEST() macro is added to
support dynamically disabled tests. gtest does not have its own
first-class support for this
(https://groups.google.com/d/topic/googletestframework/Nwh3u7YFuN4,
https://github.com/google/googletest/issues/490) so this local solution
is used instead.
For tests via Crashpad’s own build\run_tests.py, which is how Crashpad’s
own buildbots and trybots invoke tests, CRASHPAD_TEST_32_BIT_OUTPUT is
set to a locaton compatible with the paths expected for the GYP-based
build. No test coverage is lost on Crashpad’s own buildbots and trybots.
For Crashpad tests in Chromium’s buildbots and trybots, this environment
variable will not be set, causing these tests to be dynamically
disabled.
Bug: crashpad:203, chromium:743139, chromium:777924
Change-Id: I3c0de2bf4f835e13ed5a4adda5760d6fed508126
Reviewed-on: https://chromium-review.googlesource.com/739795
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-10-26 13:48:01 -04:00
|
|
|
}
|
|
|
|
|
2017-11-01 10:37:01 -04:00
|
|
|
TestAnnotationsOnCrash(kDontCrash, TestPaths::Architecture::k32Bit);
|
2015-09-22 10:37:11 -07:00
|
|
|
}
|
|
|
|
|
2018-12-20 20:44:15 -08:00
|
|
|
TEST(ModuleSnapshotWinTest, CrashDebugBreakWOW64) {
|
2017-11-01 10:37:01 -04:00
|
|
|
if (!TestPaths::Has32BitBuildArtifacts()) {
|
2019-10-11 12:07:21 -04:00
|
|
|
GTEST_SKIP();
|
win: Dynamically disable WoW64 tests absent explicit 32-bit build output
Rather than having the 64-bit build assume that it lives in
out\{Debug,Release}_x64 and that it can find 32-bit build output in
out\{Debug,Release}, require the location of 32-bit build output to be
provided explicitly via the CRASHPAD_TEST_32_BIT_OUTPUT environment
variable. If this variable is not set, 64-bit tests that require 32-bit
test build output will dynamically disable themselves at runtime.
In order for this to work, a new DISABLED_TEST() macro is added to
support dynamically disabled tests. gtest does not have its own
first-class support for this
(https://groups.google.com/d/topic/googletestframework/Nwh3u7YFuN4,
https://github.com/google/googletest/issues/490) so this local solution
is used instead.
For tests via Crashpad’s own build\run_tests.py, which is how Crashpad’s
own buildbots and trybots invoke tests, CRASHPAD_TEST_32_BIT_OUTPUT is
set to a locaton compatible with the paths expected for the GYP-based
build. No test coverage is lost on Crashpad’s own buildbots and trybots.
For Crashpad tests in Chromium’s buildbots and trybots, this environment
variable will not be set, causing these tests to be dynamically
disabled.
Bug: crashpad:203, chromium:743139, chromium:777924
Change-Id: I3c0de2bf4f835e13ed5a4adda5760d6fed508126
Reviewed-on: https://chromium-review.googlesource.com/739795
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-10-26 13:48:01 -04:00
|
|
|
}
|
|
|
|
|
2017-11-01 10:37:01 -04:00
|
|
|
TestAnnotationsOnCrash(kCrashDebugBreak, TestPaths::Architecture::k32Bit);
|
2015-05-28 14:41:32 -07:00
|
|
|
}
|
2015-09-22 10:37:11 -07:00
|
|
|
#endif // ARCH_CPU_64_BITS
|
2015-05-28 14:41:32 -07:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace test
|
|
|
|
} // namespace crashpad
|