forked from BlockstreamResearch/secp256k1-zkp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin-core/secp256k1#1169: Add support for msan instead of va…
…lgrind (for memcheck and ctime test) 0f088ec Rename CTIMETEST -> CTIMETESTS (Pieter Wuille) 74b026f Add runtime checking for DECLASSIFY flag (Pieter Wuille) 5e2e6fc Run ctime test in Linux MSan CI job (Pieter Wuille) 1897406 Make ctime tests building configurable (Pieter Wuille) 5048be1 Rename valgrind_ctime_test -> ctime_tests (Pieter Wuille) 6eed6c1 Update error messages to suggest msan as well (Pieter Wuille) 8e11f89 Add support for msan integration to checkmem.h (Pieter Wuille) 8dc6407 Add compile-time error to valgrind_ctime_test (Pieter Wuille) 0db05a7 Abstract interactions with valgrind behind new checkmem.h (Pieter Wuille) 4f1a54e Move valgrind CPPFLAGS into SECP_CONFIG_DEFINES (Pieter Wuille) Pull request description: This introduces an abstraction layer `src/checkmem.h`, which defines macros for interacting with memory checking tools. Depending on the environment, they're mapped to MemorySanitizer builtins, Valgrind integration macros, or nothing at all. This means that msan builds immediately benefit from existing undefined memory checks in the tests. It also means those builds result in a `ctime_tests` (new name for `valgrind_ctime_test`) binary that can usefully test constant-timeness (not inside Valgrind, and with the downside that it's not running against a production library build, but it's faster and available on more platforms). Such an msan-ctime test is added to the Linux x86_64 msan CI job, as an example. More CI cases could be added (e.g. for MacOs or ARM Linux) later. ACKs for top commit: real-or-random: ACK 0f088ec hebasto: ACK 0f088ec, I have reviewed the code and it looks OK. Able to build `ctime_tests` using MSan. Tree-SHA512: f4ffcc0c2ea794894662d9797b3a349770a4b361996f967f33d7d14b332171de5d525f50bcebaeaf7d0624957083380962079c75e490d1b7d71f8f9eb6211590
- Loading branch information
Showing
16 changed files
with
279 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/*********************************************************************** | ||
* Copyright (c) 2022 Pieter Wuille * | ||
* Distributed under the MIT software license, see the accompanying * | ||
* file COPYING or https://www.opensource.org/licenses/mit-license.php.* | ||
***********************************************************************/ | ||
|
||
/* The code here is inspired by Kris Kwiatkowski's approach in | ||
* https://github.com/kriskwiatkowski/pqc/blob/main/src/common/ct_check.h | ||
* to provide a general interface for memory-checking mechanisms, primarily | ||
* for constant-time checking. | ||
*/ | ||
|
||
/* These macros are defined by this header file: | ||
* | ||
* - SECP256K1_CHECKMEM_ENABLED: | ||
* - 1 if memory-checking integration is available, 0 otherwise. | ||
* This is just a compile-time macro. Use the next macro to check it is actually | ||
* available at runtime. | ||
* - SECP256K1_CHECKMEM_RUNNING(): | ||
* - Acts like a function call, returning 1 if memory checking is available | ||
* at runtime. | ||
* - SECP256K1_CHECKMEM_CHECK(p, len): | ||
* - Assert or otherwise fail in case the len-byte memory block pointed to by p is | ||
* not considered entirely defined. | ||
* - SECP256K1_CHECKMEM_CHECK_VERIFY(p, len): | ||
* - Like SECP256K1_CHECKMEM_CHECK, but only works in VERIFY mode. | ||
* - SECP256K1_CHECKMEM_UNDEFINE(p, len): | ||
* - marks the len-byte memory block pointed to by p as undefined data (secret data, | ||
* in the context of constant-time checking). | ||
* - SECP256K1_CHECKMEM_DEFINE(p, len): | ||
* - marks the len-byte memory pointed to by p as defined data (public data, in the | ||
* context of constant-time checking). | ||
* | ||
*/ | ||
|
||
#ifndef SECP256K1_CHECKMEM_H | ||
#define SECP256K1_CHECKMEM_H | ||
|
||
/* Define a statement-like macro that ignores the arguments. */ | ||
#define SECP256K1_CHECKMEM_NOOP(p, len) do { (void)(p); (void)(len); } while(0) | ||
|
||
/* If compiling under msan, map the SECP256K1_CHECKMEM_* functionality to msan. | ||
* Choose this preferentially, even when VALGRIND is defined, as msan-compiled | ||
* binaries can't be run under valgrind anyway. */ | ||
#if defined(__has_feature) | ||
# if __has_feature(memory_sanitizer) | ||
# include <sanitizer/msan_interface.h> | ||
# define SECP256K1_CHECKMEM_ENABLED 1 | ||
# define SECP256K1_CHECKMEM_UNDEFINE(p, len) __msan_allocated_memory((p), (len)) | ||
# define SECP256K1_CHECKMEM_DEFINE(p, len) __msan_unpoison((p), (len)) | ||
# define SECP256K1_CHECKMEM_CHECK(p, len) __msan_check_mem_is_initialized((p), (len)) | ||
# define SECP256K1_CHECKMEM_RUNNING() (1) | ||
# endif | ||
#endif | ||
|
||
/* If valgrind integration is desired (through the VALGRIND define), implement the | ||
* SECP256K1_CHECKMEM_* macros using valgrind. */ | ||
#if !defined SECP256K1_CHECKMEM_ENABLED | ||
# if defined VALGRIND | ||
# include <stddef.h> | ||
# include <valgrind/memcheck.h> | ||
# define SECP256K1_CHECKMEM_ENABLED 1 | ||
# define SECP256K1_CHECKMEM_UNDEFINE(p, len) VALGRIND_MAKE_MEM_UNDEFINED((p), (len)) | ||
# define SECP256K1_CHECKMEM_DEFINE(p, len) VALGRIND_MAKE_MEM_DEFINED((p), (len)) | ||
# define SECP256K1_CHECKMEM_CHECK(p, len) VALGRIND_CHECK_MEM_IS_DEFINED((p), (len)) | ||
/* VALGRIND_MAKE_MEM_DEFINED returns 0 iff not running on memcheck. | ||
* This is more precise than the RUNNING_ON_VALGRIND macro, which | ||
* checks for valgrind in general instead of memcheck specifically. */ | ||
# define SECP256K1_CHECKMEM_RUNNING() (VALGRIND_MAKE_MEM_DEFINED(NULL, 0) != 0) | ||
# endif | ||
#endif | ||
|
||
/* As a fall-back, map these macros to dummy statements. */ | ||
#if !defined SECP256K1_CHECKMEM_ENABLED | ||
# define SECP256K1_CHECKMEM_ENABLED 0 | ||
# define SECP256K1_CHECKMEM_UNDEFINE(p, len) SECP256K1_CHECKMEM_NOOP((p), (len)) | ||
# define SECP256K1_CHECKMEM_DEFINE(p, len) SECP256K1_CHECKMEM_NOOP((p), (len)) | ||
# define SECP256K1_CHECKMEM_CHECK(p, len) SECP256K1_CHECKMEM_NOOP((p), (len)) | ||
# define SECP256K1_CHECKMEM_RUNNING() (0) | ||
#endif | ||
|
||
#if defined VERIFY | ||
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len) SECP256K1_CHECKMEM_CHECK((p), (len)) | ||
#else | ||
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len) SECP256K1_CHECKMEM_NOOP((p), (len)) | ||
#endif | ||
|
||
#endif /* SECP256K1_CHECKMEM_H */ |
Oops, something went wrong.