1 /* Copyright (C) 2014 The Android Open Source Project 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This file implements interfaces from the file jvm.h. This implementation 5 * is licensed under the same terms as the file jvm.h. The 6 * copyright and license information for the file jvm.h follows. 7 * 8 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 10 * 11 * This code is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License version 2 only, as 13 * published by the Free Software Foundation. Oracle designates this 14 * particular file as subject to the "Classpath" exception as provided 15 * by Oracle in the LICENSE file that accompanied this code. 16 * 17 * This code is distributed in the hope that it will be useful, but WITHOUT 18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 20 * version 2 for more details (a copy is included in the LICENSE file that 21 * accompanied this code). 22 * 23 * You should have received a copy of the GNU General Public License version 24 * 2 along with this work; if not, write to the Free Software Foundation, 25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 26 * 27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 28 * or visit www.oracle.com if you need additional information or have any 29 * questions. 30 */ 31 32 /* 33 * Services that OpenJDK expects the VM to provide. 34 */ 35 #include <dlfcn.h> 36 #include <limits.h> 37 #include <stdio.h> 38 #include <sys/ioctl.h> 39 #include <sys/socket.h> 40 #include <sys/time.h> 41 #include <unistd.h> 42 43 #include <android-base/logging.h> 44 45 #include "../../libcore/ojluni/src/main/native/jvm.h" // TODO(narayan): fix it 46 47 #include "base/macros.h" 48 #include "common_throws.h" 49 #include "gc/heap.h" 50 #include "handle_scope-inl.h" 51 #include "java_vm_ext.h" 52 #include "jni_internal.h" 53 #include "mirror/class_loader.h" 54 #include "mirror/string-inl.h" 55 #include "monitor.h" 56 #include "native/scoped_fast_native_object_access-inl.h" 57 #include "nativehelper/scoped_local_ref.h" 58 #include "nativehelper/scoped_utf_chars.h" 59 #include "runtime.h" 60 #include "scoped_thread_state_change-inl.h" 61 #include "thread.h" 62 #include "thread_list.h" 63 #include "verify_object.h" 64 65 #undef LOG_TAG 66 #define LOG_TAG "artopenjdk" 67 68 using ::android::base::WARNING; 69 using ::android::base::INFO; 70 using ::android::base::ERROR; 71 using ::android::base::FATAL; 72 73 /* posix open() with extensions; used by e.g. ZipFile */ 74 JNIEXPORT jint JVM_Open(const char* fname, jint flags, jint mode) { 75 /* 76 * Some code seems to want the special return value JVM_EEXIST if the 77 * file open fails due to O_EXCL. 78 */ 79 // Don't use JVM_O_DELETE, it's problematic with FUSE, see b/28901232. 80 if (flags & JVM_O_DELETE) { 81 LOG(FATAL) << "JVM_O_DELETE option is not supported (while opening: '" 82 << fname << "')"; 83 } 84 85 int fd = TEMP_FAILURE_RETRY(open(fname, flags & ~JVM_O_DELETE, mode)); 86 if (fd < 0) { 87 int err = errno; 88 if (err == EEXIST) { 89 return JVM_EEXIST; 90 } else { 91 return -1; 92 } 93 } 94 95 return fd; 96 } 97 98 /* posix close() */ 99 JNIEXPORT jint JVM_Close(jint fd) { 100 // don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR 101 return close(fd); 102 } 103 104 /* posix read() */ 105 JNIEXPORT jint JVM_Read(jint fd, char* buf, jint nbytes) { 106 return TEMP_FAILURE_RETRY(read(fd, buf, nbytes)); 107 } 108 109 /* posix write(); is used to write messages to stderr */ 110 JNIEXPORT jint JVM_Write(jint fd, char* buf, jint nbytes) { 111 return TEMP_FAILURE_RETRY(write(fd, buf, nbytes)); 112 } 113 114 /* posix lseek() */ 115 JNIEXPORT jlong JVM_Lseek(jint fd, jlong offset, jint whence) { 116 #if !defined(__APPLE__) 117 // NOTE: Using TEMP_FAILURE_RETRY here is busted for LP32 on glibc - the return 118 // value will be coerced into an int32_t. 119 // 120 // lseek64 isn't specified to return EINTR so it shouldn't be necessary 121 // anyway. 122 return lseek64(fd, offset, whence); 123 #else 124 // NOTE: This code is compiled for Mac OS but isn't ever run on that 125 // platform. 126 return lseek(fd, offset, whence); 127 #endif 128 } 129 130 /* 131 * "raw monitors" seem to be expected to behave like non-recursive pthread 132 * mutexes. They're used by ZipFile. 133 */ 134 JNIEXPORT void* JVM_RawMonitorCreate(void) { 135 pthread_mutex_t* mutex = 136 reinterpret_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t))); 137 CHECK(mutex != nullptr); 138 CHECK_PTHREAD_CALL(pthread_mutex_init, (mutex, nullptr), "JVM_RawMonitorCreate"); 139 return mutex; 140 } 141 142 JNIEXPORT void JVM_RawMonitorDestroy(void* mon) { 143 CHECK_PTHREAD_CALL(pthread_mutex_destroy, 144 (reinterpret_cast<pthread_mutex_t*>(mon)), 145 "JVM_RawMonitorDestroy"); 146 free(mon); 147 } 148 149 JNIEXPORT jint JVM_RawMonitorEnter(void* mon) { 150 return pthread_mutex_lock(reinterpret_cast<pthread_mutex_t*>(mon)); 151 } 152 153 JNIEXPORT void JVM_RawMonitorExit(void* mon) { 154 CHECK_PTHREAD_CALL(pthread_mutex_unlock, 155 (reinterpret_cast<pthread_mutex_t*>(mon)), 156 "JVM_RawMonitorExit"); 157 } 158 159 JNIEXPORT char* JVM_NativePath(char* path) { 160 return path; 161 } 162 163 JNIEXPORT jint JVM_GetLastErrorString(char* buf, int len) { 164 #if defined(__GLIBC__) || defined(__BIONIC__) 165 if (len == 0) { 166 return 0; 167 } 168 169 const int err = errno; 170 char* result = strerror_r(err, buf, len); 171 if (result != buf) { 172 strncpy(buf, result, len); 173 buf[len - 1] = '\0'; 174 } 175 176 return strlen(buf); 177 #else 178 UNUSED(buf); 179 UNUSED(len); 180 return -1; 181 #endif 182 } 183 184 JNIEXPORT int jio_fprintf(FILE* fp, const char* fmt, ...) { 185 va_list args; 186 187 va_start(args, fmt); 188 int len = jio_vfprintf(fp, fmt, args); 189 va_end(args); 190 191 return len; 192 } 193 194 JNIEXPORT int jio_vfprintf(FILE* fp, const char* fmt, va_list args) { 195 assert(fp != NULL); 196 return vfprintf(fp, fmt, args); 197 } 198 199 /* posix fsync() */ 200 JNIEXPORT jint JVM_Sync(jint fd) { 201 return TEMP_FAILURE_RETRY(fsync(fd)); 202 } 203 204 JNIEXPORT void* JVM_FindLibraryEntry(void* handle, const char* name) { 205 return dlsym(handle, name); 206 } 207 208 JNIEXPORT jlong JVM_CurrentTimeMillis(JNIEnv* env ATTRIBUTE_UNUSED, 209 jclass clazz ATTRIBUTE_UNUSED) { 210 struct timeval tv; 211 gettimeofday(&tv, (struct timezone *) NULL); 212 jlong when = tv.tv_sec * 1000LL + tv.tv_usec / 1000; 213 return when; 214 } 215 216 JNIEXPORT jint JVM_Socket(jint domain, jint type, jint protocol) { 217 return TEMP_FAILURE_RETRY(socket(domain, type, protocol)); 218 } 219 220 JNIEXPORT jint JVM_InitializeSocketLibrary() { 221 return 0; 222 } 223 224 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) { 225 if ((intptr_t)count <= 0) return -1; 226 return vsnprintf(str, count, fmt, args); 227 } 228 229 int jio_snprintf(char *str, size_t count, const char *fmt, ...) { 230 va_list args; 231 int len; 232 va_start(args, fmt); 233 len = jio_vsnprintf(str, count, fmt, args); 234 va_end(args); 235 return len; 236 } 237 238 JNIEXPORT jint JVM_SetSockOpt(jint fd, int level, int optname, 239 const char* optval, int optlen) { 240 return TEMP_FAILURE_RETRY(setsockopt(fd, level, optname, optval, optlen)); 241 } 242 243 JNIEXPORT jint JVM_SocketShutdown(jint fd, jint howto) { 244 return TEMP_FAILURE_RETRY(shutdown(fd, howto)); 245 } 246 247 JNIEXPORT jint JVM_GetSockOpt(jint fd, int level, int optname, char* optval, 248 int* optlen) { 249 socklen_t len = *optlen; 250 int cc = TEMP_FAILURE_RETRY(getsockopt(fd, level, optname, optval, &len)); 251 *optlen = len; 252 return cc; 253 } 254 255 JNIEXPORT jint JVM_GetSockName(jint fd, struct sockaddr* addr, int* addrlen) { 256 socklen_t len = *addrlen; 257 int cc = TEMP_FAILURE_RETRY(getsockname(fd, addr, &len)); 258 *addrlen = len; 259 return cc; 260 } 261 262 JNIEXPORT jint JVM_SocketAvailable(jint fd, jint* result) { 263 if (TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, result)) < 0) { 264 return JNI_FALSE; 265 } 266 267 return JNI_TRUE; 268 } 269 270 JNIEXPORT jint JVM_Send(jint fd, char* buf, jint nBytes, jint flags) { 271 return TEMP_FAILURE_RETRY(send(fd, buf, nBytes, flags)); 272 } 273 274 JNIEXPORT jint JVM_SocketClose(jint fd) { 275 // Don't want TEMP_FAILURE_RETRY here -- file is closed even if EINTR. 276 return close(fd); 277 } 278 279 JNIEXPORT jint JVM_Listen(jint fd, jint count) { 280 return TEMP_FAILURE_RETRY(listen(fd, count)); 281 } 282 283 JNIEXPORT jint JVM_Connect(jint fd, struct sockaddr* addr, jint addrlen) { 284 return TEMP_FAILURE_RETRY(connect(fd, addr, addrlen)); 285 } 286 287 JNIEXPORT int JVM_GetHostName(char* name, int namelen) { 288 return TEMP_FAILURE_RETRY(gethostname(name, namelen)); 289 } 290 291 JNIEXPORT jstring JVM_InternString(JNIEnv* env, jstring jstr) { 292 art::ScopedFastNativeObjectAccess soa(env); 293 art::ObjPtr<art::mirror::String> s = soa.Decode<art::mirror::String>(jstr); 294 return soa.AddLocalReference<jstring>(s->Intern()); 295 } 296 297 JNIEXPORT jlong JVM_FreeMemory(void) { 298 return art::Runtime::Current()->GetHeap()->GetFreeMemory(); 299 } 300 301 JNIEXPORT jlong JVM_TotalMemory(void) { 302 return art::Runtime::Current()->GetHeap()->GetTotalMemory(); 303 } 304 305 JNIEXPORT jlong JVM_MaxMemory(void) { 306 return art::Runtime::Current()->GetHeap()->GetMaxMemory(); 307 } 308 309 JNIEXPORT void JVM_GC(void) { 310 if (art::Runtime::Current()->IsExplicitGcDisabled()) { 311 LOG(INFO) << "Explicit GC skipped."; 312 return; 313 } 314 art::Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references */ false); 315 } 316 317 JNIEXPORT __attribute__((noreturn)) void JVM_Exit(jint status) { 318 LOG(INFO) << "System.exit called, status: " << status; 319 art::Runtime::Current()->CallExitHook(status); 320 exit(status); 321 } 322 323 JNIEXPORT jstring JVM_NativeLoad(JNIEnv* env, 324 jstring javaFilename, 325 jobject javaLoader) { 326 ScopedUtfChars filename(env, javaFilename); 327 if (filename.c_str() == NULL) { 328 return NULL; 329 } 330 331 std::string error_msg; 332 { 333 art::JavaVMExt* vm = art::Runtime::Current()->GetJavaVM(); 334 bool success = vm->LoadNativeLibrary(env, 335 filename.c_str(), 336 javaLoader, 337 &error_msg); 338 if (success) { 339 return nullptr; 340 } 341 } 342 343 // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF. 344 env->ExceptionClear(); 345 return env->NewStringUTF(error_msg.c_str()); 346 } 347 348 JNIEXPORT void JVM_StartThread(JNIEnv* env, jobject jthread, jlong stack_size, jboolean daemon) { 349 art::Thread::CreateNativeThread(env, jthread, stack_size, daemon == JNI_TRUE); 350 } 351 352 JNIEXPORT void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio) { 353 art::ScopedObjectAccess soa(env); 354 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); 355 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); 356 if (thread != NULL) { 357 thread->SetNativePriority(prio); 358 } 359 } 360 361 JNIEXPORT void JVM_Yield(JNIEnv* env ATTRIBUTE_UNUSED, jclass threadClass ATTRIBUTE_UNUSED) { 362 sched_yield(); 363 } 364 365 JNIEXPORT void JVM_Sleep(JNIEnv* env, jclass threadClass ATTRIBUTE_UNUSED, 366 jobject java_lock, jlong millis) { 367 art::ScopedFastNativeObjectAccess soa(env); 368 art::ObjPtr<art::mirror::Object> lock = soa.Decode<art::mirror::Object>(java_lock); 369 art::Monitor::Wait(art::Thread::Current(), lock.Ptr(), millis, 0, true, art::kSleeping); 370 } 371 372 JNIEXPORT jobject JVM_CurrentThread(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED) { 373 art::ScopedFastNativeObjectAccess soa(env); 374 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer()); 375 } 376 377 JNIEXPORT void JVM_Interrupt(JNIEnv* env, jobject jthread) { 378 art::ScopedFastNativeObjectAccess soa(env); 379 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); 380 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); 381 if (thread != nullptr) { 382 thread->Interrupt(soa.Self()); 383 } 384 } 385 386 JNIEXPORT jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clearInterrupted) { 387 if (clearInterrupted) { 388 return static_cast<art::JNIEnvExt*>(env)->GetSelf()->Interrupted() ? JNI_TRUE : JNI_FALSE; 389 } else { 390 art::ScopedFastNativeObjectAccess soa(env); 391 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); 392 art::Thread* thread = art::Thread::FromManagedThread(soa, jthread); 393 return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE; 394 } 395 } 396 397 JNIEXPORT jboolean JVM_HoldsLock(JNIEnv* env, jclass unused ATTRIBUTE_UNUSED, jobject jobj) { 398 art::ScopedObjectAccess soa(env); 399 art::ObjPtr<art::mirror::Object> object = soa.Decode<art::mirror::Object>(jobj); 400 if (object == nullptr) { 401 art::ThrowNullPointerException("object == null"); 402 return JNI_FALSE; 403 } 404 return soa.Self()->HoldsLock(object.Ptr()); 405 } 406 407 JNIEXPORT void JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring java_name) { 408 ScopedUtfChars name(env, java_name); 409 { 410 art::ScopedObjectAccess soa(env); 411 if (soa.Decode<art::mirror::Object>(jthread) == soa.Self()->GetPeer()) { 412 soa.Self()->SetThreadName(name.c_str()); 413 return; 414 } 415 } 416 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the 417 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock 418 // in the DDMS send code. 419 art::ThreadList* thread_list = art::Runtime::Current()->GetThreadList(); 420 bool timed_out; 421 // Take suspend thread lock to avoid races with threads trying to suspend this one. 422 art::Thread* thread; 423 { 424 thread = thread_list->SuspendThreadByPeer(jthread, 425 true, 426 art::SuspendReason::kInternal, 427 &timed_out); 428 } 429 if (thread != NULL) { 430 { 431 art::ScopedObjectAccess soa(env); 432 thread->SetThreadName(name.c_str()); 433 } 434 bool resumed = thread_list->Resume(thread, art::SuspendReason::kInternal); 435 DCHECK(resumed); 436 } else if (timed_out) { 437 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread " 438 "failed to suspend within a generous timeout."; 439 } 440 } 441 442 JNIEXPORT jint JVM_IHashCode(JNIEnv* env ATTRIBUTE_UNUSED, 443 jobject javaObject ATTRIBUTE_UNUSED) { 444 UNIMPLEMENTED(FATAL) << "JVM_IHashCode is not implemented"; 445 return 0; 446 } 447 448 JNIEXPORT jlong JVM_NanoTime(JNIEnv* env ATTRIBUTE_UNUSED, jclass unused ATTRIBUTE_UNUSED) { 449 UNIMPLEMENTED(FATAL) << "JVM_NanoTime is not implemented"; 450 return 0L; 451 } 452 453 JNIEXPORT void JVM_ArrayCopy(JNIEnv* /* env */, jclass /* unused */, jobject /* javaSrc */, 454 jint /* srcPos */, jobject /* javaDst */, jint /* dstPos */, 455 jint /* length */) { 456 UNIMPLEMENTED(FATAL) << "JVM_ArrayCopy is not implemented"; 457 } 458 459 JNIEXPORT jint JVM_FindSignal(const char* name ATTRIBUTE_UNUSED) { 460 LOG(FATAL) << "JVM_FindSignal is not implemented"; 461 return 0; 462 } 463 464 JNIEXPORT void* JVM_RegisterSignal(jint signum ATTRIBUTE_UNUSED, void* handler ATTRIBUTE_UNUSED) { 465 LOG(FATAL) << "JVM_RegisterSignal is not implemented"; 466 return nullptr; 467 } 468 469 JNIEXPORT jboolean JVM_RaiseSignal(jint signum ATTRIBUTE_UNUSED) { 470 LOG(FATAL) << "JVM_RaiseSignal is not implemented"; 471 return JNI_FALSE; 472 } 473 474 JNIEXPORT __attribute__((noreturn)) void JVM_Halt(jint code) { 475 exit(code); 476 } 477 478 JNIEXPORT jboolean JVM_IsNaN(jdouble d) { 479 return isnan(d); 480 } 481