Repository: bitcoin/bitcoin (language: cpp)

Issue:
docs: "Fuzzing the Bitcoin Core P2P layer using Honggfuzz NetDriver" is outdated
Due to changes to the functions `V1TransportDeserializer::readHeader` and `V1TransportDeserializer::GetMessage`, the "Fuzzing the Bitcoin Core P2P layer using Honggfuzz NetDriver" section in `fuzzing.md` is outdated. The `git apply` command doesn't work.

```sh
$ git apply << "EOF"
diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp
index 455a82e39..2faa3f80f 100644
--- a/src/bitcoind.cpp
+++ b/src/bitcoind.cpp
@@ -158,7 +158,11 @@ static bool AppInit(int argc, char* argv[])
     return fRet;
 }

+#ifdef HFND_FUZZING_ENTRY_FUNCTION_CXX
+HFND_FUZZING_ENTRY_FUNCTION_CXX(int argc, char* argv[])
+#else
 int main(int argc, char* argv[])
+#endif
 {
 #ifdef WIN32
     util::WinCmdLineArgs winArgs;
diff --git a/src/net.cpp b/src/net.cpp
index cf987b699..636a4176a 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -709,7 +709,7 @@ int V1TransportDeserializer::readHeader(const char *pch, unsigned int nBytes)
     }

     // Check start string, network magic
-    if (memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
+    if (false && memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) { // skip network magic checking
         LogPrint(BCLog::NET, "HEADER ERROR - MESSAGESTART (%s, %u bytes), received %s, peer=%d\n", hdr.GetCommand(), hdr.nMessageSize, HexStr(hdr.pchMessageStart), m_node_id);
         return -1;
     }
@@ -768,7 +768,7 @@ Optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::mic
     RandAddEvent(ReadLE32(hash.begin()));

     // Check checksum and header command string
-    if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) {
+    if (false && memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) { // skip checksum checking
         LogPrint(BCLog::NET, "CHECKSUM ERROR (%s, %u bytes), expected %s was %s, peer=%d\n",
                  SanitizeString(msg->m_command), msg->m_message_size,
                  HexStr(Span<uint8_t>(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)),
EOF
```


Discussion / hints:

Fix patch:
diff --git a/doc/fuzzing.md b/doc/fuzzing.md
index 84ebb0986d266..a4b0198dd99fe 100644
--- a/doc/fuzzing.md
+++ b/doc/fuzzing.md
@@ -219,44 +219,45 @@ $ CC=$(pwd)/honggfuzz/hfuzz_cc/hfuzz-clang \
       ./configure --disable-wallet --with-gui=no \
                   --with-sanitizers=address,undefined
 $ git apply << "EOF"
-diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp
-index 455a82e39..2faa3f80f 100644
---- a/src/bitcoind.cpp
-+++ b/src/bitcoind.cpp
-@@ -158,7 +158,11 @@ static bool AppInit(int argc, char* argv[])
-     return fRet;
- }
-
+diff --git a/src/compat/compat.h b/src/compat/compat.h
+index 8195bceaec..cce2b31ff0 100644
+--- a/src/compat/compat.h
++++ b/src/compat/compat.h
+@@ -90,8 +90,12 @@ typedef char* sockopt_arg_type;
+ // building with a binutils < 2.36 is subject to this ld bug.
+ #define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])
+ #else
 +#ifdef HFND_FUZZING_ENTRY_FUNCTION_CXX
-+HFND_FUZZING_ENTRY_FUNCTION_CXX(int argc, char* argv[])
++#define MAIN_FUNCTION HFND_FUZZING_ENTRY_FUNCTION_CXX(int argc, char* argv[])
 +#else
- int main(int argc, char* argv[])
+ #define MAIN_FUNCTION int main(int argc, char* argv[])
+ #endif
 +#endif
- {
- #ifdef WIN32
-     util::WinCmdLineArgs winArgs;
+
+ // Note these both should work with the current usage of poll, but best to be safe
+ // WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
 diff --git a/src/net.cpp b/src/net.cpp
-index cf987b699..636a4176a 100644
+index 7601a6ea84..702d0f56ce 100644
 --- a/src/net.cpp
 +++ b/src/net.cpp
-@@ -709,7 +709,7 @@ int V1TransportDeserializer::readHeader(const char *pch, unsigned int nBytes)
+@@ -727,7 +727,7 @@ int V1TransportDeserializer::readHeader(Span<const uint8_t> msg_bytes)
      }
 
      // Check start string, network magic
 -    if (memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
 +    if (false && memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) { // skip network magic checking
-         LogPrint(BCLog::NET, "HEADER ERROR - MESSAGESTART (%s, %u bytes), received %s, peer=%d\n", hdr.GetCommand(), hdr.nMessageSize, HexStr(hdr.pchMessageStart), m_node_id);
+         LogPrint(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
          return -1;
      }
-@@ -768,7 +768,7 @@ Optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::mic
+@@ -788,7 +788,7 @@ CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds
      RandAddEvent(ReadLE32(hash.begin()));
 
-     // Check checksum and header command string
+     // Check checksum and header message type string
 -    if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) {
 +    if (false && memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) { // skip checksum checking
-         LogPrint(BCLog::NET, "CHECKSUM ERROR (%s, %u bytes), expected %s was %s, peer=%d\n",
-                  SanitizeString(msg->m_command), msg->m_message_size,
-                  HexStr(Span<uint8_t>(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)),
+         LogPrint(BCLog::NET, "Header error: Wrong checksum (%s, %u bytes), expected %s was %s, peer=%d\n",
+                  SanitizeString(msg.m_type), msg.m_message_size,
+                  HexStr(Span{hash}.first(CMessageHeader::CHECKSUM_SIZE)),
 EOF
 $ make -C src/ bitcoind
 $ mkdir -p inputs/


Test patch: