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