mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-29 01:23:56 +01:00
[algs] Give hb_qsort_inline an introsort depth limit and heapsort fallback
hb_qsort_loop() is a median-of-three quicksort with no recursion-depth bound, so an input order chosen against its pivot selection (McIlroy's adversary) costs about n^2/4 comparisons. Several arrays it sorts come in a font-chosen order: the lookup list that hb_ot_map_builder_t::compile() sorts for every shape plan, COLRv1 color stops, and the post/CFF glyph-name indexes. A 262 KB font makes hb-shape spend ~5 s per shape plan, against ~0.09 s for an otherwise identical font. Restore the invariant std::sort has: hb_qsort_inline() starts a depth limit of 2*log2(n), hb_qsort_loop() decrements it per partition, and when it reaches zero it hands the remaining range to a new hb_heapsort_inline(), the shape of libstdc++'s __introsort_loop. Every typed-comparator call site reaches this through hb_array_t::qsort(), so the one change covers all of them; the void* overload still forwards to libc qsort. Ordinary inputs never reach the fallback and the sort stays non-stable as documented. A regression test in src/test-algs.cc runs McIlroy's adversary against hb_qsort_inline() and asserts the comparison count stays below 16*n*log2(n); it aborts on the unpatched sort. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-K90M0RY4. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
507575fd82
commit
e550098147
2 files changed
+114
-5
No files matched your search
+49
-5
@@ -1462,6 +1462,39 @@ hb_bsearch (const K& key, V* base,
|
||||
}
|
||||
|
||||
|
||||
/* Heapsort: O(n log n) worst case. Used by hb_qsort_loop below
|
||||
* as the fallback when quicksort partitioning degenerates. */
|
||||
template <typename T, typename Compar>
|
||||
static inline void
|
||||
hb_heapsort_sift_down (T *base, size_t root, size_t end, Compar compar)
|
||||
{
|
||||
for (size_t child; (child = 2 * root + 1) < end; root = child)
|
||||
{
|
||||
if (child + 1 < end && compar (base[child], base[child + 1]) < 0)
|
||||
child++;
|
||||
if (compar (base[root], base[child]) >= 0)
|
||||
break;
|
||||
hb_swap (base[root], base[child]);
|
||||
}
|
||||
}
|
||||
template <typename T, typename Compar>
|
||||
static inline void
|
||||
hb_heapsort_inline (T *base, size_t nel, Compar compar)
|
||||
{
|
||||
if (nel < 2) return;
|
||||
|
||||
/* Build a max-heap. */
|
||||
for (size_t i = nel / 2; i--;)
|
||||
hb_heapsort_sift_down (base, i, nel, compar);
|
||||
|
||||
/* Pop the maximum to the end, one at a time. */
|
||||
for (size_t end = nel; --end;)
|
||||
{
|
||||
hb_swap (base[0], base[end]);
|
||||
hb_heapsort_sift_down (base, 0, end, compar);
|
||||
}
|
||||
}
|
||||
|
||||
/* Quicksort partitioning loop: median-of-three pivot, two-way
|
||||
* Hoare partition, tail-call elimination on the larger side.
|
||||
* Stops partitioning when subranges shrink below the threshold;
|
||||
@@ -1475,15 +1508,26 @@ hb_bsearch (const K& key, V* base,
|
||||
* Stops partitioning when subranges shrink below the threshold;
|
||||
* a single insertion-sort pass over the whole array (run by
|
||||
* hb_qsort_inline below) finishes the job. Same shape libstdc++
|
||||
* uses for std::sort.
|
||||
* uses for std::sort, including the introsort depth limit: once
|
||||
* a range has been partitioned depth_limit times without
|
||||
* finishing, it is handed to heapsort instead. Without that an
|
||||
* input ordered against the pivot selection (which a font can
|
||||
* arrange, e.g. the lookup indices of a Feature) costs O(n^2).
|
||||
*
|
||||
* Not stable; equivalent values may be swapped. */
|
||||
template <typename T, typename Compar>
|
||||
static inline void
|
||||
hb_qsort_loop (T *base, size_t nel, Compar compar)
|
||||
hb_qsort_loop (T *base, size_t nel, Compar compar, unsigned depth_limit)
|
||||
{
|
||||
while (nel > 24)
|
||||
{
|
||||
if (unlikely (!depth_limit))
|
||||
{
|
||||
hb_heapsort_inline (base, nel, compar);
|
||||
return;
|
||||
}
|
||||
depth_limit--;
|
||||
|
||||
T *last = base + nel - 1;
|
||||
T *mid = base + nel / 2;
|
||||
|
||||
@@ -1516,13 +1560,13 @@ hb_qsort_loop (T *base, size_t nel, Compar compar)
|
||||
size_t right = nel - left - 1;
|
||||
if (left < right)
|
||||
{
|
||||
hb_qsort_loop (base, left, compar);
|
||||
hb_qsort_loop (base, left, compar, depth_limit);
|
||||
base = i + 1;
|
||||
nel = right;
|
||||
}
|
||||
else
|
||||
{
|
||||
hb_qsort_loop (i + 1, right, compar);
|
||||
hb_qsort_loop (i + 1, right, compar, depth_limit);
|
||||
nel = left;
|
||||
}
|
||||
}
|
||||
@@ -1532,7 +1576,7 @@ template <typename T, typename Compar>
|
||||
static inline void
|
||||
hb_qsort_inline (T *base, size_t nel, Compar compar)
|
||||
{
|
||||
hb_qsort_loop (base, nel, compar);
|
||||
hb_qsort_loop (base, nel, compar, 2 * hb_bit_storage (nel));
|
||||
|
||||
/* Single final insertion sort over the whole array. After
|
||||
* the partitioning loop, every element is within the threshold
|
||||
|
||||
@@ -40,6 +40,69 @@ struct A
|
||||
void a () {}
|
||||
};
|
||||
|
||||
/* McIlroy's "killer adversary" for quicksort: hands out keys lazily,
|
||||
* always giving the sort's current pivot candidate the smallest key
|
||||
* left, so every partition splits off almost nothing. Drives any
|
||||
* deterministic quicksort without a depth limit to O(n^2). */
|
||||
struct qsort_adversary_t
|
||||
{
|
||||
qsort_adversary_t (unsigned n) : n (n)
|
||||
{
|
||||
keys.resize (n);
|
||||
for (unsigned i = 0; i < n; i++)
|
||||
keys[i] = n; /* Unassigned; compares greater than any key. */
|
||||
}
|
||||
|
||||
int operator () (const unsigned &a, const unsigned &b)
|
||||
{
|
||||
if (keys[a] == n && keys[b] == n)
|
||||
keys[a == candidate ? a : b] = next++;
|
||||
if (keys[a] == n) candidate = a;
|
||||
else if (keys[b] == n) candidate = b;
|
||||
return (int) keys[a] - (int) keys[b];
|
||||
}
|
||||
|
||||
unsigned n;
|
||||
hb_vector_t<unsigned> keys;
|
||||
unsigned candidate = 0;
|
||||
unsigned next = 0;
|
||||
};
|
||||
|
||||
static void
|
||||
test_qsort_adversary ()
|
||||
{
|
||||
const unsigned n = 1u << 15;
|
||||
hb_vector_t<unsigned> items;
|
||||
items.resize (n);
|
||||
for (unsigned i = 0; i < n; i++)
|
||||
items[i] = i;
|
||||
|
||||
/* First run: let the adversary pick the worst order for our sort. */
|
||||
qsort_adversary_t adversary (n);
|
||||
hb_qsort_inline (items.arrayZ, items.length,
|
||||
[&] (const unsigned &a, const unsigned &b) { return adversary (a, b); });
|
||||
for (unsigned i = 0; i < n; i++)
|
||||
if (adversary.keys[i] == n)
|
||||
adversary.keys[i] = adversary.next++;
|
||||
|
||||
/* Second run: the same input, now with the keys the adversary chose. */
|
||||
for (unsigned i = 0; i < n; i++)
|
||||
items[i] = i;
|
||||
size_t comparisons = 0;
|
||||
hb_qsort_inline (items.arrayZ, items.length,
|
||||
[&] (const unsigned &a, const unsigned &b)
|
||||
{
|
||||
comparisons++;
|
||||
return (int) adversary.keys[a] - (int) adversary.keys[b];
|
||||
});
|
||||
for (unsigned i = 1; i < n; i++)
|
||||
hb_always_assert (adversary.keys[items[i - 1]] < adversary.keys[items[i]]);
|
||||
|
||||
/* Introsort: stays within a small multiple of n log n; a plain
|
||||
* quicksort would take about n^2 / 4 comparisons here. */
|
||||
hb_always_assert (comparisons < 16 * (size_t) n * hb_bit_storage (n));
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -154,5 +217,7 @@ main (int argc, char **argv)
|
||||
hb_always_assert (hb_hash (set1) == hb_hash (hb::shared_ptr<hb_set_t> {hb_set_reference (&set1)}));
|
||||
hb_always_assert (hb_hash (set1) == hb_hash (hb::unique_ptr<hb_set_t> {hb_set_reference (&set1)}));
|
||||
|
||||
test_qsort_adversary ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user