-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccc.c
311 lines (266 loc) · 7.86 KB
/
ccc.c
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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <spawn.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdbool.h>
#include <dirent.h>
#include <unistd.h>
static char const *const COMPILED_APP_NAME = "a.out";
extern char *const *const environ;
#define DEBUG (false)
typedef struct DccArgs {
char **args;
size_t const length;
} DccArgs;
static DccArgs constructDccArgs(int const argc, char const *const argv[]);
static void freeDccArgs(DccArgs *args);
static bool spawnDcc(DccArgs const *args);
static void spawnCompiledApp(void);
static bool findError(char const stderrOutput[]);
static void pretendDelete(void);
static void ignoreSignals(void);
static void fakeTemp(void);
int main(int const argc, char const *const argv[])
{
printf("===\n"
"Welcome to ccc! A compiler that compiles your C file, and "
"then runs it.\n"
"If it encounters any undefined behaviour, it deletes all your "
"files! How exciting!\n"
"===\n\n");
if (argc < 2) {
fprintf(stderr,
"===\nHow to use:\n~z5257526/ccc <your C file>\n===\n");
return EXIT_FAILURE;
}
DccArgs dccArgs = constructDccArgs(argc, argv);
bool const dccOk = spawnDcc(&dccArgs);
if (dccOk) {
printf("==< Your program compiled! Now let's run it >:) >==\n\n");
spawnCompiledApp();
} else {
printf("==< YOUR PROGRAM FAILED TO COMPILE. GOODBYE. >==\n");
pretendDelete();
}
freeDccArgs(&dccArgs);
}
static DccArgs constructDccArgs(int const argc, char const *const argv[])
{
static const size_t EXTRA_ARGS = 2;
DccArgs args = { .args = calloc(argc + EXTRA_ARGS + 1, sizeof(char *)),
.length = argc + EXTRA_ARGS + 1 };
if (!args.args)
err(EXIT_FAILURE, "Failed to calloc DccArgs");
args.args[0] = strdup("dcc");
args.args[1] = strdup("-o");
args.args[2] = strdup(COMPILED_APP_NAME);
for (unsigned i = EXTRA_ARGS + 1; i < EXTRA_ARGS + argc; ++i) {
args.args[i] = strdup(argv[i - EXTRA_ARGS]);
}
args.args[args.length - 1] = NULL;
if (DEBUG) {
printf("args:\n");
for (unsigned i = 0; i < args.length; ++i) {
printf("\t%s\n", args.args[i]);
}
}
return args;
}
static void freeDccArgs(DccArgs *args)
{
// Last element is a NULL
for (size_t i = 0; i < args->length - 1; ++i) {
free(args->args[i]);
}
free(args->args);
}
static bool spawnDcc(DccArgs const *args)
{
pid_t pid = -1;
int error = posix_spawnp(&pid, args->args[0], NULL, NULL, args->args,
environ);
if (error) {
errno = error;
err(EXIT_FAILURE, "Couldn't spawn %s", args->args[0]);
}
int exitStatus;
if (waitpid(pid, &exitStatus, 0) == -1) {
err(EXIT_FAILURE, "Couldn't wait for %s, (PID %d)",
args->args[0], pid);
}
if (DEBUG) {
printf("%s exited with status %d.\n", args->args[0],
exitStatus);
}
return exitStatus == 0;
}
static void spawnCompiledApp(void)
{
// Pipe the child's `stderr` back here
// [0] is read, [1] is write
int pipeFd[2];
if (pipe(pipeFd) == -1)
err(EXIT_FAILURE, "Couldn't create pipe");
// Create file actions
posix_spawn_file_actions_t fileActions;
int error = posix_spawn_file_actions_init(&fileActions);
if (error) {
errno = error;
err(EXIT_FAILURE, "Couldn't create file actions");
}
// Replace the child's `stderr` with the write end of the pipe
if ((error = posix_spawn_file_actions_adddup2(
&fileActions, pipeFd[STDOUT_FILENO], STDERR_FILENO))) {
errno = error;
err(EXIT_FAILURE, "Couldn't adddup2");
}
// Tell the child to close the read end of the pipe
if ((error = posix_spawn_file_actions_addclose(&fileActions,
pipeFd[STDIN_FILENO]))) {
errno = error;
err(EXIT_FAILURE, "Couldn't addclose pipefd 0");
}
// Now that we've dup2-ed, tell the child to close the write end
if ((error = posix_spawn_file_actions_addclose(
&fileActions, pipeFd[STDOUT_FILENO]))) {
errno = error;
err(EXIT_FAILURE, "Couldn't addclose pipefd 1");
}
// Args for the child
// +2 for "./" and +1 for NULL
char childPath[strlen(COMPILED_APP_NAME) + 3];
snprintf(childPath, sizeof(childPath), "./%s", COMPILED_APP_NAME);
char *const args[] = { childPath, NULL };
pid_t pid = -1;
error = posix_spawn(&pid, args[0], &fileActions, NULL, args, environ);
if (error) {
errno = error;
err(EXIT_FAILURE, "Couldn't spawn %s", args[0]);
}
// Close the parent's write end of the pipe
close(pipeFd[STDOUT_FILENO]);
// Read the child's `stderr`
FILE *childStderr = fdopen(pipeFd[STDIN_FILENO], "r");
size_t maxOutputSize = 128;
char *stderrOutput = calloc(maxOutputSize, sizeof(char));
if (!stderrOutput)
err(EXIT_FAILURE, "Failed to calloc child output buffer");
size_t outputCount = 0;
int readChar;
while ((readChar = fgetc(childStderr)) != EOF) {
if (outputCount >= maxOutputSize) {
maxOutputSize *= 2;
stderrOutput = realloc(stderrOutput, maxOutputSize);
if (!stderrOutput)
err(EXIT_FAILURE, "Failed to realloc buffer");
}
stderrOutput[outputCount] = readChar;
++outputCount;
}
stderrOutput[outputCount] = '\0';
if (DEBUG) {
fprintf(stderr, "Child's stderr:\n");
fprintf(stderr, "%s", stderrOutput);
fprintf(stderr, "End child's stderr\n");
}
fclose(childStderr);
int exitStatus;
if (waitpid(pid, &exitStatus, 0) == -1) {
err(EXIT_FAILURE, "Couldn't wait for %s, (PID %d)", args[0],
pid);
}
if (DEBUG) {
printf("%s exited with status %d.\n", args[0], exitStatus);
}
if (findError(stderrOutput)) {
printf("\n==< UNDEFINED BEHAVIOUR DETECTED. GOODBYE. >==\n");
pretendDelete();
} else {
fprintf(stderr, "%s\n", stderrOutput);
printf("==< No errors found, your're safe... for now :) >==\n");
}
}
static bool findError(char const stderrOutput[])
{
return (strstr(stderrOutput, "Runtime error") ||
strstr(stderrOutput, "Execution terminated") ||
strstr(stderrOutput, "Execution stopped") ||
strstr(stderrOutput, "dcc-help"));
}
static void pretendDelete(void)
{
ignoreSignals();
// usleep(3000000);
printf("==< DELETING YOUR FILES... >==\n");
char const *const homePath = getenv("HOME");
if (!homePath)
errx(EXIT_FAILURE, "Couldn't get $HOME");
DIR *const homeDir = opendir(homePath);
if (!homeDir)
err(EXIT_FAILURE, "Couldn't open %s", homePath);
struct dirent *currentFile = NULL;
while ((currentFile = readdir(homeDir))) {
if (currentFile->d_name[0] == '.')
// Ignore hidden files
continue;
printf("Deleting ~/%s\n", currentFile->d_name);
usleep(200000);
}
closedir(homeDir);
printf("\nTip: try writing correct code next time :)\n\n");
fakeTemp();
// printf("Lol jks :P\n");
}
static void ignoreSignals(void)
{
for (int signal = 1; signal < 32; ++signal) {
if (signal == SIGKILL || signal == SIGSTOP || signal == SIGHUP)
continue;
if (sigaction(signal,
&(struct sigaction){ .sa_handler = SIG_IGN },
NULL) == -1) {
err(EXIT_FAILURE, "Couldn't set signal %d", signal);
}
}
}
static void fakeTemp(void)
{
// Create a temp dir and change to it
char tempDirTemplate[] = "/tmp/tmp.XXXXXX";
char const *const tempDir = mkdtemp(tempDirTemplate);
if (tempDir == NULL) {
perror("Failed to create temporary directory");
exit(EXIT_FAILURE);
}
if (chdir(tempDir) == -1) {
perror("Failed to change directory to temporary directory");
exit(EXIT_FAILURE);
}
// Set the user's $HOME to the temp dir
char const *const front = "export HOME=";
char buffer[strlen(front) + strlen(tempDir) + 1];
snprintf(buffer, sizeof(buffer), "%s%s", front, tempDir);
// printf("Running '%s'", buffer);
if (setenv("HOME", tempDir, 1) == -1) {
perror("Failed to set $HOME");
exit(EXIT_FAILURE);
}
char *const userShell = getenv("SHELL");
// posix_spawn the user shell
pid_t pid = -1;
int error = posix_spawnp(&pid, userShell, NULL, NULL,
(char *const[]){ userShell, NULL }, environ);
if (error) {
errno = error;
err(EXIT_FAILURE, "Couldn't spawn %s", userShell);
}
// Wait for the user shell to exit
int exitStatus;
waitpid(pid, &exitStatus, 0);
}