Segmentation fault on minimal hello-world example

569 views
Skip to first unread message

Erling Jellum

unread,
Apr 20, 2020, 11:25:50 AM4/20/20
to cpputest
Skriv inn kode her
Hi, I am new to CppUTest and I am trying to get a really simple minimal example running. But I am getting a segmentation fault when running the test runner. I have used NewCModule.sh to create a new module and I am trying to just run the test-runner to see it fail.

I have:
src/
---->LightScheduler.c
---->LightSchdueler.h
test/
---->LightSchedulerTest.cpp
---->AllTests.cpp
---->Makefile
---->MakefileWorker.mk #Copied from CppUTest
Makefile

The Makefiles are configured based on this tutorial: https://www.sparkpost.com/blog/getting-started-cpputest/

The relevant Makefile, within the test directory:

# we don’t want to use relative paths, so we set these variables
PROJECT_DIR
=/home/erling/tdd/lightscheduler
SRC_DIR
=$(PROJECT_DIR)/src
TEST_DIR
=$(PROJECT_DIR)/test
# specify where the source code and includes are located
INCLUDE_DIRS
=$(SRC_DIR)
SRC_DIRS
=$(SRC_DIR)
 
# specify where the test code is located
TEST_SRC_DIRS
= $(TEST_DIR)
 
# what to call the test binary
TEST_TARGET
=test_runner.x
 
# where the cpputest library is located
CPPUTEST_HOME
=/home/erling/tdd/cpputest
 
# run MakefileWorker.mk with the variables defined here
include
MakefileWorker.mk


LightSchedulerTest.cpp
extern "C"
{
#include "LightScheduler.h"
}

#include "CppUTest/TestHarness.h"

TEST_GROUP
(LightScheduler)
{
   
void setup()
   
{
     
LightScheduler_Create();
   
}

   
void teardown()
   
{
       
LightScheduler_Destroy();
   
}
};

TEST
(LightScheduler, Create)
{
  FAIL
("Start here");
}



When running make, it compiles and linkes without errors, but fails when running the executable


> $ make
compiling
LightScheduler.c
mkdir
-p objs//home/erling/tdd/lightscheduler/src/
cc
-g -Wstrict-prototypes  -Wall -Wextra -Werror -Wshadow -Wswitch-default -Wswitch-enum -Wconversion -Wno-long-long -pedantic-errors -Wsign-conversion -include /home/erling/tdd/cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h  -I/home/erling/tdd/lightscheduler/src    -c -MMD -MP -o objs//home/erling/tdd/lightscheduler/src/LightScheduler.o /home/erling/tdd/lightscheduler/src/LightScheduler.c
Building archive lib/libname_this_in_the_makefile.a
mkdir
-p lib/
ar rvc lib
/libname_this_in_the_makefile.a objs//home/erling/tdd/lightscheduler/src/LightScheduler.o objs//home/erling/tdd/lightscheduler/src/main.o
r
- objs//home/erling/tdd/lightscheduler/src/LightScheduler.o
r
- objs//home/erling/tdd/lightscheduler/src/main.o
ranlib lib
/libname_this_in_the_makefile.a
Linking test_runner.x
g
++ -o test_runner.x objs//home/erling/tdd/lightscheduler/test/AllTests.o objs//home/erling/tdd/lightscheduler/test/LightSchedulerTest.o lib/libname_this_in_the_makefile.a /home/erling/tdd/cpputest/lib/libCppUTest.a  -g
START_TIME
=1587396244
rm
-f objs//home/erling/tdd/lightscheduler/src/LightScheduler.gcda objs//home/erling/tdd/lightscheduler/src/main.gcda objs//home/erling/tdd/lightscheduler/test/AllTests.gcda objs//home/erling/tdd/lightscheduler/test/LightSchedulerTest.gcda gcov_output.txt gcov_report.txt gcov_error.txt ; echo "Running test_runner.x"; ./test_runner.x
Running test_runner.x
Segmentation fault (core dumped)
Makefile:25: recipe for target 'run' failed
make
[1]: *** [run] Error 139
make
[1]: Leaving directory '/home/erling/tdd/lightscheduler/test'
Makefile:16: recipe for target 'test' failed
make
: *** [test] Error 2

I ran test-runner.x in GDB and got:

Program received signal SIGSEGV, Segmentation fault.
0x0000555555567705 in UtestShell::destroyTest (
   
this=0x55555577c1c0 <TEST_LightScheduler_Create_TestShell_instance>, test=0x1) at src/CppUTest/Utest.cpp:195
195         delete test;

Thanks for any help in getting this simple example up and running.

- Erling

Ryan Hartlage

unread,
Apr 20, 2020, 3:57:40 PM4/20/20
to cpputest
Hi Erling,

Have you tried taking LightScheduler_Create() and LightScheduler_Destroy() out of setup() and teardown()?

Ryan

--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cpputest/892eebed-17ce-48dc-ad3a-86efd77d94c4%40googlegroups.com.

Erling Jellum

unread,
Apr 21, 2020, 2:38:51 AM4/21/20
to cppu...@googlegroups.com

Hi Ryan

Thanks for your reply. I tried commenting out those function calls. Now we only have empty functions but still there is a segmentation fault.

--
Erling Rennemo Jellum

P: +47 465 15 653



Martin Dída

unread,
Apr 21, 2020, 2:59:07 AM4/21/20
to cppu...@googlegroups.com
When you make such a changes like removing some functionality. Try also to rebuild all solution. It means first make clean and then make build. Lately, it's happened to me I was trying something with FUNCTIONPOINTERS_EQUAL macro and also experienced undefined behavior of two pointers being equal even though they were twe different functions. Finally, rebuild overall project helped to get expected behavior back.


Best regards                                        Martin Dida


Erling Jellum

unread,
Apr 21, 2020, 12:31:00 PM4/21/20
to cppu...@googlegroups.com
I have tried "make clean" without any luck. I have uploaded the project to Github if anyone care have a look at it. You need to update the CPPUTEST_HOME variable in the Makefile of the test directory. Running make test in the root directory should start it.


--
Erling Rennemo Jellum

P: +47 465 15 653


James Grenning

unread,
Apr 21, 2020, 3:10:51 PM4/21/20
to cppu...@googlegroups.com

Hi Erling

Your main is a c file. Try changing it to main.cpp and add this content.

#include "CppUTest/CommandLineTestRunner.h"

int main(int ac, char** av)
{
    return RUN_ALL_TESTS(ac, av);
}

HTH


James Grenning - Author of TDD for Embedded C - wingman-sw.com/tddec
wingman-sw.com
wingman-sw.com/blog
twitter.com/jwgrenning
facebook.com/wingman.sw
wingman software

Reply all
Reply to author
Forward
0 new messages