Skip to content

Commit

Permalink
Replace criterion with test runner on x86
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrtonm committed Oct 17, 2024
1 parent 7afbed0 commit 1f8a394
Show file tree
Hide file tree
Showing 62 changed files with 117 additions and 116 deletions.
2 changes: 1 addition & 1 deletion cmake/define-test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function(define_test)

set(ADD_SANDBOX_DEP FALSE)
if (DEFINE_TEST_CRITERION_TEST)
list(APPEND DEFINE_TEST_UNWRAPPED_LIBS criterion)
list(APPEND DEFINE_TEST_UNWRAPPED_LIBS ia2_test_runner)
if (NOT DEFINE_TEST_NOT_IN_CHECK_IA2)
if (LIBIA2_AARCH64)
# unless natively AArch64, default to running tests with qemu-aarch64 and a custom LD_LIBRARY_PATH
Expand Down
18 changes: 7 additions & 11 deletions cmake/ia2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ function(add_ia2_compartment NAME TYPE)
add_library(${NAME} SHARED)
endif()

# The x86 version is missing a dependency here and libs rely on include path overlap to pick up
# criterion header. I want to keep spoofed criterion static for simplicity so we add the -I
# directly here.
if (LIBIA2_AARCH64)
target_include_directories(${NAME} PRIVATE
${CMAKE_SOURCE_DIR}/misc/spoofed_criterion/include)
endif()
target_include_directories(${NAME} PRIVATE
${CMAKE_SOURCE_DIR}/misc/test_runner/include)
target_compile_definitions(${NAME} PRIVATE
IA2_ENABLE=1
PKEY=${ARG_PKEY}
Expand Down Expand Up @@ -153,10 +148,11 @@ function(create_compile_commands NAME TYPE)
# Copy target properties from the real target. We might need to add more properties.
target_link_libraries(${COMPILE_COMMAND_TARGET} PRIVATE $<TARGET_PROPERTY:${NAME},LINK_LIBRARIES>)
target_include_directories(${COMPILE_COMMAND_TARGET} PRIVATE ${INCLUDE_DIRECTORIES})
if (LIBIA2_AARCH64)
target_include_directories(${COMPILE_COMMAND_TARGET} PRIVATE
${CMAKE_SOURCE_DIR}/misc/spoofed_criterion/include)
endif()
# The test runner is a static library that just defines main so we don't link it into
# the libraries defined by tests and instead just add the include flags for its
# assertions
target_include_directories(${COMPILE_COMMAND_TARGET} PRIVATE
${CMAKE_SOURCE_DIR}/misc/test_runner/include)
set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
endfunction()

Expand Down
4 changes: 1 addition & 3 deletions misc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
if (LIBIA2_AARCH64)
add_subdirectory(spoofed_criterion)
endif()
add_subdirectory(test_runner)
3 changes: 0 additions & 3 deletions misc/spoofed_criterion/CMakeLists.txt

This file was deleted.

5 changes: 0 additions & 5 deletions misc/spoofed_criterion/include/criterion/logging.h

This file was deleted.

10 changes: 0 additions & 10 deletions misc/spoofed_criterion/include/criterion/new/assert.h

This file was deleted.

3 changes: 3 additions & 0 deletions misc/test_runner/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(ia2_test_runner STATIC test_runner.c)
target_include_directories(ia2_test_runner
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <criterion/logging.h>
#include <criterion/new/assert.h>
#pragma once
#include <stdio.h>

struct fake_criterion_test {
void (*test)(void);
Expand All @@ -13,3 +13,15 @@ struct fake_criterion_test {
.test = fake_criterion_##suite##_##name, \
##__VA_ARGS__}; \
void fake_criterion_##suite##_##name(void)

#define cr_log_info(f, ...) printf(f "\n", ##__VA_ARGS__)
#define cr_log_error(f, ...) fprintf(stderr, f "\n", ##__VA_ARGS__)

#define cr_assert assert
#define cr_assert_eq(a, b) cr_assert((a) == (b))
#define cr_assert_lt(a, b) cr_assert((a) < (b))
#define cr_fatal(s) \
do { \
fprintf(stderr, s "\n"); \
exit(1); \
} while (0)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "include/criterion/criterion.h"
#include "include/ia2_test_runner.h"
#include <stdbool.h>
#include <stdio.h>
#include <sys/wait.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/abi/abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ RUN: cat minimal_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/

#include "abi.h"
#include <criterion/criterion.h>
#include <ia2_test_runner.h>

// LINKARGS: --wrap=arg1
void arg1(int x) {
Expand Down
2 changes: 1 addition & 1 deletion tests/abi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RUN: sh -c 'if [ ! -s "minimal_call_gates_0.ld" ]; then echo "No link args as ex

// Check that readelf shows exactly one executable segment

#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include "abi.h"
#include <ia2.h>

Expand Down
4 changes: 2 additions & 2 deletions tests/destructors/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/destructors/plugin.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <stdio.h>
#include <ia2.h>
#include "exported_fn.h"
Expand Down
2 changes: 1 addition & 1 deletion tests/global_fn_ptr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ RUN: sh -c 'if [ ! -s "global_fn_ptr_call_gates_0.ld" ]; then echo "No link args
*/
#include "operations.h"
#include <ia2.h>
#include <criterion/criterion.h>
#include <ia2_test_runner.h>

uint32_t add(uint32_t x, uint32_t y) { return x + y; }
uint16_t sub(uint16_t x, uint16_t y) { return x - y; }
Expand Down
2 changes: 1 addition & 1 deletion tests/global_fn_ptr/operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
RUN: cat global_fn_ptr_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include "operations.h"
#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include <stdio.h>

extern Op operations[2];
Expand Down
3 changes: 2 additions & 1 deletion tests/header_includes/liboption.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
RUN: cat header_includes_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include <criterion/logging.h>

#include <ia2_test_runner.h>
#include "liboption.h"
#include "types.h"

Expand Down
4 changes: 2 additions & 2 deletions tests/header_includes/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ RUN: sh -c 'if [ ! -s "header_includes_call_gates_0.ld" ]; then echo "No link ar
*/
#include "liboption.h"
#include "types.h"
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <ia2.h>

INIT_RUNTIME(1);
Expand Down
6 changes: 3 additions & 3 deletions tests/heap_two_keys/main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
RUN: sh -c 'if [ ! -s "heap_two_keys_call_gates_0.ld" ]; then echo "No link args as expected"; exit 0; fi; echo "Unexpected link args"; exit 1;'
*/
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <criterion/new/assert.h>
#include <ia2_test_runner.h>


#include <unistd.h>
#include <assert.h>
#include <ia2.h>
Expand Down
3 changes: 2 additions & 1 deletion tests/macro_attr/functions.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
RUN: cat macro_attr_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include <criterion/logging.h>

#include <ia2_test_runner.h>
#include "functions.h"

// LINKARGS: --wrap=f
Expand Down
2 changes: 1 addition & 1 deletion tests/macro_attr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ RUN: sh -c 'if [ ! -s "macro_attr_call_gates_0.ld" ]; then echo "No link args as
*/
#include "functions.h"
#include <ia2.h>
#include <criterion/criterion.h>
#include <ia2_test_runner.h>

INIT_RUNTIME(1);
#define IA2_COMPARTMENT 1
Expand Down
2 changes: 1 addition & 1 deletion tests/minimal/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RUN: sh -c 'if [ ! -s "minimal_call_gates_0.ld" ]; then echo "No link args as ex

// Check that readelf shows exactly one executable segment

#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include "minimal.h"
#include <ia2.h>

Expand Down
3 changes: 2 additions & 1 deletion tests/minimal/minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
RUN: cat minimal_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/

#include <ia2_test_runner.h>
#include "minimal.h"
#include <criterion/logging.h>


// LINKARGS: --wrap=arg1
void arg1(int x) {
Expand Down
2 changes: 1 addition & 1 deletion tests/mmap_loop/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN: sh -c 'if [ ! -s "mmap_loop_call_gates_0.ld" ]; then echo "No link args as
#include <ia2.h>
#include <math.h>
#include <stdio.h>
#include <criterion/criterion.h>
#include <ia2_test_runner.h>

#define IA2_DEFINE_TEST_HANDLER
#include "test_fault_handler.h"
Expand Down
2 changes: 1 addition & 1 deletion tests/permissive_mode/permissive_mode.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include <ia2.h>
#include <permissive_mode.h>

Expand Down
2 changes: 1 addition & 1 deletion tests/protected_threads/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN: sh -c 'if [ ! -s "protected_threads_call_gates_0.ld" ]; then echo "No link
#include <string.h>
#include <sys/wait.h>
#define IA2_DEFINE_TEST_HANDLER
#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include <test_fault_handler.h>
#include <unistd.h>

Expand Down
3 changes: 2 additions & 1 deletion tests/read_config/builtin.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
RUN: cat read_config_call_gates_2.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include <criterion/logging.h>

#include <ia2_test_runner.h>
#include <stdlib.h>
#include <string.h>
#include <ia2.h>
Expand Down
4 changes: 2 additions & 2 deletions tests/read_config/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ RUN: cat main.c | FileCheck --match-full-lines --check-prefix=REWRITER %s

// Check that readelf shows exactly one executable segment
#include "plugin.h"
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <ia2.h>
#include <ia2_allocator.h>
#include <string.h>
Expand Down
3 changes: 2 additions & 1 deletion tests/read_config/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ We need this because main.c uses LINKARGS checks but not this file.
#include "plugin.h"
#include "core.h"
#include <ia2.h>
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <stdlib.h>
#include <string.h>

Expand Down
2 changes: 1 addition & 1 deletion tests/recursion/dso.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
RUN: cat recursion_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include "recursion_main.h"
#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include <ia2.h>
#include <stdio.h>

Expand Down
4 changes: 2 additions & 2 deletions tests/recursion/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ RUN: cat recursion_call_gates_2.ld | FileCheck --check-prefix=LINKARGS %s
*/

#include "recursion_dso.h"
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <ia2.h>
#include <stdio.h>
#define IA2_DEFINE_TEST_HANDLER
Expand Down
4 changes: 2 additions & 2 deletions tests/rewrite_fn_ptr_eq/lib.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
RUN: cat rewrite_fn_ptr_eq_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <lib.h>
#include <stddef.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/rewrite_fn_ptr_eq/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
RUN: cat main.c | FileCheck --match-full-lines --check-prefix=REWRITER %S/main.c
*/
#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include <stdio.h>
#include <lib.h>
#include <ia2.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/rewrite_macros/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RUN: cat main.c | FileCheck --match-full-lines --check-prefix=REWRITER %s
#include "lib.h"
#include <stddef.h>
#include <ia2.h>
#include <criterion/criterion.h>
#include <ia2_test_runner.h>

INIT_RUNTIME(1);
#define IA2_COMPARTMENT 1
Expand Down
4 changes: 2 additions & 2 deletions tests/ro_sharing/main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
RUN: sh -c 'if [ ! -s "ro_sharing_call_gates_0.ld" ]; then echo "No link args as expected"; exit 0; fi; echo "Unexpected link args"; exit 1;'
*/
#include <criterion/criterion.h>
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <plugin.h>
#include <ia2.h>
#include <stdio.h>
Expand Down
3 changes: 2 additions & 1 deletion tests/ro_sharing/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
RUN: cat ro_sharing_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include "test_fault_handler.h"
#include <criterion/logging.h>
#include <ia2_test_runner.h>

#include <ia2.h>
#include <stdint.h>
#include <stdio.h>
Expand Down
3 changes: 2 additions & 1 deletion tests/shared_data/access_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ RUN: cat shared_data_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include <stdio.h>
#include "access_shared.h"
#include <criterion/logging.h>
#include <ia2_test_runner.h>


// LINKARGS: --wrap=read_shared
uint8_t read_shared(uint8_t *shared) {
Expand Down
2 changes: 1 addition & 1 deletion tests/shared_data/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RUN: sh -c 'if [ ! -s "shared_data_call_gates_0.ld" ]; then echo "No link args a
#include <assert.h>
#include <ia2.h>
#include "access_shared.h"
#include <criterion/criterion.h>
#include <ia2_test_runner.h>

INIT_RUNTIME(1);
#define IA2_COMPARTMENT 1
Expand Down
2 changes: 1 addition & 1 deletion tests/should_segfault/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
RUN: sh -c 'if [ ! -s "should_segfault_call_gates_0.ld" ]; then echo "No link args as expected"; exit 0; fi; echo "Unexpected link args"; exit 1;'
*/
#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/should_segfault/print_secret.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
RUN: cat should_segfault_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
*/
#include <criterion/criterion.h>
#include <ia2_test_runner.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Expand Down
Loading

0 comments on commit 1f8a394

Please sign in to comment.