From 9308ce071f6208a20a7abc087d00be227d1be9ee Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 26 May 2019 02:21:31 +0200 Subject: [PATCH] Userland: Add a helpful little program for provoking different crashes. Currently supported crash types: -s : Segmentation violation -d : Division by zero -i : Illegal instruction -a : Abort --- Userland/crash.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Userland/crash.cpp diff --git a/Userland/crash.cpp b/Userland/crash.cpp new file mode 100644 index 0000000000..f19b714d6d --- /dev/null +++ b/Userland/crash.cpp @@ -0,0 +1,55 @@ +#include +#include +#include + +static void print_usage_and_exit() +{ + printf("usage: crash -[sdia]\n"); + exit(0); +} + +int main(int argc, char** argv) +{ + enum Mode { SegmentationViolation, DivisionByZero, IllegalInstruction, Abort }; + 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; + 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; + ASSERT_NOT_REACHED(); + } + + if (mode == IllegalInstruction) { + asm volatile("ud2"); + ASSERT_NOT_REACHED(); + } + + if (mode == Abort) { + abort(); + ASSERT_NOT_REACHED(); + } + + ASSERT_NOT_REACHED(); + return 0; +}