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" |
| 37 | #include "../../libcore/ojluni/src/main/native/jvm.h" // TODO(haaawk): fix it |
| 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 */ |
| 50 | JNIEXPORT jint JVM_Open(const char* fname, jint flags, jint mode) |
| 51 | { |
| 52 | LOG(DEBUG) << "JVM_Open fname='" << fname << "', flags=" << flags << ", mode=" << mode; |
| 53 | |
| 54 | /* |
| 55 | * The call is expected to handle JVM_O_DELETE, which causes the file |
| 56 | * to be removed after it is opened. Also, some code seems to |
| 57 | * want the special return value JVM_EEXIST if the file open fails |
| 58 | * due to O_EXCL. |
| 59 | */ |
| 60 | int fd = TEMP_FAILURE_RETRY(open(fname, flags & ~JVM_O_DELETE, mode)); |
| 61 | if (fd < 0) { |
| 62 | int err = errno; |
| 63 | LOG(DEBUG) << "open(" << fname << ") failed: " << strerror(errno); |
| 64 | if (err == EEXIST) { |
| 65 | return JVM_EEXIST; |
| 66 | } else { |
| 67 | return -1; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (flags & JVM_O_DELETE) { |
| 72 | LOG(DEBUG) << "Deleting '" << fname << "' after open\n"; |
| 73 | if (unlink(fname) != 0) { |
| 74 | LOG(WARNING) << "Post-open deletion of '" << fname << "' failed: " << strerror(errno); |
| 75 | } |
| 76 | /* ignore */ |
| 77 | } |
| 78 | |
| 79 | LOG(VERBOSE) << "open(" << fname << ") --> " << fd; |
| 80 | return fd; |
| 81 | } |
| 82 | |
| 83 | /* posix close() */ |
| 84 | JNIEXPORT jint JVM_Close(jint fd) |
| 85 | { |
| 86 | LOG(DEBUG) << "JVM_Close fd=" << fd; |
| 87 | // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR |
| 88 | return close(fd); |
| 89 | } |
| 90 | |
| 91 | /* posix read() */ |
| 92 | JNIEXPORT jint JVM_Read(jint fd, char* buf, jint nbytes) |
| 93 | { |
| 94 | LOG(DEBUG) << "JVM_Read fd=" << fd << ", buf='" << buf << "', nbytes=" << nbytes; |
| 95 | return TEMP_FAILURE_RETRY(read(fd, buf, nbytes)); |
| 96 | } |
| 97 | |
| 98 | /* posix write(); is used to write messages to stderr */ |
| 99 | JNIEXPORT jint JVM_Write(jint fd, char* buf, jint nbytes) |
| 100 | { |
| 101 | LOG(DEBUG) << "JVM_Write fd=" << fd << ", buf='" << buf << "', nbytes=" << nbytes; |
| 102 | return TEMP_FAILURE_RETRY(write(fd, buf, nbytes)); |
| 103 | } |
| 104 | |
| 105 | /* posix lseek() */ |
| 106 | JNIEXPORT jlong JVM_Lseek(jint fd, jlong offset, jint whence) |
| 107 | { |
| 108 | LOG(DEBUG) << "JVM_Lseek fd=" << fd << ", offset=" << offset << ", whence=" << whence; |
| 109 | return TEMP_FAILURE_RETRY(lseek(fd, offset, whence)); |
| 110 | } |
| 111 | /* |
| 112 | * "raw monitors" seem to be expected to behave like non-recursive pthread |
| 113 | * mutexes. They're used by ZipFile. |
| 114 | */ |
| 115 | |
| 116 | JNIEXPORT void* JVM_RawMonitorCreate(void) |
| 117 | { |
| 118 | LOG(DEBUG) << "JVM_RawMonitorCreate"; |
| 119 | pthread_mutex_t* newMutex = |
| 120 | (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); |
| 121 | pthread_mutex_init(newMutex, NULL); |
| 122 | return newMutex; |
| 123 | } |
| 124 | |
| 125 | JNIEXPORT void JVM_RawMonitorDestroy(void* mon) |
| 126 | { |
| 127 | LOG(DEBUG) << "JVM_RawMonitorDestroy mon=" << mon; |
| 128 | pthread_mutex_destroy((pthread_mutex_t*) mon); |
| 129 | } |
| 130 | |
| 131 | JNIEXPORT jint JVM_RawMonitorEnter(void* mon) |
| 132 | { |
| 133 | LOG(DEBUG) << "JVM_RawMonitorEnter mon=" << mon; |
| 134 | return pthread_mutex_lock((pthread_mutex_t*) mon); |
| 135 | } |
| 136 | |
| 137 | JNIEXPORT void JVM_RawMonitorExit(void* mon) |
| 138 | { |
| 139 | LOG(DEBUG) << "JVM_RawMonitorExit mon=" << mon; |
| 140 | pthread_mutex_unlock((pthread_mutex_t*) mon); |
| 141 | } |
| 142 | |
| 143 | JNIEXPORT char* JVM_NativePath(char* path) |
| 144 | { |
| 145 | LOG(DEBUG) << "JVM_NativePath path='" << path << "'"; |
| 146 | return path; |
| 147 | } |
| 148 | |
| 149 | JNIEXPORT jint JVM_GetLastErrorString(char* buf, int len) |
| 150 | { |
| 151 | int err = errno; // grab before JVM_TRACE can trash it |
| 152 | LOG(DEBUG) << "JVM_GetLastErrorString buf=" << buf << ", len=" << len; |
| 153 | |
| 154 | #ifdef __GLIBC__ |
| 155 | if (len == 0) |
| 156 | return 0; |
| 157 | |
| 158 | char* result = strerror_r(err, buf, len); |
| 159 | if (result != buf) { |
| 160 | strncpy(buf, result, len); |
| 161 | buf[len-1] = '\0'; |
| 162 | } |
| 163 | return strlen(buf); |
| 164 | #else |
| 165 | return strerror_r(err, buf, len); |
| 166 | #endif |
| 167 | } |
| 168 | |
| 169 | JNIEXPORT int jio_fprintf(FILE* fp, const char* fmt, ...) |
| 170 | { |
| 171 | va_list args; |
| 172 | |
| 173 | va_start(args, fmt); |
| 174 | int len = jio_vfprintf(fp, fmt, args); |
| 175 | va_end(args); |
| 176 | |
| 177 | return len; |
| 178 | } |
| 179 | |
| 180 | JNIEXPORT int jio_vfprintf(FILE* fp, const char* fmt, va_list args) |
| 181 | { |
| 182 | assert(fp != NULL); |
| 183 | return vfprintf(fp, fmt, args); |
| 184 | } |
| 185 | |
| 186 | /* posix fsync() */ |
| 187 | JNIEXPORT jint JVM_Sync(jint fd) |
| 188 | { |
| 189 | LOG(DEBUG) << "JVM_Sync fd=" << fd; |
| 190 | return TEMP_FAILURE_RETRY(fsync(fd)); |
| 191 | } |
| 192 | |
| 193 | JNIEXPORT void* JVM_FindLibraryEntry(void* handle, const char* name) |
| 194 | { |
| 195 | LOG(DEBUG) << "JVM_FindLibraryEntry handle=" << handle << " name=" << name; |
Przemyslaw Szczepaniak | 67d39ad | 2015-07-03 13:54:00 +0100 | [diff] [blame^] | 196 | return dlsym(handle, name); |
Piotr Jastrzebski | df0b17a | 2015-04-24 09:18:00 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | JNIEXPORT jlong JVM_CurrentTimeMillis(JNIEnv* env, jclass unused) |
| 200 | { |
| 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 | |
| 209 | JNIEXPORT jint JVM_Socket(jint domain, jint type, jint protocol) |
| 210 | { |
| 211 | LOG(DEBUG) << "JVM_Socket domain=" << domain << ", type=" << type << ", protocol=" << protocol; |
| 212 | |
| 213 | return TEMP_FAILURE_RETRY(socket(domain, type, protocol)); |
| 214 | } |
| 215 | |
| 216 | JNIEXPORT jint JVM_InitializeSocketLibrary() { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) { |
| 221 | if ((intptr_t)count <= 0) return -1; |
| 222 | return vsnprintf(str, count, fmt, args); |
| 223 | } |
| 224 | |
| 225 | int jio_snprintf(char *str, size_t count, const char *fmt, ...) { |
| 226 | va_list args; |
| 227 | int len; |
| 228 | va_start(args, fmt); |
| 229 | len = jio_vsnprintf(str, count, fmt, args); |
| 230 | va_end(args); |
| 231 | return len; |
| 232 | } |
| 233 | |
| 234 | JNIEXPORT jint JVM_SetSockOpt(jint fd, int level, int optname, |
| 235 | const char* optval, int optlen) |
| 236 | { |
| 237 | LOG(DEBUG) << "JVM_SetSockOpt fd=" << fd << ", level=" << level << ", optname=" << optname |
| 238 | << ", optval=" << optval << ", optlen=" << optlen; |
| 239 | return TEMP_FAILURE_RETRY(setsockopt(fd, level, optname, optval, optlen)); |
| 240 | } |
| 241 | |
| 242 | JNIEXPORT jint JVM_SocketShutdown(jint fd, jint howto) |
| 243 | { |
| 244 | LOG(DEBUG) << "JVM_SocketShutdown fd=" << fd << ", howto=" << howto; |
| 245 | return TEMP_FAILURE_RETRY(shutdown(fd, howto)); |
| 246 | } |
| 247 | |
| 248 | JNIEXPORT jint JVM_GetSockOpt(jint fd, int level, int optname, char* optval, |
| 249 | int* optlen) |
| 250 | { |
| 251 | LOG(DEBUG) << "JVM_GetSockOpt fd=" << fd << ", level=" << level << ", optname=" << optname |
| 252 | << ", optval=" << optval << ", optlen=" << optlen; |
| 253 | |
| 254 | socklen_t len = *optlen; |
| 255 | int cc = TEMP_FAILURE_RETRY(getsockopt(fd, level, optname, optval, &len)); |
| 256 | *optlen = len; |
| 257 | return cc; |
| 258 | } |
| 259 | |
| 260 | JNIEXPORT jint JVM_GetSockName(jint fd, struct sockaddr* addr, int* addrlen) |
| 261 | { |
| 262 | LOG(DEBUG) << "JVM_GetSockName fd=" << fd << ", addr=" << addr << ", addrlen=" << addrlen; |
| 263 | |
| 264 | socklen_t len = *addrlen; |
| 265 | int cc = TEMP_FAILURE_RETRY(getsockname(fd, addr, &len)); |
| 266 | *addrlen = len; |
| 267 | return cc; |
| 268 | } |
| 269 | |
| 270 | JNIEXPORT jint JVM_SocketAvailable(jint fd, jint* result) |
| 271 | { |
| 272 | LOG(DEBUG) << "JVM_SocketAvailable fd=" << fd << ", result=" << result; |
| 273 | |
| 274 | if (TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, result)) < 0) { |
| 275 | LOG(DEBUG) << "ioctl(" << fd << ", FIONREAD) failed: " << strerror(errno); |
| 276 | return JNI_FALSE; |
| 277 | } |
| 278 | |
| 279 | return JNI_TRUE; |
| 280 | } |
| 281 | |
| 282 | JNIEXPORT jint JVM_Send(jint fd, char* buf, jint nBytes, jint flags) |
| 283 | { |
| 284 | LOG(DEBUG) << "JVM_Send fd=" << fd << ", buf=" << buf << ", nBytes=" << nBytes << ", flags=" |
| 285 | << flags; |
| 286 | |
| 287 | return TEMP_FAILURE_RETRY(send(fd, buf, nBytes, flags)); |
| 288 | } |
| 289 | |
| 290 | JNIEXPORT jint JVM_SocketClose(jint fd) |
| 291 | { |
| 292 | LOG(DEBUG) << "JVM_SocketClose fd=" << fd; |
| 293 | |
| 294 | // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR |
| 295 | return close(fd); |
| 296 | } |
| 297 | |
| 298 | JNIEXPORT jint JVM_Listen(jint fd, jint count) |
| 299 | { |
| 300 | LOG(DEBUG) << "JVM_Listen fd=" << fd << ", count=" << count; |
| 301 | |
| 302 | return TEMP_FAILURE_RETRY(listen(fd, count)); |
| 303 | } |
| 304 | |
| 305 | JNIEXPORT jint JVM_Connect(jint fd, struct sockaddr* addr, jint addrlen) |
| 306 | { |
| 307 | LOG(DEBUG) << "JVM_Connect fd=" << fd << ", addr=" << addr << ", addrlen=" << addrlen; |
| 308 | |
| 309 | return TEMP_FAILURE_RETRY(connect(fd, addr, addrlen)); |
| 310 | } |
| 311 | |
| 312 | JNIEXPORT int JVM_GetHostName(char* name, int namelen) |
| 313 | { |
| 314 | LOG(DEBUG) << "JVM_GetHostName name=" << name << ", namelen=" << namelen; |
| 315 | |
| 316 | return TEMP_FAILURE_RETRY(gethostname(name, namelen)); |
| 317 | } |
| 318 | |
| 319 | JNIEXPORT jstring JVM_InternString(JNIEnv* env, jstring jstr) |
| 320 | { |
| 321 | LOG(DEBUG) << "JVM_InternString env=" << env << ", jstr=" << jstr; |
| 322 | art::ScopedFastNativeObjectAccess soa(env); |
| 323 | art::mirror::String* s = soa.Decode<art::mirror::String*>(jstr); |
| 324 | art::mirror::String* result = s->Intern(); |
| 325 | return soa.AddLocalReference<jstring>(result); |
| 326 | } |
| 327 | |
| 328 | JNIEXPORT jlong JVM_FreeMemory(void) { |
| 329 | return art::Runtime::Current()->GetHeap()->GetFreeMemory(); |
| 330 | } |
| 331 | |
| 332 | JNIEXPORT jlong JVM_TotalMemory(void) { |
| 333 | return art::Runtime::Current()->GetHeap()->GetTotalMemory(); |
| 334 | } |
| 335 | |
| 336 | JNIEXPORT jlong JVM_MaxMemory(void) { |
| 337 | return art::Runtime::Current()->GetHeap()->GetMaxMemory(); |
| 338 | } |
| 339 | |
| 340 | JNIEXPORT void JVM_GC(void) { |
| 341 | if (art::Runtime::Current()->IsExplicitGcDisabled()) { |
| 342 | LOG(INFO) << "Explicit GC skipped."; |
| 343 | return; |
| 344 | } |
| 345 | art::Runtime::Current()->GetHeap()->CollectGarbage(false); |
| 346 | } |
| 347 | |
| 348 | JNIEXPORT void JVM_Exit(jint status) { |
| 349 | LOG(INFO) << "System.exit called, status: " << status; |
| 350 | art::Runtime::Current()->CallExitHook(status); |
| 351 | exit(status); |
| 352 | } |
| 353 | |
| 354 | JNIEXPORT jstring JVM_NativeLoad(JNIEnv* env, jstring javaFilename, jobject javaLoader, |
| 355 | jstring javaLdLibraryPath) { |
| 356 | ScopedUtfChars filename(env, javaFilename); |
| 357 | if (filename.c_str() == NULL) { |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | if (javaLdLibraryPath != NULL) { |
| 362 | ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath); |
| 363 | if (ldLibraryPath.c_str() == NULL) { |
| 364 | return NULL; |
| 365 | } |
| 366 | void* sym = dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"); |
| 367 | if (sym != NULL) { |
| 368 | typedef void (*Fn)(const char*); |
| 369 | Fn android_update_LD_LIBRARY_PATH = reinterpret_cast<Fn>(sym); |
| 370 | (*android_update_LD_LIBRARY_PATH)(ldLibraryPath.c_str()); |
| 371 | } else { |
| 372 | LOG(ERROR) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!"; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | std::string detail; |
| 377 | { |
| 378 | art::ScopedObjectAccess soa(env); |
| 379 | art::StackHandleScope<1> hs(soa.Self()); |
| 380 | art::Handle<art::mirror::ClassLoader> classLoader( |
| 381 | hs.NewHandle(soa.Decode<art::mirror::ClassLoader*>(javaLoader))); |
| 382 | art::JavaVMExt* vm = art::Runtime::Current()->GetJavaVM(); |
| 383 | bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, &detail); |
| 384 | if (success) { |
| 385 | return nullptr; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF. |
| 390 | env->ExceptionClear(); |
| 391 | return env->NewStringUTF(detail.c_str()); |
| 392 | } |
| 393 | |
| 394 | JNIEXPORT void JVM_StartThread(JNIEnv* env, jobject jthread, jlong stack_size, jboolean daemon) { |
| 395 | art::Thread::CreateNativeThread(env, jthread, stack_size, daemon == JNI_TRUE); |
| 396 | } |
| 397 | |
| 398 | JNIEXPORT void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio) { |
| 399 | art::ScopedObjectAccess soa(env); |
| 400 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 401 | art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); |
| 402 | if (thread != NULL) { |
| 403 | thread->SetNativePriority(prio); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | JNIEXPORT void JVM_Yield(JNIEnv* env, jclass threadClass) { |
| 408 | sched_yield(); |
| 409 | } |
| 410 | |
| 411 | JNIEXPORT void JVM_Sleep(JNIEnv* env, jclass threadClass, jobject java_lock, jlong millis) { |
| 412 | art::ScopedFastNativeObjectAccess soa(env); |
| 413 | art::mirror::Object* lock = soa.Decode<art::mirror::Object*>(java_lock); |
| 414 | art::Monitor::Wait(art::Thread::Current(), lock, millis, 0, true, art::kSleeping); |
| 415 | } |
| 416 | |
| 417 | JNIEXPORT jobject JVM_CurrentThread(JNIEnv* env, jclass unused) { |
| 418 | art::ScopedFastNativeObjectAccess soa(env); |
| 419 | return soa.AddLocalReference<jobject>(soa.Self()->GetPeer()); |
| 420 | } |
| 421 | |
| 422 | JNIEXPORT void JVM_Interrupt(JNIEnv* env, jobject jthread) { |
| 423 | art::ScopedFastNativeObjectAccess soa(env); |
| 424 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 425 | art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); |
| 426 | if (thread != nullptr) { |
| 427 | thread->Interrupt(soa.Self()); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | JNIEXPORT jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clearInterrupted) { |
| 432 | if (clearInterrupted) { |
| 433 | return static_cast<art::JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE; |
| 434 | } else { |
| 435 | art::ScopedFastNativeObjectAccess soa(env); |
| 436 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 437 | art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); |
| 438 | return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | JNIEXPORT jboolean JVM_HoldsLock(JNIEnv* env, jclass unused, jobject jobj) { |
| 443 | art::ScopedObjectAccess soa(env); |
| 444 | art::mirror::Object* object = soa.Decode<art::mirror::Object*>(jobj); |
| 445 | if (object == NULL) { |
| 446 | art::ThrowNullPointerException(NULL, "object == null"); |
| 447 | return JNI_FALSE; |
| 448 | } |
| 449 | return soa.Self()->HoldsLock(object); |
| 450 | } |
| 451 | |
| 452 | JNIEXPORT void JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring java_name) { |
| 453 | ScopedUtfChars name(env, java_name); |
| 454 | art::Thread* self; |
| 455 | { |
| 456 | art::ScopedObjectAccess soa(env); |
| 457 | if (soa.Decode<art::mirror::Object*>(jthread) == soa.Self()->GetPeer()) { |
| 458 | soa.Self()->SetThreadName(name.c_str()); |
| 459 | return; |
| 460 | } |
| 461 | self = soa.Self(); |
| 462 | } |
| 463 | // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the |
| 464 | // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock |
| 465 | // in the DDMS send code. |
| 466 | art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList(); |
| 467 | bool timed_out; |
| 468 | // Take suspend thread lock to avoid races with threads trying to suspend this one. |
| 469 | art::Thread* thread; |
| 470 | { |
| 471 | art::MutexLock mu(self, *art::Locks::thread_list_suspend_thread_lock_); |
| 472 | thread = thread_list->SuspendThreadByPeer(jthread, true, false, &timed_out); |
| 473 | } |
| 474 | if (thread != NULL) { |
| 475 | { |
| 476 | art::ScopedObjectAccess soa(env); |
| 477 | thread->SetThreadName(name.c_str()); |
| 478 | } |
| 479 | thread_list->Resume(thread, false); |
| 480 | } else if (timed_out) { |
| 481 | LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread " |
| 482 | "failed to suspend within a generous timeout."; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | JNIEXPORT jint JVM_IHashCode(JNIEnv* env, jobject javaObject) { |
| 487 | if (UNLIKELY(javaObject == nullptr)) { |
| 488 | return 0; |
| 489 | } |
| 490 | art::ScopedFastNativeObjectAccess soa(env); |
| 491 | art::mirror::Object* o = soa.Decode<art::mirror::Object*>(javaObject); |
| 492 | return static_cast<jint>(o->IdentityHashCode()); |
| 493 | } |
| 494 | |
| 495 | JNIEXPORT jlong JVM_NanoTime(JNIEnv* env, jclass unused) { |
| 496 | #if defined(HAVE_POSIX_CLOCKS) |
| 497 | timespec now; |
| 498 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 499 | return now.tv_sec * 1000000000LL + now.tv_nsec; |
| 500 | #else |
| 501 | timeval now; |
| 502 | gettimeofday(&now, NULL); |
| 503 | return static_cast<jlong>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL; |
| 504 | #endif |
| 505 | } |
| 506 | static void ThrowArrayStoreException_NotAnArray(const char* identifier, art::mirror::Object* array) |
| 507 | SHARED_LOCKS_REQUIRED(art::Locks::mutator_lock_) { |
| 508 | std::string actualType(art::PrettyTypeOf(array)); |
| 509 | art::Thread* self = art::Thread::Current(); |
| 510 | art::ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 511 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;", |
| 512 | "%s of type %s is not an array", identifier, actualType.c_str()); |
| 513 | } |
| 514 | |
| 515 | JNIEXPORT void JVM_ArrayCopy(JNIEnv* env, jclass unused, jobject javaSrc, |
| 516 | jint srcPos, jobject javaDst, jint dstPos, jint length) { |
| 517 | // The API is defined in terms of length, but length is somewhat overloaded so we use count. |
| 518 | const jint count = length; |
| 519 | art::ScopedFastNativeObjectAccess soa(env); |
| 520 | |
| 521 | // Null pointer checks. |
| 522 | if (UNLIKELY(javaSrc == nullptr)) { |
| 523 | art::ThrowNullPointerException(nullptr, "src == null"); |
| 524 | return; |
| 525 | } |
| 526 | if (UNLIKELY(javaDst == nullptr)) { |
| 527 | art::ThrowNullPointerException(nullptr, "dst == null"); |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | // Make sure source and destination are both arrays. |
| 532 | art::mirror::Object* srcObject = soa.Decode<art::mirror::Object*>(javaSrc); |
| 533 | if (UNLIKELY(!srcObject->IsArrayInstance())) { |
| 534 | ThrowArrayStoreException_NotAnArray("source", srcObject); |
| 535 | return; |
| 536 | } |
| 537 | art::mirror::Object* dstObject = soa.Decode<art::mirror::Object*>(javaDst); |
| 538 | if (UNLIKELY(!dstObject->IsArrayInstance())) { |
| 539 | ThrowArrayStoreException_NotAnArray("destination", dstObject); |
| 540 | return; |
| 541 | } |
| 542 | art::mirror::Array* srcArray = srcObject->AsArray(); |
| 543 | art::mirror::Array* dstArray = dstObject->AsArray(); |
| 544 | |
| 545 | // Bounds checking. |
| 546 | if (UNLIKELY(srcPos < 0) || UNLIKELY(dstPos < 0) || UNLIKELY(count < 0) || |
| 547 | UNLIKELY(srcPos > srcArray->GetLength() - count) || |
| 548 | UNLIKELY(dstPos > dstArray->GetLength() - count)) { |
| 549 | art::ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 550 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 551 | "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d", |
| 552 | srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, |
| 553 | count); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | art::mirror::Class* dstComponentType = dstArray->GetClass()->GetComponentType(); |
| 558 | art::mirror::Class* srcComponentType = srcArray->GetClass()->GetComponentType(); |
| 559 | art::Primitive::Type dstComponentPrimitiveType = dstComponentType->GetPrimitiveType(); |
| 560 | |
| 561 | if (LIKELY(srcComponentType == dstComponentType)) { |
| 562 | // Trivial assignability. |
| 563 | switch (dstComponentPrimitiveType) { |
| 564 | case art::Primitive::kPrimVoid: |
| 565 | LOG(FATAL) << "Unreachable, cannot have arrays of type void"; |
| 566 | return; |
| 567 | case art::Primitive::kPrimBoolean: |
| 568 | case art::Primitive::kPrimByte: |
| 569 | DCHECK_EQ(art::Primitive::ComponentSize(dstComponentPrimitiveType), 1U); |
| 570 | dstArray->AsByteSizedArray()->Memmove(dstPos, srcArray->AsByteSizedArray(), srcPos, count); |
| 571 | return; |
| 572 | case art::Primitive::kPrimChar: |
| 573 | case art::Primitive::kPrimShort: |
| 574 | DCHECK_EQ(art::Primitive::ComponentSize(dstComponentPrimitiveType), 2U); |
| 575 | dstArray->AsShortSizedArray()->Memmove(dstPos, srcArray->AsShortSizedArray(), srcPos, count); |
| 576 | return; |
| 577 | case art::Primitive::kPrimInt: |
| 578 | case art::Primitive::kPrimFloat: |
| 579 | DCHECK_EQ(art::Primitive::ComponentSize(dstComponentPrimitiveType), 4U); |
| 580 | dstArray->AsIntArray()->Memmove(dstPos, srcArray->AsIntArray(), srcPos, count); |
| 581 | return; |
| 582 | case art::Primitive::kPrimLong: |
| 583 | case art::Primitive::kPrimDouble: |
| 584 | DCHECK_EQ(art::Primitive::ComponentSize(dstComponentPrimitiveType), 8U); |
| 585 | dstArray->AsLongArray()->Memmove(dstPos, srcArray->AsLongArray(), srcPos, count); |
| 586 | return; |
| 587 | case art::Primitive::kPrimNot: { |
| 588 | art::mirror::ObjectArray<art::mirror::Object>* dstObjArray = dstArray->AsObjectArray<art::mirror::Object>(); |
| 589 | art::mirror::ObjectArray<art::mirror::Object>* srcObjArray = srcArray->AsObjectArray<art::mirror::Object>(); |
| 590 | dstObjArray->AssignableMemmove(dstPos, srcObjArray, srcPos, count); |
| 591 | return; |
| 592 | } |
| 593 | default: |
| 594 | LOG(FATAL) << "Unknown array type: " << art::PrettyTypeOf(srcArray); |
| 595 | return; |
| 596 | } |
| 597 | } |
| 598 | // If one of the arrays holds a primitive type the other array must hold the exact same type. |
| 599 | if (UNLIKELY((dstComponentPrimitiveType != art::Primitive::kPrimNot) || |
| 600 | srcComponentType->IsPrimitive())) { |
| 601 | std::string srcType(art::PrettyTypeOf(srcArray)); |
| 602 | std::string dstType(art::PrettyTypeOf(dstArray)); |
| 603 | art::ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 604 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;", |
| 605 | "Incompatible types: src=%s, dst=%s", |
| 606 | srcType.c_str(), dstType.c_str()); |
| 607 | return; |
| 608 | } |
| 609 | // Arrays hold distinct types and so therefore can't alias - use memcpy instead of memmove. |
| 610 | art::mirror::ObjectArray<art::mirror::Object>* dstObjArray = dstArray->AsObjectArray<art::mirror::Object>(); |
| 611 | art::mirror::ObjectArray<art::mirror::Object>* srcObjArray = srcArray->AsObjectArray<art::mirror::Object>(); |
| 612 | // If we're assigning into say Object[] then we don't need per element checks. |
| 613 | if (dstComponentType->IsAssignableFrom(srcComponentType)) { |
| 614 | dstObjArray->AssignableMemcpy(dstPos, srcObjArray, srcPos, count); |
| 615 | return; |
| 616 | } |
| 617 | dstObjArray->AssignableCheckingMemcpy(dstPos, srcObjArray, srcPos, count, true); |
| 618 | } |
| 619 | |
| 620 | JNIEXPORT jint JVM_FindSignal(const char* name) { |
| 621 | static const char* names[] = { |
| 622 | "", "HUP", "INT", "QUIT", |
| 623 | "ILL", "TRAP", "ABRT", "BUS", |
| 624 | "FPE", "KILL", "USR1", "SEGV", |
| 625 | "USR2", "PIPE", "ALRM", "TERM", |
| 626 | NULL |
| 627 | }; |
| 628 | |
| 629 | int i = 0; |
| 630 | while (names[++i] != NULL) { |
| 631 | if (strcmp(name, names[i]) == 0) { |
| 632 | return i; |
| 633 | } |
| 634 | } |
| 635 | LOG(WARNING) << "Signal '" << name << "' not found"; |
| 636 | assert(false); |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | /* signal handler */ |
| 641 | static void internalSignalHandler(int sig) |
| 642 | { |
| 643 | /* |
| 644 | * This is expected to invoke sun.misc.Signal.dispatch(). We really |
| 645 | * don't want to do that directly from a signal handler, so if we |
| 646 | * decide we need this we should hook it into the safe-point mechanism. |
| 647 | */ |
| 648 | } |
| 649 | |
| 650 | JNIEXPORT void* JVM_RegisterSignal(jint signum, void* handler) |
| 651 | { |
| 652 | LOG(INFO) << "SIGNAL: signum=" << signum << ", handler=" << handler; |
| 653 | |
| 654 | struct sigaction act, oldact; |
| 655 | |
| 656 | /* OpenJDK code makes these assumptions */ |
| 657 | assert(SIG_DFL == (void*)0); |
| 658 | assert(SIG_IGN == (void*)1); |
| 659 | |
| 660 | if (handler == (void*) 2) { |
| 661 | /* magic value indicating we should use our internal handler */ |
| 662 | handler = (void*) internalSignalHandler; |
| 663 | } |
| 664 | |
| 665 | act.sa_handler = (void (*)(int))handler; |
| 666 | sigemptyset(&act.sa_mask); |
| 667 | act.sa_flags = SA_RESTART; |
| 668 | sigaction(signum, &act, &oldact); |
| 669 | |
| 670 | if (oldact.sa_handler == internalSignalHandler) { |
| 671 | return (void*) 2; |
| 672 | } else { |
| 673 | return (void*) oldact.sa_handler; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | JNIEXPORT jboolean JVM_RaiseSignal(jint signum) |
| 678 | { |
| 679 | raise(signum); |
| 680 | return JNI_TRUE; |
| 681 | } |
| 682 | |
| 683 | JNIEXPORT void JVM_Halt(jint code) { |
| 684 | exit(code); |
| 685 | } |