Last active
February 2, 2018 16:36
-
-
Save rnburn/e45475ca234ee673380714f5c4e28cf6 to your computer and use it in GitHub Desktop.
dlopen - undefined behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
extern "C" { | |
int | |
OpenTracingMakeTracerFactory(const char* opentracing_version, | |
const void** error_category, | |
void** tracer_factory) { | |
std::cout << "called\n"; | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main.cpp:36:22: runtime error: call to function (unknown) through pointer to incorrect function type 'int (*)(const char *, const void **, void **)' | |
(liba.so+0x980): note: (unknown) defined here | |
called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <dlfcn.h> | |
#include <iostream> | |
#include <exception> | |
extern "C" { | |
int __attribute((weak)) | |
OpenTracingMakeTracerFactory(const char* opentracing_version, | |
const void** error_category, | |
void** tracer_factory); | |
} // extern "C" | |
void f(const char* shared_library) { | |
dlerror(); | |
const auto handle = dlopen(shared_library, RTLD_NOW | RTLD_LOCAL); | |
if (handle == nullptr) { | |
std::cerr << dlerror() << '\n'; | |
std::terminate(); | |
} | |
int (*make_tracer_factory)(const char*, const void**, void**) = | |
reinterpret_cast<decltype(OpenTracingMakeTracerFactory)*>( | |
dlsym(handle, "OpenTracingMakeTracerFactory")); | |
if (make_tracer_factory == nullptr) { | |
std::cerr << dlerror() << '\n'; | |
dlclose(handle); | |
std::terminate(); | |
} | |
const void* error_category = nullptr; | |
void* tracer_factory = nullptr; | |
const auto rcode = make_tracer_factory("1.0.0", &error_category, | |
&tracer_factory); | |
(void)rcode; | |
dlclose(handle); | |
} | |
int main() { | |
f("./liba.so"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: a.cpp main.cpp | |
clang++-5.0 -std=c++11 -fsanitize=undefined -g -fno-omit-frame-pointer -fPIC -shared a.cpp -o liba.so | |
clang++-5.0 -std=c++11 -fsanitize=undefined -g -fno-omit-frame-pointer main.cpp -ldl | |
clean: | |
rm a.out liba.so |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment