2022-09-06 19:14:07 -04:00
|
|
|
// Copyright 2017 The Crashpad Authors
|
2017-10-25 16:57:44 -04:00
|
|
|
//
|
|
|
|
// 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 "client/annotation.h"
|
|
|
|
|
2017-12-08 12:55:08 -05:00
|
|
|
#include <array>
|
2017-10-25 16:57:44 -04:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "client/annotation_list.h"
|
|
|
|
#include "client/crashpad_info.h"
|
|
|
|
#include "gtest/gtest.h"
|
mac: Tests that crash intentionally shouldn’t go to ReportCrash
Crashpad has many tests that crash intentionally. Some of these are
gtest death tests, and others arrange for intentional crashes to test
Crashpad’s own crash-catching logic. On macOS, all of the gtest death
tests and some of the other intentional crashes were being logged by
ReportCrash, the system’s crash reporter. Since these reports
corresponded to intentional crashes, they were never useful, and served
only to clutter ~/Library/Logs/DiagnosticReports.
Since Crashpad is adept at handling exceptions on its own, this
introduces the “exception swallowing server”,
crashpad_exception_swallower, which is a Mach exception server that
implements a no-op exception handler routine for all exceptions
received. The exception swallowing server is established as the task
handler for EXC_CRASH and EXC_CORPSE_NOTIFY exceptions during gtest
death tests invoked by {ASSERT,EXPECT}_DEATH_{CHECK,CRASH}, and for all
child processes invoked by the Multiprocess test infrastructure. The
exception swallowing server is not in effect at other times, so
unexpected crashes in test code can still be handled by ReportCrash or
another crash reporter.
With this change in place, no new reports are generated in the
user-level ~/Library/Logs/DiagnosticReports or the system’s
/Library/Logs/DiagnosticReports during a run of Crashpad’s full test
suite on macOS.
Bug: crashpad:33
Change-Id: I13891853a7e25accc30da21fa7ea8bd7d1f3bd2f
Reviewed-on: https://chromium-review.googlesource.com/777859
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-11-20 13:32:26 -05:00
|
|
|
#include "test/gtest_death.h"
|
2023-01-31 14:28:15 -07:00
|
|
|
#include "util/misc/clock.h"
|
|
|
|
#include "util/synchronization/scoped_spin_guard.h"
|
|
|
|
#include "util/thread/thread.h"
|
2017-10-25 16:57:44 -04:00
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace test {
|
|
|
|
namespace {
|
|
|
|
|
2023-01-31 14:28:15 -07:00
|
|
|
class SpinGuardAnnotation final : public Annotation {
|
|
|
|
public:
|
|
|
|
SpinGuardAnnotation(Annotation::Type type, const char name[])
|
|
|
|
: Annotation(type,
|
|
|
|
name,
|
|
|
|
&value_,
|
|
|
|
ConcurrentAccessGuardMode::kScopedSpinGuard) {}
|
|
|
|
|
|
|
|
bool Set(bool value, uint64_t spin_guard_timeout_ns) {
|
|
|
|
auto guard = TryCreateScopedSpinGuard(spin_guard_timeout_ns);
|
|
|
|
if (!guard) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value_ = value;
|
|
|
|
SetSize(sizeof(value_));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScopedSpinGuardUnlockThread final : public Thread {
|
|
|
|
public:
|
|
|
|
ScopedSpinGuardUnlockThread(ScopedSpinGuard scoped_spin_guard,
|
|
|
|
uint64_t sleep_time_ns)
|
|
|
|
: scoped_spin_guard_(std::move(scoped_spin_guard)),
|
|
|
|
sleep_time_ns_(sleep_time_ns) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ThreadMain() override {
|
|
|
|
SleepNanoseconds(sleep_time_ns_);
|
|
|
|
|
|
|
|
// Move the ScopedSpinGuard member into a local variable which is
|
|
|
|
// destroyed when ThreadMain() returns.
|
|
|
|
ScopedSpinGuard local_scoped_spin_guard(std::move(scoped_spin_guard_));
|
|
|
|
|
|
|
|
// After this point, local_scoped_spin_guard will be destroyed and unlocked.
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedSpinGuard scoped_spin_guard_;
|
|
|
|
const uint64_t sleep_time_ns_;
|
|
|
|
};
|
|
|
|
|
2017-10-25 16:57:44 -04:00
|
|
|
class Annotation : public testing::Test {
|
|
|
|
public:
|
|
|
|
void SetUp() override {
|
|
|
|
CrashpadInfo::GetCrashpadInfo()->set_annotations_list(&annotations_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
CrashpadInfo::GetCrashpadInfo()->set_annotations_list(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t AnnotationsCount() {
|
|
|
|
size_t result = 0;
|
|
|
|
for (auto* annotation : annotations_) {
|
|
|
|
if (annotation->is_set())
|
|
|
|
++result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
crashpad::AnnotationList annotations_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(Annotation, Basics) {
|
|
|
|
constexpr crashpad::Annotation::Type kType =
|
|
|
|
crashpad::Annotation::UserDefinedType(1);
|
|
|
|
|
|
|
|
const char kName[] = "annotation 1";
|
|
|
|
char buffer[1024];
|
|
|
|
crashpad::Annotation annotation(kType, kName, buffer);
|
|
|
|
|
|
|
|
EXPECT_FALSE(annotation.is_set());
|
|
|
|
EXPECT_EQ(0u, AnnotationsCount());
|
|
|
|
|
|
|
|
EXPECT_EQ(kType, annotation.type());
|
|
|
|
EXPECT_EQ(0u, annotation.size());
|
|
|
|
EXPECT_EQ(std::string(kName), annotation.name());
|
|
|
|
EXPECT_EQ(buffer, annotation.value());
|
|
|
|
|
|
|
|
annotation.SetSize(10);
|
|
|
|
|
|
|
|
EXPECT_TRUE(annotation.is_set());
|
|
|
|
EXPECT_EQ(1u, AnnotationsCount());
|
|
|
|
|
|
|
|
EXPECT_EQ(10u, annotation.size());
|
|
|
|
EXPECT_EQ(&annotation, *annotations_.begin());
|
|
|
|
|
|
|
|
annotation.Clear();
|
|
|
|
|
|
|
|
EXPECT_FALSE(annotation.is_set());
|
|
|
|
EXPECT_EQ(0u, AnnotationsCount());
|
|
|
|
|
|
|
|
EXPECT_EQ(0u, annotation.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Annotation, StringType) {
|
|
|
|
crashpad::StringAnnotation<5> annotation("name");
|
|
|
|
|
|
|
|
EXPECT_FALSE(annotation.is_set());
|
|
|
|
|
|
|
|
EXPECT_EQ(crashpad::Annotation::Type::kString, annotation.type());
|
|
|
|
EXPECT_EQ(0u, annotation.size());
|
|
|
|
EXPECT_EQ(std::string("name"), annotation.name());
|
2017-11-16 15:10:08 -05:00
|
|
|
EXPECT_EQ(0u, annotation.value().size());
|
2017-10-25 16:57:44 -04:00
|
|
|
|
|
|
|
annotation.Set("test");
|
|
|
|
|
|
|
|
EXPECT_TRUE(annotation.is_set());
|
|
|
|
EXPECT_EQ(1u, AnnotationsCount());
|
|
|
|
|
|
|
|
EXPECT_EQ(4u, annotation.size());
|
2017-11-16 15:10:08 -05:00
|
|
|
EXPECT_EQ("test", annotation.value());
|
2017-10-25 16:57:44 -04:00
|
|
|
|
2017-11-16 15:10:08 -05:00
|
|
|
annotation.Set(std::string("loooooooooooong"));
|
2017-10-25 16:57:44 -04:00
|
|
|
|
|
|
|
EXPECT_TRUE(annotation.is_set());
|
|
|
|
EXPECT_EQ(1u, AnnotationsCount());
|
|
|
|
|
|
|
|
EXPECT_EQ(5u, annotation.size());
|
2017-11-16 15:10:08 -05:00
|
|
|
EXPECT_EQ("loooo", annotation.value());
|
mac: Tests that crash intentionally shouldn’t go to ReportCrash
Crashpad has many tests that crash intentionally. Some of these are
gtest death tests, and others arrange for intentional crashes to test
Crashpad’s own crash-catching logic. On macOS, all of the gtest death
tests and some of the other intentional crashes were being logged by
ReportCrash, the system’s crash reporter. Since these reports
corresponded to intentional crashes, they were never useful, and served
only to clutter ~/Library/Logs/DiagnosticReports.
Since Crashpad is adept at handling exceptions on its own, this
introduces the “exception swallowing server”,
crashpad_exception_swallower, which is a Mach exception server that
implements a no-op exception handler routine for all exceptions
received. The exception swallowing server is established as the task
handler for EXC_CRASH and EXC_CORPSE_NOTIFY exceptions during gtest
death tests invoked by {ASSERT,EXPECT}_DEATH_{CHECK,CRASH}, and for all
child processes invoked by the Multiprocess test infrastructure. The
exception swallowing server is not in effect at other times, so
unexpected crashes in test code can still be handled by ReportCrash or
another crash reporter.
With this change in place, no new reports are generated in the
user-level ~/Library/Logs/DiagnosticReports or the system’s
/Library/Logs/DiagnosticReports during a run of Crashpad’s full test
suite on macOS.
Bug: crashpad:33
Change-Id: I13891853a7e25accc30da21fa7ea8bd7d1f3bd2f
Reviewed-on: https://chromium-review.googlesource.com/777859
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-11-20 13:32:26 -05:00
|
|
|
}
|
2017-11-16 15:10:08 -05:00
|
|
|
|
2023-01-31 14:28:15 -07:00
|
|
|
TEST_F(Annotation, BaseAnnotationShouldNotSupportSpinGuard) {
|
|
|
|
char buffer[1024];
|
|
|
|
crashpad::Annotation annotation(
|
|
|
|
crashpad::Annotation::Type::kString, "no-spin-guard", buffer);
|
|
|
|
EXPECT_EQ(annotation.concurrent_access_guard_mode(),
|
|
|
|
crashpad::Annotation::ConcurrentAccessGuardMode::kUnguarded);
|
2023-02-15 12:09:32 -07:00
|
|
|
#if !DCHECK_IS_ON()
|
|
|
|
// This fails a DCHECK() in debug builds, so only test it when DCHECK()
|
|
|
|
// is not on.
|
2023-01-31 14:28:15 -07:00
|
|
|
EXPECT_EQ(std::nullopt, annotation.TryCreateScopedSpinGuard(0));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Annotation, CustomAnnotationShouldSupportSpinGuardAndSet) {
|
|
|
|
constexpr crashpad::Annotation::Type kType =
|
|
|
|
crashpad::Annotation::UserDefinedType(1);
|
|
|
|
SpinGuardAnnotation spin_guard_annotation(kType, "spin-guard");
|
|
|
|
EXPECT_EQ(spin_guard_annotation.concurrent_access_guard_mode(),
|
|
|
|
crashpad::Annotation::ConcurrentAccessGuardMode::kScopedSpinGuard);
|
|
|
|
EXPECT_TRUE(spin_guard_annotation.Set(true, 0));
|
|
|
|
EXPECT_EQ(1U, spin_guard_annotation.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Annotation, CustomAnnotationSetShouldFailIfRunConcurrently) {
|
|
|
|
constexpr crashpad::Annotation::Type kType =
|
|
|
|
crashpad::Annotation::UserDefinedType(1);
|
|
|
|
SpinGuardAnnotation spin_guard_annotation(kType, "spin-guard");
|
|
|
|
auto guard = spin_guard_annotation.TryCreateScopedSpinGuard(0);
|
|
|
|
EXPECT_NE(std::nullopt, guard);
|
|
|
|
// This should fail, since the guard is already held and the timeout is 0.
|
|
|
|
EXPECT_FALSE(spin_guard_annotation.Set(true, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Annotation,
|
|
|
|
CustomAnnotationSetShouldSucceedIfSpinGuardUnlockedAsynchronously) {
|
|
|
|
constexpr crashpad::Annotation::Type kType =
|
|
|
|
crashpad::Annotation::UserDefinedType(1);
|
|
|
|
SpinGuardAnnotation spin_guard_annotation(kType, "spin-guard");
|
|
|
|
auto guard = spin_guard_annotation.TryCreateScopedSpinGuard(0);
|
|
|
|
EXPECT_NE(std::nullopt, guard);
|
|
|
|
// Pass the guard off to a background thread which unlocks it after 1 ms.
|
|
|
|
constexpr uint64_t kSleepTimeNs = 1000000; // 1 ms
|
|
|
|
ScopedSpinGuardUnlockThread unlock_thread(std::move(guard.value()),
|
|
|
|
kSleepTimeNs);
|
|
|
|
unlock_thread.Start();
|
|
|
|
|
|
|
|
// Try to set the annotation with a 100 ms timeout.
|
|
|
|
constexpr uint64_t kSpinGuardTimeoutNanos = 100000000; // 100 ms
|
|
|
|
|
|
|
|
// This should succeed after 1 ms, since the timeout is much larger than the
|
|
|
|
// time the background thread holds the guard.
|
|
|
|
EXPECT_TRUE(spin_guard_annotation.Set(true, kSpinGuardTimeoutNanos));
|
|
|
|
|
|
|
|
unlock_thread.Join();
|
|
|
|
}
|
|
|
|
|
2017-12-08 12:55:08 -05:00
|
|
|
TEST(StringAnnotation, ArrayOfString) {
|
|
|
|
static crashpad::StringAnnotation<4> annotations[] = {
|
|
|
|
{"test-1", crashpad::StringAnnotation<4>::Tag::kArray},
|
|
|
|
{"test-2", crashpad::StringAnnotation<4>::Tag::kArray},
|
|
|
|
{"test-3", crashpad::StringAnnotation<4>::Tag::kArray},
|
|
|
|
{"test-4", crashpad::StringAnnotation<4>::Tag::kArray},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& annotation : annotations) {
|
|
|
|
EXPECT_FALSE(annotation.is_set());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 15:10:08 -05:00
|
|
|
#if DCHECK_IS_ON()
|
mac: Tests that crash intentionally shouldn’t go to ReportCrash
Crashpad has many tests that crash intentionally. Some of these are
gtest death tests, and others arrange for intentional crashes to test
Crashpad’s own crash-catching logic. On macOS, all of the gtest death
tests and some of the other intentional crashes were being logged by
ReportCrash, the system’s crash reporter. Since these reports
corresponded to intentional crashes, they were never useful, and served
only to clutter ~/Library/Logs/DiagnosticReports.
Since Crashpad is adept at handling exceptions on its own, this
introduces the “exception swallowing server”,
crashpad_exception_swallower, which is a Mach exception server that
implements a no-op exception handler routine for all exceptions
received. The exception swallowing server is established as the task
handler for EXC_CRASH and EXC_CORPSE_NOTIFY exceptions during gtest
death tests invoked by {ASSERT,EXPECT}_DEATH_{CHECK,CRASH}, and for all
child processes invoked by the Multiprocess test infrastructure. The
exception swallowing server is not in effect at other times, so
unexpected crashes in test code can still be handled by ReportCrash or
another crash reporter.
With this change in place, no new reports are generated in the
user-level ~/Library/Logs/DiagnosticReports or the system’s
/Library/Logs/DiagnosticReports during a run of Crashpad’s full test
suite on macOS.
Bug: crashpad:33
Change-Id: I13891853a7e25accc30da21fa7ea8bd7d1f3bd2f
Reviewed-on: https://chromium-review.googlesource.com/777859
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-11-20 13:32:26 -05:00
|
|
|
|
|
|
|
TEST(AnnotationDeathTest, EmbeddedNUL) {
|
|
|
|
crashpad::StringAnnotation<5> annotation("name");
|
|
|
|
EXPECT_DEATH_CHECK(annotation.Set(std::string("te\0st", 5)), "embedded NUL");
|
2017-10-25 16:57:44 -04:00
|
|
|
}
|
|
|
|
|
mac: Tests that crash intentionally shouldn’t go to ReportCrash
Crashpad has many tests that crash intentionally. Some of these are
gtest death tests, and others arrange for intentional crashes to test
Crashpad’s own crash-catching logic. On macOS, all of the gtest death
tests and some of the other intentional crashes were being logged by
ReportCrash, the system’s crash reporter. Since these reports
corresponded to intentional crashes, they were never useful, and served
only to clutter ~/Library/Logs/DiagnosticReports.
Since Crashpad is adept at handling exceptions on its own, this
introduces the “exception swallowing server”,
crashpad_exception_swallower, which is a Mach exception server that
implements a no-op exception handler routine for all exceptions
received. The exception swallowing server is established as the task
handler for EXC_CRASH and EXC_CORPSE_NOTIFY exceptions during gtest
death tests invoked by {ASSERT,EXPECT}_DEATH_{CHECK,CRASH}, and for all
child processes invoked by the Multiprocess test infrastructure. The
exception swallowing server is not in effect at other times, so
unexpected crashes in test code can still be handled by ReportCrash or
another crash reporter.
With this change in place, no new reports are generated in the
user-level ~/Library/Logs/DiagnosticReports or the system’s
/Library/Logs/DiagnosticReports during a run of Crashpad’s full test
suite on macOS.
Bug: crashpad:33
Change-Id: I13891853a7e25accc30da21fa7ea8bd7d1f3bd2f
Reviewed-on: https://chromium-review.googlesource.com/777859
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-11-20 13:32:26 -05:00
|
|
|
#endif
|
|
|
|
|
2017-10-25 16:57:44 -04:00
|
|
|
} // namespace
|
|
|
|
} // namespace test
|
|
|
|
} // namespace crashpad
|