Skip to content

Commit b951fd9

Browse files
Merge pull request #310 from gilles-peskine-arm/memory_buffer_alloc-fatal-pass
Clarify test descriptions in test_suite_memory_buffer_alloc
2 parents 8f4df81 + cf5abd8 commit b951fd9

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

tests/suites/test_suite_memory_buffer_alloc.data

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ memory_buffer_alloc_free_alloc:100:64:100:100:0:0:0:1:200:0
1616
Memory buffer alloc - Out of Memory test
1717
memory_buffer_alloc_oom_test:
1818

19-
Memory buffer small buffer
20-
memory_buffer_small_buffer:
19+
Memory buffer: heap too small (header verification should fail)
20+
memory_buffer_heap_too_small:
2121

22-
Memory buffer underalloc
22+
Memory buffer: attempt to allocate SIZE_MAX
2323
memory_buffer_underalloc:

tests/suites/test_suite_memory_buffer_alloc.function

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void mbedtls_memory_buffer_alloc_self_test( )
2929
}
3030
/* END_CASE */
3131

32-
/* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
32+
/* BEGIN_CASE */
3333
void memory_buffer_alloc_free_alloc( int a_bytes, int b_bytes, int c_bytes,
3434
int d_bytes, int free_a, int free_b,
3535
int free_c, int free_d, int e_bytes,
@@ -39,8 +39,11 @@ void memory_buffer_alloc_free_alloc( int a_bytes, int b_bytes, int c_bytes,
3939
unsigned char *ptr_a = NULL, *ptr_b = NULL, *ptr_c = NULL, *ptr_d = NULL,
4040
*ptr_e = NULL, *ptr_f = NULL;
4141

42+
#if defined(MBEDTLS_MEMORY_DEBUG)
4243
size_t reported_blocks;
43-
size_t allocated_bytes = 0, reported_bytes;
44+
size_t reported_bytes;
45+
#endif
46+
size_t allocated_bytes = 0;
4447

4548
mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
4649

@@ -78,8 +81,10 @@ void memory_buffer_alloc_free_alloc( int a_bytes, int b_bytes, int c_bytes,
7881
allocated_bytes += d_bytes * sizeof(char);
7982
}
8083

84+
#if defined(MBEDTLS_MEMORY_DEBUG)
8185
mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
8286
TEST_ASSERT( reported_bytes == allocated_bytes );
87+
#endif
8388

8489
if( free_a )
8590
{
@@ -117,8 +122,10 @@ void memory_buffer_alloc_free_alloc( int a_bytes, int b_bytes, int c_bytes,
117122
allocated_bytes -= d_bytes * sizeof(char);
118123
}
119124

125+
#if defined(MBEDTLS_MEMORY_DEBUG)
120126
mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
121127
TEST_ASSERT( reported_bytes == allocated_bytes );
128+
#endif
122129

123130
if( e_bytes > 0 )
124131
{
@@ -178,8 +185,10 @@ void memory_buffer_alloc_free_alloc( int a_bytes, int b_bytes, int c_bytes,
178185
ptr_f = NULL;
179186
}
180187

188+
#if defined(MBEDTLS_MEMORY_DEBUG)
181189
mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
182190
TEST_ASSERT( reported_bytes == 0 );
191+
#endif
183192

184193
TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
185194

@@ -188,12 +197,14 @@ exit:
188197
}
189198
/* END_CASE */
190199

191-
/* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
200+
/* BEGIN_CASE */
192201
void memory_buffer_alloc_oom_test( )
193202
{
194203
unsigned char buf[1024];
195204
unsigned char *ptr_a = NULL, *ptr_b = NULL, *ptr_c = NULL;
205+
#if defined(MBEDTLS_MEMORY_DEBUG)
196206
size_t reported_blocks, reported_bytes;
207+
#endif
197208

198209
(void)ptr_c;
199210

@@ -210,8 +221,10 @@ void memory_buffer_alloc_oom_test( )
210221
ptr_c = mbedtls_calloc( 431, sizeof(char) );
211222
TEST_ASSERT( ptr_c == NULL );
212223

224+
#if defined(MBEDTLS_MEMORY_DEBUG)
213225
mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
214226
TEST_ASSERT( reported_bytes >= 864 && reported_bytes <= sizeof(buf) );
227+
#endif
215228

216229
mbedtls_free( ptr_a );
217230
ptr_a = NULL;
@@ -221,8 +234,10 @@ void memory_buffer_alloc_oom_test( )
221234
ptr_b = NULL;
222235
TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
223236

237+
#if defined(MBEDTLS_MEMORY_DEBUG)
224238
mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
225239
TEST_ASSERT( reported_bytes == 0 );
240+
#endif
226241

227242
TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
228243

@@ -231,17 +246,20 @@ exit:
231246
}
232247
/* END_CASE */
233248

234-
/* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
235-
void memory_buffer_small_buffer( )
249+
/* BEGIN_CASE */
250+
void memory_buffer_heap_too_small( )
236251
{
237252
unsigned char buf[1];
238253

239254
mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
255+
/* With MBEDTLS_MEMORY_DEBUG enabled, this prints a message
256+
* "FATAL: verification of first header failed".
257+
*/
240258
TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() != 0 );
241259
}
242260
/* END_CASE */
243261

244-
/* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
262+
/* BEGIN_CASE */
245263
void memory_buffer_underalloc( )
246264
{
247265
unsigned char buf[100];

0 commit comments

Comments
 (0)