Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7690add0 authored by Devin Moore's avatar Devin Moore
Browse files

Check for leaking FDs in binder_parcel_fuzzer

Ignore-AOSP-First: na
Flag: TEST_ONLY
Test: binder_parcel_fuzzer
Bug: 406499223
Change-Id: I65aa5ebd753c58041feb77c5b2de8abc3ca677a5
parent a0d6a716
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -28,10 +28,11 @@
#include <fuzzbinder/random_parcel.h>
#include <fuzzer/FuzzedDataProvider.h>

#include <cstdlib>
#include <ctime>
#include <sys/resource.h>
#include <sys/time.h>
#include <cstdlib>
#include <ctime>
#include <filesystem>

#include "../../Utils.h"

@@ -157,12 +158,21 @@ static AIBinder_Class* kNothingClass =
        AIBinder_Class_define("nothing", NothingClass_onCreate, NothingClass_onDestroy,
                              NothingClass_onTransact);

static long numFds() {
    return std::distance(std::filesystem::directory_iterator("/proc/self/fd"),
                         std::filesystem::directory_iterator{});
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    if (size <= 1) return 0;  // no use

    // avoid timeouts, see b/142617274, b/142473153
    if (size > 50000) return 0;

    struct rlimit limit{};
    CHECK_EQ(0, getrlimit(RLIMIT_NOFILE, &limit));
    uint64_t maxFds = limit.rlim_cur;
    int initialFds = numFds();

    FuzzedDataProvider provider = FuzzedDataProvider(data, size);

    const std::function<void(FuzzedDataProvider&&)> fuzzBackend[] = {
@@ -209,5 +219,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {

    provider.PickValueInArray(fuzzBackend)(std::move(provider));

    CHECK_EQ(initialFds, numFds()) << "FDs are being leaked";
    return 0;
}