Skip to content

Commit

Permalink
core_self_tests.c: build with -fno-builtin
Browse files Browse the repository at this point in the history
The memory tests in core_self_tests.c call the malloc()/calloc() API
without doing anything meaningful with the output. It turns out that a
clever compiler (read: Clang) will detect this and aggressively
optimize the code, to the point that a call to calloc() is removed
entirely. Here is a reduced test case for the record:

 $ cat test.c
 #include <stdlib.h>

 int main(int argc, char *argv[])
 {
 	return calloc(1000000, 1) ? 1 : 0;
 }
 $ clang --target=arm-linux-gnueabihf -Os -c test.c
 $ llvm-objdump -d test.o

 test.o:	file format ELF32-arm-little

 Disassembly of section .text:
 0000000000000000 main:
        0:	01 00 a0 e3 	mov	r0, #1
        4:	1e ff 2f e1 	bx	lr

No call to calloc() in the generated code! As strange as it may seem,
this is reportedly a valid behavior for the compiler [1].

This optimization is obviously not wanted for the test that tries to
check that allocation of a very large buffer fails in OP-TEE.

This commit adds the -fno-builtins flag to the compiler command for that
particular source file, thus preventing the optimization and making the
test pass.

Link: [1] https://bugs.llvm.org/show_bug.cgi?id=37304
Signed-off-by: Jerome Forissier <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
  • Loading branch information
jforissier committed Jul 10, 2019
1 parent e739500 commit 62dd517
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions core/arch/arm/pta/sub.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ifeq (y,$(CFG_TEE_CORE_EMBED_INTERNAL_TESTS))
srcs-y += pta_invoke_tests.c
srcs-y += core_self_tests.c
cflags-core_self_tests.c-y += -fno-builtin
srcs-y += interrupt_tests.c
srcs-y += core_mutex_tests.c
srcs-$(CFG_WITH_USER_TA) += core_fs_htree_tests.c
Expand Down

0 comments on commit 62dd517

Please sign in to comment.