Skip to content

Commit d2aeef3

Browse files
committed
Add shims for hidden API
1 parent 4c43037 commit d2aeef3

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ add_compile_definitions(VIPER_VERSION=20240314)
77

88
# External
99
set(CORE_SRC
10-
external/core/libcutils/ashmem-host.cpp
10+
#[[external/core/libcutils/ashmem-host.cpp
1111
external/core/libcutils/native_handle.cpp
1212
external/core/libutils/SystemClock.cpp
13-
external/core/libutils/Timers.cpp)
13+
external/core/libutils/Timers.cpp]])
1414

1515
set(LIBFMQ_SRC
16-
external/libfmq/EventFlag.cpp)
16+
#[[external/libfmq/EventFlag.cpp]])
1717

1818
include_directories(
1919
external/libbase/include
@@ -223,5 +223,6 @@ add_library(v4a_re SHARED
223223
${FILES})
224224

225225
target_link_libraries(v4a_re log binder_ndk) # kissfft)
226+
target_link_options(v4a_re PRIVATE "LINKER:--no-demangle")
226227
target_compile_options(v4a_re PRIVATE -flto -O3 -DNDEBUG)
227228
#target_compile_options(v4afx_r PRIVATE -O2 -DNDEBUG -Wall -Wsign-conversion -Wno-unused-result -Wno-unneeded-internal-declaration -fstrict-aliasing -fvisibility=hidden -Wextra -Wno-unused-parameter)

src/shim.cpp

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,79 @@
1-
#include <string>
1+
/**
2+
* Shims for the "hidden" api, not found in the Android NDK.
3+
*
4+
* The symbols are compiled with the NDK, but the real Android ones have
5+
* a slightly different mangled name.
6+
*/
7+
28
#include <log/log.h>
9+
#include <dlfcn.h>
10+
11+
// libcutils
12+
void *ashmem_create_region;
13+
void *ashmem_set_prot_region;
14+
void *native_handle_create;
15+
void *native_handle_delete;
16+
void *native_handle_close;
17+
18+
// libfmq
19+
void *_ZN7android8hardware7details5checkEbPKc;
20+
void *_ZN7android8hardware7details8logErrorERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE; // ndk
21+
void *_ZN7android8hardware9EventFlag15createEventFlagEPNSt6__ndk16atomicIjEEPPS1_; // ndk
22+
void *_ZN7android8hardware9EventFlag15deleteEventFlagEPPS1_;
323

4-
namespace android::hardware::details {
5-
void check(bool exp) {
6-
ALOGE_IF(!exp, "Check failed");
24+
static void init_libcutils() {
25+
void *libcutils = dlopen("libcutils.so", RTLD_NOW);
26+
if (libcutils == nullptr) {
27+
ALOGE("Failed to load libcutils.so");
28+
return;
729
}
830

9-
void check(bool exp, const char* message) {
10-
ALOGE_IF(!exp, "%s", message);
31+
ashmem_create_region = dlsym(libcutils, "ashmem_create_region");
32+
ashmem_set_prot_region = dlsym(libcutils, "ashmem_set_prot_region");
33+
34+
native_handle_create = dlsym(libcutils, "native_handle_create");
35+
native_handle_close = dlsym(libcutils, "native_handle_close");
36+
native_handle_delete = dlsym(libcutils, "native_handle_delete");
37+
38+
if (ashmem_create_region == nullptr ||
39+
ashmem_set_prot_region == nullptr ||
40+
native_handle_create == nullptr ||
41+
native_handle_close == nullptr ||
42+
native_handle_delete == nullptr) {
43+
ALOGE("Failed to load symbols from libcutils.so");
1144
}
1245

13-
void logError(const std::string &message) {
14-
ALOGE("%s", message.c_str());
46+
dlclose(libcutils);
47+
}
48+
49+
static void init_libfmq() {
50+
void *libfmq = dlopen("libfmq.so", RTLD_NOW);
51+
if (libfmq == nullptr) {
52+
ALOGE("Failed to load libfmq.so");
53+
return;
1554
}
1655

17-
void errorWriteLog(int tag, const char* info) {
18-
ALOGE("%d: %s", tag, info);
56+
_ZN7android8hardware7details5checkEbPKc = dlsym(libfmq, "_ZN7android8hardware7details5checkEbPKc");
57+
// ndk variant, real symbol: _ZN7android8hardware7details8logErrorERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
58+
_ZN7android8hardware7details8logErrorERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE = dlsym(libfmq, "_ZN7android8hardware7details8logErrorERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE");
59+
// ndk variant, real symbol: _ZN7android8hardware9EventFlag15createEventFlagEPNSt3__16atomicIjEEPPS1_
60+
_ZN7android8hardware9EventFlag15createEventFlagEPNSt6__ndk16atomicIjEEPPS1_ = dlsym(libfmq, "_ZN7android8hardware9EventFlag15createEventFlagEPNSt3__16atomicIjEEPPS1_");
61+
_ZN7android8hardware9EventFlag15deleteEventFlagEPPS1_ = dlsym(libfmq, "_ZN7android8hardware9EventFlag15deleteEventFlagEPPS1_");
62+
63+
if (_ZN7android8hardware7details5checkEbPKc == nullptr ||
64+
_ZN7android8hardware7details8logErrorERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE == nullptr ||
65+
_ZN7android8hardware9EventFlag15createEventFlagEPNSt6__ndk16atomicIjEEPPS1_ == nullptr ||
66+
_ZN7android8hardware9EventFlag15deleteEventFlagEPPS1_ == nullptr) {
67+
ALOGE("Failed to load symbols from libfmq.so");
1968
}
20-
} // namespace android::hardware::details
69+
70+
dlclose(libfmq);
71+
}
72+
73+
__attribute__((constructor))
74+
void shim_init() {
75+
ALOGD("shim_init");
76+
init_libcutils();
77+
init_libfmq();
78+
ALOGD("shim_init done");
79+
}

src/viper_aidl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,11 @@ ndk::ScopedAStatus ViPER4AndroidAIDL::command(CommandId id) {
238238
stopThread();
239239
break;
240240
}
241+
default:
242+
ALOGE("command: unknown command %d", static_cast<int>(id));
243+
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
241244
}
242-
return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
245+
return ndk::ScopedAStatus::ok();
243246
}
244247

245248
ndk::ScopedAStatus ViPER4AndroidAIDL::getState(State *state) {

0 commit comments

Comments
 (0)