-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
55 lines (44 loc) · 1.51 KB
/
Copy pathcompiler.cpp
File metadata and controls
55 lines (44 loc) · 1.51 KB
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
#include "compiler.h"
#include "security.h" // Week 8 Integration
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
std::string Compiler::injectSecurityPreamble(const std::string& sourceFile) {
std::string tempFileName = "temp_sandboxed.cpp";
std::ofstream outFile(tempFileName);
std::ifstream inFile(sourceFile);
// 1. Inject Week 8 Security Module Preamble
outFile << SecurityModule::getSeccompPreamble() << "\n";
// 2. Append User Code
outFile << inFile.rdbuf();
outFile.close();
return tempFileName;
}
CompileResult Compiler::compile(const std::string& sourceFile) {
CompileResult result;
result.binaryPath = "./a.out";
std::string readyFile = injectSecurityPreamble(sourceFile);
int logFd = open("compile_errors.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
pid_t pid = fork();
if (pid == 0) {
dup2(logFd, STDERR_FILENO);
close(logFd);
execlp("g++", "g++", readyFile.c_str(), "-o", result.binaryPath.c_str(), NULL);
_exit(1);
}
close(logFd);
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
result.success = true;
} else {
result.success = false;
std::ifstream logReader("compile_errors.txt");
result.errorOutput.assign((std::istreambuf_iterator<char>(logReader)),
std::istreambuf_iterator<char>());
}
remove(readyFile.c_str());
return result;
}