Bitcoin Core  24.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  LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 1000000) {
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 }
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:236
bool IsPayToScriptHash() const
Definition: script.cpp:201
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:549
void clear()
Definition: script.h:554
bool IsPayToWitnessScriptHash() const
Definition: script.cpp:210
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:153
bool HasValidOps() const
Check if the script contains valid OP_CODES.
Definition: script.cpp:270
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:492
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:220
iterator begin()
Definition: prevector.h:292
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
opcodetype
Script opcodes.
Definition: script.h:70
FUZZ_TARGET(script_ops)
Definition: script_ops.cpp:14
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
Definition: util.cpp:366
CScriptNum ConsumeScriptNum(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:187
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:109
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:88
opcodetype ConsumeOpcodeType(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:170