gnunet-android

GNUnet for Android
Log | Files | Refs | README

memory.h (26055B)


      1 /* memory.h
      2  *
      3  * Copyright (C) 2006-2025 wolfSSL Inc.
      4  *
      5  * This file is part of wolfSSL.
      6  *
      7  * wolfSSL is free software; you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation; either version 3 of the License, or
     10  * (at your option) any later version.
     11  *
     12  * wolfSSL is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  * GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with this program; if not, write to the Free Software
     19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
     20  */
     21 
     22 
     23 /* submitted by eof */
     24 
     25 /*!
     26     \file wolfssl/wolfcrypt/memory.h
     27 */
     28 
     29 #ifndef WOLFSSL_MEMORY_H
     30 #define WOLFSSL_MEMORY_H
     31 
     32 #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
     33 #include <stdlib.h>
     34 #endif
     35 
     36 #ifndef WOLF_CRYPT_TYPES_H
     37 #include <wolfssl/wolfcrypt/types.h>
     38 #endif
     39 
     40 #ifdef __cplusplus
     41     extern "C" {
     42 #endif
     43 
     44 #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
     45     WOLFSSL_API void wolfSSL_SetMemFailCount(int memFailCount);
     46 #endif
     47 
     48 #ifdef OPENSSL_EXTRA
     49     typedef void *(*wolfSSL_OSSL_Malloc_cb)(size_t, const char *, int);
     50     typedef void  (*wolfSSL_OSSL_Free_cb)(void *, const char *, int);
     51     typedef void *(*wolfSSL_OSSL_Realloc_cb)(void *, size_t, const char *, int);
     52 #endif /* OPENSSL_EXTRA */
     53 
     54 #ifdef WOLFSSL_STATIC_MEMORY
     55     #ifdef WOLFSSL_DEBUG_MEMORY
     56         typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type, const char* func, unsigned int line);
     57         typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type, const char* func, unsigned int line);
     58         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
     59         WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line);
     60         WOLFSSL_API void  wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line);
     61         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
     62     #else
     63         typedef void *(*wolfSSL_Malloc_cb)(size_t size, void* heap, int type);
     64         typedef void (*wolfSSL_Free_cb)(void *ptr, void* heap, int type);
     65         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, void* heap, int type);
     66         WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
     67         WOLFSSL_API void  wolfSSL_Free(void *ptr, void* heap, int type);
     68         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
     69     #endif /* WOLFSSL_DEBUG_MEMORY */
     70 #else
     71     #ifdef WOLFSSL_DEBUG_MEMORY
     72         typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
     73         typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
     74         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
     75 
     76         /* Public in case user app wants to use XMALLOC/XFREE */
     77         WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
     78         WOLFSSL_API void  wolfSSL_Free(void *ptr, const char* func, unsigned int line);
     79         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
     80     #else
     81         typedef void *(*wolfSSL_Malloc_cb)(size_t size);
     82         typedef void (*wolfSSL_Free_cb)(void *ptr);
     83         typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
     84         /* Public in case user app wants to use XMALLOC/XFREE */
     85         WOLFSSL_API void* wolfSSL_Malloc(size_t size);
     86         WOLFSSL_API void  wolfSSL_Free(void *ptr);
     87         WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
     88     #endif /* WOLFSSL_DEBUG_MEMORY */
     89 #endif /* WOLFSSL_STATIC_MEMORY */
     90 
     91 /* Public get/set functions */
     92 WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
     93                                       wolfSSL_Free_cb ff,
     94                                       wolfSSL_Realloc_cb rf);
     95 WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
     96                                       wolfSSL_Free_cb* ff,
     97                                       wolfSSL_Realloc_cb* rf);
     98 
     99 #ifdef WOLFSSL_STATIC_MEMORY
    100     #define WOLFSSL_STATIC_TIMEOUT 1
    101     #ifndef WOLFSSL_STATIC_ALIGN
    102         #define WOLFSSL_STATIC_ALIGN 16
    103     #endif
    104 /* WOLFMEM_BUCKETS - list of the sizes of buckets in the pool
    105  * WOLFMEM_DIST - list of quantities of buffers in the buckets
    106  * WOLFMEM_DEF_BUCKETS - number of values in WOLFMEM_BUCKETS and WOLFMEM_DIST
    107  * WOLFMEM_MAX_BUCKETS - size of the arrays used to store the buckets and
    108  *     dists in the memory pool; defaults to WOLFMEM_DEF_BUCKETS
    109  *
    110  * The following defines provide a reasonable set of buckets in the memory
    111  * pool for running wolfSSL on a Linux box. The bucket and dist lists below
    112  * have nine items each, so WOLFMEM_DEF_BUCKETS is set to 9.
    113  *
    114  * If WOLFMEM_DEF_BUCKETS is less then WOLFMEM_MAX_BUCKETS, the unused values
    115  * are set to zero and ignored. If WOLFMEM_MAX_BUCKETS is less than
    116  * WOLFMEM_DEF_BUCKETS, not all the buckets will be created in the pool.
    117  */
    118     #ifndef WOLFMEM_DEF_BUCKETS
    119         #define WOLFMEM_DEF_BUCKETS  9  /* number of default memory blocks */
    120     #endif
    121 
    122     #ifndef WOLFMEM_MAX_BUCKETS
    123         #define WOLFMEM_MAX_BUCKETS  WOLFMEM_DEF_BUCKETS
    124     #endif
    125 
    126     #if WOLFMEM_MAX_BUCKETS < WOLFMEM_DEF_BUCKETS
    127         #warning "ignoring excess buckets, MAX_BUCKETS less than DEF_BUCKETS"
    128     #endif
    129 
    130     #ifndef WOLFMEM_IO_SZ
    131         #define WOLFMEM_IO_SZ        16992 /* 16 byte aligned */
    132     #endif
    133 
    134     #ifndef LARGEST_MEM_BUCKET
    135         #ifndef SESSION_CERTS
    136             #ifdef HAVE_DILITHIUM
    137                 #if defined(WOLFSSL_DILITHIUM_VERIFY_SMALL_MEM) && \
    138                     defined(WOLFSSL_DILITHIUM_SIGN_SMALL_MEM) && \
    139                     defined(WOLFSSL_DILITHIUM_MAKE_KEY_SMALL_MEM) && \
    140                     defined(WOLFSSL_DILITHIUM_VERIFY_ONLY)
    141                     #define LARGEST_MEM_BUCKET 14000 /* Dilithium low mem */
    142                 #else
    143                     #define LARGEST_MEM_BUCKET 131072 /* Dilithium full mem */
    144                 #endif
    145             #else
    146                 #define LARGEST_MEM_BUCKET 16128
    147             #endif
    148         #elif defined(OPENSSL_EXTRA)
    149             #ifdef WOLFSSL_TLS13
    150                 #define LARGEST_MEM_BUCKET 30400
    151             #else
    152                 #define LARGEST_MEM_BUCKET 25600
    153             #endif
    154         #elif defined(WOLFSSL_CERT_EXT)
    155             /* certificate extensions requires 24k for the SSL struct */
    156             #define LARGEST_MEM_BUCKET 24576
    157         #else
    158             /* increase 23k for object member of WOLFSSL_X509_NAME_ENTRY */
    159             #define LARGEST_MEM_BUCKET 23440
    160         #endif
    161     #endif
    162 
    163     #ifndef WOLFMEM_BUCKETS
    164         #ifndef SESSION_CERTS
    165             #ifdef HAVE_DILITHIUM
    166                 #if defined(WOLFSSL_DILITHIUM_VERIFY_SMALL_MEM) && \
    167                     defined(WOLFSSL_DILITHIUM_SIGN_SMALL_MEM) && \
    168                     defined(WOLFSSL_DILITHIUM_MAKE_KEY_SMALL_MEM) && \
    169                     defined(WOLFSSL_DILITHIUM_VERIFY_ONLY)
    170                     /* default size of chunks of memory to separate into */
    171                     #define WOLFMEM_BUCKETS 64,128,256,512,1024,2048,4096,\
    172                                            8192,LARGEST_MEM_BUCKET
    173                 #else
    174                     /* default size of chunks of memory to separate into */
    175                     #define WOLFMEM_BUCKETS 64,128,256,512,1024,8192,32768,\
    176                                             65536,LARGEST_MEM_BUCKET
    177                 #endif
    178             #else
    179                 /* default size of chunks of memory to separate into */
    180                 #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
    181                                         LARGEST_MEM_BUCKET
    182             #endif
    183         #elif defined(OPENSSL_EXTRA)
    184             /* extra storage in structs for multiple attributes and order */
    185             #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3360,4480,\
    186                                     LARGEST_MEM_BUCKET
    187         #elif defined(WOLFSSL_CERT_EXT)
    188             #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
    189                                     LARGEST_MEM_BUCKET
    190         #else
    191             #define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
    192                                     LARGEST_MEM_BUCKET
    193         #endif
    194     #endif
    195 
    196     #ifndef WOLFMEM_DIST
    197         #ifdef HAVE_DILITHIUM
    198             #if defined(WOLFSSL_DILITHIUM_VERIFY_SMALL_MEM) && \
    199                 defined(WOLFSSL_DILITHIUM_SIGN_SMALL_MEM) && \
    200                 defined(WOLFSSL_DILITHIUM_MAKE_KEY_SMALL_MEM) && \
    201                 defined(WOLFSSL_DILITHIUM_VERIFY_ONLY)
    202                 #define WOLFMEM_DIST    20,8,6,10,8,6,4,2,1
    203             #else
    204                 #define WOLFMEM_DIST    30,10,8,15,8,10,8,5,1
    205             #endif
    206         #elif !defined(WOLFSSL_STATIC_MEMORY_SMALL)
    207             #define WOLFMEM_DIST    49,10,6,14,5,6,9,1,1
    208         #else
    209             /* Low resource and not RSA */
    210             #define WOLFMEM_DIST    29, 7,6, 9,4,4,0,0,0
    211         #endif
    212     #endif
    213 
    214     /* flags for loading static memory (one hot bit) */
    215     #define WOLFMEM_GENERAL       0x01
    216     #define WOLFMEM_IO_POOL       0x02
    217     #define WOLFMEM_IO_POOL_FIXED 0x04
    218     #define WOLFMEM_TRACK_STATS   0x08
    219 
    220     #ifndef WOLFSSL_MEM_GUARD
    221     #define WOLFSSL_MEM_GUARD
    222         typedef struct WOLFSSL_MEM_STATS      WOLFSSL_MEM_STATS;
    223         typedef struct WOLFSSL_MEM_CONN_STATS WOLFSSL_MEM_CONN_STATS;
    224     #endif
    225 
    226     struct WOLFSSL_MEM_CONN_STATS {
    227         word32 peakMem;   /* peak memory usage    */
    228         word32 curMem;    /* current memory usage */
    229         word32 peakAlloc; /* peak memory allocations */
    230         word32 curAlloc;  /* current memory allocations */
    231         word32 totalAlloc;/* total memory allocations for lifetime */
    232         word32 totalFr;   /* total frees for lifetime */
    233     };
    234 
    235     struct WOLFSSL_MEM_STATS {
    236         word32 curAlloc;  /* current memory allocations */
    237         word32 totalAlloc;/* total memory allocations for lifetime */
    238         word32 totalFr;   /* total frees for lifetime */
    239         word32 totalUse;  /* total amount of memory used in blocks */
    240         word32 avaIO;     /* available IO specific pools */
    241         word32 maxHa;     /* max number of concurrent handshakes allowed */
    242         word32 maxIO;     /* max number of concurrent IO connections allowed */
    243         word32 blockSz[WOLFMEM_MAX_BUCKETS]; /* block sizes in stacks */
    244         word32 avaBlock[WOLFMEM_MAX_BUCKETS];/* ava block sizes */
    245         word32 usedBlock[WOLFMEM_MAX_BUCKETS];
    246         int    flag; /* flag used */
    247     };
    248 
    249     typedef struct wc_Memory wc_Memory; /* internal structure for mem bucket */
    250     typedef struct WOLFSSL_HEAP {
    251         wc_Memory* ava[WOLFMEM_MAX_BUCKETS];
    252     #ifndef WOLFSSL_STATIC_MEMORY_LEAN
    253         wc_Memory* io;                  /* list of buffers to use for IO */
    254     #endif
    255 
    256     #ifdef WOLFSSL_STATIC_MEMORY_LEAN
    257         word32     sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */
    258         word32     distList[WOLFMEM_MAX_BUCKETS];/* general distribution */
    259     #else
    260         word32     maxHa;               /* max concurrent handshakes */
    261         word32     curHa;
    262         word32     maxIO;               /* max concurrent IO connections */
    263         word32     curIO;
    264         word32     sizeList[WOLFMEM_MAX_BUCKETS];/* memory sizes in ava list */
    265         word32     distList[WOLFMEM_MAX_BUCKETS];/* general distribution */
    266         word32     inUse; /* amount of memory currently in use */
    267         word32     ioUse;
    268     #endif
    269 
    270     #ifndef WOLFSSL_STATIC_MEMORY_LEAN
    271         word32     alloc; /* total number of allocs */
    272         word32     frAlc; /* total number of frees  */
    273         int        flag;
    274     #endif
    275     #ifndef SINGLE_THREADED
    276         wolfSSL_Mutex memory_mutex;
    277     #endif
    278     } WOLFSSL_HEAP;
    279 
    280     /* structure passed into XMALLOC as heap hint
    281      * having this abstraction allows tracking statistics of individual ssl's
    282      */
    283     typedef struct WOLFSSL_HEAP_HINT {
    284         WOLFSSL_HEAP*           memory;
    285         WOLFSSL_MEM_CONN_STATS* stats;  /* hold individual connection stats */
    286     #ifndef WOLFSSL_STATIC_MEMORY_LEAN
    287         wc_Memory*  outBuf; /* set if using fixed io buffers */
    288         wc_Memory*  inBuf;
    289         byte        haFlag; /* flag used for checking handshake count */
    290     #endif
    291     } WOLFSSL_HEAP_HINT;
    292 
    293     WOLFSSL_API void* wolfSSL_SetGlobalHeapHint(void* heap);
    294     WOLFSSL_API void* wolfSSL_GetGlobalHeapHint(void);
    295     WOLFSSL_API int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
    296             unsigned int listSz, const word32 *sizeList,
    297             const word32 *distList, unsigned char* buf, unsigned int sz,
    298             int flag, int max);
    299 #ifdef WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK
    300     #define WOLFSSL_DEBUG_MEMORY_ALLOC 0
    301     #define WOLFSSL_DEBUG_MEMORY_FAIL  1
    302     #define WOLFSSL_DEBUG_MEMORY_FREE  2
    303     #define WOLFSSL_DEBUG_MEMORY_INIT  3
    304 
    305 
    306     typedef void (*DebugMemoryCb)(size_t sz, int bucketSz, byte st, int type);
    307     WOLFSSL_API void wolfSSL_SetDebugMemoryCb(DebugMemoryCb cb);
    308 #endif
    309     WOLFSSL_API int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
    310             unsigned char* buf, unsigned int sz, int flag, int max);
    311     WOLFSSL_API void wc_UnloadStaticMemory(WOLFSSL_HEAP_HINT* heap);
    312 
    313     WOLFSSL_API int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap,
    314                                                       WOLFSSL_MEM_STATS* stats);
    315     WOLFSSL_LOCAL int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
    316     WOLFSSL_LOCAL int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io);
    317 
    318     WOLFSSL_API int wolfSSL_StaticBufferSz_ex(unsigned int listSz,
    319             const word32 *sizeList, const word32 *distList,
    320             byte* buffer, word32 sz, int flag);
    321     WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag);
    322     WOLFSSL_API int wolfSSL_MemoryPaddingSz(void);
    323 #endif /* WOLFSSL_STATIC_MEMORY */
    324 
    325 #ifdef WOLFSSL_STACK_LOG
    326     WOLFSSL_API void __attribute__((no_instrument_function))
    327             __cyg_profile_func_enter(void *func,  void *caller);
    328     WOLFSSL_API void __attribute__((no_instrument_function))
    329             __cyg_profile_func_exit(void *func, void *caller);
    330 #endif /* WOLFSSL_STACK_LOG */
    331 
    332 #ifdef WOLFSSL_MEM_FAIL_COUNT
    333 WOLFSSL_LOCAL void wc_MemFailCount_Init(void);
    334 WOLFSSL_LOCAL void wc_MemFailCount_Free(void);
    335 #endif
    336 
    337 #ifdef WOLFSSL_CHECK_MEM_ZERO
    338 WOLFSSL_LOCAL void wc_MemZero_Init(void);
    339 WOLFSSL_LOCAL void wc_MemZero_Free(void);
    340 WOLFSSL_LOCAL void wc_MemZero_Add(const char* name, const void* addr,
    341     size_t len);
    342 WOLFSSL_LOCAL void wc_MemZero_Check(void* addr, size_t len);
    343 #endif
    344 
    345 #ifdef WC_DEBUG_CIPHER_LIFECYCLE
    346 WOLFSSL_LOCAL int wc_debug_CipherLifecycleInit(void **CipherLifecycleTag,
    347                                                void *heap);
    348 WOLFSSL_LOCAL int wc_debug_CipherLifecycleCheck(void *CipherLifecycleTag,
    349                                                 int abort_p);
    350 WOLFSSL_LOCAL int wc_debug_CipherLifecycleFree(void **CipherLifecycleTag,
    351                                                void *heap, int abort_p);
    352 #else
    353 #define wc_debug_CipherLifecycleInit(CipherLifecycleTag, heap) \
    354         ((void)(CipherLifecycleTag), (void)(heap), 0)
    355 #define wc_debug_CipherLifecycleCheck(CipherLifecycleTag, abort_p) \
    356         ((void)(CipherLifecycleTag), (void)(abort_p), 0)
    357 #define wc_debug_CipherLifecycleFree(CipherLifecycleTag, heap, abort_p) \
    358         ((void)(CipherLifecycleTag), (void)(heap), (void)(abort_p), 0)
    359 #endif
    360 
    361 #ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
    362     WOLFSSL_LOCAL int SAVE_VECTOR_REGISTERS2_fuzzer(void);
    363     #ifndef WC_DEBUG_VECTOR_REGISTERS_FUZZING_SEED
    364         #define WC_DEBUG_VECTOR_REGISTERS_FUZZING_SEED 0
    365     #endif
    366     #ifndef CAN_SAVE_VECTOR_REGISTERS
    367         #define CAN_SAVE_VECTOR_REGISTERS() (SAVE_VECTOR_REGISTERS2_fuzzer() == 0)
    368     #endif
    369 #endif
    370 
    371 #ifdef DEBUG_VECTOR_REGISTER_ACCESS
    372     WOLFSSL_API extern THREAD_LS_T int wc_svr_count;
    373     WOLFSSL_API extern THREAD_LS_T const char *wc_svr_last_file;
    374     WOLFSSL_API extern THREAD_LS_T int wc_svr_last_line;
    375 
    376     #ifdef DEBUG_VECTOR_REGISTERS_ABORT_ON_FAIL
    377         #define DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE abort();
    378     #elif defined(DEBUG_VECTOR_REGISTERS_EXIT_ON_FAIL)
    379         #define DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE exit(1);
    380     #elif !defined(DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE)
    381         #define DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE
    382     #endif
    383 
    384     #define SAVE_VECTOR_REGISTERS(fail_clause) {                    \
    385         int _svr_ret = wc_debug_vector_registers_retval;            \
    386         if (_svr_ret != 0) { fail_clause }                          \
    387         else {                                                      \
    388           ++wc_svr_count;                                           \
    389           if (wc_svr_count > 5) {                                   \
    390               fprintf(stderr,                                       \
    391                       ("%s @ L%d : incr : "                         \
    392                        "wc_svr_count %d (last op %s L%d)\n"),       \
    393                       __FILE__,                                     \
    394                       __LINE__,                                     \
    395                       wc_svr_count,                                 \
    396                       wc_svr_last_file,                             \
    397                       wc_svr_last_line);                            \
    398               DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE              \
    399           }                                                         \
    400           wc_svr_last_file = __FILE__;                              \
    401           wc_svr_last_line = __LINE__;                              \
    402         }                                                           \
    403     }
    404 
    405     WOLFSSL_API extern THREAD_LS_T int wc_debug_vector_registers_retval;
    406 
    407 #ifndef WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL
    408 #define WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL 0
    409 #endif
    410 #define WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(x) do { \
    411             if (((x) != 0) && (wc_svr_count > 0)) {                 \
    412                 fprintf(stderr,                                     \
    413                         ("%s @ L%d : incr : "                       \
    414                          "wc_svr_count %d (last op %s L%d)\n"),     \
    415                         __FILE__,                                   \
    416                         __LINE__,                                   \
    417                         wc_svr_count,                               \
    418                         wc_svr_last_file,                           \
    419                         wc_svr_last_line);                          \
    420                 DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE            \
    421             }                                                       \
    422         wc_debug_vector_registers_retval = (x);                     \
    423     } while (0)
    424 
    425 #ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
    426     #define SAVE_VECTOR_REGISTERS2(...) ({                          \
    427         int _svr2_val = SAVE_VECTOR_REGISTERS2_fuzzer();            \
    428         if (_svr2_val == 0) {                                       \
    429             ++wc_svr_count;                                         \
    430             if (wc_svr_count > 5) {                                 \
    431                 fprintf(stderr,                                     \
    432                         ("%s @ L%d : incr : "                       \
    433                          "wc_svr_count %d (last op %s L%d)\n"),     \
    434                         __FILE__,                                   \
    435                         __LINE__,                                   \
    436                         wc_svr_count,                               \
    437                         wc_svr_last_file,                           \
    438                         wc_svr_last_line);                          \
    439                 DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE            \
    440             }                                                       \
    441             wc_svr_last_file = __FILE__;                            \
    442             wc_svr_last_line = __LINE__;                            \
    443             _svr2_val = 0;                                          \
    444         }                                                           \
    445         _svr2_val;                                                  \
    446     })
    447 
    448 #else
    449 
    450     #define SAVE_VECTOR_REGISTERS2(...) ({                          \
    451         int _svr2_val;                                              \
    452         if (wc_debug_vector_registers_retval != 0) {                \
    453             if (wc_svr_count > 0) {                                 \
    454                 fprintf(stderr,                                     \
    455                         ("%s @ L%d : incr : "                       \
    456                         "wc_svr_count %d (last op %s L%d)\n"),      \
    457                         __FILE__,                                   \
    458                         __LINE__,                                   \
    459                         wc_svr_count,                               \
    460                         wc_svr_last_file,                           \
    461                         wc_svr_last_line);                          \
    462                 DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE            \
    463             }                                                       \
    464             _svr2_val = wc_debug_vector_registers_retval;           \
    465         } else {                                                    \
    466             ++wc_svr_count;                                         \
    467             if (wc_svr_count > 5) {                                 \
    468                 fprintf(stderr,                                     \
    469                         ("%s @ L%d : incr : "                       \
    470                          "wc_svr_count %d (last op %s L%d)\n"),     \
    471                         __FILE__,                                   \
    472                         __LINE__,                                   \
    473                         wc_svr_count,                               \
    474                         wc_svr_last_file,                           \
    475                         wc_svr_last_line);                          \
    476                 DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE            \
    477             }                                                       \
    478             wc_svr_last_file = __FILE__;                            \
    479             wc_svr_last_line = __LINE__;                            \
    480             _svr2_val = 0;                                          \
    481         }                                                           \
    482         _svr2_val;                                                  \
    483     })
    484 
    485 #endif
    486 
    487     #define ASSERT_SAVED_VECTOR_REGISTERS() do {                    \
    488         if (wc_svr_count <= 0) {                                    \
    489             fprintf(stderr,                                         \
    490                     ("ASSERT_SAVED_VECTOR_REGISTERS : %s @ L%d : "  \
    491                     "wc_svr_count %d (last op %s L%d)\n"),          \
    492                     __FILE__,                                       \
    493                     __LINE__,                                       \
    494                     wc_svr_count,                                   \
    495                     wc_svr_last_file,                               \
    496                     wc_svr_last_line);                              \
    497             DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE                \
    498         }                                                           \
    499     } while (0)
    500     #define ASSERT_RESTORED_VECTOR_REGISTERS(fail_clause) do {      \
    501         if (wc_svr_count != 0) {                                    \
    502             fprintf(stderr,                                         \
    503                     ("ASSERT_RESTORED_VECTOR_REGISTERS : %s @ L%d"  \
    504                      " : wc_svr_count %d (last op %s L%d)\n"),      \
    505                     __FILE__,                                       \
    506                     __LINE__,                                       \
    507                     wc_svr_count,                                   \
    508                     wc_svr_last_file,                               \
    509                     wc_svr_last_line);                              \
    510             DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE                \
    511             { fail_clause }                                         \
    512         }                                                           \
    513     } while (0)
    514     #define RESTORE_VECTOR_REGISTERS() do {                         \
    515         --wc_svr_count;                                             \
    516         if ((wc_svr_count > 4) || (wc_svr_count < 0)) {             \
    517             fprintf(stderr,                                         \
    518                     ("%s @ L%d : decr : "                           \
    519                      "wc_svr_count %d (last op %s L%d)\n"),         \
    520                     __FILE__,                                       \
    521                     __LINE__,                                       \
    522                     wc_svr_count,                                   \
    523                     wc_svr_last_file,                               \
    524                     wc_svr_last_line);                              \
    525             DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE                \
    526         }                                                           \
    527         wc_svr_last_file = __FILE__;                                \
    528         wc_svr_last_line = __LINE__;                                \
    529     } while(0)
    530 
    531 #else /* !DEBUG_VECTOR_REGISTER_ACCESS */
    532     #if !defined(SAVE_VECTOR_REGISTERS2) && defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
    533         #define SAVE_VECTOR_REGISTERS2(...) SAVE_VECTOR_REGISTERS2_fuzzer()
    534     #endif
    535 #endif
    536 
    537 #ifdef __cplusplus
    538     }  /* extern "C" */
    539 #endif
    540 
    541 #endif /* WOLFSSL_MEMORY_H */