1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <endian.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <map>
32
33 #include <base/debug/stack_trace.h>
34
35 #include <libavb/libavb.h>
36
avb_memcmp(const void * src1,const void * src2,size_t n)37 int avb_memcmp(const void* src1, const void* src2, size_t n) {
38 return memcmp(src1, src2, n);
39 }
40
avb_memcpy(void * dest,const void * src,size_t n)41 void* avb_memcpy(void* dest, const void* src, size_t n) {
42 return memcpy(dest, src, n);
43 }
44
avb_memset(void * dest,const int c,size_t n)45 void* avb_memset(void* dest, const int c, size_t n) {
46 return memset(dest, c, n);
47 }
48
avb_strcmp(const char * s1,const char * s2)49 int avb_strcmp(const char* s1, const char* s2) {
50 return strcmp(s1, s2);
51 }
52
avb_strncmp(const char * s1,const char * s2,size_t n)53 int avb_strncmp(const char* s1, const char* s2, size_t n) {
54 return strncmp(s1, s2, n);
55 }
56
avb_strlen(const char * str)57 size_t avb_strlen(const char* str) {
58 return strlen(str);
59 }
60
avb_abort(void)61 void avb_abort(void) {
62 abort();
63 }
64
avb_print(const char * message)65 void avb_print(const char* message) {
66 fprintf(stderr, "%s", message);
67 }
68
avb_printv(const char * message,...)69 void avb_printv(const char* message, ...) {
70 va_list ap;
71 const char* m;
72
73 va_start(ap, message);
74 for (m = message; m != NULL; m = va_arg(ap, const char*)) {
75 fprintf(stderr, "%s", m);
76 }
77 va_end(ap);
78 }
79
80 typedef struct {
81 size_t size;
82 base::debug::StackTrace stack_trace;
83 } AvbAllocatedBlock;
84
85 static std::map<void*, AvbAllocatedBlock> allocated_blocks;
86
avb_malloc_(size_t size)87 void* avb_malloc_(size_t size) {
88 void* ptr = malloc(size);
89 avb_assert(ptr != nullptr);
90 AvbAllocatedBlock block;
91 block.size = size;
92 allocated_blocks[ptr] = block;
93 return ptr;
94 }
95
avb_free(void * ptr)96 void avb_free(void* ptr) {
97 auto block_it = allocated_blocks.find(ptr);
98 if (block_it == allocated_blocks.end()) {
99 avb_fatal("Tried to free pointer to non-allocated block.\n");
100 return;
101 }
102 allocated_blocks.erase(block_it);
103 free(ptr);
104 }
105
avb_div_by_10(uint64_t * dividend)106 uint32_t avb_div_by_10(uint64_t* dividend) {
107 uint32_t rem = (uint32_t)(*dividend % 10);
108 *dividend /= 10;
109 return rem;
110 }
111
112 namespace avb {
113
testing_memory_reset()114 void testing_memory_reset() {
115 allocated_blocks.clear();
116 }
117
testing_memory_all_freed()118 bool testing_memory_all_freed() {
119 if (allocated_blocks.size() == 0) {
120 return true;
121 }
122
123 size_t sum = 0;
124 for (const auto& block_it : allocated_blocks) {
125 sum += block_it.second.size;
126 }
127 fprintf(stderr,
128 "%zd bytes still allocated in %zd blocks:\n",
129 sum,
130 allocated_blocks.size());
131 size_t n = 0;
132 for (const auto& block_it : allocated_blocks) {
133 fprintf(stderr,
134 "--\nAllocation %zd/%zd of %zd bytes:\n",
135 1 + n++,
136 allocated_blocks.size(),
137 block_it.second.size);
138 block_it.second.stack_trace.Print();
139 }
140 return false;
141 }
142
143 // Also check leaks at process exit.
ensure_all_memory_freed_at_exit()144 __attribute__((destructor)) static void ensure_all_memory_freed_at_exit() {
145 if (!testing_memory_all_freed()) {
146 avb_fatal("libavb memory leaks at process exit.\n");
147 }
148 }
149
150 } // namespace avb
151