1 /*
   2  * Copyright (c) 2003, 2011, 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 
  32 ZeroStack::ZeroStack()
  33     : _base(NULL), _top(NULL), _sp(NULL) {
  34     _shadow_pages_size = JavaThread::stack_shadow_zone_size();
  35   }
  36 
  37 int ZeroStack::suggest_size(Thread *thread) const {
  38   assert(needs_setup(), "already set up");
  39   int abi_available = abi_stack_available(thread);
  40   assert(abi_available >= 0, "available abi stack must be >= 0");
  41   return align_size_down(abi_available / 2, wordSize);
  42 }
  43 
  44 void ZeroStack::handle_overflow(TRAPS) {
  45   JavaThread *thread = (JavaThread *) THREAD;
  46 
  47   // Set up the frame anchor if it isn't already
  48   bool has_last_Java_frame = thread->has_last_Java_frame();
  49   if (!has_last_Java_frame) {
  50     intptr_t *sp = thread->zero_stack()->sp();
  51     ZeroFrame *frame = thread->top_zero_frame();
  52     while (frame) {
  53       if (frame->is_shark_frame())
  54         break;
  55 
  56       if (frame->is_interpreter_frame()) {
  57         interpreterState istate =
  58           frame->as_interpreter_frame()->interpreter_state();
  59         if (istate->self_link() == istate)
  60           break;
  61       }
  62 
  63       sp = ((intptr_t *) frame) + 1;
  64       frame = frame->next();
  65     }
  66 
  67     if (frame == NULL)
  68       fatal("unrecoverable stack overflow");
  69 
  70     thread->set_last_Java_frame(frame, sp);
  71   }
  72 
  73   // Throw the exception
  74   switch (thread->thread_state()) {
  75   case _thread_in_Java:
  76     InterpreterRuntime::throw_StackOverflowError(thread);
  77     break;
  78 
  79   case _thread_in_vm:
  80     Exceptions::throw_stack_overflow_exception(thread, __FILE__, __LINE__,
  81                                                methodHandle());
  82     break;
  83 
  84   default:
  85     ShouldNotReachHere();
  86   }
  87 
  88   // Reset the frame anchor if necessary
  89   if (!has_last_Java_frame)
  90     thread->reset_last_Java_frame();
  91 }
  92 
  93 #ifndef PRODUCT
  94 void ZeroStack::zap(int c) {
  95   memset(_base, c, available_words() * wordSize);
  96 }
  97 #endif // PRODUCT