Issue
I am experiencing dynamic_cast issue when porting vsomeip to Android.
In order to simplify the case, I created a rather simple test app/lib structure as below:

It contains a lib and an app:
libbase_1.so: implement class Final which inherrit from Base_1 and Base_2, provides get() to return std::shared<Base_1>
test_app: dynamic loading libbase_1.so (by dl_open), get std::shared<Base>, and dynamic cast to std::shared<Base_2>.
The dynamic cast works well on x86 and other arm platforms(e.g.: use linaro toolchain); but failed to work on Android, it always get nullptr.
The environemt of the AOSP is:
- NDK Version: R18
- Build sytem: AOSP
- Host OS: Ubuntu 18.04
- Compiler: Clang++ (9.0.3)
- TARGET_ARCH: armv8a
I have followed some reference below but all do not work:
- Android NDK + Two Shared Libraries + RTTI + Dynamic cast = Impossible
- https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md#rtti_exceptions-not-working-across-library-boundaries
- https://github.com/android/ndk/issues/533#issuecomment-335977747
Does anyone have idea? Thanks a lot~~
The complete runnable src can be downloaded from: https://drive.google.com/file/d/1YNsZVPVIn7_y247byBfl6JaTVtjR6tYn/view?usp=sharing
And also pastes here:
//Android.bp: can be build directly by mm
libbase_1_srcs = [
"src_1.cpp",
"src_2.cpp",
"src_3.cpp",
"lib_1.cpp",
]
main_srcs = [
"main.cpp",
"src_1.cpp",
"src_2.cpp",
]
cc_defaults {
name: "test_cast_defaults",
cppflags: [
"-std=c++11",
"-frtti",
]
}
cc_library_shared {
name: "libbase_1",
vendor: true,
srcs: libbase_1_srcs,
defaults: [
"test_cast_defaults"
],
rtti: true,
}
cc_binary {
name: "test_cast",
srcs: main_srcs,
vendor: true,
defaults: [
"test_cast_defaults"
],
shared_libs: [
],
rtti: true,
}
//CMakeLists.txt: test for X86 or others
cmake_minimum_required(VERSION 3.1)
set(project_name "test_cast")
project(${project_name})
add_library(base_1 SHARED lib_1.cpp src_1.cpp src_2.cpp src_3.cpp)
target_include_directories(base_1 PUBLIC ${${project_name}_SOURCE_DIR}/)
add_executable(test_cast main.cpp src_1.cpp src_2.cpp)
target_include_directories(test_cast PUBLIC ${${project_name}_SOURCE_DIR}/)
target_link_libraries(test_cast PUBLIC dl)
//main.cpp
#include <dlfcn.h>
#include <iostream>
#include "lib_1.h"
#include <memory>
#define libname "libbase_1.so"
#define funcname "get_plugin"
int main(void)
{
void *handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
void *func;
if (handle == nullptr) {
std::cout << "Can not find: " << libname << std::endl;
return -1;
}
func = dlsym(handle, funcname);
const char *dlsym_error = dlerror();
if (dlsym_error) {
std::cout << "dlsym err: " << dlsym_error <<std::endl;
return -1;
}
plugin_init_func func_get = reinterpret_cast<plugin_init_func>(func);
get_plugin_func func_create = (*func_get)();
auto base_1 = (*func_create)();
auto base_2 = std::dynamic_pointer_cast<Base_2>(base_1);
if (base_2) {
std::cout << "dl: dynamic cast success!" << std::endl;
} else {
std::cout << "dl: dynamic cast failed!" << std::endl;
}
return 0;
}
//lib_1.cpp
#include "lib_1.h"
get_plugin_func get_plugin(void)
{
return Final::get;
}
//lib_1.h
#ifndef __LIB_1_H__
#define __LIB_1_H__
#include "header_3.h"
typedef std::shared_ptr<Base_1> (*get_plugin_func)();
typedef get_plugin_func (*plugin_init_func)();
extern "C" {
get_plugin_func get_plugin(void);
};
#endif
//header_1.h
#ifndef __HEADER_1__
#define __HEADER_1__
class Base_1 {
public:
virtual ~Base_1();
};
#endif
//header_2.h
#ifndef __HEADER_2__
#define __HEADER_2__
class Base_2 {
public:
virtual ~Base_2();
};
#endif
//header_3.h
#ifndef __HEADER_3__
#define __HEADER_3__
#include "header_1.h"
#include "header_2.h"
#include <iostream>
#include <memory>
class Final : public Base_2,
public Base_1 {
public:
static std::shared_ptr<Base_1> get(void) {
std::shared_ptr<Base_1> base_1 = std::make_shared<Final>();
auto base_2 = std::dynamic_pointer_cast<Base_2>(base_1);
if (base_2) {
std::cout << "in lib: dynamic cast success!" << std::endl;
} else {
std::cout << "in lib: dynamic cast failed!" << std::endl;
}
return base_1;
}
~Final();
};
#endif
//src_1.cpp
#include <header_1.h>
#include <iostream>
Base_1::~Base_1()
{
std::cout << "in: " << __func__ << std::endl;
}
//src_2.cpp
#include "header_2.h"
#include <iostream>
Base_2::~Base_2()
{
std::cout << "in: " << __func__ << std::endl;
}
//src_3.cpp
#include "header_3.h"
#include <iostream>
Final::~Final() {
std::cout << "in: " << __func__ << std::endl;
}
Succcess Run(X86):
in lib: dynamic cast success!
dl: dynamic cast success!
in: ~Final
in: ~Base_1
in: ~Base_2
Failure Run(Android):
in lib: dynamic cast success!
dl: dynamic cast failed!
in: ~Final
in: ~Base_1
in: ~Base_2
Solution
I had the same issue when I used CommonAPI via vsomeip with clang compiler and only workaround helped me to fix it. More details here: https://github.com/nkh-lab/genivi-capi-someip-examples/blob/ndk/CMakeLists.txt
The problem is that we have the same typeinfo for vsomeip_v3::configuration_plugin in libvsomeip3.so and libvsomeip3-cfg.so and dynamic_pointer_cast uses the wrong one (for example first entry in typeinfo table). So, we need to change the loading order of mentioned libraries and after that dynamic_pointer_cast will use correct one.
Also answered in GENIVI/vsomeip/issues
Answered By - Mykola Khyliuk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.