From 189017f750d54e7b53d0dd3a035e8c4e1cd5cab9 Mon Sep 17 00:00:00 2001 From: Chen Liang Date: Fri, 11 Jul 2025 22:52:26 +0000 Subject: [PATCH] 8361908: Mix and match of dead and valid exception handler leads to malformed class file Reviewed-by: asotona --- .../classfile/impl/DirectCodeBuilder.java | 1 - .../jdk/classfile/FilterDeadLabelsTest.java | 22 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/DirectCodeBuilder.java b/src/java.base/share/classes/jdk/internal/classfile/impl/DirectCodeBuilder.java index b01f355adb4..0ed4a3a9418 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/DirectCodeBuilder.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/DirectCodeBuilder.java @@ -206,7 +206,6 @@ public final class DirectCodeBuilder } else { buf.writeU2U2U2(startPc, endPc, handlerPc); buf.writeIndexOrZero(h.catchTypeEntry()); - handlersSize++; } } if (handlersSize < handlers.size()) diff --git a/test/jdk/jdk/classfile/FilterDeadLabelsTest.java b/test/jdk/jdk/classfile/FilterDeadLabelsTest.java index 53c0f4f7a65..c013b8516b6 100644 --- a/test/jdk/jdk/classfile/FilterDeadLabelsTest.java +++ b/test/jdk/jdk/classfile/FilterDeadLabelsTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,15 +23,18 @@ /* * @test + * @bug 8361908 * @summary Testing filtering of dead labels. * @run junit FilterDeadLabelsTest */ import java.lang.classfile.ClassFile; +import java.lang.classfile.instruction.ExceptionCatch; import java.lang.constant.ClassDesc; import java.lang.constant.ConstantDescs; import java.lang.constant.MethodTypeDesc; import java.util.List; +import java.util.Optional; import java.util.function.Consumer; import java.lang.classfile.Attributes; import java.lang.classfile.CodeBuilder; @@ -41,6 +44,8 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import static java.lang.constant.ConstantDescs.*; + class FilterDeadLabelsTest { static List> deadLabelFragments() { @@ -71,6 +76,21 @@ class FilterDeadLabelsTest { code.findAttribute(Attributes.characterRangeTable()).ifPresent(a -> assertTrue(a.characterRangeTable().isEmpty())); } + @Test // JDK-8361908 + void testFilterMixedExceptionCatch() { + var cc = ClassFile.of(ClassFile.DeadLabelsOption.DROP_DEAD_LABELS); + var code = cc.parse(cc.build(CD_Void, clb -> + clb.withMethodBody("m", MTD_void, 0, cob -> { + cob.return_(); + var l = cob.newBoundLabel(); + cob.pop().return_(); + cob.exceptionCatch(cob.startLabel(), l, l, Optional.empty()); + cob.exceptionCatch(cob.newLabel(), l, l, CD_Exception); + }))).methods().get(0).code().get(); + assertEquals(1, code.exceptionHandlers().size(), () -> code.exceptionHandlers().toString()); + assertEquals(Optional.empty(), code.exceptionHandlers().getFirst().catchType()); + } + @ParameterizedTest @MethodSource("deadLabelFragments") void testThrowOnDeadLabels(Consumer fragment) {