blob: 675a401bb6b859f3cd86489c5ae5fc8962a82b91 [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 */
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010035#include <dlfcn.h>
36#include <limits.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include <stdio.h>
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010038#include <sys/ioctl.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include <sys/socket.h>
40#include <sys/time.h>
Andreas Gampec15a2f42017-04-21 12:09:39 -070041#include <unistd.h>
42
Andreas Gampe57943812017-12-06 21:39:13 -080043#include <android-base/logging.h>
44
Andreas Gampec15a2f42017-04-21 12:09:39 -070045#include "../../libcore/ojluni/src/main/native/jvm.h" // TODO(narayan): fix it
46
Andreas Gampec15a2f42017-04-21 12:09:39 -070047#include "base/macros.h"
48#include "common_throws.h"
49#include "gc/heap.h"
50#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010051#include "jni/java_vm_ext.h"
52#include "jni/jni_internal.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070053#include "mirror/class_loader.h"
54#include "mirror/string-inl.h"
55#include "monitor.h"
56#include "native/scoped_fast_native_object_access-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070057#include "nativehelper/scoped_local_ref.h"
58#include "nativehelper/scoped_utf_chars.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070059#include "runtime.h"
Steven Morelande431e272017-07-18 16:53:49 -070060#include "scoped_thread_state_change-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070061#include "thread.h"
62#include "thread_list.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070063#include "verify_object.h"
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010064
65#undef LOG_TAG
Narayan Kamath6280ef82015-12-17 12:34:57 +000066#define LOG_TAG "artopenjdk"
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010067
68/* posix open() with extensions; used by e.g. ZipFile */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +010069JNIEXPORT jint JVM_Open(const char* fname, jint flags, jint mode) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010070 /*
Przemyslaw Szczepaniaka89e51a2016-05-26 15:52:36 +010071 * Some code seems to want the special return value JVM_EEXIST if the
72 * file open fails due to O_EXCL.
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010073 */
Przemyslaw Szczepaniaka89e51a2016-05-26 15:52:36 +010074 // Don't use JVM_O_DELETE, it's problematic with FUSE, see b/28901232.
75 if (flags & JVM_O_DELETE) {
76 LOG(FATAL) << "JVM_O_DELETE option is not supported (while opening: '"
77 << fname << "')";
78 }
79
Nick Kralevich98eebb42018-12-18 13:46:34 -080080 flags |= O_CLOEXEC;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010081 int fd = TEMP_FAILURE_RETRY(open(fname, flags & ~JVM_O_DELETE, mode));
82 if (fd < 0) {
83 int err = errno;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010084 if (err == EEXIST) {
85 return JVM_EEXIST;
86 } else {
87 return -1;
88 }
89 }
90
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +010091 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 // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR
97 return close(fd);
98}
99
100/* posix read() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100101JNIEXPORT jint JVM_Read(jint fd, char* buf, jint nbytes) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100102 return TEMP_FAILURE_RETRY(read(fd, buf, nbytes));
103}
104
105/* posix write(); is used to write messages to stderr */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100106JNIEXPORT jint JVM_Write(jint fd, char* buf, jint nbytes) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100107 return TEMP_FAILURE_RETRY(write(fd, buf, nbytes));
108}
109
110/* posix lseek() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100111JNIEXPORT jlong JVM_Lseek(jint fd, jlong offset, jint whence) {
Narayan Kamath5b2cfd82016-04-15 17:51:55 +0100112#if !defined(__APPLE__)
Narayan Kamath77f5d652016-04-15 15:57:28 +0100113 // NOTE: Using TEMP_FAILURE_RETRY here is busted for LP32 on glibc - the return
114 // value will be coerced into an int32_t.
115 //
116 // lseek64 isn't specified to return EINTR so it shouldn't be necessary
117 // anyway.
118 return lseek64(fd, offset, whence);
Narayan Kamath5b2cfd82016-04-15 17:51:55 +0100119#else
120 // NOTE: This code is compiled for Mac OS but isn't ever run on that
121 // platform.
122 return lseek(fd, offset, whence);
123#endif
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100124}
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100125
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100126/*
127 * "raw monitors" seem to be expected to behave like non-recursive pthread
128 * mutexes. They're used by ZipFile.
129 */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100130JNIEXPORT void* JVM_RawMonitorCreate(void) {
Narayan Kamath6c37e9a2016-02-09 13:11:09 +0000131 pthread_mutex_t* mutex =
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100132 reinterpret_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
Narayan Kamath6c37e9a2016-02-09 13:11:09 +0000133 CHECK(mutex != nullptr);
134 CHECK_PTHREAD_CALL(pthread_mutex_init, (mutex, nullptr), "JVM_RawMonitorCreate");
135 return mutex;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100136}
137
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100138JNIEXPORT void JVM_RawMonitorDestroy(void* mon) {
Narayan Kamath6c37e9a2016-02-09 13:11:09 +0000139 CHECK_PTHREAD_CALL(pthread_mutex_destroy,
140 (reinterpret_cast<pthread_mutex_t*>(mon)),
141 "JVM_RawMonitorDestroy");
142 free(mon);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100143}
144
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100145JNIEXPORT jint JVM_RawMonitorEnter(void* mon) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100146 return pthread_mutex_lock(reinterpret_cast<pthread_mutex_t*>(mon));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100147}
148
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100149JNIEXPORT void JVM_RawMonitorExit(void* mon) {
Narayan Kamath6c37e9a2016-02-09 13:11:09 +0000150 CHECK_PTHREAD_CALL(pthread_mutex_unlock,
151 (reinterpret_cast<pthread_mutex_t*>(mon)),
152 "JVM_RawMonitorExit");
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100153}
154
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100155JNIEXPORT char* JVM_NativePath(char* path) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100156 return path;
157}
158
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100159JNIEXPORT jint JVM_GetLastErrorString(char* buf, int len) {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000160#if defined(__GLIBC__) || defined(__BIONIC__)
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000161 if (len == 0) {
162 return 0;
163 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100164
Narayan Kamath44ba97e2016-02-05 14:58:02 +0000165 const int err = errno;
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000166 char* result = strerror_r(err, buf, len);
167 if (result != buf) {
168 strncpy(buf, result, len);
169 buf[len - 1] = '\0';
170 }
171
172 return strlen(buf);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100173#else
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000174 UNUSED(buf);
175 UNUSED(len);
176 return -1;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100177#endif
178}
179
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100180JNIEXPORT int jio_fprintf(FILE* fp, const char* fmt, ...) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100181 va_list args;
182
183 va_start(args, fmt);
184 int len = jio_vfprintf(fp, fmt, args);
185 va_end(args);
186
187 return len;
188}
189
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100190JNIEXPORT int jio_vfprintf(FILE* fp, const char* fmt, va_list args) {
Yi Kong4b22b342018-08-02 14:43:21 -0700191 assert(fp != nullptr);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100192 return vfprintf(fp, fmt, args);
193}
194
195/* posix fsync() */
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100196JNIEXPORT jint JVM_Sync(jint fd) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100197 return TEMP_FAILURE_RETRY(fsync(fd));
198}
199
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100200JNIEXPORT void* JVM_FindLibraryEntry(void* handle, const char* name) {
Przemyslaw Szczepaniak67d39ad2015-07-03 13:54:00 +0100201 return dlsym(handle, name);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100202}
203
Narayan Kamath44ba97e2016-02-05 14:58:02 +0000204JNIEXPORT jlong JVM_CurrentTimeMillis(JNIEnv* env ATTRIBUTE_UNUSED,
205 jclass clazz ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100206 struct timeval tv;
Yi Kong4b22b342018-08-02 14:43:21 -0700207 gettimeofday(&tv, (struct timezone *) nullptr);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100208 jlong when = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
209 return when;
210}
211
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100212JNIEXPORT jint JVM_Socket(jint domain, jint type, jint protocol) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100213 return TEMP_FAILURE_RETRY(socket(domain, type, protocol));
214}
215
216JNIEXPORT jint JVM_InitializeSocketLibrary() {
217 return 0;
218}
219
220int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100221 if ((intptr_t)count <= 0) return -1;
222 return vsnprintf(str, count, fmt, args);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100223}
224
225int jio_snprintf(char *str, size_t count, const char *fmt, ...) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100226 va_list args;
227 int len;
228 va_start(args, fmt);
229 len = jio_vsnprintf(str, count, fmt, args);
230 va_end(args);
231 return len;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100232}
233
234JNIEXPORT jint JVM_SetSockOpt(jint fd, int level, int optname,
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100235 const char* optval, int optlen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100236 return TEMP_FAILURE_RETRY(setsockopt(fd, level, optname, optval, optlen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100237}
238
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100239JNIEXPORT jint JVM_SocketShutdown(jint fd, jint howto) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100240 return TEMP_FAILURE_RETRY(shutdown(fd, howto));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100241}
242
243JNIEXPORT jint JVM_GetSockOpt(jint fd, int level, int optname, char* optval,
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100244 int* optlen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100245 socklen_t len = *optlen;
246 int cc = TEMP_FAILURE_RETRY(getsockopt(fd, level, optname, optval, &len));
247 *optlen = len;
248 return cc;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100249}
250
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100251JNIEXPORT jint JVM_GetSockName(jint fd, struct sockaddr* addr, int* addrlen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100252 socklen_t len = *addrlen;
253 int cc = TEMP_FAILURE_RETRY(getsockname(fd, addr, &len));
254 *addrlen = len;
255 return cc;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100256}
257
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100258JNIEXPORT jint JVM_SocketAvailable(jint fd, jint* result) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100259 if (TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, result)) < 0) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100260 return JNI_FALSE;
261 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100262
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100263 return JNI_TRUE;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100264}
265
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100266JNIEXPORT jint JVM_Send(jint fd, char* buf, jint nBytes, jint flags) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100267 return TEMP_FAILURE_RETRY(send(fd, buf, nBytes, flags));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100268}
269
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100270JNIEXPORT jint JVM_SocketClose(jint fd) {
Narayan Kamath44ba97e2016-02-05 14:58:02 +0000271 // Don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR.
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100272 return close(fd);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100273}
274
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100275JNIEXPORT jint JVM_Listen(jint fd, jint count) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100276 return TEMP_FAILURE_RETRY(listen(fd, count));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100277}
278
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100279JNIEXPORT jint JVM_Connect(jint fd, struct sockaddr* addr, jint addrlen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100280 return TEMP_FAILURE_RETRY(connect(fd, addr, addrlen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100281}
282
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100283JNIEXPORT int JVM_GetHostName(char* name, int namelen) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100284 return TEMP_FAILURE_RETRY(gethostname(name, namelen));
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100285}
286
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100287JNIEXPORT jstring JVM_InternString(JNIEnv* env, jstring jstr) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100288 art::ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700289 art::ObjPtr<art::mirror::String> s = soa.Decode<art::mirror::String>(jstr);
290 return soa.AddLocalReference<jstring>(s->Intern());
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100291}
292
293JNIEXPORT jlong JVM_FreeMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100294 return art::Runtime::Current()->GetHeap()->GetFreeMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100295}
296
297JNIEXPORT jlong JVM_TotalMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100298 return art::Runtime::Current()->GetHeap()->GetTotalMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100299}
300
301JNIEXPORT jlong JVM_MaxMemory(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100302 return art::Runtime::Current()->GetHeap()->GetMaxMemory();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100303}
304
305JNIEXPORT void JVM_GC(void) {
Narayan Kamatha0cf5a62015-09-07 11:41:37 +0100306 if (art::Runtime::Current()->IsExplicitGcDisabled()) {
307 LOG(INFO) << "Explicit GC skipped.";
308 return;
309 }
Roland Levillainaf290312018-02-27 20:02:17 +0000310 art::Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references */ false);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100311}
312
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100313JNIEXPORT __attribute__((noreturn)) void JVM_Exit(jint status) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100314 LOG(INFO) << "System.exit called, status: " << status;
315 art::Runtime::Current()->CallExitHook(status);
Hans Boehm36896be2020-03-09 14:23:23 -0700316 // Unsafe to call exit() while threads may still be running. They would race
317 // with static destructors.
318 _exit(status);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100319}
320
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800321JNIEXPORT jstring JVM_NativeLoad(JNIEnv* env,
322 jstring javaFilename,
Nicolas Geoffray96259f12019-01-18 10:04:51 +0000323 jobject javaLoader,
324 jclass caller) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100325 ScopedUtfChars filename(env, javaFilename);
Yi Kong4b22b342018-08-02 14:43:21 -0700326 if (filename.c_str() == nullptr) {
327 return nullptr;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100328 }
329
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800330 std::string error_msg;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100331 {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100332 art::JavaVMExt* vm = art::Runtime::Current()->GetJavaVM();
Dimitry Ivanov986f6502015-12-15 14:08:18 -0800333 bool success = vm->LoadNativeLibrary(env,
334 filename.c_str(),
335 javaLoader,
Nicolas Geoffray96259f12019-01-18 10:04:51 +0000336 caller,
Dimitry Ivanov986f6502015-12-15 14:08:18 -0800337 &error_msg);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100338 if (success) {
339 return nullptr;
340 }
341 }
342
343 // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF.
344 env->ExceptionClear();
Dmitriy Ivanov3e381722015-11-23 17:40:11 -0800345 return env->NewStringUTF(error_msg.c_str());
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100346}
347
348JNIEXPORT void JVM_StartThread(JNIEnv* env, jobject jthread, jlong stack_size, jboolean daemon) {
349 art::Thread::CreateNativeThread(env, jthread, stack_size, daemon == JNI_TRUE);
350}
351
352JNIEXPORT void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio) {
353 art::ScopedObjectAccess soa(env);
354 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
355 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
Yi Kong4b22b342018-08-02 14:43:21 -0700356 if (thread != nullptr) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100357 thread->SetNativePriority(prio);
358 }
359}
360
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100361JNIEXPORT void JVM_Yield(JNIEnv* env ATTRIBUTE_UNUSED, jclass threadClass ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100362 sched_yield();
363}
364
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100365JNIEXPORT void JVM_Sleep(JNIEnv* env, jclass threadClass ATTRIBUTE_UNUSED,
366 jobject java_lock, jlong millis) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100367 art::ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700368 art::ObjPtr<art::mirror::Object> lock = soa.Decode<art::mirror::Object>(java_lock);
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700369 art::Monitor::Wait(art::Thread::Current(), lock.Ptr(), millis, 0, true, art::kSleeping);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100370}
371
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100372JNIEXPORT jobject JVM_CurrentThread(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100373 art::ScopedFastNativeObjectAccess soa(env);
374 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
375}
376
377JNIEXPORT void JVM_Interrupt(JNIEnv* env, jobject jthread) {
378 art::ScopedFastNativeObjectAccess soa(env);
379 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
380 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
381 if (thread != nullptr) {
382 thread->Interrupt(soa.Self());
383 }
384}
385
386JNIEXPORT jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clearInterrupted) {
387 if (clearInterrupted) {
Ian Rogers55256cb2017-12-21 17:07:11 -0800388 return static_cast<art::JNIEnvExt*>(env)->GetSelf()->Interrupted() ? JNI_TRUE : JNI_FALSE;
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100389 } else {
390 art::ScopedFastNativeObjectAccess soa(env);
391 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
392 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread);
393 return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE;
394 }
395}
396
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100397JNIEXPORT jboolean JVM_HoldsLock(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED, jobject jobj) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100398 art::ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700399 art::ObjPtr<art::mirror::Object> object = soa.Decode<art::mirror::Object>(jobj);
400 if (object == nullptr) {
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000401 art::ThrowNullPointerException("object == null");
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100402 return JNI_FALSE;
403 }
Vladimir Markobcf17522018-06-01 13:14:32 +0100404 return soa.Self()->HoldsLock(object);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100405}
406
407JNIEXPORT void JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring java_name) {
408 ScopedUtfChars name(env, java_name);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100409 {
410 art::ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700411 if (soa.Decode<art::mirror::Object>(jthread) == soa.Self()->GetPeer()) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100412 soa.Self()->SetThreadName(name.c_str());
413 return;
414 }
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100415 }
416 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
417 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
418 // in the DDMS send code.
419 art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList();
420 bool timed_out;
421 // Take suspend thread lock to avoid races with threads trying to suspend this one.
422 art::Thread* thread;
423 {
Alex Light46f93402017-06-29 11:59:50 -0700424 thread = thread_list->SuspendThreadByPeer(jthread,
425 true,
426 art::SuspendReason::kInternal,
427 &timed_out);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100428 }
Yi Kong4b22b342018-08-02 14:43:21 -0700429 if (thread != nullptr) {
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100430 {
431 art::ScopedObjectAccess soa(env);
432 thread->SetThreadName(name.c_str());
433 }
Alex Light88fd7202017-06-30 08:31:59 -0700434 bool resumed = thread_list->Resume(thread, art::SuspendReason::kInternal);
435 DCHECK(resumed);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100436 } else if (timed_out) {
437 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
438 "failed to suspend within a generous timeout.";
439 }
440}
441
Elliott Hughesc1896c92018-11-29 11:33:18 -0800442JNIEXPORT __attribute__((noreturn)) jint JVM_IHashCode(JNIEnv* env ATTRIBUTE_UNUSED,
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000443 jobject javaObject ATTRIBUTE_UNUSED) {
444 UNIMPLEMENTED(FATAL) << "JVM_IHashCode is not implemented";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800445 UNREACHABLE();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100446}
447
Elliott Hughesc1896c92018-11-29 11:33:18 -0800448JNIEXPORT __attribute__((noreturn)) jlong JVM_NanoTime(JNIEnv* env ATTRIBUTE_UNUSED, jclass unused ATTRIBUTE_UNUSED) {
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000449 UNIMPLEMENTED(FATAL) << "JVM_NanoTime is not implemented";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800450 UNREACHABLE();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100451}
Narayan Kamathb7802892015-10-01 12:34:52 +0100452
Elliott Hughesc1896c92018-11-29 11:33:18 -0800453JNIEXPORT __attribute__((noreturn)) void JVM_ArrayCopy(JNIEnv* /* env */, jclass /* unused */, jobject /* javaSrc */,
Narayan Kamath68d8ff42015-11-16 12:31:28 +0000454 jint /* srcPos */, jobject /* javaDst */, jint /* dstPos */,
455 jint /* length */) {
456 UNIMPLEMENTED(FATAL) << "JVM_ArrayCopy is not implemented";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800457 UNREACHABLE();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100458}
459
Elliott Hughesc1896c92018-11-29 11:33:18 -0800460JNIEXPORT __attribute__((noreturn)) jint JVM_FindSignal(const char* name ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100461 LOG(FATAL) << "JVM_FindSignal is not implemented";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800462 UNREACHABLE();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100463}
464
Elliott Hughesc1896c92018-11-29 11:33:18 -0800465JNIEXPORT __attribute__((noreturn)) void* JVM_RegisterSignal(jint signum ATTRIBUTE_UNUSED, void* handler ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100466 LOG(FATAL) << "JVM_RegisterSignal is not implemented";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800467 UNREACHABLE();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100468}
469
Elliott Hughesc1896c92018-11-29 11:33:18 -0800470JNIEXPORT __attribute__((noreturn)) jboolean JVM_RaiseSignal(jint signum ATTRIBUTE_UNUSED) {
Narayan Kamath36379fd2015-08-13 17:33:24 +0100471 LOG(FATAL) << "JVM_RaiseSignal is not implemented";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800472 UNREACHABLE();
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100473}
474
Przemyslaw Szczepaniak6c0ea272015-09-23 08:48:00 +0100475JNIEXPORT __attribute__((noreturn)) void JVM_Halt(jint code) {
Hans Boehm36896be2020-03-09 14:23:23 -0700476 _exit(code);
Piotr Jastrzebskidf0b17a2015-04-24 09:18:00 +0100477}
Przemyslaw Szczepaniakb02d9b72015-08-20 15:46:16 +0100478
479JNIEXPORT jboolean JVM_IsNaN(jdouble d) {
480 return isnan(d);
481}