# HG changeset patch # User twisti # Date 1271324412 25200 # Node ID 4319cb12ddfebb431d3453dc9093cd4cbac83d30 # Parent bb01e3adecb460e18a54a23065be43fa3cdac882 6941224: Improved stack overflow handling for Zero Summary: Adding stack overflow checking to Shark brought to light a bunch of deficiencies in Zero's stack overflow code. Reviewed-by: twisti Contributed-by: Gary Benson diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/cpu/zero/vm/cppInterpreter_zero.cpp --- a/hotspot/src/cpu/zero/vm/cppInterpreter_zero.cpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/cpu/zero/vm/cppInterpreter_zero.cpp Thu Apr 15 02:40:12 2010 -0700 @@ -39,21 +39,9 @@ void CppInterpreter::normal_entry(methodOop method, intptr_t UNUSED, TRAPS) { JavaThread *thread = (JavaThread *) THREAD; - ZeroStack *stack = thread->zero_stack(); - - // Adjust the caller's stack frame to accomodate any additional - // local variables we have contiguously with our parameters. - int extra_locals = method->max_locals() - method->size_of_parameters(); - if (extra_locals > 0) { - if (extra_locals > stack->available_words()) { - Unimplemented(); - } - for (int i = 0; i < extra_locals; i++) - stack->push(0); - } // Allocate and initialize our frame. - InterpreterFrame *frame = InterpreterFrame::build(stack, method, thread); + InterpreterFrame *frame = InterpreterFrame::build(method, CHECK); thread->push_zero_frame(frame); // Execute those bytecodes! @@ -76,12 +64,6 @@ intptr_t *result = NULL; int result_slots = 0; - // Check we're not about to run out of stack - if (stack_overflow_imminent(thread)) { - CALL_VM_NOCHECK(InterpreterRuntime::throw_StackOverflowError(thread)); - goto unwind_and_return; - } - while (true) { // We can set up the frame anchor with everything we want at // this point as we are thread_in_Java and no safepoints can @@ -123,9 +105,9 @@ int monitor_words = frame::interpreter_frame_monitor_size(); // Allocate the space - if (monitor_words > stack->available_words()) { - Unimplemented(); - } + stack->overflow_check(monitor_words, THREAD); + if (HAS_PENDING_EXCEPTION) + break; stack->alloc(monitor_words * wordSize); // Move the expression stack contents @@ -172,8 +154,6 @@ } } - unwind_and_return: - // Unwind the current frame thread->pop_zero_frame(); @@ -193,17 +173,11 @@ ZeroStack *stack = thread->zero_stack(); // Allocate and initialize our frame - InterpreterFrame *frame = InterpreterFrame::build(stack, method, thread); + InterpreterFrame *frame = InterpreterFrame::build(method, CHECK); thread->push_zero_frame(frame); interpreterState istate = frame->interpreter_state(); intptr_t *locals = istate->locals(); - // Check we're not about to run out of stack - if (stack_overflow_imminent(thread)) { - CALL_VM_NOCHECK(InterpreterRuntime::throw_StackOverflowError(thread)); - goto unwind_and_return; - } - // Update the invocation counter if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) { InvocationCounter *counter = method->invocation_counter(); @@ -264,9 +238,10 @@ assert(function != NULL, "should be set if signature handler is"); // Build the argument list - if (handler->argument_count() * 2 > stack->available_words()) { - Unimplemented(); - } + stack->overflow_check(handler->argument_count() * 2, THREAD); + if (HAS_PENDING_EXCEPTION) + goto unlock_unwind_and_return; + void **arguments; void *mirror; { arguments = @@ -503,9 +478,7 @@ switch (entry->flag_state()) { case ltos: case dtos: - if (stack->available_words() < 1) { - Unimplemented(); - } + stack->overflow_check(1, CHECK); stack->alloc(wordSize); break; } @@ -601,39 +574,30 @@ stack->set_sp(stack->sp() + method->size_of_parameters()); } -bool CppInterpreter::stack_overflow_imminent(JavaThread *thread) { - // How is the ABI stack? - address stack_top = thread->stack_base() - thread->stack_size(); - int free_stack = os::current_stack_pointer() - stack_top; - if (free_stack < StackShadowPages * os::vm_page_size()) { - return true; - } +InterpreterFrame *InterpreterFrame::build(const methodOop method, TRAPS) { + JavaThread *thread = (JavaThread *) THREAD; + ZeroStack *stack = thread->zero_stack(); + + // Calculate the size of the frame we'll build, including + // any adjustments to the caller's frame that we'll make. + int extra_locals = 0; + int monitor_words = 0; + int stack_words = 0; - // How is the Zero stack? - // Throwing a StackOverflowError involves a VM call, which means - // we need a frame on the stack. We should be checking here to - // ensure that methods we call have enough room to install the - // largest possible frame, but that's more than twice the size - // of the entire Zero stack we get by default, so we just check - // we have *some* space instead... - free_stack = thread->zero_stack()->available_words() * wordSize; - if (free_stack < StackShadowPages * os::vm_page_size()) { - return true; + if (!method->is_native()) { + extra_locals = method->max_locals() - method->size_of_parameters(); + stack_words = method->max_stack(); } + if (method->is_synchronized()) { + monitor_words = frame::interpreter_frame_monitor_size(); + } + stack->overflow_check( + extra_locals + header_words + monitor_words + stack_words, CHECK_NULL); - return false; -} - -InterpreterFrame *InterpreterFrame::build(ZeroStack* stack, - const methodOop method, - JavaThread* thread) { - int monitor_words = - method->is_synchronized() ? frame::interpreter_frame_monitor_size() : 0; - int stack_words = method->is_native() ? 0 : method->max_stack(); - - if (header_words + monitor_words + stack_words > stack->available_words()) { - Unimplemented(); - } + // Adjust the caller's stack frame to accomodate any additional + // local variables we have contiguously with our parameters. + for (int i = 0; i < extra_locals; i++) + stack->push(0); intptr_t *locals; if (method->is_native()) @@ -812,14 +776,13 @@ // Deoptimization helpers -InterpreterFrame *InterpreterFrame::build(ZeroStack* stack, int size) { +InterpreterFrame *InterpreterFrame::build(int size, TRAPS) { + ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack(); + int size_in_words = size >> LogBytesPerWord; assert(size_in_words * wordSize == size, "unaligned"); assert(size_in_words >= header_words, "too small"); - - if (size_in_words > stack->available_words()) { - Unimplemented(); - } + stack->overflow_check(size_in_words, CHECK_NULL); stack->push(0); // next_frame, filled in later intptr_t *fp = stack->sp(); diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/cpu/zero/vm/cppInterpreter_zero.hpp --- a/hotspot/src/cpu/zero/vm/cppInterpreter_zero.hpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/cpu/zero/vm/cppInterpreter_zero.hpp Thu Apr 15 02:40:12 2010 -0700 @@ -39,9 +39,5 @@ static void main_loop(int recurse, TRAPS); private: - // Stack overflow checks - static bool stack_overflow_imminent(JavaThread *thread); - - private: // Fast result type determination static BasicType result_type_of(methodOop method); diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/cpu/zero/vm/entryFrame_zero.hpp --- a/hotspot/src/cpu/zero/vm/entryFrame_zero.hpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/cpu/zero/vm/entryFrame_zero.hpp Thu Apr 15 02:40:12 2010 -0700 @@ -1,6 +1,6 @@ /* * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. - * Copyright 2008 Red Hat, Inc. + * Copyright 2008, 2010 Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,10 +47,10 @@ }; public: - static EntryFrame *build(ZeroStack* stack, - const intptr_t* parameters, + static EntryFrame *build(const intptr_t* parameters, int parameter_words, - JavaCallWrapper* call_wrapper); + JavaCallWrapper* call_wrapper, + TRAPS); public: JavaCallWrapper *call_wrapper() const { return (JavaCallWrapper *) value_of_word(call_wrapper_off); diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/cpu/zero/vm/fakeStubFrame_zero.hpp --- a/hotspot/src/cpu/zero/vm/fakeStubFrame_zero.hpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/cpu/zero/vm/fakeStubFrame_zero.hpp Thu Apr 15 02:40:12 2010 -0700 @@ -1,6 +1,6 @@ /* * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. - * Copyright 2008 Red Hat, Inc. + * Copyright 2008, 2010 Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -42,7 +42,7 @@ }; public: - static FakeStubFrame *build(ZeroStack* stack); + static FakeStubFrame *build(TRAPS); public: void identify_word(int frame_index, diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/cpu/zero/vm/interpreterFrame_zero.hpp --- a/hotspot/src/cpu/zero/vm/interpreterFrame_zero.hpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/cpu/zero/vm/interpreterFrame_zero.hpp Thu Apr 15 02:40:12 2010 -0700 @@ -1,6 +1,6 @@ /* * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. - * Copyright 2008 Red Hat, Inc. + * Copyright 2008, 2010 Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -55,10 +55,8 @@ }; public: - static InterpreterFrame *build(ZeroStack* stack, - const methodOop method, - JavaThread* thread); - static InterpreterFrame *build(ZeroStack* stack, int size); + static InterpreterFrame *build(const methodOop method, TRAPS); + static InterpreterFrame *build(int size, TRAPS); public: interpreterState interpreter_state() const { diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/cpu/zero/vm/interpreterRT_zero.cpp --- a/hotspot/src/cpu/zero/vm/interpreterRT_zero.cpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/cpu/zero/vm/interpreterRT_zero.cpp Thu Apr 15 02:40:12 2010 -0700 @@ -1,6 +1,6 @@ /* * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved. - * Copyright 2007, 2008 Red Hat, Inc. + * Copyright 2007, 2008, 2010 Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -140,9 +140,8 @@ int required_words = (align_size_up(sizeof(ffi_cif), wordSize) >> LogBytesPerWord) + (method->is_static() ? 2 : 1) + method->size_of_parameters() + 1; - if (required_words > stack->available_words()) { - Unimplemented(); - } + + stack->overflow_check(required_words, CHECK_NULL); intptr_t *buf = (intptr_t *) stack->alloc(required_words * wordSize); SlowSignatureHandlerGenerator sshg(methodHandle(thread, method), buf); diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/cpu/zero/vm/stack_zero.hpp --- a/hotspot/src/cpu/zero/vm/stack_zero.hpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/cpu/zero/vm/stack_zero.hpp Thu Apr 15 02:40:12 2010 -0700 @@ -1,6 +1,6 @@ /* * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. - * Copyright 2008, 2009 Red Hat, Inc. + * Copyright 2008, 2009, 2010 Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,9 +29,14 @@ intptr_t *_top; // the word past the end of the stack intptr_t *_sp; // the top word on the stack + private: + int _shadow_pages_size; // how much ABI stack must we keep free? + public: ZeroStack() - : _base(NULL), _top(NULL), _sp(NULL) {} + : _base(NULL), _top(NULL), _sp(NULL) { + _shadow_pages_size = StackShadowPages * os::vm_page_size(); + } bool needs_setup() const { return _base == NULL; @@ -81,6 +86,14 @@ return _sp -= count; } + int shadow_pages_size() const { + return _shadow_pages_size; + } + + public: + void overflow_check(int required_words, TRAPS); + static void handle_overflow(TRAPS); + public: static ByteSize base_offset() { return byte_offset_of(ZeroStack, _base); diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/cpu/zero/vm/stubGenerator_zero.cpp --- a/hotspot/src/cpu/zero/vm/stubGenerator_zero.cpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/cpu/zero/vm/stubGenerator_zero.cpp Thu Apr 15 02:40:12 2010 -0700 @@ -1,6 +1,6 @@ /* * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. - * Copyright 2007, 2008 Red Hat, Inc. + * Copyright 2007, 2008, 2010 Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -60,38 +60,43 @@ } // Allocate and initialize our frame - thread->push_zero_frame( - EntryFrame::build(stack, parameters, parameter_words, call_wrapper)); + EntryFrame *frame = + EntryFrame::build(parameters, parameter_words, call_wrapper, THREAD); - // Make the call - Interpreter::invoke_method(method, entry_point, THREAD); - - // Store result depending on type if (!HAS_PENDING_EXCEPTION) { - switch (result_type) { - case T_INT: - *(jint *) result = *(jint *) stack->sp(); - break; - case T_LONG: - *(jlong *) result = *(jlong *) stack->sp(); - break; - case T_FLOAT: - *(jfloat *) result = *(jfloat *) stack->sp(); - break; - case T_DOUBLE: - *(jdouble *) result = *(jdouble *) stack->sp(); - break; - case T_OBJECT: - *(oop *) result = *(oop *) stack->sp(); - break; - default: - ShouldNotReachHere(); + // Push the frame + thread->push_zero_frame(frame); + + // Make the call + Interpreter::invoke_method(method, entry_point, THREAD); + + // Store the result + if (!HAS_PENDING_EXCEPTION) { + switch (result_type) { + case T_INT: + *(jint *) result = *(jint *) stack->sp(); + break; + case T_LONG: + *(jlong *) result = *(jlong *) stack->sp(); + break; + case T_FLOAT: + *(jfloat *) result = *(jfloat *) stack->sp(); + break; + case T_DOUBLE: + *(jdouble *) result = *(jdouble *) stack->sp(); + break; + case T_OBJECT: + *(oop *) result = *(oop *) stack->sp(); + break; + default: + ShouldNotReachHere(); + } } + + // Unwind the frame + thread->pop_zero_frame(); } - // Unwind our frame - thread->pop_zero_frame(); - // Tear down the stack if necessary if (stack_needs_teardown) stack->teardown(); @@ -226,13 +231,13 @@ StubGenerator g(code, all); } -EntryFrame *EntryFrame::build(ZeroStack* stack, - const intptr_t* parameters, +EntryFrame *EntryFrame::build(const intptr_t* parameters, int parameter_words, - JavaCallWrapper* call_wrapper) { - if (header_words + parameter_words > stack->available_words()) { - Unimplemented(); - } + JavaCallWrapper* call_wrapper, + TRAPS) { + + ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack(); + stack->overflow_check(header_words + parameter_words, CHECK_NULL); stack->push(0); // next_frame, filled in later intptr_t *fp = stack->sp(); diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/os_cpu/linux_zero/vm/thread_linux_zero.hpp --- a/hotspot/src/os_cpu/linux_zero/vm/thread_linux_zero.hpp Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/os_cpu/linux_zero/vm/thread_linux_zero.hpp Thu Apr 15 02:40:12 2010 -0700 @@ -1,6 +1,6 @@ /* * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved. - * Copyright 2007, 2008, 2009 Red Hat, Inc. + * Copyright 2007, 2008, 2009, 2010 Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -68,12 +68,13 @@ public: void set_last_Java_frame() { - JavaFrameAnchor *jfa = frame_anchor(); - jfa->set_last_Java_sp((intptr_t *) top_zero_frame()); + set_last_Java_frame(top_zero_frame()); } void reset_last_Java_frame() { - JavaFrameAnchor *jfa = frame_anchor(); - jfa->set_last_Java_sp(NULL); + set_last_Java_frame(NULL); + } + void set_last_Java_frame(ZeroFrame* frame) { + frame_anchor()->set_last_Java_sp((intptr_t *) frame); } private: diff -r bb01e3adecb4 -r 4319cb12ddfe hotspot/src/share/vm/includeDB_zero --- a/hotspot/src/share/vm/includeDB_zero Wed Apr 14 15:30:13 2010 -0700 +++ b/hotspot/src/share/vm/includeDB_zero Thu Apr 15 02:40:12 2010 -0700 @@ -1,6 +1,6 @@ // // Copyright 2001-2009 Sun Microsystems, Inc. All Rights Reserved. -// Copyright 2009 Red Hat, Inc. +// Copyright 2009, 2010 Red Hat, Inc. // DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. // // This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,8 @@ // NOTE: DO NOT CHANGE THIS COPYRIGHT TO NEW STYLE - IT WILL BREAK makeDeps! +cppInterpreter_.cpp stack_.inline.hpp + entryFrame_.hpp javaCalls.hpp entryFrame_.hpp stack_.hpp @@ -47,9 +49,19 @@ interpreterFrame_.hpp stack_.hpp interpreterFrame_.hpp thread.hpp +interpreterRT_.cpp stack_.inline.hpp + sharkFrame_.hpp methodOop.hpp sharkFrame_.hpp stack_.hpp stack_.hpp sizes.hpp +stack_.inline.hpp stack_.hpp +stack_.inline.hpp thread.hpp + +stack_.cpp interpreterRuntime.hpp +stack_.cpp stack_.hpp + +stubGenerator_.cpp stack_.inline.hpp + thread.hpp stack_.hpp