2021-01-25 12:23:45 +01:00
|
|
|
// Copyright (c) 2019-2021 The Bitcoin Core developers
|
2019-10-08 14:36:57 +00:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <compressor.h>
|
|
|
|
#include <core_io.h>
|
|
|
|
#include <core_memusage.h>
|
|
|
|
#include <policy/policy.h>
|
|
|
|
#include <pubkey.h>
|
|
|
|
#include <script/descriptor.h>
|
2020-04-04 01:32:39 +08:00
|
|
|
#include <script/interpreter.h>
|
2019-10-08 14:36:57 +00:00
|
|
|
#include <script/script.h>
|
2020-05-16 18:15:13 +00:00
|
|
|
#include <script/script_error.h>
|
2019-10-08 14:36:57 +00:00
|
|
|
#include <script/sign.h>
|
|
|
|
#include <script/signingprovider.h>
|
|
|
|
#include <script/standard.h>
|
|
|
|
#include <streams.h>
|
2020-03-10 11:58:30 +00:00
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
2019-10-08 14:36:57 +00:00
|
|
|
#include <test/fuzz/fuzz.h>
|
2020-03-10 11:58:30 +00:00
|
|
|
#include <test/fuzz/util.h>
|
2020-01-15 23:37:27 +00:00
|
|
|
#include <univalue.h>
|
2019-10-08 14:36:57 +00:00
|
|
|
|
2020-05-16 18:15:13 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2020-05-10 18:35:55 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
void initialize_script()
|
2019-10-08 14:36:57 +00:00
|
|
|
{
|
|
|
|
// Fuzzers using pubkey must hold an ECCVerifyHandle.
|
2020-03-10 12:55:41 +00:00
|
|
|
static const ECCVerifyHandle verify_handle;
|
2020-01-15 23:37:27 +00:00
|
|
|
|
|
|
|
SelectParams(CBaseChainParams::REGTEST);
|
2019-10-08 14:36:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
FUZZ_TARGET_INIT(script, initialize_script)
|
2019-10-08 14:36:57 +00:00
|
|
|
{
|
2020-04-04 01:32:39 +08:00
|
|
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
2020-05-10 18:35:55 +00:00
|
|
|
const std::optional<CScript> script_opt = ConsumeDeserializable<CScript>(fuzzed_data_provider);
|
2020-04-04 01:32:39 +08:00
|
|
|
if (!script_opt) return;
|
|
|
|
const CScript script{*script_opt};
|
2019-10-08 14:36:57 +00:00
|
|
|
|
2020-05-01 17:31:38 -07:00
|
|
|
CompressedScript compressed;
|
2020-01-15 23:37:27 +00:00
|
|
|
if (CompressScript(script, compressed)) {
|
|
|
|
const unsigned int size = compressed[0];
|
2020-03-07 17:03:47 -05:00
|
|
|
compressed.erase(compressed.begin());
|
2020-07-18 20:27:56 +02:00
|
|
|
assert(size <= 5);
|
2020-01-15 23:37:27 +00:00
|
|
|
CScript decompressed_script;
|
|
|
|
const bool ok = DecompressScript(decompressed_script, size, compressed);
|
|
|
|
assert(ok);
|
2020-03-07 17:03:47 -05:00
|
|
|
assert(script == decompressed_script);
|
2020-01-15 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 14:36:57 +00:00
|
|
|
CTxDestination address;
|
2020-05-30 09:16:05 -04:00
|
|
|
TxoutType type_ret;
|
2019-10-08 14:36:57 +00:00
|
|
|
std::vector<CTxDestination> addresses;
|
|
|
|
int required_ret;
|
2020-12-26 18:53:32 -06:00
|
|
|
bool extract_destinations_ret = ExtractDestinations(script, type_ret, addresses, required_ret);
|
|
|
|
bool extract_destination_ret = ExtractDestination(script, address);
|
|
|
|
if (!extract_destinations_ret) {
|
|
|
|
assert(!extract_destination_ret);
|
|
|
|
if (type_ret == TxoutType::MULTISIG) {
|
|
|
|
assert(addresses.empty() && required_ret == 0);
|
|
|
|
} else {
|
|
|
|
assert(type_ret == TxoutType::PUBKEY ||
|
|
|
|
type_ret == TxoutType::NONSTANDARD ||
|
|
|
|
type_ret == TxoutType::NULL_DATA);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(required_ret >= 1 && required_ret <= 16);
|
|
|
|
assert((unsigned long)required_ret == addresses.size());
|
|
|
|
assert(type_ret == TxoutType::MULTISIG || required_ret == 1);
|
|
|
|
}
|
|
|
|
if (type_ret == TxoutType::NONSTANDARD || type_ret == TxoutType::NULL_DATA) {
|
|
|
|
assert(!extract_destinations_ret);
|
|
|
|
}
|
|
|
|
if (!extract_destination_ret) {
|
|
|
|
assert(type_ret == TxoutType::PUBKEY ||
|
|
|
|
type_ret == TxoutType::NONSTANDARD ||
|
|
|
|
type_ret == TxoutType::NULL_DATA ||
|
|
|
|
type_ret == TxoutType::MULTISIG);
|
|
|
|
} else {
|
|
|
|
assert(address == addresses[0]);
|
|
|
|
}
|
|
|
|
if (type_ret == TxoutType::NONSTANDARD ||
|
|
|
|
type_ret == TxoutType::NULL_DATA ||
|
|
|
|
type_ret == TxoutType::MULTISIG) {
|
|
|
|
assert(!extract_destination_ret);
|
|
|
|
}
|
2019-10-08 14:36:57 +00:00
|
|
|
|
2020-05-30 09:16:05 -04:00
|
|
|
TxoutType which_type;
|
2020-12-24 13:48:30 -06:00
|
|
|
bool is_standard_ret = IsStandard(script, which_type);
|
2020-12-26 18:53:32 -06:00
|
|
|
assert(type_ret == which_type);
|
2020-12-24 13:48:30 -06:00
|
|
|
if (!is_standard_ret) {
|
|
|
|
assert(which_type == TxoutType::NONSTANDARD ||
|
|
|
|
which_type == TxoutType::NULL_DATA ||
|
|
|
|
which_type == TxoutType::MULTISIG);
|
|
|
|
}
|
|
|
|
if (which_type == TxoutType::NONSTANDARD) {
|
|
|
|
assert(!is_standard_ret);
|
|
|
|
}
|
2020-12-24 14:16:46 +01:00
|
|
|
if (which_type == TxoutType::NULL_DATA) {
|
|
|
|
assert(script.IsUnspendable());
|
|
|
|
}
|
|
|
|
if (script.IsUnspendable()) {
|
|
|
|
assert(which_type == TxoutType::NULL_DATA ||
|
|
|
|
which_type == TxoutType::NONSTANDARD);
|
|
|
|
}
|
2019-10-08 14:36:57 +00:00
|
|
|
|
2020-12-26 18:53:32 -06:00
|
|
|
const FlatSigningProvider signing_provider;
|
|
|
|
(void)InferDescriptor(script, signing_provider);
|
|
|
|
(void)IsSegWitOutput(signing_provider, script);
|
|
|
|
(void)IsSolvable(signing_provider, script);
|
|
|
|
|
2019-10-08 14:36:57 +00:00
|
|
|
(void)RecursiveDynamicUsage(script);
|
|
|
|
|
|
|
|
std::vector<std::vector<unsigned char>> solutions;
|
|
|
|
(void)Solver(script, solutions);
|
|
|
|
|
|
|
|
(void)script.HasValidOps();
|
|
|
|
(void)script.IsPayToScriptHash();
|
|
|
|
(void)script.IsPayToWitnessScriptHash();
|
|
|
|
(void)script.IsPushOnly();
|
|
|
|
(void)script.GetSigOpCount(/* fAccurate= */ false);
|
2020-01-15 23:37:27 +00:00
|
|
|
|
|
|
|
(void)FormatScript(script);
|
|
|
|
(void)ScriptToAsmStr(script, false);
|
|
|
|
(void)ScriptToAsmStr(script, true);
|
|
|
|
|
|
|
|
UniValue o1(UniValue::VOBJ);
|
2021-02-01 09:52:07 -06:00
|
|
|
ScriptPubKeyToUniv(script, o1, true, true);
|
|
|
|
ScriptPubKeyToUniv(script, o1, true, false);
|
2020-01-15 23:37:27 +00:00
|
|
|
UniValue o2(UniValue::VOBJ);
|
2021-02-01 09:52:07 -06:00
|
|
|
ScriptPubKeyToUniv(script, o2, false, true);
|
|
|
|
ScriptPubKeyToUniv(script, o2, false, false);
|
2020-01-15 23:37:27 +00:00
|
|
|
UniValue o3(UniValue::VOBJ);
|
|
|
|
ScriptToUniv(script, o3, true);
|
|
|
|
UniValue o4(UniValue::VOBJ);
|
|
|
|
ScriptToUniv(script, o4, false);
|
2020-03-10 11:58:30 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
2020-05-01 17:31:38 -07:00
|
|
|
CompressedScript compressed_script;
|
|
|
|
compressed_script.assign(bytes.begin(), bytes.end());
|
2020-04-04 01:32:39 +08:00
|
|
|
// DecompressScript(..., ..., bytes) is not guaranteed to be defined if the bytes vector is too short
|
2020-05-01 17:31:38 -07:00
|
|
|
if (compressed_script.size() >= 32) {
|
2020-03-10 11:58:30 +00:00
|
|
|
CScript decompressed_script;
|
2020-05-01 17:31:38 -07:00
|
|
|
DecompressScript(decompressed_script, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), compressed_script);
|
2020-03-10 11:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 01:32:39 +08:00
|
|
|
|
2020-05-10 18:35:55 +00:00
|
|
|
const std::optional<CScript> other_script = ConsumeDeserializable<CScript>(fuzzed_data_provider);
|
2020-04-04 01:32:39 +08:00
|
|
|
if (other_script) {
|
|
|
|
{
|
|
|
|
CScript script_mut{script};
|
|
|
|
(void)FindAndDelete(script_mut, *other_script);
|
|
|
|
}
|
|
|
|
const std::vector<std::string> random_string_vector = ConsumeRandomLengthStringVector(fuzzed_data_provider);
|
|
|
|
const uint32_t u32{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
|
|
|
|
const uint32_t flags{u32 | SCRIPT_VERIFY_P2SH};
|
|
|
|
{
|
|
|
|
CScriptWitness wit;
|
|
|
|
for (const auto& s : random_string_vector) {
|
|
|
|
wit.stack.emplace_back(s.begin(), s.end());
|
|
|
|
}
|
|
|
|
(void)CountWitnessSigOps(script, *other_script, &wit, flags);
|
|
|
|
wit.SetNull();
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 18:15:13 +00:00
|
|
|
|
|
|
|
(void)GetOpName(ConsumeOpcodeType(fuzzed_data_provider));
|
|
|
|
(void)ScriptErrorString(static_cast<ScriptError>(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, SCRIPT_ERR_ERROR_COUNT)));
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
|
|
|
CScript append_script{bytes.begin(), bytes.end()};
|
|
|
|
append_script << fuzzed_data_provider.ConsumeIntegral<int64_t>();
|
|
|
|
append_script << ConsumeOpcodeType(fuzzed_data_provider);
|
|
|
|
append_script << CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
|
|
|
|
append_script << ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-04 21:18:59 +02:00
|
|
|
const CTxDestination tx_destination_1{
|
|
|
|
fuzzed_data_provider.ConsumeBool() ?
|
|
|
|
DecodeDestination(fuzzed_data_provider.ConsumeRandomLengthString()) :
|
|
|
|
ConsumeTxDestination(fuzzed_data_provider)};
|
2020-05-16 18:15:13 +00:00
|
|
|
const CTxDestination tx_destination_2 = ConsumeTxDestination(fuzzed_data_provider);
|
2021-07-04 21:18:59 +02:00
|
|
|
|
2020-05-16 18:15:13 +00:00
|
|
|
(void)(tx_destination_1 == tx_destination_2);
|
|
|
|
(void)(tx_destination_1 < tx_destination_2);
|
|
|
|
}
|
2019-10-08 14:36:57 +00:00
|
|
|
}
|