Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/mem_set_test.cc

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4 #include <gtest/gtest.h>
5 #include <openssl/mem.h>
6
7 int alloc_count = 0;
8 size_t test_size = 0;
9 int free_count = 0;
10 int size_count = 0;
11 int realloc_count = 0;
12
13 extern "C" {
14 void *new_malloc_impl(size_t size, const char *file, int line);
15 void new_free_impl(void *ptr, const char *file, int line);
16 void *new_realloc_impl(void *ptr, size_t size, const char *file, int line);
17 }
18
19 void *new_malloc_impl(size_t size, const char *file, int line) {
20 alloc_count++;
21 test_size = size;
22 return malloc(size);
23 }
24
25 void new_free_impl(void *ptr, const char *file, int line) {
26 free_count++;
27 free(ptr);
28 }
29
30 void *new_realloc_impl(void *ptr, size_t size, const char *file, int line) {
31 realloc_count++;
32 return realloc(ptr, size);
33 }
34
35 // This test is copy of |MemTest.BasicOverrides| in mem_test.cc.
36 // |MemTest.BasicOverrides| changed the |OPENSSL_malloc/free/realloc| by overriding related weak symbols.
37 // This test achieved the mem behavior change by calling |CRYPTO_set_mem_functions|.
38 TEST(MemTest, BasicMemSet) {
39 // The FIPS build which runs the power on self tests can call a lot of functions
40 // before this test. Therefore, all the expected counts are relative to the
41 // starting point
42 int starting_alloc = alloc_count;
43 int starting_free = free_count;
44 int starting_realloc = realloc_count;
45
46 void *malloc_ptr = nullptr;
47 // Check setup conditions
48 ASSERT_EQ(starting_alloc, alloc_count);
49 ASSERT_EQ(starting_free, free_count);
50 ASSERT_EQ(starting_realloc, realloc_count);
51 ASSERT_EQ(0, size_count);
52 ASSERT_EQ(malloc_ptr, nullptr);
53
54 // Call |CRYPTO_set_mem_functions| to override |OPENSSL_malloc/realloc/free|.
55 ASSERT_EQ(1, CRYPTO_set_mem_functions(new_malloc_impl, new_realloc_impl, new_free_impl));
56
57 // Verify malloc calls |new_malloc_impl| and doesn't do anything else
58 test_size = 10;
59 malloc_ptr = OPENSSL_malloc(test_size);
60 ASSERT_NE(malloc_ptr, nullptr);
61 ASSERT_EQ(starting_alloc + 1, alloc_count);
62 ASSERT_EQ(starting_free, free_count);
63 ASSERT_EQ(starting_realloc, realloc_count);
64 ASSERT_EQ(0, size_count);
65
66 // Calling realloc with null actually calls |OPENSSL_malloc| which calls
67 // |new_realloc_impl|
68 void *null_realloc_ptr = nullptr;
69 test_size = 200;
70 void *realloc_ptr_1 = OPENSSL_realloc(null_realloc_ptr, test_size);
71 ASSERT_NE(realloc_ptr_1, nullptr);
72 ASSERT_EQ(starting_alloc + 2, alloc_count);
73 ASSERT_EQ(starting_free, free_count);
74 ASSERT_EQ(starting_realloc, realloc_count);
75 ASSERT_EQ(0, size_count);
76 ASSERT_EQ(nullptr, null_realloc_ptr);
77
78 // Calling realloc with a not null pointer calls |new_realloc_impl|
79 test_size = 300;
80 void *realloc_ptr_2 = OPENSSL_realloc(malloc_ptr, test_size);
81 ASSERT_EQ(starting_alloc + 2, alloc_count);
82 ASSERT_EQ(starting_free, free_count);
83 ASSERT_EQ(starting_realloc + 1, realloc_count);
84 ASSERT_EQ(0, size_count);
85
86 // Check |new_free_impl| calls our symbol as expected
87 OPENSSL_free(realloc_ptr_1);
88 ASSERT_EQ(starting_alloc + 2, alloc_count);
89 ASSERT_EQ(starting_free + 1, free_count);
90 ASSERT_EQ(starting_realloc + 1, realloc_count);
91 ASSERT_EQ(0, size_count);
92 OPENSSL_free(realloc_ptr_2);
93 }