1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015-2019 Antony Polukhin.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org
10 #include "../example/b2_workarounds.hpp"
11 #include <boost/dll.hpp>
12 #include <boost/dll/runtime_symbol_info.hpp>
13 #include <boost/filesystem/operations.hpp>
15 #include <boost/core/lightweight_test.hpp>
17 #include <boost/predef/os.h>
21 #include <exception> // std::set_terminate
22 #include <signal.h> // ::signal
24 // Makes global error variables dirty. Useful for preventing issues like https://github.com/boostorg/dll/issues/16
25 void make_error_code_dirty() {
30 boost::winapi::WCHAR_ path_hldr
[10];
31 int some_invalid_value_for_handle
= 0xFF004242;
32 boost::winapi::HMODULE_ some_invalid_handle
;
33 memcpy(&some_invalid_handle
, &some_invalid_value_for_handle
, sizeof(some_invalid_value_for_handle
));
34 boost::winapi::GetModuleFileNameW(some_invalid_handle
, path_hldr
, 10);
42 typedef float (lib_version_func
)();
43 typedef void (say_hello_func
) ();
44 typedef int (increment
) (int);
47 extern "C" int BOOST_SYMBOL_EXPORT
exef() {
52 extern "C" void BOOST_SYMBOL_EXPORT
my_terminate_handler() {
56 extern "C" void BOOST_SYMBOL_EXPORT
my_signal_handler(int) {
60 void internal_function() {}
61 int internal_variable
= 1;
64 int main(int argc
, char* argv
[]) {
65 using namespace boost::dll
;
67 boost::dll::fs::path shared_library_path
= b2_workarounds::first_lib_from_argv(argc
, argv
);
68 BOOST_TEST(shared_library_path
.string().find("test_library") != std::string::npos
);
70 make_error_code_dirty();
72 shared_library
lib(shared_library_path
);
74 std::cout
<< std::endl
;
75 std::cout
<< "shared_library: " << shared_library_path
<< std::endl
;
76 std::cout
<< "symbol_location: " << symbol_location(lib
.get
<int>("integer_g")) << std::endl
;
77 std::cout
<< "lib.location(): " << lib
.location() << std::endl
;
79 symbol_location(lib
.get
<int>("integer_g")) == lib
.location()
82 make_error_code_dirty();
85 symbol_location(lib
.get
<say_hello_func
>("say_hello")) == lib
.location()
89 symbol_location(lib
.get
<lib_version_func
>("lib_version")) == lib
.location()
92 make_error_code_dirty();
95 symbol_location(lib
.get
<const int>("const_integer_g")) == lib
.location()
98 // Checking that symbols are still available, after another load+unload of the library
99 { shared_library
sl2(shared_library_path
); }
102 symbol_location(lib
.get
<int>("integer_g")) == lib
.location()
106 make_error_code_dirty();
110 symbol_location(lib
.get
<std::size_t(*)(const std::vector
<int>&)>("foo_bar")) == lib
.location()
113 symbol_location(lib
.get_alias
<std::size_t(const std::vector
<int>&)>("foo_bar")) == lib
.location()
118 symbol_location(lib
.get
<std::size_t*>("foo_variable")) == lib
.location()
121 symbol_location(lib
.get_alias
<std::size_t>("foo_variable")) == lib
.location()
126 make_error_code_dirty();
128 shared_library
sl(program_location());
130 make_error_code_dirty();
133 (boost::dll::fs::equivalent(symbol_location(sl
.get
<int(void)>("exef")), argv
[0]))
137 { // self with error_code
138 boost::dll::fs::error_code ec
;
139 shared_library
sl(program_location(ec
));
143 (boost::dll::fs::equivalent(symbol_location(sl
.get
<int(void)>("exef"), ec
), argv
[0]))
147 symbol_location(&sl
.get
<int(void)>("exef"), ec
);
151 std::cout
<< "\ninternal_function: " << symbol_location(internal_function
);
152 std::cout
<< "\nargv[0] : " << boost::filesystem::absolute(argv
[0]);
154 (boost::dll::fs::equivalent(symbol_location(internal_function
), argv
[0]))
158 (boost::dll::fs::equivalent(symbol_location(internal_variable
), argv
[0]))
161 make_error_code_dirty();
164 (boost::dll::fs::equivalent(this_line_location(), argv
[0]))
167 { // this_line_location with error_code
168 boost::dll::fs::error_code ec
;
169 make_error_code_dirty();
171 (boost::dll::fs::equivalent(this_line_location(ec
), argv
[0]))
177 lib
.get_alias
<boost::dll::fs::path()>("module_location_from_itself")() == lib
.location()
180 // Checking docs content
181 std::cout
<< "\nsymbol_location(std::cerr); // " << symbol_location(std::cerr
);
182 std::cout
<< "\nsymbol_location(std::puts); // " << symbol_location(std::puts
);
184 std::set_terminate(&my_terminate_handler
);
185 BOOST_TEST((boost::dll::fs::equivalent(
186 symbol_location_ptr(std::set_terminate(0)),
191 boost::dll::fs::error_code ec
;
192 boost::dll::fs::path p
= symbol_location_ptr(std::set_terminate(0), ec
);
193 BOOST_TEST(ec
|| !p
.empty());
197 boost::dll::fs::error_code ec
;
198 symbol_location(std::set_terminate(0), ec
),
203 std::set_terminate(&my_terminate_handler
);
204 boost::dll::fs::error_code ec
;
205 make_error_code_dirty();
206 symbol_location(std::set_terminate(0), ec
),
211 boost::dll::fs::error_code ec
;
212 ::signal(SIGSEGV
, &my_signal_handler
);
213 boost::dll::fs::path p
= symbol_location_ptr(::signal(SIGSEGV
, SIG_DFL
), ec
);
214 BOOST_TEST((boost::dll::fs::equivalent(
221 ::signal(SIGSEGV
, &my_signal_handler
);
222 boost::dll::fs::error_code ec
;
223 make_error_code_dirty();
224 symbol_location(::signal(SIGSEGV
, SIG_DFL
), ec
);
229 return boost::report_errors();