Coverage Report

Created: 2024-09-11 06:39

/src/cryptofuzz/modules/bitcoin/cleanse.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2019 The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <support/cleanse.h>
7
8
#include <cstring>
9
10
#if defined(_MSC_VER)
11
#include <Windows.h> // For SecureZeroMemory.
12
#endif
13
14
void memory_cleanse(void *ptr, size_t len)
15
124M
{
16
#if defined(_MSC_VER)
17
    /* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */
18
    SecureZeroMemory(ptr, len);
19
#else
20
124M
    std::memset(ptr, 0, len);
21
22
    /* Memory barrier that scares the compiler away from optimizing out the memset.
23
     *
24
     * Quoting Adam Langley <agl@google.com> in commit ad1907fe73334d6c696c8539646c21b11178f20f
25
     * in BoringSSL (ISC License):
26
     *    As best as we can tell, this is sufficient to break any optimisations that
27
     *    might try to eliminate "superfluous" memsets.
28
     * This method is used in memzero_explicit() the Linux kernel, too. Its advantage is that it
29
     * is pretty efficient because the compiler can still implement the memset() efficiently,
30
     * just not remove it entirely. See "Dead Store Elimination (Still) Considered Harmful" by
31
     * Yang et al. (USENIX Security 2017) for more background.
32
     */
33
124M
    __asm__ __volatile__("" : : "r"(ptr) : "memory");
34
124M
#endif
35
124M
}