Skip to content

Commit abe9d83

Browse files
committed
Merge remote-tracking branch 'origin/GT-3252-subBlock_issues' into Ghidra_9.1
2 parents aa186d3 + 0f80eef commit abe9d83

File tree

10 files changed

+148
-96
lines changed

10 files changed

+148
-96
lines changed

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/BitMappedSubMemoryBlock.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ public boolean isInitialized() {
4545
}
4646

4747
@Override
48-
public byte getByte(long offset) throws MemoryAccessException, IOException {
48+
public byte getByte(long offsetInMemBlock) throws MemoryAccessException, IOException {
49+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
4950
if (ioPending) {
5051
throw new MemoryAccessException("Cyclic Access");
5152
}
5253
try {
5354
ioPending = true;
54-
return getBitOverlayByte(offset);
55+
return getBitOverlayByte(offsetInSubBlock);
5556
}
5657
catch (AddressOverflowException e) {
5758
throw new MemoryAccessException("No memory at address");
@@ -62,21 +63,23 @@ public byte getByte(long offset) throws MemoryAccessException, IOException {
6263
}
6364

6465
public AddressRange getMappedRange() {
65-
Address endMappedAddress = mappedAddress.add((length - 1) / 8);
66+
Address endMappedAddress = mappedAddress.add((subBlockLength - 1) / 8);
6667
return new AddressRangeImpl(mappedAddress, endMappedAddress);
6768
}
6869

6970
@Override
70-
public int getBytes(long offset, byte[] b, int off, int len)
71+
public int getBytes(long offsetInMemBlock, byte[] b, int off, int len)
7172
throws MemoryAccessException, IOException {
73+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
74+
long available = subBlockLength - offsetInSubBlock;
75+
len = (int) Math.min(len, available);
7276
if (ioPending) {
7377
new MemoryAccessException("Cyclic Access");
7478
}
7579
try {
7680
ioPending = true;
77-
len = (int) Math.min(len, length - offset);
7881
for (int i = 0; i < len; i++) {
79-
b[i + off] = getBitOverlayByte(offset++);
82+
b[i + off] = getBitOverlayByte(offsetInMemBlock++);
8083
}
8184
return len;
8285
}
@@ -89,13 +92,15 @@ public int getBytes(long offset, byte[] b, int off, int len)
8992
}
9093

9194
@Override
92-
public void putByte(long offset, byte b) throws MemoryAccessException, IOException {
95+
public void putByte(long offsetInMemBlock, byte b) throws MemoryAccessException, IOException {
96+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
9397
try {
9498
if (ioPending) {
9599
new MemoryAccessException("Cyclic Access");
96100
}
97101
ioPending = true;
98-
doPutByte(mappedAddress.addNoWrap(offset / 8), (int) (offset % 8), b);
102+
doPutByte(mappedAddress.addNoWrap(offsetInSubBlock / 8), (int) (offsetInSubBlock % 8),
103+
b);
99104
}
100105
catch (AddressOverflowException e) {
101106
new MemoryAccessException("No memory at address");
@@ -107,17 +112,20 @@ public void putByte(long offset, byte b) throws MemoryAccessException, IOExcepti
107112
}
108113

109114
@Override
110-
public int putBytes(long offset, byte[] b, int off, int len)
115+
public int putBytes(long offsetInMemBlock, byte[] b, int off, int len)
111116
throws MemoryAccessException, IOException {
117+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
118+
long available = subBlockLength - offsetInSubBlock;
119+
len = (int) Math.min(len, available);
112120
try {
113121
if (ioPending) {
114122
new MemoryAccessException("Cyclic Access");
115123
}
116124
ioPending = true;
117-
len = (int) Math.min(len, length - offset);
118125
for (int i = 0; i < len; i++) {
119-
doPutByte(mappedAddress.addNoWrap(offset / 8), (int) (offset % 8), b[off + i]);
120-
offset++;
126+
doPutByte(mappedAddress.addNoWrap(offsetInSubBlock / 8),
127+
(int) (offsetInSubBlock % 8), b[off + i]);
128+
offsetInSubBlock++;
121129
}
122130
return len;
123131
}

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/BufferSubMemoryBlock.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,32 @@ public boolean isInitialized() {
4141
}
4242

4343
@Override
44-
public byte getByte(long offset) throws IOException {
45-
return buf.getByte((int) (offset - startingOffset));
44+
public byte getByte(long offsetInMemBlock) throws IOException {
45+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
46+
return buf.getByte((int) offsetInSubBlock);
4647
}
4748

4849
@Override
49-
public int getBytes(long offset, byte[] b, int off, int len) throws IOException {
50-
len = Math.min(len, (int) (length - (offset - startingOffset)));
51-
buf.get((int) (offset - startingOffset), b, off, len);
50+
public int getBytes(long offsetInMemBlock, byte[] b, int off, int len) throws IOException {
51+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
52+
long available = subBlockLength - offsetInSubBlock;
53+
len = (int) Math.min(len, available);
54+
buf.get((int) offsetInSubBlock, b, off, len);
5255
return len;
5356
}
5457

5558
@Override
56-
public void putByte(long offset, byte b) throws IOException {
57-
buf.putByte((int) (offset - startingOffset), b);
59+
public void putByte(long offsetInMemBlock, byte b) throws IOException {
60+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
61+
buf.putByte((int) offsetInSubBlock, b);
5862
}
5963

6064
@Override
61-
public int putBytes(long offset, byte[] b, int off, int len) throws IOException {
62-
len = Math.min(len, (int) (length - offset - startingOffset));
63-
buf.put((int) (offset - startingOffset), b, off, len);
65+
public int putBytes(long offsetInMemBlock, byte[] b, int off, int len) throws IOException {
66+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
67+
long available = subBlockLength - offsetInSubBlock;
68+
len = (int) Math.min(len, available);
69+
buf.put((int) offsetInSubBlock, b, off, len);
6470
return len;
6571
}
6672

@@ -76,11 +82,11 @@ protected boolean join(SubMemoryBlock block) throws IOException {
7682
return false;
7783
}
7884
BufferSubMemoryBlock other = (BufferSubMemoryBlock) block;
79-
if (other.length + length > Memory.GBYTE) {
85+
if (other.subBlockLength + subBlockLength > Memory.GBYTE) {
8086
return false;
8187
}
8288
buf.append(other.buf);
83-
setLength(length + other.length);
89+
setLength(subBlockLength + other.subBlockLength);
8490
adapter.deleteSubBlock(other.record.getKey());
8591
return true;
8692
}
@@ -97,10 +103,10 @@ protected MemoryBlockType getType() {
97103
@Override
98104
protected SubMemoryBlock split(long memBlockOffset) throws IOException {
99105
// convert from offset in block to offset in this sub block
100-
int offset = (int) (memBlockOffset - startingOffset);
101-
long newLength = length - offset;
102-
length = offset;
103-
record.setLongValue(MemoryMapDBAdapter.SUB_LENGTH_COL, length);
106+
int offset = (int) (memBlockOffset - subBlockOffset);
107+
long newLength = subBlockLength - offset;
108+
subBlockLength = offset;
109+
record.setLongValue(MemoryMapDBAdapter.SUB_LENGTH_COL, subBlockLength);
104110
adapter.updateSubBlockRecord(record);
105111

106112
DBBuffer split = buf.split(offset);
@@ -122,8 +128,7 @@ protected ByteSourceRangeList getByteSourceRangeList(MemoryBlock block, Address
122128
long size) {
123129
long sourceId = -buf.getId(); // buffers use negative id values; FileBytes use positive id values.
124130
ByteSourceRange bsRange =
125-
new ByteSourceRange(block, start, size, sourceId, memBlockOffset - startingOffset);
131+
new ByteSourceRange(block, start, size, sourceId, memBlockOffset - subBlockOffset);
126132
return new ByteSourceRangeList(bsRange);
127133
}
128134
}
129-

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/ByteMappedSubMemoryBlock.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ public boolean isInitialized() {
4646
}
4747

4848
@Override
49-
public byte getByte(long offset) throws MemoryAccessException, IOException {
49+
public byte getByte(long offsetInMemBlock) throws MemoryAccessException, IOException {
50+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
5051
if (ioPending) {
5152
new MemoryAccessException("Cyclic Access");
5253
}
5354
try {
5455
ioPending = true;
55-
return memMap.getByte(mappedAddress.addNoWrap(offset - startingOffset));
56+
return memMap.getByte(mappedAddress.addNoWrap(offsetInSubBlock));
5657
}
5758
catch (AddressOverflowException e) {
5859
throw new MemoryAccessException("No memory at address");
@@ -63,15 +64,17 @@ public byte getByte(long offset) throws MemoryAccessException, IOException {
6364
}
6465

6566
@Override
66-
public int getBytes(long offset, byte[] b, int off, int len)
67+
public int getBytes(long offsetInMemBlock, byte[] b, int off, int len)
6768
throws MemoryAccessException, IOException {
69+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
70+
long available = subBlockLength - offsetInSubBlock;
71+
len = (int) Math.min(len, available);
6872
if (ioPending) {
6973
new MemoryAccessException("Cyclic Access");
7074
}
7175
try {
7276
ioPending = true;
73-
len = (int) Math.min(len, length - (offset - startingOffset));
74-
return memMap.getBytes(mappedAddress.addNoWrap(offset), b, off, len);
77+
return memMap.getBytes(mappedAddress.addNoWrap(offsetInSubBlock), b, off, len);
7578
}
7679
catch (AddressOverflowException e) {
7780
throw new MemoryAccessException("No memory at address");
@@ -82,13 +85,14 @@ public int getBytes(long offset, byte[] b, int off, int len)
8285
}
8386

8487
@Override
85-
public void putByte(long offset, byte b) throws MemoryAccessException, IOException {
88+
public void putByte(long offsetInMemBlock, byte b) throws MemoryAccessException, IOException {
89+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
8690
try {
8791
if (ioPending) {
8892
new MemoryAccessException("Cyclic Access");
8993
}
9094
ioPending = true;
91-
memMap.setByte(mappedAddress.addNoWrap(offset - startingOffset), b);
95+
memMap.setByte(mappedAddress.addNoWrap(offsetInSubBlock), b);
9296
}
9397
catch (AddressOverflowException e) {
9498
throw new MemoryAccessException("No memory at address");
@@ -100,15 +104,18 @@ public void putByte(long offset, byte b) throws MemoryAccessException, IOExcepti
100104
}
101105

102106
@Override
103-
public int putBytes(long offset, byte[] b, int off, int len)
107+
public int putBytes(long offsetInMemBlock, byte[] b, int off, int len)
104108
throws MemoryAccessException, IOException {
109+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
110+
long available = subBlockLength - offsetInSubBlock;
111+
len = (int) Math.min(len, available);
105112
try {
106113
if (ioPending) {
107114
new MemoryAccessException("Cyclic Access");
108115
}
109116
ioPending = true;
110-
len = (int) Math.min(len, length - (offset - startingOffset));
111-
memMap.setBytes(mappedAddress.addNoWrap(offset - startingOffset), b, off, len);
117+
memMap.setBytes(mappedAddress.addNoWrap(offsetInSubBlock), b, off,
118+
len);
112119
return len;
113120
}
114121
catch (AddressOverflowException e) {
@@ -120,7 +127,7 @@ public int putBytes(long offset, byte[] b, int off, int len)
120127
}
121128

122129
public AddressRange getMappedRange() {
123-
Address endMappedAddress = mappedAddress.add(length - 1);
130+
Address endMappedAddress = mappedAddress.add(subBlockLength - 1);
124131
return new AddressRangeImpl(mappedAddress, endMappedAddress);
125132
}
126133

@@ -142,10 +149,10 @@ protected MemoryBlockType getType() {
142149
@Override
143150
protected SubMemoryBlock split(long memBlockOffset) throws IOException {
144151
// convert from offset in block to offset in this sub block
145-
int offset = (int) (memBlockOffset - startingOffset);
146-
long newLength = length - offset;
147-
length = offset;
148-
record.setLongValue(MemoryMapDBAdapter.SUB_LENGTH_COL, length);
152+
int offset = (int) (memBlockOffset - subBlockOffset);
153+
long newLength = subBlockLength - offset;
154+
subBlockLength = offset;
155+
record.setLongValue(MemoryMapDBAdapter.SUB_LENGTH_COL, subBlockLength);
149156
adapter.updateSubBlockRecord(record);
150157

151158
Address newAddr = mappedAddress.add(offset);
@@ -167,7 +174,7 @@ protected String getDescription() {
167174
protected ByteSourceRangeList getByteSourceRangeList(MemoryBlock block, Address start,
168175
long offset, long size) {
169176
ByteSourceRangeList result = new ByteSourceRangeList();
170-
long relativeOffset = offset - startingOffset;
177+
long relativeOffset = offset - subBlockOffset;
171178
Address startAddress = mappedAddress.add(relativeOffset);
172179
Address endAddress = startAddress.add(size - 1);
173180
List<MemoryBlockDB> blocks = memMap.getBlocks(startAddress, endAddress);

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/FileBytesSubMemoryBlock.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,31 @@ public boolean isInitialized() {
4141
}
4242

4343
@Override
44-
public byte getByte(long memBlockOffset) throws IOException {
45-
return fileBytes.getModifiedByte(fileBytesOffset + memBlockOffset - startingOffset);
44+
public byte getByte(long offsetInMemBlock) throws IOException {
45+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
46+
return fileBytes.getModifiedByte(fileBytesOffset + offsetInSubBlock);
4647
}
4748

4849
@Override
49-
public int getBytes(long memBlockOffset, byte[] b, int off, int len) throws IOException {
50-
return fileBytes.getModifiedBytes(fileBytesOffset + memBlockOffset - startingOffset, b, off,
51-
len);
50+
public int getBytes(long offsetInMemBlock, byte[] b, int off, int len) throws IOException {
51+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
52+
long available = subBlockLength - offsetInSubBlock;
53+
len = (int) Math.min(len, available);
54+
return fileBytes.getModifiedBytes(fileBytesOffset + offsetInSubBlock, b, off, len);
5255
}
5356

5457
@Override
55-
public void putByte(long memBlockOffset, byte b) throws MemoryAccessException, IOException {
56-
fileBytes.putByte(fileBytesOffset + memBlockOffset - startingOffset, b);
58+
public void putByte(long offsetInMemBlock, byte b) throws MemoryAccessException, IOException {
59+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
60+
fileBytes.putByte(fileBytesOffset + offsetInSubBlock, b);
5761
}
5862

5963
@Override
60-
public int putBytes(long memBlockOffset, byte[] b, int off, int len) throws IOException {
61-
return fileBytes.putBytes(fileBytesOffset + memBlockOffset - startingOffset, b, off, len);
64+
public int putBytes(long offsetInMemBlock, byte[] b, int off, int len) throws IOException {
65+
long offsetInSubBlock = offsetInMemBlock - subBlockOffset;
66+
long available = subBlockLength - offsetInSubBlock;
67+
len = (int) Math.min(len, available);
68+
return fileBytes.putBytes(fileBytesOffset + offsetInSubBlock, b, off, len);
6269
}
6370

6471
@Override
@@ -71,11 +78,11 @@ protected boolean join(SubMemoryBlock block) throws IOException {
7178
return false;
7279
}
7380
// are the two block consecutive in the fileBytes space?
74-
if (other.fileBytesOffset != fileBytesOffset + length) {
81+
if (other.fileBytesOffset != fileBytesOffset + subBlockLength) {
7582
return false;
7683
}
7784
// ok we can join them
78-
setLength(length + other.length);
85+
setLength(subBlockLength + other.subBlockLength);
7986
adapter.deleteSubBlock(other.record.getKey());
8087
return true;
8188
}
@@ -96,10 +103,10 @@ protected MemoryBlockType getType() {
96103
@Override
97104
protected SubMemoryBlock split(long memBlockOffset) throws IOException {
98105
// convert from offset in block to offset in this sub block
99-
int offset = (int) (memBlockOffset - startingOffset);
100-
long newLength = length - offset;
101-
length = offset;
102-
record.setLongValue(MemoryMapDBAdapter.SUB_LENGTH_COL, length);
106+
int offset = (int) (memBlockOffset - subBlockOffset);
107+
long newLength = subBlockLength - offset;
108+
subBlockLength = offset;
109+
record.setLongValue(MemoryMapDBAdapter.SUB_LENGTH_COL, subBlockLength);
103110
adapter.updateSubBlockRecord(record);
104111

105112
int fileBytesID = record.getIntValue(MemoryMapDBAdapter.SUB_SOURCE_ID_COL);
@@ -128,9 +135,8 @@ protected ByteSourceRangeList getByteSourceRangeList(MemoryBlock block, Address
128135
long size) {
129136
long sourceId = fileBytes.getId();
130137
ByteSourceRange bsRange = new ByteSourceRange(block, start, size, sourceId,
131-
fileBytesOffset + memBlockOffset - startingOffset);
138+
fileBytesOffset + memBlockOffset - subBlockOffset);
132139
return new ByteSourceRangeList(bsRange);
133140
}
134141

135142
}
136-

0 commit comments

Comments
 (0)