exception.h
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #ifndef EXCEPTION_H
23 #define EXCEPTION_H
24 
25 #include <string>
26 #include <ostream>
27 #include <typeinfo>
28 #include <stdexcept>
29 
30 // *INDENT-OFF*
31 
32 namespace bfn
33 {
34 
35 // -----------------------------------------------------------------------------
36 // General Exception
37 // -----------------------------------------------------------------------------
38 
57 class general_exception : public std::exception
58 {
59 public:
60 
63  general_exception() noexcept = default;
64 
67  ~general_exception() override = default;
68 
76  const char *what() const noexcept override
77  { return typeid(*this).name(); }
78 
79 public:
80 
81  virtual std::ostream &print(std::ostream &os) const
82  { return os << "general exception"; }
83 };
84 
85 }
86 
105 inline std::ostream &
106 operator<<(std::ostream &os, const bfn::general_exception &ge)
107 { return ge.print(os); }
108 
109 namespace bfn
110 {
111 
112 // -----------------------------------------------------------------------------
113 // Unknown Command Error
114 // -----------------------------------------------------------------------------
115 
117 {
118 public:
120  m_mesg(std::move(mesg))
121  {}
122 
123  std::ostream &print(std::ostream &os) const override
124  { return os << "unknown command: `" << m_mesg << "`"; }
125 
126 private:
127  std::string m_mesg;
128 };
129 
130 #define unknown_command(a) bfn::unknown_command_error(a)
131 
132 // -----------------------------------------------------------------------------
133 // Unknown VMCall Type Error
134 // -----------------------------------------------------------------------------
135 
137 {
138 public:
140  m_mesg(std::move(mesg))
141  {}
142 
143  std::ostream &print(std::ostream &os) const override
144  { return os << "unknown vmcall type: `" << m_mesg << "`"; }
145 
146 private:
147  std::string m_mesg;
148 };
149 
150 #define unknown_vmcall_type(a) bfn::unknown_vmcall_type_error(a)
151 
152 // -----------------------------------------------------------------------------
153 // Unknown VMCall String Type Error
154 // -----------------------------------------------------------------------------
155 
157 {
158 public:
160  m_mesg(std::move(mesg))
161  {}
162 
163  std::ostream &print(std::ostream &os) const override
164  { return os << "unknown string type: `" << m_mesg << "`"; }
165 
166 private:
167  std::string m_mesg;
168 };
169 
170 #define unknown_vmcall_string_type(a) bfn::unknown_vmcall_string_type_error(a)
171 
172 // -----------------------------------------------------------------------------
173 // Unknown VMCall String Type Error
174 // -----------------------------------------------------------------------------
175 
177 {
178 public:
180  m_mesg(std::move(mesg))
181  {}
182 
183  std::ostream &print(std::ostream &os) const override
184  { return os << "unknown data type: `" << m_mesg << "`"; }
185 
186 private:
187  std::string m_mesg;
188 };
189 
190 #define unknown_vmcall_data_type(a) bfn::unknown_vmcall_data_type_error(a)
191 
192 // -----------------------------------------------------------------------------
193 // Missing Argument Error
194 // -----------------------------------------------------------------------------
195 
197 {
198 public:
199  std::ostream &print(std::ostream &os) const override
200  { return os << "missing argument"; }
201 };
202 
203 #define missing_argument() bfn::missing_argument_error()
204 
205 // -----------------------------------------------------------------------------
206 // Invalid Filename Error
207 // -----------------------------------------------------------------------------
208 
210 {
211 public:
213  m_mesg(std::move(mesg))
214  {}
215 
216  std::ostream &print(std::ostream &os) const override
217  { return os << "invalid filename: `" << m_mesg << "`"; }
218 
219 private:
220  std::string m_mesg;
221 };
222 
223 #define invalid_file(a) bfn::invalid_file_error(a)
224 
225 // -----------------------------------------------------------------------------
226 // Driver Inaccessible
227 // -----------------------------------------------------------------------------
228 
230 {
231 public:
232  std::ostream &print(std::ostream &os) const override
233  {
234  os << "bareflank driver inaccessible:";
235  os << std::endl << " - check that the bareflank driver is loaded";
236  os << std::endl << " - check that bfm was exectued with the "
237  << "proper permissions";
238  return os;
239  }
240 };
241 
242 #define driver_inaccessible(a) bfn::driver_inaccessible_error(a)
243 
244 // -----------------------------------------------------------------------------
245 // IOCTL Failed
246 // -----------------------------------------------------------------------------
247 
249 {
250 public:
252  m_ioctl(std::move(ioctl))
253  {}
254 
255  std::ostream &print(std::ostream &os) const override
256  { return os << "ioctl failed: `" << m_ioctl << "`"; }
257 
258 private:
259  std::string m_ioctl;
260 };
261 
262 #define ioctl_failed(a) bfn::ioctl_failed_error(#a)
263 
264 // -----------------------------------------------------------------------------
265 // Corrupt VMM
266 // -----------------------------------------------------------------------------
267 
269 {
270 public:
271  std::ostream &print(std::ostream &os) const override
272  { return os << "unable to process request. vmm is in a corrupt state"; }
273 };
274 
275 #define corrupt_vmm() bfn::corrupt_vmm_error()
276 
277 // -----------------------------------------------------------------------------
278 // Unknown VMM Status
279 // -----------------------------------------------------------------------------
280 
282 {
283 public:
284  std::ostream &print(std::ostream &os) const override
285  { return os << "unable to process request. vmm status unknown"; }
286 };
287 
288 #define unknown_status() bfn::unknown_status_error()
289 
290 // -----------------------------------------------------------------------------
291 // Invalid VMM Status
292 // -----------------------------------------------------------------------------
293 
295 {
296 public:
298  m_mesg(std::move(mesg))
299  {}
300 
301  std::ostream &print(std::ostream &os) const override
302  { return os << m_mesg; }
303 
304 private:
305  std::string m_mesg;
306 };
307 
308 #define invalid_vmm_state(a) bfn::invalid_vmm_state_error(a)
309 
310 }
311 
312 // *INDENT-ON*
313 
314 #endif
~general_exception() override=default
std::ostream & print(std::ostream &os) const override
Definition: exception.h:255
virtual std::ostream & print(std::ostream &os) const
Definition: exception.h:81
invalid_file_error(std::string mesg)
Definition: exception.h:212
std::ostream & print(std::ostream &os) const override
Definition: exception.h:232
std::ostream & print(std::ostream &os) const override
Definition: exception.h:123
unknown_command_error(std::string mesg)
Definition: exception.h:119
unknown_vmcall_type_error(std::string mesg)
Definition: exception.h:139
unknown_vmcall_string_type_error(std::string mesg)
Definition: exception.h:159
void uint64_t uint64_t uint64_t *rdx noexcept
ioctl_failed_error(std::string ioctl)
Definition: exception.h:251
unknown_vmcall_data_type_error(std::string mesg)
Definition: exception.h:179
std::ostream & operator<<(std::ostream &os, const bfn::general_exception &ge)
Definition: exception.h:106
std::ostream & print(std::ostream &os) const override
Definition: exception.h:271
std::ostream & print(std::ostream &os) const override
Definition: exception.h:143
std::ostream & print(std::ostream &os) const override
Definition: exception.h:183
const char * what() const noexcept override
Definition: exception.h:76
std::ostream & print(std::ostream &os) const override
Definition: exception.h:284
std::ostream & print(std::ostream &os) const override
Definition: exception.h:199
invalid_vmm_state_error(std::string mesg)
Definition: exception.h:297
std::ostream & print(std::ostream &os) const override
Definition: exception.h:216
general_exception() noexcept=default
Definition: ioctl.h:48
std::ostream & print(std::ostream &os) const override
Definition: exception.h:163
std::ostream & print(std::ostream &os) const override
Definition: exception.h:301