-
Notifications
You must be signed in to change notification settings - Fork 302
/
executable.cpp
347 lines (304 loc) · 12.5 KB
/
executable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/***********************************************************************************
* Copyright (c) 2020, Jonas Hahnfeld *
* Copyright (c) 2020, Chair for Computer Science 12 (HPC), RWTH Aachen University *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/
#include <algorithm>
#include <iostream>
#include <iterator>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclGroup.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/DebugInfoOptions.h"
#include "clang/Basic/Sanitizers.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CodeGen/BackendUtil.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/Frontend/CompilerInstance.h"
#include "cling/Interpreter/Interpreter.h"
#include "cling/Interpreter/Transaction.h"
#include "xeus-cling/xoptions.hpp"
#include "../xparser.hpp"
#include "executable.hpp"
namespace xcpp
{
argparser executable::get_options()
{
argparser argpars("executable", XEUS_CLING_VERSION, argparse::default_arguments::none);
argpars.add_description("write executable");
argpars.add_argument("filename")
.help("filename")
.required();
argpars.add_argument("-g")
.help("linker options: enable debug information in the executable")
.default_value(false)
.implicit_value(true);
argpars.add_argument("-fsanitize")
.help("linker options: enable instrumentation with ThreadSanitizer using \'-fsanitize=thread\'")
.default_value(false)
.implicit_value(true);
// Add custom help (does not call `exit` avoiding to restart the kernel)
argpars.add_argument("-h", "--help")
.action([&](const std::string & /*unused*/)
{
std::cout << argpars.help().str();
})
.default_value(false)
.help("shows help message")
.implicit_value(true)
.nargs(0);
return argpars;
}
std::string executable::generate_fns(const std::string& cell,
std::string& main,
std::string& unique_fn)
{
// See https://en.cppreference.com/w/cpp/language/main_function
// TODO: Find out if argc and argv would make problems if declared as
// arguments and in the body.
// Generate a unique fn that is not unloaded after generating the
// executable. This is necessary for templates like std::endl to
// work correctly in subsequent cells.
std::string fn_name = "__xeus_cling_main_wrapper_";
fn_name += std::to_string(m_unique++);
unique_fn = "int " + fn_name + "() {\n";
unique_fn += cell + "\n";
unique_fn += "return 0;\n";
unique_fn += "}";
// This code is unloaded after the executable has been generated.
main = "int main() {\n";
main += "return " + fn_name + "();\n";
main += "}\n";
// Define the function that is called for checking any pointer used
// as a member base or passed to a function call. This avoids pulling
// in the full libcling.so which is not needed.
main += "void *cling_runtime_internal_throwIfInvalidPointer(\n";
main += " void *, void *, const void *Arg) {\n";
main += " return const_cast<void*>(Arg);\n";
main += "}";
return main;
}
class FindTopLevelDecls
: public clang::RecursiveASTVisitor<FindTopLevelDecls>
{
public:
FindTopLevelDecls(clang::ASTConsumer* C) : m_consumer(C) {}
bool shouldVisitTemplateInstantiations() { return true; }
bool VisitFunctionDecl(clang::FunctionDecl* D)
{
// Filter out functions added by Cling.
if (auto Identifier = D->getIdentifier())
{
if (Identifier->getName().startswith("__cling"))
{
return true;
}
}
m_consumer->HandleTopLevelDecl(clang::DeclGroupRef(D));
return true;
}
bool VisitVarDecl(clang::VarDecl* D)
{
if (D->isFileVarDecl())
{
m_consumer->HandleTopLevelDecl(clang::DeclGroupRef(D));
}
return true;
}
private:
clang::ASTConsumer* m_consumer;
};
bool executable::generate_obj(std::string& ObjectFile, bool EnableDebugInfo)
{
// Generate LLVM IR for current AST.
auto* CI = m_interpreter.getCI();
auto* Context = m_interpreter.getLLVMContext();
auto& AST = CI->getASTContext();
auto& HeaderSearchOpts = CI->getHeaderSearchOpts();
// Generate relocations suitable for dynamic linking.
auto CodeGenOpts = CI->getCodeGenOpts();
CodeGenOpts.RelocationModel = llvm::Reloc::Model::PIC_;
// Enable debug information if requested.
if (EnableDebugInfo)
{
CodeGenOpts.setDebugInfo(
clang::codegenoptions::DebugInfoKind::FullDebugInfo);
}
std::unique_ptr<clang::CodeGenerator> CG(clang::CreateLLVMCodeGen(
CI->getDiagnostics(), "object", HeaderSearchOpts,
CI->getPreprocessorOpts(), CodeGenOpts, *Context));
CG->Initialize(AST);
FindTopLevelDecls Visitor(CG.get());
Visitor.TraverseDecl(AST.getTranslationUnitDecl());
CG->HandleTranslationUnit(AST);
// Generate (temporary) object code from LLVM IR.
int ObjectFD;
llvm::SmallString<64> ObjectFilePath;
std::error_code EC = llvm::sys::fs::createTemporaryFile(
"object", "o", ObjectFD, ObjectFilePath);
if (EC)
{
std::cerr << "Could not create temporary object file:" << std::endl
<< EC.message() << std::endl;
return false;
}
ObjectFile = ObjectFilePath.str();
std::unique_ptr<llvm::raw_pwrite_stream> OS(
new llvm::raw_fd_ostream(ObjectFD, true));
auto DataLayout = AST.getTargetInfo().getDataLayout();
EmitBackendOutput(CI->getDiagnostics(), HeaderSearchOpts,
CodeGenOpts, CI->getTargetOpts(),
CI->getLangOpts(), DataLayout, CG->GetModule(),
clang::Backend_EmitObj, std::move(OS));
return true;
}
bool executable::generate_exe(const std::string& ObjectFile,
const std::string& ExeFile,
const std::vector<std::string>& LinkerOptions)
{
auto& HeaderSearchOpts = m_interpreter.getCI()->getHeaderSearchOpts();
// Generate executable by linking the created object code.
llvm::StringRef InstallDir = llvm::sys::path::parent_path(
llvm::sys::path::parent_path(
llvm::sys::path::parent_path(HeaderSearchOpts.ResourceDir)));
llvm::SmallString<256> Compiler(InstallDir);
llvm::sys::path::append(Compiler, "bin", "clang++");
// Construct arguments to linker command.
llvm::SmallVector<llvm::StringRef, 16> Args;
Args.push_back(Compiler.c_str());
Args.push_back(ObjectFile.c_str());
for (auto& O : LinkerOptions)
{
Args.push_back(O.c_str());
}
Args.push_back("-o");
Args.push_back(ExeFile.c_str());
// Redirect output and error streams from linker.
llvm::SmallString<64> OutputFile, ErrorFile;
llvm::sys::fs::createTemporaryFile("linker", "out", OutputFile);
llvm::sys::fs::createTemporaryFile("linker", "err", ErrorFile);
llvm::FileRemover OutputRemover(OutputFile.c_str());
llvm::FileRemover ErrorRemover(ErrorFile.c_str());
llvm::StringRef OutputFileStr(OutputFile);
llvm::StringRef ErrorFileStr(ErrorFile);
llvm::SmallVector<llvm::Optional<llvm::StringRef>, 16> Redirects = {llvm::NoneType::None, OutputFileStr, ErrorFileStr};
// Finally run the linker.
int ret = llvm::sys::ExecuteAndWait(Compiler, Args, llvm::NoneType::None,
Redirects);
// Read back output and error streams.
llvm::StringRef OutputStr, ErrorStr;
auto OutputBuf = llvm::MemoryBuffer::getFile(OutputFileStr);
if (OutputBuf)
{
OutputStr = OutputBuf.get()->getBuffer();
}
auto ErrorBuf = llvm::MemoryBuffer::getFile(ErrorFileStr);
if (ErrorBuf)
{
ErrorStr = ErrorBuf.get()->getBuffer();
}
// Forward to user.
if (!OutputStr.empty())
{
std::cout << "---" << std::endl;
std::cout << OutputStr.str();
}
if (!ErrorStr.empty())
{
std::cerr << ErrorStr.str();
return false;
}
else if (ret != 0)
{
// At least let the user know that something went wrong.
std::cerr << "Could not link executable" << std::endl;
return false;
}
// Return success!
return true;
}
void executable::operator()(const std::string& line, const std::string& cell)
{
auto argpars = get_options();
argpars.parse(line);
std::string ExeFile = argpars.get<std::string>("filename");
std::string main, unique_fn;
generate_fns(cell, main, unique_fn);
// First declare the unique_fn that is not unloaded.
auto result = m_interpreter.declare(unique_fn);
if (result != cling::Interpreter::kSuccess)
{
return;
}
// Now declare main() function.
cling::Transaction* t = nullptr;
result = m_interpreter.declare(main, &t);
if (result != cling::Interpreter::kSuccess || t == nullptr)
{
return;
}
// Make sure to unload the transaction that added the main() function.
// This enables repeated execution of a %%executable cell.
struct Unloader
{
cling::Interpreter& m_interpreter;
cling::Transaction& m_transaction;
Unloader(cling::Interpreter& i, cling::Transaction& t)
: m_interpreter(i), m_transaction(t) {}
~Unloader()
{
m_interpreter.unload(m_transaction);
}
}
unloader(m_interpreter, *t);
std::vector<std::string> LinkerOptions;
// Enable debug information if user requested -g in the linker options.
bool EnableDebugInfo = argpars.is_used("-g");
if (EnableDebugInfo)
{
std::cout << "Enabling debug information" << std::endl;
LinkerOptions.push_back("-g");
}
// Enable TSan instrumentation if user requested -fsanitize in
// the linker options.
bool SanitizeThread = argpars.is_used("-fsanitize");
auto& SanitizeOpts = m_interpreter.getCI()->getLangOpts().Sanitize;
if (SanitizeThread)
{
std::cout << "Enabling instrumentation for ThreadSanitizer"
<< std::endl;
SanitizeOpts.set(clang::SanitizerKind::Thread, true);
// Imply debug information because it gives the user a clue which
// line of the input caused the race.
EnableDebugInfo = true;
LinkerOptions.push_back("-fsanitize=thread");
}
std::cout << "Writing executable to " << ExeFile << std::endl;
std::string ObjectFile;
if (!generate_obj(ObjectFile, EnableDebugInfo))
{
return;
}
// Cleanup after we exit.
llvm::FileRemover ObjectRemover(ObjectFile);
generate_exe(ObjectFile, ExeFile, LinkerOptions);
if (SanitizeThread)
{
SanitizeOpts.set(clang::SanitizerKind::Thread, false);
}
}
}