From b98ffb1fbd02eeaac71ed4115e209cfae208acff Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 19 Oct 2026 19:16:51 -0600 Subject: [PATCH] [cache] Reject out-of-range keys on reads (#6419) * [cache] Reject out-of-range keys on reads An empty entry can match an out-of-range key when the packed cache entry leaves storage bits unused. In the 256-entry binary coverage cache this reports glyph IDs 32512-32767 as NOT_COVERED, skipping Context/ChainContext format 2 substitutions and positioning. Apply the same key range check in get() as set(). Add regression coverage for spare-bit and full-width entries across all three storage sizes, with and without atomics. The new cache test fails before the fix and passes afterward, also with HB_TINY. All 28 GSUB/GPOS Context/ChainContext boundary cases pass. meson test -C build: 272 passed, 1 skipped, 0 failed. Fixes: https://github.com/harfbuzz/harfbuzz/issues/6418 Assisted-by: OpenAI Codex * [cache] Combine cache miss checks into one branch Fold the key range check into the existing miss condition using bitwise operations. XOR the stored and queried tags so their mismatch can be combined directly with key overflow and the empty entry check. Use a scalar empty-entry constant rather than casting through item_t, which creates temporary atomic objects in GCC builds. The binary, mapping, and cmap lookup probes now each have one conditional branch with GCC 15 and Clang 21 at -O2 on x86-64, compared with two or three in the initial fix. Testing: meson test -C build: 272 passed, 1 skipped, 0 failed. All 28 GSUB/GPOS Context/ChainContext boundary cases pass. The cache regression also passes with Clang and with HB_TINY. Assisted-by: OpenAI Codex --- src/hb-cache.hh | 7 ++-- src/meson.build | 1 + src/test-cache.cc | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/test-cache.cc diff --git a/src/hb-cache.hh b/src/hb-cache.hh index 9ac4ab4c0..84df35ebd 100644 --- a/src/hb-cache.hh +++ b/src/hb-cache.hh @@ -94,8 +94,11 @@ struct hb_cache_t { unsigned int k = key & ((1u<> value_bits) != (key >> cache_bits)) + constexpr unsigned int empty = (1ULL << (8 * sizeof (item_t))) - 1; + /* Combine overflow, tag mismatch, and empty checks into one branch. */ + if ((key >> key_bits) | + ((v >> value_bits) ^ (key >> cache_bits)) | + ((key_bits + value_bits - cache_bits == 8 * sizeof (item_t)) && (v == empty))) return false; *value = v & ((1u< +static void +test_cache () +{ + hb_cache_t cache; + unsigned key_max = (1u << key_bits) - 1; + unsigned value = 42; + unsigned keys[] = {0, 1, 255, 256, key_max}; + unsigned overflow_keys[] = {key_max + 1, 32512, 32767, 65535, UINT_MAX}; + + for (unsigned key : keys) + hb_always_assert (!cache.get (key, &value)); + + for (unsigned key : overflow_keys) + if (key > key_max) + { + hb_always_assert (!cache.get (key, &value)); + hb_always_assert (value == 42); + } + + for (unsigned key : keys) + { + cache.set (key, 0); + hb_always_assert (cache.get (key, &value) && value == 0); + } + + cache.set (42, cache.MAX_VALUE); + hb_always_assert (cache.get (42, &value) && value == cache.MAX_VALUE); + + for (unsigned key : overflow_keys) + if (key > key_max) + { + cache.set (key, 0); + hb_always_assert (!cache.get (key, &value)); + } + + cache.set (42, cache.MAX_VALUE + 1); + hb_always_assert (cache.get (42, &value) && value == cache.MAX_VALUE); + + cache.clear (); + for (unsigned key : keys) + hb_always_assert (!cache.get (key, &value)); +} + +template +static void +test_caches () +{ + /* Entries with spare bits, and entries that fill the storage type. */ + test_cache<14, 1, 8, thread_safe> (); + test_cache<14, 1, 7, thread_safe> (); + test_cache<14, 8, 8, thread_safe> (); + test_cache<16, 8, 8, thread_safe> (); + test_cache<24, 8, 8, thread_safe> (); + test_cache<24, 16, 8, thread_safe> (); +} + +int +main (int argc, char **argv) +{ + test_caches (); + test_caches (); +}