blob: 6bcc1f5413220544ab332b9cdb9327677bfed274 [file] [log] [blame]
Andreas Gampe855564b2014-07-25 02:32:19 -07001/*
2 * Copyright (C) 2014 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// A simple implementation of the native-bridge interface.
18
19#include <algorithm>
20#include <dlfcn.h>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070021#include <jni.h>
Andreas Gampe855564b2014-07-25 02:32:19 -070022#include <vector>
23
Andreas Gampe855564b2014-07-25 02:32:19 -070024#include "stdio.h"
Andreas Gampe855564b2014-07-25 02:32:19 -070025#include "unistd.h"
Calin Juravle44a35062014-10-22 20:17:58 +010026#include "sys/stat.h"
Andreas Gampe855564b2014-07-25 02:32:19 -070027
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070028#include "base/macros.h"
Calin Juravlec8423522014-08-12 20:55:20 +010029#include "nativebridge/native_bridge.h"
Andreas Gampe855564b2014-07-25 02:32:19 -070030
Yong WUf7a68c12014-08-03 16:06:52 +080031struct NativeBridgeMethod {
32 const char* name;
33 const char* signature;
34 bool static_method;
35 void* fnPtr;
36 void* trampoline;
37};
Andreas Gampe855564b2014-07-25 02:32:19 -070038
Yong WUf7a68c12014-08-03 16:06:52 +080039static NativeBridgeMethod* find_native_bridge_method(const char *name);
Calin Juravlec8423522014-08-12 20:55:20 +010040static const android::NativeBridgeRuntimeCallbacks* gNativeBridgeArtCallbacks;
Andreas Gampe855564b2014-07-25 02:32:19 -070041
Yong WUf7a68c12014-08-03 16:06:52 +080042static jint trampoline_JNI_OnLoad(JavaVM* vm, void* reserved) {
43 JNIEnv* env = nullptr;
44 typedef jint (*FnPtr_t)(JavaVM*, void*);
45 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("JNI_OnLoad")->fnPtr);
46
47 vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
48 if (env == nullptr) {
49 return 0;
50 }
51
52 jclass klass = env->FindClass("Main");
53 if (klass != nullptr) {
54 int i, count1, count2;
55 count1 = gNativeBridgeArtCallbacks->getNativeMethodCount(env, klass);
56 std::unique_ptr<JNINativeMethod[]> methods(new JNINativeMethod[count1]);
57 if (methods == nullptr) {
58 return 0;
59 }
60 count2 = gNativeBridgeArtCallbacks->getNativeMethods(env, klass, methods.get(), count1);
61 if (count1 == count2) {
62 printf("Test ART callbacks: all JNI function number is %d.\n", count1);
63 }
64
65 for (i = 0; i < count1; i++) {
66 NativeBridgeMethod* nb_method = find_native_bridge_method(methods[i].name);
67 if (nb_method != nullptr) {
68 jmethodID mid = nullptr;
69 if (nb_method->static_method) {
70 mid = env->GetStaticMethodID(klass, methods[i].name, nb_method->signature);
71 } else {
72 mid = env->GetMethodID(klass, methods[i].name, nb_method->signature);
73 }
74 if (mid != nullptr) {
75 const char* shorty = gNativeBridgeArtCallbacks->getMethodShorty(env, mid);
76 if (strcmp(shorty, methods[i].signature) == 0) {
77 printf(" name:%s, signature:%s, shorty:%s.\n",
78 methods[i].name, nb_method->signature, shorty);
79 }
80 }
81 }
82 }
83 methods.release();
84 }
85
86 printf("%s called!\n", __FUNCTION__);
87 return fnPtr(vm, reserved);
88}
89
90static void trampoline_Java_Main_testFindClassOnAttachedNativeThread(JNIEnv* env,
91 jclass klass) {
92 typedef void (*FnPtr_t)(JNIEnv*, jclass);
93 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>
94 (find_native_bridge_method("testFindClassOnAttachedNativeThread")->fnPtr);
95 printf("%s called!\n", __FUNCTION__);
96 return fnPtr(env, klass);
97}
98
99static void trampoline_Java_Main_testFindFieldOnAttachedNativeThreadNative(JNIEnv* env,
100 jclass klass) {
101 typedef void (*FnPtr_t)(JNIEnv*, jclass);
102 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>
103 (find_native_bridge_method("testFindFieldOnAttachedNativeThreadNative")->fnPtr);
104 printf("%s called!\n", __FUNCTION__);
105 return fnPtr(env, klass);
106}
107
108static void trampoline_Java_Main_testCallStaticVoidMethodOnSubClassNative(JNIEnv* env,
109 jclass klass) {
110 typedef void (*FnPtr_t)(JNIEnv*, jclass);
111 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>
112 (find_native_bridge_method("testCallStaticVoidMethodOnSubClassNative")->fnPtr);
113 printf("%s called!\n", __FUNCTION__);
114 return fnPtr(env, klass);
115}
116
117static jobject trampoline_Java_Main_testGetMirandaMethodNative(JNIEnv* env, jclass klass) {
118 typedef jobject (*FnPtr_t)(JNIEnv*, jclass);
119 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>
120 (find_native_bridge_method("testGetMirandaMethodNative")->fnPtr);
121 printf("%s called!\n", __FUNCTION__);
122 return fnPtr(env, klass);
123}
124
125static void trampoline_Java_Main_testZeroLengthByteBuffers(JNIEnv* env, jclass klass) {
126 typedef void (*FnPtr_t)(JNIEnv*, jclass);
127 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>
128 (find_native_bridge_method("testZeroLengthByteBuffers")->fnPtr);
129 printf("%s called!\n", __FUNCTION__);
130 return fnPtr(env, klass);
131}
132
133static jbyte trampoline_Java_Main_byteMethod(JNIEnv* env, jclass klass, jbyte b1, jbyte b2,
134 jbyte b3, jbyte b4, jbyte b5, jbyte b6,
135 jbyte b7, jbyte b8, jbyte b9, jbyte b10) {
136 typedef jbyte (*FnPtr_t)(JNIEnv*, jclass, jbyte, jbyte, jbyte, jbyte, jbyte,
137 jbyte, jbyte, jbyte, jbyte, jbyte);
138 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("byteMethod")->fnPtr);
139 printf("%s called!\n", __FUNCTION__);
140 return fnPtr(env, klass, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10);
141}
142
143static jshort trampoline_Java_Main_shortMethod(JNIEnv* env, jclass klass, jshort s1, jshort s2,
144 jshort s3, jshort s4, jshort s5, jshort s6,
145 jshort s7, jshort s8, jshort s9, jshort s10) {
146 typedef jshort (*FnPtr_t)(JNIEnv*, jclass, jshort, jshort, jshort, jshort, jshort,
147 jshort, jshort, jshort, jshort, jshort);
148 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("shortMethod")->fnPtr);
149 printf("%s called!\n", __FUNCTION__);
150 return fnPtr(env, klass, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10);
151}
152
153static jboolean trampoline_Java_Main_booleanMethod(JNIEnv* env, jclass klass, jboolean b1,
154 jboolean b2, jboolean b3, jboolean b4,
155 jboolean b5, jboolean b6, jboolean b7,
156 jboolean b8, jboolean b9, jboolean b10) {
157 typedef jboolean (*FnPtr_t)(JNIEnv*, jclass, jboolean, jboolean, jboolean, jboolean, jboolean,
158 jboolean, jboolean, jboolean, jboolean, jboolean);
159 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("booleanMethod")->fnPtr);
160 printf("%s called!\n", __FUNCTION__);
161 return fnPtr(env, klass, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10);
162}
163
164static jchar trampoline_Java_Main_charMethod(JNIEnv* env, jclass klass, jchar c1, jchar c2,
165 jchar c3, jchar c4, jchar c5, jchar c6,
166 jchar c7, jchar c8, jchar c9, jchar c10) {
167 typedef jchar (*FnPtr_t)(JNIEnv*, jclass, jchar, jchar, jchar, jchar, jchar,
168 jchar, jchar, jchar, jchar, jchar);
169 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("charMethod")->fnPtr);
170 printf("%s called!\n", __FUNCTION__);
171 return fnPtr(env, klass, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
172}
173
174NativeBridgeMethod gNativeBridgeMethods[] = {
175 { "JNI_OnLoad", "", true, nullptr,
176 reinterpret_cast<void*>(trampoline_JNI_OnLoad) },
177 { "booleanMethod", "(ZZZZZZZZZZ)Z", true, nullptr,
178 reinterpret_cast<void*>(trampoline_Java_Main_booleanMethod) },
179 { "byteMethod", "(BBBBBBBBBB)B", true, nullptr,
180 reinterpret_cast<void*>(trampoline_Java_Main_byteMethod) },
181 { "charMethod", "(CCCCCCCCCC)C", true, nullptr,
182 reinterpret_cast<void*>(trampoline_Java_Main_charMethod) },
183 { "shortMethod", "(SSSSSSSSSS)S", true, nullptr,
184 reinterpret_cast<void*>(trampoline_Java_Main_shortMethod) },
185 { "testCallStaticVoidMethodOnSubClassNative", "()V", true, nullptr,
186 reinterpret_cast<void*>(trampoline_Java_Main_testCallStaticVoidMethodOnSubClassNative) },
187 { "testFindClassOnAttachedNativeThread", "()V", true, nullptr,
188 reinterpret_cast<void*>(trampoline_Java_Main_testFindClassOnAttachedNativeThread) },
189 { "testFindFieldOnAttachedNativeThreadNative", "()V", true, nullptr,
190 reinterpret_cast<void*>(trampoline_Java_Main_testFindFieldOnAttachedNativeThreadNative) },
191 { "testGetMirandaMethodNative", "()Ljava/lang/reflect/Method;", true, nullptr,
192 reinterpret_cast<void*>(trampoline_Java_Main_testGetMirandaMethodNative) },
193 { "testZeroLengthByteBuffers", "()V", true, nullptr,
194 reinterpret_cast<void*>(trampoline_Java_Main_testZeroLengthByteBuffers) },
195};
196
197static NativeBridgeMethod* find_native_bridge_method(const char *name) {
198 const char* pname = name;
199 if (strncmp(name, "Java_Main_", 10) == 0) {
200 pname += 10;
201 }
202
203 for (size_t i = 0; i < sizeof(gNativeBridgeMethods) / sizeof(gNativeBridgeMethods[0]); i++) {
204 if (strcmp(pname, gNativeBridgeMethods[i].name) == 0) {
205 return &gNativeBridgeMethods[i];
206 }
207 }
208 return nullptr;
209}
Andreas Gampe855564b2014-07-25 02:32:19 -0700210
211// NativeBridgeCallbacks implementations
jgu21a6da74e2014-09-10 06:57:17 -0400212extern "C" bool native_bridge_initialize(const android::NativeBridgeRuntimeCallbacks* art_cbs,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700213 const char* app_code_cache_dir,
214 const char* isa ATTRIBUTE_UNUSED) {
Calin Juravle44a35062014-10-22 20:17:58 +0100215 struct stat st;
216 if ((app_code_cache_dir != nullptr)
217 && (stat(app_code_cache_dir, &st) == 0)
218 && S_ISDIR(st.st_mode)) {
219 printf("Code cache exists: '%s'.\n", app_code_cache_dir);
220 }
Yong WUf7a68c12014-08-03 16:06:52 +0800221 if (art_cbs != nullptr) {
222 gNativeBridgeArtCallbacks = art_cbs;
223 printf("Native bridge initialized.\n");
224 }
Andreas Gampe855564b2014-07-25 02:32:19 -0700225 return true;
226}
227
228extern "C" void* native_bridge_loadLibrary(const char* libpath, int flag) {
229 size_t len = strlen(libpath);
230 char* tmp = new char[len + 10];
231 strncpy(tmp, libpath, len);
232 tmp[len - 3] = '2';
233 tmp[len - 2] = '.';
234 tmp[len - 1] = 's';
235 tmp[len] = 'o';
236 tmp[len + 1] = 0;
237 void* handle = dlopen(tmp, flag);
238 delete[] tmp;
239
240 if (handle == nullptr) {
241 printf("Handle = nullptr!\n");
242 printf("Was looking for %s.\n", libpath);
243 printf("Error = %s.\n", dlerror());
244 char cwd[1024];
245 if (getcwd(cwd, sizeof(cwd)) != nullptr) {
246 printf("Current working dir: %s\n", cwd);
247 }
248 }
249 return handle;
250}
251
252extern "C" void* native_bridge_getTrampoline(void* handle, const char* name, const char* shorty,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700253 uint32_t len ATTRIBUTE_UNUSED) {
Yong WUf7a68c12014-08-03 16:06:52 +0800254 printf("Getting trampoline for %s with shorty %s.\n", name, shorty);
Andreas Gampe855564b2014-07-25 02:32:19 -0700255
256 // The name here is actually the JNI name, so we can directly do the lookup.
257 void* sym = dlsym(handle, name);
Yong WUf7a68c12014-08-03 16:06:52 +0800258 NativeBridgeMethod* method = find_native_bridge_method(name);
259 if (method == nullptr)
260 return nullptr;
261 method->fnPtr = sym;
Andreas Gampe855564b2014-07-25 02:32:19 -0700262
Yong WUf7a68c12014-08-03 16:06:52 +0800263 return method->trampoline;
Andreas Gampe855564b2014-07-25 02:32:19 -0700264}
265
266extern "C" bool native_bridge_isSupported(const char* libpath) {
267 printf("Checking for support.\n");
268
269 if (libpath == nullptr) {
270 return false;
271 }
272 // We don't want to hijack javacore. So we should get libarttest...
273 return strcmp(libpath, "libjavacore.so") != 0;
274}
275
jgu21a6da74e2014-09-10 06:57:17 -0400276namespace android {
277
278// Environment values required by the apps running with native bridge.
279struct NativeBridgeRuntimeValues {
280 const char* os_arch;
281 const char* cpu_abi;
282 const char* cpu_abi2;
283 const char* *supported_abis;
284 int32_t abi_count;
285};
286
287} // namespace android
288
289const char* supported_abis[] = {
290 "supported1", "supported2", "supported3"
291};
292
293const struct android::NativeBridgeRuntimeValues nb_env {
294 .os_arch = "os.arch",
295 .cpu_abi = "cpu_abi",
296 .cpu_abi2 = "cpu_abi2",
297 .supported_abis = supported_abis,
298 .abi_count = 3
299};
300
301extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge_getAppEnv(
302 const char* abi) {
303 printf("Checking for getEnvValues.\n");
304
305 if (abi == nullptr) {
306 return nullptr;
307 }
308
309 return &nb_env;
310}
311
Calin Juravlec8423522014-08-12 20:55:20 +0100312// "NativeBridgeItf" is effectively an API (it is the name of the symbol that will be loaded
313// by the native bridge library).
314android::NativeBridgeCallbacks NativeBridgeItf {
jgu21a6da74e2014-09-10 06:57:17 -0400315 .version = 1,
Andreas Gampe855564b2014-07-25 02:32:19 -0700316 .initialize = &native_bridge_initialize,
317 .loadLibrary = &native_bridge_loadLibrary,
318 .getTrampoline = &native_bridge_getTrampoline,
jgu21a6da74e2014-09-10 06:57:17 -0400319 .isSupported = &native_bridge_isSupported,
320 .getAppEnv = &native_bridge_getAppEnv
Andreas Gampe855564b2014-07-25 02:32:19 -0700321};