mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-05-17 23:56:39 +00:00
fuzz: implement unimplemented FuzzedSock methods
We want `Get()` to always return the same value, otherwise it will look like the `FuzzedSock` implementation itself is broken. So assign `m_socket` a random number in the `FuzzedSock` constructor. There is nothing to fuzz about the `Get()` and `Release()` methods, so use the ones from the base class `Sock`. `Reset()` is just setting our socket to `INVALID_SOCKET`. We don't want to use the base `Reset()` because it will close `m_socket` and given that our `m_socket` is just a random number it may end up closing a real opened file descriptor if it coincides with our random `m_socket`.
This commit is contained in:
parent
1b6c463e03
commit
9b05c49ade
@ -558,33 +558,27 @@ class FuzzedSock : public Sock
|
|||||||
public:
|
public:
|
||||||
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
|
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
|
||||||
{
|
{
|
||||||
|
m_socket = fuzzed_data_provider.ConsumeIntegral<SOCKET>();
|
||||||
}
|
}
|
||||||
|
|
||||||
~FuzzedSock() override
|
~FuzzedSock() override
|
||||||
{
|
{
|
||||||
|
// Sock::~Sock() will be called after FuzzedSock::~FuzzedSock() and it will call
|
||||||
|
// Sock::Reset() (not FuzzedSock::Reset()!) which will call CloseSocket(m_socket).
|
||||||
|
// Avoid closing an arbitrary file descriptor (m_socket is just a random number which
|
||||||
|
// may concide with a real opened file descriptor).
|
||||||
|
Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
FuzzedSock& operator=(Sock&& other) override
|
FuzzedSock& operator=(Sock&& other) override
|
||||||
{
|
{
|
||||||
assert(false && "Not implemented yet.");
|
assert(false && "Move of Sock into FuzzedSock not allowed.");
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
SOCKET Get() const override
|
|
||||||
{
|
|
||||||
assert(false && "Not implemented yet.");
|
|
||||||
return INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
SOCKET Release() override
|
|
||||||
{
|
|
||||||
assert(false && "Not implemented yet.");
|
|
||||||
return INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Reset() override
|
void Reset() override
|
||||||
{
|
{
|
||||||
assert(false && "Not implemented yet.");
|
m_socket = INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t Send(const void* data, size_t len, int flags) const override
|
ssize_t Send(const void* data, size_t len, int flags) const override
|
||||||
@ -667,6 +661,14 @@ public:
|
|||||||
{
|
{
|
||||||
return m_fuzzed_data_provider.ConsumeBool();
|
return m_fuzzed_data_provider.ConsumeBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IsConnected(std::string& errmsg) const override {
|
||||||
|
if (m_fuzzed_data_provider.ConsumeBool()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
errmsg = "disconnected at random by the fuzzer";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
[[nodiscard]] inline FuzzedSock ConsumeSock(FuzzedDataProvider& fuzzed_data_provider)
|
[[nodiscard]] inline FuzzedSock ConsumeSock(FuzzedDataProvider& fuzzed_data_provider)
|
||||||
|
@ -153,7 +153,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
virtual bool IsConnected(std::string& errmsg) const;
|
virtual bool IsConnected(std::string& errmsg) const;
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
/**
|
/**
|
||||||
* Contained socket. `INVALID_SOCKET` designates the object is empty.
|
* Contained socket. `INVALID_SOCKET` designates the object is empty.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user