Skip to content

Commit

Permalink
missing bfdriver unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rian Quinn <“rianquinn@gmail.com”>
  • Loading branch information
Rian Quinn committed Sep 5, 2017
1 parent 43e74b4 commit 4d02990
Show file tree
Hide file tree
Showing 29 changed files with 1,036 additions and 1,609 deletions.
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ endif()
# ------------------------------------------------------------------------------
# SDK
# ------------------------------------------------------------------------------

list(APPEND BFSDK_CMAKE_ARGS
-DBAREFLANK_SOURCE_DIR=${CMAKE_SOURCE_DIR}
-DBAREFLANK_BINARY_DIR=${CMAKE_BINARY_DIR}
Expand Down Expand Up @@ -506,8 +507,8 @@ list(APPEND BFDRIVER_CMAKE_ARGS
-DENABLE_TIDY=${ENABLE_TIDY}
-DENABLE_ASTYLE=${ENABLE_ASTYLE}
-DENABLE_UNITTESTING=${ENABLE_UNITTESTING}
-DBUILD_SHARED_LIBS=${BAREFLANK_TEST_SHARED_LIBS}
-DBUILD_STATIC_LIBS=${BAREFLANK_TEST_STATIC_LIBS}
-DBUILD_SHARED_LIBS=${BAREFLANK_HOST_SHARED_LIBS}
-DBUILD_STATIC_LIBS=${BAREFLANK_HOST_STATIC_LIBS}
)

ExternalProject_Add(
Expand All @@ -520,11 +521,14 @@ ExternalProject_Add(
SOURCE_DIR ${CMAKE_SOURCE_DIR}/bfdriver
BINARY_DIR ${CMAKE_BINARY_DIR}/bfdriver/build
UPDATE_COMMAND ${CMAKE_COMMAND} -E echo "checking for updates"
BUILD_COMMAND ""
INSTALL_COMMAND ""
DEPENDS bfsdk bfelf_loader
)

if(ENABLE_UNITTESTING AND NOT WIN32)
add_dependencies(bfdriver bfsysroot)
endif()

# ------------------------------------------------------------------------------
# Extended APIs
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -673,7 +677,7 @@ if(ENABLE_UNITTESTING)
endif()

add_custom_target(test
# COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR}/bfdriver/build ctest
COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR}/bfdriver/build ctest
COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR}/bfelf_loader/build ctest
COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR}/bfm/build ctest
COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_BINARY_DIR}/bfsdk/build ctest
Expand All @@ -694,13 +698,13 @@ endif()
if(ENABLE_TIDY AND NOT WIN32)

add_custom_target(tidy
# COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfdriver/build --target tidy
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfdriver/build --target tidy
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfm/build --target tidy
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfsdk/build --target tidy
)

add_custom_target(tidy-all
# COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfdriver/build --target tidy-all
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfdriver/build --target tidy-all
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfm/build --target tidy-all
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfsdk/build --target tidy-all
)
Expand Down
3 changes: 2 additions & 1 deletion bfdriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ include(${CMAKE_INSTALL_PREFIX}/cmake/CMakeGlobal_Project.txt)
# Subdirectories
# ------------------------------------------------------------------------------

if(ENABLE_UNITTESTING)
if(NOT WIN32 AND ENABLE_UNITTESTING AND BUILD_SHARED_LIBS)
add_subdirectory(src)
add_subdirectory(tests)
endif()

Expand Down
8 changes: 2 additions & 6 deletions bfdriver/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ common_vmm_status(void);
*
* This function should not be called directly. Instead, use common_unload.
* This is only exposed publically for unit testing.
*
* @return will always return BF_SUCCESS
*/
int64_t
void
common_reset(void);

/**
Expand All @@ -58,10 +56,8 @@ common_reset(void);
* This code should be run as part of the driver entry's init code. This
* sets up some resources that are needed throughout the lifetime of the
* driver entry.
*
* @return BF_SUCCESS on success, negative error code on failure
*/
int64_t
void
common_init(void);

