diff --git a/cpp/src/arrow/compute/exec/key_map.cc b/cpp/src/arrow/compute/exec/key_map.cc index f06a090cf85..bff352e0155 100644 --- a/cpp/src/arrow/compute/exec/key_map.cc +++ b/cpp/src/arrow/compute/exec/key_map.cc @@ -510,7 +510,7 @@ void SwissTable::find(const int num_keys, const uint32_t* hashes, return; } - auto slot_ids_buf = util::TempVectorHolder(temp_stack_, num_ids); + auto slot_ids_buf = util::TempVectorHolder(temp_stack_, num_keys); uint32_t* slot_ids = slot_ids_buf.mutable_data(); init_slot_ids(num_ids, ids, hashes, local_slots, inout_match_bitvector, slot_ids); diff --git a/cpp/src/arrow/compute/exec/util.h b/cpp/src/arrow/compute/exec/util.h index ed89bece6a3..55f9a7e14c9 100644 --- a/cpp/src/arrow/compute/exec/util.h +++ b/cpp/src/arrow/compute/exec/util.h @@ -89,24 +89,35 @@ class TempVectorStack { // using SIMD when number of vector elements is not divisible // by the number of SIMD lanes. // - return ::arrow::BitUtil::RoundUp(num_bytes, sizeof(int64_t)) + padding; + return ::arrow::BitUtil::RoundUp(num_bytes, sizeof(int64_t)) + kPadding; } void alloc(uint32_t num_bytes, uint8_t** data, int* id) { int64_t old_top = top_; - top_ += PaddedAllocationSize(num_bytes); + top_ += PaddedAllocationSize(num_bytes) + 2 * sizeof(uint64_t); // Stack overflow check ARROW_DCHECK(top_ <= buffer_size_); - *data = buffer_->mutable_data() + old_top; + *data = buffer_->mutable_data() + old_top + sizeof(uint64_t); + // We set 8 bytes before the beginning of the allocated range and + // 8 bytes after the end to check for stack overflow (which would + // result in those known bytes being corrupted). + reinterpret_cast(buffer_->mutable_data() + old_top)[0] = kGuard1; + reinterpret_cast(buffer_->mutable_data() + top_)[-1] = kGuard2; *id = num_vectors_++; } void release(int id, uint32_t num_bytes) { ARROW_DCHECK(num_vectors_ == id + 1); - int64_t size = PaddedAllocationSize(num_bytes); + int64_t size = PaddedAllocationSize(num_bytes) + 2 * sizeof(uint64_t); + ARROW_DCHECK(reinterpret_cast(buffer_->mutable_data() + top_)[-1] == + kGuard2); ARROW_DCHECK(top_ >= size); top_ -= size; + ARROW_DCHECK(reinterpret_cast(buffer_->mutable_data() + top_)[0] == + kGuard1); --num_vectors_; } - static constexpr int64_t padding = 64; + static constexpr uint64_t kGuard1 = 0x3141592653589793ULL; + static constexpr uint64_t kGuard2 = 0x0577215664901532ULL; + static constexpr int64_t kPadding = 64; int num_vectors_; int64_t top_; std::unique_ptr buffer_;