| // Copyright 2022 The Fuchsia Authors | |
| // | |
| // Use of this source code is governed by a MIT-style | |
| // license that can be found in the LICENSE file or at | |
| // https://opensource.org/licenses/MIT | |
| #include <inttypes.h> | |
| #include <lib/zbitl/error-stdio.h> | |
| #include <lib/zbitl/view.h> | |
| #include <stdio.h> | |
| #include <fbl/no_destructor.h> | |
| #include <ktl/byte.h> | |
| #include <ktl/span.h> | |
| #include <ktl/string_view.h> | |
| #include <phys/address-space/address-space.h> | |
| #include <phys/elf/image.h> | |
| #include <phys/kernel-package/kernel-package.h> | |
| #include <phys/runtime/self.h> | |
| #include <phys/runtime/symbolize.h> | |
| #include "../test-main.h" | |
| #include "test.h" | |
| #include <ktl/enforce.h> | |
| namespace { | |
| constexpr const char* kTestName = "elf-code-patching-test"; | |
| constexpr ktl::string_view kAddOne = "add-one"; | |
| constexpr ktl::string_view kMultiply = "multiply_by_factor"; | |
| constexpr ktl::string_view kAddValue = "add_value"; | |
| // The BOOTFS namespace under which kAddOne, kMultiply, and kAddValue live. | |
| constexpr ktl::string_view kNamespace = "elf-code-patching-test-data"; | |
| // Symbolizer module IDs for the loaded test modules (module 0 is this test). | |
| enum ModuleId : unsigned int { | |
| kUnpatchedAddOne = 1, | |
| kPatchedAddOne, | |
| kMultiplyBy2, | |
| kMultiplyBy10, | |
| kAddValue2, | |
| kAddValue3, | |
| kAddValue10, | |
| }; | |
| } // namespace | |
| int TestMain(void* zbi_ptr, ktl::optional<EarlyBootZbi> early_zbi, arch::EarlyTicks) { | |
| Symbolize symbolize(kTestName); | |
| // Initialize memory for allocation/free. | |
| AddressSpace aspace; | |
| InitMemory(zbi_ptr, ktl::move(early_zbi), &aspace); | |
| zbitl::View zbi( | |
| zbitl::StorageFromRawHeader<ktl::span<ktl::byte>>(static_cast<zbi_header_t*>(zbi_ptr))); | |
| KernelStorage kernelfs; | |
| kernelfs.Init(zbi); | |
| KernelStorage::Bootfs bootfs; | |
| if (auto result = kernelfs.root().subdir(kNamespace); result.is_error()) { | |
| zbitl::PrintBootfsError(result.error_value()); | |
| return 1; | |
| } else { | |
| bootfs = ktl::move(result).value(); | |
| } | |
| symbolize.Context(); | |
| auto emit_markup = [&symbolize](ModuleId module_id) { | |
| return [&symbolize, module_id](const ElfImage& image) { | |
| symbolize.ModuleContext(image, module_id); | |
| }; | |
| }; | |
| constexpr uint64_t kValue = 42; | |
| // Test that unpatched add-one loads and behaves as expected. | |
| // Note this keeps the Allocation alive after the test so its | |
| // pages won't be reused for the second test, since that could | |
| // require cache flushing operations. | |
| Allocation unpatched; | |
| printf("%s: Testing unpatched add-one...\n", symbolize.name()); | |
| { | |
| static fbl::NoDestructor<ElfImage> add_one; | |
| if (auto result = add_one->Init(bootfs, kAddOne, true); result.is_error()) { | |
| zbitl::PrintBootfsError(result.error_value()); | |
| return 1; | |
| } | |
| add_one->AssertInterpMatchesBuildId(kAddOne, *Self().build_id()); | |
| unpatched = | |
| add_one->Load(memalloc::Type::kPhysElf, emit_markup(ModuleId::kUnpatchedAddOne), {}, false); | |
| add_one->Relocate(); | |
| printf("%s: Calling %p...", symbolize.name(), add_one->ImageEntry<TestFn>()); | |
| uint64_t value = add_one->Call<TestFn>(kValue); | |
| ZX_ASSERT_MSG(value == kValue + 1, "unpatched add-one: got %" PRIu64 " != expected %" PRIu64, | |
| value, kValue + 1); | |
| } | |
| printf("OK\n"); | |
| // Now test it with nop patching: AddOne becomes the identity function. | |
| Allocation patched; | |
| printf("%s: Testing patched add-one...\n", symbolize.name()); | |
| { | |
| static fbl::NoDestructor<ElfImage> add_one; | |
| if (auto result = add_one->Init(bootfs, kAddOne, true); result.is_error()) { | |
| zbitl::PrintBootfsError(result.error_value()); | |
| return 1; | |
| } | |
| add_one->AssertInterpMatchesBuildId(kAddOne, *Self().build_id()); | |
| patched = | |
| add_one->Load(memalloc::Type::kPhysElf, emit_markup(ModuleId::kPatchedAddOne), {}, false); | |
| add_one->Relocate(); | |
| enum class ExpectedCase : uint32_t { kAddOne = kAddOneCaseId }; | |
| auto patch = [](code_patching::Patcher& patcher, ExpectedCase case_id, | |
| ktl::span<ktl::byte> code, auto&& print) -> fit::result<ElfImage::Error> { | |
| ZX_ASSERT_MSG(case_id == ExpectedCase::kAddOne, | |
| "code-patching case ID %" PRIu32 " != expected %" PRIu32, | |
| static_cast<uint32_t>(case_id), static_cast<uint32_t>(ExpectedCase::kAddOne)); | |
| ZX_ASSERT_MSG(code.size_bytes() == PATCH_SIZE_ADD_ONE, "code patch %zu bytes != expected %d", | |
| code.size_bytes(), PATCH_SIZE_ADD_ONE); | |
| print({"patched nop-fill"}); | |
| patcher.NopFill(code); | |
| return fit::ok(); | |
| }; | |
| auto result = add_one->ForEachPatch<ExpectedCase>(patch); | |
| ZX_ASSERT(result.is_ok()); | |
| printf("%s: Calling %p...", symbolize.name(), add_one->ImageEntry<TestFn>()); | |
| uint64_t value = add_one->Call<TestFn>(kValue); | |
| ZX_ASSERT_MSG(value == kValue, "nop-patched add-one: got %" PRIu64 " != expected %" PRIu64, | |
| value, kValue); | |
| } | |
| printf("OK\n"); | |
| // Now test the hermetic blob stub case. | |
| Allocation patched_stub2; | |
| printf("%s: Testing hermetic blob (alternative 1)...\n", symbolize.name()); | |
| { | |
| static fbl::NoDestructor<ElfImage> multiply; | |
| if (auto result = multiply->Init(bootfs, kMultiply, true); result.is_error()) { | |
| zbitl::PrintBootfsError(result.error_value()); | |
| return 1; | |
| } | |
| multiply->AssertInterpMatchesBuildId(kMultiply, *Self().build_id()); | |
| patched_stub2 = | |
| multiply->Load(memalloc::Type::kPhysElf, emit_markup(ModuleId::kMultiplyBy2), {}, false); | |
| multiply->Relocate(); | |
| enum class ExpectedCase : uint32_t { kMultiply = kMultiplyByFactorCaseId }; | |
| auto patch = [](code_patching::Patcher& patcher, ExpectedCase case_id, | |
| ktl::span<ktl::byte> code, auto&& print) -> fit::result<ElfImage::Error> { | |
| ZX_ASSERT_MSG(case_id == ExpectedCase::kMultiply, | |
| "code-patching case ID %" PRIu32 " != expected %" PRIu32, | |
| static_cast<uint32_t>(case_id), static_cast<uint32_t>(ExpectedCase::kMultiply)); | |
| ZX_ASSERT_MSG(code.size_bytes() == PATCH_SIZE_MULTIPLY_BY_FACTOR, | |
| "code patch %zu bytes != expected %d", code.size_bytes(), | |
| PATCH_SIZE_MULTIPLY_BY_FACTOR); | |
| print({"patch in multiply_by_two"}); | |
| return patcher.PatchWithAlternative(code, "multiply_by_two"); | |
| }; | |
| auto result = multiply->ForEachPatch<ExpectedCase>(patch); | |
| ZX_ASSERT_MSG(result.is_ok(), "%.*s", static_cast<int>(result.error_value().reason.size()), | |
| result.error_value().reason.data()); | |
| printf("%s: Calling %p...", symbolize.name(), multiply->ImageEntry<TestFn>()); | |
| uint64_t value = multiply->Call<TestFn>(kValue); | |
| ZX_ASSERT_MSG(value == kValue * 2, "multiply_by_two got %" PRIu64 " != expected %" PRIu64, | |
| value, kValue * 2); | |
| } | |
| printf("OK\n"); | |
| // Now test the hermetic blob stub case. | |
| Allocation patched_stub10; | |
| printf("%s: Testing hermetic blob (alternative 2)...\n", symbolize.name()); | |
| { | |
| static fbl::NoDestructor<ElfImage> multiply; | |
| if (auto result = multiply->Init(bootfs, kMultiply, true); result.is_error()) { | |
| zbitl::PrintBootfsError(result.error_value()); | |
| return 1; | |
| } | |
| multiply->AssertInterpMatchesBuildId(kMultiply, *Self().build_id()); | |
| patched_stub10 = | |
| multiply->Load(memalloc::Type::kPhysElf, emit_markup(ModuleId::kMultiplyBy10), {}, false); | |
| multiply->Relocate(); | |
| enum class ExpectedCase : uint32_t { kMultiply = kMultiplyByFactorCaseId }; | |
| auto patch = [](code_patching::Patcher& patcher, ExpectedCase case_id, | |
| ktl::span<ktl::byte> code, auto&& print) -> fit::result<ElfImage::Error> { | |
| ZX_ASSERT_MSG(case_id == ExpectedCase::kMultiply, | |
| "code-patching case ID %" PRIu32 " != expected %" PRIu32, | |
| static_cast<uint32_t>(case_id), static_cast<uint32_t>(ExpectedCase::kMultiply)); | |
| ZX_ASSERT_MSG(code.size_bytes() == PATCH_SIZE_MULTIPLY_BY_FACTOR, | |
| "code patch %zu bytes != expected %d", code.size_bytes(), | |
| PATCH_SIZE_MULTIPLY_BY_FACTOR); | |
| print({"patch in multiply_by_ten"}); | |
| return patcher.PatchWithAlternative(code, "multiply_by_ten"); | |
| }; | |
| auto result = multiply->ForEachPatch<ExpectedCase>(patch); | |
| ZX_ASSERT_MSG(result.is_ok(), "%.*s", static_cast<int>(result.error_value().reason.size()), | |
| result.error_value().reason.data()); | |
| printf("%s: Calling %p...", symbolize.name(), multiply->ImageEntry<TestFn>()); | |
| uint64_t value = multiply->Call<TestFn>(kValue); | |
| ZX_ASSERT_MSG(value == kValue * 10, "multiply_by_ten got %" PRIu64 " != expected %" PRIu64, | |
| value, kValue * 10); | |
| } | |
| printf("OK\n"); | |
| // Now test the multiversion hermetic blob stub cases. As above, each | |
| // Allocation is kept alive after its test so that its pages won't be reused | |
| // for the next test's code, since that could require cache flushing. | |
| auto test_add_value = [&](ktl::string_view alternative, uint64_t addend, ModuleId module_id, | |
| Allocation& loaded) -> bool { | |
| printf("%s: Testing multiversion hermetic blob (%.*s)...\n", symbolize.name(), | |
| static_cast<int>(alternative.size()), alternative.data()); | |
| ElfImage image; | |
| if (auto result = image.Init(bootfs, kAddValue, true); result.is_error()) { | |
| zbitl::PrintBootfsError(result.error_value()); | |
| return false; | |
| } | |
| image.AssertInterpMatchesBuildId(kAddValue, *Self().build_id()); | |
| loaded = image.Load(memalloc::Type::kPhysElf, emit_markup(module_id), {}, false); | |
| image.Relocate(); | |
| enum class ExpectedCase : uint32_t { kAddValue = kAddValueCaseId }; | |
| auto patch = [alternative](code_patching::Patcher& patcher, ExpectedCase case_id, | |
| ktl::span<ktl::byte> code, | |
| auto&& print) -> fit::result<ElfImage::Error> { | |
| ZX_ASSERT_MSG(case_id == ExpectedCase::kAddValue, | |
| "code-patching case ID %" PRIu32 " != expected %" PRIu32, | |
| static_cast<uint32_t>(case_id), static_cast<uint32_t>(ExpectedCase::kAddValue)); | |
| print({"patch in "sv, alternative}); | |
| return patcher.PatchWithAlternative(code, alternative); | |
| }; | |
| if (auto result = image.ForEachPatch<ExpectedCase>(patch); result.is_error()) { | |
| image.Printf(result.error_value()); | |
| return false; | |
| } | |
| printf("%s: Calling %p...", symbolize.name(), image.ImageEntry<TestFn>()); | |
| uint64_t value = image.Call<TestFn>(kValue); | |
| if (value != kValue + addend) { | |
| printf("FAILED: %.*s got %" PRIu64 " != expected %" PRIu64 "\n", | |
| static_cast<int>(alternative.size()), alternative.data(), value, kValue + addend); | |
| return false; | |
| } | |
| printf("OK\n"); | |
| return true; | |
| }; | |
| Allocation add_two, add_three, add_ten; | |
| bool ok = test_add_value("by-config/add_value"sv, 2, ModuleId::kAddValue2, add_two); | |
| ok = test_add_value("add_value"sv, 3, ModuleId::kAddValue3, add_three) && ok; | |
| ok = test_add_value("by-deps/add_ten"sv, 10, ModuleId::kAddValue10, add_ten) && ok; | |
| return ok ? 0 : 1; | |
| } |