diff options
| -rw-r--r-- | libs/binder/tests/parcel_fuzzer/random_fd.cpp | 93 |
1 files changed, 59 insertions, 34 deletions
diff --git a/libs/binder/tests/parcel_fuzzer/random_fd.cpp b/libs/binder/tests/parcel_fuzzer/random_fd.cpp index e4dbb2d955..7390d497c4 100644 --- a/libs/binder/tests/parcel_fuzzer/random_fd.cpp +++ b/libs/binder/tests/parcel_fuzzer/random_fd.cpp @@ -29,40 +29,65 @@ std::vector<unique_fd> getRandomFds(FuzzedDataProvider* provider) { const char* fdType; std::vector<unique_fd> fds = provider->PickValueInArray< - std::function<std::vector<unique_fd>()>>({ - [&]() { - fdType = "ashmem"; - std::vector<unique_fd> ret; - ret.push_back(unique_fd( - ashmem_create_region("binder test region", - provider->ConsumeIntegralInRange<size_t>(0, 4096)))); - return ret; - }, - [&]() { - fdType = "/dev/null"; - std::vector<unique_fd> ret; - ret.push_back(unique_fd(open("/dev/null", O_RDWR))); - return ret; - }, - [&]() { - fdType = "pipefd"; - - int pipefds[2]; - - int flags = O_CLOEXEC; - if (provider->ConsumeBool()) flags |= O_DIRECT; - if (provider->ConsumeBool()) flags |= O_NONBLOCK; - - CHECK_EQ(0, pipe2(pipefds, flags)) << flags; - - if (provider->ConsumeBool()) std::swap(pipefds[0], pipefds[1]); - - std::vector<unique_fd> ret; - ret.push_back(unique_fd(pipefds[0])); - ret.push_back(unique_fd(pipefds[1])); - return ret; - }, - })(); + std::function<std::vector<unique_fd>()>>( + {[&]() { + fdType = "ashmem"; + std::vector<unique_fd> ret; + ret.push_back(unique_fd( + ashmem_create_region("binder test region", + provider->ConsumeIntegralInRange<size_t>(0, 4096)))); + return ret; + }, + [&]() { + fdType = "/dev/null"; + std::vector<unique_fd> ret; + ret.push_back(unique_fd(open("/dev/null", O_RDWR))); + return ret; + }, + [&]() { + fdType = "pipefd"; + + int pipefds[2]; + + int flags = O_CLOEXEC; + if (provider->ConsumeBool()) flags |= O_DIRECT; + if (provider->ConsumeBool()) flags |= O_NONBLOCK; + + CHECK_EQ(0, pipe2(pipefds, flags)) << flags; + + if (provider->ConsumeBool()) std::swap(pipefds[0], pipefds[1]); + + std::vector<unique_fd> ret; + ret.push_back(unique_fd(pipefds[0])); + ret.push_back(unique_fd(pipefds[1])); + return ret; + }, + [&]() { + fdType = "tempfd"; + char name[PATH_MAX]; +#if defined(__ANDROID__) + snprintf(name, sizeof(name), "/data/local/tmp/android-tempfd-test-%d-XXXXXX", + getpid()); +#else + snprintf(name, sizeof(name), "/tmp/android-tempfd-test-%d-XXXXXX", getpid()); +#endif + int fd = mkstemp(name); + CHECK_NE(fd, -1) << "Failed to create file " << name << ", errno: " << errno; + unlink(name); + if (provider->ConsumeBool()) { + CHECK_NE(TEMP_FAILURE_RETRY( + ftruncate(fd, + provider->ConsumeIntegralInRange<size_t>(0, 4096))), + -1) + << "Failed to truncate file, errno: " << errno; + } + + std::vector<unique_fd> ret; + ret.push_back(unique_fd(fd)); + return ret; + } + + })(); for (const auto& fd : fds) CHECK(fd.ok()) << fd.get() << " " << fdType; |