wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 13:f67a6c6013ca 1 /* mem_track.h
wolfSSL 13:f67a6c6013ca 2 *
wolfSSL 13:f67a6c6013ca 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 13:f67a6c6013ca 4 *
wolfSSL 13:f67a6c6013ca 5 * This file is part of wolfSSL.
wolfSSL 13:f67a6c6013ca 6 *
wolfSSL 13:f67a6c6013ca 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 13:f67a6c6013ca 8 * it under the terms of the GNU General Public License as published by
wolfSSL 13:f67a6c6013ca 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 13:f67a6c6013ca 10 * (at your option) any later version.
wolfSSL 13:f67a6c6013ca 11 *
wolfSSL 13:f67a6c6013ca 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 13:f67a6c6013ca 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 13:f67a6c6013ca 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 13:f67a6c6013ca 15 * GNU General Public License for more details.
wolfSSL 13:f67a6c6013ca 16 *
wolfSSL 13:f67a6c6013ca 17 * You should have received a copy of the GNU General Public License
wolfSSL 13:f67a6c6013ca 18 * along with this program; if not, write to the Free Software
wolfSSL 13:f67a6c6013ca 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 13:f67a6c6013ca 20 */
wolfSSL 13:f67a6c6013ca 21
wolfSSL 13:f67a6c6013ca 22
wolfSSL 13:f67a6c6013ca 23 /* The memory tracker overrides the wolfSSL memory callback system and uses a
wolfSSL 13:f67a6c6013ca 24 * static to track the total, peak and currently allocated bytes.
wolfSSL 13:f67a6c6013ca 25 *
wolfSSL 13:f67a6c6013ca 26 * If you are already using the memory callbacks then enabling this will
wolfSSL 13:f67a6c6013ca 27 * override the memory callbacks and prevent your memory callbacks from
wolfSSL 13:f67a6c6013ca 28 * working. This assumes malloc() and free() are available. Feel free to
wolfSSL 13:f67a6c6013ca 29 * customize this for your needs.
wolfSSL 13:f67a6c6013ca 30
wolfSSL 13:f67a6c6013ca 31 * The enable this feature define the following:
wolfSSL 13:f67a6c6013ca 32 * #define USE_WOLFSSL_MEMORY
wolfSSL 13:f67a6c6013ca 33 * #define WOLFSSL_TRACK_MEMORY
wolfSSL 13:f67a6c6013ca 34 *
wolfSSL 13:f67a6c6013ca 35 * On startup call:
wolfSSL 13:f67a6c6013ca 36 * InitMemoryTracker();
wolfSSL 13:f67a6c6013ca 37 *
wolfSSL 13:f67a6c6013ca 38 * When ready to dump the memory report call:
wolfSSL 13:f67a6c6013ca 39 * ShowMemoryTracker();
wolfSSL 13:f67a6c6013ca 40 *
wolfSSL 13:f67a6c6013ca 41 * Report example:
wolfSSL 13:f67a6c6013ca 42 * total Allocs = 228
wolfSSL 13:f67a6c6013ca 43 * total Bytes = 93442
wolfSSL 13:f67a6c6013ca 44 * peak Bytes = 8840
wolfSSL 13:f67a6c6013ca 45 * current Bytes = 0
wolfSSL 13:f67a6c6013ca 46 *
wolfSSL 13:f67a6c6013ca 47 *
wolfSSL 13:f67a6c6013ca 48 * You can also:
wolfSSL 13:f67a6c6013ca 49 * #define WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 50 *
wolfSSL 13:f67a6c6013ca 51 * To print every alloc/free along with the function and line number.
wolfSSL 13:f67a6c6013ca 52 * Example output:
wolfSSL 13:f67a6c6013ca 53 * Alloc: 0x7fa14a500010 -> 120 at wc_InitRng:496
wolfSSL 13:f67a6c6013ca 54 * Free: 0x7fa14a500010 -> 120 at wc_FreeRng:606
wolfSSL 13:f67a6c6013ca 55 */
wolfSSL 13:f67a6c6013ca 56
wolfSSL 13:f67a6c6013ca 57
wolfSSL 13:f67a6c6013ca 58 #ifndef WOLFSSL_MEM_TRACK_H
wolfSSL 13:f67a6c6013ca 59 #define WOLFSSL_MEM_TRACK_H
wolfSSL 13:f67a6c6013ca 60
wolfSSL 13:f67a6c6013ca 61 #if defined(USE_WOLFSSL_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
wolfSSL 13:f67a6c6013ca 62
wolfSSL 13:f67a6c6013ca 63 #include "wolfssl/wolfcrypt/logging.h"
wolfSSL 13:f67a6c6013ca 64
wolfSSL 13:f67a6c6013ca 65 typedef struct memoryStats {
wolfSSL 13:f67a6c6013ca 66 size_t totalAllocs; /* number of allocations */
wolfSSL 13:f67a6c6013ca 67 size_t totalDeallocs; /* number of deallocations */
wolfSSL 13:f67a6c6013ca 68 size_t totalBytes; /* total number of bytes allocated */
wolfSSL 13:f67a6c6013ca 69 size_t peakBytes; /* concurrent max bytes */
wolfSSL 13:f67a6c6013ca 70 size_t currentBytes; /* total current bytes in use */
wolfSSL 13:f67a6c6013ca 71 } memoryStats;
wolfSSL 13:f67a6c6013ca 72
wolfSSL 13:f67a6c6013ca 73 typedef struct memHint {
wolfSSL 13:f67a6c6013ca 74 size_t thisSize; /* size of this memory */
wolfSSL 13:f67a6c6013ca 75 void* thisMemory; /* actual memory for user */
wolfSSL 13:f67a6c6013ca 76 } memHint;
wolfSSL 13:f67a6c6013ca 77
wolfSSL 13:f67a6c6013ca 78 typedef struct memoryTrack {
wolfSSL 13:f67a6c6013ca 79 union {
wolfSSL 13:f67a6c6013ca 80 memHint hint;
wolfSSL 13:f67a6c6013ca 81 byte alignit[16]; /* make sure we have strong alignment */
wolfSSL 13:f67a6c6013ca 82 } u;
wolfSSL 13:f67a6c6013ca 83 } memoryTrack;
wolfSSL 13:f67a6c6013ca 84
wolfSSL 13:f67a6c6013ca 85 #if defined(WOLFSSL_TRACK_MEMORY)
wolfSSL 13:f67a6c6013ca 86 #define DO_MEM_STATS
wolfSSL 13:f67a6c6013ca 87 static memoryStats ourMemStats;
wolfSSL 13:f67a6c6013ca 88 #endif
wolfSSL 13:f67a6c6013ca 89
wolfSSL 13:f67a6c6013ca 90 /* if defined to not using inline then declare function prototypes */
wolfSSL 13:f67a6c6013ca 91 #ifdef NO_INLINE
wolfSSL 13:f67a6c6013ca 92 #define STATIC
wolfSSL 13:f67a6c6013ca 93 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 94 WOLFSSL_LOCAL void* TrackMalloc(size_t sz, const char* func, unsigned int line);
wolfSSL 13:f67a6c6013ca 95 WOLFSSL_LOCAL void TrackFree(void* ptr, const char* func, unsigned int line);
wolfSSL 13:f67a6c6013ca 96 WOLFSSL_LOCAL void* TrackRealloc(void* ptr, size_t sz, const char* func, unsigned int line);
wolfSSL 13:f67a6c6013ca 97 #else
wolfSSL 13:f67a6c6013ca 98 WOLFSSL_LOCAL void* TrackMalloc(size_t sz);
wolfSSL 13:f67a6c6013ca 99 WOLFSSL_LOCAL void TrackFree(void* ptr);
wolfSSL 13:f67a6c6013ca 100 WOLFSSL_LOCAL void* TrackRealloc(void* ptr, size_t sz);
wolfSSL 13:f67a6c6013ca 101 #endif
wolfSSL 13:f67a6c6013ca 102 WOLFSSL_LOCAL int InitMemoryTracker(void);
wolfSSL 13:f67a6c6013ca 103 WOLFSSL_LOCAL void ShowMemoryTracker(void);
wolfSSL 13:f67a6c6013ca 104 #else
wolfSSL 13:f67a6c6013ca 105 #define STATIC static
wolfSSL 13:f67a6c6013ca 106 #endif
wolfSSL 13:f67a6c6013ca 107
wolfSSL 13:f67a6c6013ca 108 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 109 STATIC INLINE void* TrackMalloc(size_t sz, const char* func, unsigned int line)
wolfSSL 13:f67a6c6013ca 110 #else
wolfSSL 13:f67a6c6013ca 111 STATIC INLINE void* TrackMalloc(size_t sz)
wolfSSL 13:f67a6c6013ca 112 #endif
wolfSSL 13:f67a6c6013ca 113 {
wolfSSL 13:f67a6c6013ca 114 memoryTrack* mt;
wolfSSL 13:f67a6c6013ca 115
wolfSSL 13:f67a6c6013ca 116 if (sz == 0)
wolfSSL 13:f67a6c6013ca 117 return NULL;
wolfSSL 13:f67a6c6013ca 118
wolfSSL 13:f67a6c6013ca 119 mt = (memoryTrack*)malloc(sizeof(memoryTrack) + sz);
wolfSSL 13:f67a6c6013ca 120 if (mt == NULL)
wolfSSL 13:f67a6c6013ca 121 return NULL;
wolfSSL 13:f67a6c6013ca 122
wolfSSL 13:f67a6c6013ca 123 mt->u.hint.thisSize = sz;
wolfSSL 13:f67a6c6013ca 124 mt->u.hint.thisMemory = (byte*)mt + sizeof(memoryTrack);
wolfSSL 13:f67a6c6013ca 125
wolfSSL 13:f67a6c6013ca 126 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 127 printf("Alloc: %p -> %u at %s:%d\n", mt->u.hint.thisMemory, (word32)sz, func, line);
wolfSSL 13:f67a6c6013ca 128 #endif
wolfSSL 13:f67a6c6013ca 129
wolfSSL 13:f67a6c6013ca 130 #ifdef DO_MEM_STATS
wolfSSL 13:f67a6c6013ca 131 ourMemStats.totalAllocs++;
wolfSSL 13:f67a6c6013ca 132 ourMemStats.totalBytes += sz;
wolfSSL 13:f67a6c6013ca 133 ourMemStats.currentBytes += sz;
wolfSSL 13:f67a6c6013ca 134 if (ourMemStats.currentBytes > ourMemStats.peakBytes)
wolfSSL 13:f67a6c6013ca 135 ourMemStats.peakBytes = ourMemStats.currentBytes;
wolfSSL 13:f67a6c6013ca 136 #endif
wolfSSL 13:f67a6c6013ca 137
wolfSSL 13:f67a6c6013ca 138 return mt->u.hint.thisMemory;
wolfSSL 13:f67a6c6013ca 139 }
wolfSSL 13:f67a6c6013ca 140
wolfSSL 13:f67a6c6013ca 141
wolfSSL 13:f67a6c6013ca 142 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 143 STATIC INLINE void TrackFree(void* ptr, const char* func, unsigned int line)
wolfSSL 13:f67a6c6013ca 144 #else
wolfSSL 13:f67a6c6013ca 145 STATIC INLINE void TrackFree(void* ptr)
wolfSSL 13:f67a6c6013ca 146 #endif
wolfSSL 13:f67a6c6013ca 147 {
wolfSSL 13:f67a6c6013ca 148 memoryTrack* mt;
wolfSSL 13:f67a6c6013ca 149
wolfSSL 13:f67a6c6013ca 150 if (ptr == NULL) {
wolfSSL 13:f67a6c6013ca 151 return;
wolfSSL 13:f67a6c6013ca 152 }
wolfSSL 13:f67a6c6013ca 153
wolfSSL 13:f67a6c6013ca 154 mt = (memoryTrack*)ptr;
wolfSSL 13:f67a6c6013ca 155 --mt; /* same as minus sizeof(memoryTrack), removes header */
wolfSSL 13:f67a6c6013ca 156
wolfSSL 13:f67a6c6013ca 157 #ifdef DO_MEM_STATS
wolfSSL 13:f67a6c6013ca 158 ourMemStats.currentBytes -= mt->u.hint.thisSize;
wolfSSL 13:f67a6c6013ca 159 ourMemStats.totalDeallocs++;
wolfSSL 13:f67a6c6013ca 160 #endif
wolfSSL 13:f67a6c6013ca 161
wolfSSL 13:f67a6c6013ca 162 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 163 printf("Free: %p -> %u at %s:%d\n", ptr, (word32)mt->u.hint.thisSize, func, line);
wolfSSL 13:f67a6c6013ca 164 #endif
wolfSSL 13:f67a6c6013ca 165
wolfSSL 13:f67a6c6013ca 166 free(mt);
wolfSSL 13:f67a6c6013ca 167 }
wolfSSL 13:f67a6c6013ca 168
wolfSSL 13:f67a6c6013ca 169
wolfSSL 13:f67a6c6013ca 170 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 171 STATIC INLINE void* TrackRealloc(void* ptr, size_t sz, const char* func, unsigned int line)
wolfSSL 13:f67a6c6013ca 172 #else
wolfSSL 13:f67a6c6013ca 173 STATIC INLINE void* TrackRealloc(void* ptr, size_t sz)
wolfSSL 13:f67a6c6013ca 174 #endif
wolfSSL 13:f67a6c6013ca 175 {
wolfSSL 13:f67a6c6013ca 176 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 177 void* ret = TrackMalloc(sz, func, line);
wolfSSL 13:f67a6c6013ca 178 #else
wolfSSL 13:f67a6c6013ca 179 void* ret = TrackMalloc(sz);
wolfSSL 13:f67a6c6013ca 180 #endif
wolfSSL 13:f67a6c6013ca 181
wolfSSL 13:f67a6c6013ca 182 if (ptr) {
wolfSSL 13:f67a6c6013ca 183 /* if realloc is bigger, don't overread old ptr */
wolfSSL 13:f67a6c6013ca 184 memoryTrack* mt = (memoryTrack*)ptr;
wolfSSL 13:f67a6c6013ca 185 --mt; /* same as minus sizeof(memoryTrack), removes header */
wolfSSL 13:f67a6c6013ca 186
wolfSSL 13:f67a6c6013ca 187 if (mt->u.hint.thisSize < sz)
wolfSSL 13:f67a6c6013ca 188 sz = mt->u.hint.thisSize;
wolfSSL 13:f67a6c6013ca 189 }
wolfSSL 13:f67a6c6013ca 190
wolfSSL 13:f67a6c6013ca 191 if (ret && ptr)
wolfSSL 13:f67a6c6013ca 192 XMEMCPY(ret, ptr, sz);
wolfSSL 13:f67a6c6013ca 193
wolfSSL 13:f67a6c6013ca 194 if (ret) {
wolfSSL 13:f67a6c6013ca 195 #ifdef WOLFSSL_DEBUG_MEMORY
wolfSSL 13:f67a6c6013ca 196 TrackFree(ptr, func, line);
wolfSSL 13:f67a6c6013ca 197 #else
wolfSSL 13:f67a6c6013ca 198 TrackFree(ptr);
wolfSSL 13:f67a6c6013ca 199 #endif
wolfSSL 13:f67a6c6013ca 200 }
wolfSSL 13:f67a6c6013ca 201
wolfSSL 13:f67a6c6013ca 202 return ret;
wolfSSL 13:f67a6c6013ca 203 }
wolfSSL 13:f67a6c6013ca 204
wolfSSL 13:f67a6c6013ca 205 #ifdef WOLFSSL_TRACK_MEMORY
wolfSSL 13:f67a6c6013ca 206 STATIC INLINE int InitMemoryTracker(void)
wolfSSL 13:f67a6c6013ca 207 {
wolfSSL 13:f67a6c6013ca 208 int ret = wolfSSL_SetAllocators(TrackMalloc, TrackFree, TrackRealloc);
wolfSSL 13:f67a6c6013ca 209 if (ret < 0) {
wolfSSL 13:f67a6c6013ca 210 printf("wolfSSL SetAllocators failed for track memory\n");
wolfSSL 13:f67a6c6013ca 211 return ret;
wolfSSL 13:f67a6c6013ca 212 }
wolfSSL 13:f67a6c6013ca 213
wolfSSL 13:f67a6c6013ca 214 #ifdef DO_MEM_STATS
wolfSSL 13:f67a6c6013ca 215 ourMemStats.totalAllocs = 0;
wolfSSL 13:f67a6c6013ca 216 ourMemStats.totalDeallocs = 0;
wolfSSL 13:f67a6c6013ca 217 ourMemStats.totalBytes = 0;
wolfSSL 13:f67a6c6013ca 218 ourMemStats.peakBytes = 0;
wolfSSL 13:f67a6c6013ca 219 ourMemStats.currentBytes = 0;
wolfSSL 13:f67a6c6013ca 220 #endif
wolfSSL 13:f67a6c6013ca 221
wolfSSL 13:f67a6c6013ca 222 return ret;
wolfSSL 13:f67a6c6013ca 223 }
wolfSSL 13:f67a6c6013ca 224
wolfSSL 13:f67a6c6013ca 225 STATIC INLINE void ShowMemoryTracker(void)
wolfSSL 13:f67a6c6013ca 226 {
wolfSSL 13:f67a6c6013ca 227 #ifdef DO_MEM_STATS
wolfSSL 13:f67a6c6013ca 228 printf("total Allocs = %9lu\n",
wolfSSL 13:f67a6c6013ca 229 (unsigned long)ourMemStats.totalAllocs);
wolfSSL 13:f67a6c6013ca 230 printf("total Deallocs = %9lu\n",
wolfSSL 13:f67a6c6013ca 231 (unsigned long)ourMemStats.totalDeallocs);
wolfSSL 13:f67a6c6013ca 232 printf("total Bytes = %9lu\n",
wolfSSL 13:f67a6c6013ca 233 (unsigned long)ourMemStats.totalBytes);
wolfSSL 13:f67a6c6013ca 234 printf("peak Bytes = %9lu\n",
wolfSSL 13:f67a6c6013ca 235 (unsigned long)ourMemStats.peakBytes);
wolfSSL 13:f67a6c6013ca 236 printf("current Bytes = %9lu\n",
wolfSSL 13:f67a6c6013ca 237 (unsigned long)ourMemStats.currentBytes);
wolfSSL 13:f67a6c6013ca 238 #endif
wolfSSL 13:f67a6c6013ca 239 }
wolfSSL 13:f67a6c6013ca 240 #endif
wolfSSL 13:f67a6c6013ca 241
wolfSSL 13:f67a6c6013ca 242 #endif /* USE_WOLFSSL_MEMORY */
wolfSSL 13:f67a6c6013ca 243
wolfSSL 13:f67a6c6013ca 244 #endif /* WOLFSSL_MEM_TRACK_H */
wolfSSL 13:f67a6c6013ca 245
wolfSSL 13:f67a6c6013ca 246