xref: /freebsd-src/crypto/openssl/test/memleaktest.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <string.h>
11*e0c4386eSCy Schubert #include <openssl/bio.h>
12*e0c4386eSCy Schubert #include <openssl/crypto.h>
13*e0c4386eSCy Schubert 
14*e0c4386eSCy Schubert #include "testutil.h"
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert /* __has_feature is a clang-ism, while __SANITIZE_ADDRESS__ is a gcc-ism */
17*e0c4386eSCy Schubert #if defined(__has_feature)
18*e0c4386eSCy Schubert # if __has_feature(address_sanitizer)
19*e0c4386eSCy Schubert #  define __SANITIZE_ADDRESS__ 1
20*e0c4386eSCy Schubert # endif
21*e0c4386eSCy Schubert #endif
22*e0c4386eSCy Schubert /* If __SANITIZE_ADDRESS__ isn't defined, define it to be false */
23*e0c4386eSCy Schubert /* Leak detection is not yet supported with MSVC on Windows, so */
24*e0c4386eSCy Schubert /* set __SANITIZE_ADDRESS__ to false in this case as well.      */
25*e0c4386eSCy Schubert #if !defined(__SANITIZE_ADDRESS__) || defined(_MSC_VER)
26*e0c4386eSCy Schubert # undef __SANITIZE_ADDRESS__
27*e0c4386eSCy Schubert # define __SANITIZE_ADDRESS__ 0
28*e0c4386eSCy Schubert #endif
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert /*
31*e0c4386eSCy Schubert  * We use a proper main function here instead of the custom main from the
32*e0c4386eSCy Schubert  * test framework to avoid CRYPTO_mem_leaks stuff.
33*e0c4386eSCy Schubert  */
34*e0c4386eSCy Schubert 
main(int argc,char * argv[])35*e0c4386eSCy Schubert int main(int argc, char *argv[])
36*e0c4386eSCy Schubert {
37*e0c4386eSCy Schubert #if __SANITIZE_ADDRESS__
38*e0c4386eSCy Schubert     int exitcode = EXIT_SUCCESS;
39*e0c4386eSCy Schubert #else
40*e0c4386eSCy Schubert     /*
41*e0c4386eSCy Schubert      * When we don't sanitize, we set the exit code to what we would expect
42*e0c4386eSCy Schubert      * to get when we are sanitizing.  This makes it easy for wrapper scripts
43*e0c4386eSCy Schubert      * to detect that we get the result we expect.
44*e0c4386eSCy Schubert      */
45*e0c4386eSCy Schubert     int exitcode = EXIT_FAILURE;
46*e0c4386eSCy Schubert #endif
47*e0c4386eSCy Schubert     char *lost;
48*e0c4386eSCy Schubert 
49*e0c4386eSCy Schubert     lost = OPENSSL_malloc(3);
50*e0c4386eSCy Schubert     if (!TEST_ptr(lost))
51*e0c4386eSCy Schubert         return EXIT_FAILURE;
52*e0c4386eSCy Schubert 
53*e0c4386eSCy Schubert     strcpy(lost, "ab");
54*e0c4386eSCy Schubert 
55*e0c4386eSCy Schubert     if (argv[1] && strcmp(argv[1], "freeit") == 0) {
56*e0c4386eSCy Schubert         OPENSSL_free(lost);
57*e0c4386eSCy Schubert         exitcode = EXIT_SUCCESS;
58*e0c4386eSCy Schubert     }
59*e0c4386eSCy Schubert 
60*e0c4386eSCy Schubert     lost = NULL;
61*e0c4386eSCy Schubert     return exitcode;
62*e0c4386eSCy Schubert }
63