blob: 1f33651243bace4bbf4883d86109d644125a38ea [file] [log] [blame]
Narayan Kamath28ee8db2015-12-20 20:32:01 +00001/* Copyright (C) 2014 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +01003 *
Narayan Kamath28ee8db2015-12-20 20:32:01 +00004 * This file implements interfaces from the file jvm.h. This implementation
5 * is licensed under the same terms as the file jvm.h. The
6 * copyright and license information for the file jvm.h follows.
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +01007 *
Narayan Kamath28ee8db2015-12-20 20:32:01 +00008 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010010 *
Narayan Kamath28ee8db2015-12-20 20:32:01 +000011 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010030 */
31
32/*
33 * Services that OpenJDK expects the VM to provide.
34 */
35#include<stdio.h>
36#include <dlfcn.h>
37#include <limits.h>
38#include <unistd.h>
39
40#include "common_throws.h"
41#include "gc/heap.h"
42#include "thread.h"
43#include "thread_list.h"
44#include "runtime.h"
45#include "handle_scope-inl.h"
46#include "scoped_thread_state_change.h"
47#include "ScopedUtfChars.h"
48#include "mirror/class_loader.h"
49#include "verify_object-inl.h"
50#include "base/logging.h"
51#include "base/macros.h"
Narayan Kamatha0cf5a62015-09-07 11:41:37 +010052#include "../../libcore/ojluni/src/main/native/jvm.h" // TODO(narayan): fix it
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010053#include "jni_internal.h"
54#include "mirror/string-inl.h"
Narayan Kamath18d20952015-12-22 14:56:59 +000055#include "native/scoped_fast_native_object_access.h"
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010056#include "ScopedLocalRef.h"
57#include <sys/time.h>
58#include <sys/socket.h>
59#include <sys/ioctl.h>
60
Dmitriy Ivanov3e381722015-11-23 17:40:11 -080061#ifdef __ANDROID__
62// This function is provided by android linker.
63extern "C" void android_update_LD_LIBRARY_PATH(const char* ld_library_path);
64#endif // __ANDROID__
65
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010066#undef LOG_TAG
Narayan Kamath6280ef82015-12-17 12:34:57 +000067#define LOG_TAG "artopenjdk"
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010068
Narayan Kamathd1ef4362015-11-12 11:49:06 +000069using art::WARNING;
Narayan Kamathd1ef4362015-11-12 11:49:06 +000070using art::INFO;
71using art::ERROR;
72using art::FATAL;
73
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010074/* posix open() with extensions; used by e.g. ZipFile */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +010075JNIEXPORT jint JVM_Open(const char* fname, jint flags, jint mode) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010076 /*
77 * The call is expected to handle JVM_O_DELETE, which causes the file
78 * to be removed after it is opened. Also, some code seems to
79 * want the special return value JVM_EEXIST if the file open fails
80 * due to O_EXCL.
81 */
82 int fd = TEMP_FAILURE_RETRY(open(fname, flags & ~JVM_O_DELETE, mode));
83 if (fd < 0) {
84 int err = errno;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010085 if (err == EEXIST) {
86 return JVM_EEXIST;
87 } else {
88 return -1;
89 }
90 }
91
92 if (flags & JVM_O_DELETE) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010093 if (unlink(fname) != 0) {
94 LOG(WARNING) << "Post-open deletion of '" << fname << "' failed: " << strerror(errno);
95 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010096 }
97
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010098 return fd;
99}
100
101/* posix close() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100102JNIEXPORT jint JVM_Close(jint fd) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100103 // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR
104 return close(fd);
105}
106
107/* posix read() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100108JNIEXPORT jint JVM_Read(jint fd, char* buf, jint nbytes) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100109 return TEMP_FAILURE_RETRY(read(fd, buf, nbytes));
110}
111
112/* posix write(); is used to write messages to stderr */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100113JNIEXPORT jint JVM_Write(jint fd, char* buf, jint nbytes) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100114 return TEMP_FAILURE_RETRY(write(fd, buf, nbytes));
115}
116
117/* posix lseek() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100118JNIEXPORT jlong JVM_Lseek(jint fd, jlong offset, jint whence) {
Narayan Kamath5b2cfd82016-04-15 17:51:55 +0100119#if !defined(__APPLE__)
Narayan Kamath77f5d652016-04-15 15:57:28 +0100120 // NOTE: Using TEMP_FAILURE_RETRY here is busted for LP32 on glibc - the return
121 // value will be coerced into an int32_t.
122 //
123 // lseek64 isn't specified to return EINTR so it shouldn't be necessary
124 // anyway.
125 return lseek64(fd, offset, whence);
Narayan Kamath5b2cfd82016-04-15 17:51:55 +0100126#else
127 // NOTE: This code is compiled for Mac OS but isn't ever run on that
128 // platform.
129 return lseek(fd, offset, whence);
130#endif
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100131}
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100132
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100133/*
134 * "raw monitors" seem to be expected to behave like non-recursive pthread
135 * mutexes. They're used by ZipFile.
136 */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100137JNIEXPORT void* JVM_RawMonitorCreate(void) {
Narayan Kamath6c37e9a2016-02-09 13:11:09 +0000138 pthread_mutex_t* mutex =
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100139 reinterpret_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
Narayan Kamath6c37e9a2016-02-09 13:11:09 +0000140 CHECK(mutex != nullptr);
141 CHECK_PTHREAD_CALL(pthread_mutex_init, (mutex, nullptr), "JVM_RawMonitorCreate");
142 return mutex;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100143}
144
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100145JNIEXPORT void JVM_RawMonitorDestroy(void* mon) {
Narayan Kamath6c37e9a2016-02-09 13:11:09 +0000146 CHECK_PTHREAD_CALL(pthread_mutex_destroy,
147 (reinterpret_cast<pthread_mutex_t*>(mon)),
148 "JVM_RawMonitorDestroy");
149 free(mon);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100150}
151
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100152JNIEXPORT jint JVM_RawMonitorEnter(void* mon) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100153 return pthread_mutex_lock(reinterpret_cast<pthread_mutex_t*>(mon));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100154}
155
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100156JNIEXPORT void JVM_RawMonitorExit(void* mon) {
Narayan Kamath6c37e9a2016-02-09 13:11:09 +0000157 CHECK_PTHREAD_CALL(pthread_mutex_unlock,
158 (reinterpret_cast<pthread_mutex_t*>(mon)),
159 "JVM_RawMonitorExit");
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100160}
161
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100162JNIEXPORT char* JVM_NativePath(char* path) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100163 return path;
164}
165
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100166JNIEXPORT jint JVM_GetLastErrorString(char* buf, int len) {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000167#if defined(__GLIBC__) || defined(__BIONIC__)
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000168 if (len == 0) {
169 return 0;
170 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100171
Narayan Kamath44ba97e2016-02-05 14:58:02 +0000172 const int err = errno;
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000173 char* result = strerror_r(err, buf, len);
174 if (result != buf) {
175 strncpy(buf, result, len);
176 buf[len - 1] = '\0';
177 }
178
179 return strlen(buf);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100180#else
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000181 UNUSED(buf);
182 UNUSED(len);
183 return -1;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100184#endif
185}
186
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100187JNIEXPORT int jio_fprintf(FILE* fp, const char* fmt, ...) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100188 va_list args;
189
190 va_start(args, fmt);
191 int len = jio_vfprintf(fp, fmt, args);
192 va_end(args);
193
194 return len;
195}
196
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100197JNIEXPORT int jio_vfprintf(FILE* fp, const char* fmt, va_list args) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100198 assert(fp != NULL);
199 return vfprintf(fp, fmt, args);
200}
201
202/* posix fsync() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100203JNIEXPORT jint JVM_Sync(jint fd) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100204 return TEMP_FAILURE_RETRY(fsync(fd));
205}
206
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100207JNIEXPORT void* JVM_FindLibraryEntry(void* handle, const char* name) {
Przemyslaw Szczepaniak67d39ad2015-07-03 13:54:00 +0100208 return dlsym(handle, name);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100209}
210
Narayan Kamath44ba97e2016-02-05 14:58:02 +0000211JNIEXPORT jlong JVM_CurrentTimeMillis(JNIEnv* env ATTRIBUTE_UNUSED,
212 jclass clazz ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100213 struct timeval tv;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100214 gettimeofday(&tv, (struct timezone *) NULL);
215 jlong when = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
216 return when;
217}
218
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100219JNIEXPORT jint JVM_Socket(jint domain, jint type, jint protocol) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100220 return TEMP_FAILURE_RETRY(socket(domain, type, protocol));
221}
222
223JNIEXPORT jint JVM_InitializeSocketLibrary() {
224 return 0;
225}
226
227int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100228 if ((intptr_t)count <= 0) return -1;
229 return vsnprintf(str, count, fmt, args);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100230}
231
232int jio_snprintf(char *str, size_t count, const char *fmt, ...) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100233 va_list args;
234 int len;
235 va_start(args, fmt);
236 len = jio_vsnprintf(str, count, fmt, args);
237 va_end(args);
238 return len;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100239}
240
241JNIEXPORT jint JVM_SetSockOpt(jint fd, int level, int optname,
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100242 const char* optval, int optlen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100243 return TEMP_FAILURE_RETRY(setsockopt(fd, level, optname, optval, optlen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100244}
245
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100246JNIEXPORT jint JVM_SocketShutdown(jint fd, jint howto) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100247 return TEMP_FAILURE_RETRY(shutdown(fd, howto));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100248}
249
250JNIEXPORT jint JVM_GetSockOpt(jint fd, int level, int optname, char* optval,
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100251 int* optlen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100252 socklen_t len = *optlen;
253 int cc = TEMP_FAILURE_RETRY(getsockopt(fd, level, optname, optval, &len));
254 *optlen = len;
255 return cc;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100256}
257
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100258JNIEXPORT jint JVM_GetSockName(jint fd, struct sockaddr* addr, int* addrlen) {
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) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100266 if (TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, result)) < 0) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100267 return JNI_FALSE;
268 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100269
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100270 return JNI_TRUE;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100271}
272
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100273JNIEXPORT jint JVM_Send(jint fd, char* buf, jint nBytes, jint flags) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100274 return TEMP_FAILURE_RETRY(send(fd, buf, nBytes, flags));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100275}
276
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100277JNIEXPORT jint JVM_SocketClose(jint fd) {
Narayan Kamath44ba97e2016-02-05 14:58:02 +0000278 // Don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR.
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100279 return close(fd);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100280}
281
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100282JNIEXPORT jint JVM_Listen(jint fd, jint count) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100283 return TEMP_FAILURE_RETRY(listen(fd, count));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100284}
285
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100286JNIEXPORT jint JVM_Connect(jint fd, struct sockaddr* addr, jint addrlen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100287 return TEMP_FAILURE_RETRY(connect(fd, addr, addrlen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100288}
289
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100290JNIEXPORT int JVM_GetHostName(char* name, int namelen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100291 return TEMP_FAILURE_RETRY(gethostname(name, namelen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100292}
293
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100294JNIEXPORT jstring JVM_InternString(JNIEnv* env, jstring jstr) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100295 art::ScopedFastNativeObjectAccess soa(env);
296 art::mirror::String* s = soa.Decode<art::mirror::String*>(jstr);
297 art::mirror::String* result = s->Intern();
298 return soa.AddLocalReference<jstring>(result);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100299}
300
301JNIEXPORT jlong JVM_FreeMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100302 return art::Runtime::Current()->GetHeap()->GetFreeMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100303}
304
305JNIEXPORT jlong JVM_TotalMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100306 return art::Runtime::Current()->GetHeap()->GetTotalMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100307}
308
309JNIEXPORT jlong JVM_MaxMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100310 return art::Runtime::Current()->GetHeap()->GetMaxMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100311}
312
313JNIEXPORT void JVM_GC(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100314 if (art::Runtime::Current()->IsExplicitGcDisabled()) {
315 LOG(INFO) << "Explicit GC skipped.";
316 return;
317 }
318 art::Runtime::Current()->GetHeap()->CollectGarbage(false);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100319}
320
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100321JNIEXPORT __attribute__((noreturn)) void JVM_Exit(jint status) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100322 LOG(INFO) << "System.exit called, status: " << status;
323 art::Runtime::Current()->CallExitHook(status);
324 exit(status);
325}
326
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800327static void SetLdLibraryPath(JNIEnv* env, jstring javaLdLibraryPath) {
328#ifdef __ANDROID__
329 if (javaLdLibraryPath != nullptr) {
330 ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath);
331 if (ldLibraryPath.c_str() != nullptr) {
332 android_update_LD_LIBRARY_PATH(ldLibraryPath.c_str());
333 }
334 }
335
336#else
337 LOG(WARNING) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!";
338 UNUSED(javaLdLibraryPath, env);
339#endif
340}
341
342
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800343JNIEXPORT jstring JVM_NativeLoad(JNIEnv* env,
344 jstring javaFilename,
345 jobject javaLoader,
346 jstring javaLibrarySearchPath) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100347 ScopedUtfChars filename(env, javaFilename);
348 if (filename.c_str() == NULL) {
349 return NULL;
350 }
351
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800352 int32_t target_sdk_version = art::Runtime::Current()->GetTargetSdkVersion();
353
354 // Starting with N nativeLoad uses classloader local
355 // linker namespace instead of global LD_LIBRARY_PATH
Narayan Kamath5f971572016-03-15 14:47:29 +0000356 // (23 is Marshmallow). This call is here to preserve
357 // backwards compatibility for the apps targeting sdk
358 // version <= 23
359 if (target_sdk_version == 0) {
Dimitry Ivanov986f6502015-12-15 14:08:18 -0800360 SetLdLibraryPath(env, javaLibrarySearchPath);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100361 }
362
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800363 std::string error_msg;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100364 {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100365 art::JavaVMExt* vm = art::Runtime::Current()->GetJavaVM();
Dimitry Ivanov986f6502015-12-15 14:08:18 -0800366 bool success = vm->LoadNativeLibrary(env,
367 filename.c_str(),
368 javaLoader,
Dimitry Ivanov986f6502015-12-15 14:08:18 -0800369 javaLibrarySearchPath,
Dimitry Ivanov986f6502015-12-15 14:08:18 -0800370 &error_msg);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100371 if (success) {
372 return nullptr;
373 }
374 }
375
376 // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF.
377 env->ExceptionClear();
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800378 return env->NewStringUTF(error_msg.c_str());
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100379}
380
381JNIEXPORT void JVM_StartThread(JNIEnv* env, jobject jthread, jlong stack_size, jboolean daemon) {
382 art::Thread::CreateNativeThread(env, jthread, stack_size, daemon == JNI_TRUE);
383}
384
385JNIEXPORT void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio) {
386 art::ScopedObjectAccess soa(env);
387 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
388 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
389 if (thread != NULL) {
390 thread->SetNativePriority(prio);
391 }
392}
393
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100394JNIEXPORT void JVM_Yield(JNIEnv* env ATTRIBUTE_UNUSED, jclass threadClass ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100395 sched_yield();
396}
397
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100398JNIEXPORT void JVM_Sleep(JNIEnv* env, jclass threadClass ATTRIBUTE_UNUSED,
399 jobject java_lock, jlong millis) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100400 art::ScopedFastNativeObjectAccess soa(env);
401 art::mirror::Object* lock = soa.Decode<art::mirror::Object*>(java_lock);
402 art::Monitor::Wait(art::Thread::Current(), lock, millis, 0, true, art::kSleeping);
403}
404
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100405JNIEXPORT jobject JVM_CurrentThread(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100406 art::ScopedFastNativeObjectAccess soa(env);
407 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
408}
409
410JNIEXPORT void JVM_Interrupt(JNIEnv* env, jobject jthread) {
411 art::ScopedFastNativeObjectAccess soa(env);
412 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
413 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
414 if (thread != nullptr) {
415 thread->Interrupt(soa.Self());
416 }
417}
418
419JNIEXPORT jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clearInterrupted) {
420 if (clearInterrupted) {
421 return static_cast<art::JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
422 } else {
423 art::ScopedFastNativeObjectAccess soa(env);
424 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
425 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
426 return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE;
427 }
428}
429
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100430JNIEXPORT jboolean JVM_HoldsLock(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED, jobject jobj) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100431 art::ScopedObjectAccess soa(env);
432 art::mirror::Object* object = soa.Decode<art::mirror::Object*>(jobj);
433 if (object == NULL) {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000434 art::ThrowNullPointerException("object == null");
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100435 return JNI_FALSE;
436 }
437 return soa.Self()->HoldsLock(object);
438}
439
440JNIEXPORT void JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring java_name) {
441 ScopedUtfChars name(env, java_name);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100442 {
443 art::ScopedObjectAccess soa(env);
444 if (soa.Decode<art::mirror::Object*>(jthread) == soa.Self()->GetPeer()) {
445 soa.Self()->SetThreadName(name.c_str());
446 return;
447 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100448 }
449 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
450 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
451 // in the DDMS send code.
452 art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList();
453 bool timed_out;
454 // Take suspend thread lock to avoid races with threads trying to suspend this one.
455 art::Thread* thread;
456 {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100457 thread = thread_list->SuspendThreadByPeer(jthread, true, false, &timed_out);
458 }
459 if (thread != NULL) {
460 {
461 art::ScopedObjectAccess soa(env);
462 thread->SetThreadName(name.c_str());
463 }
464 thread_list->Resume(thread, false);
465 } else if (timed_out) {
466 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
467 "failed to suspend within a generous timeout.";
468 }
469}
470
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000471JNIEXPORT jint JVM_IHashCode(JNIEnv* env ATTRIBUTE_UNUSED,
472 jobject javaObject ATTRIBUTE_UNUSED) {
473 UNIMPLEMENTED(FATAL) << "JVM_IHashCode is not implemented";
474 return 0;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100475}
476
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100477JNIEXPORT jlong JVM_NanoTime(JNIEnv* env ATTRIBUTE_UNUSED, jclass unused ATTRIBUTE_UNUSED) {
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000478 UNIMPLEMENTED(FATAL) << "JVM_NanoTime is not implemented";
479 return 0L;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100480}
Narayan Kamathb7802892015-10-01 12:34:52 +0100481
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000482JNIEXPORT void JVM_ArrayCopy(JNIEnv* /* env */, jclass /* unused */, jobject /* javaSrc */,
483 jint /* srcPos */, jobject /* javaDst */, jint /* dstPos */,
484 jint /* length */) {
485 UNIMPLEMENTED(FATAL) << "JVM_ArrayCopy is not implemented";
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100486}
487
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100488JNIEXPORT jint JVM_FindSignal(const char* name ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100489 LOG(FATAL) << "JVM_FindSignal is not implemented";
490 return 0;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100491}
492
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100493JNIEXPORT void* JVM_RegisterSignal(jint signum ATTRIBUTE_UNUSED, void* handler ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100494 LOG(FATAL) << "JVM_RegisterSignal is not implemented";
495 return nullptr;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100496}
497
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100498JNIEXPORT jboolean JVM_RaiseSignal(jint signum ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100499 LOG(FATAL) << "JVM_RaiseSignal is not implemented";
500 return JNI_FALSE;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100501}
502
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100503JNIEXPORT __attribute__((noreturn)) void JVM_Halt(jint code) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100504 exit(code);
505}
Przemyslaw Szczepaniakb02d9b72015-08-20 15:46:16 +0100506
507JNIEXPORT jboolean JVM_IsNaN(jdouble d) {
508 return isnan(d);
509}