AMQ-9254 - Rework data file size validation and add unit test

This isolates the validation on data file length on read and adds unit
tests to verify we properly fallback to the real file length on initial
size check failure

(cherry picked from commit bcc74f93fe)
This commit is contained in:
Christopher L. Shannon (cshannon) 2023-05-02 19:09:23 -04:00
parent cd676023cc
commit 78a399cb87
3 changed files with 123 additions and 15 deletions

View File

@ -137,6 +137,11 @@
<artifactId>jmock-legacy</artifactId> <artifactId>jmock-legacy</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -19,6 +19,7 @@ package org.apache.activemq.store.kahadb.disk.journal;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.apache.activemq.util.ByteSequence; import org.apache.activemq.util.ByteSequence;
import org.apache.activemq.util.RecoverableRandomAccessFile; import org.apache.activemq.util.RecoverableRandomAccessFile;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -75,7 +76,6 @@ final class DataFileAccessor {
} }
try { try {
if (location.getSize() == Location.NOT_SET) { if (location.getSize() == Location.NOT_SET) {
file.seek(location.getOffset()); file.seek(location.getOffset());
location.setSize(file.readInt()); location.setSize(file.readInt());
@ -83,19 +83,7 @@ final class DataFileAccessor {
} else { } else {
file.seek(location.getOffset() + Journal.RECORD_HEAD_SPACE); file.seek(location.getOffset() + Journal.RECORD_HEAD_SPACE);
} }
if ((long)location.getOffset() + location.getSize() > dataFile.length) { validateFileLength(location);
/**
* AMQ-9254 if the read request is outside expected dataFile length,
* perform expensive OS file length lookup operation
* to allow read operation if it will succeed
*/
long osFileLength = dataFile.getFile().length();
if((long)location.getOffset() + location.getSize() > osFileLength) {
throw new IOException("Invalid location size: " + location + ", size: " + location.getSize());
} else {
LOG.warn("DataFile:{} actual length:{} larger than expected:{} for readRecord location:{} size:{}", dataFile.file.getName(), osFileLength, dataFile.length, location, location.getSize());
}
}
byte[] data = new byte[location.getSize() - Journal.RECORD_HEAD_SPACE]; byte[] data = new byte[location.getSize() - Journal.RECORD_HEAD_SPACE];
file.readFully(data); file.readFully(data);
return new ByteSequence(data, 0, data.length); return new ByteSequence(data, 0, data.length);
@ -127,7 +115,6 @@ final class DataFileAccessor {
} }
} }
public void updateRecord(Location location, ByteSequence data, boolean sync) throws IOException { public void updateRecord(Location location, ByteSequence data, boolean sync) throws IOException {
file.seek(location.getOffset() + Journal.RECORD_HEAD_SPACE); file.seek(location.getOffset() + Journal.RECORD_HEAD_SPACE);
@ -141,4 +128,24 @@ final class DataFileAccessor {
public RecoverableRandomAccessFile getRaf() { public RecoverableRandomAccessFile getRaf() {
return file; return file;
} }
void validateFileLength(final Location location) throws IOException {
final long recordEnd = location.getOffset() + location.getSize();
//Check if the end of the record will go past the file length
if (recordEnd > dataFile.length) {
/*
* AMQ-9254 if the read request is outside expected dataFile length,
* perform expensive OS file length lookup operation to allow read
* operation if it will succeed
*/
final long osFileLength = dataFile.getFile().length();
if(recordEnd > osFileLength) {
throw new IOException("Invalid location size: " + location + ", size: " + location.getSize());
} else {
LOG.warn("DataFile:{} actual length:{} larger than expected:{} for readRecord location:{} size:{}",
dataFile.file.getName(), osFileLength, dataFile.length, location, location.getSize());
}
}
}
} }

View File

@ -0,0 +1,96 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.activemq.store.kahadb.disk.journal;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class DataFileAccessorTest {
@Rule
public TemporaryFolder dataDir = new TemporaryFolder();
@Test
public void testValidLocation() throws IOException {
//Create file of size 1024
final DataFileAccessor accessor = new DataFileAccessor(mock(Journal.class),
newTestDataFile(1024));
//The read check will add the offset and location size and will be 612
//so should be fine as it's less than the set file size of 1024
final Location location = new Location(0, 100);
location.setSize(512);
accessor.validateFileLength(location);
}
@Test(expected = IOException.class)
public void testInvalidLocationSize() throws IOException {
//Create file of size 1024
final DataFileAccessor accessor = new DataFileAccessor(mock(Journal.class),
newTestDataFile(1024));
//Set a size that is too large so this should fail
final Location location = new Location(0, 100);
location.setSize(2048);
accessor.validateFileLength(location);
}
@Test
public void testValidateUsingRealFileLength() throws IOException {
//Create file of size 1024
final DataFile dataFile = newTestDataFile(1024);
final DataFileAccessor accessor = new DataFileAccessor(mock(Journal.class), dataFile);
//Set a bad length value on the dataFile so that the initial check fails
//because the location is greater than dataFile.length
//We should read the real file size (1024) which is greater than the
//location size + offset so this should work
dataFile.setLength(512);
final Location location = new Location(0, 100);
location.setSize(512);
accessor.validateFileLength(location);
}
private DataFile newTestDataFile(int size) throws IOException {
final DataFile dataFile = new DataFile(writeTestFile(1024), 0);
assertEquals(1024, dataFile.length);
return dataFile;
}
private File writeTestFile(int size) throws IOException {
final File file = dataDir.newFile();
final byte[] data = new byte[size];
Arrays.fill(data, (byte)0);
Files.write(Path.of(file.toURI()), data);
assertEquals(1024, file.length());
return file;
}
}