blob: 15cec90a70abc1612988fe6bbe40b17a0e0143f3 [file] [log] [blame]
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +01001/*
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/*
18 * Services that OpenJDK expects the VM to provide.
19 */
20#include<stdio.h>
21#include <dlfcn.h>
22#include <limits.h>
23#include <unistd.h>
24
25#include "common_throws.h"
26#include "gc/heap.h"
27#include "thread.h"
28#include "thread_list.h"
29#include "runtime.h"
30#include "handle_scope-inl.h"
31#include "scoped_thread_state_change.h"
32#include "ScopedUtfChars.h"
33#include "mirror/class_loader.h"
34#include "verify_object-inl.h"
35#include "base/logging.h"
36#include "base/macros.h"
Narayan Kamatha0cf5a62015-09-07 11:41:37 +010037#include "../../libcore/ojluni/src/main/native/jvm.h" // TODO(narayan): fix it
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010038#include "jni_internal.h"
39#include "mirror/string-inl.h"
40#include "scoped_fast_native_object_access.h"
41#include "ScopedLocalRef.h"
42#include <sys/time.h>
43#include <sys/socket.h>
44#include <sys/ioctl.h>
45
Dmitriy Ivanov3e381722015-11-23 17:40:11 -080046#ifdef __ANDROID__
47// This function is provided by android linker.
48extern "C" void android_update_LD_LIBRARY_PATH(const char* ld_library_path);
49#endif // __ANDROID__
50
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010051#undef LOG_TAG
52#define LOG_TAG "artopenjdx"
53
Narayan Kamathd1ef4362015-11-12 11:49:06 +000054using art::DEBUG;
55using art::WARNING;
56using art::VERBOSE;
57using art::INFO;
58using art::ERROR;
59using art::FATAL;
60
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010061/* posix open() with extensions; used by e.g. ZipFile */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +010062JNIEXPORT jint JVM_Open(const char* fname, jint flags, jint mode) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010063 LOG(DEBUG) << "JVM_Open fname='" << fname << "', flags=" << flags << ", mode=" << mode;
64
65 /*
66 * The call is expected to handle JVM_O_DELETE, which causes the file
67 * to be removed after it is opened. Also, some code seems to
68 * want the special return value JVM_EEXIST if the file open fails
69 * due to O_EXCL.
70 */
71 int fd = TEMP_FAILURE_RETRY(open(fname, flags & ~JVM_O_DELETE, mode));
72 if (fd < 0) {
73 int err = errno;
74 LOG(DEBUG) << "open(" << fname << ") failed: " << strerror(errno);
75 if (err == EEXIST) {
76 return JVM_EEXIST;
77 } else {
78 return -1;
79 }
80 }
81
82 if (flags & JVM_O_DELETE) {
83 LOG(DEBUG) << "Deleting '" << fname << "' after open\n";
84 if (unlink(fname) != 0) {
85 LOG(WARNING) << "Post-open deletion of '" << fname << "' failed: " << strerror(errno);
86 }
87 /* ignore */
88 }
89
90 LOG(VERBOSE) << "open(" << fname << ") --> " << fd;
91 return fd;
92}
93
94/* posix close() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +010095JNIEXPORT jint JVM_Close(jint fd) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010096 LOG(DEBUG) << "JVM_Close fd=" << fd;
97 // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR
98 return close(fd);
99}
100
101/* posix read() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100102JNIEXPORT jint JVM_Read(jint fd, char* buf, jint nbytes) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100103 LOG(DEBUG) << "JVM_Read fd=" << fd << ", buf='" << buf << "', nbytes=" << nbytes;
104 return TEMP_FAILURE_RETRY(read(fd, buf, nbytes));
105}
106
107/* posix write(); is used to write messages to stderr */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100108JNIEXPORT jint JVM_Write(jint fd, char* buf, jint nbytes) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100109 LOG(DEBUG) << "JVM_Write fd=" << fd << ", buf='" << buf << "', nbytes=" << nbytes;
110 return TEMP_FAILURE_RETRY(write(fd, buf, nbytes));
111}
112
113/* posix lseek() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100114JNIEXPORT jlong JVM_Lseek(jint fd, jlong offset, jint whence) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100115 LOG(DEBUG) << "JVM_Lseek fd=" << fd << ", offset=" << offset << ", whence=" << whence;
116 return TEMP_FAILURE_RETRY(lseek(fd, offset, whence));
117}
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100118
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100119/*
120 * "raw monitors" seem to be expected to behave like non-recursive pthread
121 * mutexes. They're used by ZipFile.
122 */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100123JNIEXPORT void* JVM_RawMonitorCreate(void) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100124 LOG(DEBUG) << "JVM_RawMonitorCreate";
125 pthread_mutex_t* newMutex =
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100126 reinterpret_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100127 pthread_mutex_init(newMutex, NULL);
128 return newMutex;
129}
130
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100131JNIEXPORT void JVM_RawMonitorDestroy(void* mon) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100132 LOG(DEBUG) << "JVM_RawMonitorDestroy mon=" << mon;
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100133 pthread_mutex_destroy(reinterpret_cast<pthread_mutex_t*>(mon));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100134}
135
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100136JNIEXPORT jint JVM_RawMonitorEnter(void* mon) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100137 LOG(DEBUG) << "JVM_RawMonitorEnter mon=" << mon;
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100138 return pthread_mutex_lock(reinterpret_cast<pthread_mutex_t*>(mon));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100139}
140
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100141JNIEXPORT void JVM_RawMonitorExit(void* mon) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100142 LOG(DEBUG) << "JVM_RawMonitorExit mon=" << mon;
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100143 pthread_mutex_unlock(reinterpret_cast<pthread_mutex_t*>(mon));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100144}
145
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100146JNIEXPORT char* JVM_NativePath(char* path) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100147 LOG(DEBUG) << "JVM_NativePath path='" << path << "'";
148 return path;
149}
150
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100151JNIEXPORT jint JVM_GetLastErrorString(char* buf, int len) {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000152#if defined(__GLIBC__) || defined(__BIONIC__)
153 int err = errno; // grab before JVM_TRACE can trash it
154 LOG(DEBUG) << "JVM_GetLastErrorString buf=" << buf << ", len=" << len;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100155
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000156 if (len == 0) {
157 return 0;
158 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100159
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000160 char* result = strerror_r(err, buf, len);
161 if (result != buf) {
162 strncpy(buf, result, len);
163 buf[len - 1] = '\0';
164 }
165
166 return strlen(buf);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100167#else
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000168 UNUSED(buf);
169 UNUSED(len);
170 return -1;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100171#endif
172}
173
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100174JNIEXPORT int jio_fprintf(FILE* fp, const char* fmt, ...) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100175 va_list args;
176
177 va_start(args, fmt);
178 int len = jio_vfprintf(fp, fmt, args);
179 va_end(args);
180
181 return len;
182}
183
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100184JNIEXPORT int jio_vfprintf(FILE* fp, const char* fmt, va_list args) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100185 assert(fp != NULL);
186 return vfprintf(fp, fmt, args);
187}
188
189/* posix fsync() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100190JNIEXPORT jint JVM_Sync(jint fd) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100191 LOG(DEBUG) << "JVM_Sync fd=" << fd;
192 return TEMP_FAILURE_RETRY(fsync(fd));
193}
194
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100195JNIEXPORT void* JVM_FindLibraryEntry(void* handle, const char* name) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100196 LOG(DEBUG) << "JVM_FindLibraryEntry handle=" << handle << " name=" << name;
Przemyslaw Szczepaniak67d39ad2015-07-03 13:54:00 +0100197 return dlsym(handle, name);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100198}
199
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100200JNIEXPORT jlong JVM_CurrentTimeMillis(JNIEnv* env, jclass clazz ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100201 LOG(DEBUG) << "JVM_CurrentTimeMillis env=" << env;
202 struct timeval tv;
203
204 gettimeofday(&tv, (struct timezone *) NULL);
205 jlong when = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
206 return when;
207}
208
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100209JNIEXPORT jint JVM_Socket(jint domain, jint type, jint protocol) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100210 LOG(DEBUG) << "JVM_Socket domain=" << domain << ", type=" << type << ", protocol=" << protocol;
211
212 return TEMP_FAILURE_RETRY(socket(domain, type, protocol));
213}
214
215JNIEXPORT jint JVM_InitializeSocketLibrary() {
216 return 0;
217}
218
219int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100220 if ((intptr_t)count <= 0) return -1;
221 return vsnprintf(str, count, fmt, args);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100222}
223
224int jio_snprintf(char *str, size_t count, const char *fmt, ...) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100225 va_list args;
226 int len;
227 va_start(args, fmt);
228 len = jio_vsnprintf(str, count, fmt, args);
229 va_end(args);
230 return len;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100231}
232
233JNIEXPORT jint JVM_SetSockOpt(jint fd, int level, int optname,
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100234 const char* optval, int optlen) {
235 LOG(DEBUG) << "JVM_SetSockOpt fd=" << fd << ", level=" << level << ", optname=" << optname
236 << ", optval=" << optval << ", optlen=" << optlen;
237 return TEMP_FAILURE_RETRY(setsockopt(fd, level, optname, optval, optlen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100238}
239
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100240JNIEXPORT jint JVM_SocketShutdown(jint fd, jint howto) {
241 LOG(DEBUG) << "JVM_SocketShutdown fd=" << fd << ", howto=" << howto;
242 return TEMP_FAILURE_RETRY(shutdown(fd, howto));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100243}
244
245JNIEXPORT jint JVM_GetSockOpt(jint fd, int level, int optname, char* optval,
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100246 int* optlen) {
247 LOG(DEBUG) << "JVM_GetSockOpt fd=" << fd << ", level=" << level << ", optname=" << optname
248 << ", optval=" << optval << ", optlen=" << optlen;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100249
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100250 socklen_t len = *optlen;
251 int cc = TEMP_FAILURE_RETRY(getsockopt(fd, level, optname, optval, &len));
252 *optlen = len;
253 return cc;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100254}
255
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100256JNIEXPORT jint JVM_GetSockName(jint fd, struct sockaddr* addr, int* addrlen) {
257 LOG(DEBUG) << "JVM_GetSockName fd=" << fd << ", addr=" << addr << ", addrlen=" << addrlen;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100258
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100259 socklen_t len = *addrlen;
260 int cc = TEMP_FAILURE_RETRY(getsockname(fd, addr, &len));
261 *addrlen = len;
262 return cc;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100263}
264
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100265JNIEXPORT jint JVM_SocketAvailable(jint fd, jint* result) {
266 LOG(DEBUG) << "JVM_SocketAvailable fd=" << fd << ", result=" << result;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100267
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100268 if (TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, result)) < 0) {
269 LOG(DEBUG) << "ioctl(" << fd << ", FIONREAD) failed: " << strerror(errno);
270 return JNI_FALSE;
271 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100272
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100273 return JNI_TRUE;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100274}
275
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100276JNIEXPORT jint JVM_Send(jint fd, char* buf, jint nBytes, jint flags) {
277 LOG(DEBUG) << "JVM_Send fd=" << fd << ", buf=" << buf << ", nBytes="
278 << nBytes << ", flags=" << flags;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100279
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100280 return TEMP_FAILURE_RETRY(send(fd, buf, nBytes, flags));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100281}
282
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100283JNIEXPORT jint JVM_SocketClose(jint fd) {
284 LOG(DEBUG) << "JVM_SocketClose fd=" << fd;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100285
286 // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100287 return close(fd);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100288}
289
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100290JNIEXPORT jint JVM_Listen(jint fd, jint count) {
291 LOG(DEBUG) << "JVM_Listen fd=" << fd << ", count=" << count;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100292
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100293 return TEMP_FAILURE_RETRY(listen(fd, count));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100294}
295
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100296JNIEXPORT jint JVM_Connect(jint fd, struct sockaddr* addr, jint addrlen) {
297 LOG(DEBUG) << "JVM_Connect fd=" << fd << ", addr=" << addr << ", addrlen=" << addrlen;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100298
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100299 return TEMP_FAILURE_RETRY(connect(fd, addr, addrlen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100300}
301
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100302JNIEXPORT int JVM_GetHostName(char* name, int namelen) {
303 LOG(DEBUG) << "JVM_GetHostName name=" << name << ", namelen=" << namelen;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100304
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100305 return TEMP_FAILURE_RETRY(gethostname(name, namelen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100306}
307
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100308JNIEXPORT jstring JVM_InternString(JNIEnv* env, jstring jstr) {
309 LOG(DEBUG) << "JVM_InternString env=" << env << ", jstr=" << jstr;
310 art::ScopedFastNativeObjectAccess soa(env);
311 art::mirror::String* s = soa.Decode<art::mirror::String*>(jstr);
312 art::mirror::String* result = s->Intern();
313 return soa.AddLocalReference<jstring>(result);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100314}
315
316JNIEXPORT jlong JVM_FreeMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100317 return art::Runtime::Current()->GetHeap()->GetFreeMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100318}
319
320JNIEXPORT jlong JVM_TotalMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100321 return art::Runtime::Current()->GetHeap()->GetTotalMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100322}
323
324JNIEXPORT jlong JVM_MaxMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100325 return art::Runtime::Current()->GetHeap()->GetMaxMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100326}
327
328JNIEXPORT void JVM_GC(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100329 if (art::Runtime::Current()->IsExplicitGcDisabled()) {
330 LOG(INFO) << "Explicit GC skipped.";
331 return;
332 }
333 art::Runtime::Current()->GetHeap()->CollectGarbage(false);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100334}
335
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100336JNIEXPORT __attribute__((noreturn)) void JVM_Exit(jint status) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100337 LOG(INFO) << "System.exit called, status: " << status;
338 art::Runtime::Current()->CallExitHook(status);
339 exit(status);
340}
341
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800342static void SetLdLibraryPath(JNIEnv* env, jstring javaLdLibraryPath) {
343#ifdef __ANDROID__
344 if (javaLdLibraryPath != nullptr) {
345 ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath);
346 if (ldLibraryPath.c_str() != nullptr) {
347 android_update_LD_LIBRARY_PATH(ldLibraryPath.c_str());
348 }
349 }
350
351#else
352 LOG(WARNING) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!";
353 UNUSED(javaLdLibraryPath, env);
354#endif
355}
356
357
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100358JNIEXPORT jstring JVM_NativeLoad(JNIEnv* env, jstring javaFilename, jobject javaLoader,
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800359 jstring javaLdLibraryPath, jstring javaLibraryPermittedPath) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100360 ScopedUtfChars filename(env, javaFilename);
361 if (filename.c_str() == NULL) {
362 return NULL;
363 }
364
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800365 int32_t target_sdk_version = art::Runtime::Current()->GetTargetSdkVersion();
366
367 // Starting with N nativeLoad uses classloader local
368 // linker namespace instead of global LD_LIBRARY_PATH
369 // (23 is Marshmallow)
370 if (target_sdk_version <= 23) {
371 SetLdLibraryPath(env, javaLdLibraryPath);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100372 }
373
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800374 std::string error_msg;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100375 {
376 art::ScopedObjectAccess soa(env);
377 art::StackHandleScope<1> hs(soa.Self());
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100378 art::JavaVMExt* vm = art::Runtime::Current()->GetJavaVM();
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800379 bool success = vm->LoadNativeLibrary(env, filename.c_str(), javaLoader,
380 javaLdLibraryPath, javaLibraryPermittedPath, &error_msg);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100381 if (success) {
382 return nullptr;
383 }
384 }
385
386 // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF.
387 env->ExceptionClear();
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800388 return env->NewStringUTF(error_msg.c_str());
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100389}
390
391JNIEXPORT void JVM_StartThread(JNIEnv* env, jobject jthread, jlong stack_size, jboolean daemon) {
392 art::Thread::CreateNativeThread(env, jthread, stack_size, daemon == JNI_TRUE);
393}
394
395JNIEXPORT void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio) {
396 art::ScopedObjectAccess soa(env);
397 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
398 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
399 if (thread != NULL) {
400 thread->SetNativePriority(prio);
401 }
402}
403
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100404JNIEXPORT void JVM_Yield(JNIEnv* env ATTRIBUTE_UNUSED, jclass threadClass ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100405 sched_yield();
406}
407
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100408JNIEXPORT void JVM_Sleep(JNIEnv* env, jclass threadClass ATTRIBUTE_UNUSED,
409 jobject java_lock, jlong millis) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100410 art::ScopedFastNativeObjectAccess soa(env);
411 art::mirror::Object* lock = soa.Decode<art::mirror::Object*>(java_lock);
412 art::Monitor::Wait(art::Thread::Current(), lock, millis, 0, true, art::kSleeping);
413}
414
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100415JNIEXPORT jobject JVM_CurrentThread(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100416 art::ScopedFastNativeObjectAccess soa(env);
417 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
418}
419
420JNIEXPORT void JVM_Interrupt(JNIEnv* env, jobject jthread) {
421 art::ScopedFastNativeObjectAccess soa(env);
422 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
423 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
424 if (thread != nullptr) {
425 thread->Interrupt(soa.Self());
426 }
427}
428
429JNIEXPORT jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clearInterrupted) {
430 if (clearInterrupted) {
431 return static_cast<art::JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
432 } else {
433 art::ScopedFastNativeObjectAccess soa(env);
434 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
435 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
436 return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE;
437 }
438}
439
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100440JNIEXPORT jboolean JVM_HoldsLock(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED, jobject jobj) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100441 art::ScopedObjectAccess soa(env);
442 art::mirror::Object* object = soa.Decode<art::mirror::Object*>(jobj);
443 if (object == NULL) {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000444 art::ThrowNullPointerException("object == null");
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100445 return JNI_FALSE;
446 }
447 return soa.Self()->HoldsLock(object);
448}
449
450JNIEXPORT void JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring java_name) {
451 ScopedUtfChars name(env, java_name);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100452 {
453 art::ScopedObjectAccess soa(env);
454 if (soa.Decode<art::mirror::Object*>(jthread) == soa.Self()->GetPeer()) {
455 soa.Self()->SetThreadName(name.c_str());
456 return;
457 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100458 }
459 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
460 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
461 // in the DDMS send code.
462 art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList();
463 bool timed_out;
464 // Take suspend thread lock to avoid races with threads trying to suspend this one.
465 art::Thread* thread;
466 {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100467 thread = thread_list->SuspendThreadByPeer(jthread, true, false, &timed_out);
468 }
469 if (thread != NULL) {
470 {
471 art::ScopedObjectAccess soa(env);
472 thread->SetThreadName(name.c_str());
473 }
474 thread_list->Resume(thread, false);
475 } else if (timed_out) {
476 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
477 "failed to suspend within a generous timeout.";
478 }
479}
480
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000481JNIEXPORT jint JVM_IHashCode(JNIEnv* env ATTRIBUTE_UNUSED,
482 jobject javaObject ATTRIBUTE_UNUSED) {
483 UNIMPLEMENTED(FATAL) << "JVM_IHashCode is not implemented";
484 return 0;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100485}
486
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100487JNIEXPORT jlong JVM_NanoTime(JNIEnv* env ATTRIBUTE_UNUSED, jclass unused ATTRIBUTE_UNUSED) {
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000488 UNIMPLEMENTED(FATAL) << "JVM_NanoTime is not implemented";
489 return 0L;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100490}
Narayan Kamathb7802892015-10-01 12:34:52 +0100491
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000492JNIEXPORT void JVM_ArrayCopy(JNIEnv* /* env */, jclass /* unused */, jobject /* javaSrc */,
493 jint /* srcPos */, jobject /* javaDst */, jint /* dstPos */,
494 jint /* length */) {
495 UNIMPLEMENTED(FATAL) << "JVM_ArrayCopy is not implemented";
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100496}
497
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100498JNIEXPORT jint JVM_FindSignal(const char* name ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100499 LOG(FATAL) << "JVM_FindSignal is not implemented";
500 return 0;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100501}
502
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100503JNIEXPORT void* JVM_RegisterSignal(jint signum ATTRIBUTE_UNUSED, void* handler ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100504 LOG(FATAL) << "JVM_RegisterSignal is not implemented";
505 return nullptr;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100506}
507
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100508JNIEXPORT jboolean JVM_RaiseSignal(jint signum ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100509 LOG(FATAL) << "JVM_RaiseSignal is not implemented";
510 return JNI_FALSE;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100511}
512
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100513JNIEXPORT __attribute__((noreturn)) void JVM_Halt(jint code) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100514 exit(code);
515}
Przemyslaw Szczepaniakb02d9b72015-08-20 15:46:16 +0100516
517JNIEXPORT jboolean JVM_IsNaN(jdouble d) {
518 return isnan(d);
519}