GP-176 code review fixes

GP-176 added additional test
GP-176 fixed bugs in INT_CARRY and INT_REM
This commit is contained in:
ghidra1 2020-09-22 10:46:52 -04:00
parent 02e017f507
commit 9d58d72749
4 changed files with 81 additions and 27 deletions

View file

@ -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 {
@Override
public 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;
}
@Override

View file

@ -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 {
@Override
public 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);
}
@Override
public 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;
}

View file

@ -44,7 +44,11 @@ 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
@ -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));

View file

@ -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);
}
}