Files
harfbuzz/src/test-vector.cc
T
Behdad Esfahbod e8a578bf07 [build] Avoid libstdc++ assertions in unoptimized builds (#6534)
* [build] Avoid libstdc++ assertions in unoptimized builds

Recent libstdc++ headers enable assertions at -O0. Atomic operations
then reference std::__glibcxx_assert_fail, which is unavailable when
HarfBuzz uses the C linker without libstdc++.

Define _GLIBCXX_NO_ASSERTIONS in Meson and CMake builds. Undefine
_GLIBCXX_ASSERTIONS in Meson as well, since Meson enables it for debug
builds.

Verified Clang 21.1.8 with libstdc++ 15.3.1 debug shared-library builds
in both build systems, using --no-undefined with CMake. Confirmed that
harfbuzz and harfbuzz-gpu have no assertion reference or libstdc++
dependency. The full Meson suite passed: 295 tests, one skipped.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6528
Assisted-by: Codex

* [build] Configure libstdc++ assertions in hb.hh

Move the assertion settings from Meson and CMake to the internal header
so custom builds and amalgamations also avoid the runtime dependency.

The settings must precede the first C++ standard header. hb-atomic.hh
is included after other standard headers, so set them at the top of
hb.hh instead.

Verified Clang debug shared-library builds with Meson and CMake, plus a
direct -O0 build of harfbuzz.cc with pthread mutexes and assertions
explicitly enabled on the command line. All link without libstdc++ and
have no assertion reference. The GCC atomic repro also passes. The full
Meson suite passed: 295 tests, one skipped.

Assisted-by: Codex

* [test] Keep vector string operations instrumented under MSan

Including hb.hh first disables libstdc++ assertions and restores its
extern template declarations for std::string. The vector test then
calls the uninstrumented copy constructor from libstdc++, and MSan
reports uninitialized state when moving the temporary copy.

Include string before hb.hh so the test retains the inline string
implementations that MSan can instrument.

Reproduced with Clang 18 and libstdc++ 14 using CI's assertion flags.
The original test fails and the updated test passes under MSan. The
full regular Meson suite passed: 295 tests, one skipped.

Assisted-by: Codex
2026-10-31 20:10:00 -06:00

336 lines
9.1 KiB
C++

/*
* Copyright © 2021 Behdad Esfahbod
*
* 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 before hb.hh so libstdc++'s inline string implementations remain
* available for MemorySanitizer instrumentation. */
#include <string>
#define hb_realloc vector_test_realloc
#include "hb.hh"
#include "hb-vector.hh"
#include "hb-alloc-pool.hh"
#include "hb-set.hh"
#include "hb-map.hh"
static size_t allocation_limit = SIZE_MAX;
static size_t last_reallocation;
extern "C" void *
vector_test_realloc (void *ptr, size_t size)
{
last_reallocation = size;
return size > allocation_limit ? nullptr : realloc (ptr, size);
}
int
main (int argc, char **argv)
{
hb_always_assert (sizeof (hb_vector_t<int>) == sizeof (hb_sorted_vector_t<int>));
/* Exercise large requests without allocating large buffers. */
allocation_limit = 4096;
for (bool exact : {false, true})
for (unsigned size : {(unsigned) INT_MAX + 1u, UINT_MAX})
{
hb_vector_t<char> v {'x'};
last_reallocation = 0;
hb_always_assert (!v.alloc (size, exact));
hb_always_assert (v.in_error ());
hb_always_assert (!last_reallocation);
hb_always_assert (v.length == 1 && v[0] == 'x');
}
{
hb_vector_t<char> v;
hb_always_assert (!v.alloc (INT_MAX));
hb_always_assert (v.in_error ());
/* Geometric growth must stay representable in the signed capacity. */
hb_always_assert (last_reallocation == INT_MAX);
}
allocation_limit = SIZE_MAX;
/* Test copy constructor. */
{
hb_vector_t<int> v1 {1, 2};
hb_vector_t<int> v2 {v1};
hb_vector_t<int> V2 {v1};
hb_always_assert (v1.length == 2);
hb_always_assert (v1[0] == 1);
hb_always_assert (v1[1] == 2);
hb_always_assert (v2.length == 2);
hb_always_assert (v2[0] == 1);
hb_always_assert (v2[1] == 2);
}
/* Test copy assignment. */
{
hb_vector_t<int> v1 {1, 2};
hb_vector_t<int> v2 = v1;
hb_vector_t<int> V2 = v1;
hb_always_assert (v1.length == 2);
hb_always_assert (v1[0] == 1);
hb_always_assert (v1[1] == 2);
hb_always_assert (v2.length == 2);
hb_always_assert (v2[0] == 1);
hb_always_assert (v2[1] == 2);
}
/* Self-copy assignment must preserve both trivial and nontrivial items. */
{
hb_vector_t<int> v {1, 2};
const auto &self = v;
v = self;
hb_always_assert (v.length == 2);
hb_always_assert (v[0] == 1);
hb_always_assert (v[1] == 2);
}
{
hb_vector_t<std::string> v;
v.push (std::string ("one"));
v.push (std::string ("two"));
const auto &self = v;
v = self;
hb_always_assert (v.length == 2);
hb_always_assert (v[0] == "one");
hb_always_assert (v[1] == "two");
}
/* Test pushing an element of the vector itself while it grows. */
{
hb_vector_t<int> v {7};
for (unsigned i = 0; i < 1000; i++)
v.push (v.arrayZ[0]);
hb_always_assert (v.length == 1001);
for (unsigned i = 0; i < v.length; i++)
hb_always_assert (v[i] == 7);
}
{
hb_vector_t<std::string> v;
v.push (std::string ("a string longer than the small-string buffer"));
for (unsigned i = 0; i < 1000; i++)
v.push (v.arrayZ[i]);
hb_always_assert (v.length == 1001);
for (unsigned i = 0; i < v.length; i++)
hb_always_assert (v[i] == "a string longer than the small-string buffer");
}
{
hb_vector_t<hb_vector_t<int>> v;
v.push (hb_vector_t<int> {1, 2, 3});
for (unsigned i = 0; i < 1000; i++)
v.push (v.arrayZ[0]);
hb_always_assert (v.length == 1001);
for (unsigned i = 0; i < v.length; i++)
hb_always_assert (v[i].length == 3 && v[i][2] == 3);
}
{
hb_vector_t<hb_vector_t<int>> v {hb_vector_t<int> {1, 2, 3}};
v.push (std::move (v[0]));
hb_always_assert (v.length == 2 && v[0].length == 0);
hb_always_assert (v[1].length == 3 && v[1][2] == 3);
}
{
hb_vector_t<std::string> v {"one"};
v.push (3, v[0][0]);
hb_always_assert (v.length == 2);
hb_always_assert (v[0] == "one" && v[1] == "ooo");
}
{
hb_vector_t<int> v {1, 2, 3};
v.extend (v.as_array ());
hb_always_assert (v.length == 6);
hb_always_assert (v[3] == 1 && v[4] == 2 && v[5] == 3);
}
{
hb_vector_t<int> v {1, 2, 3};
v.extend (hb_array_t<const int> (v.arrayZ + 1, 2));
hb_always_assert (v.length == 5);
hb_always_assert (v[3] == 2 && v[4] == 3);
}
{
hb_vector_t<std::string> v {"one", "two", "three"};
v.extend (v.as_array ());
hb_always_assert (v.length == 6);
for (unsigned i = 0; i < 3; i++)
hb_always_assert (v[i] == v[i + 3]);
}
{
hb_vector_t<std::string> v {"one", "two", "three"};
v.extend (v);
hb_always_assert (v.length == 6);
for (unsigned i = 0; i < 3; i++)
hb_always_assert (v[i] == v[i + 3]);
}
/* A failed vector must not move from an aliased argument. */
{
hb_vector_t<std::string> v {"one"};
v.set_error ();
hb_always_assert (!v.push_or_fail (std::move (v[0])));
hb_always_assert (v.length == 1 && v[0] == "one");
}
/* Test move constructor. */
{
hb_vector_t<int> s {1, 2};
hb_sorted_vector_t<int> S {1, 2};
hb_vector_t<int> v (std::move (s));
hb_sorted_vector_t<int> V (std::move (S));
hb_always_assert (s.length == 0);
hb_always_assert (S.length == 0);
hb_always_assert (v.length == 2);
hb_always_assert (v[0] == 1);
hb_always_assert (v[1] == 2);
}
/* Test move assignment. */
{
hb_vector_t<int> s {1, 2};
hb_sorted_vector_t<int> S {1, 2};
hb_vector_t<int> v;
hb_sorted_vector_t<int> V;
v = std::move (s);
V = std::move (S);
hb_always_assert (s.length == 0);
hb_always_assert (S.length == 0);
hb_always_assert (v.length == 2);
hb_always_assert (V.length == 2);
hb_always_assert (v[0] == 1);
hb_always_assert (v[1] == 2);
}
/* Test initializing from iterable. */
{
hb_set_t s;
s.add (18);
s.add (12);
hb_vector_t<int> v (s);
hb_sorted_vector_t<int> V (s);
hb_always_assert (v.length == 2);
hb_always_assert (V.length == 2);
hb_always_assert (v[0] == 12);
hb_always_assert (V[0] == 12);
hb_always_assert (v[1] == 18);
hb_always_assert (V[1] == 18);
}
/* Test initializing from iterator. */
{
hb_set_t s;
s.add (18);
s.add (12);
hb_vector_t<int> v (hb_iter (s));
hb_vector_t<int> V (hb_iter (s));
hb_always_assert (v.length == 2);
hb_always_assert (V.length == 2);
hb_always_assert (v[0] == 12);
hb_always_assert (V[0] == 12);
hb_always_assert (v[1] == 18);
hb_always_assert (V[1] == 18);
}
/* Test initializing from initializer list and swapping. */
{
hb_vector_t<int> v1 {1, 2, 3};
hb_vector_t<int> v2 {4, 5};
hb_swap (v1, v2);
hb_always_assert (v1.length == 2);
hb_always_assert (v1[0] == 4);
hb_always_assert (v2.length == 3);
hb_always_assert (v2[2] == 3);
}
/* Test initializing sorted-vector from initializer list and swapping. */
{
hb_sorted_vector_t<int> v1 {1, 2, 3};
hb_sorted_vector_t<int> v2 {4, 5};
hb_swap (v1, v2);
hb_always_assert (v1.length == 2);
hb_always_assert (v1[0] == 4);
hb_always_assert (v2.length == 3);
hb_always_assert (v2[2] == 3);
}
{
hb_vector_t<std::string> v;
std::string s;
for (unsigned i = 1; i < 100; i++)
{
s += "x";
v.push (s);
}
hb_vector_t<std::string> v2;
v2 = v;
v2.remove_ordered (50);
v2.remove_unordered (50);
}
{
hb_vector_t<hb_set_t> v;
hb_set_t s {1, 5, 7};
v.push (s);
v << s;
hb_always_assert (s.get_population () == 3);
v << std::move (s);
hb_always_assert (s.get_population () == 0);
}
{
hb_vector_t<hb_map_t> v;
hb_map_t m;
v.push (m);
}
{
hb_vector_t<hb_map_t> v;
hb_map_t m;
v.push (m);
}
/* An empty allocation from a pool with no open chunk is not a failure. */
{
hb_alloc_pool_t pool;
hb_vector_t<int> v, empty;
hb_always_assert (v.allocate_from_pool (&pool, 0));
hb_always_assert (!v.in_error ());
hb_always_assert (v.duplicate_vector_from_pool (&pool, empty));
hb_always_assert (!v.in_error ());
hb_always_assert (v.allocate_from_pool (&pool, 2));
hb_always_assert (!v.in_error ());
hb_always_assert (v.length == 2);
}
return 0;
}