blob: db2fc9b6e9edddeb42a639e3fdc7cb0ac45a5c0d [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
Jeff Hao848f70a2014-01-15 13:49:50 -0800125static void trampoline_Java_Main_testNewStringObject(JNIEnv* env, jclass klass) {
126 typedef void (*FnPtr_t)(JNIEnv*, jclass);
127 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>
128 (find_native_bridge_method("testNewStringObject")->fnPtr);
129 printf("%s called!\n", __FUNCTION__);
130 return fnPtr(env, klass);
131}
132
Yong WUf7a68c12014-08-03 16:06:52 +0800133static void trampoline_Java_Main_testZeroLengthByteBuffers(JNIEnv* env, jclass klass) {
134 typedef void (*FnPtr_t)(JNIEnv*, jclass);
135 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>
136 (find_native_bridge_method("testZeroLengthByteBuffers")->fnPtr);
137 printf("%s called!\n", __FUNCTION__);
138 return fnPtr(env, klass);
139}
140
141static jbyte trampoline_Java_Main_byteMethod(JNIEnv* env, jclass klass, jbyte b1, jbyte b2,
142 jbyte b3, jbyte b4, jbyte b5, jbyte b6,
143 jbyte b7, jbyte b8, jbyte b9, jbyte b10) {
144 typedef jbyte (*FnPtr_t)(JNIEnv*, jclass, jbyte, jbyte, jbyte, jbyte, jbyte,
145 jbyte, jbyte, jbyte, jbyte, jbyte);
146 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("byteMethod")->fnPtr);
147 printf("%s called!\n", __FUNCTION__);
148 return fnPtr(env, klass, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10);
149}
150
151static jshort trampoline_Java_Main_shortMethod(JNIEnv* env, jclass klass, jshort s1, jshort s2,
152 jshort s3, jshort s4, jshort s5, jshort s6,
153 jshort s7, jshort s8, jshort s9, jshort s10) {
154 typedef jshort (*FnPtr_t)(JNIEnv*, jclass, jshort, jshort, jshort, jshort, jshort,
155 jshort, jshort, jshort, jshort, jshort);
156 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("shortMethod")->fnPtr);
157 printf("%s called!\n", __FUNCTION__);
158 return fnPtr(env, klass, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10);
159}
160
161static jboolean trampoline_Java_Main_booleanMethod(JNIEnv* env, jclass klass, jboolean b1,
162 jboolean b2, jboolean b3, jboolean b4,
163 jboolean b5, jboolean b6, jboolean b7,
164 jboolean b8, jboolean b9, jboolean b10) {
165 typedef jboolean (*FnPtr_t)(JNIEnv*, jclass, jboolean, jboolean, jboolean, jboolean, jboolean,
166 jboolean, jboolean, jboolean, jboolean, jboolean);
167 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("booleanMethod")->fnPtr);
168 printf("%s called!\n", __FUNCTION__);
169 return fnPtr(env, klass, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10);
170}
171
172static jchar trampoline_Java_Main_charMethod(JNIEnv* env, jclass klass, jchar c1, jchar c2,
173 jchar c3, jchar c4, jchar c5, jchar c6,
174 jchar c7, jchar c8, jchar c9, jchar c10) {
175 typedef jchar (*FnPtr_t)(JNIEnv*, jclass, jchar, jchar, jchar, jchar, jchar,
176 jchar, jchar, jchar, jchar, jchar);
177 FnPtr_t fnPtr = reinterpret_cast<FnPtr_t>(find_native_bridge_method("charMethod")->fnPtr);
178 printf("%s called!\n", __FUNCTION__);
179 return fnPtr(env, klass, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);
180}
181
182NativeBridgeMethod gNativeBridgeMethods[] = {
183 { "JNI_OnLoad", "", true, nullptr,
184 reinterpret_cast<void*>(trampoline_JNI_OnLoad) },
185 { "booleanMethod", "(ZZZZZZZZZZ)Z", true, nullptr,
186 reinterpret_cast<void*>(trampoline_Java_Main_booleanMethod) },
187 { "byteMethod", "(BBBBBBBBBB)B", true, nullptr,
188 reinterpret_cast<void*>(trampoline_Java_Main_byteMethod) },
189 { "charMethod", "(CCCCCCCCCC)C", true, nullptr,
190 reinterpret_cast<void*>(trampoline_Java_Main_charMethod) },
191 { "shortMethod", "(SSSSSSSSSS)S", true, nullptr,
192 reinterpret_cast<void*>(trampoline_Java_Main_shortMethod) },
193 { "testCallStaticVoidMethodOnSubClassNative", "()V", true, nullptr,
194 reinterpret_cast<void*>(trampoline_Java_Main_testCallStaticVoidMethodOnSubClassNative) },
195 { "testFindClassOnAttachedNativeThread", "()V", true, nullptr,
196 reinterpret_cast<void*>(trampoline_Java_Main_testFindClassOnAttachedNativeThread) },
197 { "testFindFieldOnAttachedNativeThreadNative", "()V", true, nullptr,
198 reinterpret_cast<void*>(trampoline_Java_Main_testFindFieldOnAttachedNativeThreadNative) },
199 { "testGetMirandaMethodNative", "()Ljava/lang/reflect/Method;", true, nullptr,
200 reinterpret_cast<void*>(trampoline_Java_Main_testGetMirandaMethodNative) },
Jeff Hao848f70a2014-01-15 13:49:50 -0800201 { "testNewStringObject", "()V", true, nullptr,
202 reinterpret_cast<void*>(trampoline_Java_Main_testNewStringObject) },
Yong WUf7a68c12014-08-03 16:06:52 +0800203 { "testZeroLengthByteBuffers", "()V", true, nullptr,
204 reinterpret_cast<void*>(trampoline_Java_Main_testZeroLengthByteBuffers) },
205};
206
207static NativeBridgeMethod* find_native_bridge_method(const char *name) {
208 const char* pname = name;
209 if (strncmp(name, "Java_Main_", 10) == 0) {
210 pname += 10;
211 }
212
213 for (size_t i = 0; i < sizeof(gNativeBridgeMethods) / sizeof(gNativeBridgeMethods[0]); i++) {
214 if (strcmp(pname, gNativeBridgeMethods[i].name) == 0) {
215 return &gNativeBridgeMethods[i];
216 }
217 }
218 return nullptr;
219}
Andreas Gampe855564b2014-07-25 02:32:19 -0700220
221// NativeBridgeCallbacks implementations
jgu21a6da74e2014-09-10 06:57:17 -0400222extern "C" bool native_bridge_initialize(const android::NativeBridgeRuntimeCallbacks* art_cbs,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700223 const char* app_code_cache_dir,
224 const char* isa ATTRIBUTE_UNUSED) {
Calin Juravle44a35062014-10-22 20:17:58 +0100225 struct stat st;
226 if ((app_code_cache_dir != nullptr)
227 && (stat(app_code_cache_dir, &st) == 0)
228 && S_ISDIR(st.st_mode)) {
229 printf("Code cache exists: '%s'.\n", app_code_cache_dir);
230 }
Yong WUf7a68c12014-08-03 16:06:52 +0800231 if (art_cbs != nullptr) {
232 gNativeBridgeArtCallbacks = art_cbs;
233 printf("Native bridge initialized.\n");
234 }
Andreas Gampe855564b2014-07-25 02:32:19 -0700235 return true;
236}
237
238extern "C" void* native_bridge_loadLibrary(const char* libpath, int flag) {
239 size_t len = strlen(libpath);
240 char* tmp = new char[len + 10];
241 strncpy(tmp, libpath, len);
242 tmp[len - 3] = '2';
243 tmp[len - 2] = '.';
244 tmp[len - 1] = 's';
245 tmp[len] = 'o';
246 tmp[len + 1] = 0;
247 void* handle = dlopen(tmp, flag);
248 delete[] tmp;
249
250 if (handle == nullptr) {
251 printf("Handle = nullptr!\n");
252 printf("Was looking for %s.\n", libpath);
253 printf("Error = %s.\n", dlerror());
254 char cwd[1024];
255 if (getcwd(cwd, sizeof(cwd)) != nullptr) {
256 printf("Current working dir: %s\n", cwd);
257 }
258 }
259 return handle;
260}
261
262extern "C" void* native_bridge_getTrampoline(void* handle, const char* name, const char* shorty,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700263 uint32_t len ATTRIBUTE_UNUSED) {
Yong WUf7a68c12014-08-03 16:06:52 +0800264 printf("Getting trampoline for %s with shorty %s.\n", name, shorty);
Andreas Gampe855564b2014-07-25 02:32:19 -0700265
266 // The name here is actually the JNI name, so we can directly do the lookup.
267 void* sym = dlsym(handle, name);
Yong WUf7a68c12014-08-03 16:06:52 +0800268 NativeBridgeMethod* method = find_native_bridge_method(name);
269 if (method == nullptr)
270 return nullptr;
271 method->fnPtr = sym;
Andreas Gampe855564b2014-07-25 02:32:19 -0700272
Yong WUf7a68c12014-08-03 16:06:52 +0800273 return method->trampoline;
Andreas Gampe855564b2014-07-25 02:32:19 -0700274}
275
276extern "C" bool native_bridge_isSupported(const char* libpath) {
277 printf("Checking for support.\n");
278
279 if (libpath == nullptr) {
280 return false;
281 }
282 // We don't want to hijack javacore. So we should get libarttest...
283 return strcmp(libpath, "libjavacore.so") != 0;
284}
285
jgu21a6da74e2014-09-10 06:57:17 -0400286namespace android {
287
288// Environment values required by the apps running with native bridge.
289struct NativeBridgeRuntimeValues {
290 const char* os_arch;
291 const char* cpu_abi;
292 const char* cpu_abi2;
293 const char* *supported_abis;
294 int32_t abi_count;
295};
296
297} // namespace android
298
299const char* supported_abis[] = {
300 "supported1", "supported2", "supported3"
301};
302
303const struct android::NativeBridgeRuntimeValues nb_env {
304 .os_arch = "os.arch",
305 .cpu_abi = "cpu_abi",
306 .cpu_abi2 = "cpu_abi2",
307 .supported_abis = supported_abis,
308 .abi_count = 3
309};
310
311extern "C" const struct android::NativeBridgeRuntimeValues* native_bridge_getAppEnv(
312 const char* abi) {
313 printf("Checking for getEnvValues.\n");
314
315 if (abi == nullptr) {
316 return nullptr;
317 }
318
319 return &nb_env;
320}
321
Calin Juravlec8423522014-08-12 20:55:20 +0100322// "NativeBridgeItf" is effectively an API (it is the name of the symbol that will be loaded
323// by the native bridge library).
324android::NativeBridgeCallbacks NativeBridgeItf {
jgu21a6da74e2014-09-10 06:57:17 -0400325 .version = 1,
Andreas Gampe855564b2014-07-25 02:32:19 -0700326 .initialize = &native_bridge_initialize,
327 .loadLibrary = &native_bridge_loadLibrary,
328 .getTrampoline = &native_bridge_getTrampoline,
jgu21a6da74e2014-09-10 06:57:17 -0400329 .isSupported = &native_bridge_isSupported,
Andreas Gampe540cc3d2015-05-20 18:01:30 -0700330 .getAppEnv = &native_bridge_getAppEnv,
331 .isCompatibleWith = nullptr,
332 .getSignalHandler = nullptr
Andreas Gampe855564b2014-07-25 02:32:19 -0700333};