Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/mem_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 // Weak symbols are only supported well on Linux platforms. The memory functions
5 // that attempt to use weak symbols in mem.c are disabled on other platforms.
6 // Therefore, only run these tests on supported platforms.
7 #if defined(__ELF__) && defined(__GNUC__)
8
9 #include <gtest/gtest.h>
10 #include <openssl/mem.h>
11
12 int alloc_count = 0;
13 size_t test_size = 0;
14 int free_count = 0;
15 int size_count = 0;
16 int realloc_count = 0;
17
18 extern "C" {
19 OPENSSL_EXPORT void *OPENSSL_memory_alloc(size_t size);
20 OPENSSL_EXPORT void OPENSSL_memory_free(void *ptr);
21 OPENSSL_EXPORT size_t OPENSSL_memory_get_size(void *ptr);
22 OPENSSL_EXPORT void *OPENSSL_memory_realloc(void *ptr, size_t size);
23
24 void *new_malloc_impl(size_t size, const char *file, int line);
25 void new_free_impl(void *ptr, const char *file, int line);
26 void *new_realloc_impl(void *ptr, size_t size, const char *file, int line);
27 }
28
29 void *OPENSSL_memory_alloc(size_t size) {
30 alloc_count++;
31 test_size = size;
32 return malloc(size);
33 }
34
35 void OPENSSL_memory_free(void *ptr) {
36 free_count++;
37 free(ptr);
38 }
39
40 size_t OPENSSL_memory_get_size(void *ptr) {
41 size_count++;
42 return test_size;
43 }
44
45 void *OPENSSL_memory_realloc(void *ptr, size_t size) {
46 realloc_count++;
47 return realloc(ptr, size);
48 }
49
50 TEST(MemTest, BasicOverrides) {
51 // The FIPS build which runs the power on self tests can call a lot of functions
52 // before this test. Therefore, all the expected counts are relative to the
53 // starting point
54 int starting_alloc = alloc_count;
55 int starting_free = free_count;
56 int starting_realloc = realloc_count;
57
58 void *malloc_ptr = nullptr;
59 // Check setup conditions
60 ASSERT_EQ(starting_alloc, alloc_count);
61 ASSERT_EQ(starting_free, free_count);
62 ASSERT_EQ(starting_realloc, realloc_count);
63 ASSERT_EQ(0, size_count);
64 ASSERT_EQ(malloc_ptr, nullptr);
65
66 // Verify malloc calls |OPENSSL_memory_alloc| and doesn't do anything else
67 test_size = 10;
68 malloc_ptr = OPENSSL_malloc(test_size);
69 ASSERT_NE(malloc_ptr, nullptr);
70 ASSERT_EQ(starting_alloc + 1, alloc_count);
71 ASSERT_EQ(starting_free, free_count);
72 ASSERT_EQ(starting_realloc, realloc_count);
73 ASSERT_EQ(0, size_count);
74
75 // Calling realloc with null actually calls |OPENSSL_malloc| which calls
76 // |OPENSSL_memory_alloc|
77 void *null_realloc_ptr = nullptr;
78 test_size = 200;
79 void *realloc_ptr_1 = OPENSSL_realloc(null_realloc_ptr, test_size);
80 ASSERT_NE(realloc_ptr_1, nullptr);
81 ASSERT_EQ(starting_alloc + 2, alloc_count);
82 ASSERT_EQ(starting_free, free_count);
83 ASSERT_EQ(starting_realloc, realloc_count);
84 ASSERT_EQ(0, size_count);
85 ASSERT_EQ(nullptr, null_realloc_ptr);
86
87 // Calling realloc with a not null pointer calls |OPENSSL_memory_realloc|
88 test_size = 300;
89 void *realloc_ptr_2 = OPENSSL_realloc(malloc_ptr, test_size);
90 ASSERT_EQ(starting_alloc + 2, alloc_count);
91 ASSERT_EQ(starting_free, free_count);
92 ASSERT_EQ(starting_realloc + 1, realloc_count);
93 ASSERT_EQ(0, size_count);
94
95 // Check |OPENSSL_memory_free| calls our symbol as expected
96 OPENSSL_free(realloc_ptr_1);
97 ASSERT_EQ(starting_alloc + 2, alloc_count);
98 ASSERT_EQ(starting_free + 1, free_count);
99 ASSERT_EQ(starting_realloc + 1, realloc_count);
100 ASSERT_EQ(0, size_count);
101 OPENSSL_free(realloc_ptr_2);
102 }
103
104 void *new_malloc_impl(size_t size, const char *file, int line) {
105 return NULL;
106 }
107
108 void *new_realloc_impl(void *ptr, size_t size, const char *file, int line) {
109 return NULL;
110 }
111
112 void new_free_impl(void *ptr, const char *file, int line) {
113 return;
114 }
115
116 TEST(MemTest, MemSetFailWhenWeakSymbolsOverrided) {
117 // CRYPTO_set_mem_functions returns 0 when |OPENSSL_malloc/free/realloc| are customized by overriding the symbols.
118 ASSERT_EQ(0, CRYPTO_set_mem_functions(new_malloc_impl, new_realloc_impl, new_free_impl));
119 }
120 #endif