-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But, actually, isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, that's reasonable. Done.
Sorry I don't quite understand how this is coming. For instance, for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Removed |
||
return result_or == 0 ? 0xff : 0; | ||
}); | ||
|
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 |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Done.