mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 10:19:23 +02:00
GT-3064 fixing importing to be cancellable
This commit is contained in:
parent
70757b658e
commit
33c5feac44
22 changed files with 279 additions and 252 deletions
|
@ -20,6 +20,7 @@ import java.io.InputStream;
|
|||
import java.util.*;
|
||||
|
||||
import db.*;
|
||||
import ghidra.util.exception.IOCancelledException;
|
||||
import ghidra.util.exception.VersionException;
|
||||
|
||||
/**
|
||||
|
@ -158,9 +159,18 @@ class FileBytesAdapterV0 extends FileBytesAdapter {
|
|||
}
|
||||
buffers[bufCount - 1] = handle.createBuffer(sizeLastBuf);
|
||||
|
||||
for (DBBuffer buffer : buffers) {
|
||||
buffer.fill(is);
|
||||
try {
|
||||
for (DBBuffer buffer : buffers) {
|
||||
buffer.fill(is);
|
||||
}
|
||||
}
|
||||
catch (IOCancelledException e) {
|
||||
for (DBBuffer buffer : buffers) {
|
||||
buffer.delete();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
return buffers;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -494,8 +494,10 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
|
|||
return newBlock;
|
||||
}
|
||||
catch (IOCancelledException e) {
|
||||
// TODO: this could leave things in a bad state.
|
||||
// Canceling requires additional improvements (see GT-3064)
|
||||
// this assumes the adapter has already cleaned up any partially created buffers.
|
||||
if (overlay) {
|
||||
checkRemoveAddressSpace(start.getAddressSpace());
|
||||
}
|
||||
throw new CancelledException();
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
@ -2022,14 +2024,18 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
|
|||
}
|
||||
|
||||
@Override
|
||||
public FileBytes createFileBytes(String filename, long offset, long size, InputStream is)
|
||||
throws IOException {
|
||||
public FileBytes createFileBytes(String filename, long offset, long size, InputStream is,
|
||||
TaskMonitor monitor) throws IOException, CancelledException {
|
||||
lock.acquire();
|
||||
try {
|
||||
// TODO: this should accept task monitor to permit cancellation although
|
||||
// canceling requires additional improvements (see GT-3064)
|
||||
if (monitor != null && is != null) {
|
||||
is = new MonitoredInputStream(is, monitor);
|
||||
}
|
||||
return fileBytesAdapter.createFileBytes(filename, offset, size, is);
|
||||
}
|
||||
catch (IOCancelledException e) {
|
||||
throw new CancelledException();
|
||||
}
|
||||
finally {
|
||||
lock.release();
|
||||
}
|
||||
|
|
|
@ -148,6 +148,8 @@ public class MemoryMapDBAdapterV3 extends MemoryMapDBAdapter {
|
|||
@Override
|
||||
MemoryBlockDB createInitializedBlock(String name, Address startAddr, InputStream is,
|
||||
long length, int permissions) throws AddressOverflowException, IOException {
|
||||
|
||||
// TODO verify that it is necessary to pre-define all segments in the address map
|
||||
updateAddressMapForAllAddresses(startAddr, length);
|
||||
|
||||
List<SubMemoryBlock> subBlocks = new ArrayList<>();
|
||||
|
@ -171,7 +173,7 @@ public class MemoryMapDBAdapterV3 extends MemoryMapDBAdapter {
|
|||
Collections.sort(memoryBlocks);
|
||||
return newBlock;
|
||||
}
|
||||
catch (IOException e) {
|
||||
catch (IOCancelledException e) {
|
||||
// clean up any created DBBufferss
|
||||
for (SubMemoryBlock subMemoryBlock : subBlocks) {
|
||||
BufferSubMemoryBlock bufferSubMemoryBlock = (BufferSubMemoryBlock) subMemoryBlock;
|
||||
|
|
|
@ -729,11 +729,14 @@ public interface Memory extends AddressSetView {
|
|||
* @param offset the offset into the file for the first byte in the input stream.
|
||||
* @param size the number of bytes to store from the input stream.
|
||||
* @param is the input stream that will supply the bytes to store in the program.
|
||||
* @param monitor
|
||||
* @return a FileBytes that was created to access the bytes.
|
||||
* @throws IOException if there was an IOException saving the bytes to the program database.
|
||||
* @throws CancelledException if the user cancelled this operation. Note: the database will
|
||||
* be stable, but the buffers may contain 0s instead of the actual bytes.
|
||||
*/
|
||||
public FileBytes createFileBytes(String filename, long offset, long size, InputStream is)
|
||||
throws IOException;
|
||||
public FileBytes createFileBytes(String filename, long offset, long size, InputStream is,
|
||||
TaskMonitor monitor) throws IOException, CancelledException;
|
||||
|
||||
/**
|
||||
* Returns a list of all the stored original file bytes objects
|
||||
|
|
|
@ -469,8 +469,8 @@ public class MemoryStub implements Memory {
|
|||
}
|
||||
|
||||
@Override
|
||||
public FileBytes createFileBytes(String filename, long offset, long size, InputStream is)
|
||||
throws IOException {
|
||||
public FileBytes createFileBytes(String filename, long offset, long size, InputStream is,
|
||||
TaskMonitor monitor) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ public class FileBytesTest extends AbstractGenericTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testStoreAndRetrieveFileBytes() throws IOException {
|
||||
public void testStoreAndRetrieveFileBytes() throws Exception {
|
||||
int dataSize = MAX_BUFFER_SIZE_FOR_TESTING / 2;
|
||||
FileBytes fileBytes = createFileBytes("testFile", dataSize);
|
||||
|
||||
|
@ -186,13 +186,13 @@ public class FileBytesTest extends AbstractGenericTest {
|
|||
}
|
||||
}
|
||||
|
||||
private FileBytes createFileBytes(String name, int size) throws IOException {
|
||||
private FileBytes createFileBytes(String name, int size) throws Exception {
|
||||
byte[] bytes = new byte[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
bytes[i] = (byte) i;
|
||||
}
|
||||
try (ByteArrayInputStream is = new ByteArrayInputStream(bytes)) {
|
||||
return mem.createFileBytes(name, 0, size, is);
|
||||
return mem.createFileBytes(name, 0, size, is, TaskMonitor.DUMMY);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ package ghidra.program.database.mem;
|
|||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.*;
|
||||
|
@ -282,7 +281,8 @@ public class MemBlockDBTest extends AbstractGenericTest {
|
|||
@Test
|
||||
public void testCreateFileBytesBlockOutSideRange() throws Exception {
|
||||
byte[] bytes = new byte[256];
|
||||
FileBytes fileBytes = mem.createFileBytes("test", 0, 100, new ByteArrayInputStream(bytes));
|
||||
FileBytes fileBytes =
|
||||
mem.createFileBytes("test", 0, 100, new ByteArrayInputStream(bytes), TaskMonitor.DUMMY);
|
||||
try {
|
||||
mem.createInitializedBlock("test", addr(100), fileBytes, 10, 100, false);
|
||||
fail(
|
||||
|
@ -379,7 +379,6 @@ public class MemBlockDBTest extends AbstractGenericTest {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testJoinFileBytesBlockAndBufferBlock() throws Exception {
|
||||
FileBytes fileBytes = createFileBytes();
|
||||
|
@ -695,7 +694,6 @@ public class MemBlockDBTest extends AbstractGenericTest {
|
|||
assertEquals(2, ranges.getRangeCount()); // we have two sublocks so two distinct ranges
|
||||
assertEquals(10, ranges.get(0).getSize() + ranges.get(1).getSize());
|
||||
|
||||
|
||||
ByteSourceRange range = ranges.get(0);
|
||||
assertEquals(10, range.getStart().getOffset());
|
||||
assertEquals(15, range.getEnd().getOffset());
|
||||
|
@ -857,7 +855,7 @@ public class MemBlockDBTest extends AbstractGenericTest {
|
|||
assertEquals(0, range.getOffset());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test
|
||||
public void testAddressSourceInfoForFileBytesBlock() throws Exception {
|
||||
FileBytes fileBytes = createFileBytes();
|
||||
mem.createInitializedBlock("block", addr(100), fileBytes, 10, 50, false);
|
||||
|
@ -919,12 +917,13 @@ public class MemBlockDBTest extends AbstractGenericTest {
|
|||
false);
|
||||
}
|
||||
|
||||
private FileBytes createFileBytes() throws IOException {
|
||||
private FileBytes createFileBytes() throws Exception {
|
||||
byte[] bytes = new byte[256];
|
||||
for (int i = 0; i < 256; i++) {
|
||||
bytes[i] = (byte) i;
|
||||
}
|
||||
FileBytes fileBytes = mem.createFileBytes("test", 0, 100, new ByteArrayInputStream(bytes));
|
||||
FileBytes fileBytes =
|
||||
mem.createFileBytes("test", 0, 100, new ByteArrayInputStream(bytes), TaskMonitor.DUMMY);
|
||||
return fileBytes;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue