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