1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // An implementation of the native-bridge interface for testing.
18 
19 #include "nativebridge/native_bridge.h"
20 
21 #include "NativeBridge6PreZygoteFork_lib.h"
22 
23 // NativeBridgeCallbacks implementations
native_bridge6_initialize(const android::NativeBridgeRuntimeCallbacks *,const char *,const char *)24 extern "C" bool native_bridge6_initialize(
25                       const android::NativeBridgeRuntimeCallbacks* /* art_cbs */,
26                       const char* /* app_code_cache_dir */,
27                       const char* /* isa */) {
28   return true;
29 }
30 
native_bridge6_loadLibrary(const char *,int)31 extern "C" void* native_bridge6_loadLibrary(const char* /* libpath */, int /* flag */) {
32   return nullptr;
33 }
34 
native_bridge6_getTrampoline(void *,const char *,const char *,uint32_t)35 extern "C" void* native_bridge6_getTrampoline(void* /* handle */, const char* /* name */,
36                                              const char* /* shorty */, uint32_t /* len */) {
37   return nullptr;
38 }
39 
native_bridge6_isSupported(const char *)40 extern "C" bool native_bridge6_isSupported(const char* /* libpath */) {
41   return false;
42 }
43 
native_bridge6_getAppEnv(const char *)44 extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge6_getAppEnv(
45     const char* /* abi */) {
46   return nullptr;
47 }
48 
native_bridge6_isCompatibleWith(uint32_t version)49 extern "C" bool native_bridge6_isCompatibleWith(uint32_t version) {
50   // For testing, allow 1-6, but disallow 7+.
51   return version <= 6;
52 }
53 
native_bridge6_getSignalHandler(int)54 extern "C" android::NativeBridgeSignalHandlerFn native_bridge6_getSignalHandler(int /* signal */) {
55   return nullptr;
56 }
57 
native_bridge6_unloadLibrary(void *)58 extern "C" int native_bridge6_unloadLibrary(void* /* handle */) {
59   return 0;
60 }
61 
native_bridge6_getError()62 extern "C" const char* native_bridge6_getError() {
63   return nullptr;
64 }
65 
native_bridge6_isPathSupported(const char *)66 extern "C" bool native_bridge6_isPathSupported(const char* /* path */) {
67   return true;
68 }
69 
native_bridge6_initAnonymousNamespace(const char *,const char *)70 extern "C" bool native_bridge6_initAnonymousNamespace(const char* /* public_ns_sonames */,
71                                                       const char* /* anon_ns_library_path */) {
72   return true;
73 }
74 
75 extern "C" android::native_bridge_namespace_t*
native_bridge6_createNamespace(const char *,const char *,const char *,uint64_t,const char *,android::native_bridge_namespace_t *)76 native_bridge6_createNamespace(const char* /* name */,
77                                const char* /* ld_library_path */,
78                                const char* /* default_library_path */,
79                                uint64_t /* type */,
80                                const char* /* permitted_when_isolated_path */,
81                                android::native_bridge_namespace_t* /* parent_ns */) {
82   return nullptr;
83 }
84 
native_bridge6_linkNamespaces(android::native_bridge_namespace_t *,android::native_bridge_namespace_t *,const char *)85 extern "C" bool native_bridge6_linkNamespaces(android::native_bridge_namespace_t* /* from */,
86                                               android::native_bridge_namespace_t* /* to */,
87                                               const char* /* shared_libs_soname */) {
88   return true;
89 }
90 
native_bridge6_loadLibraryExt(const char *,int,android::native_bridge_namespace_t *)91 extern "C" void* native_bridge6_loadLibraryExt(const char* /* libpath */,
92                                                int /* flag */,
93                                                android::native_bridge_namespace_t* /* ns */) {
94   return nullptr;
95 }
96 
native_bridge6_getVendorNamespace()97 extern "C" android::native_bridge_namespace_t* native_bridge6_getVendorNamespace() {
98   return nullptr;
99 }
100 
native_bridge6_getExportedNamespace(const char *)101 extern "C" android::native_bridge_namespace_t* native_bridge6_getExportedNamespace(const char* /* name */) {
102   return nullptr;
103 }
104 
native_bridge6_preZygoteFork()105 extern "C" void native_bridge6_preZygoteFork() {
106   android::SetPreZygoteForkDone();
107 }
108 
109 android::NativeBridgeCallbacks NativeBridgeItf{
110     // v1
111     .version = 6,
112     .initialize = &native_bridge6_initialize,
113     .loadLibrary = &native_bridge6_loadLibrary,
114     .getTrampoline = &native_bridge6_getTrampoline,
115     .isSupported = &native_bridge6_isSupported,
116     .getAppEnv = &native_bridge6_getAppEnv,
117     // v2
118     .isCompatibleWith = &native_bridge6_isCompatibleWith,
119     .getSignalHandler = &native_bridge6_getSignalHandler,
120     // v3
121     .unloadLibrary = &native_bridge6_unloadLibrary,
122     .getError = &native_bridge6_getError,
123     .isPathSupported = &native_bridge6_isPathSupported,
124     .initAnonymousNamespace = &native_bridge6_initAnonymousNamespace,
125     .createNamespace = &native_bridge6_createNamespace,
126     .linkNamespaces = &native_bridge6_linkNamespaces,
127     .loadLibraryExt = &native_bridge6_loadLibraryExt,
128     // v4
129     &native_bridge6_getVendorNamespace,
130     // v5
131     &native_bridge6_getExportedNamespace,
132     // v6
133     &native_bridge6_preZygoteFork};
134