Avoid use of low file descriptor ids (which may be in use) in FuzzedSock
.
Context: https://github.com/bitcoin/bitcoin/pull/21630/files#r610694541
Avoid use of low file descriptor ids (which may be in use) in FuzzedSock
.
Context: https://github.com/bitcoin/bitcoin/pull/21630/files#r610694541
pipe()
or /dev/null
etc, and use that? Or do you use the fact that it needs to be a non-allocated file descriptor somewhere?
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
576@@ -577,15 +577,15 @@ class FuzzedSock : public Sock
577 public:
578 explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
579 {
580- m_socket = fuzzed_data_provider.ConsumeIntegral<SOCKET>();
581+ m_socket = fuzzed_data_provider.ConsumeIntegralInRange<SOCKET>(INVALID_SOCKET - 1, INVALID_SOCKET);
Maybe this should receive similar treatment:
… could you open a (real) file descriptor, connected to …
Right! It does not need to be connected to anything. Just reuse this:
plus a close()
in the FuzzedSocket
destructor.
Concept ACK but this seems a bit… arbitrary. If you want to be 100% certain to not step on an existing one, could you open a (real) file descriptor, connected to something like a
pipe()
or/dev/null
etc, and use that? Or do you use the fact that it needs to be a non-allocated file descriptor somewhere?
The nice thing about going with INVALID_SOCKET - 1
is that it doesn’t involve slowdowns due to syscalls, and that reading or writing from fd INVALID_SOCKET - 1
is guaranteed to fail (unless the fuzzing harness has four billion open file descriptors :)).
Note that INVALID_SOCKET - 1
is 4 294 967 294, and that POSIX requires file descriptor id re-use on close
.
Note that this PR is only a short-term bug fix to avoid file descriptors stdin (0), stdout (1), stderr (2) and other likely to be used file descriptors while awaiting vasild’s suggested cleanup/refactoring.
The chosen constant INVALID_SOCKET - 1
is bigger than FD_SETSIZE
, so those dummy sockets are not selectable:
I think that’s ok. I am working on a change to convert IsSelectableSocket(bare SOCKET or sock.Get())
to sock.IsSelectable()
so that the mocked sockets could override that.
practicalswift
laanwj
DrahtBot
vasild
Labels
Tests