diff options
| author | 2023-09-12 17:08:36 +0000 | |
|---|---|---|
| committer | 2023-09-12 18:01:16 +0000 | |
| commit | 3d3f8961e9c00b6f9cca81faf7095e9a24104d51 (patch) | |
| tree | 99db9e13f3b71012dd5c33ad2394b0ec06c8705c | |
| parent | 66a0d71aff19e5ed4b9d820607b8a3fef1d9f384 (diff) | |
Add tmp file to ramdom_fd for parcel fuzzing
This happens on host under ashmem_create_region, but not on device. Add
it as an explicit option to get more coverage on device.
Test: binder_parcel_fuzzer
Bug: 294593418
Change-Id: Icfb7c91543540d2b16df791b659fef414c8ec563
| -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; |