Merge remote-tracking branch

'origin/GP-3155_caheckman_PR-2810_Pokechu22_countleadingzeros'
(Closes #2810)
This commit is contained in:
Ryan Kurtz 2023-03-24 14:27:51 -04:00
commit 9cf60faef0
94 changed files with 3604 additions and 3451 deletions

View file

@ -1,57 +0,0 @@
/* ###
* 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.emulate.callother;
import ghidra.pcode.emulate.Emulate;
import ghidra.pcode.memstate.MemoryState;
import ghidra.pcodeCPort.error.LowlevelError;
import ghidra.program.model.pcode.Varnode;
public class CountLeadingOnesOpBehavior implements OpBehaviorOther {
@Override
public void evaluate(Emulate emu, Varnode out, Varnode[] inputs) {
if (out == null) {
throw new LowlevelError("CALLOTHER: Count Leading Ones op missing required output");
}
if (inputs.length != 2 || inputs[1].getSize() == 0 || inputs[1].isConstant()) {
throw new LowlevelError(
"CALLOTHER: Count Leading Ones op requires one non-constant varnode input");
}
// TODO: add support for larger varnode sizes
Varnode in = inputs[1];
if (in.getSize() > 8 || out.getSize() > 8) {
throw new LowlevelError(
"CALLOTHER: Count Leading Ones op only supports varnodes of size 8-bytes or less");
}
MemoryState memoryState = emu.getMemoryState();
long value = memoryState.getValue(in);
long mask = 1L << ((in.getSize() * 8) - 1);
long count = 0;
while ( (mask & value) != 0 ) {
++count;
value = value << 1;
}
memoryState.setValue(out, count);
}
}

View file

@ -1,60 +0,0 @@
/* ###
* 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.emulate.callother;
import ghidra.pcode.emulate.Emulate;
import ghidra.pcode.memstate.MemoryState;
import ghidra.pcodeCPort.error.LowlevelError;
import ghidra.program.model.pcode.Varnode;
public class CountLeadingZerosOpBehavior implements OpBehaviorOther {
@Override
public void evaluate(Emulate emu, Varnode out, Varnode[] inputs) {
if (out == null) {
throw new LowlevelError("CALLOTHER: Count Leading Zeros op missing required output");
}
if (inputs.length != 2 || inputs[1].getSize() == 0 || inputs[1].isConstant()) {
throw new LowlevelError(
"CALLOTHER: Count Leading Zeros op requires one non-constant varnode input");
}
// TODO: add support for larger varnode sizes
Varnode in = inputs[1];
if (in.getSize() > 8 || out.getSize() > 8) {
throw new LowlevelError(
"CALLOTHER: Count Leading Zeros op only supports varnodes of size 8-bytes or less");
}
MemoryState memoryState = emu.getMemoryState();
long value = memoryState.getValue(in);
long mask = 1L << ((in.getSize() * 8) - 1);
long count = 0;
while (mask != 0) {
if ((mask & value) != 0) {
break;
}
++count;
mask >>>= 1;
}
memoryState.setValue(out, count);
}
}

View file

@ -104,6 +104,7 @@ public class OpBehaviorFactory {
opBehaviorMap.put(PcodeOp.INSERT, new SpecialOpBehavior(PcodeOp.INSERT));
opBehaviorMap.put(PcodeOp.EXTRACT, new SpecialOpBehavior(PcodeOp.EXTRACT));
opBehaviorMap.put(PcodeOp.POPCOUNT, new OpBehaviorPopcount());
opBehaviorMap.put(PcodeOp.LZCOUNT, new OpBehaviorLzcount());
}
private OpBehaviorFactory() {

View file

@ -0,0 +1,62 @@
/* ###
* 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 ghidra.program.model.pcode.PcodeOp;
public class OpBehaviorLzcount extends UnaryOpBehavior {
public OpBehaviorLzcount() {
super(PcodeOp.LZCOUNT);
}
@Override
public long evaluateUnary(int sizeout, int sizein, long val) {
long mask = 1L << ((sizein * 8) - 1);
long count = 0;
while (mask != 0) {
if ((mask & val) != 0) {
break;
}
++count;
mask >>>= 1;
}
return count;
}
@Override
public BigInteger evaluateUnary(int sizeout, int sizein, BigInteger unsignedIn1) {
int bitcount = 0;
sizein = sizein * 8 - 1;
while (sizein >= 0) {
if (unsignedIn1.testBit(sizein)) {
break;
}
bitcount += 1;
sizein -= 1;
}
if (sizeout == 1) {
bitcount &= 0xff;
}
else if (sizeout == 2) {
bitcount &= 0xffff;
}
return BigInteger.valueOf(bitcount);
}
}

View file

@ -39,8 +39,26 @@ public class OpBehaviorPopcount extends UnaryOpBehavior {
@Override
public BigInteger evaluateUnary(int sizeout, int sizein, BigInteger unsignedIn1) {
// TODO Auto-generated method stub
return null;
int bitcount = 0;
while (sizein >= 8) {
bitcount += evaluateUnary(1, 8, unsignedIn1.longValue());
sizein -= 8;
if (sizein == 0) {
break;
}
unsignedIn1 = unsignedIn1.shiftRight(64);
}
if (sizein > 0) {
long mask = sizein * 8 - 1;
bitcount += evaluateUnary(1, 8, unsignedIn1.longValue() & mask);
}
if (sizeout == 1) {
bitcount &= 0xff;
}
else if (sizeout == 2) {
bitcount &= 0xffff;
}
return BigInteger.valueOf(bitcount);
}
}

View file

@ -113,6 +113,7 @@ public enum OpCode {
CPUI_INSERT,
CPUI_EXTRACT,
CPUI_POPCOUNT,
CPUI_LZCOUNT,
CPUI_MAX;
@ -203,7 +204,8 @@ public enum OpCode {
"UNUSED1", "FLOAT_NAN", "FLOAT_ADD", "FLOAT_DIV", "FLOAT_MULT", "FLOAT_SUB",
"FLOAT_NEG", "FLOAT_ABS", "FLOAT_SQRT", "INT2FLOAT", "FLOAT2FLOAT", "TRUNC", "CEIL",
"FLOOR", "ROUND", "BUILD", "DELAY_SLOT", "PIECE", "SUBPIECE", "CAST", "LABEL",
"CROSSBUILD", "SEGMENTOP", "CPOOLREF", "NEW", "INSERT", "EXTRACT", "POPCOUNT" };
"CROSSBUILD", "SEGMENTOP", "CPOOLREF", "NEW", "INSERT", "EXTRACT", "POPCOUNT",
"LZCOUNT" };
public static String get_opname(OpCode op) {
return opcode_name[op.ordinal()];
@ -212,7 +214,7 @@ public enum OpCode {
static final int opcode_indices[] = { 0, 39, 37, 40, 38, 4, 6, 60, 7, 8, 9, 64, 5, 57, 1, 68, 66,
61, 71, 55, 52, 47, 48, 41, 43, 44, 49, 46, 51, 42, 53, 50, 58, 70, 54, 24, 19, 27, 21,
33, 11, 29, 15, 16, 32, 25, 12, 28, 35, 30, 23, 22, 34, 18, 13, 14, 36, 31, 20, 26, 17,
65, 2, 69, 62, 72, 10, 59, 67, 3, 63, 56, 45 };
65, 2, 73, 69, 62, 72, 10, 59, 67, 3, 63, 56, 45 };
public static OpCode get_opcode(String nm) { // Use binary search to find name
int min = 1; // Don't include BLANK

View file

@ -996,6 +996,9 @@ public abstract class PcodeCompile {
if ("popcount".equals(name) && hasOperands(1, operands, location, name)) {
return createOp(location, OpCode.CPUI_POPCOUNT, r);
}
if ("lzcount".equals(name) && hasOperands(1, operands, location, name)) {
return createOp(location, OpCode.CPUI_LZCOUNT, r);
}
return null;
}
@ -1073,6 +1076,9 @@ public abstract class PcodeCompile {
if ("popcount".equals(name)) {
return true;
}
if ("lzcount".equals(name)) {
return true;
}
return false;
}

View file

@ -75,7 +75,7 @@ public class DynamicHash {
0, // CAST is skipped
PcodeOp.INT_ADD, PcodeOp.INT_ADD, // PTRADD and PTRSUB hash same as INT_ADD
PcodeOp.SEGMENTOP, PcodeOp.CPOOLREF, PcodeOp.NEW, PcodeOp.INSERT, PcodeOp.EXTRACT,
PcodeOp.POPCOUNT };
PcodeOp.POPCOUNT, PcodeOp.LZCOUNT };
/**
* An edge between a Varnode and a PcodeOp

View file

@ -132,8 +132,9 @@ public class PcodeOp {
public static final int INSERT = 70;
public static final int EXTRACT = 71;
public static final int POPCOUNT = 72;
public static final int LZCOUNT = 73;
public static final int PCODE_MAX = 73;
public static final int PCODE_MAX = 74;
private static Hashtable<String, Integer> opcodeTable;
@ -689,6 +690,8 @@ public class PcodeOp {
return "EXTRACT";
case POPCOUNT:
return "POPCOUNT";
case LZCOUNT:
return "LZCOUNT";
default:
return "INVALID_OP";

View file

@ -0,0 +1,91 @@
/* ###
* 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.utils.Utils;
public class OpBehaviorLzcountTest extends AbstractOpBehaviorTest {
@Test
public void testEvaluateUnaryLong() {
OpBehaviorLzcount op = new OpBehaviorLzcount();
Assert.assertEquals(8, op.evaluateUnary(1, 1, 0L));
Assert.assertEquals(16, op.evaluateUnary(1, 2, 0L));
Assert.assertEquals(32, op.evaluateUnary(1, 4, 0L));
Assert.assertEquals(64, op.evaluateUnary(1, 8, 0L));
Assert.assertEquals(0, op.evaluateUnary(1, 1, 0xffL));
Assert.assertEquals(0, op.evaluateUnary(1, 2, 0xffffL));
Assert.assertEquals(0, op.evaluateUnary(1, 4, 0xffffffffL));
Assert.assertEquals(0, op.evaluateUnary(1, 8, 0xffffffffffffffffL));
Assert.assertEquals(0, op.evaluateUnary(1, 1, 0x96L));
Assert.assertEquals(0, op.evaluateUnary(1, 2, 0xdbf4L));
Assert.assertEquals(1, op.evaluateUnary(1, 4, 0x460f457bL));
Assert.assertEquals(3, op.evaluateUnary(1, 8, 0x1fae97efca7d5759L));
Assert.assertEquals(4, op.evaluateUnary(1, 1, 0xaL));
Assert.assertEquals(6, op.evaluateUnary(1, 2, 0x2a5L));
Assert.assertEquals(9, op.evaluateUnary(1, 4, 0x60dfffL));
Assert.assertEquals(13, op.evaluateUnary(1, 8, 0x635017adefe4eL));
Assert.assertEquals(3, op.evaluateUnary(1, 1, 0x17L));
Assert.assertEquals(8, op.evaluateUnary(1, 2, 0xd1L));
Assert.assertEquals(22, op.evaluateUnary(1, 4, 0x39eL));
Assert.assertEquals(27, op.evaluateUnary(1, 8, 0x189c178d6aL));
Assert.assertEquals(4, op.evaluateUnary(1, 1, 0xfL));
Assert.assertEquals(4, op.evaluateUnary(1, 2, 0xff0L));
Assert.assertEquals(0, op.evaluateUnary(1, 4, 0xffff0000L));
Assert.assertEquals(24, op.evaluateUnary(1, 8, 0xff00ff00ffL));
}
@Test
public void testEvaluateUnaryBigInteger() {
OpBehaviorLzcount op = new OpBehaviorLzcount();
BigInteger NEGATIVE_ONE = Utils.convertToUnsignedValue(BigInteger.valueOf(-1), 16);
BigInteger BIG_POSITIVE = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
BigInteger BIG_NEGATIVE = Utils
.convertToUnsignedValue(new BigInteger("80000000000000000000000000000000", 16), 16);
assertEquals(BigInteger.valueOf(128), op.evaluateUnary(1, 16, BigInteger.ZERO), 16);
assertEquals(BigInteger.ZERO, op.evaluateUnary(1, 16, NEGATIVE_ONE), 16);
assertEquals(BigInteger.ONE, op.evaluateUnary(1, 16, BIG_POSITIVE), 16);
assertEquals(BigInteger.ZERO, op.evaluateUnary(1, 16, BIG_NEGATIVE), 16);
BigInteger val = BigInteger.valueOf(0x35017adefe4eL);
val = val.shiftLeft(64);
val = val.or(Utils.convertToUnsignedValue(BigInteger.valueOf(0xd46223189c178d6aL), 8));
assertEquals(BigInteger.valueOf(18), op.evaluateUnary(1, 16, val), 16);
BigInteger FF = BigInteger.valueOf(0xff);
val = BigInteger.ZERO;
for (int i = 0; i < 20; ++i) {
val = val.shiftLeft(16);
val = val.add(FF);
}
assertEquals(BigInteger.valueOf(8), op.evaluateUnary(1, 40, val), 40);
}
}

View file

@ -0,0 +1,90 @@
/* ###
* 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.utils.Utils;
public class OpBehaviorPopcountTest extends AbstractOpBehaviorTest {
@Test
public void testEvaluateUnaryLong() {
OpBehaviorPopcount op = new OpBehaviorPopcount();
Assert.assertEquals(0, op.evaluateUnary(1, 1, 0L));
Assert.assertEquals(0, op.evaluateUnary(1, 2, 0L));
Assert.assertEquals(0, op.evaluateUnary(1, 4, 0L));
Assert.assertEquals(0, op.evaluateUnary(1, 8, 0L));
Assert.assertEquals(8, op.evaluateUnary(1, 1, 0xffL));
Assert.assertEquals(16, op.evaluateUnary(1, 2, 0xffffL));
Assert.assertEquals(32, op.evaluateUnary(1, 4, 0xffffffffL));
Assert.assertEquals(64, op.evaluateUnary(1, 8, 0xffffffffffffffffL));
Assert.assertEquals(4, op.evaluateUnary(1, 1, 0x96L));
Assert.assertEquals(11, op.evaluateUnary(1, 2, 0xdbf4L));
Assert.assertEquals(16, op.evaluateUnary(1, 4, 0x460f457bL));
Assert.assertEquals(41, op.evaluateUnary(1, 8, 0x1fae97efca7d5759L));
Assert.assertEquals(5, op.evaluateUnary(1, 1, 0x7aL));
Assert.assertEquals(10, op.evaluateUnary(1, 2, 0xfca5L));
Assert.assertEquals(20, op.evaluateUnary(1, 4, 0x2660dfffL));
Assert.assertEquals(38, op.evaluateUnary(1, 8, 0x79f635017adefe4eL));
Assert.assertEquals(4, op.evaluateUnary(1, 1, 0x17L));
Assert.assertEquals(10, op.evaluateUnary(1, 2, 0x77d1L));
Assert.assertEquals(15, op.evaluateUnary(1, 4, 0x5758039eL));
Assert.assertEquals(28, op.evaluateUnary(1, 8, 0xd46223189c178d6aL));
Assert.assertEquals(7, op.evaluateUnary(1, 1, 0xbfL));
Assert.assertEquals(12, op.evaluateUnary(1, 2, 0xe3efL));
Assert.assertEquals(17, op.evaluateUnary(1, 4, 0xb2d7e134L));
Assert.assertEquals(34, op.evaluateUnary(1, 8, 0x69f7a0fa6eeda6L));
}
@Test
public void testEvaluateUnaryBigInteger() {
OpBehaviorPopcount op = new OpBehaviorPopcount();
BigInteger NEGATIVE_ONE = Utils.convertToUnsignedValue(BigInteger.valueOf(-1), 16);
BigInteger BIG_POSITIVE = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
BigInteger BIG_NEGATIVE = Utils
.convertToUnsignedValue(new BigInteger("80000000000000000000000000000000", 16), 16);
assertEquals(BigInteger.ZERO, op.evaluateUnary(1, 16, BigInteger.ZERO), 16);
assertEquals(BigInteger.valueOf(128), op.evaluateUnary(1, 16, NEGATIVE_ONE), 16);
assertEquals(BigInteger.valueOf(127), op.evaluateUnary(1, 16, BIG_POSITIVE), 16);
assertEquals(BigInteger.ONE, op.evaluateUnary(1, 16, BIG_NEGATIVE), 16);
BigInteger val = BigInteger.valueOf(0x79f635017adefe4eL);
val = val.shiftLeft(64);
val = val.or(Utils.convertToUnsignedValue(BigInteger.valueOf(0xd46223189c178d6aL), 8));
assertEquals(BigInteger.valueOf(66), op.evaluateUnary(1, 16, val), 16);
BigInteger FF = BigInteger.valueOf(0xff);
val = BigInteger.ZERO;
for (int i = 0; i < 20; ++i) {
val = val.shiftLeft(16);
val = val.add(FF);
}
assertEquals(BigInteger.valueOf(160), op.evaluateUnary(1, 40, val), 40);
}
}