aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cxx/include/aesnixx/error.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'cxx/include/aesnixx/error.hpp')
-rw-r--r--cxx/include/aesnixx/error.hpp61
1 files changed, 55 insertions, 6 deletions
diff --git a/cxx/include/aesnixx/error.hpp b/cxx/include/aesnixx/error.hpp
index 4c1fc98..013e2ef 100644
--- a/cxx/include/aesnixx/error.hpp
+++ b/cxx/include/aesnixx/error.hpp
@@ -8,16 +8,70 @@
#pragma once
+#include "debug.hpp"
+
#include <aesni/all.h>
#include <cstdlib>
+#include <cstring>
+#include <algorithm>
+#include <functional>
+#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
namespace aesni
{
+ class Error : public std::runtime_error
+ {
+ public:
+ Error(const AesNI_ErrorDetails& err_details)
+ : std::runtime_error(format_error_message(err_details))
+ {
+ copy_call_stack(err_details);
+ }
+
+ void for_each_in_call_stack(const std::function<void (void*, const std::string&)>& callback) const
+ {
+ aux::CallStackFormatter formatter;
+ std::for_each(m_call_stack, m_call_stack + m_call_stack_size, [&formatter, &callback] (void* addr)
+ {
+ callback(addr, formatter.format(addr));
+ });
+ }
+
+ private:
+ static std::string format_error_message(const AesNI_ErrorDetails& err_details)
+ {
+ std::vector<char> buf;
+ buf.resize(aesni_format_error(&err_details, NULL, 0));
+ aesni_format_error(&err_details, buf.data(), buf.size());
+ return { buf.begin(), buf.end() };
+ }
+
+ void copy_call_stack(const AesNI_ErrorDetails& err_details)
+ {
+ m_call_stack_size = err_details.call_stack_size;
+ std::memcpy(m_call_stack, err_details.call_stack, m_call_stack_size * sizeof(void*));
+ }
+
+ void* m_call_stack[AESNI_MAX_CALL_STACK_LENGTH];
+ std::size_t m_call_stack_size;
+ };
+
+ std::ostream& operator<<(std::ostream& os, const Error& e)
+ {
+ os << "AesNI error: " << e.what() << '\n';
+ os << "Call stack:\n";
+ e.for_each_in_call_stack([&os] (void* addr, const std::string& name)
+ {
+ os << '\t' << addr << ' ' << name << '\n';
+ });
+ return os;
+ }
+
class ErrorDetailsThrowsInDestructor
{
public:
@@ -29,12 +83,7 @@ namespace aesni
~ErrorDetailsThrowsInDestructor()
{
if (aesni_is_error(aesni_get_error_code(get())))
- {
- std::vector<char> msg;
- msg.resize(aesni_format_error(get(), NULL, 0));
- aesni_format_error(get(), msg.data(), msg.size());
- throw std::runtime_error(std::string(msg.begin(), msg.end()));
- }
+ throw Error(m_impl);
}
AesNI_ErrorDetails* get() { return &m_impl; }