diff --git a/src/asherah.cc b/src/asherah.cc index ba10759..307b9ca 100644 --- a/src/asherah.cc +++ b/src/asherah.cc @@ -5,15 +5,13 @@ #include "logging.h" #include "napi_utils.h" #include "scoped_allocate.h" -#include -#include #include static volatile std::atomic setup_state{0}; class Asherah : public Napi::Addon { public: - Asherah(Napi::Env, Napi::Object exports) { + Asherah(Napi::Env env, Napi::Object exports) : logger(env, "asherah-node") { DefineAddon( exports, { @@ -26,7 +24,8 @@ class Asherah : public Napi::Addon { InstanceMethod("decrypt", &Asherah::DecryptSync), InstanceMethod("decrypt_async", &Asherah::DecryptAsync), InstanceMethod("decrypt_string", &Asherah::DecryptStringSync), - InstanceMethod("decrypt_string_async", &Asherah::DecryptStringAsync), + InstanceMethod("decrypt_string_async", + &Asherah::DecryptStringAsync), InstanceMethod("shutdown", &Asherah::ShutdownAsherahSync), InstanceMethod("shutdown_async", &Asherah::ShutdownAsherahAsync), InstanceMethod("set_max_stack_alloc_item_size", @@ -105,9 +104,6 @@ class Asherah : public Napi::Addon { Napi::Env env = info.Env(); Napi::HandleScope scope(env); try { - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); - } BeginShutdownAsherah(); Shutdown(); EndShutdownAsherah(); @@ -120,10 +116,6 @@ class Asherah : public Napi::Addon { Napi::Env env = info.Env(); Napi::HandleScope scope(env); try { - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); - } - BeginShutdownAsherah(); auto worker = new ShutdownAsherahWorker(env, this); worker->Queue(); @@ -180,8 +172,7 @@ class Asherah : public Napi::Addon { try { Napi::String partition_id_string; Napi::Value input_value; - BeginEncryptToJson(env, __func__, info, partition_id_string, - input_value); + BeginEncryptToJson(env, __func__, info, partition_id_string, input_value); CobhanBufferNapi partition_id(env, partition_id_string); CobhanBufferNapi input(env, input_value); @@ -269,14 +260,6 @@ class Asherah : public Napi::Addon { Napi::HandleScope scope(env); Napi::String output_string; try { - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); - } - - if (unlikely(setup_state.load(std::memory_order_relaxed) == 0)) { - logger.log_error_and_throw(__func__, "setup() not called"); - } - if (unlikely(info.Length() < 2)) { logger.log_error_and_throw(__func__, "Wrong number of arguments"); } @@ -308,14 +291,6 @@ class Asherah : public Napi::Addon { Napi::Env env = info.Env(); Napi::HandleScope scope(env); try { - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); - } - - if (unlikely(setup_state == 0)) { - logger.log_error_and_throw(__func__, "setup() not called"); - } - if (unlikely(info.Length() < 2)) { logger.log_error_and_throw(__func__, "Wrong number of arguments"); } @@ -334,8 +309,8 @@ class Asherah : public Napi::Addon { CobhanBufferNapi output(input.get_data_len_bytes()); - auto worker = - new DecryptFromJsonToStringWorker(env, this, partition_id, input, output); + auto worker = new DecryptFromJsonToStringWorker(env, this, partition_id, + input, output); worker->Queue(); return worker->Promise(); @@ -344,11 +319,7 @@ class Asherah : public Napi::Addon { } } - void SetMaxStackAllocItemSize(const Napi::CallbackInfo &info) { - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); - } - + void SetMaxStackAllocItemSize(const Napi::CallbackInfo &info) { if (unlikely(info.Length() < 1)) { logger.log_error_and_throw(__func__, "Wrong number of arguments"); } @@ -360,19 +331,33 @@ class Asherah : public Napi::Addon { } void SetSafetyPaddingOverhead(const Napi::CallbackInfo &info) { - - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); + if (unlikely(info.Length() < 1)) { + logger.log_error_and_throw(__func__, "Wrong number of arguments"); } + // Napi::Number safety_padding_number = info[0].ToNumber(); + // auto new_safety_padding_bytes = (size_t) + // safety_padding_number.Int32Value(); Safety padding size is now fixed - + // ignore the input set_safety_padding_bytes(new_safety_padding_bytes); + } + + Napi::Value + GetSetupStatus(const Napi::CallbackInfo + &info) { // NOLINT(*-convert-member-functions-to-static) + return Napi::Boolean::New(info.Env(), + setup_state.load(std::memory_order_acquire) != 0); + } + + void SetLogHook(const Napi::CallbackInfo &info) { if (unlikely(info.Length() < 1)) { logger.log_error_and_throw(__func__, "Wrong number of arguments"); } - //Napi::Number safety_padding_number = info[0].ToNumber(); - //auto new_safety_padding_bytes = (size_t) safety_padding_number.Int32Value(); - //Safety padding size is now fixed - ignore the input - //set_safety_padding_bytes(new_safety_padding_bytes); + if (unlikely(!info[0].IsFunction())) { + logger.log_error_and_throw(__func__, "Wrong argument type"); + } + + logger.set_log_hook(info[0].As()); } void BeginSetupAsherah(const Napi::Env &env, const char *func_name, @@ -504,20 +489,12 @@ class Asherah : public Napi::Addon { } void BeginShutdownAsherah() { - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); - } - if (unlikely(setup_state.load(std::memory_order_acquire) == 0)) { logger.log_error_and_throw(__func__, "setup() not called"); } } void EndShutdownAsherah() { - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); - } - auto old_setup_state = setup_state.exchange(0, std::memory_order_acq_rel); if (unlikely(old_setup_state == 0)) { logger.log_error_and_throw(__func__, "setup() not called"); @@ -576,9 +553,11 @@ class Asherah : public Napi::Addon { class DecryptFromJsonWorker : public AsherahAsyncWorker { public: DecryptFromJsonWorker(const Napi::Env &env, Asherah *instance, - CobhanBufferNapi &partition_id, CobhanBufferNapi &input, CobhanBufferNapi &output) + CobhanBufferNapi &partition_id, + CobhanBufferNapi &input, CobhanBufferNapi &output) : AsherahAsyncWorker(env, instance), - partition_id(std::move(partition_id)), input(std::move(input)), output(std::move(output)) {} + partition_id(std::move(partition_id)), input(std::move(input)), + output(std::move(output)) {} // extern GoInt32 DecryptFromJson(void* partitionIdPtr, void* jsonPtr, // void* dataPtr); @@ -613,8 +592,7 @@ class Asherah : public Napi::Addon { CobhanBufferNapi &partition_id, CobhanBufferNapi &input, CobhanBufferNapi &output) - : DecryptFromJsonWorker(env, instance, partition_id, input, - output) {} + : DecryptFromJsonWorker(env, instance, partition_id, input, output) {} Napi::Value OnOKTask(Napi::Env &env) override { Napi::String output_string; @@ -706,29 +684,6 @@ class Asherah : public Napi::Addon { return "Unknown error"; } } - - void SetLogHook(const Napi::CallbackInfo &info) { - if (unlikely(verbose_flag)) { - logger.debug_log(__func__, "called"); - } - - if (unlikely(info.Length() < 1)) { - logger.log_error_and_throw(__func__, "Wrong number of arguments"); - } - - if (unlikely(!info[0].IsFunction())) { - logger.log_error_and_throw(__func__, "Wrong argument type"); - } - - logger.set_log_hook(info[0].As()); - } - - Napi::Value - GetSetupStatus(const Napi::CallbackInfo - &info) { // NOLINT(*-convert-member-functions-to-static) - return Napi::Boolean::New(info.Env(), - setup_state.load(std::memory_order_acquire) != 0); - } }; NODE_API_NAMED_ADDON('asherah', Asherah) diff --git a/src/asherah_async_worker.h b/src/asherah_async_worker.h index 6d5f79f..255c684 100644 --- a/src/asherah_async_worker.h +++ b/src/asherah_async_worker.h @@ -2,7 +2,6 @@ #ifndef ASHERAH_ASYNC_WORKER_H #define ASHERAH_ASYNC_WORKER_H -#include #include #include diff --git a/src/cobhan_buffer.h b/src/cobhan_buffer.h index a0f6b3b..5dc456f 100644 --- a/src/cobhan_buffer.h +++ b/src/cobhan_buffer.h @@ -1,10 +1,11 @@ -#ifndef ASHERAH_NODE_COBHAN_BUFFER_H -#define ASHERAH_NODE_COBHAN_BUFFER_H +#ifndef COBHAN_BUFFER_H +#define COBHAN_BUFFER_H #include #include // for std::memcpy #include // for std::numeric_limits #include // for std::runtime_error, std::invalid_argument +#include // for std::cerr, std::terminate class CobhanBuffer { public: @@ -55,8 +56,8 @@ class CobhanBuffer { ~CobhanBuffer() { if (!verify_canaries()) { - std::cerr << "asherah-node: Memory corruption detected: Canary values " - "are corrupted." + std::cerr << "CobhanBuffer: Memory corruption detected: Canary values " + "are corrupted. Terminating process." << std::endl << std::flush; std::terminate(); @@ -229,4 +230,4 @@ class CobhanBuffer { static_cast(std::numeric_limits::max()); }; -#endif // ASHERAH_NODE_COBHAN_BUFFER_H +#endif // COBHAN_BUFFER_H diff --git a/src/logging.cc b/src/logging.cc index 253b8d2..b9c9796 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -1,8 +1,11 @@ #include "logging.h" -Logger::Logger() = default; +Logger::Logger(Napi::Env &env, std::string system_name) + : env(env), system_name(system_name), log_hook(Napi::FunctionReference()) {} -Logger::Logger(Napi::Function new_log_hook) { +Logger::Logger(Napi::Env &env, std::string system_name, + Napi::Function new_log_hook) + : env(env), system_name(system_name) { if (unlikely(new_log_hook.IsEmpty())) { throw std::runtime_error("new_log_hook cannot be nullptr"); } @@ -38,8 +41,8 @@ void Logger::debug_log(const char *function_name, const char *message) const { Napi::Function log_hook_function = log_hook.Value(); log_hook_function.Call( {Napi::Number::New(env, posix_log_level_debug), - Napi::String::New(env, asherah_node_prefix + function_name + ": " + - message)}); + Napi::String::New(env, + system_name + function_name + ": " + message)}); } } } @@ -55,8 +58,8 @@ void Logger::debug_log(const char *function_name, Napi::Function log_hook_function = log_hook.Value(); log_hook_function.Call( {Napi::Number::New(env, posix_log_level_debug), - Napi::String::New(env, asherah_node_prefix + function_name + ": " + - message)}); + Napi::String::New(env, + system_name + function_name + ": " + message)}); } } } @@ -72,7 +75,7 @@ void Logger::debug_log_alloca(const char *function_name, Napi::Function log_hook_function = log_hook.Value(); log_hook_function.Call( {Napi::Number::New(env, posix_log_level_debug), - Napi::String::New(env, asherah_node_prefix + function_name + + Napi::String::New(env, system_name + function_name + ": Calling alloca(" + std::to_string(length) + ") (stack) for " + variable_name)}); @@ -91,7 +94,7 @@ void Logger::debug_log_new(const char *function_name, const char *variable_name, Napi::Function log_hook_function = log_hook.Value(); log_hook_function.Call( {Napi::Number::New(env, posix_log_level_debug), - Napi::String::New(env, asherah_node_prefix + function_name + + Napi::String::New(env, system_name + function_name + ": Calling new[" + std::to_string(length) + "] (heap) for " + variable_name)}); @@ -127,8 +130,8 @@ void Logger::error_log(const char *function_name, const char *message) const { Napi::Function log_hook_function = log_hook.Value(); log_hook_function.Call( {Napi::Number::New(env, posix_log_level_error), - Napi::String::New(env, asherah_node_prefix + function_name + ": " + - message)}); + Napi::String::New(env, + system_name + function_name + ": " + message)}); } } } @@ -144,15 +147,14 @@ void Logger::error_log(const char *function_name, Napi::Function log_hook_function = log_hook.Value(); log_hook_function.Call( {Napi::Number::New(env, posix_log_level_error), - Napi::String::New(env, asherah_node_prefix + function_name + ": " + - message)}); + Napi::String::New(env, system_name + function_name + ": " + message)}); } } __attribute__((always_inline)) inline void Logger::stderr_debug_log(const char *function_name, const char *message) const { if (unlikely(verbose_flag)) { - std::cerr << "asherah-node: [DEBUG] " << function_name << ": " << message + std::cerr << system_name << ": [DEBUG] " << function_name << ": " << message << std::endl << std::flush; } @@ -162,7 +164,7 @@ __attribute__((always_inline)) inline void Logger::stderr_debug_log(const char *function_name, const std::string &message) const { if (unlikely(verbose_flag)) { - std::cerr << "asherah-node: [DEBUG] " << function_name << ": " << message + std::cerr << system_name << ": [DEBUG] " << function_name << ": " << message << std::endl << std::flush; } @@ -171,7 +173,7 @@ Logger::stderr_debug_log(const char *function_name, __attribute__((always_inline)) inline void Logger::stderr_debug_log_alloca( const char *function_name, const char *variable_name, size_t length) const { if (unlikely(verbose_flag)) { - std::cerr << "asherah-node: [DEBUG] " << function_name + std::cerr << system_name << ": [DEBUG] " << function_name << ": Calling alloca(" << length << ") (stack) for " << variable_name << std::endl << std::flush; @@ -182,15 +184,16 @@ __attribute__((always_inline)) inline void Logger::stderr_debug_log_new(const char *function_name, const char *variable_name, size_t length) const { if (unlikely(verbose_flag)) { - std::cerr << "asherah-node: [DEBUG] " << function_name << ": Calling new[" - << length << "] (heap) for " << variable_name << std::endl + std::cerr << system_name << ": [DEBUG] " << function_name + << ": Calling new[" << length << "] (heap) for " << variable_name + << std::endl << std::flush; } } __attribute__((always_inline)) inline void Logger::stderr_error_log(const char *function_name, const char *message) const { - std::cerr << "asherah-node: [ERROR] " << function_name << ": " << message + std::cerr << system_name << ": [ERROR] " << function_name << ": " << message << std::endl << std::flush; } @@ -198,7 +201,7 @@ Logger::stderr_error_log(const char *function_name, const char *message) const { __attribute__((always_inline)) inline void Logger::stderr_error_log(const char *function_name, const std::string &message) const { - std::cerr << "asherah-node: [ERROR] " << function_name << ": " << message + std::cerr << system_name << ": [ERROR] " << function_name << ": " << message << std::endl << std::flush; } @@ -206,15 +209,18 @@ Logger::stderr_error_log(const char *function_name, __attribute__((always_inline, noreturn)) inline void Logger::log_error_and_throw(const char *function_name, const std::string &error_msg) const { - std::string final_error_msg = function_name + (": " + error_msg); + std::string final_error_msg = + system_name + ": [EXCEPTION] " + function_name + (": " + error_msg); error_log(function_name, final_error_msg); // Unconditionally log errors to stderr? stderr_error_log(function_name, final_error_msg); if (likely(!log_hook.IsEmpty())) { - Napi::Error::New(log_hook.Env(), final_error_msg).ThrowAsJavaScriptException(); + Napi::Error::New(log_hook.Env(), final_error_msg) + .ThrowAsJavaScriptException(); } - // If log_hook is empty or the error was not thrown by NAPI, throw a C++ exception + // If log_hook is empty or the error was not thrown by NAPI, throw a C++ + // exception throw std::runtime_error(final_error_msg); } diff --git a/src/logging.h b/src/logging.h index aada116..21feb45 100644 --- a/src/logging.h +++ b/src/logging.h @@ -1,17 +1,17 @@ #ifndef LOGGING_H #define LOGGING_H #include "hints.h" -#include -#include -#include +#include // int32_t +#include // std::setw +#include // std::cerr #include -#include -#include +#include // std::ostringstream +#include // std::string class Logger { public: - Logger(); - explicit Logger(Napi::Function new_log_hook); + Logger(Napi::Env &env, std::string system_name); + explicit Logger(Napi::Env &env, std::string system_name, Napi::Function new_log_hook); ~Logger(); void set_log_hook(Napi::Function new_log_hook); @@ -59,9 +59,10 @@ class Logger { int32_t verbose_flag = 0; Napi::FunctionReference log_hook; - std::string asherah_node_prefix = "asherah-node: "; + Napi::Env env; + std::string system_name; const int posix_log_level_error = 3; const int posix_log_level_debug = 7; }; -#endif +#endif // LOGGING_H diff --git a/src/scoped_allocate.h b/src/scoped_allocate.h index c7aeed8..346b078 100644 --- a/src/scoped_allocate.h +++ b/src/scoped_allocate.h @@ -1,5 +1,5 @@ -#ifndef ASHERAH_NODE_SCOPED_ALLOCATE_H -#define ASHERAH_NODE_SCOPED_ALLOCATE_H +#ifndef SCOPED_ALLOCATE_H +#define SCOPED_ALLOCATE_H #include "logging.h" @@ -38,4 +38,4 @@ buffer##_unique_ptr, max_stack_alloc_size, \ function_name) -#endif // ASHERAH_NODE_SCOPED_ALLOCATE_H +#endif // SCOPED_ALLOCATE_H