Serenity Operating System
1/*
2 * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/Format.h>
8#include <LibTest/TestCase.h>
9
10// Note: Needs to be 'noinline' so stack canary isn't optimized out.
11static void __attribute__((noinline)) smasher(char* string)
12{
13#pragma GCC diagnostic push
14#pragma GCC diagnostic ignored "-Warray-bounds"
15 for (int i = 0; i < 256; i++) {
16 string[i] = 'A';
17 }
18#pragma GCC diagnostic pop
19}
20
21// Note: Needs to be 'noinline' so stack canary isn't optimized out.
22static void __attribute__((noinline)) stack_to_smash()
23{
24 char string[8] = {};
25 smasher(string);
26}
27
28TEST_CASE(stack_smash)
29{
30 EXPECT_CRASH("Smash the stack and trigger __stack_chk_fail", [] {
31 outln("[+] Starting the stack smash...");
32 stack_to_smash();
33 outln("[+] Stack smash wasn't detected!");
34 return Test::Crash::Failure::DidNotCrash;
35 });
36}