Skip to content

[Review] Merge bitcoin/bitcoin#37007: refactor: Add missing explicit instantiations of function templates - #3

Merged
jonatack merged 1 commit into
bitcoin-v:masterfrom
BTC4Peace:mirror/pr-8e09dc7bf97f
Dec 1, 2026
Merged

jonatack merged 1 commit into
bitcoin-v:masterfrom
BTC4Peace:mirror/pr-8e09dc7bf97f

Conversation

@auditbob

@auditbob auditbob Bot commented Nov 30, 2026

Copy link
Copy Markdown

Upstream commit: bitcoin/bitcoin@8e09dc7

7ec6081ecab27b1ff453f986567b299f7b76d994 refactor: Add missing explicit instantiations of function templates (Hennadii Stepanov)

Pull request description:

The SignatureHash and SignatureHashSchnorr function templates are defined in interpreter.cpp but are also used from other translation units, which have relied on implicit instantiations being emitted in that file. The standard does not guarantee that.

This PR adds the missing explicit instantiations. It fixes linker errors (see the nightly builds with libstdc++ and libc++) when building with Clang 24, where module-local optimizer hints are now enabled by default. The change was introduced in llvm/llvm-project#238529.

A successful build of this branch with Clang 24 is available here: hebasto/bitcoin-core-nightly#496.

As a workaround, Clang's previous behavior can be restored with the -fno-module-local-hints flag.

ACKs for top commit:
maflcko:
review ACK 7ec6081ecab27b1ff453f986567b299f7b76d994 🔌
sedited:
ACK 7ec6081ecab27b1ff453f986567b299f7b76d994

Tree-SHA512: d49b578876df77f4636d750e5c11c094308b06c8803d3ef2bd1644303dcaf25a9af9afd695f93ecaf76f7dd43ef158221cc355518c039ad8fc80612fa229cf8d


Mirrored from audited staging PR: BTC4Peace/bitcoin-agentic-workflow#43

… function templates

7ec6081 refactor: Add missing explicit instantiations of function templates (Hennadii Stepanov)

