2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2019-2020 The Bitcoin Core developers
|
2019-10-08 11:36:57 -03: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-03 14:32:39 -03:00
|
|
|
#include <script/interpreter.h>
|
2019-10-08 11:36:57 -03:00
|
|
|
#include <script/script.h>
|
|
|
|
#include <script/sign.h>
|
|
|
|
#include <script/signingprovider.h>
|
|
|
|
#include <script/standard.h>
|
|
|
|
#include <streams.h>
|
2020-03-10 08:58:30 -03:00
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
2019-10-08 11:36:57 -03:00
|
|
|
#include <test/fuzz/fuzz.h>
|
2020-03-10 08:58:30 -03:00
|
|
|
#include <test/fuzz/util.h>
|
2020-01-15 20:37:27 -03:00
|
|
|
#include <univalue.h>
|
2019-10-08 11:36:57 -03:00
|
|
|
#include <util/memory.h>
|
|
|
|
|
2020-05-10 14:35:55 -04:00
|
|
|
#include <cstdint>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-10-08 11:36:57 -03:00
|
|
|
void initialize()
|
|
|
|
{
|
|
|
|
// Fuzzers using pubkey must hold an ECCVerifyHandle.
|
2020-03-10 09:55:41 -03:00
|
|
|
static const ECCVerifyHandle verify_handle;
|
2020-01-15 20:37:27 -03:00
|
|
|
|
|
|
|
SelectParams(CBaseChainParams::REGTEST);
|
2019-10-08 11:36:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_one_input(const std::vector<uint8_t>& buffer)
|
|
|
|
{
|
2020-04-03 14:32:39 -03:00
|
|
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
2020-05-10 14:35:55 -04:00
|
|
|
const std::optional<CScript> script_opt = ConsumeDeserializable<CScript>(fuzzed_data_provider);
|
2020-04-03 14:32:39 -03:00
|
|
|
if (!script_opt) return;
|
|
|
|
const CScript script{*script_opt};
|
2019-10-08 11:36:57 -03:00
|
|
|
|
|
|
|
std::vector<unsigned char> compressed;
|
2020-01-15 20:37:27 -03:00
|
|
|
if (CompressScript(script, compressed)) {
|
|
|
|
const unsigned int size = compressed[0];
|
2020-03-07 19:03:47 -03:00
|
|
|
compressed.erase(compressed.begin());
|
2020-01-15 20:37:27 -03:00
|
|
|
assert(size >= 0 && size <= 5);
|
|
|
|
CScript decompressed_script;
|
|
|
|
const bool ok = DecompressScript(decompressed_script, size, compressed);
|
|
|
|
assert(ok);
|
2020-03-07 19:03:47 -03:00
|
|
|
assert(script == decompressed_script);
|
2020-01-15 20:37:27 -03:00
|
|
|
}
|
|
|
|
|
2019-10-08 11:36:57 -03:00
|
|
|
CTxDestination address;
|
|
|
|
(void)ExtractDestination(script, address);
|
|
|
|
|
|
|
|
txnouttype type_ret;
|
|
|
|
std::vector<CTxDestination> addresses;
|
|
|
|
int required_ret;
|
|
|
|
(void)ExtractDestinations(script, type_ret, addresses, required_ret);
|
|
|
|
|
|
|
|
(void)GetScriptForWitness(script);
|
|
|
|
|
|
|
|
const FlatSigningProvider signing_provider;
|
|
|
|
(void)InferDescriptor(script, signing_provider);
|
|
|
|
|
|
|
|
(void)IsSegWitOutput(signing_provider, script);
|
|
|
|
|
|
|
|
(void)IsSolvable(signing_provider, script);
|
|
|
|
|
|
|
|
txnouttype which_type;
|
|
|
|
(void)IsStandard(script, which_type);
|
|
|
|
|
|
|
|
(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.IsUnspendable();
|
|
|
|
(void)script.GetSigOpCount(/* fAccurate= */ false);
|
2020-01-15 20:37:27 -03:00
|
|
|
|
|
|
|
(void)FormatScript(script);
|
|
|
|
(void)ScriptToAsmStr(script, false);
|
|
|
|
(void)ScriptToAsmStr(script, true);
|
|
|
|
|
|
|
|
UniValue o1(UniValue::VOBJ);
|
|
|
|
ScriptPubKeyToUniv(script, o1, true);
|
|
|
|
UniValue o2(UniValue::VOBJ);
|
|
|
|
ScriptPubKeyToUniv(script, o2, false);
|
|
|
|
UniValue o3(UniValue::VOBJ);
|
|
|
|
ScriptToUniv(script, o3, true);
|
|
|
|
UniValue o4(UniValue::VOBJ);
|
|
|
|
ScriptToUniv(script, o4, false);
|
2020-03-10 08:58:30 -03:00
|
|
|
|
|
|
|
{
|
|
|
|
const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
2020-04-03 14:32:39 -03:00
|
|
|
// DecompressScript(..., ..., bytes) is not guaranteed to be defined if the bytes vector is too short
|
|
|
|
if (bytes.size() >= 32) {
|
2020-03-10 08:58:30 -03:00
|
|
|
CScript decompressed_script;
|
|
|
|
DecompressScript(decompressed_script, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), bytes);
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 14:32:39 -03:00
|
|
|
|
2020-05-10 14:35:55 -04:00
|
|
|
const std::optional<CScript> other_script = ConsumeDeserializable<CScript>(fuzzed_data_provider);
|
2020-04-03 14:32:39 -03: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();
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 11:36:57 -03:00
|
|
|
}
|