nativebridge: Add PreZygoteFork callback
It's required to clean-up the emulated enviroment (e.g. close file
descriptors) after emulated execution in doPreload() in app-zygote.
Test: NativeBridge6PreZygoteFork_test
Test: CtsSeccompHostTestCases
android.seccomp.cts.SeccompHostJUnit4DeviceTest
testAppZygoteSyscalls
both for Q.sdk_gphone_x86_arm.armeabi-v7a
Bug: 146904103
Change-Id: Id192a1647c2f405570bf196daf65b3f2a9faca42
diff --git a/libnativebridge/tests/Android.bp b/libnativebridge/tests/Android.bp
index 9211634..4ccf35e 100644
--- a/libnativebridge/tests/Android.bp
+++ b/libnativebridge/tests/Android.bp
@@ -54,6 +54,22 @@
defaults: ["libnativebridge-dummy-defaults"],
}
+cc_library_shared {
+ name: "libnativebridge6-dummy",
+ srcs: ["DummyNativeBridge6.cpp"],
+ defaults: ["libnativebridge-dummy-defaults"],
+ shared_libs: [
+ "libnativebridge6prezygotefork",
+ ],
+}
+
+// A helper library to produce dummy side effect of PreZygoteForkNativeBridge.
+cc_library_shared {
+ name: "libnativebridge6prezygotefork",
+ srcs: ["NativeBridge6PreZygoteFork_lib.cpp"],
+ defaults: ["libnativebridge-dummy-defaults"],
+}
+
// Build the unit tests.
cc_defaults {
name: "libnativebridge-tests-defaults",
@@ -96,11 +112,13 @@
"NativeBridge3InitAnonymousNamespace_test.cpp",
"NativeBridge3CreateNamespace_test.cpp",
"NativeBridge3LoadLibraryExt_test.cpp",
+ "NativeBridge6PreZygoteFork_test.cpp",
],
shared_libs: [
"liblog",
"libnativebridge-dummy",
+ "libnativebridge6prezygotefork",
],
header_libs: ["libbase_headers"],
}
diff --git a/libnativebridge/tests/DummyNativeBridge6.cpp b/libnativebridge/tests/DummyNativeBridge6.cpp
new file mode 100644
index 0000000..ce27e67
--- /dev/null
+++ b/libnativebridge/tests/DummyNativeBridge6.cpp
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+// A dummy implementation of the native-bridge interface.
+
+#include "nativebridge/native_bridge.h"
+
+#include "NativeBridge6PreZygoteFork_lib.h"
+
+// NativeBridgeCallbacks implementations
+extern "C" bool native_bridge6_initialize(
+ const android::NativeBridgeRuntimeCallbacks* /* art_cbs */,
+ const char* /* app_code_cache_dir */,
+ const char* /* isa */) {
+ return true;
+}
+
+extern "C" void* native_bridge6_loadLibrary(const char* /* libpath */, int /* flag */) {
+ return nullptr;
+}
+
+extern "C" void* native_bridge6_getTrampoline(void* /* handle */, const char* /* name */,
+ const char* /* shorty */, uint32_t /* len */) {
+ return nullptr;
+}
+
+extern "C" bool native_bridge6_isSupported(const char* /* libpath */) {
+ return false;
+}
+
+extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge6_getAppEnv(
+ const char* /* abi */) {
+ return nullptr;
+}
+
+extern "C" bool native_bridge6_isCompatibleWith(uint32_t version) {
+ // For testing, allow 1-6, but disallow 7+.
+ return version <= 6;
+}
+
+extern "C" android::NativeBridgeSignalHandlerFn native_bridge6_getSignalHandler(int /* signal */) {
+ return nullptr;
+}
+
+extern "C" int native_bridge6_unloadLibrary(void* /* handle */) {
+ return 0;
+}
+
+extern "C" const char* native_bridge6_getError() {
+ return nullptr;
+}
+
+extern "C" bool native_bridge6_isPathSupported(const char* /* path */) {
+ return true;
+}
+
+extern "C" bool native_bridge6_initAnonymousNamespace(const char* /* public_ns_sonames */,
+ const char* /* anon_ns_library_path */) {
+ return true;
+}
+
+extern "C" android::native_bridge_namespace_t*
+native_bridge6_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_bridge6_linkNamespaces(android::native_bridge_namespace_t* /* from */,
+ android::native_bridge_namespace_t* /* to */,
+ const char* /* shared_libs_soname */) {
+ return true;
+}
+
+extern "C" void* native_bridge6_loadLibraryExt(const char* /* libpath */,
+ int /* flag */,
+ android::native_bridge_namespace_t* /* ns */) {
+ return nullptr;
+}
+
+extern "C" android::native_bridge_namespace_t* native_bridge6_getVendorNamespace() {
+ return nullptr;
+}
+
+extern "C" android::native_bridge_namespace_t* native_bridge6_getExportedNamespace(const char* /* name */) {
+ return nullptr;
+}
+
+extern "C" void native_bridge6_preZygoteFork() {
+ android::SetPreZygoteForkDone();
+}
+
+android::NativeBridgeCallbacks NativeBridgeItf{
+ // v1
+ .version = 6,
+ .initialize = &native_bridge6_initialize,
+ .loadLibrary = &native_bridge6_loadLibrary,
+ .getTrampoline = &native_bridge6_getTrampoline,
+ .isSupported = &native_bridge6_isSupported,
+ .getAppEnv = &native_bridge6_getAppEnv,
+ // v2
+ .isCompatibleWith = &native_bridge6_isCompatibleWith,
+ .getSignalHandler = &native_bridge6_getSignalHandler,
+ // v3
+ .unloadLibrary = &native_bridge6_unloadLibrary,
+ .getError = &native_bridge6_getError,
+ .isPathSupported = &native_bridge6_isPathSupported,
+ .initAnonymousNamespace = &native_bridge6_initAnonymousNamespace,
+ .createNamespace = &native_bridge6_createNamespace,
+ .linkNamespaces = &native_bridge6_linkNamespaces,
+ .loadLibraryExt = &native_bridge6_loadLibraryExt,
+ // v4
+ &native_bridge6_getVendorNamespace,
+ // v5
+ &native_bridge6_getExportedNamespace,
+ // v6
+ &native_bridge6_preZygoteFork};
diff --git a/libnativebridge/tests/NativeBridge6PreZygoteFork_lib.cpp b/libnativebridge/tests/NativeBridge6PreZygoteFork_lib.cpp
new file mode 100644
index 0000000..0da5bb6
--- /dev/null
+++ b/libnativebridge/tests/NativeBridge6PreZygoteFork_lib.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+namespace android {
+
+static bool g_pre_zygote_fork_done = false;
+
+bool IsPreZygoteForkDone() {
+ return g_pre_zygote_fork_done;
+}
+
+void SetPreZygoteForkDone() {
+ g_pre_zygote_fork_done = true;
+}
+
+} // namespace android
diff --git a/libnativebridge/tests/NativeBridge6PreZygoteFork_lib.h b/libnativebridge/tests/NativeBridge6PreZygoteFork_lib.h
new file mode 100644
index 0000000..bcbf0d3
--- /dev/null
+++ b/libnativebridge/tests/NativeBridge6PreZygoteFork_lib.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2020 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_NATIVEBRIDGE6PREZYGOTEFORK_LIB_H_
+#define ART_LIBNATIVEBRIDGE_TESTS_NATIVEBRIDGE6PREZYGOTEFORK_LIB_H_
+
+namespace android {
+
+bool IsPreZygoteForkDone();
+void SetPreZygoteForkDone();
+
+} // namespace android
+
+#endif // ART_LIBNATIVEBRIDGE_TESTS_NATIVEBRIDGE6PREZYGOTEFORK_LIB_H_
diff --git a/libnativebridge/tests/NativeBridge6PreZygoteFork_test.cpp b/libnativebridge/tests/NativeBridge6PreZygoteFork_test.cpp
new file mode 100644
index 0000000..9e348a2
--- /dev/null
+++ b/libnativebridge/tests/NativeBridge6PreZygoteFork_test.cpp
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2020 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 "NativeBridgeTest.h"
+#include "NativeBridge6PreZygoteFork_lib.h"
+
+namespace android {
+
+constexpr const char* kNativeBridgeLibrary6 = "libnativebridge6-dummy.so";
+
+TEST_F(NativeBridgeTest, V6_PreZygoteFork) {
+ // Init
+ ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary6, nullptr));
+ ASSERT_TRUE(NativeBridgeAvailable());
+ ASSERT_TRUE(PreInitializeNativeBridge(".", "isa"));
+ ASSERT_TRUE(NativeBridgeAvailable());
+ ASSERT_TRUE(InitializeNativeBridge(nullptr, nullptr));
+ ASSERT_TRUE(NativeBridgeAvailable());
+
+ ASSERT_EQ(6U, NativeBridgeGetVersion());
+
+ ASSERT_FALSE(IsPreZygoteForkDone());
+ PreZygoteForkNativeBridge();
+ ASSERT_TRUE(IsPreZygoteForkDone());
+}
+
+} // namespace android