Thu, 21 Apr 2016 16:19:33 +0300
8130309: Need to bailout cleanly if creation of stubs fails when codecache is out of space
Summary: Check for failed expansion of stub section in code buffer and bailout.
Reviewed-by: kvn, thartmann
1.1 --- a/src/cpu/ppc/vm/compiledIC_ppc.cpp Mon Mar 14 12:35:48 2016 +0300 1.2 +++ b/src/cpu/ppc/vm/compiledIC_ppc.cpp Thu Apr 21 16:19:33 2016 +0300 1.3 @@ -94,7 +94,7 @@ 1.4 1.5 const int IC_pos_in_java_to_interp_stub = 8; 1.6 #define __ _masm. 1.7 -void CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) { 1.8 +address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) { 1.9 #ifdef COMPILER2 1.10 // Get the mark within main instrs section which is set to the address of the call. 1.11 address call_addr = cbuf.insts_mark(); 1.12 @@ -106,8 +106,7 @@ 1.13 // Start the stub. 1.14 address stub = __ start_a_stub(CompiledStaticCall::to_interp_stub_size()); 1.15 if (stub == NULL) { 1.16 - Compile::current()->env()->record_out_of_memory_failure(); 1.17 - return; 1.18 + return NULL; // CodeCache is full 1.19 } 1.20 1.21 // For java_to_interp stubs we use R11_scratch1 as scratch register 1.22 @@ -149,6 +148,7 @@ 1.23 1.24 // End the stub. 1.25 __ end_a_stub(); 1.26 + return stub; 1.27 #else 1.28 ShouldNotReachHere(); 1.29 #endif
2.1 --- a/src/cpu/ppc/vm/ppc.ad Mon Mar 14 12:35:48 2016 +0300 2.2 +++ b/src/cpu/ppc/vm/ppc.ad Thu Apr 21 16:19:33 2016 +0300 2.3 @@ -1171,7 +1171,7 @@ 2.4 // Start the stub. 2.5 address stub = __ start_a_stub(Compile::MAX_stubs_size/2); 2.6 if (stub == NULL) { 2.7 - Compile::current()->env()->record_out_of_memory_failure(); 2.8 + ciEnv::current()->record_failure("CodeCache is full"); 2.9 return; 2.10 } 2.11 2.12 @@ -1249,7 +1249,7 @@ 2.13 2.14 // Emit the trampoline stub which will be related to the branch-and-link below. 2.15 CallStubImpl::emit_trampoline_stub(_masm, entry_point_toc_offset, offsets.insts_call_instruction_offset); 2.16 - if (Compile::current()->env()->failing()) { return offsets; } // Code cache may be full. 2.17 + if (ciEnv::current()->failing()) { return offsets; } // Code cache may be full. 2.18 __ relocate(rtype); 2.19 } 2.20 2.21 @@ -3488,7 +3488,7 @@ 2.22 2.23 // Emit the trampoline stub which will be related to the branch-and-link below. 2.24 CallStubImpl::emit_trampoline_stub(_masm, entry_point_toc_offset, start_offset); 2.25 - if (Compile::current()->env()->failing()) { return; } // Code cache may be full. 2.26 + if (ciEnv::current()->failing()) { return; } // Code cache may be full. 2.27 __ relocate(_optimized_virtual ? 2.28 relocInfo::opt_virtual_call_type : relocInfo::static_call_type); 2.29 } 2.30 @@ -3501,7 +3501,11 @@ 2.31 __ bl(__ pc()); // Emits a relocation. 2.32 2.33 // The stub for call to interpreter. 2.34 - CompiledStaticCall::emit_to_interp_stub(cbuf); 2.35 + address stub = CompiledStaticCall::emit_to_interp_stub(cbuf); 2.36 + if (stub == NULL) { 2.37 + ciEnv::current()->record_failure("CodeCache is full"); 2.38 + return; 2.39 + } 2.40 } 2.41 %} 2.42 2.43 @@ -3546,7 +3550,11 @@ 2.44 2.45 assert(_method, "execute next statement conditionally"); 2.46 // The stub for call to interpreter. 2.47 - CompiledStaticCall::emit_to_interp_stub(cbuf); 2.48 + address stub = CompiledStaticCall::emit_to_interp_stub(cbuf); 2.49 + if (stub == NULL) { 2.50 + ciEnv::current()->record_failure("CodeCache is full"); 2.51 + return; 2.52 + } 2.53 2.54 // Restore original sp. 2.55 __ ld(R11_scratch1, 0, R1_SP); // Load caller sp.
3.1 --- a/src/cpu/sparc/vm/c1_CodeStubs_sparc.cpp Mon Mar 14 12:35:48 2016 +0300 3.2 +++ b/src/cpu/sparc/vm/c1_CodeStubs_sparc.cpp Thu Apr 21 16:19:33 2016 +0300 3.3 @@ -431,6 +431,9 @@ 3.4 __ mov(length()->as_register(), O4); 3.5 3.6 ce->emit_static_call_stub(); 3.7 + if (ce->compilation()->bailed_out()) { 3.8 + return; // CodeCache is full 3.9 + } 3.10 3.11 __ call(SharedRuntime::get_resolve_static_call_stub(), relocInfo::static_call_type); 3.12 __ delayed()->nop();
4.1 --- a/src/cpu/sparc/vm/compiledIC_sparc.cpp Mon Mar 14 12:35:48 2016 +0300 4.2 +++ b/src/cpu/sparc/vm/compiledIC_sparc.cpp Thu Apr 21 16:19:33 2016 +0300 4.3 @@ -53,7 +53,7 @@ 4.4 // ---------------------------------------------------------------------------- 4.5 4.6 #define __ _masm. 4.7 -void CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) { 4.8 +address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) { 4.9 #ifdef COMPILER2 4.10 // Stub is fixed up when the corresponding call is converted from calling 4.11 // compiled code to calling interpreted code. 4.12 @@ -64,9 +64,10 @@ 4.13 4.14 MacroAssembler _masm(&cbuf); 4.15 4.16 - address base = 4.17 - __ start_a_stub(to_interp_stub_size()*2); 4.18 - if (base == NULL) return; // CodeBuffer::expand failed. 4.19 + address base = __ start_a_stub(to_interp_stub_size()); 4.20 + if (base == NULL) { 4.21 + return NULL; // CodeBuffer::expand failed. 4.22 + } 4.23 4.24 // Static stub relocation stores the instruction address of the call. 4.25 __ relocate(static_stub_Relocation::spec(mark)); 4.26 @@ -81,6 +82,7 @@ 4.27 4.28 // Update current stubs pointer and restore code_end. 4.29 __ end_a_stub(); 4.30 + return base; 4.31 #else 4.32 ShouldNotReachHere(); 4.33 #endif
5.1 --- a/src/cpu/sparc/vm/sparc.ad Mon Mar 14 12:35:48 2016 +0300 5.2 +++ b/src/cpu/sparc/vm/sparc.ad Thu Apr 21 16:19:33 2016 +0300 5.3 @@ -1775,9 +1775,11 @@ 5.4 AddressLiteral exception_blob(OptoRuntime::exception_blob()->entry_point()); 5.5 MacroAssembler _masm(&cbuf); 5.6 5.7 - address base = 5.8 - __ start_a_stub(size_exception_handler()); 5.9 - if (base == NULL) return 0; // CodeBuffer::expand failed 5.10 + address base = __ start_a_stub(size_exception_handler()); 5.11 + if (base == NULL) { 5.12 + ciEnv::current()->record_failure("CodeCache is full"); 5.13 + return 0; // CodeBuffer::expand failed 5.14 + } 5.15 5.16 int offset = __ offset(); 5.17 5.18 @@ -1798,9 +1800,11 @@ 5.19 AddressLiteral deopt_blob(SharedRuntime::deopt_blob()->unpack()); 5.20 MacroAssembler _masm(&cbuf); 5.21 5.22 - address base = 5.23 - __ start_a_stub(size_deopt_handler()); 5.24 - if (base == NULL) return 0; // CodeBuffer::expand failed 5.25 + address base = __ start_a_stub(size_deopt_handler()); 5.26 + if (base == NULL) { 5.27 + ciEnv::current()->record_failure("CodeCache is full"); 5.28 + return 0; // CodeBuffer::expand failed 5.29 + } 5.30 5.31 int offset = __ offset(); 5.32 __ save_frame(0); 5.33 @@ -2601,7 +2605,12 @@ 5.34 emit_call_reloc(cbuf, $meth$$method, relocInfo::static_call_type); 5.35 } 5.36 if (_method) { // Emit stub for static call. 5.37 - CompiledStaticCall::emit_to_interp_stub(cbuf); 5.38 + address stub = CompiledStaticCall::emit_to_interp_stub(cbuf); 5.39 + // Stub does not fit into scratch buffer if TraceJumps is enabled 5.40 + if (stub == NULL && !(TraceJumps && Compile::current()->in_scratch_emit_size())) { 5.41 + ciEnv::current()->record_failure("CodeCache is full"); 5.42 + return; 5.43 + } 5.44 } 5.45 %} 5.46
6.1 --- a/src/cpu/x86/vm/c1_CodeStubs_x86.cpp Mon Mar 14 12:35:48 2016 +0300 6.2 +++ b/src/cpu/x86/vm/c1_CodeStubs_x86.cpp Thu Apr 21 16:19:33 2016 +0300 6.3 @@ -502,6 +502,9 @@ 6.4 ce->align_call(lir_static_call); 6.5 6.6 ce->emit_static_call_stub(); 6.7 + if (ce->compilation()->bailed_out()) { 6.8 + return; // CodeCache is full 6.9 + } 6.10 AddressLiteral resolve(SharedRuntime::get_resolve_static_call_stub(), 6.11 relocInfo::static_call_type); 6.12 __ call(resolve);
7.1 --- a/src/cpu/x86/vm/compiledIC_x86.cpp Mon Mar 14 12:35:48 2016 +0300 7.2 +++ b/src/cpu/x86/vm/compiledIC_x86.cpp Thu Apr 21 16:19:33 2016 +0300 7.3 @@ -50,7 +50,7 @@ 7.4 // ---------------------------------------------------------------------------- 7.5 7.6 #define __ _masm. 7.7 -void CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) { 7.8 +address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) { 7.9 // Stub is fixed up when the corresponding call is converted from 7.10 // calling compiled code to calling interpreted code. 7.11 // movq rbx, 0 7.12 @@ -62,9 +62,10 @@ 7.13 // That's why we must use the macroassembler to generate a stub. 7.14 MacroAssembler _masm(&cbuf); 7.15 7.16 - address base = 7.17 - __ start_a_stub(to_interp_stub_size()*2); 7.18 - if (base == NULL) return; // CodeBuffer::expand failed. 7.19 + address base = __ start_a_stub(to_interp_stub_size()); 7.20 + if (base == NULL) { 7.21 + return NULL; // CodeBuffer::expand failed. 7.22 + } 7.23 // Static stub relocation stores the instruction address of the call. 7.24 __ relocate(static_stub_Relocation::spec(mark), Assembler::imm_operand); 7.25 // Static stub relocation also tags the Method* in the code-stream. 7.26 @@ -74,6 +75,7 @@ 7.27 7.28 // Update current stubs pointer and restore insts_end. 7.29 __ end_a_stub(); 7.30 + return base; 7.31 } 7.32 #undef __ 7.33
8.1 --- a/src/cpu/x86/vm/x86.ad Mon Mar 14 12:35:48 2016 +0300 8.2 +++ b/src/cpu/x86/vm/x86.ad Thu Apr 21 16:19:33 2016 +0300 8.3 @@ -550,7 +550,10 @@ 8.4 // That's why we must use the macroassembler to generate a handler. 8.5 MacroAssembler _masm(&cbuf); 8.6 address base = __ start_a_stub(size_exception_handler()); 8.7 - if (base == NULL) return 0; // CodeBuffer::expand failed 8.8 + if (base == NULL) { 8.9 + ciEnv::current()->record_failure("CodeCache is full"); 8.10 + return 0; // CodeBuffer::expand failed 8.11 + } 8.12 int offset = __ offset(); 8.13 __ jump(RuntimeAddress(OptoRuntime::exception_blob()->entry_point())); 8.14 assert(__ offset() - offset <= (int) size_exception_handler(), "overflow"); 8.15 @@ -565,7 +568,10 @@ 8.16 // That's why we must use the macroassembler to generate a handler. 8.17 MacroAssembler _masm(&cbuf); 8.18 address base = __ start_a_stub(size_deopt_handler()); 8.19 - if (base == NULL) return 0; // CodeBuffer::expand failed 8.20 + if (base == NULL) { 8.21 + ciEnv::current()->record_failure("CodeCache is full"); 8.22 + return 0; // CodeBuffer::expand failed 8.23 + } 8.24 int offset = __ offset(); 8.25 8.26 #ifdef _LP64
9.1 --- a/src/cpu/x86/vm/x86_32.ad Mon Mar 14 12:35:48 2016 +0300 9.2 +++ b/src/cpu/x86/vm/x86_32.ad Thu Apr 21 16:19:33 2016 +0300 9.3 @@ -1870,7 +1870,11 @@ 9.4 static_call_Relocation::spec(), RELOC_IMM32 ); 9.5 } 9.6 if (_method) { // Emit stub for static call. 9.7 - CompiledStaticCall::emit_to_interp_stub(cbuf); 9.8 + address stub = CompiledStaticCall::emit_to_interp_stub(cbuf); 9.9 + if (stub == NULL) { 9.10 + ciEnv::current()->record_failure("CodeCache is full"); 9.11 + return; 9.12 + } 9.13 } 9.14 %} 9.15
10.1 --- a/src/cpu/x86/vm/x86_64.ad Mon Mar 14 12:35:48 2016 +0300 10.2 +++ b/src/cpu/x86/vm/x86_64.ad Thu Apr 21 16:19:33 2016 +0300 10.3 @@ -2125,7 +2125,11 @@ 10.4 } 10.5 if (_method) { 10.6 // Emit stub for static call. 10.7 - CompiledStaticCall::emit_to_interp_stub(cbuf); 10.8 + address stub = CompiledStaticCall::emit_to_interp_stub(cbuf); 10.9 + if (stub == NULL) { 10.10 + ciEnv::current()->record_failure("CodeCache is full"); 10.11 + return; 10.12 + } 10.13 } 10.14 %} 10.15
11.1 --- a/src/cpu/zero/vm/compiledIC_zero.cpp Mon Mar 14 12:35:48 2016 +0300 11.2 +++ b/src/cpu/zero/vm/compiledIC_zero.cpp Thu Apr 21 16:19:33 2016 +0300 11.3 @@ -60,8 +60,9 @@ 11.4 11.5 // ---------------------------------------------------------------------------- 11.6 11.7 -void CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) { 11.8 +address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf) { 11.9 ShouldNotReachHere(); // Only needed for COMPILER2. 11.10 + return NULL; 11.11 } 11.12 11.13 int CompiledStaticCall::to_interp_stub_size() {
12.1 --- a/src/share/vm/c1/c1_LIRAssembler.cpp Mon Mar 14 12:35:48 2016 +0300 12.2 +++ b/src/share/vm/c1/c1_LIRAssembler.cpp Thu Apr 21 16:19:33 2016 +0300 12.3 @@ -464,6 +464,7 @@ 12.4 12.5 // emit the static call stub stuff out of line 12.6 emit_static_call_stub(); 12.7 + CHECK_BAILOUT(); 12.8 12.9 switch (op->code()) { 12.10 case lir_static_call:
13.1 --- a/src/share/vm/code/compiledIC.hpp Mon Mar 14 12:35:48 2016 +0300 13.2 +++ b/src/share/vm/code/compiledIC.hpp Thu Apr 21 16:19:33 2016 +0300 13.3 @@ -320,7 +320,7 @@ 13.4 friend CompiledStaticCall* compiledStaticCall_at(Relocation* call_site); 13.5 13.6 // Code 13.7 - static void emit_to_interp_stub(CodeBuffer &cbuf); 13.8 + static address emit_to_interp_stub(CodeBuffer &cbuf); 13.9 static int to_interp_stub_size(); 13.10 static int reloc_to_interp_stub(); 13.11
14.1 --- a/src/share/vm/opto/compile.cpp Mon Mar 14 12:35:48 2016 +0300 14.2 +++ b/src/share/vm/opto/compile.cpp Thu Apr 21 16:19:33 2016 +0300 14.3 @@ -608,6 +608,10 @@ 14.4 n->as_MachBranch()->label_set(&fakeL, 0); 14.5 } 14.6 n->emit(buf, this->regalloc()); 14.7 + 14.8 + // Emitting into the scratch buffer should not fail 14.9 + assert (!failing(), err_msg_res("Must not have pending failure. Reason is: %s", failure_reason())); 14.10 + 14.11 if (is_branch) // Restore label. 14.12 n->as_MachBranch()->label_set(saveL, save_bnum); 14.13
15.1 --- a/src/share/vm/opto/output.cpp Mon Mar 14 12:35:48 2016 +0300 15.2 +++ b/src/share/vm/opto/output.cpp Thu Apr 21 16:19:33 2016 +0300 15.3 @@ -1502,6 +1502,13 @@ 15.4 n->emit(*cb, _regalloc); 15.5 current_offset = cb->insts_size(); 15.6 15.7 + // Above we only verified that there is enough space in the instruction section. 15.8 + // However, the instruction may emit stubs that cause code buffer expansion. 15.9 + // Bail out here if expansion failed due to a lack of code cache space. 15.10 + if (failing()) { 15.11 + return; 15.12 + } 15.13 + 15.14 #ifdef ASSERT 15.15 if (n->size(_regalloc) < (current_offset-instr_offset)) { 15.16 n->dump(); 15.17 @@ -1630,11 +1637,14 @@ 15.18 if (_method) { 15.19 // Emit the exception handler code. 15.20 _code_offsets.set_value(CodeOffsets::Exceptions, HandlerImpl::emit_exception_handler(*cb)); 15.21 + if (failing()) { 15.22 + return; // CodeBuffer::expand failed 15.23 + } 15.24 // Emit the deopt handler code. 15.25 _code_offsets.set_value(CodeOffsets::Deopt, HandlerImpl::emit_deopt_handler(*cb)); 15.26 15.27 // Emit the MethodHandle deopt handler code (if required). 15.28 - if (has_method_handle_invokes()) { 15.29 + if (has_method_handle_invokes() && !failing()) { 15.30 // We can use the same code as for the normal deopt handler, we 15.31 // just need a different entry point address. 15.32 _code_offsets.set_value(CodeOffsets::DeoptMH, HandlerImpl::emit_deopt_handler(*cb));