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