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