Bitcoin Core  22.99.0
P2P Digital Currency
script_ops.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <script/script.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
9 
10 #include <cstdint>
11 #include <string>
12 #include <vector>
13 
14 FUZZ_TARGET(script_ops)
15 {
16  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
17  CScript script_mut = ConsumeScript(fuzzed_data_provider);
18  while (fuzzed_data_provider.remaining_bytes() > 0) {
19  CallOneOf(
20  fuzzed_data_provider,
21  [&] {
22  CScript s = ConsumeScript(fuzzed_data_provider);
23  script_mut = std::move(s);
24  },
25  [&] {
26  const CScript& s = ConsumeScript(fuzzed_data_provider);
27  script_mut = s;
28  },
29  [&] {
30  script_mut << fuzzed_data_provider.ConsumeIntegral<int64_t>();
31  },
32  [&] {
33  script_mut << ConsumeOpcodeType(fuzzed_data_provider);
34  },
35  [&] {
36  script_mut << ConsumeScriptNum(fuzzed_data_provider);
37  },
38  [&] {
39  script_mut << ConsumeRandomLengthByteVector(fuzzed_data_provider);
40  },
41  [&] {
42  script_mut.clear();
43  });
44  }
45  const CScript& script = script_mut;
46  (void)script.GetSigOpCount(false);
47  (void)script.GetSigOpCount(true);
48  (void)script.GetSigOpCount(script);
49  (void)script.HasValidOps();
50  (void)script.IsPayToScriptHash();
51  (void)script.IsPayToWitnessScriptHash();
52  (void)script.IsPushOnly();
53  (void)script.IsUnspendable();
54  {
55  CScript::const_iterator pc = script.begin();
56  opcodetype opcode;
57  (void)script.GetOp(pc, opcode);
58  std::vector<uint8_t> data;
59  (void)script.GetOp(pc, opcode, data);
60  (void)script.IsPushOnly(pc);
61  }
62  {
63  int version;
64  std::vector<uint8_t> program;
65  (void)script.IsWitnessProgram(version, program);
66  }
67 }
prevector::const_iterator
Definition: prevector.h:98
CScript::IsPayToWitnessScriptHash
bool IsPayToWitnessScriptHash() const
Definition: script.cpp:210
FUZZ_TARGET
FUZZ_TARGET(script_ops)
Definition: script_ops.cpp:14
CScript::IsWitnessProgram
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:220
CScript::HasValidOps
bool HasValidOps() const
Check if the script contains valid OP_CODES.
Definition: script.cpp:270
util.h
opcodetype
opcodetype
Script opcodes.
Definition: script.h:66
CScript::IsPayToScriptHash
bool IsPayToScriptHash() const
Definition: script.cpp:201
CScript::clear
void clear()
Definition: script.h:549
ConsumeOpcodeType
opcodetype ConsumeOpcodeType(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:122
ConsumeScript
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length, const bool maybe_p2wsh) noexcept
Definition: util.cpp:285
CScript::IsPushOnly
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:236
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
FuzzedDataProvider.h
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
script.h
FuzzedDataProvider::remaining_bytes
size_t remaining_bytes()
Definition: FuzzedDataProvider.h:84
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
CScript::GetSigOpCount
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:153
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
prevector::begin
iterator begin()
Definition: prevector.h:290
CScript::IsUnspendable
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:544
ConsumeScriptNum
CScriptNum ConsumeScriptNum(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:139
CScript::GetOp
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:487