Narayan Kamath | 28ee8db | 2015-12-20 20:32:01 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2014 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 3 | * |
Narayan Kamath | 28ee8db | 2015-12-20 20:32:01 +0000 | [diff] [blame] | 4 | * 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 Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 7 | * |
Narayan Kamath | 28ee8db | 2015-12-20 20:32:01 +0000 | [diff] [blame] | 8 | * 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 Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 10 | * |
Narayan Kamath | 28ee8db | 2015-12-20 20:32:01 +0000 | [diff] [blame] | 11 | * 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 Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * Services that OpenJDK expects the VM to provide. |
| 34 | */ |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 35 | #include <dlfcn.h> |
| 36 | #include <limits.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 37 | #include <stdio.h> |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 38 | #include <sys/ioctl.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 39 | #include <sys/socket.h> |
| 40 | #include <sys/time.h> |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 41 | #include <unistd.h> |
| 42 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 43 | #include <android-base/logging.h> |
| 44 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 45 | #include "../../libcore/ojluni/src/main/native/jvm.h" // TODO(narayan): fix it |
| 46 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 47 | #include "base/macros.h" |
| 48 | #include "common_throws.h" |
| 49 | #include "gc/heap.h" |
| 50 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 51 | #include "jni/java_vm_ext.h" |
| 52 | #include "jni/jni_internal.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 53 | #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 Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 57 | #include "nativehelper/scoped_local_ref.h" |
| 58 | #include "nativehelper/scoped_utf_chars.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 59 | #include "runtime.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 60 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 61 | #include "thread.h" |
| 62 | #include "thread_list.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 63 | #include "verify_object.h" |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 64 | |
| 65 | #undef LOG_TAG |
Narayan Kamath | 6280ef8 | 2015-12-17 12:34:57 +0000 | [diff] [blame] | 66 | #define LOG_TAG "artopenjdk" |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 67 | |
| 68 | /* posix open() with extensions; used by e.g. ZipFile */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 69 | JNIEXPORT jint JVM_Open(const char* fname, jint flags, jint mode) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 70 | /* |
Przemyslaw Szczepaniak | a89e51a | 2016-05-26 15:52:36 +0100 | [diff] [blame] | 71 | * Some code seems to want the special return value JVM_EEXIST if the |
| 72 | * file open fails due to O_EXCL. |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 73 | */ |
Przemyslaw Szczepaniak | a89e51a | 2016-05-26 15:52:36 +0100 | [diff] [blame] | 74 | // 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 | |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 80 | int fd = TEMP_FAILURE_RETRY(open(fname, flags & ~JVM_O_DELETE, mode)); |
| 81 | if (fd < 0) { |
| 82 | int err = errno; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 83 | if (err == EEXIST) { |
| 84 | return JVM_EEXIST; |
| 85 | } else { |
| 86 | return -1; |
| 87 | } |
| 88 | } |
| 89 | |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 90 | return fd; |
| 91 | } |
| 92 | |
| 93 | /* posix close() */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 94 | JNIEXPORT jint JVM_Close(jint fd) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 95 | // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR |
| 96 | return close(fd); |
| 97 | } |
| 98 | |
| 99 | /* posix read() */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 100 | JNIEXPORT jint JVM_Read(jint fd, char* buf, jint nbytes) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 101 | return TEMP_FAILURE_RETRY(read(fd, buf, nbytes)); |
| 102 | } |
| 103 | |
| 104 | /* posix write(); is used to write messages to stderr */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 105 | JNIEXPORT jint JVM_Write(jint fd, char* buf, jint nbytes) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 106 | return TEMP_FAILURE_RETRY(write(fd, buf, nbytes)); |
| 107 | } |
| 108 | |
| 109 | /* posix lseek() */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 110 | JNIEXPORT jlong JVM_Lseek(jint fd, jlong offset, jint whence) { |
Narayan Kamath | 5b2cfd8 | 2016-04-15 17:51:55 +0100 | [diff] [blame] | 111 | #if !defined(__APPLE__) |
Narayan Kamath | 77f5d65 | 2016-04-15 15:57:28 +0100 | [diff] [blame] | 112 | // NOTE: Using TEMP_FAILURE_RETRY here is busted for LP32 on glibc - the return |
| 113 | // value will be coerced into an int32_t. |
| 114 | // |
| 115 | // lseek64 isn't specified to return EINTR so it shouldn't be necessary |
| 116 | // anyway. |
| 117 | return lseek64(fd, offset, whence); |
Narayan Kamath | 5b2cfd8 | 2016-04-15 17:51:55 +0100 | [diff] [blame] | 118 | #else |
| 119 | // NOTE: This code is compiled for Mac OS but isn't ever run on that |
| 120 | // platform. |
| 121 | return lseek(fd, offset, whence); |
| 122 | #endif |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 123 | } |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 124 | |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 125 | /* |
| 126 | * "raw monitors" seem to be expected to behave like non-recursive pthread |
| 127 | * mutexes. They're used by ZipFile. |
| 128 | */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 129 | JNIEXPORT void* JVM_RawMonitorCreate(void) { |
Narayan Kamath | 6c37e9a | 2016-02-09 13:11:09 +0000 | [diff] [blame] | 130 | pthread_mutex_t* mutex = |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 131 | reinterpret_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t))); |
Narayan Kamath | 6c37e9a | 2016-02-09 13:11:09 +0000 | [diff] [blame] | 132 | CHECK(mutex != nullptr); |
| 133 | CHECK_PTHREAD_CALL(pthread_mutex_init, (mutex, nullptr), "JVM_RawMonitorCreate"); |
| 134 | return mutex; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 137 | JNIEXPORT void JVM_RawMonitorDestroy(void* mon) { |
Narayan Kamath | 6c37e9a | 2016-02-09 13:11:09 +0000 | [diff] [blame] | 138 | CHECK_PTHREAD_CALL(pthread_mutex_destroy, |
| 139 | (reinterpret_cast<pthread_mutex_t*>(mon)), |
| 140 | "JVM_RawMonitorDestroy"); |
| 141 | free(mon); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 144 | JNIEXPORT jint JVM_RawMonitorEnter(void* mon) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 145 | return pthread_mutex_lock(reinterpret_cast<pthread_mutex_t*>(mon)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 148 | JNIEXPORT void JVM_RawMonitorExit(void* mon) { |
Narayan Kamath | 6c37e9a | 2016-02-09 13:11:09 +0000 | [diff] [blame] | 149 | CHECK_PTHREAD_CALL(pthread_mutex_unlock, |
| 150 | (reinterpret_cast<pthread_mutex_t*>(mon)), |
| 151 | "JVM_RawMonitorExit"); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 154 | JNIEXPORT char* JVM_NativePath(char* path) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 155 | return path; |
| 156 | } |
| 157 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 158 | JNIEXPORT jint JVM_GetLastErrorString(char* buf, int len) { |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 159 | #if defined(__GLIBC__) || defined(__BIONIC__) |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 160 | if (len == 0) { |
| 161 | return 0; |
| 162 | } |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 163 | |
Narayan Kamath | 44ba97e | 2016-02-05 14:58:02 +0000 | [diff] [blame] | 164 | const int err = errno; |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 165 | char* result = strerror_r(err, buf, len); |
| 166 | if (result != buf) { |
| 167 | strncpy(buf, result, len); |
| 168 | buf[len - 1] = '\0'; |
| 169 | } |
| 170 | |
| 171 | return strlen(buf); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 172 | #else |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 173 | UNUSED(buf); |
| 174 | UNUSED(len); |
| 175 | return -1; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 176 | #endif |
| 177 | } |
| 178 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 179 | JNIEXPORT int jio_fprintf(FILE* fp, const char* fmt, ...) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 180 | va_list args; |
| 181 | |
| 182 | va_start(args, fmt); |
| 183 | int len = jio_vfprintf(fp, fmt, args); |
| 184 | va_end(args); |
| 185 | |
| 186 | return len; |
| 187 | } |
| 188 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 189 | JNIEXPORT int jio_vfprintf(FILE* fp, const char* fmt, va_list args) { |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 190 | assert(fp != nullptr); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 191 | return vfprintf(fp, fmt, args); |
| 192 | } |
| 193 | |
| 194 | /* posix fsync() */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 195 | JNIEXPORT jint JVM_Sync(jint fd) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 196 | return TEMP_FAILURE_RETRY(fsync(fd)); |
| 197 | } |
| 198 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 199 | JNIEXPORT void* JVM_FindLibraryEntry(void* handle, const char* name) { |
Przemyslaw Szczepaniak | 67d39ad | 2015-07-03 13:54:00 +0100 | [diff] [blame] | 200 | return dlsym(handle, name); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Narayan Kamath | 44ba97e | 2016-02-05 14:58:02 +0000 | [diff] [blame] | 203 | JNIEXPORT jlong JVM_CurrentTimeMillis(JNIEnv* env ATTRIBUTE_UNUSED, |
| 204 | jclass clazz ATTRIBUTE_UNUSED) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 205 | struct timeval tv; |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 206 | gettimeofday(&tv, (struct timezone *) nullptr); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 207 | jlong when = tv.tv_sec * 1000LL + tv.tv_usec / 1000; |
| 208 | return when; |
| 209 | } |
| 210 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 211 | JNIEXPORT jint JVM_Socket(jint domain, jint type, jint protocol) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 212 | return TEMP_FAILURE_RETRY(socket(domain, type, protocol)); |
| 213 | } |
| 214 | |
| 215 | JNIEXPORT jint JVM_InitializeSocketLibrary() { |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 220 | if ((intptr_t)count <= 0) return -1; |
| 221 | return vsnprintf(str, count, fmt, args); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | int jio_snprintf(char *str, size_t count, const char *fmt, ...) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 225 | 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 Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | JNIEXPORT jint JVM_SetSockOpt(jint fd, int level, int optname, |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 234 | const char* optval, int optlen) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 235 | return TEMP_FAILURE_RETRY(setsockopt(fd, level, optname, optval, optlen)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 238 | JNIEXPORT jint JVM_SocketShutdown(jint fd, jint howto) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 239 | return TEMP_FAILURE_RETRY(shutdown(fd, howto)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | JNIEXPORT jint JVM_GetSockOpt(jint fd, int level, int optname, char* optval, |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 243 | int* optlen) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 244 | socklen_t len = *optlen; |
| 245 | int cc = TEMP_FAILURE_RETRY(getsockopt(fd, level, optname, optval, &len)); |
| 246 | *optlen = len; |
| 247 | return cc; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 250 | JNIEXPORT jint JVM_GetSockName(jint fd, struct sockaddr* addr, int* addrlen) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 251 | socklen_t len = *addrlen; |
| 252 | int cc = TEMP_FAILURE_RETRY(getsockname(fd, addr, &len)); |
| 253 | *addrlen = len; |
| 254 | return cc; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 257 | JNIEXPORT jint JVM_SocketAvailable(jint fd, jint* result) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 258 | if (TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, result)) < 0) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 259 | return JNI_FALSE; |
| 260 | } |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 261 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 262 | return JNI_TRUE; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 265 | JNIEXPORT jint JVM_Send(jint fd, char* buf, jint nBytes, jint flags) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 266 | return TEMP_FAILURE_RETRY(send(fd, buf, nBytes, flags)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 269 | JNIEXPORT jint JVM_SocketClose(jint fd) { |
Narayan Kamath | 44ba97e | 2016-02-05 14:58:02 +0000 | [diff] [blame] | 270 | // Don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR. |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 271 | return close(fd); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 274 | JNIEXPORT jint JVM_Listen(jint fd, jint count) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 275 | return TEMP_FAILURE_RETRY(listen(fd, count)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 278 | JNIEXPORT jint JVM_Connect(jint fd, struct sockaddr* addr, jint addrlen) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 279 | return TEMP_FAILURE_RETRY(connect(fd, addr, addrlen)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 282 | JNIEXPORT int JVM_GetHostName(char* name, int namelen) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 283 | return TEMP_FAILURE_RETRY(gethostname(name, namelen)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 286 | JNIEXPORT jstring JVM_InternString(JNIEnv* env, jstring jstr) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 287 | art::ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 288 | art::ObjPtr<art::mirror::String> s = soa.Decode<art::mirror::String>(jstr); |
| 289 | return soa.AddLocalReference<jstring>(s->Intern()); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | JNIEXPORT jlong JVM_FreeMemory(void) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 293 | return art::Runtime::Current()->GetHeap()->GetFreeMemory(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | JNIEXPORT jlong JVM_TotalMemory(void) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 297 | return art::Runtime::Current()->GetHeap()->GetTotalMemory(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | JNIEXPORT jlong JVM_MaxMemory(void) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 301 | return art::Runtime::Current()->GetHeap()->GetMaxMemory(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | JNIEXPORT void JVM_GC(void) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 305 | if (art::Runtime::Current()->IsExplicitGcDisabled()) { |
| 306 | LOG(INFO) << "Explicit GC skipped."; |
| 307 | return; |
| 308 | } |
Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 309 | art::Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references */ false); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Przemyslaw Szczepaniak | 6c0ea27 | 2015-09-23 08:48:00 +0100 | [diff] [blame] | 312 | JNIEXPORT __attribute__((noreturn)) void JVM_Exit(jint status) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 313 | LOG(INFO) << "System.exit called, status: " << status; |
| 314 | art::Runtime::Current()->CallExitHook(status); |
| 315 | exit(status); |
| 316 | } |
| 317 | |
Dimitry Ivanov | 942dc298 | 2016-02-24 13:33:33 -0800 | [diff] [blame] | 318 | JNIEXPORT jstring JVM_NativeLoad(JNIEnv* env, |
| 319 | jstring javaFilename, |
Andreas Gampe | 473191c | 2017-12-28 16:55:31 -0800 | [diff] [blame] | 320 | jobject javaLoader) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 321 | ScopedUtfChars filename(env, javaFilename); |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 322 | if (filename.c_str() == nullptr) { |
| 323 | return nullptr; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Dmitriy Ivanov | 3e38172 | 2015-11-23 17:40:11 -0800 | [diff] [blame] | 326 | std::string error_msg; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 327 | { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 328 | art::JavaVMExt* vm = art::Runtime::Current()->GetJavaVM(); |
Dimitry Ivanov | 986f650 | 2015-12-15 14:08:18 -0800 | [diff] [blame] | 329 | bool success = vm->LoadNativeLibrary(env, |
| 330 | filename.c_str(), |
| 331 | javaLoader, |
Dimitry Ivanov | 986f650 | 2015-12-15 14:08:18 -0800 | [diff] [blame] | 332 | &error_msg); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 333 | if (success) { |
| 334 | return nullptr; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF. |
| 339 | env->ExceptionClear(); |
Dmitriy Ivanov | 3e38172 | 2015-11-23 17:40:11 -0800 | [diff] [blame] | 340 | return env->NewStringUTF(error_msg.c_str()); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | JNIEXPORT void JVM_StartThread(JNIEnv* env, jobject jthread, jlong stack_size, jboolean daemon) { |
| 344 | art::Thread::CreateNativeThread(env, jthread, stack_size, daemon == JNI_TRUE); |
| 345 | } |
| 346 | |
| 347 | JNIEXPORT void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio) { |
| 348 | art::ScopedObjectAccess soa(env); |
| 349 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 350 | art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 351 | if (thread != nullptr) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 352 | thread->SetNativePriority(prio); |
| 353 | } |
| 354 | } |
| 355 | |
Przemyslaw Szczepaniak | 6c0ea27 | 2015-09-23 08:48:00 +0100 | [diff] [blame] | 356 | JNIEXPORT void JVM_Yield(JNIEnv* env ATTRIBUTE_UNUSED, jclass threadClass ATTRIBUTE_UNUSED) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 357 | sched_yield(); |
| 358 | } |
| 359 | |
Przemyslaw Szczepaniak | 6c0ea27 | 2015-09-23 08:48:00 +0100 | [diff] [blame] | 360 | JNIEXPORT void JVM_Sleep(JNIEnv* env, jclass threadClass ATTRIBUTE_UNUSED, |
| 361 | jobject java_lock, jlong millis) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 362 | art::ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 363 | art::ObjPtr<art::mirror::Object> lock = soa.Decode<art::mirror::Object>(java_lock); |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 364 | art::Monitor::Wait(art::Thread::Current(), lock.Ptr(), millis, 0, true, art::kSleeping); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Przemyslaw Szczepaniak | 6c0ea27 | 2015-09-23 08:48:00 +0100 | [diff] [blame] | 367 | JNIEXPORT jobject JVM_CurrentThread(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 368 | art::ScopedFastNativeObjectAccess soa(env); |
| 369 | return soa.AddLocalReference<jobject>(soa.Self()->GetPeer()); |
| 370 | } |
| 371 | |
| 372 | JNIEXPORT void JVM_Interrupt(JNIEnv* env, jobject jthread) { |
| 373 | art::ScopedFastNativeObjectAccess soa(env); |
| 374 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 375 | art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); |
| 376 | if (thread != nullptr) { |
| 377 | thread->Interrupt(soa.Self()); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | JNIEXPORT jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clearInterrupted) { |
| 382 | if (clearInterrupted) { |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 383 | return static_cast<art::JNIEnvExt*>(env)->GetSelf()->Interrupted() ? JNI_TRUE : JNI_FALSE; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 384 | } else { |
| 385 | art::ScopedFastNativeObjectAccess soa(env); |
| 386 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 387 | art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); |
| 388 | return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE; |
| 389 | } |
| 390 | } |
| 391 | |
Przemyslaw Szczepaniak | 6c0ea27 | 2015-09-23 08:48:00 +0100 | [diff] [blame] | 392 | JNIEXPORT jboolean JVM_HoldsLock(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED, jobject jobj) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 393 | art::ScopedObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 394 | art::ObjPtr<art::mirror::Object> object = soa.Decode<art::mirror::Object>(jobj); |
| 395 | if (object == nullptr) { |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 396 | art::ThrowNullPointerException("object == null"); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 397 | return JNI_FALSE; |
| 398 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 399 | return soa.Self()->HoldsLock(object); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | JNIEXPORT void JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring java_name) { |
| 403 | ScopedUtfChars name(env, java_name); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 404 | { |
| 405 | art::ScopedObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 406 | if (soa.Decode<art::mirror::Object>(jthread) == soa.Self()->GetPeer()) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 407 | soa.Self()->SetThreadName(name.c_str()); |
| 408 | return; |
| 409 | } |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 410 | } |
| 411 | // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the |
| 412 | // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock |
| 413 | // in the DDMS send code. |
| 414 | art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList(); |
| 415 | bool timed_out; |
| 416 | // Take suspend thread lock to avoid races with threads trying to suspend this one. |
| 417 | art::Thread* thread; |
| 418 | { |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 419 | thread = thread_list->SuspendThreadByPeer(jthread, |
| 420 | true, |
| 421 | art::SuspendReason::kInternal, |
| 422 | &timed_out); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 423 | } |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 424 | if (thread != nullptr) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 425 | { |
| 426 | art::ScopedObjectAccess soa(env); |
| 427 | thread->SetThreadName(name.c_str()); |
| 428 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 429 | bool resumed = thread_list->Resume(thread, art::SuspendReason::kInternal); |
| 430 | DCHECK(resumed); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 431 | } else if (timed_out) { |
| 432 | LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread " |
| 433 | "failed to suspend within a generous timeout."; |
| 434 | } |
| 435 | } |
| 436 | |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 437 | JNIEXPORT __attribute__((noreturn)) jint JVM_IHashCode(JNIEnv* env ATTRIBUTE_UNUSED, |
Narayan Kamath | 68d8ff4 | 2015-11-16 12:31:28 +0000 | [diff] [blame] | 438 | jobject javaObject ATTRIBUTE_UNUSED) { |
| 439 | UNIMPLEMENTED(FATAL) << "JVM_IHashCode is not implemented"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 440 | UNREACHABLE(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 443 | JNIEXPORT __attribute__((noreturn)) jlong JVM_NanoTime(JNIEnv* env ATTRIBUTE_UNUSED, jclass unused ATTRIBUTE_UNUSED) { |
Narayan Kamath | 68d8ff4 | 2015-11-16 12:31:28 +0000 | [diff] [blame] | 444 | UNIMPLEMENTED(FATAL) << "JVM_NanoTime is not implemented"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 445 | UNREACHABLE(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 446 | } |
Narayan Kamath | b780289 | 2015-10-01 12:34:52 +0100 | [diff] [blame] | 447 | |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 448 | JNIEXPORT __attribute__((noreturn)) void JVM_ArrayCopy(JNIEnv* /* env */, jclass /* unused */, jobject /* javaSrc */, |
Narayan Kamath | 68d8ff4 | 2015-11-16 12:31:28 +0000 | [diff] [blame] | 449 | jint /* srcPos */, jobject /* javaDst */, jint /* dstPos */, |
| 450 | jint /* length */) { |
| 451 | UNIMPLEMENTED(FATAL) << "JVM_ArrayCopy is not implemented"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 452 | UNREACHABLE(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 455 | JNIEXPORT __attribute__((noreturn)) jint JVM_FindSignal(const char* name ATTRIBUTE_UNUSED) { |
Narayan Kamath | 36379fd | 2015-08-13 17:33:24 +0100 | [diff] [blame] | 456 | LOG(FATAL) << "JVM_FindSignal is not implemented"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 457 | UNREACHABLE(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 460 | JNIEXPORT __attribute__((noreturn)) void* JVM_RegisterSignal(jint signum ATTRIBUTE_UNUSED, void* handler ATTRIBUTE_UNUSED) { |
Narayan Kamath | 36379fd | 2015-08-13 17:33:24 +0100 | [diff] [blame] | 461 | LOG(FATAL) << "JVM_RegisterSignal is not implemented"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 462 | UNREACHABLE(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 465 | JNIEXPORT __attribute__((noreturn)) jboolean JVM_RaiseSignal(jint signum ATTRIBUTE_UNUSED) { |
Narayan Kamath | 36379fd | 2015-08-13 17:33:24 +0100 | [diff] [blame] | 466 | LOG(FATAL) << "JVM_RaiseSignal is not implemented"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame^] | 467 | UNREACHABLE(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Przemyslaw Szczepaniak | 6c0ea27 | 2015-09-23 08:48:00 +0100 | [diff] [blame] | 470 | JNIEXPORT __attribute__((noreturn)) void JVM_Halt(jint code) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 471 | exit(code); |
| 472 | } |
Przemyslaw Szczepaniak | b02d9b7 | 2015-08-20 15:46:16 +0100 | [diff] [blame] | 473 | |
| 474 | JNIEXPORT jboolean JVM_IsNaN(jdouble d) { |
| 475 | return isnan(d); |
| 476 | } |