Skip to content

Commit b9a9635

Browse files
committed
hardening exception handling
1 parent 12191bf commit b9a9635

File tree

14 files changed

+106
-84
lines changed

14 files changed

+106
-84
lines changed

IntelPresentMon/AppCef/source/winmain.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,22 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
206206
// name this process / thread
207207
log::IdentificationTable::AddThisProcess(str::ToWide(opt.cefType.AsOptional().value_or("main-client")));
208208
log::IdentificationTable::AddThisThread(L"main");
209-
// connect to the diagnostic layer
209+
// connect to the diagnostic layer (not generally used by appcef since we connect to logging directly)
210210
std::optional<pmapi::DiagnosticHandler> diag;
211-
if (opt.enableDiagnostic && opt.cefType && *opt.cefType == "renderer") {
212-
diag.emplace(
213-
(PM_DIAGNOSTIC_LEVEL)opt.logLevel.AsOptional().value_or(log::GlobalPolicy::Get().GetLogLevel()),
214-
PM_DIAGNOSTIC_OUTPUT_FLAGS_DEBUGGER | PM_DIAGNOSTIC_OUTPUT_FLAGS_QUEUE,
215-
[](const PM_DIAGNOSTIC_MESSAGE& msg) {
211+
try {
212+
if (opt.enableDiagnostic && opt.cefType && *opt.cefType == "renderer") {
213+
diag.emplace(
214+
(PM_DIAGNOSTIC_LEVEL)opt.logLevel.AsOptional().value_or(log::GlobalPolicy::Get().GetLogLevel()),
215+
PM_DIAGNOSTIC_OUTPUT_FLAGS_DEBUGGER | PM_DIAGNOSTIC_OUTPUT_FLAGS_QUEUE,
216+
[](const PM_DIAGNOSTIC_MESSAGE& msg) {
216217
auto ts = msg.pTimestamp ? str::ToWide(msg.pTimestamp) : std::wstring{};
217218
pmlog_(log::Level(msg.level)).note(
218219
std::format(L"@@ D I A G @@ => <{}> {}", ts, str::ToWide(msg.pText)));
219220
}
220-
);
221-
}
221+
);
222+
}
223+
} pmcatch_report;
224+
222225
// configure the logging system (partially based on command line options)
223226
ConfigureLogging();
224227

IntelPresentMon/CommonUtilities/Exception.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ namespace pmon::util
7878
std::rethrow_exception(pEx);
7979
}
8080
catch (const std::exception& e) {
81-
return std::format("[{}] {}", typeid(e).name(), e.what());
81+
try { return std::format("[{}] {}", typeid(e).name(), e.what()); }
82+
catch (...) { return {}; }
8283
}
8384
catch (...) {
8485
return "Unrecognized exception";

IntelPresentMon/CommonUtilities/Memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace pmon::util
3636
CloningUptr& operator=(CloningUptr&& rhs) noexcept
3737
{
3838
std::unique_ptr<T>::reset(rhs.release());
39+
return *this;
3940
}
4041
~CloningUptr() = default;
4142
};

