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

GH-39577: [C++] Fix tail-word access cross buffer boundary in CompareBinaryColumnToRow #39606

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions cpp/src/arrow/compute/row/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
# in a row-major order.

arrow_install_all_headers("arrow/compute/row")

add_arrow_compute_test(compare_test SOURCES compare_test.cc)
Copy link
Member

Choose a reason for hiding this comment

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

Could you add this to internals_test instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure. Done.

6 changes: 4 additions & 2 deletions cpp/src/arrow/compute/row/compare_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ void KeyCompare::CompareBinaryColumnToRow(uint32_t offset_within_row,
uint64_t key_right = key_right_ptr[i];
result_or |= key_left ^ key_right;
}
uint64_t key_left = util::SafeLoad(key_left_ptr + i);
uint64_t key_right = key_right_ptr[i];
uint64_t key_left = 0;
memcpy(&key_left, key_left_ptr + i, length - num_loops_less_one * 8);
Copy link
Member

Choose a reason for hiding this comment

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

length - num_loops_less_one * 8 is used several times in this closure, can you factor it out and give it a meaningful name (perhaps num_tail_bytes)?

But, actually, isn't length - num_loops_less_one * 8 simply equal to length?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

length - num_loops_less_one * 8 is used several times in this closure, can you factor it out and give it a meaningful name (perhaps num_tail_bytes)?

Yeah, that's reasonable. Done.

But, actually, isn't length - num_loops_less_one * 8 simply equal to length?

Sorry I don't quite understand how this is coming. For instance, for fsb(19) type, length will be 19 and length - num_loops_less_one * 8 (aka. num_tail_bytes) will be 3.

Copy link
Member

Choose a reason for hiding this comment

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

You're right, thank you.

uint64_t key_right = 0;
memcpy(&key_right, key_right_ptr + i, length - num_loops_less_one * 8);
result_or |= tail_mask & (key_left ^ key_right);
Copy link
Member

Choose a reason for hiding this comment

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

Is tail_mask still useful here? We're extracting exactly the desired number of bytes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch! Removed tail_mask.

return result_or == 0 ? 0xff : 0;
});
Expand Down
110 changes: 110 additions & 0 deletions cpp/src/arrow/compute/row/compare_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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.

#include <numeric>

#include "arrow/compute/row/compare_internal.h"
#include "arrow/testing/gtest_util.h"

namespace arrow {
namespace compute {

using arrow::bit_util::BytesForBits;
using arrow::internal::CpuInfo;
using arrow::util::MiniBatch;
using arrow::util::TempVectorStack;

// Specialized case for GH-39577.
TEST(KeyCompare, CompareColumnsToRowsCuriousFSB) {
int fsb_length = 9;
MemoryPool* pool = default_memory_pool();
TempVectorStack stack;
ASSERT_OK(stack.Init(pool, 8 * MiniBatch::kMiniBatchLength * sizeof(uint64_t)));

int num_rows = 7;
auto column_right = ArrayFromJSON(fixed_size_binary(fsb_length), R"([
"000000000",
"111111111",
"222222222",
"333333333",
"444444444",
"555555555",
"666666666"])");
ExecBatch batch_right({column_right}, num_rows);

std::vector<KeyColumnMetadata> column_metadatas_right;
ASSERT_OK(ColumnMetadatasFromExecBatch(batch_right, &column_metadatas_right));

RowTableMetadata table_metadata_right;
table_metadata_right.FromColumnMetadataVector(column_metadatas_right, sizeof(uint64_t),
sizeof(uint64_t));

std::vector<KeyColumnArray> column_arrays_right;
ASSERT_OK(ColumnArraysFromExecBatch(batch_right, &column_arrays_right));

RowTableImpl row_table;
ASSERT_OK(row_table.Init(pool, table_metadata_right));

RowTableEncoder row_encoder;
row_encoder.Init(column_metadatas_right, sizeof(uint64_t), sizeof(uint64_t));
row_encoder.PrepareEncodeSelected(0, num_rows, column_arrays_right);

std::vector<uint16_t> row_ids_right(num_rows);
std::iota(row_ids_right.begin(), row_ids_right.end(), 0);
ASSERT_OK(row_encoder.EncodeSelected(&row_table, num_rows, row_ids_right.data()));

auto column_left = ArrayFromJSON(fixed_size_binary(fsb_length), R"([
"000000000",
"111111111",
"222222222",
"333333333",
"444444444",
"555555555",
"777777777"])");
ExecBatch batch_left({column_left}, num_rows);
std::vector<KeyColumnArray> column_arrays_left;
ASSERT_OK(ColumnArraysFromExecBatch(batch_left, &column_arrays_left));

std::vector<uint32_t> row_ids_left(num_rows);
std::iota(row_ids_left.begin(), row_ids_left.end(), 0);

LightContext ctx{CpuInfo::GetInstance()->hardware_flags(), &stack};

{
uint32_t num_rows_no_match;
std::vector<uint16_t> row_ids_out(num_rows);
KeyCompare::CompareColumnsToRows(num_rows, NULLPTR, row_ids_left.data(), &ctx,
&num_rows_no_match, row_ids_out.data(),
column_arrays_left, row_table, true, NULLPTR);
ASSERT_EQ(num_rows_no_match, 1);
ASSERT_EQ(row_ids_out[0], 6);
}

{
std::vector<uint8_t> match_bitvector(BytesForBits(num_rows));
KeyCompare::CompareColumnsToRows(num_rows, NULLPTR, row_ids_left.data(), &ctx,
NULLPTR, NULLPTR, column_arrays_left, row_table,
true, match_bitvector.data());
for (int i = 0; i < num_rows; ++i) {
SCOPED_TRACE(i);
ASSERT_EQ(arrow::bit_util::GetBit(match_bitvector.data(), i), i != 6);
}
}
}

} // namespace compute
} // namespace arrow
Loading