#include "utils/log.h" #include #include #include #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); } } void do_fsize() { FILE *fp = fopen("test.dat", "w"); if (fp == NULL) { LOG_ERR("FAIL: fopen failed"); return; } for (int i = 0; i < 1 << 30; i++) { if (fputc('a', fp) == EOF) { LOG_INFO("OK: fputc failed: i=%d", i); break; } } fclose(fp); } #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; case 9: LOG_AND_EXECUTE(fsize); 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; }