Simplify dllresolver caching & actually use it 49/207349/3
authorTomasz Swierczek <t.swierczek@samsung.com>
Mon, 3 Jun 2019 11:56:25 +0000 (13:56 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Wed, 12 Jun 2019 08:01:49 +0000 (10:01 +0200)
Change-Id: I8e6f0e18984fc8106ac48f35178e6550b91c74d8

dcm-daemon/dcmsession.cpp
dcm-daemon/soresolver.cpp
dcm-daemon/soresolver.h

index 0c13b97..c464dab 100644 (file)
@@ -317,33 +317,21 @@ void dcm_session::handle_context_association(const AssociateKeyContext& message)
 
                bool loaded = fSoResolver->ensure_loaded();
                if (loaded) {
-                       //auto dcm_backend_context_deleter = [&resolver](dcm_backend_context *dcmBackendContext) {
-                       //      resolver.invoke<void, dcm_backend_context&>(
-                       //              nullptr,
-                       //              "dcm_backend_free_key_context",
-                       //              *dcmBackendContext
-                       //      );
-                       //};
-                       //fBackendContext = std::unique_ptr<dcm_backend_context, decltype(dcm_backend_context_deleter)>
-                       //              (new dcm_backend_context, dcm_backend_context_deleter);
                        fBackendContext = std::unique_ptr<dcm_backend_context>(new dcm_backend_context);
 
                        fSoResolver->invoke<void, dcm_backend_context&, const std::string&>(
-                               nullptr,
                                "dcm_backend_create_key_context",
                                *fBackendContext,
                                message.key_type()
                        );
 
                        CryptoKeyType crypto_key_type = fSoResolver->invoke<CryptoKeyType, dcm_backend_context&>(
-                               nullptr,
                                "dcm_backend_key_type",
                                *fBackendContext
                        );
                        contextResponse->set_key_type(crypto_key_type);
 
                        unsigned int crypto_key_length = fSoResolver->invoke<unsigned int, dcm_backend_context&>(
-                               nullptr,
                                "dcm_backend_key_length",
                                *fBackendContext
                        );
@@ -394,7 +382,6 @@ void dcm_session::handle_cert_chain(const RequestCertificateChain& message)
        bool loaded = fSoResolver->ensure_loaded();
        if (loaded) {
                error = fSoResolver->invoke<int, dcm_backend_context&, std::string&>(
-                       nullptr,
                        "dcm_backend_request_certificate_chain",
                        *fBackendContext,
                        cert_chain
@@ -484,7 +471,6 @@ void dcm_session::handle_sign_request(const SignRequest& message)
        bool loaded = fSoResolver->ensure_loaded();
        if (loaded) {
                error = fSoResolver->invoke<int, dcm_backend_context&, MessageDigestType, const std::string&, std::string&>(
-                       nullptr,
                        "dcm_backend_sign_crypto_data",
                        *fBackendContext,
                        message.digest_type(),
index 9775df8..30e0b62 100644 (file)
@@ -37,32 +37,27 @@ so_resolver::~so_resolver()
        }
 }
 
-void * so_resolver::resolve_function(const int * key_ptr, const char * name) noexcept
+void * so_resolver::resolve_function(const std::string& name) noexcept
 {
        BOOST_LOG_FUNCTION();
-       
+
        std::unique_lock<std::mutex> locker(fCacheLock);
-       
-       if(key_ptr) {
-               auto it = fCache.find(key_ptr);
-               
-               if(it != fCache.end())
-                       return it->second;
-       }
-       
+       auto it = fCache.find(name);
+
+       if(it != fCache.end())
+               return it->second;
+
        void * handle = fLibraryHandle.load(std::memory_order_relaxed);
 
        if(handle) {
                BOOST_LOG_SEV(dcm_logger::get(), log_severity::debug) << "Resolving symbol " << name << " from " << fLibraryName;
-               void * sym = dlsym(handle, name);
+               void * sym = dlsym(handle, name.c_str());
                if(!sym) {
                        BOOST_LOG_SEV(dcm_logger::get(), log_severity::error) <<  "Unable to resolve symbol " << name << " from " <<
                                        fLibraryName << ": Error is " << dlerror();
                } else {
                        try {
-                               if(key_ptr) {
-                                       fCache.emplace(key_ptr, sym);
-                               }
+                               fCache.emplace(name, sym);
                        } catch(...) {
                        }
                }
@@ -77,9 +72,9 @@ void * so_resolver::resolve_function(const int * key_ptr, const char * name) noe
 bool so_resolver::ensure_loaded() noexcept
 {
        BOOST_LOG_FUNCTION();
-       
+
        std::unique_lock<std::mutex> locker(fCacheLock);
-       
+
        void * handle = fLibraryHandle.load(std::memory_order_acquire);
 
        if(handle)
index 49b2443..800e624 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdexcept>
 #include <map>
 #include <mutex>
+#include <string>
 
 class so_resolver : public boost::noncopyable {
 public:
@@ -34,11 +35,11 @@ public:
        ~so_resolver();
 
        bool ensure_loaded() noexcept;
-       void * resolve_function(const int * key_ptr, const char * name) noexcept;
+       void * resolve_function(const std::string& name) noexcept;
 
-       template<typename ReturnValue, typename... Args> ReturnValue invoke(const int * __key_ptr, const char * name, Args... args) {
+       template<typename ReturnValue, typename... Args> ReturnValue invoke(const std::string&  name, Args... args) {
                typedef ReturnValue (* function_t)(Args...);
-               function_t func = (function_t)resolve_function(__key_ptr, name);
+               function_t func = (function_t)resolve_function(name);
                if(!func) {
                        throw std::runtime_error("Trying to call unresolved function");
                }
@@ -49,7 +50,7 @@ private:
        std::string                                             fLibraryName;
        std::atomic<void *>                     fLibraryHandle;
        std::mutex                                              fCacheLock;
-       std::map<const int *, void *>   fCache;
+       std::map<std::string, void *>   fCache;
 };
 
 #endif /* DCM_DAEMON_SORESOLVER_H_ */