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_interpreter_frame()) {
  56         interpreterState istate =
  57           frame->as_interpreter_frame()->interpreter_state();
  58         if (istate->self_link() == istate)
  59           break;
  60       }
  61 
  62       sp = ((intptr_t *) frame) + 1;
  63       frame = frame->next();
  64     }
  65 
  66     if (frame == NULL)
  67       fatal("unrecoverable stack overflow");
  68 
  69     thread->set_last_Java_frame(frame, sp);
  70   }
  71 
  72   // Throw the exception
  73   switch (thread->thread_state()) {
  74   case _thread_in_Java:
  75     InterpreterRuntime::throw_StackOverflowError(thread);
  76     break;
  77 
  78   case _thread_in_vm:
  79     Exceptions::throw_stack_overflow_exception(thread, __FILE__, __LINE__,
  80                                                methodHandle());
  81     break;
  82 
  83   default:
  84     ShouldNotReachHere();
  85   }
  86 
  87   // Reset the frame anchor if necessary
  88   if (!has_last_Java_frame)
  89     thread->reset_last_Java_frame();
  90 }
  91 
  92 #ifndef PRODUCT
  93 void ZeroStack::zap(int c) {
  94   memset(_base, c, available_words() * wordSize);
  95 }
  96 #endif // PRODUCT