diff options
-rw-r--r-- | libnativebridge/include/nativebridge/native_bridge.h | 36 | ||||
-rw-r--r-- | libnativebridge/native_bridge.cc | 22 | ||||
-rw-r--r-- | libnativebridge/native_bridge_lazy.cc | 6 | ||||
-rw-r--r-- | libnativebridge/tests/Android.bp | 18 | ||||
-rw-r--r-- | libnativebridge/tests/NativeBridge7CriticalNative_lib.cpp | 44 | ||||
-rw-r--r-- | libnativebridge/tests/NativeBridge7CriticalNative_lib.h | 35 | ||||
-rw-r--r-- | libnativebridge/tests/NativeBridge7CriticalNative_test.cpp | 53 | ||||
-rw-r--r-- | libnativebridge/tests/NativeBridgeTest.h | 1 | ||||
-rw-r--r-- | libnativebridge/tests/NativeBridgeTestCase7.cpp | 137 | ||||
-rw-r--r-- | test/115-native-bridge/nativebridge.cc | 4 |
10 files changed, 351 insertions, 5 deletions
diff --git a/libnativebridge/include/nativebridge/native_bridge.h b/libnativebridge/include/nativebridge/native_bridge.h index 5904c0fe3f..a87b04fa36 100644 --- a/libnativebridge/include/nativebridge/native_bridge.h +++ b/libnativebridge/include/nativebridge/native_bridge.h @@ -29,6 +29,11 @@ namespace android { extern "C" { #endif // __cplusplus +enum JNICallType { + kJNICallTypeRegular = 1, + kJNICallTypeCriticalNative = 2, +}; + // Loads a shared library from the system linker namespace, suitable for // platform libraries in /system/lib(64). If linker namespaces don't exist (i.e. // on host), this simply calls dlopen(). @@ -81,8 +86,15 @@ bool NativeBridgeInitialized(); void* NativeBridgeLoadLibrary(const char* libpath, int flag); // Get a native bridge trampoline for specified native method. +// This version is deprecated - please use NativeBridgeGetTrampoline2 void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, uint32_t len); +void* NativeBridgeGetTrampoline2(void* handle, + const char* name, + const char* shorty, + uint32_t len, + enum JNICallType jni_call_type); + // True if native library paths are valid and is for an ABI that is supported by native bridge. // The *libpath* must point to a library. // @@ -206,7 +218,7 @@ struct NativeBridgeCallbacks { void* (*loadLibrary)(const char* libpath, int flag); // Get a native bridge trampoline for specified native method. The trampoline has same - // sigature as the native method. + // signature as the native method. // // Parameters: // handle [IN] the handle returned from loadLibrary @@ -214,6 +226,9 @@ struct NativeBridgeCallbacks { // len [IN] length of shorty // Returns: // address of trampoline if successful, otherwise NULL + // Deprecated in v7 + // Starting with version 7 native bridge uses getTrampolineWithJNICallType + // instead void* (*getTrampoline)(void* handle, const char* name, const char* shorty, uint32_t len); // Check whether native library is valid and is for an ABI that is supported by native bridge. @@ -387,6 +402,25 @@ struct NativeBridgeCallbacks { // If native bridge is used in app-zygote (in doPreload()) this callback is // required to clean-up the environment before the fork (see b/146904103). void (*preZygoteFork)(); + + // This replaces previous getTrampoline call starting with version 7 of the + // interface. + // + // Get a native bridge trampoline for specified native method. The trampoline + // has same signature as the native method. + // + // Parameters: + // handle [IN] the handle returned from loadLibrary + // shorty [IN] short descriptor of native method + // len [IN] length of shorty + // jni_call_type [IN] the type of JNI call + // Returns: + // address of trampoline if successful, otherwise NULL + void* (*getTrampolineWithJNICallType)(void* handle, + const char* name, + const char* shorty, + uint32_t len, + enum JNICallType jni_call_type); }; // Runtime interfaces to native bridge. diff --git a/libnativebridge/native_bridge.cc b/libnativebridge/native_bridge.cc index fb13d62be0..aabdae6205 100644 --- a/libnativebridge/native_bridge.cc +++ b/libnativebridge/native_bridge.cc @@ -131,6 +131,8 @@ enum NativeBridgeImplementationVersion { RUNTIME_NAMESPACE_VERSION = 5, // The version with pre-zygote-fork hook to support app-zygotes. PRE_ZYGOTE_FORK_VERSION = 6, + // The version with critical_native support + CRITICAL_NATIVE_SUPPORT_VERSION = 7, }; // Whether we had an error at some point. @@ -569,10 +571,26 @@ void* NativeBridgeLoadLibrary(const char* libpath, int flag) { void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, uint32_t len) { - if (NativeBridgeInitialized()) { + return NativeBridgeGetTrampoline2(handle, name, shorty, len, kJNICallTypeRegular); +} + +void* NativeBridgeGetTrampoline2( + void* handle, const char* name, const char* shorty, uint32_t len, JNICallType jni_call_type) { + if (!NativeBridgeInitialized()) { + return nullptr; + } + + // For version 1 isCompatibleWith is always true, even though the extensions + // are not supported, so we need to handle it separately. + if (callbacks != nullptr && callbacks->version == DEFAULT_VERSION) { return callbacks->getTrampoline(handle, name, shorty, len); } - return nullptr; + + if (isCompatibleWith(CRITICAL_NATIVE_SUPPORT_VERSION)) { + return callbacks->getTrampolineWithJNICallType(handle, name, shorty, len, jni_call_type); + } + + return callbacks->getTrampoline(handle, name, shorty, len); } bool NativeBridgeIsSupported(const char* libpath) { diff --git a/libnativebridge/native_bridge_lazy.cc b/libnativebridge/native_bridge_lazy.cc index dd8a8068b4..12dfb0ddb9 100644 --- a/libnativebridge/native_bridge_lazy.cc +++ b/libnativebridge/native_bridge_lazy.cc @@ -69,6 +69,12 @@ void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shor return f(handle, name, shorty, len); } +void* NativeBridgeGetTrampoline2( + void* handle, const char* name, const char* shorty, uint32_t len, JNICallType jni_call_type) { + static auto f = GET_FUNC_PTR(NativeBridgeGetTrampoline2); + return f(handle, name, shorty, len, jni_call_type); +} + const char* NativeBridgeGetError() { static auto f = GET_FUNC_PTR(NativeBridgeGetError); return f(); diff --git a/libnativebridge/tests/Android.bp b/libnativebridge/tests/Android.bp index f1725fc278..b416b0081e 100644 --- a/libnativebridge/tests/Android.bp +++ b/libnativebridge/tests/Android.bp @@ -75,6 +75,15 @@ cc_test_library { ], } +cc_test_library { + name: "libnativebridge7-test-case", + srcs: ["NativeBridgeTestCase7.cpp"], + defaults: ["libnativebridge-test-case-defaults"], + shared_libs: [ + "libnativebridge7criticalnative", + ], +} + // A helper library to produce test-case side effect of PreZygoteForkNativeBridge. cc_test_library { name: "libnativebridge6prezygotefork", @@ -82,6 +91,12 @@ cc_test_library { defaults: ["libnativebridge-test-case-defaults"], } +cc_test_library { + name: "libnativebridge7criticalnative", + srcs: ["NativeBridge7CriticalNative_lib.cpp"], + defaults: ["libnativebridge-test-case-defaults"], +} + cc_defaults { name: "libnativebridge-tests-defaults", defaults: [ @@ -140,6 +155,7 @@ cc_test { "NativeBridge3CreateNamespace_test.cpp", "NativeBridge3LoadLibraryExt_test.cpp", "NativeBridge6PreZygoteFork_test.cpp", + "NativeBridge7CriticalNative_test.cpp", ], shared_libs: [ @@ -149,7 +165,9 @@ cc_test { "libnativebridge2-test-case", "libnativebridge3-test-case", "libnativebridge6-test-case", + "libnativebridge7-test-case", "libnativebridge6prezygotefork", + "libnativebridge7criticalnative", ], header_libs: ["libbase_headers"], } diff --git a/libnativebridge/tests/NativeBridge7CriticalNative_lib.cpp b/libnativebridge/tests/NativeBridge7CriticalNative_lib.cpp new file mode 100644 index 0000000000..6446182487 --- /dev/null +++ b/libnativebridge/tests/NativeBridge7CriticalNative_lib.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "NativeBridge7CriticalNative_lib.h" + +namespace android { + +static bool g_legacy_get_trampoline_called = false; +static bool g_get_trampoline2_called = false; +static JNICallType g_jni_call_type = kJNICallTypeRegular; + +void ResetTrampolineCalledState() { + g_legacy_get_trampoline_called = false; + g_get_trampoline2_called = false; + g_jni_call_type = kJNICallTypeRegular; +} + +void SetLegacyGetTrampolineCalled() { g_legacy_get_trampoline_called = true; } + +bool IsLegacyGetTrampolineCalled() { return g_legacy_get_trampoline_called; } + +void SetGetTrampoline2Called(JNICallType jni_call_type) { + g_get_trampoline2_called = true; + g_jni_call_type = jni_call_type; +} + +bool IsGetTrampoline2Called() { return g_get_trampoline2_called; } + +JNICallType GetTrampoline2JNICallType() { return g_jni_call_type; } + +} // namespace android diff --git a/libnativebridge/tests/NativeBridge7CriticalNative_lib.h b/libnativebridge/tests/NativeBridge7CriticalNative_lib.h new file mode 100644 index 0000000000..5a7594b203 --- /dev/null +++ b/libnativebridge/tests/NativeBridge7CriticalNative_lib.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ART_LIBNATIVEBRIDGE_TESTS_NATIVEBRIDGE7CRITICALNATIVE_LIB_H_ +#define ART_LIBNATIVEBRIDGE_TESTS_NATIVEBRIDGE7CRITICALNATIVE_LIB_H_ + +#include "nativebridge/native_bridge.h" + +namespace android { + +void ResetTrampolineCalledState(); + +void SetLegacyGetTrampolineCalled(); +bool IsLegacyGetTrampolineCalled(); + +void SetGetTrampoline2Called(JNICallType jni_call_type); +bool IsGetTrampoline2Called(); +JNICallType GetTrampoline2JNICallType(); + +} // namespace android + +#endif // ART_LIBNATIVEBRIDGE_TESTS_NATIVEBRIDGE7CRITICALNATIVE_LIB_H_ diff --git a/libnativebridge/tests/NativeBridge7CriticalNative_test.cpp b/libnativebridge/tests/NativeBridge7CriticalNative_test.cpp new file mode 100644 index 0000000000..0d9f8bdc3a --- /dev/null +++ b/libnativebridge/tests/NativeBridge7CriticalNative_test.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "NativeBridge7CriticalNative_lib.h" +#include "NativeBridgeTest.h" + +namespace android { + +TEST_F(NativeBridgeTest, V7_CriticalNative) { + // Init + ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary7, nullptr)); + ASSERT_TRUE(NativeBridgeAvailable()); + ASSERT_TRUE(PreInitializeNativeBridge(".", "isa")); + ASSERT_TRUE(NativeBridgeAvailable()); + ASSERT_TRUE(InitializeNativeBridge(nullptr, nullptr)); + ASSERT_TRUE(NativeBridgeAvailable()); + + ASSERT_EQ(NativeBridgeGetVersion(), 7U); + + ASSERT_FALSE(IsGetTrampoline2Called()); + ASSERT_FALSE(IsLegacyGetTrampolineCalled()); + + EXPECT_EQ(NativeBridgeGetTrampoline(nullptr, "symbol", "shorty", 6), nullptr); + ASSERT_FALSE(IsLegacyGetTrampolineCalled()); + ASSERT_TRUE(IsGetTrampoline2Called()); + EXPECT_EQ(GetTrampoline2JNICallType(), kJNICallTypeRegular); + + ResetTrampolineCalledState(); + + ASSERT_FALSE(IsGetTrampoline2Called()); + ASSERT_FALSE(IsLegacyGetTrampolineCalled()); + + EXPECT_EQ(NativeBridgeGetTrampoline2(nullptr, "symbol", "shorty", 6, kJNICallTypeCriticalNative), + nullptr); + ASSERT_FALSE(IsLegacyGetTrampolineCalled()); + ASSERT_TRUE(IsGetTrampoline2Called()); + EXPECT_EQ(GetTrampoline2JNICallType(), kJNICallTypeCriticalNative); +} + +} // namespace android diff --git a/libnativebridge/tests/NativeBridgeTest.h b/libnativebridge/tests/NativeBridgeTest.h index 62509b8917..6413233006 100644 --- a/libnativebridge/tests/NativeBridgeTest.h +++ b/libnativebridge/tests/NativeBridgeTest.h @@ -28,6 +28,7 @@ constexpr const char* kCodeCacheStatFail = "./code_cache/temp"; constexpr const char* kNativeBridgeLibrary2 = "libnativebridge2-test-case.so"; constexpr const char* kNativeBridgeLibrary3 = "libnativebridge3-test-case.so"; constexpr const char* kNativeBridgeLibrary6 = "libnativebridge6-test-case.so"; +constexpr const char* kNativeBridgeLibrary7 = "libnativebridge7-test-case.so"; namespace android { diff --git a/libnativebridge/tests/NativeBridgeTestCase7.cpp b/libnativebridge/tests/NativeBridgeTestCase7.cpp new file mode 100644 index 0000000000..03016bdbac --- /dev/null +++ b/libnativebridge/tests/NativeBridgeTestCase7.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// An implementation of the native-bridge interface for testing. + +#include "NativeBridge7CriticalNative_lib.h" +#include "nativebridge/native_bridge.h" + +// NativeBridgeCallbacks implementations +extern "C" bool native_bridge7_initialize( + const android::NativeBridgeRuntimeCallbacks* /* art_cbs */, + const char* /* app_code_cache_dir */, + const char* /* isa */) { + return true; +} + +extern "C" void* native_bridge7_loadLibrary(const char* /* libpath */, int /* flag */) { + return nullptr; +} + +extern "C" void* native_bridge7_getTrampoline(void* /* handle */, + const char* /* name */, + const char* /* shorty */, + uint32_t /* len */) { + android::SetLegacyGetTrampolineCalled(); + return nullptr; +} + +extern "C" void* native_bridge7_getTrampoline2(void* /* handle */, + const char* /* name */, + const char* /* shorty */, + uint32_t /* len */, + android::JNICallType jni_call_type) { + android::SetGetTrampoline2Called(jni_call_type); + return nullptr; +} + +extern "C" bool native_bridge7_isSupported(const char* /* libpath */) { return false; } + +extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge7_getAppEnv( + const char* /* abi */) { + return nullptr; +} + +extern "C" bool native_bridge7_isCompatibleWith(uint32_t version) { + // For testing, allow 1-7, but disallow 8+. + return version <= 7; +} + +extern "C" android::NativeBridgeSignalHandlerFn native_bridge7_getSignalHandler(int /* signal */) { + return nullptr; +} + +extern "C" int native_bridge7_unloadLibrary(void* /* handle */) { return 0; } + +extern "C" const char* native_bridge7_getError() { return nullptr; } + +extern "C" bool native_bridge7_isPathSupported(const char* /* path */) { return true; } + +extern "C" bool native_bridge7_initAnonymousNamespace(const char* /* public_ns_sonames */, + const char* /* anon_ns_library_path */) { + return true; +} + +extern "C" android::native_bridge_namespace_t* native_bridge7_createNamespace( + const char* /* name */, + const char* /* ld_library_path */, + const char* /* default_library_path */, + uint64_t /* type */, + const char* /* permitted_when_isolated_path */, + android::native_bridge_namespace_t* /* parent_ns */) { + return nullptr; +} + +extern "C" bool native_bridge7_linkNamespaces(android::native_bridge_namespace_t* /* from */, + android::native_bridge_namespace_t* /* to */, + const char* /* shared_libs_soname */) { + return true; +} + +extern "C" void* native_bridge7_loadLibraryExt(const char* /* libpath */, + int /* flag */, + android::native_bridge_namespace_t* /* ns */) { + return nullptr; +} + +extern "C" android::native_bridge_namespace_t* native_bridge7_getVendorNamespace() { + return nullptr; +} + +extern "C" android::native_bridge_namespace_t* native_bridge7_getExportedNamespace( + const char* /* name */) { + return nullptr; +} + +extern "C" void native_bridge7_preZygoteFork() {} + +android::NativeBridgeCallbacks NativeBridgeItf{ + // v1 + .version = 7, + .initialize = &native_bridge7_initialize, + .loadLibrary = &native_bridge7_loadLibrary, + .getTrampoline = &native_bridge7_getTrampoline, + .isSupported = &native_bridge7_isSupported, + .getAppEnv = &native_bridge7_getAppEnv, + // v2 + .isCompatibleWith = &native_bridge7_isCompatibleWith, + .getSignalHandler = &native_bridge7_getSignalHandler, + // v3 + .unloadLibrary = &native_bridge7_unloadLibrary, + .getError = &native_bridge7_getError, + .isPathSupported = &native_bridge7_isPathSupported, + .initAnonymousNamespace = &native_bridge7_initAnonymousNamespace, + .createNamespace = &native_bridge7_createNamespace, + .linkNamespaces = &native_bridge7_linkNamespaces, + .loadLibraryExt = &native_bridge7_loadLibraryExt, + // v4 + &native_bridge7_getVendorNamespace, + // v5 + &native_bridge7_getExportedNamespace, + // v6 + &native_bridge7_preZygoteFork, + // v7 + &native_bridge7_getTrampoline2}; diff --git a/test/115-native-bridge/nativebridge.cc b/test/115-native-bridge/nativebridge.cc index 65d7fcd315..3dcc1fcc4a 100644 --- a/test/115-native-bridge/nativebridge.cc +++ b/test/115-native-bridge/nativebridge.cc @@ -532,8 +532,8 @@ extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge_getApp // v2 parts. -extern "C" bool native_bridge_isCompatibleWith([[maybe_unused]] uint32_t bridge_version) { - return true; +extern "C" bool native_bridge_isCompatibleWith(uint32_t bridge_version) { + return bridge_version <= 3; } #if defined(__i386__) || defined(__x86_64__) |