| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 33 | static 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 | */ | ||
| 41 | ✗ | void 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 | |||
| 66 | ✗ | void checkasm_pop_stack_guard(void) | |
| 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 | ✗ | } | |
| 77 |