Skip to content

Commit 173cf31

Browse files
committed
Merge bitcoin#20839: fuzz: Avoid extraneous copy of input data, using Span<>
faf7d74 fuzz: Avoid extraneous copy of input data, using Span<> (MarcoFalke) Pull request description: Seeing speedup here in the fuzz framework part (non-fuzz-target part). Speedup is only visible for input data larger than 100kB. ACKs for top commit: practicalswift: cr ACK faf7d74: patch looks correct :) laanwj: Code review ACK faf7d74 Tree-SHA512: 41af7118846e0dfee237a6d5269a6c7cfbc775d7bd1cc2a85814cb60f6c2b37fe7fd35f1a788d4f08e6e0202c48b71054b67d2931160c445c79fc59e5347dadf
2 parents b829894 + faf7d74 commit 173cf31

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

src/test/fuzz/deserialize.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
#include <stdint.h>
3131
#include <unistd.h>
3232

33-
#include <vector>
34-
3533
#include <test/fuzz/fuzz.h>
3634

3735
void initialize_deserialize()
@@ -71,7 +69,7 @@ T Deserialize(CDataStream ds)
7169
}
7270

7371
template <typename T>
74-
void DeserializeFromFuzzingInput(const std::vector<uint8_t>& buffer, T& obj, const Optional<int> protocol_version = nullopt)
72+
void DeserializeFromFuzzingInput(FuzzBufferType buffer, T& obj, const Optional<int> protocol_version = nullopt)
7573
{
7674
CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
7775
if (protocol_version) {

src/test/fuzz/fuzz.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ static bool read_stdin(std::vector<uint8_t>& data)
5959
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
6060
{
6161
static const auto& test_one_input = *Assert(g_test_one_input);
62-
const std::vector<uint8_t> input(data, data + size);
63-
test_one_input(input);
62+
test_one_input({data, size});
6463
return 0;
6564
}
6665

src/test/fuzz/fuzz.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
#ifndef BITCOIN_TEST_FUZZ_FUZZ_H
66
#define BITCOIN_TEST_FUZZ_FUZZ_H
77

8+
#include <span.h>
9+
810
#include <cstdint>
911
#include <functional>
1012
#include <string_view>
11-
#include <vector>
1213

13-
using TypeTestOneInput = std::function<void(const std::vector<uint8_t>&)>;
14+
using FuzzBufferType = Span<const uint8_t>;
15+
16+
using TypeTestOneInput = std::function<void(FuzzBufferType)>;
1417
using TypeInitialize = std::function<void()>;
1518

1619
void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, TypeInitialize init);
@@ -21,13 +24,13 @@ inline void FuzzFrameworkEmptyFun() {}
2124
FUZZ_TARGET_INIT(name, FuzzFrameworkEmptyFun)
2225

2326
#define FUZZ_TARGET_INIT(name, init_fun) \
24-
void name##_fuzz_target(const std::vector<uint8_t>&); \
27+
void name##_fuzz_target(FuzzBufferType); \
2528
struct name##_Before_Main { \
2629
name##_Before_Main() \
2730
{ \
2831
FuzzFrameworkRegisterTarget(#name, name##_fuzz_target, init_fun); \
2932
} \
3033
} const static g_##name##_before_main; \
31-
void name##_fuzz_target(const std::vector<uint8_t>& buffer)
34+
void name##_fuzz_target(FuzzBufferType buffer)
3235

3336
#endif // BITCOIN_TEST_FUZZ_FUZZ_H

src/test/fuzz/process_message.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <iostream>
3131
#include <memory>
3232
#include <string>
33-
#include <vector>
3433

3534
namespace {
3635
const TestingSetup* g_setup;
@@ -46,7 +45,7 @@ void initialize_process_message()
4645
SyncWithValidationInterfaceQueue();
4746
}
4847

49-
void fuzz_target(const std::vector<uint8_t>& buffer, const std::string& LIMIT_TO_MESSAGE_TYPE)
48+
void fuzz_target(FuzzBufferType buffer, const std::string& LIMIT_TO_MESSAGE_TYPE)
5049
{
5150
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
5251

0 commit comments

Comments
 (0)