forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vkerrormonitor.cpp
177 lines (152 loc) · 5.55 KB
/
vkerrormonitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2015-2022 The Khronos Group Inc.
* Copyright (c) 2015-2023 Valve Corporation
* Copyright (c) 2015-2023 LunarG, Inc.
*
* 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 "vkerrormonitor.h"
ErrorMonitor::ErrorMonitor() {
MonitorReset();
ExpectSuccess(kErrorBit);
}
void ErrorMonitor::MonitorReset() {
message_flags_ = kErrorBit;
bailout_ = nullptr;
message_found_ = false;
failure_message_strings_.clear();
desired_message_strings_.clear();
ignore_message_strings_.clear();
allowed_message_strings_.clear();
}
void ErrorMonitor::Reset() {
auto guard = Lock();
MonitorReset();
}
void ErrorMonitor::SetDesiredFailureMsg(const VkFlags msgFlags, const std::string &msg) {
SetDesiredFailureMsg(msgFlags, msg.c_str());
}
void ErrorMonitor::SetDesiredFailureMsg(const VkFlags msgFlags, const char *const msgString) {
if (NeedCheckSuccess()) {
VerifyNotFound();
}
auto guard = Lock();
desired_message_strings_.insert(msgString);
message_flags_ |= msgFlags;
}
void ErrorMonitor::SetAllowedFailureMsg(const char *const msg) {
auto guard = Lock();
allowed_message_strings_.emplace_back(msg);
}
void ErrorMonitor::SetUnexpectedError(const char *const msg) {
if (NeedCheckSuccess()) {
VerifyNotFound();
}
auto guard = Lock();
ignore_message_strings_.emplace_back(msg);
}
VkBool32 ErrorMonitor::CheckForDesiredMsg(const char *const msgString) {
VkBool32 result = VK_FALSE;
auto guard = Lock();
if (bailout_ != nullptr) {
*bailout_ = true;
}
std::string error_string(msgString);
bool found_expected = false;
if (!IgnoreMessage(error_string)) {
for (auto desired_msg_it = desired_message_strings_.begin(); desired_msg_it != desired_message_strings_.end();
++desired_msg_it) {
if (error_string.find(*desired_msg_it) != std::string::npos) {
found_expected = true;
failure_message_strings_.insert(error_string);
message_found_ = true;
result = VK_TRUE;
// Remove a maximum of one failure message from the set
// Multiset mutation is acceptable because `break` causes flow of control to exit the for loop
desired_message_strings_.erase(desired_msg_it);
break;
}
}
if (!found_expected && allowed_message_strings_.size()) {
for (auto allowed_msg_it = allowed_message_strings_.begin(); allowed_msg_it != allowed_message_strings_.end();
++allowed_msg_it) {
if (error_string.find(*allowed_msg_it) != std::string::npos) {
found_expected = true;
break;
}
}
}
if (!found_expected) {
result = VK_TRUE;
ADD_FAILURE() << error_string;
}
}
return result;
}
VkDebugReportFlagsEXT ErrorMonitor::GetMessageFlags() { return message_flags_; }
bool ErrorMonitor::AnyDesiredMsgFound() const { return message_found_; }
void ErrorMonitor::SetError(const char *const errorString) {
auto guard = Lock();
message_found_ = true;
failure_message_strings_.insert(errorString);
}
void ErrorMonitor::SetBailout(std::atomic<bool> *bailout) {
auto guard = Lock();
bailout_ = bailout;
}
void ErrorMonitor::ExpectSuccess(VkDebugReportFlagsEXT const message_flag_mask) {
// Match ANY message matching specified type
auto guard = Lock();
desired_message_strings_.clear();
message_flags_ = message_flag_mask;
}
bool ErrorMonitor::ExpectingSuccess() const {
return (desired_message_strings_.size() == 1) && (desired_message_strings_.count("") == 1 && ignore_message_strings_.empty());
}
bool ErrorMonitor::NeedCheckSuccess() const { return ExpectingSuccess(); }
void ErrorMonitor::VerifyFound() {
{
// The lock must be released before the ExpectSuccess call at the end
auto guard = Lock();
// Not receiving expected message(s) is a failure.
if (!desired_message_strings_.empty()) {
for (const auto &desired_msg : desired_message_strings_) {
ADD_FAILURE() << "Did not receive expected error '" << desired_msg << "'";
}
}
MonitorReset();
}
ExpectSuccess();
}
void ErrorMonitor::Finish() {
VerifyNotFound();
Reset();
}
void ErrorMonitor::VerifyNotFound() {
auto guard = Lock();
// ExpectSuccess() configured us to match anything. Any error is a failure.
if (AnyDesiredMsgFound()) {
for (const auto &msg : failure_message_strings_) {
ADD_FAILURE() << "Expected to succeed but got error: " << msg;
}
}
MonitorReset();
}
bool ErrorMonitor::IgnoreMessage(std::string const &msg) const {
if (ignore_message_strings_.empty()) {
return false;
}
return std::find_if(ignore_message_strings_.begin(), ignore_message_strings_.end(), [&msg](std::string const &str) {
return msg.find(str) != std::string::npos;
}) != ignore_message_strings_.end();
}