aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-10-11 17:02:38 -0400
committerJack Lloyd <[email protected]>2017-10-11 17:02:38 -0400
commite831b200214ec611c3bd98f3a2df78e04dfc3536 (patch)
treedaafa5c8171008b7556a59027229251248f3260a
parentfb3076f04336418d2daa5d278f2895d0174dedb9 (diff)
Simplify ffi call overhead
Notable reductions in code size, stack size and function call overhead.
-rw-r--r--src/lib/ffi/ffi.h1
-rw-r--r--src/lib/ffi/ffi_kdf.cpp5
-rw-r--r--src/lib/ffi/ffi_mp.cpp3
-rw-r--r--src/lib/ffi/ffi_rng.cpp3
-rw-r--r--src/lib/ffi/ffi_util.h55
5 files changed, 22 insertions, 45 deletions
diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h
index 43bb9b61a..1a6014f71 100644
--- a/src/lib/ffi/ffi.h
+++ b/src/lib/ffi/ffi.h
@@ -147,6 +147,7 @@ doesn't exactly work well either!
#define BOTAN_FFI_ERROR_NULL_POINTER (-31)
#define BOTAN_FFI_ERROR_BAD_PARAMETER (-32)
#define BOTAN_FFI_ERROR_NOT_IMPLEMENTED (-40)
+#define BOTAN_FFI_ERROR_INVALID_OBJECT (-50)
#define BOTAN_FFI_ERROR_UNKNOWN_ERROR (-100)
diff --git a/src/lib/ffi/ffi_kdf.cpp b/src/lib/ffi/ffi_kdf.cpp
index 7aaf2dbe8..cff76237f 100644
--- a/src/lib/ffi/ffi_kdf.cpp
+++ b/src/lib/ffi/ffi_kdf.cpp
@@ -65,9 +65,8 @@ int botan_bcrypt_generate(uint8_t* out, size_t* out_len,
{
#if defined(BOTAN_HAS_BCRYPT)
return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() {
- BOTAN_ASSERT_ARG_NON_NULL(out);
- BOTAN_ASSERT_ARG_NON_NULL(out_len);
- BOTAN_ASSERT_ARG_NON_NULL(pass);
+ if(out == nullptr || out_len == nullptr || pass == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
if(flags != 0)
return BOTAN_FFI_ERROR_BAD_FLAG;
diff --git a/src/lib/ffi/ffi_mp.cpp b/src/lib/ffi/ffi_mp.cpp
index 0b55c1d69..87e455ccd 100644
--- a/src/lib/ffi/ffi_mp.cpp
+++ b/src/lib/ffi/ffi_mp.cpp
@@ -20,7 +20,8 @@ using namespace Botan_FFI;
int botan_mp_init(botan_mp_t* mp_out)
{
return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() {
- BOTAN_ASSERT_ARG_NON_NULL(mp_out);
+ if(mp_out == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
*mp_out = new botan_mp_struct(new Botan::BigInt);
return BOTAN_FFI_SUCCESS;
diff --git a/src/lib/ffi/ffi_rng.cpp b/src/lib/ffi/ffi_rng.cpp
index 68b4aaf64..88c522dfd 100644
--- a/src/lib/ffi/ffi_rng.cpp
+++ b/src/lib/ffi/ffi_rng.cpp
@@ -17,7 +17,8 @@ using namespace Botan_FFI;
int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
{
return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() {
- BOTAN_ASSERT_ARG_NON_NULL(rng_out);
+ if(rng_out == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
const std::string rng_type_s(rng_type ? rng_type : "system");
diff --git a/src/lib/ffi/ffi_util.h b/src/lib/ffi/ffi_util.h
index b52c319fe..e49002e62 100644
--- a/src/lib/ffi/ffi_util.h
+++ b/src/lib/ffi/ffi_util.h
@@ -15,9 +15,6 @@
namespace Botan_FFI {
-#define BOTAN_ASSERT_ARG_NON_NULL(p) \
- do { if(!p) throw Botan::Invalid_Argument("Argument " #p " is null"); } while(0)
-
class FFI_Error final : public Botan::Exception
{
public:
@@ -33,11 +30,8 @@ struct botan_struct
bool magic_ok() const { return (m_magic == MAGIC); }
- T* get() const
+ T* unsafe_get() const
{
- if(magic_ok() == false)
- throw FFI_Error("Bad magic " + std::to_string(m_magic) +
- " in ffi object expected " + std::to_string(MAGIC));
return m_obj.get();
}
private:
@@ -56,19 +50,14 @@ T& safe_get(botan_struct<T,M>* p)
{
if(!p)
throw FFI_Error("Null pointer argument");
- if(T* t = p->get())
- return *t;
- throw FFI_Error("Invalid object pointer");
- }
+ if(p->magic_ok() == false)
+ throw FFI_Error("Bad magic in ffi object");
-template<typename T, uint32_t M>
-const T& safe_get(const botan_struct<T,M>* p)
- {
- if(!p)
- throw FFI_Error("Null pointer argument");
- if(const T* t = p->get())
+ T* t = p->unsafe_get();
+ if(t)
return *t;
- throw FFI_Error("Invalid object pointer");
+ else
+ throw FFI_Error("Invalid object pointer");
}
template<typename Thunk>
@@ -78,7 +67,7 @@ int ffi_guard_thunk(const char* func_name, Thunk thunk)
{
return thunk();
}
- catch(std::bad_alloc)
+ catch(std::bad_alloc&)
{
return ffi_error_exception_thrown(func_name, "bad_alloc");
}
@@ -97,27 +86,13 @@ int ffi_guard_thunk(const char* func_name, Thunk thunk)
template<typename T, uint32_t M, typename F>
int apply_fn(botan_struct<T, M>* o, const char* func_name, F func)
{
- try
- {
- if(!o)
- throw FFI_Error("Null object to " + std::string(func_name));
- if(T* t = o->get())
- return func(*t);
- }
- catch(std::bad_alloc)
- {
- return ffi_error_exception_thrown(func_name, "bad_alloc");
- }
- catch(std::exception& e)
- {
- return ffi_error_exception_thrown(func_name, e.what());
- }
- catch(...)
- {
- return ffi_error_exception_thrown(func_name, "unknown exception");
- }
+ if(!o)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
- return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
+ if(o->magic_ok() == false)
+ return BOTAN_FFI_ERROR_INVALID_OBJECT;
+
+ return ffi_guard_thunk(func_name, [&]() { return func(*o->unsafe_get()); });
}
#define BOTAN_FFI_DO(T, obj, param, block) \
@@ -133,7 +108,7 @@ int ffi_delete_object(botan_struct<T, M>* obj, const char* func_name)
return BOTAN_FFI_SUCCESS; // ignore delete of null objects
if(obj->magic_ok() == false)
- return BOTAN_FFI_ERROR_INVALID_INPUT;
+ return BOTAN_FFI_ERROR_INVALID_OBJECT;
delete obj;
return BOTAN_FFI_SUCCESS;