Skip to content

fix(build): repair singleton double-checked locking race and harden aarch64 portability - #203

Merged
SteNicholas merged 4 commits into
apache:mainfrom
u70b3:fix/aarch64-hardening
Aug 21, 2026
Merged

SteNicholas merged 4 commits into
apache:mainfrom
u70b3:fix/aarch64-hardening

Conversation

@u70b3

@u70b3 u70b3 commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Following up on the aarch64 port (#181), an ARM portability audit (7-category static sweep plus on-hardware validation on a 128-core Kunpeng-920, ARMv8.2) found one real concurrency bug currently hidden by x86 TSO and three latent UB/divergence risks:

  1. Singleton<T> double-checked locking is broken on weak memory models. The instance pointer was published with a plain store guarded only by a compiler-only MEMORY_BARRIER, while the fast path used a plain non-atomic load. A reader can therefore observe a non-null pointer before construction is visible. The fix uses std::atomic<T*> with an acquire fast-path load and release publication, while preserving LazyInstantiation::Create(T*&)'s public signature.

  2. IOHook::Impl::mode_ has a data race. Reset() writes the plain enum while IO threads read it in Try(). The fix makes it std::atomic<Mode> and stores it before the existing sequentially consistent pos_ and io_count_ stores.

  3. SerializationUtils::DeserializeBinaryRow violates strict aliasing. It read an arity from a byte-filled buffer through reinterpret_cast<int32_t*>. The fix uses memcpy, matching the serialize side without changing the wire format.

  4. Extreme cache/block sizes can trigger undefined double -> int conversions. CacheManager and SstFileWriter could produce architecture-dependent results for pathological configurations. A common-layer SaturatingDoubleToInteger<T> helper now implements Java-style saturation and is used at both sites.

FieldSumAgg INT8 sum/negation was also audited and intentionally left unchanged: modulo-256 addition and negation produce the same stored bits regardless of plain-char signedness. Unlike the min/max comparisons fixed in #181, signedness cannot change these results.

Out of scope (documented follow-ups): tightening option validation for btree-index.block-size / cache-page-size, and the unchecked int64-to-int32 narrowing in CoreOptions::GetCachePageSize.

Tests

The concurrency tests were verified red before the fix and TSan-clean after it on the Kunpeng-920:

Test Coverage
SingletonTest.TestConcurrentFirstPublication Contended first publication of Singleton<T> on a translation-unit-local type, so no link order, --gtest_shuffle, or --gtest_filter can pre-publish the instance and silently degrade the gate into the fast path; each thread also verifies full construction visibility
IOHookTest.TestConcurrentResetAndTry Concurrent Reset() / Clear() and Try() access; a shared start barrier releases all threads together and the reset loop runs until every worker finishes, so overlap is structural rather than timing-dependent
SerializationUtilsTest.TestDeserializeBinaryRowFromStream Stream round-trip and big-endian arity prefix
CacheManagerTest Saturation, normal split, and eviction behavior
SaturatingCastTest int64/int32 normal, boundary, infinity, and NaN cases

The FactoryCreator first-publication storm from the initial revision was removed because static REGISTER_PAIMON_FACTORY constructors initialize it before main(), so it cannot test first publication reliably. For the same reason the storm no longer borrows Singleton<IOHook>: GetInstance() is now defined in the header, so the test instantiates Singleton<T> for its own TU-local type and controls first publication by construction. FactoryCreator and IOHook keep extern-template declarations with explicit instantiations in singleton.cpp, because the -Bsymbolic plugin shared libraries must share exactly one definition (implicit per-library copies split the singleton and factory registrations are lost).

Validation on the latest head (Kunpeng-920, aarch64):

  • paimon-common-factories-test: 9/9 passed (Debug), including 20× --gtest_shuffle runs and a --gtest_repeat=5 --gtest_shuffle run proving order independence
  • paimon-common-test: 1436/1436 passed and paimon-common-sst-file-format-test: 32/32 passed (Debug)
  • ThreadSanitizer: factories binary clean; the two race tests stress-run 20× under --gtest_shuffle with zero reports
  • pre-commit run --files <changed>: all checks passed
  • git diff --check: clean

API and Format

No public API or storage-format change. LazyInstantiation::Create(T*&) remains unchanged, and the big-endian arity prefix is preserved and pinned by tests.

Documentation

No documentation changes needed; code comments explain the ordering and saturation rationale.

Generative AI tooling

Generated-by: Claude Code (claude-opus-4-8)

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants