Skip to content

Commit 9b05c49

Browse files
committed
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`.
1 parent 1b6c463 commit 9b05c49

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/test/fuzz/util.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -558,33 +558,27 @@ class FuzzedSock : public Sock
558558
public:
559559
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
560560
{
561+
m_socket = fuzzed_data_provider.ConsumeIntegral<SOCKET>();
561562
}
562563

563564
~FuzzedSock() override
564565
{
566+
// Sock::~Sock() will be called after FuzzedSock::~FuzzedSock() and it will call
567+
// Sock::Reset() (not FuzzedSock::Reset()!) which will call CloseSocket(m_socket).
568+
// Avoid closing an arbitrary file descriptor (m_socket is just a random number which
569+
// may concide with a real opened file descriptor).
570+
Reset();
565571
}
566572

567573
FuzzedSock& operator=(Sock&& other) override
568574
{
569-
assert(false && "Not implemented yet.");
575+
assert(false && "Move of Sock into FuzzedSock not allowed.");
570576
return *this;
571577
}
572578

573-
SOCKET Get() const override
574-
{
575-
assert(false && "Not implemented yet.");
576-
return INVALID_SOCKET;
577-
}
578-
579-
SOCKET Release() override
580-
{
581-
assert(false && "Not implemented yet.");
582-
return INVALID_SOCKET;
583-
}
584-
585579
void Reset() override
586580
{
587-
assert(false && "Not implemented yet.");
581+
m_socket = INVALID_SOCKET;
588582
}
589583

590584
ssize_t Send(const void* data, size_t len, int flags) const override
@@ -667,6 +661,14 @@ class FuzzedSock : public Sock
667661
{
668662
return m_fuzzed_data_provider.ConsumeBool();
669663
}
664+
665+
bool IsConnected(std::string& errmsg) const override {
666+
if (m_fuzzed_data_provider.ConsumeBool()) {
667+
return true;
668+
}
669+
errmsg = "disconnected at random by the fuzzer";
670+
return false;
671+
}
670672
};
671673

672674
[[nodiscard]] inline FuzzedSock ConsumeSock(FuzzedDataProvider& fuzzed_data_provider)

src/util/sock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class Sock
153153
*/
154154
virtual bool IsConnected(std::string& errmsg) const;
155155

156-
private:
156+
protected:
157157
/**
158158
* Contained socket. `INVALID_SOCKET` designates the object is empty.
159159
*/

0 commit comments

Comments
 (0)