diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2021-06-26 15:56:54 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2021-07-12 14:01:24 +0200 |
commit | 53b2821660bfcd2f56cb358491e235ffad26d1a8 (patch) | |
tree | c7139b95694ceefe71716b3bb65e738eb93580c2 | |
parent | f7574dad6deb2b845d228fd76b96f076abc7f840 (diff) |
validation: Add MemoryAllocationFailWhen()
Update #3269.
-rw-r--r-- | spec/build/testsuites/validation/libvalidation.yml | 1 | ||||
-rw-r--r-- | spec/build/testsuites/validation/validation-0.yml | 3 | ||||
-rw-r--r-- | testsuites/validation/tx-memory-alloc.c | 79 | ||||
-rw-r--r-- | testsuites/validation/tx-support.h | 12 |
4 files changed, 94 insertions, 1 deletions
diff --git a/spec/build/testsuites/validation/libvalidation.yml b/spec/build/testsuites/validation/libvalidation.yml index 98dd2e3623..77a36f420b 100644 --- a/spec/build/testsuites/validation/libvalidation.yml +++ b/spec/build/testsuites/validation/libvalidation.yml @@ -13,6 +13,7 @@ links: [] source: - testsuites/validation/tx-call-within-isr.c - testsuites/validation/tx-interrupt.c +- testsuites/validation/tx-memory-alloc.c - testsuites/validation/tx-support.c target: validation type: build diff --git a/spec/build/testsuites/validation/validation-0.yml b/spec/build/testsuites/validation/validation-0.yml index 6778a5ec25..ddcde4b0f5 100644 --- a/spec/build/testsuites/validation/validation-0.yml +++ b/spec/build/testsuites/validation/validation-0.yml @@ -8,7 +8,8 @@ cxxflags: [] enabled-by: true features: c cprogram includes: [] -ldflags: [] +ldflags: +- -Wl,--wrap=rtems_malloc links: [] source: - testsuites/validation/tc-barrier-create.c diff --git a/testsuites/validation/tx-memory-alloc.c b/testsuites/validation/tx-memory-alloc.c new file mode 100644 index 0000000000..82b79bc558 --- /dev/null +++ b/testsuites/validation/tx-memory-alloc.c @@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +/** + * @file + * + * @ingroup RTEMSTestSuites + * + * @brief This source file contains the implementation of + * MemoryAllocationFailWhen() and __wrap_rtems_malloc(). + */ + +/* + * Copyright (C) 2021 embedded brains GmbH (http://www.embedded-brains.de) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "tx-support.h" + +void *__real_rtems_malloc( size_t ); + +void *__wrap_rtems_malloc( size_t ); + +static uint32_t fail_when_counter; + +void MemoryAllocationFailWhen( uint32_t counter ) +{ + fail_when_counter = counter; +} + +static bool IsFail( void ) +{ + uint32_t counter; + + counter = fail_when_counter; + + if ( counter == 1 ) { + fail_when_counter = 0; + return true; + } + + if ( counter > 1 ) { + fail_when_counter = counter - 1; + } + + return false; +} + +void *__wrap_rtems_malloc( size_t size ) +{ + if ( IsFail() ) { + return NULL; + } + + return __real_rtems_malloc( size ); +} diff --git a/testsuites/validation/tx-support.h b/testsuites/validation/tx-support.h index 20006f5dd8..0fbb9fdfcd 100644 --- a/testsuites/validation/tx-support.h +++ b/testsuites/validation/tx-support.h @@ -101,6 +101,18 @@ void RestoreRunnerMode( void ); void RestoreRunnerPriority( void ); +/** + * @brief Fails a dynamic memory allocation when the counter reaches zero. + * + * This function initializes an internal counter which is decremented before + * each dynamic memory allocation though the rtems_malloc() directive. When + * the counter decrements from one to zero, the allocation fails and NULL will + * be returned. + * + * @param counter is the initial counter value. + */ +void MemoryAllocationFailWhen( uint32_t counter ); + typedef struct { Chain_Node node; void ( *handler )( void * ); |