1 /* 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 3 * Copyright 2010 Red Hat, Inc. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "interpreter/interpreterRuntime.hpp" 28 #include "runtime/thread.hpp" 29 #include "stack_zero.hpp" 30 #include "stack_zero.inline.hpp" 31 #include "utilities/align.hpp" 32 33 // Inlined causes circular inclusion with thread.hpp 34 ZeroStack::ZeroStack() 35 : _base(NULL), _top(NULL), _sp(NULL) { 36 _shadow_pages_size = JavaThread::stack_shadow_zone_size(); 37 } 38 39 int ZeroStack::suggest_size(Thread *thread) const { 40 assert(needs_setup(), "already set up"); 41 int abi_available = abi_stack_available(thread); 42 assert(abi_available >= 0, "available abi stack must be >= 0"); 43 return align_down(abi_available / 2, wordSize); 44 } 45 46 void ZeroStack::handle_overflow(TRAPS) { 47 JavaThread *thread = (JavaThread *) THREAD; 48 49 // Set up the frame anchor if it isn't already 50 bool has_last_Java_frame = thread->has_last_Java_frame(); 51 if (!has_last_Java_frame) { 52 intptr_t *sp = thread->zero_stack()->sp(); 53 ZeroFrame *frame = thread->top_zero_frame(); 54 while (frame) { 55 if (frame->is_shark_frame()) 56 break; 57 58 if (frame->is_interpreter_frame()) { 59 interpreterState istate = 60 frame->as_interpreter_frame()->interpreter_state(); 61 if (istate->self_link() == istate) 62 break; 63 } 64 65 sp = ((intptr_t *) frame) + 1; 66 frame = frame->next(); 67 } 68 69 if (frame == NULL) 70 fatal("unrecoverable stack overflow"); 71 72 thread->set_last_Java_frame(frame, sp); 73 } 74 75 // Throw the exception 76 switch (thread->thread_state()) { 77 case _thread_in_Java: 78 InterpreterRuntime::throw_StackOverflowError(thread); 79 break; 80 81 case _thread_in_vm: 82 Exceptions::throw_stack_overflow_exception(thread, __FILE__, __LINE__, 83 methodHandle()); 84 break; 85 86 default: 87 ShouldNotReachHere(); 88 } 89 90 // Reset the frame anchor if necessary 91 if (!has_last_Java_frame) 92 thread->reset_last_Java_frame(); 93 } 94 95 #ifndef PRODUCT 96 void ZeroStack::zap(int c) { 97 memset(_base, c, available_words() * wordSize); 98 } 99 #endif // PRODUCT