From dc385fd3987956c7fe9f2913850db73fc22aaa73 Mon Sep 17 00:00:00 2001 From: Dan <46821332+nsadeveloper789@users.noreply.github.com> Date: Mon, 6 Jan 2025 13:08:45 -0500 Subject: [PATCH] GP-0: Fix tests (frame display. wrap-around check) --- .../exec/BytesPcodeExecutorStateSpace.java | 3 +- .../BytesPcodeExecutorStateSpaceTest.java | 99 +++++++++++++++++++ .../ghidra/pcode/exec/PcodeFrameTest.java | 12 +-- 3 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 Ghidra/Framework/Emulation/src/test/java/ghidra/pcode/exec/BytesPcodeExecutorStateSpaceTest.java diff --git a/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/exec/BytesPcodeExecutorStateSpace.java b/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/exec/BytesPcodeExecutorStateSpace.java index d08ef6f869..45f627b4fe 100644 --- a/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/exec/BytesPcodeExecutorStateSpace.java +++ b/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/exec/BytesPcodeExecutorStateSpace.java @@ -171,7 +171,8 @@ public class BytesPcodeExecutorStateSpace { */ protected ULongSpanSet computeUninitialized(long offset, int size) { long max = offset + size - 1; - if (max <= space.getMaxAddress().getOffset()) { + if (Long.compareUnsigned(max, space.getMaxAddress().getOffset()) <= 0 && + Long.compareUnsigned(offset, max) <= 0) { return bytes.getUninitialized(offset, max); } long end = space.getMinAddress().getOffset() + max - space.getMaxAddress().getOffset() - 1; diff --git a/Ghidra/Framework/Emulation/src/test/java/ghidra/pcode/exec/BytesPcodeExecutorStateSpaceTest.java b/Ghidra/Framework/Emulation/src/test/java/ghidra/pcode/exec/BytesPcodeExecutorStateSpaceTest.java new file mode 100644 index 0000000000..b5701539f9 --- /dev/null +++ b/Ghidra/Framework/Emulation/src/test/java/ghidra/pcode/exec/BytesPcodeExecutorStateSpaceTest.java @@ -0,0 +1,99 @@ +/* ### + * 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.exec; + +import static org.junit.Assert.assertEquals; + +import java.io.File; + +import org.junit.Before; +import org.junit.Test; + +import generic.ULongSpan; +import generic.ULongSpan.ULongSpanSet; +import generic.test.AbstractGTest; +import ghidra.GhidraTestApplicationLayout; +import ghidra.app.plugin.processors.sleigh.SleighLanguage; +import ghidra.app.plugin.processors.sleigh.SleighLanguageHelper; +import ghidra.framework.Application; +import ghidra.framework.ApplicationConfiguration; +import ghidra.program.model.address.AddressSpace; + +public class BytesPcodeExecutorStateSpaceTest extends AbstractGTest { + + @Before + public void setUp() throws Exception { + if (!Application.isInitialized()) { + Application.initializeApplication( + new GhidraTestApplicationLayout(new File(getTestDirectoryPath())), + new ApplicationConfiguration()); + } + } + + @Test + public void testComputeUninitialized64() throws Exception { + SleighLanguage language = SleighLanguageHelper.getMockBE64Language(); + AddressSpace space = language.getDefaultSpace(); + BytesPcodeExecutorStateSpace stateSpace = + new BytesPcodeExecutorStateSpace(language, space, null); + + ULongSpanSet.of(ULongSpan.span(0, 9)); + + assertEquals(ULongSpanSet.of( + ULongSpan.span(0, 7)), + stateSpace.computeUninitialized(0, 8)); + assertEquals(ULongSpanSet.of( + ULongSpan.span(0x7fff_ffff_ffff_fff8L, 0x7fff_ffff_ffff_ffffL)), + stateSpace.computeUninitialized(0x7fff_ffff_ffff_fff8L, 8)); + assertEquals(ULongSpanSet.of( + ULongSpan.span(0x7fff_ffff_ffff_fffcL, 0x8000_0000_0000_0003L)), + stateSpace.computeUninitialized(0x7fff_ffff_ffff_fffcL, 8)); + assertEquals(ULongSpanSet.of( + ULongSpan.span(0x8000_0000_0000_0008L, 0x8000_0000_0000_000fL)), + stateSpace.computeUninitialized(0x8000_0000_0000_0008L, 8)); + assertEquals(ULongSpanSet.of( + ULongSpan.span(0xffff_ffff_ffff_fffcL, 0xffff_ffff_ffff_ffffL), + ULongSpan.span(0, 3)), + stateSpace.computeUninitialized(0xffff_ffff_ffff_fffcL, 8)); + } + + @Test + public void testComputeUninitialized32() throws Exception { + SleighLanguage language = SleighLanguageHelper.getMockBE64Language(); + AddressSpace space = language.getAddressFactory().getRegisterSpace(); + BytesPcodeExecutorStateSpace stateSpace = + new BytesPcodeExecutorStateSpace(language, space, null); + + ULongSpanSet.of(ULongSpan.span(0, 9)); + + assertEquals(ULongSpanSet.of( + ULongSpan.span(0, 7)), + stateSpace.computeUninitialized(0, 8)); + assertEquals(ULongSpanSet.of( + ULongSpan.span(0x7fff_fff8L, 0x7fff_ffffL)), + stateSpace.computeUninitialized(0x7fff_fff8L, 8)); + assertEquals(ULongSpanSet.of( + ULongSpan.span(0x7fff_fffcL, 0x8000_0003L)), + stateSpace.computeUninitialized(0x7fff_fffcL, 8)); + assertEquals(ULongSpanSet.of( + ULongSpan.span(0x8000_0008L, 0x8000_000fL)), + stateSpace.computeUninitialized(0x8000_0008L, 8)); + assertEquals(ULongSpanSet.of( + ULongSpan.span(0xffff_fffcL, 0xffff_ffffL), + ULongSpan.span(0, 3)), + stateSpace.computeUninitialized(0xffff_fffcL, 8)); + } +} diff --git a/Ghidra/Framework/Emulation/src/test/java/ghidra/pcode/exec/PcodeFrameTest.java b/Ghidra/Framework/Emulation/src/test/java/ghidra/pcode/exec/PcodeFrameTest.java index ec8cb09f69..a1a78b1be3 100644 --- a/Ghidra/Framework/Emulation/src/test/java/ghidra/pcode/exec/PcodeFrameTest.java +++ b/Ghidra/Framework/Emulation/src/test/java/ghidra/pcode/exec/PcodeFrameTest.java @@ -4,9 +4,9 @@ * 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. @@ -226,7 +226,7 @@ public class PcodeFrameTest extends AbstractGTest { PcodeFrame frame = frame(SAMPLE_BRANCH); assertEquals(""" BRANCH *[ram]0x1234:8 + -> BRANCH *[NO ADDRESS]0x1234 }>""", frame.toString()); @@ -234,7 +234,7 @@ public class PcodeFrameTest extends AbstractGTest { frame.finishAsBranch(); assertEquals(""" BRANCH *[ram]0x1234:8 + *> BRANCH *[NO ADDRESS]0x1234 }>""", frame.toString()); } @@ -244,7 +244,7 @@ public class PcodeFrameTest extends AbstractGTest { PcodeFrame frame = frame(SAMPLE_LANG_USEROP); assertEquals(""" CALLOTHER \"pcodeop_one\", r0 + -> CALLOTHER "pcodeop_one", r0 }>""", frame.toString()); } @@ -254,7 +254,7 @@ public class PcodeFrameTest extends AbstractGTest { PcodeFrame frame = frame(SAMPLE_LIB_USEROP); assertEquals(""" CALLOTHER \"__lib_userop\", r0 + -> CALLOTHER "__lib_userop", r0 }>""", frame.toString()); }