/**
Expand Down
11 changes: 11 additions & 0 deletions bfdriver/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ------------------------------------------------------------------------------
# CMake Includes
# ------------------------------------------------------------------------------

include(${CMAKE_INSTALL_PREFIX}/cmake/CMakeGlobal_Includes.txt)

# ------------------------------------------------------------------------------
# Targets
# ------------------------------------------------------------------------------

add_library(bfdriver STATIC common.c arch/test/platform.c)
5 changes: 1 addition & 4 deletions bfdriver/src/arch/linux/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,7 @@ dev_init(void)
return -EPERM;
}

if (common_init() != 0) {
BFALERT("common_init failed\n");
return -EPERM;
}
common_init();

BFDEBUG("dev_init succeeded\n");
return 0;
Expand Down
133 changes: 43 additions & 90 deletions bfdriver/src/arch/test/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,152 +25,105 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <platform.h>
#include <sys/mman.h>
#include <constants.h>

int alloc_count_rw = 0;
int alloc_count_rwe = 0;
#include <bfplatform.h>
#include <bfconstants.h>
#include <bfvmcallinterface.h>

#define PAGE_ROUND_UP(x) ( (((uintptr_t)(x)) + MAX_PAGE_SIZE-1) & (~(MAX_PAGE_SIZE-1)) )

uint64_t g_malloc_fails = 0;
uint64_t g_set_afinity_fails = 0;
uint64_t g_vmcall = 0;

int
verify_no_mem_leaks(void)
{
printf("alloc_count_rw: %d\n", alloc_count_rw);
printf("alloc_count_rwe: %d\n", alloc_count_rwe);
#ifdef WIN64
#include <malloc.h>
#include <windows.h>
#define aligned_alloc _aligned_malloc
#endif

return (alloc_count_rw == 0) && (alloc_count_rwe == 0);
}
#define PAGE_ROUND_UP(x) ( (((uintptr_t)(x)) + MAX_PAGE_SIZE-1) & (~(MAX_PAGE_SIZE-1)) )

void *
platform_alloc_rw(uint64_t len)
{
if (g_malloc_fails == len) {
return 0;
}

alloc_count_rw++;
return malloc(len);
}

#include <errno.h>
{ return malloc(len); }

void *
platform_alloc_rwe(uint64_t len)
{
void *addr = 0;

if (g_malloc_fails == len) {
return 0;
}
#ifdef WIN64
DWORD oldProtect;
#else
int ret;
#endif

len = PAGE_ROUND_UP(len);
addr = aligned_alloc(MAX_PAGE_SIZE, len);

if (posix_memalign(&addr, MAX_PAGE_SIZE, len) != 0) {
return 0;
}

if (mprotect(addr, len, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
platform_free_rw(addr, len);
return 0;
}

alloc_count_rwe++;
#ifdef WIN64
DWORD oldProtect;
VirtualProtect(addr, len, PAGE_EXECUTE_READWRITE, &oldProtect);
#else
ret = mprotect(addr, len, PROT_READ | PROT_WRITE | PROT_EXEC);
bfignored(ret);
#endif

return addr;
}

void
platform_free_rw(void *addr, uint64_t len)
{
(void)len;

alloc_count_rw--;
bfignored(len);
free(addr);
}

void
platform_free_rwe(void *addr, uint64_t len)
{
(void) len;

alloc_count_rwe--;
bfignored(len);
free(addr);
}

void *
platform_virt_to_phys(void *virt)
{
return virt;
}
{ return virt; }

void
void *
platform_memset(void *ptr, char value, uint64_t num)
{
if (!ptr) {
return;
}

memset(ptr, value, num);
}
{ return memset(ptr, value, num); }

void
void *
platform_memcpy(void *dst, const void *src, uint64_t num)
{
if (!dst || !src) {
return;
}

memcpy(dst, src, num);
}
{ return memcpy(dst, src, num); }

void
platform_start(void)
{
}
{ }

void
platform_stop(void)
{
}
{ }

int64_t
platform_num_cpus(void)
{
return 1;
}
{ return 1; }

int64_t
platform_set_affinity(int64_t affinity)
{
(void) affinity;

if (g_set_afinity_fails != 0) {
return -1;
}

bfignored(affinity);
return 0;
}

void
platform_restore_affinity(int64_t affinity)
{
(void) affinity;
}
{ bfignored(affinity); }

int64_t
platform_get_current_cpu_num(void)
{ return 0; }

void
platform_vmcall(struct vmcall_registers_t *regs)
{
regs->r01 = g_vmcall;
}
platform_restore_preemption(void)
{ }

void
platform_vmcall_event(struct vmcall_registers_t *regs)
{
regs->r01 = g_vmcall;
}
vmcall(struct vmcall_registers_t *regs)
{ regs->r01 = 0; }

0 comments on commit 4d02990

Please sign in to comment.