Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Services that OpenJDK expects the VM to provide. |
| 19 | */ |
| 20 | #include<stdio.h> |
| 21 | #include <dlfcn.h> |
| 22 | #include <limits.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "common_throws.h" |
| 26 | #include "gc/heap.h" |
| 27 | #include "thread.h" |
| 28 | #include "thread_list.h" |
| 29 | #include "runtime.h" |
| 30 | #include "handle_scope-inl.h" |
| 31 | #include "scoped_thread_state_change.h" |
| 32 | #include "ScopedUtfChars.h" |
| 33 | #include "mirror/class_loader.h" |
| 34 | #include "verify_object-inl.h" |
| 35 | #include "base/logging.h" |
| 36 | #include "base/macros.h" |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 37 | #include "../../libcore/ojluni/src/main/native/jvm.h" // TODO(narayan): fix it |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 38 | #include "jni_internal.h" |
| 39 | #include "mirror/string-inl.h" |
| 40 | #include "scoped_fast_native_object_access.h" |
| 41 | #include "ScopedLocalRef.h" |
| 42 | #include <sys/time.h> |
| 43 | #include <sys/socket.h> |
| 44 | #include <sys/ioctl.h> |
| 45 | |
| 46 | #undef LOG_TAG |
| 47 | #define LOG_TAG "artopenjdx" |
| 48 | |
| 49 | /* posix open() with extensions; used by e.g. ZipFile */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 50 | JNIEXPORT jint JVM_Open(const char* fname, jint flags, jint mode) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 51 | LOG(DEBUG) << "JVM_Open fname='" << fname << "', flags=" << flags << ", mode=" << mode; |
| 52 | |
| 53 | /* |
| 54 | * The call is expected to handle JVM_O_DELETE, which causes the file |
| 55 | * to be removed after it is opened. Also, some code seems to |
| 56 | * want the special return value JVM_EEXIST if the file open fails |
| 57 | * due to O_EXCL. |
| 58 | */ |
| 59 | int fd = TEMP_FAILURE_RETRY(open(fname, flags & ~JVM_O_DELETE, mode)); |
| 60 | if (fd < 0) { |
| 61 | int err = errno; |
| 62 | LOG(DEBUG) << "open(" << fname << ") failed: " << strerror(errno); |
| 63 | if (err == EEXIST) { |
| 64 | return JVM_EEXIST; |
| 65 | } else { |
| 66 | return -1; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (flags & JVM_O_DELETE) { |
| 71 | LOG(DEBUG) << "Deleting '" << fname << "' after open\n"; |
| 72 | if (unlink(fname) != 0) { |
| 73 | LOG(WARNING) << "Post-open deletion of '" << fname << "' failed: " << strerror(errno); |
| 74 | } |
| 75 | /* ignore */ |
| 76 | } |
| 77 | |
| 78 | LOG(VERBOSE) << "open(" << fname << ") --> " << fd; |
| 79 | return fd; |
| 80 | } |
| 81 | |
| 82 | /* posix close() */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 83 | JNIEXPORT jint JVM_Close(jint fd) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 84 | LOG(DEBUG) << "JVM_Close fd=" << fd; |
| 85 | // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR |
| 86 | return close(fd); |
| 87 | } |
| 88 | |
| 89 | /* posix read() */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 90 | JNIEXPORT jint JVM_Read(jint fd, char* buf, jint nbytes) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 91 | LOG(DEBUG) << "JVM_Read fd=" << fd << ", buf='" << buf << "', nbytes=" << nbytes; |
| 92 | return TEMP_FAILURE_RETRY(read(fd, buf, nbytes)); |
| 93 | } |
| 94 | |
| 95 | /* posix write(); is used to write messages to stderr */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 96 | JNIEXPORT jint JVM_Write(jint fd, char* buf, jint nbytes) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 97 | LOG(DEBUG) << "JVM_Write fd=" << fd << ", buf='" << buf << "', nbytes=" << nbytes; |
| 98 | return TEMP_FAILURE_RETRY(write(fd, buf, nbytes)); |
| 99 | } |
| 100 | |
| 101 | /* posix lseek() */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 102 | JNIEXPORT jlong JVM_Lseek(jint fd, jlong offset, jint whence) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 103 | LOG(DEBUG) << "JVM_Lseek fd=" << fd << ", offset=" << offset << ", whence=" << whence; |
| 104 | return TEMP_FAILURE_RETRY(lseek(fd, offset, whence)); |
| 105 | } |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 106 | |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 107 | /* |
| 108 | * "raw monitors" seem to be expected to behave like non-recursive pthread |
| 109 | * mutexes. They're used by ZipFile. |
| 110 | */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 111 | JNIEXPORT void* JVM_RawMonitorCreate(void) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 112 | LOG(DEBUG) << "JVM_RawMonitorCreate"; |
| 113 | pthread_mutex_t* newMutex = |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 114 | reinterpret_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t))); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 115 | pthread_mutex_init(newMutex, NULL); |
| 116 | return newMutex; |
| 117 | } |
| 118 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 119 | JNIEXPORT void JVM_RawMonitorDestroy(void* mon) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 120 | LOG(DEBUG) << "JVM_RawMonitorDestroy mon=" << mon; |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 121 | pthread_mutex_destroy(reinterpret_cast<pthread_mutex_t*>(mon)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 124 | JNIEXPORT jint JVM_RawMonitorEnter(void* mon) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 125 | LOG(DEBUG) << "JVM_RawMonitorEnter mon=" << mon; |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 126 | return pthread_mutex_lock(reinterpret_cast<pthread_mutex_t*>(mon)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 129 | JNIEXPORT void JVM_RawMonitorExit(void* mon) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 130 | LOG(DEBUG) << "JVM_RawMonitorExit mon=" << mon; |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 131 | pthread_mutex_unlock(reinterpret_cast<pthread_mutex_t*>(mon)); |
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 char* JVM_NativePath(char* path) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 135 | LOG(DEBUG) << "JVM_NativePath path='" << path << "'"; |
| 136 | return path; |
| 137 | } |
| 138 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 139 | JNIEXPORT jint JVM_GetLastErrorString(char* buf, int len) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 140 | int err = errno; // grab before JVM_TRACE can trash it |
| 141 | LOG(DEBUG) << "JVM_GetLastErrorString buf=" << buf << ", len=" << len; |
| 142 | |
| 143 | #ifdef __GLIBC__ |
| 144 | if (len == 0) |
| 145 | return 0; |
| 146 | |
| 147 | char* result = strerror_r(err, buf, len); |
| 148 | if (result != buf) { |
| 149 | strncpy(buf, result, len); |
| 150 | buf[len-1] = '\0'; |
| 151 | } |
| 152 | return strlen(buf); |
| 153 | #else |
| 154 | return strerror_r(err, buf, len); |
| 155 | #endif |
| 156 | } |
| 157 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 158 | JNIEXPORT int jio_fprintf(FILE* fp, const char* fmt, ...) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 159 | va_list args; |
| 160 | |
| 161 | va_start(args, fmt); |
| 162 | int len = jio_vfprintf(fp, fmt, args); |
| 163 | va_end(args); |
| 164 | |
| 165 | return len; |
| 166 | } |
| 167 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 168 | JNIEXPORT int jio_vfprintf(FILE* fp, const char* fmt, va_list args) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 169 | assert(fp != NULL); |
| 170 | return vfprintf(fp, fmt, args); |
| 171 | } |
| 172 | |
| 173 | /* posix fsync() */ |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 174 | JNIEXPORT jint JVM_Sync(jint fd) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 175 | LOG(DEBUG) << "JVM_Sync fd=" << fd; |
| 176 | return TEMP_FAILURE_RETRY(fsync(fd)); |
| 177 | } |
| 178 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 179 | JNIEXPORT void* JVM_FindLibraryEntry(void* handle, const char* name) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 180 | LOG(DEBUG) << "JVM_FindLibraryEntry handle=" << handle << " name=" << name; |
Przemyslaw Szczepaniak | 67d39ad | 2015-07-03 13:54:00 +0100 | [diff] [blame] | 181 | return dlsym(handle, name); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 184 | JNIEXPORT jlong JVM_CurrentTimeMillis(JNIEnv* env, jclass unused) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 185 | LOG(DEBUG) << "JVM_CurrentTimeMillis env=" << env; |
| 186 | struct timeval tv; |
| 187 | |
| 188 | gettimeofday(&tv, (struct timezone *) NULL); |
| 189 | jlong when = tv.tv_sec * 1000LL + tv.tv_usec / 1000; |
| 190 | return when; |
| 191 | } |
| 192 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 193 | JNIEXPORT jint JVM_Socket(jint domain, jint type, jint protocol) { |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 194 | LOG(DEBUG) << "JVM_Socket domain=" << domain << ", type=" << type << ", protocol=" << protocol; |
| 195 | |
| 196 | return TEMP_FAILURE_RETRY(socket(domain, type, protocol)); |
| 197 | } |
| 198 | |
| 199 | JNIEXPORT jint JVM_InitializeSocketLibrary() { |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | 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] | 204 | if ((intptr_t)count <= 0) return -1; |
| 205 | return vsnprintf(str, count, fmt, args); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | int jio_snprintf(char *str, size_t count, const char *fmt, ...) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 209 | va_list args; |
| 210 | int len; |
| 211 | va_start(args, fmt); |
| 212 | len = jio_vsnprintf(str, count, fmt, args); |
| 213 | va_end(args); |
| 214 | return len; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | JNIEXPORT jint JVM_SetSockOpt(jint fd, int level, int optname, |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 218 | const char* optval, int optlen) { |
| 219 | LOG(DEBUG) << "JVM_SetSockOpt fd=" << fd << ", level=" << level << ", optname=" << optname |
| 220 | << ", optval=" << optval << ", optlen=" << optlen; |
| 221 | return TEMP_FAILURE_RETRY(setsockopt(fd, level, optname, optval, optlen)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 224 | JNIEXPORT jint JVM_SocketShutdown(jint fd, jint howto) { |
| 225 | LOG(DEBUG) << "JVM_SocketShutdown fd=" << fd << ", howto=" << howto; |
| 226 | return TEMP_FAILURE_RETRY(shutdown(fd, howto)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | JNIEXPORT jint JVM_GetSockOpt(jint fd, int level, int optname, char* optval, |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 230 | int* optlen) { |
| 231 | LOG(DEBUG) << "JVM_GetSockOpt fd=" << fd << ", level=" << level << ", optname=" << optname |
| 232 | << ", optval=" << optval << ", optlen=" << optlen; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 233 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 234 | socklen_t len = *optlen; |
| 235 | int cc = TEMP_FAILURE_RETRY(getsockopt(fd, level, optname, optval, &len)); |
| 236 | *optlen = len; |
| 237 | return cc; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 240 | JNIEXPORT jint JVM_GetSockName(jint fd, struct sockaddr* addr, int* addrlen) { |
| 241 | LOG(DEBUG) << "JVM_GetSockName fd=" << fd << ", addr=" << addr << ", addrlen=" << addrlen; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 242 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 243 | socklen_t len = *addrlen; |
| 244 | int cc = TEMP_FAILURE_RETRY(getsockname(fd, addr, &len)); |
| 245 | *addrlen = len; |
| 246 | return cc; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 249 | JNIEXPORT jint JVM_SocketAvailable(jint fd, jint* result) { |
| 250 | LOG(DEBUG) << "JVM_SocketAvailable fd=" << fd << ", result=" << result; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 251 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 252 | if (TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, result)) < 0) { |
| 253 | LOG(DEBUG) << "ioctl(" << fd << ", FIONREAD) failed: " << strerror(errno); |
| 254 | return JNI_FALSE; |
| 255 | } |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 256 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 257 | return JNI_TRUE; |
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_Send(jint fd, char* buf, jint nBytes, jint flags) { |
| 261 | LOG(DEBUG) << "JVM_Send fd=" << fd << ", buf=" << buf << ", nBytes=" |
| 262 | << nBytes << ", flags=" << flags; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 263 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 264 | return TEMP_FAILURE_RETRY(send(fd, buf, nBytes, flags)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 267 | JNIEXPORT jint JVM_SocketClose(jint fd) { |
| 268 | LOG(DEBUG) << "JVM_SocketClose fd=" << fd; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 269 | |
| 270 | // 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] | 271 | return close(fd); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 274 | JNIEXPORT jint JVM_Listen(jint fd, jint count) { |
| 275 | LOG(DEBUG) << "JVM_Listen fd=" << fd << ", count=" << count; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 276 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 277 | return TEMP_FAILURE_RETRY(listen(fd, count)); |
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_Connect(jint fd, struct sockaddr* addr, jint addrlen) { |
| 281 | LOG(DEBUG) << "JVM_Connect fd=" << fd << ", addr=" << addr << ", addrlen=" << addrlen; |
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 | return TEMP_FAILURE_RETRY(connect(fd, addr, addrlen)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 286 | JNIEXPORT int JVM_GetHostName(char* name, int namelen) { |
| 287 | LOG(DEBUG) << "JVM_GetHostName name=" << name << ", namelen=" << namelen; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 288 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 289 | return TEMP_FAILURE_RETRY(gethostname(name, namelen)); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 292 | JNIEXPORT jstring JVM_InternString(JNIEnv* env, jstring jstr) { |
| 293 | LOG(DEBUG) << "JVM_InternString env=" << env << ", jstr=" << jstr; |
| 294 | art::ScopedFastNativeObjectAccess soa(env); |
| 295 | art::mirror::String* s = soa.Decode<art::mirror::String*>(jstr); |
| 296 | art::mirror::String* result = s->Intern(); |
| 297 | return soa.AddLocalReference<jstring>(result); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | JNIEXPORT jlong JVM_FreeMemory(void) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 301 | return art::Runtime::Current()->GetHeap()->GetFreeMemory(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | JNIEXPORT jlong JVM_TotalMemory(void) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 305 | return art::Runtime::Current()->GetHeap()->GetTotalMemory(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | JNIEXPORT jlong JVM_MaxMemory(void) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 309 | return art::Runtime::Current()->GetHeap()->GetMaxMemory(); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | JNIEXPORT void JVM_GC(void) { |
Narayan Kamath | a0cf5a6 | 2015-09-07 11:41:37 +0100 | [diff] [blame] | 313 | if (art::Runtime::Current()->IsExplicitGcDisabled()) { |
| 314 | LOG(INFO) << "Explicit GC skipped."; |
| 315 | return; |
| 316 | } |
| 317 | art::Runtime::Current()->GetHeap()->CollectGarbage(false); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | JNIEXPORT void JVM_Exit(jint status) { |
| 321 | LOG(INFO) << "System.exit called, status: " << status; |
| 322 | art::Runtime::Current()->CallExitHook(status); |
| 323 | exit(status); |
| 324 | } |
| 325 | |
| 326 | JNIEXPORT jstring JVM_NativeLoad(JNIEnv* env, jstring javaFilename, jobject javaLoader, |
| 327 | jstring javaLdLibraryPath) { |
| 328 | ScopedUtfChars filename(env, javaFilename); |
| 329 | if (filename.c_str() == NULL) { |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | if (javaLdLibraryPath != NULL) { |
| 334 | ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath); |
| 335 | if (ldLibraryPath.c_str() == NULL) { |
| 336 | return NULL; |
| 337 | } |
| 338 | void* sym = dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"); |
| 339 | if (sym != NULL) { |
| 340 | typedef void (*Fn)(const char*); |
| 341 | Fn android_update_LD_LIBRARY_PATH = reinterpret_cast<Fn>(sym); |
| 342 | (*android_update_LD_LIBRARY_PATH)(ldLibraryPath.c_str()); |
| 343 | } else { |
| 344 | LOG(ERROR) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!"; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | std::string detail; |
| 349 | { |
| 350 | art::ScopedObjectAccess soa(env); |
| 351 | art::StackHandleScope<1> hs(soa.Self()); |
| 352 | art::Handle<art::mirror::ClassLoader> classLoader( |
| 353 | hs.NewHandle(soa.Decode<art::mirror::ClassLoader*>(javaLoader))); |
| 354 | art::JavaVMExt* vm = art::Runtime::Current()->GetJavaVM(); |
| 355 | bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, &detail); |
| 356 | if (success) { |
| 357 | return nullptr; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF. |
| 362 | env->ExceptionClear(); |
| 363 | return env->NewStringUTF(detail.c_str()); |
| 364 | } |
| 365 | |
| 366 | JNIEXPORT void JVM_StartThread(JNIEnv* env, jobject jthread, jlong stack_size, jboolean daemon) { |
| 367 | art::Thread::CreateNativeThread(env, jthread, stack_size, daemon == JNI_TRUE); |
| 368 | } |
| 369 | |
| 370 | JNIEXPORT void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio) { |
| 371 | art::ScopedObjectAccess soa(env); |
| 372 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 373 | art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); |
| 374 | if (thread != NULL) { |
| 375 | thread->SetNativePriority(prio); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | JNIEXPORT void JVM_Yield(JNIEnv* env, jclass threadClass) { |
| 380 | sched_yield(); |
| 381 | } |
| 382 | |
| 383 | JNIEXPORT void JVM_Sleep(JNIEnv* env, jclass threadClass, jobject java_lock, jlong millis) { |
| 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 | |
| 389 | JNIEXPORT jobject JVM_CurrentThread(JNIEnv* env, jclass unused) { |
| 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 | |
| 414 | JNIEXPORT jboolean JVM_HoldsLock(JNIEnv* env, jclass unused, jobject jobj) { |
| 415 | art::ScopedObjectAccess soa(env); |
| 416 | art::mirror::Object* object = soa.Decode<art::mirror::Object*>(jobj); |
| 417 | if (object == NULL) { |
| 418 | art::ThrowNullPointerException(NULL, "object == null"); |
| 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); |
| 426 | art::Thread* self; |
| 427 | { |
| 428 | art::ScopedObjectAccess soa(env); |
| 429 | if (soa.Decode<art::mirror::Object*>(jthread) == soa.Self()->GetPeer()) { |
| 430 | soa.Self()->SetThreadName(name.c_str()); |
| 431 | return; |
| 432 | } |
| 433 | self = soa.Self(); |
| 434 | } |
| 435 | // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the |
| 436 | // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock |
| 437 | // in the DDMS send code. |
| 438 | art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList(); |
| 439 | bool timed_out; |
| 440 | // Take suspend thread lock to avoid races with threads trying to suspend this one. |
| 441 | art::Thread* thread; |
| 442 | { |
| 443 | art::MutexLock mu(self, *art::Locks::thread_list_suspend_thread_lock_); |
| 444 | thread = thread_list->SuspendThreadByPeer(jthread, true, false, &timed_out); |
| 445 | } |
| 446 | if (thread != NULL) { |
| 447 | { |
| 448 | art::ScopedObjectAccess soa(env); |
| 449 | thread->SetThreadName(name.c_str()); |
| 450 | } |
| 451 | thread_list->Resume(thread, false); |
| 452 | } else if (timed_out) { |
| 453 | LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread " |
| 454 | "failed to suspend within a generous timeout."; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | JNIEXPORT jint JVM_IHashCode(JNIEnv* env, jobject javaObject) { |
| 459 | if (UNLIKELY(javaObject == nullptr)) { |
| 460 | return 0; |
| 461 | } |
| 462 | art::ScopedFastNativeObjectAccess soa(env); |
| 463 | art::mirror::Object* o = soa.Decode<art::mirror::Object*>(javaObject); |
| 464 | return static_cast<jint>(o->IdentityHashCode()); |
| 465 | } |
| 466 | |
| 467 | JNIEXPORT jlong JVM_NanoTime(JNIEnv* env, jclass unused) { |
| 468 | #if defined(HAVE_POSIX_CLOCKS) |
| 469 | timespec now; |
| 470 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 471 | return now.tv_sec * 1000000000LL + now.tv_nsec; |
| 472 | #else |
| 473 | timeval now; |
| 474 | gettimeofday(&now, NULL); |
| 475 | return static_cast<jlong>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL; |
| 476 | #endif |
| 477 | } |
| 478 | static void ThrowArrayStoreException_NotAnArray(const char* identifier, art::mirror::Object* array) |
| 479 | SHARED_LOCKS_REQUIRED(art::Locks::mutator_lock_) { |
| 480 | std::string actualType(art::PrettyTypeOf(array)); |
| 481 | art::Thread* self = art::Thread::Current(); |
| 482 | art::ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 483 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;", |
| 484 | "%s of type %s is not an array", identifier, actualType.c_str()); |
| 485 | } |
| 486 | |
| 487 | JNIEXPORT void JVM_ArrayCopy(JNIEnv* env, jclass unused, jobject javaSrc, |
| 488 | jint srcPos, jobject javaDst, jint dstPos, jint length) { |
| 489 | // The API is defined in terms of length, but length is somewhat overloaded so we use count. |
| 490 | const jint count = length; |
| 491 | art::ScopedFastNativeObjectAccess soa(env); |
| 492 | |
| 493 | // Null pointer checks. |
| 494 | if (UNLIKELY(javaSrc == nullptr)) { |
| 495 | art::ThrowNullPointerException(nullptr, "src == null"); |
| 496 | return; |
| 497 | } |
| 498 | if (UNLIKELY(javaDst == nullptr)) { |
| 499 | art::ThrowNullPointerException(nullptr, "dst == null"); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | // Make sure source and destination are both arrays. |
| 504 | art::mirror::Object* srcObject = soa.Decode<art::mirror::Object*>(javaSrc); |
| 505 | if (UNLIKELY(!srcObject->IsArrayInstance())) { |
| 506 | ThrowArrayStoreException_NotAnArray("source", srcObject); |
| 507 | return; |
| 508 | } |
| 509 | art::mirror::Object* dstObject = soa.Decode<art::mirror::Object*>(javaDst); |
| 510 | if (UNLIKELY(!dstObject->IsArrayInstance())) { |
| 511 | ThrowArrayStoreException_NotAnArray("destination", dstObject); |
| 512 | return; |
| 513 | } |
| 514 | art::mirror::Array* srcArray = srcObject->AsArray(); |
| 515 | art::mirror::Array* dstArray = dstObject->AsArray(); |
| 516 | |
| 517 | // Bounds checking. |
| 518 | if (UNLIKELY(srcPos < 0) || UNLIKELY(dstPos < 0) || UNLIKELY(count < 0) || |
| 519 | UNLIKELY(srcPos > srcArray->GetLength() - count) || |
| 520 | UNLIKELY(dstPos > dstArray->GetLength() - count)) { |
| 521 | art::ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 522 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 523 | "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d", |
| 524 | srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, |
| 525 | count); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | art::mirror::Class* dstComponentType = dstArray->GetClass()->GetComponentType(); |
| 530 | art::mirror::Class* srcComponentType = srcArray->GetClass()->GetComponentType(); |
| 531 | art::Primitive::Type dstComponentPrimitiveType = dstComponentType->GetPrimitiveType(); |
| 532 | |
| 533 | if (LIKELY(srcComponentType == dstComponentType)) { |
| 534 | // Trivial assignability. |
| 535 | switch (dstComponentPrimitiveType) { |
| 536 | case art::Primitive::kPrimVoid: |
| 537 | LOG(FATAL) << "Unreachable, cannot have arrays of type void"; |
| 538 | return; |
| 539 | case art::Primitive::kPrimBoolean: |
| 540 | case art::Primitive::kPrimByte: |
| 541 | DCHECK_EQ(art::Primitive::ComponentSize(dstComponentPrimitiveType), 1U); |
| 542 | dstArray->AsByteSizedArray()->Memmove(dstPos, srcArray->AsByteSizedArray(), srcPos, count); |
| 543 | return; |
| 544 | case art::Primitive::kPrimChar: |
| 545 | case art::Primitive::kPrimShort: |
| 546 | DCHECK_EQ(art::Primitive::ComponentSize(dstComponentPrimitiveType), 2U); |
| 547 | dstArray->AsShortSizedArray()->Memmove(dstPos, srcArray->AsShortSizedArray(), srcPos, count); |
| 548 | return; |
| 549 | case art::Primitive::kPrimInt: |
| 550 | case art::Primitive::kPrimFloat: |
| 551 | DCHECK_EQ(art::Primitive::ComponentSize(dstComponentPrimitiveType), 4U); |
| 552 | dstArray->AsIntArray()->Memmove(dstPos, srcArray->AsIntArray(), srcPos, count); |
| 553 | return; |
| 554 | case art::Primitive::kPrimLong: |
| 555 | case art::Primitive::kPrimDouble: |
| 556 | DCHECK_EQ(art::Primitive::ComponentSize(dstComponentPrimitiveType), 8U); |
| 557 | dstArray->AsLongArray()->Memmove(dstPos, srcArray->AsLongArray(), srcPos, count); |
| 558 | return; |
| 559 | case art::Primitive::kPrimNot: { |
| 560 | art::mirror::ObjectArray<art::mirror::Object>* dstObjArray = dstArray->AsObjectArray<art::mirror::Object>(); |
| 561 | art::mirror::ObjectArray<art::mirror::Object>* srcObjArray = srcArray->AsObjectArray<art::mirror::Object>(); |
| 562 | dstObjArray->AssignableMemmove(dstPos, srcObjArray, srcPos, count); |
| 563 | return; |
| 564 | } |
| 565 | default: |
| 566 | LOG(FATAL) << "Unknown array type: " << art::PrettyTypeOf(srcArray); |
| 567 | return; |
| 568 | } |
| 569 | } |
| 570 | // If one of the arrays holds a primitive type the other array must hold the exact same type. |
| 571 | if (UNLIKELY((dstComponentPrimitiveType != art::Primitive::kPrimNot) || |
| 572 | srcComponentType->IsPrimitive())) { |
| 573 | std::string srcType(art::PrettyTypeOf(srcArray)); |
| 574 | std::string dstType(art::PrettyTypeOf(dstArray)); |
| 575 | art::ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 576 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;", |
| 577 | "Incompatible types: src=%s, dst=%s", |
| 578 | srcType.c_str(), dstType.c_str()); |
| 579 | return; |
| 580 | } |
| 581 | // Arrays hold distinct types and so therefore can't alias - use memcpy instead of memmove. |
| 582 | art::mirror::ObjectArray<art::mirror::Object>* dstObjArray = dstArray->AsObjectArray<art::mirror::Object>(); |
| 583 | art::mirror::ObjectArray<art::mirror::Object>* srcObjArray = srcArray->AsObjectArray<art::mirror::Object>(); |
| 584 | // If we're assigning into say Object[] then we don't need per element checks. |
| 585 | if (dstComponentType->IsAssignableFrom(srcComponentType)) { |
| 586 | dstObjArray->AssignableMemcpy(dstPos, srcObjArray, srcPos, count); |
| 587 | return; |
| 588 | } |
| 589 | dstObjArray->AssignableCheckingMemcpy(dstPos, srcObjArray, srcPos, count, true); |
| 590 | } |
| 591 | |
| 592 | JNIEXPORT jint JVM_FindSignal(const char* name) { |
Narayan Kamath | 36379fd | 2015-08-13 17:33:24 +0100 | [diff] [blame] | 593 | LOG(FATAL) << "JVM_FindSignal is not implemented"; |
| 594 | return 0; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Narayan Kamath | 36379fd | 2015-08-13 17:33:24 +0100 | [diff] [blame] | 597 | JNIEXPORT void* JVM_RegisterSignal(jint signum, void* handler) { |
| 598 | LOG(FATAL) << "JVM_RegisterSignal is not implemented"; |
| 599 | return nullptr; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Narayan Kamath | 36379fd | 2015-08-13 17:33:24 +0100 | [diff] [blame] | 602 | JNIEXPORT jboolean JVM_RaiseSignal(jint signum) { |
| 603 | LOG(FATAL) << "JVM_RaiseSignal is not implemented"; |
| 604 | return JNI_FALSE; |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | JNIEXPORT void JVM_Halt(jint code) { |
| 608 | exit(code); |
| 609 | } |
Przemyslaw Szczepaniak | b02d9b7 | 2015-08-20 15:46:16 +0100 | [diff] [blame] | 610 | |
| 611 | JNIEXPORT jboolean JVM_IsNaN(jdouble d) { |
| 612 | return isnan(d); |
| 613 | } |