1

I am using cmocka on an embedded arm microcontroller (stm32) which is running FreeRTOS.

Well, cmocka seems to have some problems when it is running as a FreeRTOS Task. In line 2953 some signals are initialized and this causes an stack overflow in the FreeRTOS task. I increased to tasks stack size up to 64kB which is the maximum, but this does not help.

// Line 2953 of cmocka.c
for (i = 0; i < ARRAY_SIZE(exception_signals); i++) {
    default_signal_functions[i] = signal(
        exception_signals[i], exception_handler);
}

If I run cmocka withuot starting the scheduler of FreeRTOS it works pretty fine.

But I need to run cmocka with FreeRTOS. So I tried to run cmocka without the usage of signal.h by by passing -DHAVE_SIGNAL_H to the compiler, which should then disable the define in line 30. But this does not disable the usage of signals. It just disables the include.

// Line 30 in cmocka.c
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

How to use cmocka with FreeRTOS?

eDeviser
  • 1,605
  • 2
  • 17
  • 44
  • I think that you should ask this question on the cmocka forums. – 0___________ Jul 01 '17 at 09:51
  • Cmocka Forums? I found an irc channel, a mailing list and a bug tracker at cmocka's homepage. But no forum! – eDeviser Jul 01 '17 at 12:35
  • Do not define HAVE_SIGNAL_H. There is an ifdef statement. Moreover try to disable the signal exception handling and define *UNIT_TESTING_DEBUG*. Maybe you need also an dummy implementation for the *signal* function. – veeman Jul 01 '17 at 20:10
  • This sounds like an good Idea. I'm going to try that... – eDeviser Jul 05 '17 at 11:49

1 Answers1

1

I use cmocka with FreeRTOS on an ARM Cortex-M3. I'm currently on version 1.1.1 of cmocka, newer versions may have different configuration needs. My stack is usually 16KiB, so you shouldn't be running into any trouble with a 64KiB stack unless you have written some tests that require a huge stack.

The configuration really depends more on what C library you are using and less on your operating system. I have two projects using FreeRTOS with cmocka but one uses a cut-down version of MUSL for its C library and another uses a proprietary C library.

Here's how I configured my project using MUSL...

MUSL has signal.h and I have implemented some time functionality so my config.h ended up looking like this:

#ifndef CMOCKA_CONFIG_H_
#define CMOCKA_CONFIG_H_

#define HAVE_SIGNAL_H
#define HAVE_CLOCK_GETTIME_REALTIME
#define HAVE_STRUCT_TIMESPEC
#define CMOCKA_PLATFORM_INCLUDE

#endif /* CMOCKA_CONFIG_H_ */

I created the following C file which I named cmocka_platform.c to satisfy some cmocka C library dependencies that weren't implemented in my cut-down version of MUSL, notice I just have the signal function just return NULL:

#include <stdlib.h>
#include <stdio.h>

FILE *const stdin = NULL;
FILE *const stdout = NULL;
FILE *const stderr = NULL;

void (*signal(int sig, void (*func)(int)))(int) {
    return NULL;
}

In my cmocka_platform.h I have the following:

#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include <stddef.h>

#define CMOCKA_ENABLED

#define exit(status) while(1)

#define abort() while(1)

#define getenv(env) NULL

#define fputs(str, stream) my_printf(MYLOG_LEVEL_D, str)

#define fflush(stream)

#define fopen(stream, mode) NULL

#define fclose(stream)

#define fprintf(stream, format, ...) my_printf(MYLOG_LEVEL_D, format, ##__VA_ARGS__)

#define printf(format, ...) my_printf(MYLOG_LEVEL_D, format, ##__VA_ARGS__)

I found an open source implementation of setjmp and longjmp from newlib project that ican easily be modified to be self contained. Here is a link: https://sourceware.org/git/?p=newlib-cygwin.git;a=blob_plain;f=newlib/libc/machine/arm/setjmp.S;h=21d6ff9e7ff3762536eddcd317ae4da6e6ba64a2;hb=refs/heads/master

satur9nine
  • 13,927
  • 5
  • 80
  • 123