98 lines
2.0 KiB
C
98 lines
2.0 KiB
C
#include "utils/log.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#define LOG_AND_EXECUTE(func) \
|
|
do { \
|
|
LOG_INFO("Testing " #func); \
|
|
do_##func(); \
|
|
} while (0)
|
|
|
|
#pragma GCC push_options
|
|
#pragma GCC optimize("O0")
|
|
|
|
void do_tle() {
|
|
for (volatile int i = 0; i != -1; i++) asm("nop");
|
|
}
|
|
|
|
void do_re1() {
|
|
char *p = NULL;
|
|
*p = 'a';
|
|
printf("%c", *p);
|
|
}
|
|
|
|
void do_re2() {
|
|
int a = 0;
|
|
int b = 2 / a;
|
|
printf("%d", b);
|
|
}
|
|
|
|
void do_mle1() {
|
|
for (volatile int i = 0; i != -1; i++) {
|
|
int *p = malloc(1);
|
|
if (p == NULL) {
|
|
LOG_INFO("OK: malloc failed: i=%d", i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// void do_mle2() {
|
|
// static int a[1 << 30];
|
|
// printf("%d", a[0]);
|
|
// }
|
|
|
|
void do_block1() {
|
|
int a;
|
|
for (volatile int i = 0; i != -1; i++) scanf("%d", &a);
|
|
printf("%d", a);
|
|
}
|
|
|
|
void do_block2() { sleep(10); }
|
|
|
|
void do_nproc() {
|
|
while (1) {
|
|
pid_t pid = fork();
|
|
if (pid == -1) {
|
|
LOG_INFO("OK: fork failed");
|
|
break;
|
|
} else if (pid == 0)
|
|
LOG_ERR("FAIL: Child process, pid = %d", getpid());
|
|
else
|
|
LOG_ERR("FAIL: Parent process, pid = %d", pid);
|
|
}
|
|
}
|
|
|
|
#pragma GCC pop_options
|
|
|
|
int main() {
|
|
int op;
|
|
scanf("%d", &op);
|
|
|
|
switch (op) {
|
|
case 1: LOG_AND_EXECUTE(tle); break;
|
|
case 2: LOG_AND_EXECUTE(re1); break;
|
|
case 3: LOG_AND_EXECUTE(re2); break;
|
|
case 4: LOG_AND_EXECUTE(mle1); break;
|
|
// case 5: LOG_AND_EXECUTE(mle2); break;
|
|
case 6: LOG_AND_EXECUTE(block1); break;
|
|
case 7: LOG_AND_EXECUTE(block2); break;
|
|
case 8: LOG_AND_EXECUTE(nproc); break;
|
|
default: {
|
|
LOG_INFO("NO TEST SPECIFIED");
|
|
break;
|
|
}
|
|
}
|
|
|
|
// destroy stdin, stdout, stderr
|
|
close(0);
|
|
close(1);
|
|
close(2);
|
|
stdin = NULL;
|
|
stdout = NULL;
|
|
stderr = NULL;
|
|
|
|
return 0;
|
|
}
|