Skip to content

Commit

Permalink
Merge pull request #1281 from microsoft/enhancement-test-sizes
Browse files Browse the repository at this point in the history
[test] Test Sizes of C API
  • Loading branch information
ppenna committed May 21, 2024
2 parents c7d634e + 25fdb98 commit c1c9c02
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 5 deletions.
7 changes: 6 additions & 1 deletion linux.mk
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ test-system-rust:
test-unit: test-unit-rust

# C unit tests.
test-unit-c: all-tests $(BINDIR)/syscalls.elf
test-unit-c: all-tests test-unit-c-sizes test-unit-c-syscalls

test-unit-c-sizes: all-tests $(BINDIR)/sizes.elf
timeout $(TIMEOUT) $(BINDIR)/sizes.elf

test-unit-c-syscalls: all-tests $(BINDIR)/syscalls.elf
timeout $(TIMEOUT) $(BINDIR)/syscalls.elf

# Rust unit tests.
Expand Down
2 changes: 1 addition & 1 deletion src/rust/runtime/types/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod test {
// Size of an array of demi_sgaseg_t structures.
const SGA_SEGS_SIZE: usize = mem::size_of::<demi_sgaseg_t>() * DEMI_SGARRAY_MAXLEN;
// Size of a SockAddr structure.
const SGA_ADDR_SIZE: usize = mem::size_of::<SockAddr>();
const SGA_ADDR_SIZE: usize = 16;
// Size of a demi_sgarray_t structure.
crate::ensure_eq!(
mem::size_of::<demi_sgarray_t>(),
Expand Down
7 changes: 6 additions & 1 deletion tests/c/linux.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@ export COMPILE_CMD = $(CC) $(CFLAGS) $@.o -o $(BINDIR)/$@.$(EXEC_SUFFIX) $(LIBS)
#=======================================================================================================================

# Builds everything.
all: syscalls
all: sizes syscalls

make-dirs:
mkdir -p $(BINDIR)

# Builds 'sizes' test.
sizes: make-dirs sizes.o
$(COMPILE_CMD)

# Builds system call test.
syscalls: make-dirs syscalls.o
$(COMPILE_CMD)

# Cleans up all build artifacts.
clean:
@rm -rf $(OBJ)
@rm -rf $(BINDIR)/sizes.$(EXEC_SUFFIX)
@rm -rf $(BINDIR)/syscalls.$(EXEC_SUFFIX)

# Builds a C source file.
Expand Down
140 changes: 140 additions & 0 deletions tests/c/sizes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

/*====================================================================================================================*
* Imports *
*====================================================================================================================*/

#include <demi/types.h>
#include <stdio.h>
#include <stdlib.h>

/*====================================================================================================================*
* Macro Functions *
*====================================================================================================================*/

/**
* @brief Returns the maximum of two values.
*
* @param a First value.
* @param b Second value.
*
* @returns The maximum of 'a' and 'b'.
*/
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

/**
* @brief Asserts if 'a' and 'b' agree on size.
*
* @param a Probing size.
* @param b Control size.
*
* @returns Upon success, compilation proceeds as normal. Upon failure, a
* compilation error is generated.
*/
#ifdef _WIN32
#define KASSERT_SIZE(a, b) static_assert((a) == (b), "Size mismatch.")
#else
#define KASSERT_SIZE(a, b) ((void)sizeof(char[(((a) == (b)) ? 1 : -1)]))
#endif

/*====================================================================================================================*
* Constants *
*====================================================================================================================*/

// The following sizes are intentionally hardcoded.
#define SGASEG_BUF_SIZE 8
#define SGASEG_LEN_SIZE 4
#define DEMI_SGASEG_T_SIZE (SGASEG_BUF_SIZE + SGASEG_LEN_SIZE)
#define SGA_BUF_SIZE 8
#define SGA_NUMSEGS_SIZE 4
#define SGA_SEGS_SIZE (DEMI_SGASEG_T_SIZE * DEMI_SGARRAY_MAXSIZE)
#define SGA_ADDR_SIZE 16
#define DEMI_SGARRAY_T_SIZE (SGA_BUF_SIZE + SGA_NUMSEGS_SIZE + SGA_SEGS_SIZE + SGA_ADDR_SIZE)
#define QD_SIZE 4
#define SADDR_SIZE 16
#define DEMI_ACCEPT_RESULT_T_SIZE (QD_SIZE + SADDR_SIZE)
#define QR_OPCODE_SIZE 4
#define QR_QD_SIZE 4
#define QR_QT_SIZE 8
#define QR_RET_SIZE 8
#define QR_VALUE_SIZE (MAX(DEMI_ACCEPT_RESULT_T_SIZE, DEMI_SGARRAY_T_SIZE))
#define DEMI_QRESULT_T_SIZE (QR_OPCODE_SIZE + QR_QD_SIZE + QR_QT_SIZE + QR_RET_SIZE + QR_VALUE_SIZE)

/*====================================================================================================================*
* Private Functions *
*====================================================================================================================*/

/**
* @brief tests if @p demi_sgaseg_t has the expected size.
*
* @note This is a compile-time-test.
*/
static void test_size_sgaseg_t(void)
{
KASSERT_SIZE(sizeof(demi_sgaseg_t), DEMI_SGASEG_T_SIZE);
printf("sizeof(demi_sgaseg_t) = %zu\n", sizeof(demi_sgaseg_t));
}

/**
* @brief Tests if @p demi_sga_t has the expected size.
*
* @note This is a compile-time-test.
*/
static void test_size_sga_t(void)
{
KASSERT_SIZE(sizeof(demi_sgarray_t), DEMI_SGARRAY_T_SIZE);
printf("sizeof(demi_sgarray_t) = %zu\n", sizeof(demi_sgarray_t));
}

/**
* @brief Tests if @p demi_accept_result_t has the expected size.
*
* @note This is a compile-time-test.
*/
static void test_size_demi_accept_result_t(void)
{
KASSERT_SIZE(sizeof(demi_accept_result_t), DEMI_ACCEPT_RESULT_T_SIZE);
printf("sizeof(demi_accept_result_t) = %zu\n", sizeof(demi_accept_result_t));
}

/**
* @brief Tests if demi_qresult_t has the expected size.
*
* @note This is a compile-time-test.
*/
static void test_size_demi_qresult_t(void)
{
KASSERT_SIZE(sizeof(demi_qresult_t), DEMI_QRESULT_T_SIZE);
printf("sizeof(demi_qresult_t) = %zu\n", sizeof(demi_qresult_t));
}

/*====================================================================================================================*
* Public Functions *
*====================================================================================================================*/

/**
* @brief Drives the application.
*
* This system-level conducts tests to ensure that the sizes of structures and
* data types exposed by the C bindings of Demikernel are correct.
*
* @param argc Argument count (unused).
* @param argv Argument vector (unused).
*
* @return On successful completion EXIT_SUCCESS is returned.
*/
int main(int argc, char *const argv[])
{
((void)argc);
((void)argv);

test_size_sgaseg_t();
test_size_sga_t();
test_size_demi_accept_result_t();
test_size_demi_qresult_t();

return (EXIT_SUCCESS);
}
9 changes: 8 additions & 1 deletion tests/c/windows.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@

CC = cl

all: syscalls
all: sizes syscalls

sizes: make-dirs sizes.obj
$(CC) sizes.obj $(LIBS) /Fe: $(BINDIR)\sizes.exe

syscalls: make-dirs syscalls.obj
$(CC) syscalls.obj $(LIBS) /Fe: $(BINDIR)\syscalls.exe

clean:
IF EXIST sizes.obj del /Q sizes.obj
IF EXIST syscalls.obj del /Q syscalls.obj
IF EXIST $(BINDIR)\syscalls.exe del /S /Q $(BINDIR)\syscalls.exe

sizes.obj:
$(CC) /I $(INCDIR) tests\c\sizes.c /c

syscalls.obj:
$(CC) /I $(INCDIR) tests\c\syscalls.c /c

Expand Down
8 changes: 7 additions & 1 deletion windows.mk
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,13 @@ test-system-rust:
test-unit: test-unit-rust

# C unit tests.
test-unit-c: all-tests-c
test-unit-c: all-tests-c test-unit-c-sizes test-unit-c-syscalls

test-unit-c-sizes: all-tests-c
set RUST_LOG=$(RUST_LOG)
$(BINDIR)\sizes.exe

test-unit-c-syscalls: all-tests-c
set RUST_LOG=$(RUST_LOG)
$(BINDIR)\syscalls.exe

Expand Down

0 comments on commit c1c9c02

Please sign in to comment.