The Great Renaming
A simple execution of tmp/invoke-rename.pl
This commit is contained in:
parent
b5904d25ef
commit
2cf5a7c98e
291 changed files with 36012 additions and 36012 deletions
|
@ -20,31 +20,31 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
#include "mbedtls/memory_buffer_alloc.h"
|
||||
|
||||
/* No need for the header guard as POLARSSL_MEMORY_BUFFER_ALLOC_C
|
||||
is dependent upon POLARSSL_PLATFORM_C */
|
||||
/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
|
||||
is dependent upon MBEDTLS_PLATFORM_C */
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
#include <execinfo.h>
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
#include "mbedtls/threading.h"
|
||||
#endif
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void polarssl_zeroize( void *v, size_t n ) {
|
||||
static void mbedtls_zeroize( void *v, size_t n ) {
|
||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,7 @@ struct _memory_header
|
|||
memory_header *next;
|
||||
memory_header *prev_free;
|
||||
memory_header *next_free;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
char **trace;
|
||||
size_t trace_count;
|
||||
#endif
|
||||
|
@ -76,7 +76,7 @@ typedef struct
|
|||
memory_header *first;
|
||||
memory_header *first_free;
|
||||
int verify;
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
size_t malloc_count;
|
||||
size_t free_count;
|
||||
size_t total_used;
|
||||
|
@ -84,33 +84,33 @@ typedef struct
|
|||
size_t header_count;
|
||||
size_t maximum_header_count;
|
||||
#endif
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
threading_mutex_t mutex;
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_threading_mutex_t mutex;
|
||||
#endif
|
||||
}
|
||||
buffer_alloc_ctx;
|
||||
|
||||
static buffer_alloc_ctx heap;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
static void debug_header( memory_header *hdr )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
size_t i;
|
||||
#endif
|
||||
|
||||
polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
|
||||
mbedtls_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
|
||||
"ALLOC(%zu), SIZE(%10zu)\n",
|
||||
(size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
|
||||
hdr->alloc, hdr->size );
|
||||
polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
|
||||
mbedtls_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
|
||||
(size_t) hdr->prev_free, (size_t) hdr->next_free );
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
polarssl_fprintf( stderr, "TRACE: \n" );
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
mbedtls_fprintf( stderr, "TRACE: \n" );
|
||||
for( i = 0; i < hdr->trace_count; i++ )
|
||||
polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
|
||||
polarssl_fprintf( stderr, "\n" );
|
||||
mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
|
||||
mbedtls_fprintf( stderr, "\n" );
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -118,14 +118,14 @@ static void debug_chain()
|
|||
{
|
||||
memory_header *cur = heap.first;
|
||||
|
||||
polarssl_fprintf( stderr, "\nBlock list\n" );
|
||||
mbedtls_fprintf( stderr, "\nBlock list\n" );
|
||||
while( cur != NULL )
|
||||
{
|
||||
debug_header( cur );
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
polarssl_fprintf( stderr, "Free list\n" );
|
||||
mbedtls_fprintf( stderr, "Free list\n" );
|
||||
cur = heap.first_free;
|
||||
|
||||
while( cur != NULL )
|
||||
|
@ -134,46 +134,46 @@ static void debug_chain()
|
|||
cur = cur->next_free;
|
||||
}
|
||||
}
|
||||
#endif /* POLARSSL_MEMORY_DEBUG */
|
||||
#endif /* MBEDTLS_MEMORY_DEBUG */
|
||||
|
||||
static int verify_header( memory_header *hdr )
|
||||
{
|
||||
if( hdr->magic1 != MAGIC1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->magic2 != MAGIC2 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->alloc > 1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->prev != NULL && hdr->prev == hdr->next )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: prev == next\n" );
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
@ -187,8 +187,8 @@ static int verify_chain()
|
|||
|
||||
if( verify_header( heap.first ) != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification of first header "
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: verification of first header "
|
||||
"failed\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
|
@ -196,8 +196,8 @@ static int verify_chain()
|
|||
|
||||
if( heap.first->prev != NULL )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification failed: "
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: verification failed: "
|
||||
"first->prev != NULL\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
|
@ -207,8 +207,8 @@ static int verify_chain()
|
|||
{
|
||||
if( verify_header( cur ) != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification of header "
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: verification of header "
|
||||
"failed\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
|
@ -216,8 +216,8 @@ static int verify_chain()
|
|||
|
||||
if( cur->prev != prv )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification failed: "
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: verification failed: "
|
||||
"cur->prev != prv\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
|
@ -234,7 +234,7 @@ static void *buffer_alloc_malloc( size_t len )
|
|||
{
|
||||
memory_header *new, *cur = heap.first_free;
|
||||
unsigned char *p;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
void *trace_buffer[MAX_BT];
|
||||
size_t trace_cnt;
|
||||
#endif
|
||||
|
@ -242,10 +242,10 @@ static void *buffer_alloc_malloc( size_t len )
|
|||
if( heap.buf == NULL || heap.first == NULL )
|
||||
return( NULL );
|
||||
|
||||
if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
|
||||
len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
|
||||
}
|
||||
|
||||
// Find block that fits
|
||||
|
@ -263,21 +263,21 @@ static void *buffer_alloc_malloc( size_t len )
|
|||
|
||||
if( cur->alloc != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
|
||||
"data\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
mbedtls_exit( 1 );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
heap.malloc_count++;
|
||||
#endif
|
||||
|
||||
// Found location, split block if > memory_header + 4 room left
|
||||
//
|
||||
if( cur->size - len < sizeof(memory_header) +
|
||||
POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
MBEDTLS_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
cur->alloc = 1;
|
||||
|
||||
|
@ -294,19 +294,19 @@ static void *buffer_alloc_malloc( size_t len )
|
|||
cur->prev_free = NULL;
|
||||
cur->next_free = NULL;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
heap.total_used += cur->size;
|
||||
if( heap.total_used > heap.maximum_used )
|
||||
heap.maximum_used = heap.total_used;
|
||||
#endif
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
trace_cnt = backtrace( trace_buffer, MAX_BT );
|
||||
cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
|
||||
cur->trace_count = trace_cnt;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
|
||||
mbedtls_exit( 1 );
|
||||
|
||||
return( ( (unsigned char *) cur ) + sizeof(memory_header) );
|
||||
}
|
||||
|
@ -318,7 +318,7 @@ static void *buffer_alloc_malloc( size_t len )
|
|||
new->alloc = 0;
|
||||
new->prev = cur;
|
||||
new->next = cur->next;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
new->trace = NULL;
|
||||
new->trace_count = 0;
|
||||
#endif
|
||||
|
@ -346,7 +346,7 @@ static void *buffer_alloc_malloc( size_t len )
|
|||
cur->prev_free = NULL;
|
||||
cur->next_free = NULL;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
heap.header_count++;
|
||||
if( heap.header_count > heap.maximum_header_count )
|
||||
heap.maximum_header_count = heap.header_count;
|
||||
|
@ -354,14 +354,14 @@ static void *buffer_alloc_malloc( size_t len )
|
|||
if( heap.total_used > heap.maximum_used )
|
||||
heap.maximum_used = heap.total_used;
|
||||
#endif
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
trace_cnt = backtrace( trace_buffer, MAX_BT );
|
||||
cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
|
||||
cur->trace_count = trace_cnt;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
|
||||
mbedtls_exit( 1 );
|
||||
|
||||
return( ( (unsigned char *) cur ) + sizeof(memory_header) );
|
||||
}
|
||||
|
@ -376,31 +376,31 @@ static void buffer_alloc_free( void *ptr )
|
|||
|
||||
if( p < heap.buf || p > heap.buf + heap.len )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
|
||||
"space\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
mbedtls_exit( 1 );
|
||||
}
|
||||
|
||||
p -= sizeof(memory_header);
|
||||
hdr = (memory_header *) p;
|
||||
|
||||
if( verify_header( hdr ) != 0 )
|
||||
polarssl_exit( 1 );
|
||||
mbedtls_exit( 1 );
|
||||
|
||||
if( hdr->alloc != 1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
|
||||
"data\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
mbedtls_exit( 1 );
|
||||
}
|
||||
|
||||
hdr->alloc = 0;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
heap.free_count++;
|
||||
heap.total_used -= hdr->size;
|
||||
#endif
|
||||
|
@ -409,7 +409,7 @@ static void buffer_alloc_free( void *ptr )
|
|||
//
|
||||
if( hdr->prev != NULL && hdr->prev->alloc == 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
heap.header_count--;
|
||||
#endif
|
||||
hdr->prev->size += sizeof(memory_header) + hdr->size;
|
||||
|
@ -420,7 +420,7 @@ static void buffer_alloc_free( void *ptr )
|
|||
if( hdr->next != NULL )
|
||||
hdr->next->prev = hdr;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
free( old->trace );
|
||||
#endif
|
||||
memset( old, 0, sizeof(memory_header) );
|
||||
|
@ -430,7 +430,7 @@ static void buffer_alloc_free( void *ptr )
|
|||
//
|
||||
if( hdr->next != NULL && hdr->next->alloc == 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
heap.header_count--;
|
||||
#endif
|
||||
hdr->size += sizeof(memory_header) + hdr->next->size;
|
||||
|
@ -462,7 +462,7 @@ static void buffer_alloc_free( void *ptr )
|
|||
if( hdr->next != NULL )
|
||||
hdr->next->prev = hdr;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
free( old->trace );
|
||||
#endif
|
||||
memset( old, 0, sizeof(memory_header) );
|
||||
|
@ -479,29 +479,29 @@ static void buffer_alloc_free( void *ptr )
|
|||
heap.first_free = hdr;
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#if defined(MBEDTLS_MEMORY_BACKTRACE)
|
||||
hdr->trace = NULL;
|
||||
hdr->trace_count = 0;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
|
||||
mbedtls_exit( 1 );
|
||||
}
|
||||
|
||||
void memory_buffer_set_verify( int verify )
|
||||
void mbedtls_memory_buffer_set_verify( int verify )
|
||||
{
|
||||
heap.verify = verify;
|
||||
}
|
||||
|
||||
int memory_buffer_alloc_verify()
|
||||
int mbedtls_memory_buffer_alloc_verify()
|
||||
{
|
||||
return verify_chain();
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
void memory_buffer_alloc_status()
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
void mbedtls_memory_buffer_alloc_status()
|
||||
{
|
||||
polarssl_fprintf( stderr,
|
||||
mbedtls_fprintf( stderr,
|
||||
"Current use: %zu blocks / %zu bytes, max: %zu blocks / "
|
||||
"%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
|
||||
heap.header_count, heap.total_used,
|
||||
|
@ -511,71 +511,71 @@ void memory_buffer_alloc_status()
|
|||
heap.malloc_count, heap.free_count );
|
||||
|
||||
if( heap.first->next == NULL )
|
||||
polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
|
||||
mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
|
||||
else
|
||||
{
|
||||
polarssl_fprintf( stderr, "Memory currently allocated:\n" );
|
||||
mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
|
||||
debug_chain();
|
||||
}
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
|
||||
void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
|
||||
{
|
||||
*max_used = heap.maximum_used;
|
||||
*max_blocks = heap.maximum_header_count;
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_max_reset( void )
|
||||
void mbedtls_memory_buffer_alloc_max_reset( void )
|
||||
{
|
||||
heap.maximum_used = 0;
|
||||
heap.maximum_header_count = 0;
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
|
||||
void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
|
||||
{
|
||||
*cur_used = heap.total_used;
|
||||
*cur_blocks = heap.header_count;
|
||||
}
|
||||
#endif /* POLARSSL_MEMORY_DEBUG */
|
||||
#endif /* MBEDTLS_MEMORY_DEBUG */
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
static void *buffer_alloc_malloc_mutexed( size_t len )
|
||||
{
|
||||
void *buf;
|
||||
polarssl_mutex_lock( &heap.mutex );
|
||||
mbedtls_mutex_lock( &heap.mutex );
|
||||
buf = buffer_alloc_malloc( len );
|
||||
polarssl_mutex_unlock( &heap.mutex );
|
||||
mbedtls_mutex_unlock( &heap.mutex );
|
||||
return( buf );
|
||||
}
|
||||
|
||||
static void buffer_alloc_free_mutexed( void *ptr )
|
||||
{
|
||||
polarssl_mutex_lock( &heap.mutex );
|
||||
mbedtls_mutex_lock( &heap.mutex );
|
||||
buffer_alloc_free( ptr );
|
||||
polarssl_mutex_unlock( &heap.mutex );
|
||||
mbedtls_mutex_unlock( &heap.mutex );
|
||||
}
|
||||
#endif /* POLARSSL_THREADING_C */
|
||||
#endif /* MBEDTLS_THREADING_C */
|
||||
|
||||
int memory_buffer_alloc_init( unsigned char *buf, size_t len )
|
||||
int mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
|
||||
{
|
||||
memset( &heap, 0, sizeof(buffer_alloc_ctx) );
|
||||
memset( buf, 0, len );
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
polarssl_mutex_init( &heap.mutex );
|
||||
platform_set_malloc_free( buffer_alloc_malloc_mutexed,
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_mutex_init( &heap.mutex );
|
||||
mbedtls_platform_set_malloc_free( buffer_alloc_malloc_mutexed,
|
||||
buffer_alloc_free_mutexed );
|
||||
#else
|
||||
platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
|
||||
mbedtls_platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
|
||||
#endif
|
||||
|
||||
if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
/* Adjust len first since buf is used in the computation */
|
||||
len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
|
||||
- (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
|
||||
- (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
|
||||
- (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
|
||||
buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
|
||||
- (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
|
||||
}
|
||||
|
||||
heap.buf = buf;
|
||||
|
@ -589,21 +589,21 @@ int memory_buffer_alloc_init( unsigned char *buf, size_t len )
|
|||
return( 0 );
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_free()
|
||||
void mbedtls_memory_buffer_alloc_free()
|
||||
{
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
polarssl_mutex_free( &heap.mutex );
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_mutex_free( &heap.mutex );
|
||||
#endif
|
||||
polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
|
||||
mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
static int check_pointer( void *p )
|
||||
{
|
||||
if( p == NULL )
|
||||
return( -1 );
|
||||
|
||||
if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
|
||||
if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
|
||||
return( -1 );
|
||||
|
||||
return( 0 );
|
||||
|
@ -612,7 +612,7 @@ static int check_pointer( void *p )
|
|||
static int check_all_free( )
|
||||
{
|
||||
if(
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
heap.total_used != 0 ||
|
||||
#endif
|
||||
heap.first != heap.first_free ||
|
||||
|
@ -628,107 +628,107 @@ static int check_all_free( )
|
|||
if( ! (condition) ) \
|
||||
{ \
|
||||
if( verbose != 0 ) \
|
||||
polarssl_printf( "failed\n" ); \
|
||||
mbedtls_printf( "failed\n" ); \
|
||||
\
|
||||
ret = 1; \
|
||||
goto cleanup; \
|
||||
}
|
||||
|
||||
int memory_buffer_alloc_self_test( int verbose )
|
||||
int mbedtls_memory_buffer_alloc_self_test( int verbose )
|
||||
{
|
||||
unsigned char buf[1024];
|
||||
unsigned char *p, *q, *r, *end;
|
||||
int ret = 0;
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
|
||||
mbedtls_printf( " MBA test #1 (basic alloc-free cycle): " );
|
||||
|
||||
memory_buffer_alloc_init( buf, sizeof( buf ) );
|
||||
mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
|
||||
|
||||
p = polarssl_malloc( 1 );
|
||||
q = polarssl_malloc( 128 );
|
||||
r = polarssl_malloc( 16 );
|
||||
p = mbedtls_malloc( 1 );
|
||||
q = mbedtls_malloc( 128 );
|
||||
r = mbedtls_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 &&
|
||||
check_pointer( q ) == 0 &&
|
||||
check_pointer( r ) == 0 );
|
||||
|
||||
polarssl_free( r );
|
||||
polarssl_free( q );
|
||||
polarssl_free( p );
|
||||
mbedtls_free( r );
|
||||
mbedtls_free( q );
|
||||
mbedtls_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
/* Memorize end to compare with the next test */
|
||||
end = heap.buf + heap.len;
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
mbedtls_memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
mbedtls_printf( "passed\n" );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #2 (buf not aligned): " );
|
||||
mbedtls_printf( " MBA test #2 (buf not aligned): " );
|
||||
|
||||
memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
|
||||
mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
|
||||
|
||||
TEST_ASSERT( heap.buf + heap.len == end );
|
||||
|
||||
p = polarssl_malloc( 1 );
|
||||
q = polarssl_malloc( 128 );
|
||||
r = polarssl_malloc( 16 );
|
||||
p = mbedtls_malloc( 1 );
|
||||
q = mbedtls_malloc( 128 );
|
||||
r = mbedtls_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 &&
|
||||
check_pointer( q ) == 0 &&
|
||||
check_pointer( r ) == 0 );
|
||||
|
||||
polarssl_free( r );
|
||||
polarssl_free( q );
|
||||
polarssl_free( p );
|
||||
mbedtls_free( r );
|
||||
mbedtls_free( q );
|
||||
mbedtls_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
mbedtls_memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
mbedtls_printf( "passed\n" );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #3 (full): " );
|
||||
mbedtls_printf( " MBA test #3 (full): " );
|
||||
|
||||
memory_buffer_alloc_init( buf, sizeof( buf ) );
|
||||
mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
|
||||
|
||||
p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
|
||||
p = mbedtls_malloc( sizeof( buf ) - sizeof( memory_header ) );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 );
|
||||
TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
|
||||
TEST_ASSERT( mbedtls_malloc( 1 ) == NULL );
|
||||
|
||||
polarssl_free( p );
|
||||
mbedtls_free( p );
|
||||
|
||||
p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
|
||||
q = polarssl_malloc( 16 );
|
||||
p = mbedtls_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
|
||||
q = mbedtls_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
|
||||
TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
|
||||
TEST_ASSERT( mbedtls_malloc( 1 ) == NULL );
|
||||
|
||||
polarssl_free( q );
|
||||
mbedtls_free( q );
|
||||
|
||||
TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
|
||||
TEST_ASSERT( mbedtls_malloc( 17 ) == NULL );
|
||||
|
||||
polarssl_free( p );
|
||||
mbedtls_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
mbedtls_memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
mbedtls_printf( "passed\n" );
|
||||
|
||||
cleanup:
|
||||
memory_buffer_alloc_free( );
|
||||
mbedtls_memory_buffer_alloc_free( );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_SELF_TEST */
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */
|
||||
#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue