| 1 |
//===- unittests/Interpreter/OutOfProcessInterpreterTest.cpp --- Interpreter |
| 2 |
// tests when Out-of-Process ----===// |
| 3 |
// |
| 4 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 |
// See https://llvm.org/LICENSE.txt for license information. |
| 6 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 |
// |
| 8 |
//===----------------------------------------------------------------------===// |
| 9 |
// |
| 10 |
// Unit tests for Clang's Interpreter library. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "InterpreterTestFixture.h" |
| 15 |
|
| 16 |
#include "clang/AST/Decl.h" |
| 17 |
#include "clang/AST/DeclGroup.h" |
| 18 |
#include "clang/AST/Mangle.h" |
| 19 |
#include "clang/Frontend/CompilerInstance.h" |
| 20 |
#include "clang/Frontend/TextDiagnosticPrinter.h" |
| 21 |
#include "clang/Interpreter/IncrementalExecutor.h" |
| 22 |
#include "clang/Interpreter/Interpreter.h" |
| 23 |
#include "clang/Interpreter/Value.h" |
| 24 |
#include "clang/Sema/Lookup.h" |
| 25 |
#include "clang/Sema/Sema.h" |
| 26 |
|
| 27 |
#include "llvm/Support/Error.h" |
| 28 |
#include "llvm/TargetParser/Host.h" |
| 29 |
|
| 30 |
#include "gmock/gmock.h" |
| 31 |
#include "gtest/gtest.h" |
| 32 |
|
| 33 |
#include <memory> |
| 34 |
#include <signal.h> |
| 35 |
#include <sstream> |
| 36 |
#include <unistd.h> |
| 37 |
|
| 38 |
using namespace clang; |
| 39 |
|
| 40 |
llvm::ExitOnError ExitOnError; |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
using Args = std::vector<const char *>; |
| 45 |
|
| 46 |
struct FileDeleter { |
| 47 |
void operator()(FILE *f) { |
| 48 |
if (f) |
| 49 |
fclose(f); |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
struct IOContext { |
| 54 |
std::unique_ptr<FILE, FileDeleter> stdin_file; |
| 55 |
std::unique_ptr<FILE, FileDeleter> stdout_file; |
| 56 |
std::unique_ptr<FILE, FileDeleter> stderr_file; |
| 57 |
|
| 58 |
bool initializeTempFiles() { |
| 59 |
stdin_file.reset(tmpfile()); |
| 60 |
stdout_file.reset(tmpfile()); |
| 61 |
stderr_file.reset(tmpfile()); |
| 62 |
return stdin_file && stdout_file && stderr_file; |
| 63 |
} |
| 64 |
|
| 65 |
std::string readStdoutContent() { |
| 66 |
if (!stdout_file) |
| 67 |
return ""; |
| 68 |
rewind(stdout_file.get()); |
| 69 |
std::ostringstream content; |
| 70 |
char buffer[1024]; |
| 71 |
size_t bytes_read; |
| 72 |
while ((bytes_read = fread(buffer, 1, sizeof(buffer), stdout_file.get())) > |
| 73 |
0) { |
| 74 |
content.write(buffer, bytes_read); |
| 75 |
} |
| 76 |
return content.str(); |
| 77 |
} |
| 78 |
|
| 79 |
std::string readStderrContent() { |
| 80 |
if (!stderr_file) |
| 81 |
return ""; |
| 82 |
rewind(stderr_file.get()); |
| 83 |
std::ostringstream content; |
| 84 |
char buffer[1024]; |
| 85 |
size_t bytes_read; |
| 86 |
while ((bytes_read = fread(buffer, 1, sizeof(buffer), stderr_file.get())) > |
| 87 |
0) { |
| 88 |
content.write(buffer, bytes_read); |
| 89 |
} |
| 90 |
return content.str(); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
static void removePathComponent(unsigned N, llvm::SmallString<256> &Path) { |
| 95 |
for (unsigned i = 0; i < N; ++i) |
| 96 |
llvm::sys::path::remove_filename(Path); |
| 97 |
} |
| 98 |
|
| 99 |
static std::string getExecutorPath() { |
| 100 |
llvm::SmallString<256> ExecutorPath(llvm::sys::fs::getMainExecutable( |
| 101 |
nullptr, reinterpret_cast<void *>(&getExecutorPath))); |
| 102 |
removePathComponent(5, ExecutorPath); |
| 103 |
llvm::sys::path::append(ExecutorPath, "bin", "llvm-jitlink-executor"); |
| 104 |
return ExecutorPath.str().str(); |
| 105 |
} |
| 106 |
|
| 107 |
class OutOfProcessInterpreterTest : public InterpreterTestBase { |
| 108 |
protected: |
| 109 |
static bool HostSupportsOutOfProcessJIT() { |
| 110 |
if (!InterpreterTestBase::HostSupportsJIT()) |
| 111 |
return false; |
| 112 |
return !getExecutorPath().empty(); |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
struct OutOfProcessInterpreterInfo { |
| 117 |
std::string OrcRuntimePath; |
| 118 |
std::unique_ptr<Interpreter> Interp; |
| 119 |
}; |
| 120 |
|
| 121 |
static OutOfProcessInterpreterInfo |
| 122 |
createInterpreterWithRemoteExecution(std::shared_ptr<IOContext> io_ctx, |
| 123 |
const Args &ExtraArgs = {}) { |
| 124 |
Args ClangArgs = {"-Xclang", "-emit-llvm-only"}; |
| 125 |
llvm::append_range(ClangArgs, ExtraArgs); |
| 126 |
|
| 127 |
auto Config = std::make_unique<IncrementalExecutorBuilder>(); |
| 128 |
llvm::Triple SystemTriple(llvm::sys::getProcessTriple()); |
| 129 |
|
| 130 |
if (SystemTriple.isOSBinFormatELF() || SystemTriple.isOSBinFormatMachO()) { |
| 131 |
Config->IsOutOfProcess = true; |
| 132 |
Config->OOPExecutor = getExecutorPath(); |
| 133 |
Config->UseSharedMemory = false; |
| 134 |
Config->SlabAllocateSize = 0; |
| 135 |
|
| 136 |
// Capture the raw file descriptors by value explicitly. This lambda will |
| 137 |
// be invoked in the child process after fork(), so capturing the fd ints is |
| 138 |
// safe and avoids capturing FILE* pointers or outer 'this'. |
| 139 |
int stdin_fd = fileno(io_ctx->stdin_file.get()); |
| 140 |
int stdout_fd = fileno(io_ctx->stdout_file.get()); |
| 141 |
int stderr_fd = fileno(io_ctx->stderr_file.get()); |
| 142 |
|
| 143 |
Config->CustomizeFork = [stdin_fd, stdout_fd, stderr_fd]() { |
| 144 |
auto redirect = [](int from, int to) { |
| 145 |
if (from != to) { |
| 146 |
dup2(from, to); |
| 147 |
close(from); |
| 148 |
} |
| 149 |
}; |
| 150 |
|
| 151 |
redirect(stdin_fd, STDIN_FILENO); |
| 152 |
redirect(stdout_fd, STDOUT_FILENO); |
| 153 |
redirect(stderr_fd, STDERR_FILENO); |
| 154 |
|
| 155 |
// Unbuffer the stdio in the child; useful for deterministic tests. |
| 156 |
setvbuf(stdout, nullptr, _IONBF, 0); |
| 157 |
setvbuf(stderr, nullptr, _IONBF, 0); |
| 158 |
|
| 159 |
// Helpful marker for the unit-test to assert that fork customization ran. |
| 160 |
printf("CustomizeFork executed\n"); |
| 161 |
fflush(stdout); |
| 162 |
}; |
| 163 |
} |
| 164 |
auto CB = IncrementalCompilerBuilder(); |
| 165 |
CB.SetCompilerArgs(ClangArgs); |
| 166 |
CB.SetDriverCompilationCallback(Config->UpdateOrcRuntimePathCB); |
| 167 |
auto CI = cantFail(CB.CreateCpp()); |
| 168 |
return {Config->OrcRuntimePath, |
| 169 |
cantFail(Interpreter::create(std::move(CI), std::move(Config)))}; |
| 170 |
} |
| 171 |
|
| 172 |
static size_t DeclsSize(TranslationUnitDecl *PTUDecl) { |
| 173 |
return std::distance(PTUDecl->decls().begin(), PTUDecl->decls().end()); |
| 174 |
} |
| 175 |
|
| 176 |
TEST_F(OutOfProcessInterpreterTest, SanityWithRemoteExecution) { |
| 177 |
if (!HostSupportsOutOfProcessJIT()) |
| 178 |
GTEST_SKIP(); |
| 179 |
|
| 180 |
auto io_ctx = std::make_shared<IOContext>(); |
| 181 |
ASSERT_TRUE(io_ctx->initializeTempFiles()); |
| 182 |
|
| 183 |
OutOfProcessInterpreterInfo Info = |
| 184 |
createInterpreterWithRemoteExecution(io_ctx); |
| 185 |
Interpreter *Interp = Info.Interp.get(); |
| 186 |
ASSERT_TRUE(Interp); |
| 187 |
|
| 188 |
using PTU = PartialTranslationUnit; |
| 189 |
PTU &R1(cantFail(Interp->Parse("void g(); void g() {}"))); |
| 190 |
EXPECT_EQ(2U, DeclsSize(R1.TUPart)); |
| 191 |
|
| 192 |
PTU &R2(cantFail(Interp->Parse("int i = 42;"))); |
| 193 |
EXPECT_EQ(1U, DeclsSize(R2.TUPart)); |
| 194 |
|
| 195 |
std::string captured_stdout = io_ctx->readStdoutContent(); |
| 196 |
std::string captured_stderr = io_ctx->readStderrContent(); |
| 197 |
|
| 198 |
EXPECT_NE(std::string::npos, captured_stdout.find("CustomizeFork executed")); |
| 199 |
} |
| 200 |
|
| 201 |
TEST_F(OutOfProcessInterpreterTest, FindRuntimeInterface) { |
| 202 |
if (!HostSupportsOutOfProcessJIT()) |
| 203 |
GTEST_SKIP(); |
| 204 |
|
| 205 |
// make a fresh io context for this test |
| 206 |
auto io_ctx = std::make_shared<IOContext>(); |
| 207 |
ASSERT_TRUE(io_ctx->initializeTempFiles()); |
| 208 |
|
| 209 |
OutOfProcessInterpreterInfo I = createInterpreterWithRemoteExecution(io_ctx); |
| 210 |
ASSERT_TRUE(I.Interp); |
| 211 |
|
| 212 |
// FIXME: Not yet supported. |
| 213 |
// cantFail(I->Parse("int a = 1; a")); |
| 214 |
// cantFail(I->Parse("int b = 2; b")); |
| 215 |
// cantFail(I->Parse("int c = 3; c")); |
| 216 |
|
| 217 |
// // Make sure no clang::Value logic is attached by the Interpreter. |
| 218 |
// Value V1; |
| 219 |
// llvm::cantFail(I->ParseAndExecute("int x = 42;")); |
| 220 |
// llvm::cantFail(I->ParseAndExecute("x", &V1)); |
| 221 |
// EXPECT_FALSE(V1.isValid()); |
| 222 |
// EXPECT_FALSE(V1.hasValue()); |
| 223 |
} |
| 224 |
|
| 225 |
} // end anonymous namespace |