This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push:
new f1bc6193 InstConstraintVisitor check objectref class in getfield and
putfield verification (#547)
f1bc6193 is described below
commit f1bc61934ed3ad8c666e63a5b8dc4b236b39b1ca
Author: Naveed Khan <[email protected]>
AuthorDate: Thu Nov 19 21:18:44 2026 +0530
InstConstraintVisitor check objectref class in getfield and putfield
verification (#547)
InstConstraintVisitor.visitGETFIELD and visitPUTFIELD only check that
the objectref is an ObjectType or null, never that it is an instance of
the class named by the field reference. Structural verification thus
accepts getfield/putfield of Foo.x applied to a String, which HotSpot
rejects with "Incompatible type for getting or setting field".
Apply the same Repository.instanceOf check visitINVOKEVIRTUAL and
visitINVOKESPECIAL already use for their objectref.
---
.../structurals/InstConstraintVisitor.java | 14 +++
.../bcel/verifier/VerifierFieldAccessTest.java | 46 +++++++++
.../verifier/tests/TestFieldAccess01Creator.java | 108 +++++++++++++++++++++
3 files changed, 168 insertions(+)
diff --git
a/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java
b/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java
index 580e16c1..b66eb07b 100644
---
a/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java
+++
b/src/main/java/org/apache/bcel/verifier/structurals/InstConstraintVisitor.java
@@ -1046,6 +1046,18 @@ public class InstConstraintVisitor extends EmptyVisitor {
}
}
+ /**
+ * Assures the 'objectref' of a GETFIELD or PUTFIELD is an instance of the
class named by the field reference.
+ */
+ private void objectrefIsInstanceOfFieldClass(final FieldInstruction o,
final Type objectref) throws ClassNotFoundException {
+ if (objectref instanceof ObjectType) {
+ final String theClass = getObjectType(o).getClassName();
+ if (!Repository.instanceOf(((ObjectType)
objectref).getClassName(), theClass)) {
+ constraintViolated(o, "The 'objectref' item '" + objectref +
"' is not an instance of '" + theClass + "' as expected.");
+ }
+ }
+ }
+
private Field visitFieldInstructionInternals(final FieldInstruction o)
throws ClassNotFoundException {
final String fieldName = o.getFieldName(cpg);
final JavaClass jc =
Repository.lookupClass(getObjectType(o).getClassName());
@@ -1151,6 +1163,7 @@ public class InstConstraintVisitor extends EmptyVisitor {
if (!(objectref instanceof ObjectType || objectref == Type.NULL)) {
constraintViolated(o, "Stack top should be an object reference
that's not an array reference, but is '" + objectref + "'.");
}
+ objectrefIsInstanceOfFieldClass(o, objectref);
final String fieldName = o.getFieldName(cpg);
final JavaClass jc =
Repository.lookupClass(getObjectType(o).getClassName());
@@ -2316,6 +2329,7 @@ public class InstConstraintVisitor extends EmptyVisitor {
if (!(objectref instanceof ObjectType || objectref == Type.NULL)) {
constraintViolated(o, "Stack next-to-top should be an object
reference that's not an array reference, but is '" + objectref + "'.");
}
+ objectrefIsInstanceOfFieldClass(o, objectref);
final Field f = visitFieldInstructionInternals(o);
diff --git
a/src/test/java/org/apache/bcel/verifier/VerifierFieldAccessTest.java
b/src/test/java/org/apache/bcel/verifier/VerifierFieldAccessTest.java
new file mode 100644
index 00000000..840d6db8
--- /dev/null
+++ b/src/test/java/org/apache/bcel/verifier/VerifierFieldAccessTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.bcel.verifier;
+
+import java.io.IOException;
+
+import org.apache.bcel.Const;
+import org.apache.bcel.verifier.tests.TestFieldAccess01Creator;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class VerifierFieldAccessTest extends AbstractVerifierTest {
+
+ @ParameterizedTest
+ @ValueSource(shorts = { Const.GETFIELD, Const.PUTFIELD })
+ void testFieldAccessOnUnrelatedObjectref(final short opcode) throws
IOException, ClassNotFoundException {
+ final TestFieldAccess01Creator creator = new
TestFieldAccess01Creator(opcode, false);
+ creator.create();
+ assertVerifyRejected(creator.getSimpleClassName(), "Verification of "
+ Const.getOpcodeName(opcode) + " on a java.lang.String objectref must fail.");
+ }
+
+ @ParameterizedTest
+ @ValueSource(shorts = { Const.GETFIELD, Const.PUTFIELD })
+ void testFieldAccessOnDeclaringClassObjectref(final short opcode) throws
IOException, ClassNotFoundException {
+ final TestFieldAccess01Creator creator = new
TestFieldAccess01Creator(opcode, true);
+ creator.create();
+ assertVerifyOK(creator.getSimpleClassName(), "Verification of " +
Const.getOpcodeName(opcode) + " on an objectref of the declaring class must
pass.");
+ }
+}
diff --git
a/src/test/java/org/apache/bcel/verifier/tests/TestFieldAccess01Creator.java
b/src/test/java/org/apache/bcel/verifier/tests/TestFieldAccess01Creator.java
new file mode 100644
index 00000000..93681031
--- /dev/null
+++ b/src/test/java/org/apache/bcel/verifier/tests/TestFieldAccess01Creator.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.bcel.verifier.tests;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.bcel.Const;
+import org.apache.bcel.classfile.JavaClass;
+import org.apache.bcel.generic.ClassGen;
+import org.apache.bcel.generic.ConstantPoolGen;
+import org.apache.bcel.generic.FieldGen;
+import org.apache.bcel.generic.InstructionConst;
+import org.apache.bcel.generic.InstructionFactory;
+import org.apache.bcel.generic.InstructionList;
+import org.apache.bcel.generic.MethodGen;
+import org.apache.bcel.generic.ObjectType;
+import org.apache.bcel.generic.Type;
+
+/**
+ * Emits a class with an instance field {@code int x} and a static method
{@code test} that applies {@code getfield} or
+ * {@code putfield} for that field to its single parameter. When the parameter
type is not the class declaring the field,
+ * for example {@code java.lang.String}, the access is illegal and structural
verification must reject it.
+ */
+public class TestFieldAccess01Creator extends TestCreator {
+ private final InstructionFactory factory;
+ private final ConstantPoolGen cp;
+ private final ClassGen cg;
+ private final short opcode;
+ private final ObjectType objectrefType;
+ private final String className;
+ private final String simpleClassName;
+
+ public TestFieldAccess01Creator(final short opcode, final boolean
validObjectref) {
+ this.opcode = opcode;
+ simpleClassName = "TestFieldAccess01" + (opcode == Const.GETFIELD ?
"Get" : "Put") + (validObjectref ? "Valid" : "Invalid");
+ className = TEST_PACKAGE + "." + simpleClassName;
+ objectrefType = validObjectref ? ObjectType.getInstance(className) :
Type.STRING;
+ cg = new ClassGen(className, "java.lang.Object", simpleClassName +
".java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {});
+ cp = cg.getConstantPool();
+ factory = new InstructionFactory(cg, cp);
+ }
+
+ @Override
+ public void create(final OutputStream out) throws IOException {
+ cg.addField(new FieldGen(Const.ACC_PUBLIC, Type.INT, "x",
cp).getField());
+ createMethod_0();
+ createMethod_1();
+ cg.getJavaClass().dump(out);
+ }
+
+ private void createMethod_0() {
+ final InstructionList il = new InstructionList();
+ final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID,
Type.NO_ARGS, new String[] {}, "<init>", className, il, cp);
+ il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
+ il.append(factory.createInvoke("java.lang.Object", "<init>",
Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
+ il.append(InstructionFactory.createReturn(Type.VOID));
+ method.setMaxStack();
+ method.setMaxLocals();
+ cg.addMethod(method.getMethod());
+ il.dispose();
+ }
+
+ private void createMethod_1() {
+ final InstructionList il = new InstructionList();
+ final MethodGen method = new MethodGen(Const.ACC_PUBLIC |
Const.ACC_STATIC, Type.VOID, new Type[] { objectrefType }, new String[] { "o"
}, "test",
+ className, il, cp);
+ il.append(InstructionFactory.createLoad(objectrefType, 0));
+ if (opcode == Const.GETFIELD) {
+ il.append(factory.createFieldAccess(className, "x", Type.INT,
Const.GETFIELD));
+ il.append(InstructionConst.POP);
+ } else {
+ il.append(InstructionConst.ICONST_1);
+ il.append(factory.createFieldAccess(className, "x", Type.INT,
Const.PUTFIELD));
+ }
+ il.append(InstructionFactory.createReturn(Type.VOID));
+ method.setMaxStack();
+ method.setMaxLocals();
+ cg.addMethod(method.getMethod());
+ il.dispose();
+ }
+
+ @Override
+ protected String getClassName() {
+ return simpleClassName + JavaClass.EXTENSION;
+ }
+
+ public String getSimpleClassName() {
+ return simpleClassName;
+ }
+}