mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-19 14:01:10 +01:00
[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
This commit is contained in:
1 parent
8c59a6d47a
commit
b98ffb1fbd
3 files changed
+95
-2
No files matched your search
+5
-2
@@ -94,8 +94,11 @@ struct hb_cache_t
|
||||
{
|
||||
unsigned int k = key & ((1u<<cache_bits)-1);
|
||||
unsigned int v = values[k];
|
||||
if ((key_bits + value_bits - cache_bits == 8 * sizeof (item_t) && (item_t) v == (item_t) -1) ||
|
||||
(v >> 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<<value_bits)-1);
|
||||
return true;
|
||||
|
||||
@@ -991,6 +991,7 @@ if get_option('tests').enabled()
|
||||
'test-algs': ['test-algs.cc', 'hb-static.cc'],
|
||||
'test-array': ['test-array.cc'],
|
||||
'test-bimap': ['test-bimap.cc', 'hb-static.cc'],
|
||||
'test-cache': ['test-cache.cc'],
|
||||
'test-cff': ['test-cff.cc', 'hb-static.cc'],
|
||||
'test-classdef-graph': ['graph/test-classdef-graph.cc', 'hb-static.cc', 'graph/gsubgpos-context.cc'],
|
||||
'test-result': ['test-result.cc', 'hb-static.cc'],
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright © 2026 Google, Inc.
|
||||
*
|
||||
* This is part of HarfBuzz, a text shaping library.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and without
|
||||
* license or royalty fees, to use, copy, modify, and distribute this
|
||||
* software and its documentation for any purpose, provided that the
|
||||
* above copyright notice and the following two paragraphs appear in
|
||||
* all copies of this software.
|
||||
*
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
||||
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
||||
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
||||
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
* DAMAGE.
|
||||
*
|
||||
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
||||
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
||||
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*/
|
||||
|
||||
#include "hb-cache.hh"
|
||||
|
||||
template <unsigned key_bits, unsigned value_bits, unsigned cache_bits, bool thread_safe>
|
||||
static void
|
||||
test_cache ()
|
||||
{
|
||||
hb_cache_t<key_bits, value_bits, cache_bits, thread_safe> 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 <bool thread_safe>
|
||||
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<true> ();
|
||||
test_caches<false> ();
|
||||
}
|
||||
Reference in new issue
Block a user