Skip to content
Open
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
12 changes: 12 additions & 0 deletions tuple/include/tuple_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,18 @@ class compact_tuple_sketch: public tuple_sketch<Summary, Allocator> {

virtual void print_specifics(std::ostringstream& os) const;

// The MASK selects which bits of the first 8 bytes of an empty serial version 3 image are examined.
// The TEST gives the required values of the examined bits; it must lie within the MASK.
// Examined: preamble longs 1, serial version 3, family 9, sketch type 1, byte 4 = 0 (unused);
// flags read-only, empty, compact set; big-endian and reserved bits 5, 6, 7 clear.
// The ordered flag is ignored: older Java versions wrote empty images without it (flags 0x0e).
// The seed hash is ignored: it may be 0 or the seed hash, depending on the writer.
// These must stay the same as in Java CompactTupleSketch.
static const uint64_t EMPTY_SKETCH_MASK = 0x0000EFFFFFFFFFFFULL;
static const uint64_t EMPTY_SKETCH_TEST = 0x00000E0001090301ULL;
static_assert((EMPTY_SKETCH_TEST & ~EMPTY_SKETCH_MASK) == 0, "EMPTY_SKETCH_TEST must lie within EMPTY_SKETCH_MASK");
static void check_empty_v3(uint64_t pre0);

template<typename E, typename EK, typename P, typename S, typename CS, typename A> friend class theta_union_base;
template<typename E, typename EK, typename P, typename S, typename CS, typename A> friend class theta_intersection_base;
template<typename E, typename EK, typename CS, typename A> friend class theta_set_difference_base;
Expand Down
23 changes: 22 additions & 1 deletion tuple/include/tuple_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ compact_tuple_sketch<S, A> compact_tuple_sketch<S, A>::deserialize(std::istream&
const auto serial_version = read<uint8_t>(is);
const auto family = read<uint8_t>(is);
const auto type = read<uint8_t>(is);
read<uint8_t>(is); // unused
const auto unused = read<uint8_t>(is);
const auto flags_byte = read<uint8_t>(is);
const auto seed_hash = read<uint16_t>(is);
if (serial_version != SERIAL_VERSION && serial_version != SERIAL_VERSION_LEGACY) {
Expand All @@ -496,6 +496,12 @@ compact_tuple_sketch<S, A> compact_tuple_sketch<S, A>::deserialize(std::istream&
}
const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
if (!is_empty) checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
if (is_empty && serial_version == SERIAL_VERSION) {
check_empty_v3(static_cast<uint64_t>(preamble_longs) | static_cast<uint64_t>(serial_version) << 8
| static_cast<uint64_t>(family) << 16 | static_cast<uint64_t>(type) << 24
| static_cast<uint64_t>(unused) << 32 | static_cast<uint64_t>(flags_byte) << 40
| static_cast<uint64_t>(seed_hash) << 48);
}

uint64_t theta = theta_constants::MAX_THETA;
uint32_t num_entries = 0;
Expand Down Expand Up @@ -557,6 +563,11 @@ compact_tuple_sketch<S, A> compact_tuple_sketch<S, A>::deserialize(const void* b
}
const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
if (!is_empty) checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
if (is_empty && serial_version == SERIAL_VERSION) {
uint64_t pre0;
copy_from_mem(base, pre0);
check_empty_v3(pre0);
}

uint64_t theta = theta_constants::MAX_THETA;
uint32_t num_entries = 0;
Expand Down Expand Up @@ -594,6 +605,16 @@ compact_tuple_sketch<S, A> compact_tuple_sketch<S, A>::deserialize(const void* b
return compact_tuple_sketch(is_empty, is_ordered, seed_hash, theta, std::move(entries));
}

template<typename S, typename A>
void compact_tuple_sketch<S, A>::check_empty_v3(uint64_t pre0) {
const uint64_t masked = pre0 & EMPTY_SKETCH_MASK;
if (masked != EMPTY_SKETCH_TEST) {
std::stringstream s;
s << "empty compact tuple sketch preamble does not match: 0x" << std::hex << masked;
throw std::invalid_argument(s.str());
}
}

template<typename S, typename A>
auto compact_tuple_sketch<S, A>::begin() -> iterator {
return iterator(entries_.data(), static_cast<uint32_t>(entries_.size()), 0);
Expand Down
63 changes: 63 additions & 0 deletions tuple/test/tuple_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,69 @@ TEST_CASE("tuple sketch float: empty", "[tuple_sketch]") {
REQUIRE(update_sketch.compact(false).is_ordered());
}

TEST_CASE("tuple sketch: deserialize checks empty images strictly", "[tuple_sketch]") {
using sketch_type = compact_tuple_sketch<float>;
using vector_bytes = sketch_type::vector_bytes;
const size_t serial_version_byte = 1;
const size_t type_byte = 3;
const size_t unused_byte = 4;
const size_t flags_byte = 5;
const size_t seed_hash_offset = 6;
const vector_bytes empty = update_tuple_sketch<float>::builder().build().compact().serialize();
REQUIRE(empty.size() == 8);
REQUIRE(empty[flags_byte] == 0x1e);

auto deserialize_both = [](const vector_bytes& bytes) {
std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
REQUIRE(sketch_type::deserialize(s).is_empty());
REQUIRE(sketch_type::deserialize(bytes.data(), bytes.size()).is_empty());
};
auto check_rejected = [](const vector_bytes& bytes) {
REQUIRE_THROWS_AS(sketch_type::deserialize(bytes.data(), bytes.size()), std::invalid_argument);
std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
REQUIRE_THROWS_AS(sketch_type::deserialize(s), std::invalid_argument);
};

SECTION("accepted") {
deserialize_both(empty);
// without the ordered flag, as older Java versions wrote
auto bytes = empty;
bytes[flags_byte] = 0x0e;
deserialize_both(bytes);
// any seed hash
bytes = empty;
bytes[seed_hash_offset] = 0xcc;
bytes[seed_hash_offset + 1] = 0x93;
deserialize_both(bytes);
// serial version 1 images are not checked
bytes = empty;
bytes[serial_version_byte] = 1;
bytes[unused_byte] = 1;
deserialize_both(bytes);
}

SECTION("rejected") {
auto bytes = empty;
bytes[0] = 2; // preamble longs
check_rejected(bytes);
bytes = empty;
bytes[type_byte] = 5; // legacy sketch type
check_rejected(bytes);
bytes = empty;
bytes[unused_byte] = 1;
check_rejected(bytes);
// read-only or compact flag missing, big-endian or a reserved flag set
for (uint8_t flags: {0x1c, 0x16, 0x1f, 0x3e, 0x5e, 0x9e}) {
CAPTURE(static_cast<int>(flags));
bytes = empty;
bytes[flags_byte] = flags;
check_rejected(bytes);
}
}
}

TEST_CASE("tuple sketch: single item", "[tuple_sketch]") {
auto update_sketch = update_tuple_sketch<float>::builder().build();
update_sketch.update(1, 1.0f);
Expand Down
Loading