spice_packages/tpde/src/tpde-wrapper.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* C API wrapper around the TPDE LLVM back end (tpde-llvm/LLVMCompiler.hpp). | ||
| 2 | * | ||
| 3 | * TPDE only offers a C++ API, so Spice can't bind to it directly. These functions expose it in the style of the | ||
| 4 | * LLVM C API: they take the LLVM C handles, return true on failure and hand out error messages, that have to be | ||
| 5 | * released with LLVMDisposeMessage. | ||
| 6 | * | ||
| 7 | * TPDE emits ELF objects for x86_64 and aarch64 only, so it is only used on Linux. The TPDE headers and libraries both | ||
| 8 | * come from the TPDE_FLAGS environment variable (see linker-flags.spice), so the headers are only visible if the libraries | ||
| 9 | * are linked as well. Otherwise, the functions are stubs, that report TPDE as unavailable. | ||
| 10 | */ | ||
| 11 | #include <cstdint> | ||
| 12 | #include <cstdio> | ||
| 13 | #include <memory> | ||
| 14 | #include <string> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | #include <llvm-c/Core.h> | ||
| 18 | |||
| 19 | #if defined(__linux__) && __has_include(<tpde-llvm/LLVMCompiler.hpp>) | ||
| 20 | #define SPICE_TPDE_AVAILABLE 1 | ||
| 21 | #include <llvm/IR/Module.h> | ||
| 22 | #include <llvm/TargetParser/Triple.h> | ||
| 23 | #include <tpde-llvm/LLVMCompiler.hpp> | ||
| 24 | #else | ||
| 25 | #define SPICE_TPDE_AVAILABLE 0 | ||
| 26 | #endif | ||
| 27 | |||
| 28 | extern "C" { | ||
| 29 | typedef struct TPDEOpaqueCompiler *TPDECompilerRef; | ||
| 30 | } | ||
| 31 | |||
| 32 | namespace { | ||
| 33 | |||
| 34 | #if SPICE_TPDE_AVAILABLE | ||
| 35 | // Compile the module to an ELF object in the given buffer. Returns true on success | ||
| 36 | 8 | bool compileModule(TPDECompilerRef compiler, LLVMModuleRef module, std::vector<uint8_t> &buffer, char **errorMessage) { | |
| 37 | 8 | llvm::Module *mod = llvm::unwrap(module); | |
| 38 | 8 | if (!reinterpret_cast<tpde_llvm::LLVMCompiler *>(compiler)->compile_to_elf(*mod, buffer)) { | |
| 39 | ✗ | const std::string message = "TPDE failed to compile module '" + mod->getName().str() + "'"; | |
| 40 | ✗ | *errorMessage = LLVMCreateMessage(message.c_str()); | |
| 41 | ✗ | return false; | |
| 42 | ✗ | } | |
| 43 | 8 | return true; | |
| 44 | 8 | } | |
| 45 | #else | ||
| 46 | // Report, that the bindings are not backed by TPDE. Returns true, like the failing API functions | ||
| 47 | LLVMBool reportUnavailable(char **errorMessage) { | ||
| 48 | *errorMessage = LLVMCreateMessage("The TPDE bindings were built without TPDE"); | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | #endif | ||
| 52 | |||
| 53 | } // namespace | ||
| 54 | |||
| 55 | extern "C" { | ||
| 56 | |||
| 57 | /* Check if this build of the bindings is backed by TPDE */ | ||
| 58 | 15 | LLVMBool TPDEIsAvailable(void) { return SPICE_TPDE_AVAILABLE; } | |
| 59 | |||
| 60 | #if SPICE_TPDE_AVAILABLE | ||
| 61 | |||
| 62 | /* Create a compiler for the given target triple. Returns null if TPDE does not support the triple */ | ||
| 63 | 7 | TPDECompilerRef TPDECreateCompiler(const char *triple) { | |
| 64 | 7 | std::unique_ptr<tpde_llvm::LLVMCompiler> compiler = tpde_llvm::LLVMCompiler::create(llvm::Triple(triple)); | |
| 65 | 7 | return reinterpret_cast<TPDECompilerRef>(compiler.release()); | |
| 66 | 7 | } | |
| 67 | |||
| 68 | /* Dispose a compiler, that was created with TPDECreateCompiler */ | ||
| 69 | 5 | void TPDEDisposeCompiler(TPDECompilerRef compiler) { delete reinterpret_cast<tpde_llvm::LLVMCompiler *>(compiler); } | |
| 70 | |||
| 71 | /* Compile the module to an ELF object file. The module might be modified during compilation */ | ||
| 72 | 1 | LLVMBool TPDECompileToObjectFile(TPDECompilerRef compiler, LLVMModuleRef module, const char *filename, char **errorMessage) { | |
| 73 | 1 | std::vector<uint8_t> buffer; | |
| 74 | 1 | if (!compileModule(compiler, module, buffer, errorMessage)) | |
| 75 | ✗ | return 1; | |
| 76 | 1 | FILE *file = std::fopen(filename, "wb"); | |
| 77 | 1 | if (file == nullptr) { | |
| 78 | ✗ | const std::string message = "File '" + std::string(filename) + "' could not be opened"; | |
| 79 | ✗ | *errorMessage = LLVMCreateMessage(message.c_str()); | |
| 80 | ✗ | return 1; | |
| 81 | ✗ | } | |
| 82 | 1 | const size_t written = std::fwrite(buffer.data(), 1, buffer.size(), file); | |
| 83 | 1 | const bool closeFailed = std::fclose(file) != 0; | |
| 84 | 1 | if (written != buffer.size() || closeFailed) { | |
| 85 | ✗ | const std::string message = "File '" + std::string(filename) + "' could not be written"; | |
| 86 | ✗ | *errorMessage = LLVMCreateMessage(message.c_str()); | |
| 87 | ✗ | return 1; | |
| 88 | ✗ | } | |
| 89 | 1 | return 0; | |
| 90 | 1 | } | |
| 91 | |||
| 92 | /* Compile the module to an ELF object in memory. The module might be modified during compilation */ | ||
| 93 | 7 | LLVMBool TPDECompileToMemoryBuffer(TPDECompilerRef compiler, LLVMModuleRef module, LLVMMemoryBufferRef *outMemBuf, | |
| 94 | char **errorMessage) { | ||
| 95 | 7 | std::vector<uint8_t> buffer; | |
| 96 | 7 | if (!compileModule(compiler, module, buffer, errorMessage)) | |
| 97 | ✗ | return 1; | |
| 98 | 7 | const char *data = reinterpret_cast<const char *>(buffer.data()); | |
| 99 | 7 | *outMemBuf = LLVMCreateMemoryBufferWithMemoryRangeCopy(data, buffer.size(), "tpde-object"); | |
| 100 | 7 | return 0; | |
| 101 | 7 | } | |
| 102 | |||
| 103 | #else | ||
| 104 | |||
| 105 | /* Stub: TPDE is not available, so there is no compiler for any triple */ | ||
| 106 | TPDECompilerRef TPDECreateCompiler(const char *) { return nullptr; } | ||
| 107 | |||
| 108 | /* Stub: there is no compiler to dispose */ | ||
| 109 | void TPDEDisposeCompiler(TPDECompilerRef) {} | ||
| 110 | |||
| 111 | /* Stub: report, that TPDE is not available */ | ||
| 112 | LLVMBool TPDECompileToObjectFile(TPDECompilerRef, LLVMModuleRef, const char *, char **errorMessage) { | ||
| 113 | return reportUnavailable(errorMessage); | ||
| 114 | } | ||
| 115 | |||
| 116 | /* Stub: report, that TPDE is not available */ | ||
| 117 | LLVMBool TPDECompileToMemoryBuffer(TPDECompilerRef, LLVMModuleRef, LLVMMemoryBufferRef *outMemBuf, char **errorMessage) { | ||
| 118 | *outMemBuf = nullptr; | ||
| 119 | return reportUnavailable(errorMessage); | ||
| 120 | } | ||
| 121 | |||
| 122 | #endif | ||
| 123 | |||
| 124 | } // extern "C" | ||
| 125 |