github-actions[bot] commented on code in PR #69257: URL: https://github.com/apache/doris/pull/69257#discussion_r4322892676
########## be/src/exprs/datasketches_hll_util.cpp: ########## @@ -0,0 +1,79 @@ +// 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 "exprs/datasketches_hll_util.h" + +#include <cstring> +#include <exception> +#include <stdexcept> + +#include "common/exception.h" +#include "common/status.h" + +namespace doris::datasketches_hll { + +Sketch deserialize(const StringRef& value, std::string_view action) { + // The library reads the preamble byte before it checks the length. + if (value.size == 0) { + throw Exception(ErrorCode::CORRUPTION, "HLL sketch data corrupted when {}: empty input.", + action); + } + try { + // All modes have an eight-byte preamble. An invalid lgK would otherwise reach shifts used + // to size the register/coupon arrays before the library validates it. + datasketches::HllUtil<Alloc>::checkMemSize( + datasketches::hll_constants::EMPTY_SKETCH_SIZE_BYTES, value.size); + datasketches::HllUtil<Alloc>::checkLgK( + static_cast<uint8_t>(value.data[datasketches::hll_constants::LG_K_BYTE])); Review Comment: [P2] Bound serialized array exponents before library deserialization The precheck validates `LG_K_BYTE` but leaves `LG_ARR_BYTE` unrestricted. A noncompact SET image with valid lgK=8 and a complete 12-byte preamble can therefore pass byte 4 = 32 to the pinned [`CouponHashSet::newSet`](https://github.com/apache/datasketches-cpp/blob/46025e9aeed8368b1184cbde9634dd99d0ee47c0/hll/include/CouponHashSet-internal.hpp#L87-L101), which evaluates `1 << lgArrInts` before checking the payload length. This is an undefined shift of a 32-bit int. Updatable HLL_4 images with a nonzero auxiliary count have the same unchecked shift in [`AuxHashMap::deserialize`](https://github.com/apache/datasketches-cpp/blob/46025e9aeed8368b1184cbde9634dd99d0ee47c0/hll/include/AuxHashMap-internal.hpp#L75-L87). Thus the new scalar invokes native undefined behavior on corrupted size metadata before it can reliably report CORRUPTION. Validate the mode-specific array exponent and corresponding payload size before calling the library, and add negative cases for both paths. ########## be/src/exprs/datasketches_hll_util.cpp: ########## @@ -0,0 +1,79 @@ +// 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 "exprs/datasketches_hll_util.h" + +#include <cstring> +#include <exception> +#include <stdexcept> + +#include "common/exception.h" +#include "common/status.h" + +namespace doris::datasketches_hll { + +Sketch deserialize(const StringRef& value, std::string_view action) { + // The library reads the preamble byte before it checks the length. + if (value.size == 0) { + throw Exception(ErrorCode::CORRUPTION, "HLL sketch data corrupted when {}: empty input.", + action); + } + try { + // All modes have an eight-byte preamble. An invalid lgK would otherwise reach shifts used + // to size the register/coupon arrays before the library validates it. + datasketches::HllUtil<Alloc>::checkMemSize( + datasketches::hll_constants::EMPTY_SKETCH_SIZE_BYTES, value.size); + datasketches::HllUtil<Alloc>::checkLgK( + static_cast<uint8_t>(value.data[datasketches::hll_constants::LG_K_BYTE])); + // HLL_4 uses nibble 0xf to reference its auxiliary map. The library accepts an image + // with auxCount=0 and such a nibble, then dereferences a null map during union/conversion. + // This occurs, for example, when UTF-8 replacement bytes shift a binary sketch's fields. + // Validate this structural invariant before exposing the sketch, including on a no-op. + using namespace datasketches::hll_constants; + const auto* bytes = reinterpret_cast<const uint8_t*>(value.data); + const bool is_hll4 = bytes[PREAMBLE_INTS_BYTE] == HLL_PREINTS && + (bytes[MODE_BYTE] & loNibbleMask) == datasketches::hll_mode::HLL; + if (is_hll4) { + const size_t register_bytes = size_t {1} << (bytes[LG_K_BYTE] - 1); + datasketches::HllUtil<Alloc>::checkMemSize(HLL_BYTE_ARR_START + register_bytes, + value.size); + uint32_t aux_count = 0; + std::memcpy(&aux_count, bytes + AUX_COUNT_INT, sizeof(aux_count)); + if (aux_count == 0) { Review Comment: [P2] Prevent auxiliary-map leaks on malformed HLL_4 input For `aux_count > 0`, a complete HLL_4 auxiliary payload containing two entries for the same slot passes this guard. The pinned [`AuxHashMap::deserialize`](https://github.com/apache/datasketches-cpp/blob/46025e9aeed8368b1184cbde9634dd99d0ee47c0/hll/include/AuxHashMap-internal.hpp#L64-L95) allocates a raw map and entry vector, then `mustAdd` throws on the duplicate. Ownership in `HllArray::newHll` is established only after that call returns, so the exception leaves both allocations unreachable. Converting it to CORRUPTION here does not free them; repeated failed scalar evaluations accumulate leaked BE heap memory. Make auxiliary parsing exception-safe, or reject duplicate auxiliary slots before entering the library, and verify allocator balance on this error path. This is independent of rejecting auxiliary maps on HLL_6/HLL_8, since HLL_4 legitimately has such maps. ########## be/src/exprs/datasketches_hll_util.cpp: ########## @@ -0,0 +1,79 @@ +// 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 "exprs/datasketches_hll_util.h" + +#include <cstring> +#include <exception> +#include <stdexcept> + +#include "common/exception.h" +#include "common/status.h" + +namespace doris::datasketches_hll { + +Sketch deserialize(const StringRef& value, std::string_view action) { + // The library reads the preamble byte before it checks the length. + if (value.size == 0) { + throw Exception(ErrorCode::CORRUPTION, "HLL sketch data corrupted when {}: empty input.", + action); + } + try { + // All modes have an eight-byte preamble. An invalid lgK would otherwise reach shifts used + // to size the register/coupon arrays before the library validates it. + datasketches::HllUtil<Alloc>::checkMemSize( + datasketches::hll_constants::EMPTY_SKETCH_SIZE_BYTES, value.size); + datasketches::HllUtil<Alloc>::checkLgK( + static_cast<uint8_t>(value.data[datasketches::hll_constants::LG_K_BYTE])); + // HLL_4 uses nibble 0xf to reference its auxiliary map. The library accepts an image + // with auxCount=0 and such a nibble, then dereferences a null map during union/conversion. + // This occurs, for example, when UTF-8 replacement bytes shift a binary sketch's fields. + // Validate this structural invariant before exposing the sketch, including on a no-op. + using namespace datasketches::hll_constants; + const auto* bytes = reinterpret_cast<const uint8_t*>(value.data); + const bool is_hll4 = bytes[PREAMBLE_INTS_BYTE] == HLL_PREINTS && + (bytes[MODE_BYTE] & loNibbleMask) == datasketches::hll_mode::HLL; + if (is_hll4) { + const size_t register_bytes = size_t {1} << (bytes[LG_K_BYTE] - 1); + datasketches::HllUtil<Alloc>::checkMemSize(HLL_BYTE_ARR_START + register_bytes, + value.size); + uint32_t aux_count = 0; + std::memcpy(&aux_count, bytes + AUX_COUNT_INT, sizeof(aux_count)); + if (aux_count == 0) { + for (size_t i = HLL_BYTE_ARR_START; i < HLL_BYTE_ARR_START + register_bytes; ++i) { + if ((bytes[i] & loNibbleMask) == AUX_TOKEN || + ((bytes[i] >> 4) & loNibbleMask) == AUX_TOKEN) { + throw std::invalid_argument( + "HLL_4 contains AUX_TOKEN without an auxiliary map"); + } + } + } + } + return Sketch::deserialize(value.data, value.size, Alloc()); Review Comment: [P1] Reject auxiliary maps on HLL_6 and HLL_8 before deserializing A malformed dense HLL_6/HLL_8 image with a nonzero `AUX_COUNT_INT` and a complete auxiliary-map payload bypasses the `is_hll4` checks and reaches this call. In the pinned DataSketches revision, [`HllArray::newHll`](https://github.com/apache/datasketches-cpp/blob/46025e9aeed8368b1184cbde9634dd99d0ee47c0/hll/include/HllArray-internal.hpp#L131-L158) loads that map, allocates the object for the encoded target type, then unconditionally casts it to `Hll4Array` and calls `putAuxHashMap`. HLL_6/HLL_8 objects have no such member, so this writes outside their allocation instead of returning CORRUPTION. The new scalar reaches this even when the requested precision/type would otherwise return the original bytes. Reject nonzero auxiliary counts for dense HLL_6/HLL_8 before invoking the library and cover both types in the malformed-input tests. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
