Files
harfbuzz/src/test-algs.cc
T
Behdad EsfahbodandClaude Opus 4.8 e550098147 [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>
2026-10-30 16:35:27 -06:00

224 lines
7.2 KiB
C++

/*
* Copyright © 2019 Facebook, 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.
*
* Facebook Author(s): Behdad Esfahbod
*/
#include "hb.hh"
#include "hb-algs.hh"
#include "hb-set.hh"
static char *
test_func (int a, char **b)
{
return b ? b[a] : nullptr;
}
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)
{
hb_always_assert (roundf (0.5f) == 1.f);
hb_always_assert (roundf (-0.5f) == 0.f);
hb_always_assert (roundf (1.5f) == 2.f);
hb_always_assert (roundf (-1.5f) == -1.f);
hb_always_assert (roundf (nextafterf (0.5f, 0.f)) == 0.f);
hb_always_assert (roundf (nextafterf (-0.5f, -1.f)) == -1.f);
hb_always_assert (roundf (nextafterf (1.5f, 1.f)) == 1.f);
hb_always_assert (roundf (nextafterf (-1.5f, -2.f)) == -2.f);
hb_always_assert (roundf (15383929.f) == 15383929.f);
hb_always_assert (roundf (-15383929.f) == -15383929.f);
hb_always_assert (roundf (8388609.f) == 8388609.f);
hb_always_assert (roundf (-8388609.f) == -8388609.f);
int i = 1;
auto p = hb_pair (1, i);
p.second = 2;
hb_always_assert (i == 2);
const int c = 3;
auto pc = hb_pair (1, c);
hb_always_assert (pc.second == 3);
auto q = p;
hb_always_assert (&q != &p);
q.second = 4;
hb_always_assert (i == 4);
hb_invoke (test_func, 0, nullptr);
A a;
hb_invoke (&A::a, a);
hb_always_assert (1 == hb_min (8, 1));
hb_always_assert (8 == hb_max (8, 1));
int x = 1, y = 2;
hb_min (x, 3);
hb_min (3, x);
hb_min (x, 4 + 3);
int &z = hb_min (x, y);
z = 3;
hb_always_assert (x == 3);
hb_pair_t<const int*, int> xp = hb_pair_t<int *, long> (nullptr, 0);
xp = hb_pair_t<int *, double> (nullptr, 1);
xp = hb_pair_t<const int*, int> (nullptr, 1);
hb_always_assert (3 == hb_partial (hb_min, 3) (4));
hb_always_assert (3 == hb_partial<1> (hb_min, 4) (3));
auto M0 = hb_partial<2> (hb_max, 0);
hb_always_assert (M0 (-2) == 0);
hb_always_assert (M0 (+2) == 2);
hb_always_assert (hb_add (2) (5) == 7);
hb_always_assert (hb_add (5) (2) == 7);
hb_always_assert (hb_saturate_add (1, 2) == 3);
hb_always_assert (hb_saturate_add (INT_MAX, 1) == INT_MAX);
hb_always_assert (hb_saturate_add (INT_MIN, -1) == INT_MIN);
hb_always_assert (hb_saturate_add (INT_MAX, INT_MIN) == -1);
hb_always_assert (hb_saturate_sub (3, 2) == 1);
hb_always_assert (hb_saturate_sub (INT_MAX, -1) == INT_MAX);
hb_always_assert (hb_saturate_sub (INT_MIN, 1) == INT_MIN);
hb_always_assert (hb_saturate_sub (INT_MIN, INT_MIN) == 0);
hb_always_assert (hb_saturate_neg (2) == -2);
hb_always_assert (hb_saturate_neg (-2) == 2);
hb_always_assert (hb_saturate_neg (INT_MIN) == INT_MAX);
hb_always_assert (hb_clamp_to<int> (1.) == 1);
hb_always_assert (hb_clamp_to<int> ((double) INT_MAX + 1.) == INT_MAX);
hb_always_assert (hb_clamp_to<int> ((double) INT_MIN - 1.) == INT_MIN);
hb_always_assert (hb_clamp_to<int> ((int64_t) 1) == 1);
hb_always_assert (hb_clamp_to<int> ((int64_t) INT_MAX + 1) == INT_MAX);
hb_always_assert (hb_clamp_to<int> ((int64_t) INT_MIN - 1) == INT_MIN);
int64_t budget = 10;
hb_always_assert (hb_budget_spend (budget, 4, 2));
hb_always_assert (budget == 2);
hb_always_assert (!hb_budget_spend (budget, 3));
hb_always_assert (budget == -1);
hb_always_assert (!hb_budget_spend (budget, 1));
hb_always_assert (budget == -2);
budget = 0;
hb_always_assert (hb_budget_spend (budget, 0));
hb_always_assert (!hb_budget_spend (budget, 1));
hb_always_assert (budget == -1);
budget = INT64_MAX;
hb_always_assert (hb_budget_spend (budget, 1024));
hb_always_assert (budget == INT64_MAX - 1024);
x = 1;
hb_always_assert (++hb_inc (x) == 3);
hb_always_assert (x == 3);
hb_set_t set1 {1};
hb_set_t set2 {2};
hb_always_assert (hb_hash (set1) != hb_hash (set2));
hb_always_assert (hb_hash (set1) == hb_hash (hb_set_t {1}));
hb_always_assert (hb_hash (set1) != hb_hash (hb_set_t {}));
hb_always_assert (hb_hash (set1) != hb_hash (hb_set_t {2}));
hb_always_assert (hb_hash (set2) == hb_hash (hb_set_t {2}));
/* hb_hash, unlike std::hash, dereferences pointers. */
hb_always_assert (hb_hash (set1) == hb_hash (&set1));
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;
}