Skip to content

8361638: java.lang.classfile.CodeBuilder.catchingAll doesn't throw IllegalArgumentException if an existing catch block catches all exceptions #26372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,8 +27,9 @@
import java.lang.classfile.CodeBuilder;
import java.lang.classfile.Label;
import java.lang.classfile.Opcode;
import java.lang.classfile.constantpool.ClassEntry;
import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDesc;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand All @@ -39,14 +40,14 @@ public final class CatchBuilderImpl implements CodeBuilder.CatchBuilder {
final CodeBuilder b;
final BlockCodeBuilderImpl tryBlock;
final Label tryCatchEnd;
final Set<ConstantDesc> catchTypes;
final Set<Integer> catchIndices;
BlockCodeBuilderImpl catchBlock;

public CatchBuilderImpl(CodeBuilder b, BlockCodeBuilderImpl tryBlock, Label tryCatchEnd) {
this.b = b;
this.tryBlock = tryBlock;
this.tryCatchEnd = tryCatchEnd;
this.catchTypes = new HashSet<>();
this.catchIndices = new HashSet<>();
}

@Override
Expand All @@ -59,15 +60,27 @@ public CodeBuilder.CatchBuilder catchingMulti(List<ClassDesc> exceptionTypes, Co
Objects.requireNonNull(exceptionTypes);
Objects.requireNonNull(catchHandler);

if (catchBlock == null) {
if (tryBlock.reachable()) {
b.branch(Opcode.GOTO, tryCatchEnd);
// nullable list of CP entries - null means catching all (0)
List<ClassEntry> entries = new ArrayList<>(Math.max(1, exceptionTypes.size()));
if (exceptionTypes.isEmpty()) {
if (!catchIndices.add(0)) {
throw new IllegalArgumentException("Existing catch block catches exception of all type");
}
entries.add(null);
} else {
for (var exceptionType : exceptionTypes) {
var entry = b.constantPool().classEntry(exceptionType); // throws IAE
if (!catchIndices.add(entry.index())) {
throw new IllegalArgumentException("Existing catch block catches exception of type: " + exceptionType);
}
entries.add(entry);
}
}
// End validation

for (var exceptionType : exceptionTypes) {
if (!catchTypes.add(exceptionType)) {
throw new IllegalArgumentException("Existing catch block catches exception of type: " + exceptionType);
if (catchBlock == null) {
if (tryBlock.reachable()) {
b.branch(Opcode.GOTO, tryCatchEnd);
}
}

Expand All @@ -82,13 +95,9 @@ public CodeBuilder.CatchBuilder catchingMulti(List<ClassDesc> exceptionTypes, Co
catchBlock = new BlockCodeBuilderImpl(b, tryCatchEnd);
Label tryStart = tryBlock.startLabel();
Label tryEnd = tryBlock.endLabel();
if (exceptionTypes.isEmpty()) {
catchBlock.exceptionCatchAll(tryStart, tryEnd, catchBlock.startLabel());
}
else {
for (var exceptionType : exceptionTypes) {
catchBlock.exceptionCatch(tryStart, tryEnd, catchBlock.startLabel(), exceptionType);
}
for (var entry : entries) {
// This accepts null for catching all
catchBlock.exceptionCatch(tryStart, tryEnd, catchBlock.startLabel(), entry);
}
catchBlock.start();
catchHandler.accept(catchBlock);
Expand Down
34 changes: 28 additions & 6 deletions test/jdk/jdk/classfile/BuilderTryCatchTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,6 +23,7 @@

/*
* @test
* @bug 8361638
* @summary Testing ClassFile builder blocks.
* @run junit BuilderTryCatchTest
*/
Expand All @@ -37,6 +38,7 @@

import static java.lang.classfile.ClassFile.ACC_PUBLIC;
import static java.lang.classfile.ClassFile.ACC_STATIC;
import static java.lang.constant.ConstantDescs.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

Expand All @@ -47,20 +49,40 @@
import java.lang.invoke.MethodType;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import static java.lang.constant.ConstantDescs.CD_Double;
import static java.lang.constant.ConstantDescs.CD_Integer;
import static java.lang.constant.ConstantDescs.CD_Object;
import static java.lang.constant.ConstantDescs.CD_String;

class BuilderTryCatchTest {

static final ClassDesc CD_IOOBE = IndexOutOfBoundsException.class.describeConstable().get();
static final ClassDesc CD_NPE = NullPointerException.class.describeConstable().get();
static final MethodTypeDesc MTD_String = MethodType.methodType(String.class).describeConstable().get();

@Test
void testExceptionalContracts() throws Throwable {
generateTryCatchMethod(catchBuilder -> {
Consumer<CodeBuilder.BlockCodeBuilder> handler = tb -> tb.pop().aconst_null().areturn();
assertThrows(NullPointerException.class, () -> catchBuilder.catching(CD_NPE, null));
assertThrows(NullPointerException.class, () -> catchBuilder.catchingMulti(null, handler));
assertThrows(NullPointerException.class, () -> catchBuilder.catchingMulti(List.of(), null));
assertThrows(NullPointerException.class, () -> catchBuilder.catchingMulti(Collections.singletonList(null), null));
assertThrows(NullPointerException.class, () -> catchBuilder.catchingAll(null));
catchBuilder.catchingMulti(List.of(CD_IOOBE, CD_NPE), tb -> {
tb.invokevirtual(CD_Object, "toString", MTD_String);
tb.astore(1);
});
catchBuilder.catchingAll(tb -> tb.pop().loadConstant("all").areturn());
assertThrows(IllegalArgumentException.class, () -> catchBuilder.catching(CD_int, handler));
assertThrows(IllegalArgumentException.class, () -> catchBuilder.catching(CD_NPE, handler));
assertThrows(IllegalArgumentException.class, () -> catchBuilder.catching(null, handler));
assertThrows(IllegalArgumentException.class, () -> catchBuilder.catchingMulti(List.of(), handler));
assertThrows(IllegalArgumentException.class, () -> catchBuilder.catchingMulti(List.of(CD_Exception, CD_IOOBE), handler));
assertThrows(IllegalArgumentException.class, () -> catchBuilder.catchingMulti(List.of(CD_long, CD_Throwable), handler));
assertThrows(IllegalArgumentException.class, () -> catchBuilder.catchingAll(handler));
});
}

@Test
void testTryCatchCatchAll() throws Throwable {
byte[] bytes = generateTryCatchMethod(catchBuilder -> {
Expand Down