summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author dimitry <dimitry@google.com> 2025-01-29 13:24:43 +0100
committer Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2025-02-05 10:35:47 -0800
commit3428f9be71b0d20978d11d63b5d82962b88d02bf (patch)
tree9932f8416dbc5ea7a1b8e32dab68afc192816236
parent65914c03ce6a5568a0c98f05f8217bb89108bf1b (diff)
nativebridge: Add isNativeBridgeFunctionPointer method
This method lets native bridge report if particular pointer is in the region of code executable by native bridge implementation. Bug: 393035780 Test: atest art/libnativebridge/tests Change-Id: Icaec80cb7efae5b918aed0a98216cbfaeaba330d
-rw-r--r--libnativebridge/include/nativebridge/native_bridge.h19
-rw-r--r--libnativebridge/native_bridge.cc14
-rw-r--r--libnativebridge/tests/Android.bp19
-rw-r--r--libnativebridge/tests/NativeBridge8IdentifyTrampolines_lib.cpp31
-rw-r--r--libnativebridge/tests/NativeBridge8IdentifyTrampolines_lib.h27
-rw-r--r--libnativebridge/tests/NativeBridge8IdentifyTrampolines_test.cpp39
-rw-r--r--libnativebridge/tests/NativeBridgeTest.h1
-rw-r--r--libnativebridge/tests/NativeBridgeTestCase8.cpp147
8 files changed, 295 insertions, 2 deletions
diff --git a/libnativebridge/include/nativebridge/native_bridge.h b/libnativebridge/include/nativebridge/native_bridge.h
index 491521642c..db3d1e9509 100644
--- a/libnativebridge/include/nativebridge/native_bridge.h
+++ b/libnativebridge/include/nativebridge/native_bridge.h
@@ -100,6 +100,8 @@ void* NativeBridgeGetTrampolineForFunctionPointer(const void* method,
uint32_t len,
enum JNICallType jni_call_type);
+bool NativeBridgeIsNativeBridgeFunctionPointer(const void* method);
+
// 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.
//
@@ -403,9 +405,10 @@ struct NativeBridgeCallbacks {
// Get a native bridge trampoline for specified native method implementation pointer.
//
// Parameters:
- // method [IN] pointer to method implementation (ususally registered via call to
+ // method [IN] pointer to method implementation (usually registered via call to
// RegisterNatives).
- // shorty [IN] short descriptor of native method len [IN] length of shorty
+ // 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
@@ -413,6 +416,18 @@ struct NativeBridgeCallbacks {
const char* shorty,
uint32_t len,
enum JNICallType jni_call_type);
+
+ // Check if the method pointer belongs to native_bridge address space.
+ //
+ // Parameters:
+ // method [IN] pointer to a method implementation.
+ //
+ // Returns:
+ // true if the method is in native bridge implementation executable address
+ // space or in other words needs a trampoline to be able to run with native bridge.
+ //
+ // Introduced in: version 8
+ bool (*isNativeBridgeFunctionPointer)(const void* method);
};
// Runtime interfaces to native bridge.
diff --git a/libnativebridge/native_bridge.cc b/libnativebridge/native_bridge.cc
index 4461f79919..af85d4ee35 100644
--- a/libnativebridge/native_bridge.cc
+++ b/libnativebridge/native_bridge.cc
@@ -133,6 +133,8 @@ enum NativeBridgeImplementationVersion {
PRE_ZYGOTE_FORK_VERSION = 6,
// The version with critical_native support
CRITICAL_NATIVE_SUPPORT_VERSION = 7,
+ // The version with native bridge detection fallback for function pointers
+ IDENTIFY_NATIVELY_BRIDGED_FUNCTION_POINTERS_VERSION = 8,
};
// Whether we had an error at some point.
@@ -732,6 +734,18 @@ void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, native_bridge_na
return nullptr;
}
+bool NativeBridgeIsNativeBridgeFunctionPointer(const void* method) {
+ if (NativeBridgeInitialized()) {
+ if (isCompatibleWith(IDENTIFY_NATIVELY_BRIDGED_FUNCTION_POINTERS_VERSION)) {
+ return callbacks->isNativeBridgeFunctionPointer(method);
+ } else {
+ ALOGW("not compatible with version %d, unable to call isNativeBridgeFunctionPointer",
+ IDENTIFY_NATIVELY_BRIDGED_FUNCTION_POINTERS_VERSION);
+ }
+ }
+ return false;
+}
+
} // extern "C"
} // namespace android
diff --git a/libnativebridge/tests/Android.bp b/libnativebridge/tests/Android.bp
index 93ef6feded..add18d12e3 100644
--- a/libnativebridge/tests/Android.bp
+++ b/libnativebridge/tests/Android.bp
@@ -85,6 +85,15 @@ cc_test_library {
],
}
+cc_test_library {
+ name: "libnativebridge8-test-case",
+ srcs: ["NativeBridgeTestCase8.cpp"],
+ defaults: ["libnativebridge-test-case-defaults"],
+ shared_libs: [
+ "libnativebridge8IdentifyTrampolines",
+ ],
+}
+
// A helper library to produce test-case side effect of PreZygoteForkNativeBridge.
cc_test_library {
name: "libnativebridge6prezygotefork",
@@ -98,6 +107,12 @@ cc_test_library {
defaults: ["libnativebridge-test-case-defaults"],
}
+cc_test_library {
+ name: "libnativebridge8IdentifyTrampolines",
+ srcs: ["NativeBridge8IdentifyTrampolines_lib.cpp"],
+ defaults: ["libnativebridge-test-case-defaults"],
+}
+
cc_test {
name: "libnativebridge-tests",
defaults: [
@@ -130,6 +145,7 @@ cc_test {
"NativeBridge3LoadLibraryExt_test.cpp",
"NativeBridge6PreZygoteFork_test.cpp",
"NativeBridge7CriticalNative_test.cpp",
+ "NativeBridge8IdentifyTrampolines_test.cpp",
],
static_libs: [
@@ -140,10 +156,12 @@ cc_test {
"liblog",
"libnativebridge6prezygotefork",
"libnativebridge7criticalnative",
+ "libnativebridge8IdentifyTrampolines",
],
data_libs: [
"libnativebridge6prezygotefork",
"libnativebridge7criticalnative",
+ "libnativebridge8IdentifyTrampolines",
// These are dlopen'd by libnativebridge, not libnativebridge-tests, but
// the former is statically linked into the latter, so the linker will
@@ -153,6 +171,7 @@ cc_test {
"libnativebridge3-test-case",
"libnativebridge6-test-case",
"libnativebridge7-test-case",
+ "libnativebridge8-test-case",
],
target: {
diff --git a/libnativebridge/tests/NativeBridge8IdentifyTrampolines_lib.cpp b/libnativebridge/tests/NativeBridge8IdentifyTrampolines_lib.cpp
new file mode 100644
index 0000000000..0c4ccf03b5
--- /dev/null
+++ b/libnativebridge/tests/NativeBridge8IdentifyTrampolines_lib.cpp
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2025 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 "NativeBridge8IdentifyTrampolines_lib.h"
+
+namespace android {
+
+static const void* g_is_native_bridge_function_pointer_called_for = nullptr;
+
+void SetIsNativeBridgeFunctionPointerCalledFor(const void* ptr) {
+ g_is_native_bridge_function_pointer_called_for = ptr;
+}
+
+bool IsNativeBridgeFunctionPointerCalledFor(const void* ptr) {
+ return g_is_native_bridge_function_pointer_called_for == ptr;
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/NativeBridge8IdentifyTrampolines_lib.h b/libnativebridge/tests/NativeBridge8IdentifyTrampolines_lib.h
new file mode 100644
index 0000000000..7121e397a4
--- /dev/null
+++ b/libnativebridge/tests/NativeBridge8IdentifyTrampolines_lib.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2025 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_NATIVEBRIDGE8IDENTIFYTRAMPOLINES_LIB_H_
+#define ART_LIBNATIVEBRIDGE_TESTS_NATIVEBRIDGE8IDENTIFYTRAMPOLINES_LIB_H_
+
+namespace android {
+
+void SetIsNativeBridgeFunctionPointerCalledFor(const void* ptr);
+bool IsNativeBridgeFunctionPointerCalledFor(const void* ptr);
+
+} // namespace android
+
+#endif // ART_LIBNATIVEBRIDGE_TESTS_NATIVEBRIDGE8IDENTIFYTRAMPOLINES_LIB_H_
diff --git a/libnativebridge/tests/NativeBridge8IdentifyTrampolines_test.cpp b/libnativebridge/tests/NativeBridge8IdentifyTrampolines_test.cpp
new file mode 100644
index 0000000000..362df54fa1
--- /dev/null
+++ b/libnativebridge/tests/NativeBridge8IdentifyTrampolines_test.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2025 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 "NativeBridge8IdentifyTrampolines_lib.h"
+#include "NativeBridgeTest.h"
+
+namespace android {
+
+TEST_F(NativeBridgeTest, V8_IdentifyTrampolines) {
+ // Init
+ ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary8, nullptr));
+ ASSERT_TRUE(NativeBridgeAvailable());
+ ASSERT_TRUE(PreInitializeNativeBridge(AppDataDir(), "isa"));
+ ASSERT_TRUE(NativeBridgeAvailable());
+ ASSERT_TRUE(InitializeNativeBridge(nullptr, nullptr));
+ ASSERT_TRUE(NativeBridgeAvailable());
+
+ ASSERT_EQ(NativeBridgeGetVersion(), 8U);
+
+ const void* ptr = reinterpret_cast<void*>(NativeBridgeGetVersion);
+
+ UNUSED(NativeBridgeIsNativeBridgeFunctionPointer(ptr));
+ ASSERT_TRUE(IsNativeBridgeFunctionPointerCalledFor(ptr));
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/NativeBridgeTest.h b/libnativebridge/tests/NativeBridgeTest.h
index 509973d756..88ea0e33c8 100644
--- a/libnativebridge/tests/NativeBridgeTest.h
+++ b/libnativebridge/tests/NativeBridgeTest.h
@@ -30,6 +30,7 @@ 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";
+constexpr const char* kNativeBridgeLibrary8 = "libnativebridge8-test-case.so";
namespace android {
diff --git a/libnativebridge/tests/NativeBridgeTestCase8.cpp b/libnativebridge/tests/NativeBridgeTestCase8.cpp
new file mode 100644
index 0000000000..c6395f526b
--- /dev/null
+++ b/libnativebridge/tests/NativeBridgeTestCase8.cpp
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2025 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 "NativeBridge8IdentifyTrampolines_lib.h"
+#include "nativebridge/native_bridge.h"
+
+// NativeBridgeCallbacks implementations
+extern "C" bool native_bridge8_initialize(
+ const android::NativeBridgeRuntimeCallbacks* /* art_cbs */,
+ const char* /* app_code_cache_dir */,
+ const char* /* isa */) {
+ return true;
+}
+
+extern "C" void* native_bridge8_loadLibrary(const char* /* libpath */, int /* flag */) {
+ return nullptr;
+}
+
+extern "C" void* native_bridge8_getTrampoline(void* /* handle */,
+ const char* /* name */,
+ const char* /* shorty */,
+ uint32_t /* len */) {
+ return nullptr;
+}
+
+extern "C" void* native_bridge8_getTrampoline2(void* /* handle */,
+ const char* /* name */,
+ const char* /* shorty */,
+ uint32_t /* len */,
+ android::JNICallType /* jni_call_type */) {
+ return nullptr;
+}
+
+extern "C" void* native_bridge8_getTrampolineForFunctionPointer(
+ const void* /* method */,
+ const char* /* shorty */,
+ uint32_t /* len */,
+ android::JNICallType /* jni_call_type */) {
+ return nullptr;
+}
+
+extern "C" bool native_bridge8_isSupported(const char* /* libpath */) { return false; }
+
+extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge8_getAppEnv(
+ const char* /* abi */) {
+ return nullptr;
+}
+
+extern "C" bool native_bridge8_isCompatibleWith(uint32_t version) {
+ // For testing, allow 1-8, but disallow 9+.
+ return version <= 8;
+}
+
+extern "C" android::NativeBridgeSignalHandlerFn native_bridge8_getSignalHandler(int /* signal */) {
+ return nullptr;
+}
+
+extern "C" int native_bridge8_unloadLibrary(void* /* handle */) { return 0; }
+
+extern "C" const char* native_bridge8_getError() { return nullptr; }
+
+extern "C" bool native_bridge8_isPathSupported(const char* /* path */) { return true; }
+
+extern "C" android::native_bridge_namespace_t* native_bridge8_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_bridge8_linkNamespaces(android::native_bridge_namespace_t* /* from */,
+ android::native_bridge_namespace_t* /* to */,
+ const char* /* shared_libs_soname */) {
+ return true;
+}
+
+extern "C" void* native_bridge8_loadLibraryExt(const char* /* libpath */,
+ int /* flag */,
+ android::native_bridge_namespace_t* /* ns */) {
+ return nullptr;
+}
+
+extern "C" android::native_bridge_namespace_t* native_bridge8_getVendorNamespace() {
+ return nullptr;
+}
+
+extern "C" android::native_bridge_namespace_t* native_bridge8_getExportedNamespace(
+ const char* /* name */) {
+ return nullptr;
+}
+
+extern "C" bool native_bridge8_isNativeBridgeFunctionPointer(const void* ptr) {
+ android::SetIsNativeBridgeFunctionPointerCalledFor(ptr);
+ return true;
+}
+
+extern "C" void native_bridge8_preZygoteFork() {}
+
+android::NativeBridgeCallbacks NativeBridgeItf{
+ // v1
+ .version = 8,
+ .initialize = &native_bridge8_initialize,
+ .loadLibrary = &native_bridge8_loadLibrary,
+ .getTrampoline = &native_bridge8_getTrampoline,
+ .isSupported = &native_bridge8_isSupported,
+ .getAppEnv = &native_bridge8_getAppEnv,
+ // v2
+ .isCompatibleWith = &native_bridge8_isCompatibleWith,
+ .getSignalHandler = &native_bridge8_getSignalHandler,
+ // v3
+ .unloadLibrary = &native_bridge8_unloadLibrary,
+ .getError = &native_bridge8_getError,
+ .isPathSupported = &native_bridge8_isPathSupported,
+ .unused_initAnonymousNamespace = nullptr,
+ .createNamespace = &native_bridge8_createNamespace,
+ .linkNamespaces = &native_bridge8_linkNamespaces,
+ .loadLibraryExt = &native_bridge8_loadLibraryExt,
+ // v4
+ &native_bridge8_getVendorNamespace,
+ // v5
+ &native_bridge8_getExportedNamespace,
+ // v6
+ &native_bridge8_preZygoteFork,
+ // v7
+ &native_bridge8_getTrampoline2,
+ &native_bridge8_getTrampolineForFunctionPointer,
+ // v8
+ &native_bridge8_isNativeBridgeFunctionPointer,
+};