blob: 84038cc52b6b75afa5ab4fae492e1f2850934ac2 [file]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests for the helpers in <lib/elfldltl/testing/fuzzer/...>.
#include <lib/elfldltl/constants.h>
#include <lib/elfldltl/layout.h>
#include <lib/elfldltl/machine.h>
#include <lib/elfldltl/testing/fuzzer/ehdr.h>
#include <lib/elfldltl/testing/fuzzer/machine.h>
#include <lib/elfldltl/testing/typed-test.h>
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <set>
#include <span>
#include <vector>
#include <fuzzer/FuzzedDataProvider.h>
#include <gtest/gtest.h>
namespace {
template <class Elf>
Elf::Ehdr ReadEhdr(std::span<const std::byte> image) {
// A short image (only on test failure) is padded out with zero bytes.
std::array<std::byte, sizeof(typename Elf::Ehdr)> bytes{};
EXPECT_GE(image.size(), bytes.size());
std::copy_n(image.begin(), std::min(image.size(), bytes.size()), bytes.begin());
typename Elf::Ehdr ehdr;
std::memcpy(&ehdr, bytes.data(), sizeof(ehdr));
return ehdr;
}
// An image starting with a zeroed (invalid) Ehdr and some trailing bytes.
template <class Elf>
std::vector<std::byte> ZeroImage() {
std::vector<std::byte> image(sizeof(typename Elf::Ehdr) + 16);
for (size_t i = sizeof(typename Elf::Ehdr); i < image.size(); ++i) {
image[i] = static_cast<std::byte>(i);
}
return image;
}
FORMAT_TYPED_TEST_SUITE(ElfldltlTestingEnsureEhdrTests);
TYPED_TEST(ElfldltlTestingEnsureEhdrTests, TooShort) {
using Elf = TestFixture::Elf;
const std::vector<std::byte> image(sizeof(typename Elf::Ehdr) - 1, std::byte{0x55});
EXPECT_EQ(elfldltl::testing::EnsureEhdr<Elf>(image), image);
EXPECT_EQ(elfldltl::testing::EnsureEhdr<Elf>(image, elfldltl::ElfType::kCore), image);
}
TYPED_TEST(ElfldltlTestingEnsureEhdrTests, Dyn) {
using Elf = TestFixture::Elf;
const std::vector<std::byte> image = ZeroImage<Elf>();
const std::vector<std::byte> normalized = elfldltl::testing::EnsureEhdr<Elf>(image);
ASSERT_EQ(normalized.size(), image.size());
const auto ehdr = ReadEhdr<Elf>(normalized);
EXPECT_TRUE(ehdr.Loadable());
EXPECT_EQ(ehdr.machine, elfldltl::ElfMachine::kNative);
EXPECT_EQ(ehdr.phoff, sizeof(typename Elf::Ehdr));
EXPECT_EQ(ehdr.phentsize, sizeof(typename Elf::Phdr));
// The bytes after the header are untouched.
EXPECT_TRUE(std::equal(normalized.begin() + sizeof(typename Elf::Ehdr), normalized.end(),
image.begin() + sizeof(typename Elf::Ehdr)));
// An acceptable header is left alone.
EXPECT_EQ(elfldltl::testing::EnsureEhdr<Elf>(normalized), normalized);
// An explicit machine is used for the rewritten header.
const auto riscv = ReadEhdr<Elf>(elfldltl::testing::EnsureEhdr<Elf>(
image, elfldltl::ElfType::kDyn, elfldltl::ElfMachine::kRiscv));
EXPECT_TRUE(riscv.Loadable(elfldltl::ElfMachine::kRiscv));
}
TYPED_TEST(ElfldltlTestingEnsureEhdrTests, Core) {
using Elf = TestFixture::Elf;
using Ehdr = Elf::Ehdr;
// Start with a fuzzed e_machine in an otherwise invalid header.
std::vector<std::byte> image = ZeroImage<Elf>();
Ehdr fuzzed = ReadEhdr<Elf>(image);
fuzzed.machine = elfldltl::ElfMachine::kAarch64;
fuzzed.phoff = 0x123;
std::memcpy(image.data(), &fuzzed, sizeof(fuzzed));
const std::vector<std::byte> normalized =
elfldltl::testing::EnsureEhdr<Elf>(image, elfldltl::ElfType::kCore);
const Ehdr ehdr = ReadEhdr<Elf>(normalized);
EXPECT_TRUE(ehdr.Valid());
EXPECT_EQ(ehdr.type, elfldltl::ElfType::kCore);
EXPECT_EQ(ehdr.phentsize, sizeof(typename Elf::Phdr));
EXPECT_EQ(ehdr.machine, elfldltl::ElfMachine::kAarch64); // Left as fuzzed.
EXPECT_EQ(ehdr.phoff, 0x123u); // Nonzero kept.
// An acceptable header is left alone, whatever its e_machine.
EXPECT_EQ(elfldltl::testing::EnsureEhdr<Elf>(normalized, elfldltl::ElfType::kCore), normalized);
// An explicit machine replaces the fuzzed one when the header is rewritten.
const Ehdr x86 = ReadEhdr<Elf>(elfldltl::testing::EnsureEhdr<Elf>(image, elfldltl::ElfType::kCore,
elfldltl::ElfMachine::kX86_64));
EXPECT_EQ(x86.type, elfldltl::ElfType::kCore);
EXPECT_EQ(x86.machine, elfldltl::ElfMachine::kX86_64);
// An explicit machine is also enforced on an otherwise acceptable header.
const Ehdr retargeted = ReadEhdr<Elf>(elfldltl::testing::EnsureEhdr<Elf>(
normalized, elfldltl::ElfType::kCore, elfldltl::ElfMachine::kRiscv));
EXPECT_EQ(retargeted.type, elfldltl::ElfType::kCore);
EXPECT_EQ(retargeted.machine, elfldltl::ElfMachine::kRiscv);
EXPECT_EQ(retargeted.phoff, 0x123u);
// An acceptable header that already has the requested machine is unchanged.
EXPECT_EQ(elfldltl::testing::EnsureEhdr<Elf>(normalized, elfldltl::ElfType::kCore,
elfldltl::ElfMachine::kAarch64),
normalized);
}
TEST(ElfldltlTestingMachineTests, ConsumeElfMachine) {
constexpr const auto& kSupported =
elfldltl::AllSupportedMachines<elfldltl::testing::FuzzerMachineList>::kMachines;
// An exhausted provider falls back to a supported machine rather than EM_NONE.
FuzzedDataProvider empty(nullptr, 0);
EXPECT_NE(std::ranges::find(kSupported, elfldltl::testing::ConsumeElfMachine(empty)),
kSupported.end());
// Sweep all two-byte inputs. Every supported machine and at least one
// unsupported value should come out.
std::set<elfldltl::ElfMachine> seen_supported, seen_other;
for (uint32_t i = 0; i <= 0xffff; ++i) {
const uint8_t lo = static_cast<uint8_t>(i & 0xffu);
const uint8_t hi = static_cast<uint8_t>(i >> 8u);
const std::array<uint8_t, 2> data{lo, hi};
FuzzedDataProvider provider(data.data(), data.size());
const elfldltl::ElfMachine machine = elfldltl::testing::ConsumeElfMachine(provider);
if (std::ranges::find(kSupported, machine) != kSupported.end()) {
seen_supported.insert(machine);
} else {
seen_other.insert(machine);
}
}
EXPECT_EQ(seen_supported.size(), kSupported.size());
EXPECT_FALSE(seen_other.empty());
}
} // namespace