mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
Merge #21553: fuzz: Misc refactor
fa4926cca6
fuzz: [refactor] Use IsValidFlagCombination in signature_checker fuzz target (MarcoFalke)eeee8f5be1
fuzz: Removed unused try-catch in coins_view (MarcoFalke)fa98f3f66e
fuzz: [refactor] Use ConsumeScript in signature_checker fuzz target (MarcoFalke) Pull request description: Some small refactors to remove unused and redundant fuzz code ACKs for top commit: practicalswift: cr re-ACKfa4926cca6
Tree-SHA512: eb07a2140caad7b31495b76385fc7634cf5b6daa4947f430ebb127eb1375583dc11e541a0a42d0e5d93d430480b8a815b93974450fd5ed897528a2d47c752f86
This commit is contained in:
commit
267b60f800
7 changed files with 28 additions and 26 deletions
|
@ -26,6 +26,7 @@ libtest_util_a_SOURCES = \
|
|||
test/util/logging.cpp \
|
||||
test/util/mining.cpp \
|
||||
test/util/net.cpp \
|
||||
test/util/script.cpp \
|
||||
test/util/setup_common.cpp \
|
||||
test/util/str.cpp \
|
||||
test/util/transaction_utils.cpp \
|
||||
|
|
|
@ -230,11 +230,8 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
|
|||
// consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
|
||||
return;
|
||||
}
|
||||
try {
|
||||
(void)Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out);
|
||||
assert(MoneyRange(tx_fee_out));
|
||||
} catch (const std::runtime_error&) {
|
||||
}
|
||||
(void)Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out);
|
||||
assert(MoneyRange(tx_fee_out));
|
||||
},
|
||||
[&] {
|
||||
const CTransaction transaction{random_mutable_transaction};
|
||||
|
|
|
@ -5,13 +5,11 @@
|
|||
#include <pubkey.h>
|
||||
#include <script/interpreter.h>
|
||||
#include <streams.h>
|
||||
#include <test/util/script.h>
|
||||
#include <version.h>
|
||||
|
||||
#include <test/fuzz/fuzz.h>
|
||||
|
||||
/** Flags that are not forbidden by an assert */
|
||||
static bool IsValidFlagCombination(unsigned flags);
|
||||
|
||||
void initialize_script_flags()
|
||||
{
|
||||
static const ECCVerifyHandle verify_handle;
|
||||
|
@ -74,10 +72,3 @@ FUZZ_TARGET_INIT(script_flags, initialize_script_flags)
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static bool IsValidFlagCombination(unsigned flags)
|
||||
{
|
||||
if (flags & SCRIPT_VERIFY_CLEANSTACK && ~flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) return false;
|
||||
if (flags & SCRIPT_VERIFY_WITNESS && ~flags & SCRIPT_VERIFY_P2SH) return false;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <script/interpreter.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
#include <test/util/script.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
|
@ -56,17 +58,12 @@ FUZZ_TARGET_INIT(signature_checker, initialize_signature_checker)
|
|||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
const unsigned int flags = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
|
||||
const SigVersion sig_version = fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0});
|
||||
const std::string script_string_1 = fuzzed_data_provider.ConsumeRandomLengthString(65536);
|
||||
const std::vector<uint8_t> script_bytes_1{script_string_1.begin(), script_string_1.end()};
|
||||
const std::string script_string_2 = fuzzed_data_provider.ConsumeRandomLengthString(65536);
|
||||
const std::vector<uint8_t> script_bytes_2{script_string_2.begin(), script_string_2.end()};
|
||||
const auto script_1 = ConsumeScript(fuzzed_data_provider, 65536);
|
||||
const auto script_2 = ConsumeScript(fuzzed_data_provider, 65536);
|
||||
std::vector<std::vector<unsigned char>> stack;
|
||||
(void)EvalScript(stack, {script_bytes_1.begin(), script_bytes_1.end()}, flags, FuzzedSignatureChecker(fuzzed_data_provider), sig_version, nullptr);
|
||||
if ((flags & SCRIPT_VERIFY_CLEANSTACK) != 0 && ((flags & SCRIPT_VERIFY_P2SH) == 0 || (flags & SCRIPT_VERIFY_WITNESS) == 0)) {
|
||||
(void)EvalScript(stack, script_1, flags, FuzzedSignatureChecker(fuzzed_data_provider), sig_version, nullptr);
|
||||
if (!IsValidFlagCombination(flags)) {
|
||||
return;
|
||||
}
|
||||
if ((flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
|
||||
return;
|
||||
}
|
||||
(void)VerifyScript({script_bytes_1.begin(), script_bytes_1.end()}, {script_bytes_2.begin(), script_bytes_2.end()}, nullptr, flags, FuzzedSignatureChecker(fuzzed_data_provider), nullptr);
|
||||
(void)VerifyScript(script_1, script_2, nullptr, flags, FuzzedSignatureChecker(fuzzed_data_provider), nullptr);
|
||||
}
|
||||
|
|
|
@ -80,7 +80,7 @@ CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, co
|
|||
|
||||
CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length, const bool maybe_p2wsh) noexcept
|
||||
{
|
||||
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
||||
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
|
||||
CScript r_script{b.begin(), b.end()};
|
||||
if (maybe_p2wsh && fuzzed_data_provider.ConsumeBool()) {
|
||||
uint256 script_hash;
|
||||
|
|
13
src/test/util/script.cpp
Normal file
13
src/test/util/script.cpp
Normal file
|
@ -0,0 +1,13 @@
|
|||
// Copyright (c) 2021 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <script/interpreter.h>
|
||||
#include <test/util/script.h>
|
||||
|
||||
bool IsValidFlagCombination(unsigned flags)
|
||||
{
|
||||
if (flags & SCRIPT_VERIFY_CLEANSTACK && ~flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) return false;
|
||||
if (flags & SCRIPT_VERIFY_WITNESS && ~flags & SCRIPT_VERIFY_P2SH) return false;
|
||||
return true;
|
||||
}
|
|
@ -18,4 +18,7 @@ static const CScript P2WSH_OP_TRUE{
|
|||
return hash;
|
||||
}())};
|
||||
|
||||
/** Flags that are not forbidden by an assert in script validation */
|
||||
bool IsValidFlagCombination(unsigned flags);
|
||||
|
||||
#endif // BITCOIN_TEST_UTIL_SCRIPT_H
|
||||
|
|
Loading…
Add table
Reference in a new issue