2022-07-03 12:12:20 +02:00
|
|
|
// SPDX-FileCopyrightText: 2022 merryhime
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
2022-07-02 21:31:43 +02:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
2022-07-03 12:12:20 +02:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2022-07-02 21:31:43 +02:00
|
|
|
#include <libkern/OSCacheControl.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "oaknut/oaknut.hpp"
|
|
|
|
|
|
2022-07-03 12:12:20 +02:00
|
|
|
TEST_CASE("Basic Test")
|
2022-07-02 21:31:43 +02:00
|
|
|
{
|
|
|
|
|
const size_t page_size = getpagesize();
|
|
|
|
|
std::printf("page size: %zu\n", page_size);
|
|
|
|
|
|
|
|
|
|
std::uint32_t* mem = (std::uint32_t*)mmap(nullptr, page_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_JIT | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
|
|
|
|
|
|
|
|
|
pthread_jit_write_protect_np(false);
|
|
|
|
|
|
|
|
|
|
using namespace oaknut;
|
|
|
|
|
using namespace oaknut::util;
|
|
|
|
|
|
|
|
|
|
CodeGenerator code{mem};
|
|
|
|
|
code.MOVZ(W0, 42);
|
|
|
|
|
code.RET(X30);
|
|
|
|
|
|
|
|
|
|
pthread_jit_write_protect_np(true);
|
|
|
|
|
sys_icache_invalidate(mem, page_size);
|
|
|
|
|
|
2022-07-03 12:12:20 +02:00
|
|
|
int result = ((int (*)())mem)();
|
|
|
|
|
REQUIRE(result == 42);
|
2022-07-02 21:31:43 +02:00
|
|
|
}
|