Pull request description:

  The `SignatureHash` and `SignatureHashSchnorr` function templates are defined in `interpreter.cpp` but are also used from other translation units, which have relied on implicit instantiations being emitted in that file. The standard does not guarantee that.

  This PR adds the missing explicit instantiations. It fixes linker errors (see the nightly builds with [libstdc++](https://cdash.fish.foo/builds/941/build) and [libc++](https://cdash.fish.foo/builds/951/build)) when building with Clang 24, where module-local optimizer hints are now enabled by default. The change was introduced in llvm/llvm-project#238529.

  A successful build of this branch with Clang 24 is available here: hebasto/bitcoin-core-nightly#496.

  As a workaround, Clang's previous behavior can be [restored](hebasto/bitcoin-core-nightly#495) with the `-fno-module-local-hints` flag.

ACKs for top commit:
  maflcko:
    review ACK 7ec6081 🔌
  sedited:
    ACK 7ec6081

Tree-SHA512: d49b578876df77f4636d750e5c11c094308b06c8803d3ef2bd1644303dcaf25a9af9afd695f93ecaf76f7dd43ef158221cc355518c039ad8fc80612fa229cf8d

Cherry-picked-from: bitcoin/bitcoin@8e09dc7
@auditbob

auditbob Bot commented Nov 30, 2026

Copy link
Copy Markdown
Author

Post-Merge Audit — 8e09dc7bf97fc224f4f1d7777685e1994265db5e (PR #37007)

1. Summary

A single-file, link-time-only change in src/script/interpreter.cpp (+8 lines, no deletions): it adds explicit instantiation definitions for the two function templates that are declared in src/script/interpreter.h but defined only in the .cpp:

  • template bool SignatureHashSchnorr<CTransaction>(...) and <CMutableTransaction>(...) at src/script/interpreter.cpp:1579-1580, placed immediately after the template definition at src/script/interpreter.cpp:1488-1489 and its declared counterpart src/script/interpreter.h:310-311.
  • template uint256 SignatureHash<CTransaction>(...) and <CMutableTransaction>(...) at src/script/interpreter.cpp:1690-1691, placed immediately after the template definition at src/script/interpreter.cpp:1609-1610 and its declared counterpart src/script/interpreter.h:272-273.

Motivation (per the commit message): other translation units referenced these specializations through declarations only and depended on the compiler emitting implicitly-instantiated, externally-visible symbols from interpreter.cpp, which the standard does not guarantee; Clang 24's default-on module-local optimizer hints stop emitting them, producing link errors. The new blocks follow the existing local convention (// explicit instantiation before template void PrecomputedTransactionData::Init(...) at src/script/interpreter.cpp:1466-1470 and template class GenericTransactionSignatureChecker<...> at src/script/interpreter.cpp:1842-1844).

2. Consensus & Validation

No impact. Verified against the current file that no statement inside the two template bodies is altered by this diff: the SIGHASH_SINGLE out-of-range early return of uint256::ONE (src/script/interpreter.cpp:1616-1620), the SigHashCache midstate load/store path (:1627-1631, :1680-1682) and the BIP143/BIP341 preimage construction are unchanged context lines. Explicit instantiation forces emission of the same specialization bodies that were previously emitted implicitly; it cannot change hashing, validation, chainstate, UTXO or reorg behaviour.

3. Security, P2P & DoS

None. No new inputs, no serialization/deserialization change, no resource accounting change, no RPC surface. Both added specializations correspond to genuine in-tree callers (src/script/interpreter.cpp:1722, :1751; src/script/sign.cpp:77, :104), so no dead exported symbols are introduced.

4. Concurrency & Locking

None. Compile/link-time change only; no shared state, no mutexes, no lock-ordering effect.

5. Code Quality & Test Coverage

Verified signature correctness (the only real failure mode for explicit instantiation):

  • SignatureHashSchnorr instantiation parameter list (uint256&, ScriptExecutionData&, const T&, uint32_t, uint8_t, SigVersion, const PrecomputedTransactionData&, MissingDataBehavior) matches src/script/interpreter.h:311 and the definition at src/script/interpreter.cpp:1489 exactly; that declaration has no default arguments.
  • SignatureHash instantiation parameter list (const CScript&, const T&, unsigned int, int32_t, const CAmount&, SigVersion, const PrecomputedTransactionData*, SigHashCache*) matches src/script/interpreter.cpp:1610 and src/script/interpreter.h:273 exactly, with the pointer (not reference) last parameters and the two default arguments (cache = nullptr, sighash_cache = nullptr) correctly omitted, as required for explicit instantiation.
  • Namespace/linkage: the anonymous namespace opened at src/script/interpreter.cpp:1253 closes at src/script/interpreter.cpp:1406; both new blocks (1578, 1689) sit after that closing brace, so the symbols receive external linkage in the global namespace. Placement after each template definition is respected (1576 → 1579; 1687 → 1690).
  • Coverage of call sites: production callers use exactly these two specializations — src/script/sign.cpp:77 (m_txto is const CMutableTransaction&, src/script/sign.h:55) and src/script/sign.cpp:104; internal uses inside GenericTransactionSignatureChecker<T> at src/script/interpreter.cpp:1722 and :1751. Test/fuzz callers I confirmed use either CTransaction (src/test/sighash_tests.cpp:182,206; src/test/fuzz/script_interpreter.cpp:26,32,40) or CMutableTransaction (src/test/sighash_tests.cpp:133,141 and :218,240-245; src/test/bip352_tests.cpp:293,296; src/test/script_tests.cpp:318 where spendTx is a mutable test transaction, and :1705 whose tx is passed to MutableTransactionSignatureCreator at :1692). No third specialization appears in-tree in the search I could complete, so the pair of instantiations is sufficient.
  • No conflicting explicit instantiation of the same specialization was found in src/script or src/test (searches for SignatureHash(/SignatureHashSchnorr( returned only declarations, definitions, call sites, and the new lines).

[Nit] No extern template declarations were added next to the declarations in src/script/interpreter.h:273 and :311. Not needed for correctness here (the bodies are not visible to other TUs, so they cannot implicitly instantiate), but it would document the contract at the declaration site (compare the extern template convention used elsewhere in the tree).

[Nit] The regression class this fixes is only observable at link time, and the tree has no -Wundefined-func-template-style diagnostic enabled under src/ (search returned no occurrences), so a future caller instantiating these templates with a third T would again fail only on nightly/newer-compiler builds. Enabling such a diagnostic (or a link-smoke target) would convert this class of failure into a build-time error. This is a pre-existing coverage gap, not a defect of this merge.

[Nit] src/script/interpreter.cpp is compiled into more than one library: bitcoinkernel lists ../script/interpreter.cpp at src/kernel/CMakeLists.txt:49 and it is also listed at src/CMakeLists.txt:81. With explicit instantiation each of those objects now carries unconditional strong definitions. This does not introduce a new duplicate-symbol hazard (the same file's non-template definitions such as EvalScript/VerifyScript are already strong definitions in every copy), but it does mean the newly forced emission also occurs in the bitcoinkernel copy, which is built under the stricter kernel_warn_interface including -Wunique-object-duplication (src/kernel/CMakeLists.txt:89-90). I could not verify locally whether a warnings-as-errors configuration flags these new definitions; the PR reports a successful Clang 24 build. Worth a glance when the warnings-as-errors CI matrix next runs with Clang 24.

6. Cross-Audit Reconciliation & Post-Merge Verdict

Confirmed by both auditors and independently verified here: the four instantiations match the header declarations and .cpp definitions exactly, defaults are correctly omitted for SignatureHash, both blocks are outside the anonymous namespace (src/script/interpreter.cpp:1406) and after their respective definitions, and the pattern matches the pre-existing PrecomputedTransactionData / GenericTransactionSignatureChecker instantiations. Both auditors' "no consensus, DoS, or locking impact" conclusions hold: no statement inside either template body changed.

Caught by one auditor and verified: the SigHashCache and SIGHASH_SINGLE/uint256::ONE paths are untouched (audit A) — confirmed at src/script/interpreter.cpp:1616-1620, 1627-1631, 1680-1682. The production CMutableTransaction callers in sign.cpp (audit B) — confirmed at src/script/sign.cpp:77,104 with m_txto typed const CMutableTransaction& at src/script/sign.h:55.

Dropped/downgraded as unverified or unverifiable: audit A's line citations src/script/interpreter.cpp:1578-1580 / 1689-1691 are off by the comment line (actual instantiation lines are 1579-1580 and 1690-1691); its claim of "no hit in CMakeLists.txt/cmake/" for -Wundefined-func-template was only independently confirmed for src/. Audit B's "all 5 templates in interpreter.h now instantiated" is a loose count (two class-level templates plus the two PrecomputedTransactionData member function templates) and its assertion that the fuzz ConsumeTransaction result type is CTransaction was not verified; neither affects the verdict. The -Wunique-object-duplication watch item above is my own addition and remains unverified — treat it as a check item, not a finding.

Overall risk: very low. The change is semantically inert at runtime, fixes a real Clang 24 link failure, and the specializations/argument lists/namespace placement are correct. Files/lines to double-check if desired: src/script/interpreter.cpp:1579-1580 and :1690-1691 against src/script/interpreter.h:310-311 and :272-273.

Triage: routine


📊 Token Usage & Review Stats
Stage Model Input Tokens Cache Hit Output Reasoning Total Rounds Latency
Audit A deepseek-flash 226,179 206,848 (91%) 10,391 7,112 236,570 13 90.1s
Audit B gemini-4-neon-high 127,615 77,517 (61%) 5,791 4,276 133,406 4 73.7s
Judge deepseek-flash 206,460 158,720 (77%) 16,099 11,522 222,559 11 114.1s
Total — 560,254 443,085 (79%) 32,281 22,910 592,535 28 278.0s

@auditbob auditbob Bot added audited Automated review completed Refactoring labels Nov 30, 2026

@jonatack jonatack left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 5d39b70

@jonatack
jonatack merged commit 989d76e into bitcoin-v:master Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

audited Automated review completed Refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant