Files
xahaud/include/xrpl/hook/guard_checker.cpp
Niq Dudfield a7900a7f36 Merge dev (d20927237) into sync-2.4.0: HookAPI refactor (#681)
* Hook API Refactor2: Amendment Guards (#621)

* Hook API Refactor3: Consolidate the Hook API definitions from Enum.h and ApplyHook.h into a single file. (#622)

* Hook API Refactoring / Unit Testing (#581)

* Hook API Refactor2: Amendment Guards (#621)

* Hook API Refactor3: Consolidate the Hook API definitions from Enum.h and ApplyHook.h into a single file. (#622)

* Hook API Refactoring / Unit Testing (#581)

* fix: update clang-format to v18 and fix include ordering

- Update verify-generated-headers CI to use clang-format 18 (matching
  clang-format.yml) instead of stale v10 which can't parse .clang-format
- Add .mise.toml for local clang-format 18 tooling
- Fix include ordering in cherry-picked files per clang-format 18

* chore: update levelization results for HookAPI changes

New loop: xrpl.hook <-> xrpld.app due to HookAPI.h including
Transaction.h from xrpld.app.

---------

Co-authored-by: tequ <git@tequ.dev>
2026-02-16 18:51:04 +10:00

104 lines
2.1 KiB
C++

#define GUARD_CHECKER_BUILD
#include "Enum.h"
#include "Guard.h"
#include <fcntl.h>
#include <iostream>
#include <optional>
#include <ostream>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string_view>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
int
main(int argc, char** argv)
{
const char* fin = 0;
if (argc > 2)
return fprintf(
stderr, "Guard Checker\n\tUsage: %s somefile.wasm\n", argv[0]);
else if (argc == 1)
fin = "-";
else
fin = argv[1];
int fd = 0;
if (strcmp(fin, "-") != 0)
fd = open(fin, O_RDONLY);
if (fd < 0)
return fprintf(stderr, "Could not open file for reading:`%s`\n", fin);
off_t len = fd == 0 ? 0 : lseek(fd, 0, SEEK_END);
if (fd != 0)
lseek(fd, 0, SEEK_SET);
int length_known = len > 0;
uint8_t hook_data[0x100000U];
uint8_t* ptr = hook_data;
size_t upto = 0;
fcntl(fd, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK);
while (ptr - hook_data < sizeof(hook_data))
{
if (length_known)
{
if (upto >= len)
break;
}
else
len = upto + 1;
size_t bytes_read = read(fd, ptr + upto, len - upto);
if (!length_known && bytes_read == 0)
break;
if (bytes_read < 0)
return fprintf(
stderr,
"Error reading file `%s`, only %ld bytes could be read\n",
fin,
upto);
upto += bytes_read;
}
std::vector<uint8_t> hook(upto);
memcpy(hook.data(), hook_data, upto);
printf("Read %ld bytes from `%s` successfully...\n", upto, fin);
close(fd);
// Dummy rules for guard checker build
hook_api::Rules rules;
auto result = validateGuards(
hook,
std::cout,
"",
hook_api::getImportWhitelist(rules),
hook_api::getGuardRulesVersion(rules));
if (!result)
{
printf("Hook validation failed.\n");
return 1;
}
printf("\nHook validation successful!\n");
return 0;
}