2019-05-26 02:21:31 +02:00
|
|
|
#include <AK/AKString.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static void print_usage_and_exit()
|
|
|
|
{
|
2019-06-19 20:52:12 +02:00
|
|
|
printf("usage: crash -[sdiamfMF]\n");
|
2019-05-26 02:21:31 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:52:12 +02:00
|
|
|
#pragma GCC optimize("O0")
|
2019-05-26 02:21:31 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2019-06-07 17:13:23 +02:00
|
|
|
enum Mode {
|
2019-06-07 11:49:31 +02:00
|
|
|
SegmentationViolation,
|
|
|
|
DivisionByZero,
|
|
|
|
IllegalInstruction,
|
2019-06-19 20:52:12 +02:00
|
|
|
Abort,
|
|
|
|
WriteToUninitializedMallocMemory,
|
|
|
|
WriteToFreedMemory,
|
|
|
|
ReadFromUninitializedMallocMemory,
|
|
|
|
ReadFromFreedMemory,
|
2019-06-07 11:49:31 +02:00
|
|
|
};
|
2019-05-26 02:21:31 +02:00
|
|
|
Mode mode = SegmentationViolation;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
print_usage_and_exit();
|
|
|
|
|
|
|
|
if (String(argv[1]) == "-s")
|
|
|
|
mode = SegmentationViolation;
|
|
|
|
else if (String(argv[1]) == "-d")
|
|
|
|
mode = DivisionByZero;
|
|
|
|
else if (String(argv[1]) == "-i")
|
|
|
|
mode = IllegalInstruction;
|
|
|
|
else if (String(argv[1]) == "-a")
|
|
|
|
mode = Abort;
|
2019-06-19 20:52:12 +02:00
|
|
|
else if (String(argv[1]) == "-m")
|
|
|
|
mode = ReadFromUninitializedMallocMemory;
|
|
|
|
else if (String(argv[1]) == "-f")
|
|
|
|
mode = ReadFromFreedMemory;
|
|
|
|
else if (String(argv[1]) == "-M")
|
|
|
|
mode = WriteToUninitializedMallocMemory;
|
|
|
|
else if (String(argv[1]) == "-F")
|
|
|
|
mode = WriteToFreedMemory;
|
2019-05-26 02:21:31 +02:00
|
|
|
else
|
|
|
|
print_usage_and_exit();
|
|
|
|
|
|
|
|
if (mode == SegmentationViolation) {
|
|
|
|
volatile int* crashme = nullptr;
|
|
|
|
*crashme = 0xbeef;
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == DivisionByZero) {
|
|
|
|
volatile int lala = 10;
|
|
|
|
volatile int zero = 0;
|
|
|
|
volatile int test = lala / zero;
|
2019-06-22 16:13:47 +02:00
|
|
|
(void)test;
|
2019-05-26 02:21:31 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == IllegalInstruction) {
|
|
|
|
asm volatile("ud2");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == Abort) {
|
|
|
|
abort();
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:52:12 +02:00
|
|
|
if (mode == ReadFromUninitializedMallocMemory) {
|
2019-07-03 21:17:35 +02:00
|
|
|
auto* uninitialized_memory = (volatile u32**)malloc(1024);
|
2019-06-19 20:52:12 +02:00
|
|
|
volatile auto x = uninitialized_memory[0][0];
|
2019-06-22 16:13:47 +02:00
|
|
|
(void)x;
|
2019-06-19 20:52:12 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == ReadFromFreedMemory) {
|
2019-07-03 21:17:35 +02:00
|
|
|
auto* uninitialized_memory = (volatile u32**)malloc(1024);
|
2019-06-19 20:52:12 +02:00
|
|
|
free(uninitialized_memory);
|
|
|
|
volatile auto x = uninitialized_memory[4][0];
|
2019-06-22 16:13:47 +02:00
|
|
|
(void)x;
|
2019-06-19 20:52:12 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == WriteToUninitializedMallocMemory) {
|
2019-07-03 21:17:35 +02:00
|
|
|
auto* uninitialized_memory = (volatile u32**)malloc(1024);
|
2019-06-19 20:52:12 +02:00
|
|
|
uninitialized_memory[4][0] = 1;
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == WriteToFreedMemory) {
|
2019-07-03 21:17:35 +02:00
|
|
|
auto* uninitialized_memory = (volatile u32**)malloc(1024);
|
2019-06-19 20:52:12 +02:00
|
|
|
free(uninitialized_memory);
|
|
|
|
uninitialized_memory[4][0] = 1;
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-05-26 02:21:31 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return 0;
|
|
|
|
}
|