Raven Core  3.0.0
P2P Digital Currency
cleanse.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "cleanse.h"
8 
9 #include <cstring>
10 
11 /* Compilers have a bad habit of removing "superfluous" memset calls that
12  * are trying to zero memory. For example, when memset()ing a buffer and
13  * then free()ing it, the compiler might decide that the memset is
14  * unobservable and thus can be removed.
15  *
16  * Previously we used OpenSSL which tried to stop this by a) implementing
17  * memset in assembly on x86 and b) putting the function in its own file
18  * for other platforms.
19  *
20  * This change removes those tricks in favour of using asm directives to
21  * scare the compiler away. As best as our compiler folks can tell, this is
22  * sufficient and will continue to be so.
23  *
24  * Adam Langley <agl@google.com>
25  * Commit: ad1907fe73334d6c696c8539646c21b11178f20f
26  * BoringSSL (LICENSE: ISC)
27  */
28 void memory_cleanse(void *ptr, size_t len)
29 {
30  std::memset(ptr, 0, len);
31 
32  /* As best as we can tell, this is sufficient to break any optimisations that
33  might try to eliminate "superfluous" memsets. If there's an easy way to
34  detect memset_s, it would be better to use that. */
35 #if defined(_MSC_VER)
36  __asm;
37 #else
38  __asm__ __volatile__("" : : "r"(ptr) : "memory");
39 #endif
40 }
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:28