boostorg / dll

Library for comfortable work with DLL and DSO
https://boost.org/libs/dll
52 stars 43 forks source link

import_alias supplies wrong type to shared_library::get producing invalid function pointer. #73

Closed nkolotov closed 1 month ago

nkolotov commented 1 month ago

Assuming that we have a dll exporting void foo (void), an attempt to import and call it using import_alias may crash with access violation or cause other problems. This basic example similar to the one present at import_alias comment crashes:

#include <boost/dll/import.hpp>

int main()
{
    ::boost::dll::import_alias<void (void)>("TestDll.dll", "foo")();
}

import_alias supplies void * (void) as type of shared_library::get

template <class T>
BOOST_DLL_IMPORT_RESULT_TYPE import_alias(const boost::dll::fs::path& lib, const char* name,
    load_mode::type mode = load_mode::default_mode)
{
    typedef typename boost::dll::detail::import_type<T>::base_type type;

    boost::shared_ptr<boost::dll::shared_library> p = boost::make_shared<boost::dll::shared_library>(lib, mode);
    return type(p, p->get<T*>(name));
}

which causes it to invoke aggressive_ptr_cast<void (**)(void)>(get_void(symbol_name)) incorrectly casting symbol pointer.

template <typename T>
    inline typename boost::disable_if_c<boost::is_member_pointer<T>::value || boost::is_reference<T>::value, T&>::type get(const char* symbol_name) const {
        return *boost::dll::detail::aggressive_ptr_cast<T*>(
            get_void(symbol_name)
        );
    }

related SO question