| // 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. | |
| // Minimal test inferior for zxdump_collector_unittest.cc. Stores a known 64-bit value in | |
| // writable memory, maps read-only and unpopulated anonymous pages, prints their addresses on | |
| // stdout, and echoes stdin bytes to stdout so tests can verify the child resumed after a dump. | |
| #include <sys/mman.h> | |
| #include <unistd.h> | |
| #include <cstdint> | |
| #include <cstdio> | |
| #include <print> | |
| namespace { | |
| constexpr uint64_t kMagic = 0x12345678DEADBEEFull; | |
| volatile uint64_t g_magic = kMagic; | |
| } // namespace | |
| int main() { | |
| const int64_t page_size = sysconf(_SC_PAGESIZE); | |
| const size_t map_size = page_size > 0 ? static_cast<size_t>(page_size) : 4096zu; | |
| void* readonly = mmap(nullptr, map_size, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| if (readonly != MAP_FAILED) { | |
| std::println("zxdump_test_child: readonly_addr={:#x}", reinterpret_cast<uintptr_t>(readonly)); | |
| } | |
| void* unpopulated = | |
| mmap(nullptr, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| if (unpopulated != MAP_FAILED) { | |
| std::println("zxdump_test_child: unpopulated_addr={:#x}", | |
| reinterpret_cast<uintptr_t>(unpopulated)); | |
| } | |
| std::println("zxdump_test_child: magic_addr={:#x}", reinterpret_cast<uintptr_t>(&g_magic)); | |
| fflush(stdout); | |
| char byte = 0; | |
| while (read(STDIN_FILENO, &byte, 1) == 1) { | |
| if (write(STDOUT_FILENO, &byte, 1) != 1) { | |
| break; | |
| } | |
| } | |
| } |