IntelPresentMon/CommonUtilities/log/DiagnosticDriver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "DiagnosticDriver.h"
22
#include "../Exception.h"
33
#include "../win/WinAPI.h"
4+
#include "PanicLogger.h"
45
#include <iostream>
56
#include <span>
67
#include <algorithm>
@@ -19,7 +20,8 @@ namespace pmon::util::log
1920
DiagnosticDriver::~DiagnosticDriver()
2021
{
2122
dying_ = true;
22-
manualUnblockEvent_.Set();
23+
try { manualUnblockEvent_.Set(); }
24+
catch (...) { pmlog_panic_(L"Failed to set the manual unblock event"); }
2325
}
2426
void DiagnosticDriver::Submit(const Entry& e)
2527
{

IntelPresentMon/CommonUtilities/log/EntryBuilder.cpp

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,18 @@ namespace pmon::util::log
5555
}
5656
EntryBuilder& EntryBuilder::mark(const TimePoint& tp) noexcept
5757
{
58-
const auto now = std::chrono::high_resolution_clock::now();
59-
const auto duration = std::chrono::duration<double, std::milli>(now - tp.value).count();
58+
try {
59+
const auto now = std::chrono::high_resolution_clock::now();
60+
const auto duration = std::chrono::duration<double, std::milli>(now - tp.value).count();
6061

61-
if (note_.empty()) {
62-
note_ += std::format(L" Marked: {:.3f}ms", duration);
63-
}
64-
else {
65-
note_ += std::format(L"\n Marked: {:.3f}ms", duration);
62+
if (note_.empty()) {
63+
note_ += std::format(L" Marked: {:.3f}ms", duration);
64+
}
65+
else {
66+
note_ += std::format(L"\n Marked: {:.3f}ms", duration);
67+
}
6668
}
69+
catch (...) { pmlog_panic_(L"Failed to mark time in EntryBuilder"); }
6770
return *this;
6871
}
6972

@@ -153,32 +156,37 @@ namespace pmon::util::log
153156
}
154157
EntryBuilder::~EntryBuilder()
155158
{
156-
if (pDest_) {
157-
auto tracing = captureTrace_.value_or((int)level_ <= (int)GlobalPolicy::Get().GetTraceLevel());
158-
// do line override check
159-
if (LineTable::GetTraceOverride()) {
160-
if (auto pEntry = LineTable::TryLookup(GetSourceFileName(), sourceLine_)) {
161-
switch (pEntry->traceOverride_) {
162-
case LineTable::TraceOverride::ForceOn: tracing = true; break;
163-
case LineTable::TraceOverride::ForceOff: tracing = false; break;
159+
try {
160+
if (pDest_) {
161+
auto tracing = captureTrace_.value_or((int)level_ <= (int)GlobalPolicy::Get().GetTraceLevel());
162+
// do line override check
163+
if (LineTable::GetTraceOverride()) {
164+
if (auto pEntry = LineTable::TryLookup(GetSourceFileName(), sourceLine_)) {
165+
switch (pEntry->traceOverride_) {
166+
case LineTable::TraceOverride::ForceOn: tracing = true; break;
167+
case LineTable::TraceOverride::ForceOff: tracing = false; break;
168+
}
164169
}
165170
}
166-
}
167-
if (tracing) {
168-
try {
169-
pTrace_ = StackTrace::Here(traceSkipDepth_);
170-
if (GlobalPolicy::Get().GetResolveTraceInClientThread()) {
171-
pTrace_->Resolve();
171+
if (tracing) {
172+
try {
173+
pTrace_ = StackTrace::Here(traceSkipDepth_);
174+
if (GlobalPolicy::Get().GetResolveTraceInClientThread()) {
175+
pTrace_->Resolve();
176+
}
177+
}
178+
catch (...) {
179+
pmlog_panic_(L"Failed to get current stacktrace");
172180
}
173181
}
174-
catch (...) {
175-
pmlog_panic_(L"Failed to get current stacktrace");
176-
}
182+
pDest_->Submit(std::move(*this));
183+
}
184+
else {
185+
pmlog_panic_(L"Log entry completed with no destination channel set");
177186
}
178-
pDest_->Submit(std::move(*this));
179187
}
180-
else {
181-
pmlog_panic_(L"Log entry completed with no destination channel set");
188+
catch (...) {
189+
pmlog_panic_(L"Error when completing log entry");
182190
}
183191
}
184192
}

IntelPresentMon/CommonUtilities/log/EntryBuilder.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <format>
44
#include <memory>
55
#include "TimePoint.h"
6+
#include "PanicLogger.h"
67

78
namespace pmon::util::log
89
{
@@ -21,12 +22,15 @@ namespace pmon::util::log
2122
template<typename T>
2223
EntryBuilder& watch(const wchar_t* symbol, const T& value) noexcept
2324
{
24-
if (note_.empty()) {
25-
note_ += std::format(L" {} => {}", symbol, value);
26-
}
27-
else {
28-
note_ += std::format(L"\n {} => {}", symbol, value);
25+
try {
26+
if (note_.empty()) {
27+
note_ += std::format(L" {} => {}", symbol, value);
28+
}
29+
else {
30+
note_ += std::format(L"\n {} => {}", symbol, value);
31+
}
2932
}
33+
catch (...) { pmlog_panic_(L"Failed to format watch in EntryBuilder"); }
3034
return *this;
3135
}
3236
EntryBuilder& mark(const TimePoint& tp) noexcept;

IntelPresentMon/CommonUtilities/log/IdentificationTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ namespace pmon::util::log
122122
}
123123
return {};
124124
}
125-
IdentificationTable::Bulk IdentificationTable::GetBulk_() const noexcept
125+
IdentificationTable::Bulk IdentificationTable::GetBulk_() const
126126
{
127127
std::shared_lock lk{ mtx_ };
128128
return Bulk{

IntelPresentMon/CommonUtilities/log/IdentificationTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace pmon::util::log
5454
void AddProcess_(uint32_t pid, const std::wstring& name);
5555
std::optional<Thread> LookupThread_(uint32_t tid) const;
5656
std::optional<Process> LookupProcess_(uint32_t pid) const;
57-
Bulk GetBulk_() const noexcept;
57+
Bulk GetBulk_() const;
5858
void RegisterSink_(std::shared_ptr<IIdentificationSink> pSink);
5959
private:
6060
// functions

IntelPresentMon/CommonUtilities/log/NamedPipeMarshallReceiver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace pmon::util::log
3131

3232
NamedPipeMarshallReceiver::~NamedPipeMarshallReceiver()
3333
{
34-
SignalExit();
34+
pmquell(SignalExit());
3535
}
3636

3737
std::optional<Entry> NamedPipeMarshallReceiver::Pop()

IntelPresentMon/CommonUtilities/win/Event.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ namespace pmon::util::win
4747
return eventIndex;
4848
}
4949
}
50-
else if (status == WAIT_TIMEOUT) {
50+
if (status == WAIT_TIMEOUT) {
5151
return {};
5252
}
53-
else {
54-
// failed, bailed
55-
throw std::runtime_error{ "Failed waiting on multiple objects" };
56-
}
53+
// failed, bailed
54+
throw std::runtime_error{ "Failed waiting on multiple objects" };
5755
}
5856
}

0 commit comments

Comments
 (0)