diff options
author | 2023-08-02 22:26:34 +0200 | |
---|---|---|
committer | 2023-08-08 15:01:51 +0000 | |
commit | 1aa5d121d7eb21a96f42cfd396e56bf2acdca162 (patch) | |
tree | 116a50933b659cade5816cc1a8ea237e76e4c3f2 /libnativebridge/tests | |
parent | 473c5a01699e82723c936bfd47ceac9abee70e09 (diff) |
nativebridge: Add getTrampoline2 function to pass JNICallType
NativeBridge needs to know if JNI call is @CriticalNative, starting with
v7 getTrampoline2 is used in place of getTrampoline.
Bug: https://issuetracker.google.com/288392666
Test: art/libnativebridge/tests/runtests.sh --skip-target
Change-Id: I60a2fd6ab2fb9d19dda7bbdcbe09144797bd1d49
Diffstat (limited to 'libnativebridge/tests')
-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 |
6 files changed, 288 insertions, 0 deletions
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}; |