Allow {iput,sput}-boolean on a byte value.
javac generates code that can only be understood as storing a byte value
into a boolean field. The verifier now allows this.
diff --git a/tests/076-boolean-put/expected.txt b/tests/076-boolean-put/expected.txt
new file mode 100644
index 0000000..a965a70
--- /dev/null
+++ b/tests/076-boolean-put/expected.txt
@@ -0,0 +1 @@
+Done
diff --git a/tests/076-boolean-put/info.txt b/tests/076-boolean-put/info.txt
new file mode 100644
index 0000000..5b3ef4d
--- /dev/null
+++ b/tests/076-boolean-put/info.txt
@@ -0,0 +1,3 @@
+This checks a case where javac generates code that stores a byte into a
+boolean field. The code as generated should not pass the verifier, so the
+verifier had to be "loosened" to allow this case.
diff --git a/tests/076-boolean-put/src/Main.java b/tests/076-boolean-put/src/Main.java
new file mode 100644
index 0000000..b325422
--- /dev/null
+++ b/tests/076-boolean-put/src/Main.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed 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
+ *
+ * http://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.
+ */
+
+/**
+ * Test access to private boolean fields.
+ *
+ * Accessing private boolean fields from an inner class causes the compiler
+ * to generate an accessor method that performs the boolean operation.
+ * Unfortunately the generated method takes an integer as an argument,
+ * not a boolean, which makes the verifier upset when the result of the
+ * operation is written back to a boolean field.
+ */
+public class Main {
+ private boolean mInstance;
+ private static boolean mStatic;
+
+ public static void main(String[] args) {
+ Main foo = new Main();
+ foo.test();
+
+ System.out.println("Done");
+ }
+
+ void test() {
+ Innard innard = new Innard();
+ innard.doStuff();
+ }
+
+ class Innard {
+ void doStuff() {
+ mInstance |= true;
+ mStatic |= true;
+ }
+ }
+}
diff --git a/vm/analysis/CodeVerify.c b/vm/analysis/CodeVerify.c
index 0ab36ea..5bb546a 100644
--- a/vm/analysis/CodeVerify.c
+++ b/vm/analysis/CodeVerify.c
@@ -1941,6 +1941,13 @@
*
* Assumes we've already validated reg1/reg2.
*
+ * TODO: consider generalizing this. The key principle is that the
+ * result of a bitwise operation can only be as wide as the widest of
+ * the operands. You can safely AND/OR/XOR two chars together and know
+ * you still have a char, so it's reasonable for the compiler or "dx"
+ * to skip the int-to-char instruction. (We need to do this for boolean
+ * because there is no int-to-boolean operation.)
+ *
* Returns true if both args are Boolean, Zero, or One.
*/
static bool upcastBooleanOp(RegType* insnRegs, const int insnRegCount,
@@ -4421,9 +4428,17 @@
ClassObject* fieldClass;
InstField* instField;
- /* make sure the source register has the correct type */
srcType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
&failure);
+
+ /*
+ * javac generates synthetic functions that write byte values
+ * into boolean fields.
+ */
+ if (tmpType == kRegTypeBoolean && srcType == kRegTypeByte)
+ srcType = kRegTypeBoolean;
+
+ /* make sure the source register has the correct type */
if (!canConvertTo1nr(srcType, tmpType)) {
LOG_VFY("VFY: invalid reg type %d on iput instr (need %d)\n",
srcType, tmpType);
@@ -4676,11 +4691,19 @@
RegType srcType, fieldType;
StaticField* staticField;
- /* make sure the source register has the correct type */
srcType = getRegisterType(workRegs, insnRegCount, decInsn.vA,
&failure);
+
+ /*
+ * javac generates synthetic functions that write byte values
+ * into boolean fields.
+ */
+ if (tmpType == kRegTypeBoolean && srcType == kRegTypeByte)
+ srcType = kRegTypeBoolean;
+
+ /* make sure the source register has the correct type */
if (!canConvertTo1nr(srcType, tmpType)) {
- LOG_VFY("VFY: invalid reg type %d on iput instr (need %d)\n",
+ LOG_VFY("VFY: invalid reg type %d on sput instr (need %d)\n",
srcType, tmpType);
failure = VERIFY_ERROR_GENERIC;
break;