85 lines
2.9 KiB
C++
85 lines
2.9 KiB
C++
// E2E: structural blob validation — confirms the Windows (MZ) and macOS
|
|
// (Mach-O 64-bit) sections inside a fat .vls have correct magic bytes.
|
|
// This runs on Linux and validates cross-platform blobs without executing them.
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <vector>
|
|
|
|
#include "format/vlc_format.hpp"
|
|
|
|
#ifndef VOIDLIBC_TEST_VLC_PATH
|
|
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
|
#endif
|
|
|
|
using namespace voidlibc::format;
|
|
|
|
#define REQUIRE(cond) do { \
|
|
if (!(cond)) { \
|
|
std::fprintf(stderr, "FAIL: %s (line %d)\n", #cond, \
|
|
__LINE__); \
|
|
std::exit(1); \
|
|
} \
|
|
} while (0)
|
|
|
|
static std::vector<std::uint8_t> read_section_bytes(
|
|
std::ifstream& f, const SectionEntry& sec) {
|
|
f.seekg(static_cast<std::streamoff>(sec.code_off));
|
|
std::vector<std::uint8_t> buf(static_cast<std::size_t>(sec.code_size));
|
|
f.read(reinterpret_cast<char*>(buf.data()),
|
|
static_cast<std::streamsize>(buf.size()));
|
|
return buf;
|
|
}
|
|
|
|
int main() {
|
|
std::ifstream f(VOIDLIBC_TEST_VLC_PATH, std::ios::binary);
|
|
REQUIRE(f.is_open());
|
|
|
|
FileHeader hdr{};
|
|
f.read(reinterpret_cast<char*>(&hdr), sizeof(hdr));
|
|
REQUIRE(f.good());
|
|
REQUIRE(hdr.magic == kMagic);
|
|
REQUIRE(hdr.section_count >= 3); // must have linux + windows + macos
|
|
|
|
f.seekg(static_cast<std::streamoff>(hdr.section_dir_off));
|
|
std::vector<SectionEntry> sects(hdr.section_count);
|
|
f.read(reinterpret_cast<char*>(sects.data()),
|
|
static_cast<std::streamsize>(hdr.section_count * sizeof(SectionEntry)));
|
|
REQUIRE(f.good());
|
|
|
|
bool saw_linux = false, saw_windows = false, saw_macos = false;
|
|
|
|
for (const auto& sec : sects) {
|
|
REQUIRE(sec.kind == SectionKind::NativeBlob);
|
|
REQUIRE(sec.code_size >= 4);
|
|
auto bytes = read_section_bytes(f, sec);
|
|
REQUIRE(bytes.size() >= 4);
|
|
|
|
if (sec.target_os == TargetOS::Linux) {
|
|
// ELF magic: \x7F E L F
|
|
REQUIRE(bytes[0] == 0x7F && bytes[1] == 'E' &&
|
|
bytes[2] == 'L' && bytes[3] == 'F');
|
|
saw_linux = true;
|
|
} else if (sec.target_os == TargetOS::Windows) {
|
|
// PE: MZ header
|
|
REQUIRE(bytes[0] == 'M' && bytes[1] == 'Z');
|
|
saw_windows = true;
|
|
} else if (sec.target_os == TargetOS::MacOS) {
|
|
// Mach-O 64-bit LE: 0xCFFAEDFE
|
|
REQUIRE(bytes[0] == 0xCF && bytes[1] == 0xFA &&
|
|
bytes[2] == 0xED && bytes[3] == 0xFE);
|
|
saw_macos = true;
|
|
}
|
|
}
|
|
|
|
REQUIRE(saw_linux);
|
|
REQUIRE(saw_windows);
|
|
REQUIRE(saw_macos);
|
|
|
|
std::printf("PASS: e2e_blob_validate — ELF/PE/Mach-O magic bytes all correct\n");
|
|
return 0;
|
|
}
|