307 void Thread::record_stack_base_and_size() {
308 set_stack_base(os::current_stack_base());
309 set_stack_size(os::current_stack_size());
310 // CR 7190089: on Solaris, primordial thread's stack is adjusted
311 // in initialize_thread(). Without the adjustment, stack size is
312 // incorrect if stack is set to unlimited (ulimit -s unlimited).
313 // So far, only Solaris has real implementation of initialize_thread().
314 //
315 // set up any platform-specific state.
316 os::initialize_thread(this);
317
318 // Set stack limits after thread is initialized.
319 if (is_Java_thread()) {
320 ((JavaThread*) this)->set_stack_overflow_limit();
321 ((JavaThread*) this)->set_reserved_stack_activation(stack_base());
322 }
323 #if INCLUDE_NMT
324 // record thread's native stack, stack grows downward
325 MemTracker::record_thread_stack(stack_end(), stack_size());
326 #endif // INCLUDE_NMT
327 }
328
329
330 Thread::~Thread() {
331 // Reclaim the objectmonitors from the omFreeList of the moribund thread.
332 ObjectSynchronizer::omFlush(this);
333
334 EVENT_THREAD_DESTRUCT(this);
335
336 // stack_base can be NULL if the thread is never started or exited before
337 // record_stack_base_and_size called. Although, we would like to ensure
338 // that all started threads do call record_stack_base_and_size(), there is
339 // not proper way to enforce that.
340 #if INCLUDE_NMT
341 if (_stack_base != NULL) {
342 MemTracker::release_thread_stack(stack_end(), stack_size());
343 #ifdef ASSERT
344 set_stack_base(NULL);
345 #endif
346 }
1785 jio_fprintf(defaultStream::error_stream(),
1786 "\nException: %s thrown from the UncaughtExceptionHandler"
1787 " in thread \"%s\"\n",
1788 pending_exception()->klass()->external_name(),
1789 get_thread_name());
1790 CLEAR_PENDING_EXCEPTION;
1791 }
1792 }
1793
1794 // Called before the java thread exit since we want to read info
1795 // from java_lang_Thread object
1796 EventThreadEnd event;
1797 if (event.should_commit()) {
1798 event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1799 event.commit();
1800 }
1801
1802 // Call after last event on thread
1803 EVENT_THREAD_EXIT(this);
1804
1805 // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1806 // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1807 // is deprecated anyhow.
1808 if (!is_Compiler_thread()) {
1809 int count = 3;
1810 while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1811 EXCEPTION_MARK;
1812 JavaValue result(T_VOID);
1813 KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1814 JavaCalls::call_virtual(&result,
1815 threadObj, thread_klass,
1816 vmSymbols::exit_method_name(),
1817 vmSymbols::void_method_signature(),
1818 THREAD);
1819 CLEAR_PENDING_EXCEPTION;
1820 }
1821 }
1822 // notify JVMTI
1823 if (JvmtiExport::should_post_thread_life()) {
1824 JvmtiExport::post_thread_end(this);
2474 if (is_ext_suspended()) {
2475 clear_ext_suspended();
2476 SR_lock()->notify_all();
2477 }
2478 }
2479
2480 size_t JavaThread::_stack_red_zone_size = 0;
2481 size_t JavaThread::_stack_yellow_zone_size = 0;
2482 size_t JavaThread::_stack_reserved_zone_size = 0;
2483 size_t JavaThread::_stack_shadow_zone_size = 0;
2484
2485 void JavaThread::create_stack_guard_pages() {
2486 if (!os::uses_stack_guard_pages() || _stack_guard_state != stack_guard_unused) { return; }
2487 address low_addr = stack_end();
2488 size_t len = stack_guard_zone_size();
2489
2490 int allocate = os::allocate_stack_guard_pages();
2491 // warning("Guarding at " PTR_FORMAT " for len " SIZE_FORMAT "\n", low_addr, len);
2492
2493 if (allocate && !os::create_stack_guard_pages((char *) low_addr, len)) {
2494 warning("Attempt to allocate stack guard pages failed.");
2495 return;
2496 }
2497
2498 if (os::guard_memory((char *) low_addr, len)) {
2499 _stack_guard_state = stack_guard_enabled;
2500 } else {
2501 warning("Attempt to protect stack guard pages failed.");
2502 if (os::uncommit_memory((char *) low_addr, len)) {
2503 warning("Attempt to deallocate stack guard pages failed.");
2504 }
2505 }
2506 }
2507
2508 void JavaThread::remove_stack_guard_pages() {
2509 assert(Thread::current() == this, "from different thread");
2510 if (_stack_guard_state == stack_guard_unused) return;
2511 address low_addr = stack_end();
2512 size_t len = stack_guard_zone_size();
2513
2514 if (os::allocate_stack_guard_pages()) {
2515 if (os::remove_stack_guard_pages((char *) low_addr, len)) {
2516 _stack_guard_state = stack_guard_unused;
2517 } else {
2518 warning("Attempt to deallocate stack guard pages failed.");
2519 }
2520 } else {
2521 if (_stack_guard_state == stack_guard_unused) return;
2522 if (os::unguard_memory((char *) low_addr, len)) {
2523 _stack_guard_state = stack_guard_unused;
2524 } else {
2525 warning("Attempt to unprotect stack guard pages failed.");
2526 }
2527 }
2528 }
2529
2530 void JavaThread::enable_stack_reserved_zone() {
2531 assert(_stack_guard_state != stack_guard_unused, "must be using guard pages.");
2532 assert(_stack_guard_state != stack_guard_enabled, "already enabled");
2533
2534 // The base notation is from the stack's point of view, growing downward.
2535 // We need to adjust it to work correctly with guard_memory()
2536 address base = stack_reserved_zone_base() - stack_reserved_zone_size();
2537
2538 guarantee(base < stack_base(),"Error calculating stack reserved zone");
2539 guarantee(base < os::current_stack_pointer(),"Error calculating stack reserved zone");
2540
2541 if (os::guard_memory((char *) base, stack_reserved_zone_size())) {
2542 _stack_guard_state = stack_guard_enabled;
2543 } else {
2544 warning("Attempt to guard stack reserved zone failed.");
2545 }
2546 enable_register_stack_guard();
2547 }
|
307 void Thread::record_stack_base_and_size() {
308 set_stack_base(os::current_stack_base());
309 set_stack_size(os::current_stack_size());
310 // CR 7190089: on Solaris, primordial thread's stack is adjusted
311 // in initialize_thread(). Without the adjustment, stack size is
312 // incorrect if stack is set to unlimited (ulimit -s unlimited).
313 // So far, only Solaris has real implementation of initialize_thread().
314 //
315 // set up any platform-specific state.
316 os::initialize_thread(this);
317
318 // Set stack limits after thread is initialized.
319 if (is_Java_thread()) {
320 ((JavaThread*) this)->set_stack_overflow_limit();
321 ((JavaThread*) this)->set_reserved_stack_activation(stack_base());
322 }
323 #if INCLUDE_NMT
324 // record thread's native stack, stack grows downward
325 MemTracker::record_thread_stack(stack_end(), stack_size());
326 #endif // INCLUDE_NMT
327 log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
328 PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
329 os::current_thread_id(), p2i(stack_base() - stack_size()),
330 p2i(stack_base()), stack_size()/1024);
331 }
332
333
334 Thread::~Thread() {
335 // Reclaim the objectmonitors from the omFreeList of the moribund thread.
336 ObjectSynchronizer::omFlush(this);
337
338 EVENT_THREAD_DESTRUCT(this);
339
340 // stack_base can be NULL if the thread is never started or exited before
341 // record_stack_base_and_size called. Although, we would like to ensure
342 // that all started threads do call record_stack_base_and_size(), there is
343 // not proper way to enforce that.
344 #if INCLUDE_NMT
345 if (_stack_base != NULL) {
346 MemTracker::release_thread_stack(stack_end(), stack_size());
347 #ifdef ASSERT
348 set_stack_base(NULL);
349 #endif
350 }
1789 jio_fprintf(defaultStream::error_stream(),
1790 "\nException: %s thrown from the UncaughtExceptionHandler"
1791 " in thread \"%s\"\n",
1792 pending_exception()->klass()->external_name(),
1793 get_thread_name());
1794 CLEAR_PENDING_EXCEPTION;
1795 }
1796 }
1797
1798 // Called before the java thread exit since we want to read info
1799 // from java_lang_Thread object
1800 EventThreadEnd event;
1801 if (event.should_commit()) {
1802 event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1803 event.commit();
1804 }
1805
1806 // Call after last event on thread
1807 EVENT_THREAD_EXIT(this);
1808
1809 log_info(os, thread)("Thread " UINTX_FORMAT " %s.",
1810 os::current_thread_id(),
1811 exit_type == JavaThread::normal_exit ? "exiting" : "detaching");
1812
1813 // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1814 // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1815 // is deprecated anyhow.
1816 if (!is_Compiler_thread()) {
1817 int count = 3;
1818 while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1819 EXCEPTION_MARK;
1820 JavaValue result(T_VOID);
1821 KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1822 JavaCalls::call_virtual(&result,
1823 threadObj, thread_klass,
1824 vmSymbols::exit_method_name(),
1825 vmSymbols::void_method_signature(),
1826 THREAD);
1827 CLEAR_PENDING_EXCEPTION;
1828 }
1829 }
1830 // notify JVMTI
1831 if (JvmtiExport::should_post_thread_life()) {
1832 JvmtiExport::post_thread_end(this);
2482 if (is_ext_suspended()) {
2483 clear_ext_suspended();
2484 SR_lock()->notify_all();
2485 }
2486 }
2487
2488 size_t JavaThread::_stack_red_zone_size = 0;
2489 size_t JavaThread::_stack_yellow_zone_size = 0;
2490 size_t JavaThread::_stack_reserved_zone_size = 0;
2491 size_t JavaThread::_stack_shadow_zone_size = 0;
2492
2493 void JavaThread::create_stack_guard_pages() {
2494 if (!os::uses_stack_guard_pages() || _stack_guard_state != stack_guard_unused) { return; }
2495 address low_addr = stack_end();
2496 size_t len = stack_guard_zone_size();
2497
2498 int allocate = os::allocate_stack_guard_pages();
2499 // warning("Guarding at " PTR_FORMAT " for len " SIZE_FORMAT "\n", low_addr, len);
2500
2501 if (allocate && !os::create_stack_guard_pages((char *) low_addr, len)) {
2502 log_warning(os, thread)("Attempt to allocate stack guard pages failed.");
2503 return;
2504 }
2505
2506 if (os::guard_memory((char *) low_addr, len)) {
2507 _stack_guard_state = stack_guard_enabled;
2508 } else {
2509 log_warning(os, thread)("Attempt to protect stack guard pages failed ("
2510 PTR_FORMAT "-" PTR_FORMAT ").", p2i(low_addr), p2i(low_addr + len));
2511 if (os::uncommit_memory((char *) low_addr, len)) {
2512 log_warning(os, thread)("Attempt to deallocate stack guard pages failed.");
2513 }
2514 return;
2515 }
2516
2517 log_debug(os, thread)("Thread " UINTX_FORMAT " stack guard pages activated: "
2518 PTR_FORMAT "-" PTR_FORMAT ".",
2519 os::current_thread_id(), p2i(low_addr), p2i(low_addr + len));
2520
2521 }
2522
2523 void JavaThread::remove_stack_guard_pages() {
2524 assert(Thread::current() == this, "from different thread");
2525 if (_stack_guard_state == stack_guard_unused) return;
2526 address low_addr = stack_end();
2527 size_t len = stack_guard_zone_size();
2528
2529 if (os::allocate_stack_guard_pages()) {
2530 if (os::remove_stack_guard_pages((char *) low_addr, len)) {
2531 _stack_guard_state = stack_guard_unused;
2532 } else {
2533 log_warning(os, thread)("Attempt to deallocate stack guard pages failed ("
2534 PTR_FORMAT "-" PTR_FORMAT ").", p2i(low_addr), p2i(low_addr + len));
2535 return;
2536 }
2537 } else {
2538 if (_stack_guard_state == stack_guard_unused) return;
2539 if (os::unguard_memory((char *) low_addr, len)) {
2540 _stack_guard_state = stack_guard_unused;
2541 } else {
2542 log_warning(os, thread)("Attempt to unprotect stack guard pages failed ("
2543 PTR_FORMAT "-" PTR_FORMAT ").", p2i(low_addr), p2i(low_addr + len));
2544 return;
2545 }
2546 }
2547
2548 log_debug(os, thread)("Thread " UINTX_FORMAT " stack guard pages removed: "
2549 PTR_FORMAT "-" PTR_FORMAT ".",
2550 os::current_thread_id(), p2i(low_addr), p2i(low_addr + len));
2551
2552 }
2553
2554 void JavaThread::enable_stack_reserved_zone() {
2555 assert(_stack_guard_state != stack_guard_unused, "must be using guard pages.");
2556 assert(_stack_guard_state != stack_guard_enabled, "already enabled");
2557
2558 // The base notation is from the stack's point of view, growing downward.
2559 // We need to adjust it to work correctly with guard_memory()
2560 address base = stack_reserved_zone_base() - stack_reserved_zone_size();
2561
2562 guarantee(base < stack_base(),"Error calculating stack reserved zone");
2563 guarantee(base < os::current_stack_pointer(),"Error calculating stack reserved zone");
2564
2565 if (os::guard_memory((char *) base, stack_reserved_zone_size())) {
2566 _stack_guard_state = stack_guard_enabled;
2567 } else {
2568 warning("Attempt to guard stack reserved zone failed.");
2569 }
2570 enable_register_stack_guard();
2571 }
|