Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out of bounds read in bit chunk iterator #416

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions arrow/benches/boolean_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ fn add_benchmark(c: &mut Criterion) {
c.bench_function("and", |b| b.iter(|| bench_and(&array1, &array2)));
c.bench_function("or", |b| b.iter(|| bench_or(&array1, &array2)));
c.bench_function("not", |b| b.iter(|| bench_not(&array1)));

let array1_slice = array1.slice(1, size - 1);
let array1_slice = array1_slice
.as_any()
.downcast_ref::<BooleanArray>()
.unwrap();
let array2_slice = array2.slice(1, size - 1);
let array2_slice = array2_slice
.as_any()
.downcast_ref::<BooleanArray>()
.unwrap();

c.bench_function("and_sliced", |b| {
b.iter(|| bench_and(&array1_slice, &array2_slice))
});
c.bench_function("or_sliced", |b| {
b.iter(|| bench_or(&array1_slice, &array2_slice))
});
c.bench_function("not_sliced", |b| b.iter(|| bench_not(&array1_slice)));
}

criterion_group!(benches, add_benchmark);
Expand Down
40 changes: 27 additions & 13 deletions arrow/src/util/bit_chunk_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl<'a> BitChunks<'a> {
let byte_offset = offset / 8;
let bit_offset = offset % 8;

let chunk_bits = 8 * std::mem::size_of::<u64>();

let chunk_len = len / chunk_bits;
let remainder_len = len & (chunk_bits - 1);
// number of complete u64 chunks
let chunk_len = len / 64;
// number of remaining bits
let remainder_len = len % 64;

BitChunks::<'a> {
buffer: &buffer[byte_offset..],
Expand Down Expand Up @@ -137,14 +137,18 @@ impl Iterator for BitChunkIterator<'_> {
// so when reading as u64 on a big-endian machine, the bytes need to be swapped
let current = unsafe { std::ptr::read_unaligned(raw_data.add(index)).to_le() };

let combined = if self.bit_offset == 0 {
let bit_offset = self.bit_offset;

let combined = if bit_offset == 0 {
current
} else {
let next =
unsafe { std::ptr::read_unaligned(raw_data.add(index + 1)).to_le() };
// the constructor ensures that bit_offset is in 0..8
// that means we need to read at most one additional byte to fill in the high bits
let next = unsafe {
std::ptr::read_unaligned(raw_data.add(index + 1) as *const u8) as u64
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix here is casting the pointer back to *u8 and reading only a single byte.

The other changes are a bit of a cleanup, the masking of next below should not be needed since it masked of exactly the bits that would be shifted out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not the remainder, don't we potentially need to read more than 8 bits? I.e. doesn't this index contain between 1 and 63 bits that need to be "merged" into current?

I get a feeling that this will ignore all bits after the 8th and less than 64. At least this is what I remember from fixing it in arrow2 here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor ensures that the bit_offset is between 0..8, this means we need to be able to read unaligned u64, but need at most one additional byte. I'll add this as a comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right 👍 Good thinking. Thanks for the clarification.

};

current >> self.bit_offset
| (next & ((1 << self.bit_offset) - 1)) << (64 - self.bit_offset)
(current >> bit_offset) | (next << (64 - bit_offset))
};

self.index = index + 1;
Expand Down Expand Up @@ -184,7 +188,6 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
fn test_iter_unaligned() {
let input: &[u8] = &[
0b00000000, 0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000,
Expand All @@ -206,7 +209,6 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
fn test_iter_unaligned_remainder_1_byte() {
let input: &[u8] = &[
0b00000000, 0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000,
Expand All @@ -228,7 +230,6 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
fn test_iter_unaligned_remainder_bits_across_bytes() {
let input: &[u8] = &[0b00111111, 0b11111100];
let buffer: Buffer = Buffer::from(input);
Expand All @@ -242,7 +243,6 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
fn test_iter_unaligned_remainder_bits_large() {
let input: &[u8] = &[
0b11111111, 0b00000000, 0b11111111, 0b00000000, 0b11111111, 0b00000000,
Expand All @@ -258,4 +258,18 @@ mod tests {
bitchunks.remainder_bits()
);
}

#[test]
fn test_iter_remainder_out_of_bounds() {
// allocating a full page should trigger a fault when reading out of bounds
const ALLOC_SIZE: usize = 4 * 1024;
let input = vec![0xFF_u8; ALLOC_SIZE];

let buffer: Buffer = Buffer::from(input);

let bitchunks = buffer.bit_chunks(57, ALLOC_SIZE * 8 - 57);

assert_eq!(u64::MAX, bitchunks.iter().last().unwrap());
assert_eq!(0x7F, bitchunks.remainder_bits());
}
}