Skip to content

Commit

Permalink
ut: Tests added for cm_malloc/free functions
Browse files Browse the repository at this point in the history
Multiple changes to re-use already written tests for kmalloc/kfree to
also test similar cm_malloc/cm_free functions.
  • Loading branch information
coderarjob committed Jan 2, 2025
1 parent 6d4a8e2 commit 06a3d6f
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 84 deletions.
48 changes: 36 additions & 12 deletions include/cm/cm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,42 @@
***************************************************************************************************/
#define HALT() for (;;)

#define cm_panic() \
do { \
CM_DBG_ERROR ("Panic at %s: %u", __FILE__, __LINE__); \
cm_process_abort (CM_ABORT_EXIT_CODE); \
} while (0)

#define cm_assert(t) \
do { \
if (!(t)) { \
cm_panic(); \
} \
} while (0)
#ifndef UNITTEST
#define cm_panic() \
do { \
CM_DBG_ERROR ("Panic at %s: %u", __FILE__, __LINE__); \
cm_process_abort (CM_ABORT_EXIT_CODE); \
} while (0)
#else // UNITTEST
void cm_unittest_panic_handler();
extern bool cm_panic_invoked;

/* Returns from the 'function under testing', when an assert/panic is hit.
*
* There is x86 assembly hard coded in an arch independent header, however this corresponds to
* the host (not the target) arch. Which implies that unittests can only be built & run on an
* x86 machine.
*
* TODO: Find some way to make this host independent.
* NOTE: EAX is not preserved by GCC. So there is not point adding it to the clobber list.
*/
#define cm_panic() \
do { \
cm_unittest_panic_handler(); \
__asm__ volatile("mov esp, ebp; pop ebp; mov eax, 0; ret;" ::); \
} while (0)
#endif // UNITTEST

#if defined(DEBUG)
#define cm_assert(t) \
do { \
if (!(t)) { \
cm_panic(); \
} \
} while (0)
#else
#define cm_assert(...) (void)0
#endif // DEBUG

void cm_delay (UINT ms);

Expand Down
10 changes: 7 additions & 3 deletions include/kcmlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* Types and function declarations defined here are private to the CM library and should not be
* exposed to the applications.
*/
#pragma once

#include <intrusive_list.h>
#include <memloc.h>

#pragma once

typedef struct CM_MallocHeader
{
size_t netNodeSize; /// Size of a region together with the header size.
Expand All @@ -19,7 +19,11 @@ typedef struct CM_MallocHeader
ListNode allocnode; /// A node in the Allocation list
} CM_MallocHeader;

#define CM_MALLOC_MEM_SIZE_BYTES (ARCH_MEM_LEN_BYTES_PROCESS_DATA / 2)
#if defined(UNITTEST)
#define CM_MALLOC_MEM_SIZE_BYTES MOCK_THIS_MACRO_USING (cm_arch_mem_len_bytes_malloc)
#else
#define CM_MALLOC_MEM_SIZE_BYTES (ARCH_MEM_LEN_BYTES_PROCESS_DATA / 2)
#endif

extern uint32_t cm_error_num;

Expand Down
10 changes: 10 additions & 0 deletions include/mock/cm/cm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <unittest/fake.h>
#include <cm/cm.h>

DECLARE_FUNC (void*, cm_memset, void*, U8, size_t);
DECLARE_FUNC (char*, cm_strncpy, char*, const char*, size_t);
DECLARE_FUNC (S32, syscall, U32, U32, U32, U32, U32, U32);

void resetCMFake();
2 changes: 2 additions & 0 deletions include/mosunittest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ typedef struct MockedMacro {
uintptr_t arch_mem_start_salloc;
size_t arch_mem_len_bytes_salloc;
size_t arch_mem_len_bytes_kmalloc;
// LibCM
size_t cm_arch_mem_len_bytes_malloc;
} MockedMacro;

// Need to define it when building unittests
Expand Down
13 changes: 13 additions & 0 deletions src/mock/cm/cm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <mock/cm/cm.h>
#include <unittest/fake.h>

DEFINE_FUNC (void*, cm_memset, void*, U8, size_t);
DEFINE_FUNC (char*, cm_strncpy, char*, const char*, size_t);
DEFINE_FUNC (S32, syscall, U32, U32, U32, U32, U32, U32);

void resetCMFake()
{
RESET_FAKE(cm_memset);
RESET_FAKE(cm_strncpy);
RESET_FAKE(syscall);
}
4 changes: 4 additions & 0 deletions src/mock/mockfortests_x86.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ set(kstdlib_mock_sources
set(handles_mock_sources
${PROJECT_SOURCE_DIR}/src/mock/kernel/salloc.c
)

set(cm_malloc_mock_sources
${PROJECT_SOURCE_DIR}/src/mock/cm/cm.c
)
12 changes: 12 additions & 0 deletions src/unittests/cm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ test(
${PROJECT_SOURCE_DIR}/src/unittests/unittest.c
${CMAKE_CURRENT_SOURCE_DIR}/string_test.c
)

test(
NAME cm_malloc_test
DEPENDENT_FOR build-all
DEFINITIONS LIBCM
SOURCES
${PROJECT_SOURCE_DIR}/src/cm/malloc.c
${CMAKE_CURRENT_SOURCE_DIR}/malloc_test.c
${PROJECT_SOURCE_DIR}/src/unittests/unittest.c
${CMAKE_CURRENT_SOURCE_DIR}/common.c
${cm_malloc_mock_sources}
)
25 changes: 25 additions & 0 deletions src/unittests/cm/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* ----------------------------------------------------------------------------
* Common functions to for use in MeghaOS LibCM unitests
* ----------------------------------------------------------------------------
*/

#include <unittest/unittest.h>
#include <stdio.h>
#include <types.h>
#include <stdarg.h>
#include <kernel.h>
#include <mosunittest.h>

/* MOS Unittest Macro mock global structure */
MockedMacro g_utmm = { 0 };

/* Error number */
uint32_t cm_error_num;

/* Panic handler */
bool cm_panic_invoked;
void cm_unittest_panic_handler()
{
cm_panic_invoked = true;
}
1 change: 1 addition & 0 deletions src/unittests/cm/malloc_test.c
Loading

0 comments on commit 06a3d6f

Please sign in to comment.