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