FFmpeg
Loading...
Searching...
No Matches
stackguard.c
Go to the documentation of this file.
1/*
2 * Copyright © 2025, Rémi Denis-Courmont
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26#include "checkasm_config.h"
27
28#include <assert.h>
29#include <stdint.h>
30#include "checkasm/test.h"
31#include "internal.h"
32
33static THREAD_LOCAL uintptr_t *current = NULL;
34
35/* Sets the stack guard up.
36 *
37 * Note that whilst the canary only uses 2 addresses, it should be padded to a
38 * larger size to reduce the risk of corrupting the stack frames of the test
39 * cases or the `checkasm` run-time above the canary.
40 */
41void checkasm_push_stack_guard(uintptr_t guard[2])
42{
43 uintptr_t cookie = (uintptr_t)(void *)checkasm_push_stack_guard;
44 uintptr_t selfref = (uintptr_t)(void *)guard;
45
46 /*
47 * NOTE: We CANNOT assert that `current` is null here. If the previous test
48 * failed uncleanly, `current` is a stale pointer. As long as we do not
49 * dereference it, that is fine.
50 */
51 guard[0] = cookie ^ selfref;
52 guard[1] = selfref;
53 current = guard;
54 /*
55 * In theory, with link-time optimisations and static linking, the compiler
56 * could notice that the guard can never validly be overwritten, and elide
57 * the validity checks below, or even the memory stores above.
58 * This dummy assembler snippet prevents the compiler from making any
59 * assumption.
60 */
61#if defined(__clang__) || !defined(_MSC_VER)
62 __asm__ volatile ("# NOTHING HERE" :: "r"(guard) : "memory");
63#endif
64}
65
67{
68 uintptr_t *guard = current;
69 uintptr_t cookie = (uintptr_t)(void *)checkasm_push_stack_guard;
70 uintptr_t selfref = (uintptr_t)(void *)guard;
71
72 current = NULL;
73 assert(guard != NULL);
74 if (guard[0] != (cookie ^ selfref) || guard[1] != selfref)
75 checkasm_fail_abort("stack clobbered");
76}
__asm__(".macro parse_r var r\n\t" "\\var = -1\n\t" _IFC_REG(0) _IFC_REG(1) _IFC_REG(2) _IFC_REG(3) _IFC_REG(4) _IFC_REG(5) _IFC_REG(6) _IFC_REG(7) _IFC_REG(8) _IFC_REG(9) _IFC_REG(10) _IFC_REG(11) _IFC_REG(12) _IFC_REG(13) _IFC_REG(14) _IFC_REG(15) _IFC_REG(16) _IFC_REG(17) _IFC_REG(18) _IFC_REG(19) _IFC_REG(20) _IFC_REG(21) _IFC_REG(22) _IFC_REG(23) _IFC_REG(24) _IFC_REG(25) _IFC_REG(26) _IFC_REG(27) _IFC_REG(28) _IFC_REG(29) _IFC_REG(30) _IFC_REG(31) ".iflt \\var\n\t" ".error \"Unable to parse register name \\r\"\n\t" ".endif\n\t" ".endm")
#define NULL
Definition coverity.c:32
void checkasm_fail_abort(const char *const msg,...)
Definition checkasm.c:1076
static struct @111144215057303131116103221376075045141373005341 current
void checkasm_pop_stack_guard(void)
Definition stackguard.c:66
void checkasm_push_stack_guard(uintptr_t guard[2])
Push stack guard values for corruption detection.
Definition stackguard.c:41
Test writing API for checkasm.
#define THREAD_LOCAL
Definition internal.h:76