From 4e2437dbd6f224c431c074ccb891c481e3f5f6e5 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Wed, 5 Jun 2024 21:19:39 -0400 Subject: [PATCH] Pipe (Part 2); This Needs Work --- dependencies/trampoline/src | 2 +- launcher/src/main.cpp | 7 +++ launcher/src/options/option-list.h | 3 + media-layer/trampoline/src/guest/guest.cpp | 67 +++++++++++++++++++++- 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/dependencies/trampoline/src b/dependencies/trampoline/src index 4d09fd9396..b2cf446e9d 160000 --- a/dependencies/trampoline/src +++ b/dependencies/trampoline/src @@ -1 +1 @@ -Subproject commit 4d09fd9396be364fd3c1582fb945b394b265371f +Subproject commit b2cf446e9dd5593c388c1f244c527741070f7a26 diff --git a/launcher/src/main.cpp b/launcher/src/main.cpp index b48eb8ada7..b12d85fcba 100644 --- a/launcher/src/main.cpp +++ b/launcher/src/main.cpp @@ -2,6 +2,10 @@ #include #include +#ifdef MCPI_USE_NATIVE_TRAMPOLINE +#include +#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 diff --git a/launcher/src/options/option-list.h b/launcher/src/options/option-list.h index b69d64e142..6bd59203c5 100644 --- a/launcher/src/options/option-list.h +++ b/launcher/src/options/option-list.h @@ -9,4 +9,7 @@ OPTION(print_available_feature_flags, "print-available-feature-flags", -6, "Prin 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 \ No newline at end of file diff --git a/media-layer/trampoline/src/guest/guest.cpp b/media-layer/trampoline/src/guest/guest.cpp index 2b0a5e5899..dfdf783fae 100644 --- a/media-layer/trampoline/src/guest/guest.cpp +++ b/media-layer/trampoline/src/guest/guest.cpp @@ -1,15 +1,78 @@ #include #include +#include #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)); } // 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); + } } \ No newline at end of file