Skip to content

Commit 6a9bd77

Browse files
committed
8364660: ClassVerifier::ends_in_athrow() should be removed
Reviewed-by: mdoerr Backport-of: 1bd814c3b24eb7ef5633ee34bb418e0981ca1708
1 parent 2c9d55f commit 6a9bd77

File tree

3 files changed

+1
-258
lines changed

3 files changed

+1
-258
lines changed

src/hotspot/share/classfile/verifier.cpp

Lines changed: 0 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,216 +2438,6 @@ void ClassVerifier::verify_field_instructions(RawBytecodeStream* bcs,
24382438
}
24392439
}
24402440

2441-
// Look at the method's handlers. If the bci is in the handler's try block
2442-
// then check if the handler_pc is already on the stack. If not, push it
2443-
// unless the handler has already been scanned.
2444-
void ClassVerifier::push_handlers(ExceptionTable* exhandlers,
2445-
GrowableArray<u4>* handler_list,
2446-
GrowableArray<u4>* handler_stack,
2447-
u4 bci) {
2448-
int exlength = exhandlers->length();
2449-
for(int x = 0; x < exlength; x++) {
2450-
if (bci >= exhandlers->start_pc(x) && bci < exhandlers->end_pc(x)) {
2451-
u4 exhandler_pc = exhandlers->handler_pc(x);
2452-
if (!handler_list->contains(exhandler_pc)) {
2453-
handler_stack->append_if_missing(exhandler_pc);
2454-
handler_list->append(exhandler_pc);
2455-
}
2456-
}
2457-
}
2458-
}
2459-
2460-
// Return TRUE if all code paths starting with start_bc_offset end in
2461-
// bytecode athrow or loop.
2462-
bool ClassVerifier::ends_in_athrow(u4 start_bc_offset) {
2463-
ResourceMark rm;
2464-
// Create bytecode stream.
2465-
RawBytecodeStream bcs(method());
2466-
u4 code_length = method()->code_size();
2467-
bcs.set_start(start_bc_offset);
2468-
u4 target;
2469-
// Create stack for storing bytecode start offsets for if* and *switch.
2470-
GrowableArray<u4>* bci_stack = new GrowableArray<u4>(30);
2471-
// Create stack for handlers for try blocks containing this handler.
2472-
GrowableArray<u4>* handler_stack = new GrowableArray<u4>(30);
2473-
// Create list of handlers that have been pushed onto the handler_stack
2474-
// so that handlers embedded inside of their own TRY blocks only get
2475-
// scanned once.
2476-
GrowableArray<u4>* handler_list = new GrowableArray<u4>(30);
2477-
// Create list of visited branch opcodes (goto* and if*).
2478-
GrowableArray<u4>* visited_branches = new GrowableArray<u4>(30);
2479-
ExceptionTable exhandlers(_method());
2480-
2481-
while (true) {
2482-
if (bcs.is_last_bytecode()) {
2483-
// if no more starting offsets to parse or if at the end of the
2484-
// method then return false.
2485-
if ((bci_stack->is_empty()) || ((u4)bcs.end_bci() == code_length))
2486-
return false;
2487-
// Pop a bytecode starting offset and scan from there.
2488-
bcs.set_start(bci_stack->pop());
2489-
}
2490-
Bytecodes::Code opcode = bcs.raw_next();
2491-
u4 bci = bcs.bci();
2492-
2493-
// If the bytecode is in a TRY block, push its handlers so they
2494-
// will get parsed.
2495-
push_handlers(&exhandlers, handler_list, handler_stack, bci);
2496-
2497-
switch (opcode) {
2498-
case Bytecodes::_if_icmpeq:
2499-
case Bytecodes::_if_icmpne:
2500-
case Bytecodes::_if_icmplt:
2501-
case Bytecodes::_if_icmpge:
2502-
case Bytecodes::_if_icmpgt:
2503-
case Bytecodes::_if_icmple:
2504-
case Bytecodes::_ifeq:
2505-
case Bytecodes::_ifne:
2506-
case Bytecodes::_iflt:
2507-
case Bytecodes::_ifge:
2508-
case Bytecodes::_ifgt:
2509-
case Bytecodes::_ifle:
2510-
case Bytecodes::_if_acmpeq:
2511-
case Bytecodes::_if_acmpne:
2512-
case Bytecodes::_ifnull:
2513-
case Bytecodes::_ifnonnull:
2514-
target = bcs.dest();
2515-
if (visited_branches->contains(bci)) {
2516-
if (bci_stack->is_empty()) {
2517-
if (handler_stack->is_empty()) {
2518-
return true;
2519-
} else {
2520-
// Parse the catch handlers for try blocks containing athrow.
2521-
bcs.set_start(handler_stack->pop());
2522-
}
2523-
} else {
2524-
// Pop a bytecode starting offset and scan from there.
2525-
bcs.set_start(bci_stack->pop());
2526-
}
2527-
} else {
2528-
if (target > bci) { // forward branch
2529-
if (target >= code_length) return false;
2530-
// Push the branch target onto the stack.
2531-
bci_stack->push(target);
2532-
// then, scan bytecodes starting with next.
2533-
bcs.set_start(bcs.next_bci());
2534-
} else { // backward branch
2535-
// Push bytecode offset following backward branch onto the stack.
2536-
bci_stack->push(bcs.next_bci());
2537-
// Check bytecodes starting with branch target.
2538-
bcs.set_start(target);
2539-
}
2540-
// Record target so we don't branch here again.
2541-
visited_branches->append(bci);
2542-
}
2543-
break;
2544-
2545-
case Bytecodes::_goto:
2546-
case Bytecodes::_goto_w: {
2547-
int offset = (opcode == Bytecodes::_goto ? bcs.get_offset_s2() : bcs.get_offset_s4());
2548-
int min_offset = -1 * max_method_code_size;
2549-
// Check offset for overflow
2550-
if (offset < min_offset || offset > max_method_code_size) return false;
2551-
2552-
target = bci + offset;
2553-
if (visited_branches->contains(bci)) {
2554-
if (bci_stack->is_empty()) {
2555-
if (handler_stack->is_empty()) {
2556-
return true;
2557-
} else {
2558-
// Parse the catch handlers for try blocks containing athrow.
2559-
bcs.set_start(handler_stack->pop());
2560-
}
2561-
} else {
2562-
// Been here before, pop new starting offset from stack.
2563-
bcs.set_start(bci_stack->pop());
2564-
}
2565-
} else {
2566-
if (target >= code_length) return false;
2567-
// Continue scanning from the target onward.
2568-
bcs.set_start(target);
2569-
// Record target so we don't branch here again.
2570-
visited_branches->append(bci);
2571-
}
2572-
break;
2573-
}
2574-
2575-
// Check that all switch alternatives end in 'athrow' bytecodes. Since it
2576-
// is difficult to determine where each switch alternative ends, parse
2577-
// each switch alternative until either hit a 'return', 'athrow', or reach
2578-
// the end of the method's bytecodes. This is gross but should be okay
2579-
// because:
2580-
// 1. tableswitch and lookupswitch byte codes in handlers for ctor explicit
2581-
// constructor invocations should be rare.
2582-
// 2. if each switch alternative ends in an athrow then the parsing should be
2583-
// short. If there is no athrow then it is bogus code, anyway.
2584-
case Bytecodes::_lookupswitch:
2585-
case Bytecodes::_tableswitch:
2586-
{
2587-
address aligned_bcp = align_up(bcs.bcp() + 1, jintSize);
2588-
u4 default_offset = Bytes::get_Java_u4(aligned_bcp) + bci;
2589-
int keys, delta;
2590-
if (opcode == Bytecodes::_tableswitch) {
2591-
jint low = (jint)Bytes::get_Java_u4(aligned_bcp + jintSize);
2592-
jint high = (jint)Bytes::get_Java_u4(aligned_bcp + 2*jintSize);
2593-
// This is invalid, but let the regular bytecode verifier
2594-
// report this because the user will get a better error message.
2595-
if (low > high) return true;
2596-
keys = high - low + 1;
2597-
delta = 1;
2598-
} else {
2599-
keys = (int)Bytes::get_Java_u4(aligned_bcp + jintSize);
2600-
delta = 2;
2601-
}
2602-
// Invalid, let the regular bytecode verifier deal with it.
2603-
if (keys < 0) return true;
2604-
2605-
// Push the offset of the next bytecode onto the stack.
2606-
bci_stack->push(bcs.next_bci());
2607-
2608-
// Push the switch alternatives onto the stack.
2609-
for (int i = 0; i < keys; i++) {
2610-
int min_offset = -1 * max_method_code_size;
2611-
int offset = (jint)Bytes::get_Java_u4(aligned_bcp+(3+i*delta)*jintSize);
2612-
if (offset < min_offset || offset > max_method_code_size) return false;
2613-
u4 target = bci + offset;
2614-
if (target > code_length) return false;
2615-
bci_stack->push(target);
2616-
}
2617-
2618-
// Start bytecode parsing for the switch at the default alternative.
2619-
if (default_offset > code_length) return false;
2620-
bcs.set_start(default_offset);
2621-
break;
2622-
}
2623-
2624-
case Bytecodes::_return:
2625-
return false;
2626-
2627-
case Bytecodes::_athrow:
2628-
{
2629-
if (bci_stack->is_empty()) {
2630-
if (handler_stack->is_empty()) {
2631-
return true;
2632-
} else {
2633-
// Parse the catch handlers for try blocks containing athrow.
2634-
bcs.set_start(handler_stack->pop());
2635-
}
2636-
} else {
2637-
// Pop a bytecode offset and starting scanning from there.
2638-
bcs.set_start(bci_stack->pop());
2639-
}
2640-
}
2641-
break;
2642-
2643-
default:
2644-
;
2645-
} // end switch
2646-
} // end while loop
2647-
2648-
return false;
2649-
}
2650-
26512441
void ClassVerifier::verify_invoke_init(
26522442
RawBytecodeStream* bcs, u2 ref_class_index, VerificationType ref_class_type,
26532443
StackMapFrame* current_frame, u4 code_length, bool in_try_block,
@@ -2672,25 +2462,6 @@ void ClassVerifier::verify_invoke_init(
26722462
// sure that all catch clause paths end in a throw. Otherwise, this can
26732463
// result in returning an incomplete object.
26742464
if (in_try_block) {
2675-
ExceptionTable exhandlers(_method());
2676-
int exlength = exhandlers.length();
2677-
for(int i = 0; i < exlength; i++) {
2678-
u2 start_pc = exhandlers.start_pc(i);
2679-
u2 end_pc = exhandlers.end_pc(i);
2680-
2681-
if (bci >= start_pc && bci < end_pc) {
2682-
if (!ends_in_athrow(exhandlers.handler_pc(i))) {
2683-
verify_error(ErrorContext::bad_code(bci),
2684-
"Bad <init> method call from after the start of a try block");
2685-
return;
2686-
} else if (log_is_enabled(Debug, verification)) {
2687-
ResourceMark rm(THREAD);
2688-
log_debug(verification)("Survived call to ends_in_athrow(): %s",
2689-
current_class()->name()->as_C_string());
2690-
}
2691-
}
2692-
}
2693-
26942465
// Check the exception handler target stackmaps with the locals from the
26952466
// incoming stackmap (before initialize_object() changes them to outgoing
26962467
// state).

src/hotspot/share/classfile/verifier.hpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -335,17 +335,6 @@ class ClassVerifier : public StackObj {
335335
bool* this_uninit, const constantPoolHandle& cp, StackMapTable* stackmap_table,
336336
TRAPS);
337337

338-
// Used by ends_in_athrow() to push all handlers that contain bci onto the
339-
// handler_stack, if the handler has not already been pushed on the stack.
340-
void push_handlers(ExceptionTable* exhandlers,
341-
GrowableArray<u4>* handler_list,
342-
GrowableArray<u4>* handler_stack,
343-
u4 bci);
344-
345-
// Returns true if all paths starting with start_bc_offset end in athrow
346-
// bytecode or loop.
347-
bool ends_in_athrow(u4 start_bc_offset);
348-
349338
void verify_invoke_instructions(
350339
RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
351340
bool in_try_block, bool* this_uninit, VerificationType return_type,

src/java.base/share/classes/jdk/internal/classfile/impl/verifier/VerifierImpl.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,13 +1501,6 @@ void verify_field_instructions(RawBytecodeHelper bcs, VerificationFrame current_
15011501
}
15021502
}
15031503

1504-
// Return TRUE if all code paths starting with start_bc_offset end in
1505-
// bytecode athrow or loop.
1506-
boolean ends_in_athrow(int start_bc_offset) {
1507-
log_info("unimplemented VerifierImpl.ends_in_athrow");
1508-
return true;
1509-
}
1510-
15111504
boolean verify_invoke_init(RawBytecodeHelper bcs, int ref_class_index, VerificationType ref_class_type,
15121505
VerificationFrame current_frame, int code_length, boolean in_try_block,
15131506
boolean this_uninit, ConstantPoolWrapper cp, VerificationTable stackmap_table) {
@@ -1520,16 +1513,6 @@ boolean verify_invoke_init(RawBytecodeHelper bcs, int ref_class_index, Verificat
15201513
verifyError("Bad <init> method call");
15211514
}
15221515
if (in_try_block) {
1523-
for(var exhandler : _method.exceptionTable()) {
1524-
int start_pc = exhandler[0];
1525-
int end_pc = exhandler[1];
1526-
1527-
if (bci >= start_pc && bci < end_pc) {
1528-
if (!ends_in_athrow(exhandler[2])) {
1529-
verifyError("Bad <init> method call from after the start of a try block");
1530-
}
1531-
}
1532-
}
15331516
verify_exception_handler_targets(bci, true, current_frame, stackmap_table);
15341517
}
15351518
current_frame.initialize_object(type, current_type());

0 commit comments

Comments
 (0)