Thu, 15 Apr 2010 02:40:12 -0700
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 <gbenson@redhat.com>
1 /*
2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright 2007, 2008, 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 */
26 #include "incls/_precompiled.incl"
27 #include "incls/_stubGenerator_zero.cpp.incl"
29 // Declaration and definition of StubGenerator (no .hpp file).
30 // For a more detailed description of the stub routine structure
31 // see the comment in stubRoutines.hpp
33 class StubGenerator: public StubCodeGenerator {
34 private:
35 // The call stub is used to call Java from C
36 static void call_stub(
37 JavaCallWrapper *call_wrapper,
38 intptr_t* result,
39 BasicType result_type,
40 methodOop method,
41 address entry_point,
42 intptr_t* parameters,
43 int parameter_words,
44 TRAPS) {
45 JavaThread *thread = (JavaThread *) THREAD;
46 ZeroStack *stack = thread->zero_stack();
48 // Make sure we have no pending exceptions
49 assert(!HAS_PENDING_EXCEPTION, "call_stub called with pending exception");
51 // Set up the stack if necessary
52 bool stack_needs_teardown = false;
53 if (stack->needs_setup()) {
54 size_t stack_used = thread->stack_base() - (address) &stack_used;
55 size_t stack_free = thread->stack_size() - stack_used;
56 size_t zero_stack_size = align_size_down(stack_free / 2, wordSize);
58 stack->setup(alloca(zero_stack_size), zero_stack_size);
59 stack_needs_teardown = true;
60 }
62 // Allocate and initialize our frame
63 EntryFrame *frame =
64 EntryFrame::build(parameters, parameter_words, call_wrapper, THREAD);
66 if (!HAS_PENDING_EXCEPTION) {
67 // Push the frame
68 thread->push_zero_frame(frame);
70 // Make the call
71 Interpreter::invoke_method(method, entry_point, THREAD);
73 // Store the result
74 if (!HAS_PENDING_EXCEPTION) {
75 switch (result_type) {
76 case T_INT:
77 *(jint *) result = *(jint *) stack->sp();
78 break;
79 case T_LONG:
80 *(jlong *) result = *(jlong *) stack->sp();
81 break;
82 case T_FLOAT:
83 *(jfloat *) result = *(jfloat *) stack->sp();
84 break;
85 case T_DOUBLE:
86 *(jdouble *) result = *(jdouble *) stack->sp();
87 break;
88 case T_OBJECT:
89 *(oop *) result = *(oop *) stack->sp();
90 break;
91 default:
92 ShouldNotReachHere();
93 }
94 }
96 // Unwind the frame
97 thread->pop_zero_frame();
98 }
100 // Tear down the stack if necessary
101 if (stack_needs_teardown)
102 stack->teardown();
103 }
105 // These stubs get called from some dumb test routine.
106 // I'll write them properly when they're called from
107 // something that's actually doing something.
108 static void fake_arraycopy_stub(address src, address dst, int count) {
109 assert(count == 0, "huh?");
110 }
112 void generate_arraycopy_stubs() {
113 // Call the conjoint generation methods immediately after
114 // the disjoint ones so that short branches from the former
115 // to the latter can be generated.
116 StubRoutines::_jbyte_disjoint_arraycopy = (address) fake_arraycopy_stub;
117 StubRoutines::_jbyte_arraycopy = (address) fake_arraycopy_stub;
119 StubRoutines::_jshort_disjoint_arraycopy = (address) fake_arraycopy_stub;
120 StubRoutines::_jshort_arraycopy = (address) fake_arraycopy_stub;
122 StubRoutines::_jint_disjoint_arraycopy = (address) fake_arraycopy_stub;
123 StubRoutines::_jint_arraycopy = (address) fake_arraycopy_stub;
125 StubRoutines::_jlong_disjoint_arraycopy = (address) fake_arraycopy_stub;
126 StubRoutines::_jlong_arraycopy = (address) fake_arraycopy_stub;
128 StubRoutines::_oop_disjoint_arraycopy = ShouldNotCallThisStub();
129 StubRoutines::_oop_arraycopy = ShouldNotCallThisStub();
131 StubRoutines::_checkcast_arraycopy = ShouldNotCallThisStub();
132 StubRoutines::_unsafe_arraycopy = ShouldNotCallThisStub();
133 StubRoutines::_generic_arraycopy = ShouldNotCallThisStub();
135 // We don't generate specialized code for HeapWord-aligned source
136 // arrays, so just use the code we've already generated
137 StubRoutines::_arrayof_jbyte_disjoint_arraycopy =
138 StubRoutines::_jbyte_disjoint_arraycopy;
139 StubRoutines::_arrayof_jbyte_arraycopy =
140 StubRoutines::_jbyte_arraycopy;
142 StubRoutines::_arrayof_jshort_disjoint_arraycopy =
143 StubRoutines::_jshort_disjoint_arraycopy;
144 StubRoutines::_arrayof_jshort_arraycopy =
145 StubRoutines::_jshort_arraycopy;
147 StubRoutines::_arrayof_jint_disjoint_arraycopy =
148 StubRoutines::_jint_disjoint_arraycopy;
149 StubRoutines::_arrayof_jint_arraycopy =
150 StubRoutines::_jint_arraycopy;
152 StubRoutines::_arrayof_jlong_disjoint_arraycopy =
153 StubRoutines::_jlong_disjoint_arraycopy;
154 StubRoutines::_arrayof_jlong_arraycopy =
155 StubRoutines::_jlong_arraycopy;
157 StubRoutines::_arrayof_oop_disjoint_arraycopy =
158 StubRoutines::_oop_disjoint_arraycopy;
159 StubRoutines::_arrayof_oop_arraycopy =
160 StubRoutines::_oop_arraycopy;
161 }
163 void generate_initial() {
164 // Generates all stubs and initializes the entry points
166 // entry points that exist in all platforms Note: This is code
167 // that could be shared among different platforms - however the
168 // benefit seems to be smaller than the disadvantage of having a
169 // much more complicated generator structure. See also comment in
170 // stubRoutines.hpp.
172 StubRoutines::_forward_exception_entry = ShouldNotCallThisStub();
173 StubRoutines::_call_stub_entry = (address) call_stub;
174 StubRoutines::_catch_exception_entry = ShouldNotCallThisStub();
176 // atomic calls
177 StubRoutines::_atomic_xchg_entry = ShouldNotCallThisStub();
178 StubRoutines::_atomic_xchg_ptr_entry = ShouldNotCallThisStub();
179 StubRoutines::_atomic_cmpxchg_entry = ShouldNotCallThisStub();
180 StubRoutines::_atomic_cmpxchg_ptr_entry = ShouldNotCallThisStub();
181 StubRoutines::_atomic_cmpxchg_long_entry = ShouldNotCallThisStub();
182 StubRoutines::_atomic_add_entry = ShouldNotCallThisStub();
183 StubRoutines::_atomic_add_ptr_entry = ShouldNotCallThisStub();
184 StubRoutines::_fence_entry = ShouldNotCallThisStub();
186 // amd64 does this here, sparc does it in generate_all()
187 StubRoutines::_handler_for_unsafe_access_entry =
188 ShouldNotCallThisStub();
189 }
191 void generate_all() {
192 // Generates all stubs and initializes the entry points
194 // These entry points require SharedInfo::stack0 to be set up in
195 // non-core builds and need to be relocatable, so they each
196 // fabricate a RuntimeStub internally.
197 StubRoutines::_throw_AbstractMethodError_entry =
198 ShouldNotCallThisStub();
200 StubRoutines::_throw_ArithmeticException_entry =
201 ShouldNotCallThisStub();
203 StubRoutines::_throw_NullPointerException_entry =
204 ShouldNotCallThisStub();
206 StubRoutines::_throw_NullPointerException_at_call_entry =
207 ShouldNotCallThisStub();
209 StubRoutines::_throw_StackOverflowError_entry =
210 ShouldNotCallThisStub();
212 // support for verify_oop (must happen after universe_init)
213 StubRoutines::_verify_oop_subroutine_entry =
214 ShouldNotCallThisStub();
216 // arraycopy stubs used by compilers
217 generate_arraycopy_stubs();
218 }
220 public:
221 StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
222 if (all) {
223 generate_all();
224 } else {
225 generate_initial();
226 }
227 }
228 };
230 void StubGenerator_generate(CodeBuffer* code, bool all) {
231 StubGenerator g(code, all);
232 }
234 EntryFrame *EntryFrame::build(const intptr_t* parameters,
235 int parameter_words,
236 JavaCallWrapper* call_wrapper,
237 TRAPS) {
239 ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
240 stack->overflow_check(header_words + parameter_words, CHECK_NULL);
242 stack->push(0); // next_frame, filled in later
243 intptr_t *fp = stack->sp();
244 assert(fp - stack->sp() == next_frame_off, "should be");
246 stack->push(ENTRY_FRAME);
247 assert(fp - stack->sp() == frame_type_off, "should be");
249 stack->push((intptr_t) call_wrapper);
250 assert(fp - stack->sp() == call_wrapper_off, "should be");
252 for (int i = 0; i < parameter_words; i++)
253 stack->push(parameters[i]);
255 return (EntryFrame *) fp;
256 }