Pipe (Part 2); This Needs Work

This commit is contained in:
TheBrokenRail 2024-06-05 21:19:39 -04:00
parent a1ea91ad89
commit 4e2437dbd6
4 changed files with 76 additions and 3 deletions

@ -1 +1 @@
Subproject commit 4d09fd9396be364fd3c1582fb945b394b265371f
Subproject commit b2cf446e9dd5593c388c1f244c527741070f7a26

View File

@ -2,6 +2,10 @@
#include <libreborn/libreborn.h>
#include <sys/stat.h>
#ifdef MCPI_USE_NATIVE_TRAMPOLINE
#include <trampoline/types.h>
#endif
#include "bootstrap.h"
#include "options/parser.h"
#include "crash-report.h"
@ -24,6 +28,9 @@ static void setup_environment(const options_t &options) {
#else
bind_to_env("_MCPI_ONLY_GENERATE", options.only_generate);
#endif
#ifdef MCPI_USE_NATIVE_TRAMPOLINE
bind_to_env(TRAMPOLINE_USE_PTRACE_ENV, options.use_ptrace_trampoline);
#endif
// GTK Dark Mode
#ifndef MCPI_HEADLESS_MODE

View File

@ -10,3 +10,6 @@ OPTION(benchmark, "benchmark", -7, "Run Benchmark")
#else
OPTION(only_generate, "only-generate", -8, "Generate World And Exit")
#endif
#ifdef MCPI_USE_NATIVE_TRAMPOLINE
OPTION(use_ptrace_trampoline, "use-ptrace-trampoline", -9, "Use PTrace For Calling Host Functions Instead Of Pipes")
#endif

View File

@ -1,11 +1,13 @@
#include <unistd.h>
#include <libreborn/libreborn.h>
#include <string>
#include "guest.h"
uint32_t _raw_trampoline(const uint32_t id, const uint32_t length, const unsigned char *args) {
// Syscall Method
static uint32_t trampoline_syscall(const uint32_t id, const uint32_t length, const unsigned char *args) {
// Make Syscall
long ret = syscall(0x1337 /* See trampoline.patch */, id, length, args);
long ret = syscall(TRAMPOLINE_SYSCALL, id, length, args);
if (ret == -1) {
// Error
ERR("Trampoline Error: %s", strerror(errno));
@ -13,3 +15,64 @@ uint32_t _raw_trampoline(const uint32_t id, const uint32_t length, const unsigne
// Return
return *(uint32_t *) args;
}
// Pipe Method
#ifdef MCPI_USE_NATIVE_TRAMPOLINE
static int get_pipe(const char *env) {
const char *value = getenv(env);
if (value == nullptr) {
IMPOSSIBLE();
}
std::string str = value;
return std::stoi(str);
}
static uint32_t trampoline_pipe(const uint32_t id, const uint32_t length, const unsigned char *args) {
// Get Pipes
static int arguments_pipe = -1;
static int return_value_pipe = -1;
if (arguments_pipe == -1) {
arguments_pipe = get_pipe(TRAMPOLINE_ARGUMENTS_PIPE_ENV);
return_value_pipe = get_pipe(TRAMPOLINE_RETURN_VALUE_PIPE_ENV);
}
// Write Arguments
trampoline_pipe_arguments data = {
.id = id,
.length = length,
.args_addr = uint32_t(args)
};
if (write(arguments_pipe, &data, sizeof(trampoline_pipe_arguments)) != sizeof(trampoline_pipe_arguments)) {
ERR("Unable To Write Trampoline Command");
}
// Return
uint32_t ret;
if (read(return_value_pipe, &ret, sizeof(uint32_t)) != sizeof(uint32_t)) {
ERR("Unable To Read Trampoline Return Value");
}
return ret;
}
#else
static uint32_t trampoline_pipe(__attribute__((unused)) const uint32_t id, __attribute__((unused)) const uint32_t length, __attribute__((unused)) const unsigned char *args) {
IMPOSSIBLE();
}
#endif
// Main Function
uint32_t _raw_trampoline(const uint32_t id, const uint32_t length, const unsigned char *args) {
// Configure Method
static int use_syscall = -1;
if (use_syscall == -1) {
use_syscall =
#ifdef MCPI_USE_NATIVE_TRAMPOLINE
getenv(TRAMPOLINE_USE_PTRACE_ENV) != nullptr
#else
1
#endif
;
}
// Use Correct Method
if (use_syscall) {
return trampoline_syscall(id, length, args);
} else {
return trampoline_pipe(id, length, args);
}
}