Ghidra is a software reverse engineering (SRE) framework
GP-176 code review fixes GP-176 added additional test GP-176 fixed bugs in INT_CARRY and INT_REM
ghidra19d58d7274983
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/opbehavior/OpBehaviorIntCarry.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/opbehavior/OpBehaviorIntCarry.javaindex 96f5e78ebc..c78d2d474f 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/opbehavior/OpBehaviorIntCarry.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/opbehavior/OpBehaviorIntCarry.java@@ -1,6 +1,5 @@/* ###* IP: GHIDRA- * REVIEWED: YES** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -16,12 +15,12 @@*/package ghidra.pcode.opbehavior;+import java.math.BigInteger;+import ghidra.pcode.utils.Utils;import ghidra.program.model.pcode.PcodeOp;import ghidra.util.exception.AssertException;-import java.math.BigInteger;-public class OpBehaviorIntCarry extends BinaryOpBehavior {public OpBehaviorIntCarry() {@@ -30,16 +29,7 @@ public class OpBehaviorIntCarry extends BinaryOpBehavior {@Overridepublic long evaluateBinary(int sizeout, int sizein, long in1, long in2) {- // must handle possible 64-bit signed case special- if (sizein == 8) {- if (in1 < 0) {- return ((in2 < 0) || (-in2 <= in1)) ? 1 : 0;- }- if (in2 < 0) {- return (-in1 <= in2) ? 1 : 0;- }- }- return (in1 > ((in1 + in2) & Utils.calc_mask(sizein))) ? 1 : 0;+ return (Long.compareUnsigned(in1, (in1 + in2) & Utils.calc_mask(sizein)) > 0) ? 1 : 0;}@Overridediff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/opbehavior/OpBehaviorIntRem.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/opbehavior/OpBehaviorIntRem.javaindex d5c9aaf23e..97535b67ce 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/opbehavior/OpBehaviorIntRem.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/opbehavior/OpBehaviorIntRem.java@@ -1,6 +1,5 @@/* ###* IP: GHIDRA- * REVIEWED: YES** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -16,11 +15,11 @@*/package ghidra.pcode.opbehavior;+import java.math.BigInteger;+import ghidra.pcode.error.LowlevelError;import ghidra.program.model.pcode.PcodeOp;-import java.math.BigInteger;-public class OpBehaviorIntRem extends BinaryOpBehavior {public OpBehaviorIntRem() {@@ -29,17 +28,17 @@ public class OpBehaviorIntRem extends BinaryOpBehavior {@Overridepublic long evaluateBinary(int sizeout, int sizein, long in1, long in2) {- if (in2 == 0)+ if (in2 == 0) {throw new LowlevelError("Remainder by 0");-- long res = in1 % in2;- return res;+ }+ return Long.remainderUnsigned(in1, in2);}@Overridepublic BigInteger evaluateBinary(int sizeout, int sizein, BigInteger in1, BigInteger in2) {- if (in2.signum() == 0)+ if (in2.signum() == 0) {throw new LowlevelError("Remainder by 0");+ }BigInteger res = in1.remainder(in2);return res;}diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/opbehavior/OpBehaviorIntCarryTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/opbehavior/OpBehaviorIntCarryTest.javaindex a45074e52a..069d55e4b0 100644--- a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/opbehavior/OpBehaviorIntCarryTest.java+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/opbehavior/OpBehaviorIntCarryTest.java@@ -26,8 +26,8 @@ public class OpBehaviorIntCarryTest extends AbstractOpBehaviorTest {super();}-@Test- public void testEvaluateBinaryLong() {+ @Test+ public void testEvaluateBinaryLong() {OpBehaviorIntCarry op = new OpBehaviorIntCarry();@@ -44,11 +44,15 @@ public class OpBehaviorIntCarryTest extends AbstractOpBehaviorTest {Assert.assertEquals(1, op.evaluateBinary(8, 8, Long.MAX_VALUE, Long.MIN_VALUE + 1));Assert.assertEquals(0, op.evaluateBinary(8, 8, Long.MIN_VALUE, Long.MAX_VALUE));Assert.assertEquals(1, op.evaluateBinary(8, 8, Long.MIN_VALUE + 1, Long.MAX_VALUE));-+ Assert.assertEquals(0, op.evaluateBinary(8, 8, 0x7fffffffffffffffL, 1L));+ Assert.assertEquals(0, op.evaluateBinary(8, 8, 1L, 0x7fffffffffffffffL));+ Assert.assertEquals(1, op.evaluateBinary(8, 8, 0xffffffffffffffffL, 1L));+ Assert.assertEquals(1, op.evaluateBinary(8, 8, 1L, 0xffffffffffffffffL));+ Assert.assertEquals(1, op.evaluateBinary(8, 8, 0xffffffffffffffffL, 0xffffffffffffffffL));}-@Test- public void testEvaluateBinaryBigInteger() {+ @Test+ public void testEvaluateBinaryBigInteger() {OpBehaviorIntCarry op = new OpBehaviorIntCarry();@@ -56,7 +60,8 @@ public class OpBehaviorIntCarryTest extends AbstractOpBehaviorTest {BigInteger BIG_POSITIVE = new BigInteger("7FFFFFFFFFFFFFFF", 16);BigInteger BIG_NEGATIVE = new BigInteger("8000000000000000", 16);- Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8, BigInteger.ZERO, BigInteger.ZERO));+ Assert.assertEquals(BigInteger.ZERO,+ op.evaluateBinary(8, 8, BigInteger.ZERO, BigInteger.ZERO));Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, BigInteger.ONE, NEGATIVE_ONE));Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, NEGATIVE_ONE, BigInteger.ONE));Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8, BIG_POSITIVE, BIG_NEGATIVE));diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/opbehavior/OpBehaviorIntRemTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/opbehavior/OpBehaviorIntRemTest.javanew file mode 100644index 0000000000..d447e3bf80--- /dev/null+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/opbehavior/OpBehaviorIntRemTest.java@@ -0,0 +1,60 @@+/* ###+ * IP: GHIDRA+ *+ * 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.+ */+package ghidra.pcode.opbehavior;++import java.math.BigInteger;++import org.junit.Assert;+import org.junit.Test;++import ghidra.pcode.error.LowlevelError;++public class OpBehaviorIntRemTest extends AbstractOpBehaviorTest {++ public OpBehaviorIntRemTest() {+ super();+ }++ @Test+ public void testEvaluateBinaryLong() {+ OpBehaviorIntRem op = new OpBehaviorIntRem();+ Assert.assertEquals(0L, op.evaluateBinary(4, 4, 16, 2));+ Assert.assertEquals(1L, op.evaluateBinary(4, 4, 17, 2));+ Assert.assertEquals(1L, op.evaluateBinary(8, 8, 0xffffffffffffffffL, 2L));+ }++ @Test(expected = LowlevelError.class)+ public void testDivideByZeroLong() {+ OpBehaviorIntRem op = new OpBehaviorIntRem();+ op.evaluateBinary(4, 4, 1, 0);+ }++ @Test+ public void testEvaluateBinaryBigInteger() {+ OpBehaviorIntRem op = new OpBehaviorIntRem();+ Assert.assertEquals(BigInteger.TWO,+ op.evaluateBinary(16, 16, new BigInteger("fffffffffffffffffffffffffffffff2", 16),+ BigInteger.TWO.add(BigInteger.TWO)));++ }++ @Test(expected = LowlevelError.class)+ public void testDivideByZeroBigInteger() {+ OpBehaviorIntRem op = new OpBehaviorIntRem();+ op.evaluateBinary(16, 16, BigInteger.ONE, BigInteger.ZERO);+ }++}