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