From ef33f574fa9c4e1a4d64aeefc66ec17f7a296a7a Mon Sep 17 00:00:00 2001 From: Rian Quinn Date: Mon, 28 Aug 2017 15:59:48 -0600 Subject: [PATCH] BFSDK missing unit tests (#477) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rian Quinn <“rianquinn@gmail.com”> --- CMakeLists.txt | 7 +- bfsdk/CMakeLists.txt | 5 + bfsdk/cmake/CMakeOption_ASan.txt | 1 - bfsdk/include/bfbenchmark.h | 12 +- bfsdk/include/bfdebugringinterface.h | 28 ++--- bfsdk/include/bfexception.h | 4 +- bfsdk/include/bfgsl.h | 11 +- bfsdk/include/bfnewdelete.h | 25 +++-- bfsdk/scripts/bareflank_clang_tidy.sh | 54 ++++----- bfsdk/scripts/compiler_wrapper.sh | 16 +-- bfsdk/tests/CMakeLists.txt | 5 + bfsdk/tests/test_affinity.cpp | 29 +++++ bfsdk/tests/test_benchmark.cpp | 55 +++++++++ bfsdk/tests/test_buffer.cpp | 6 + bfsdk/tests/test_debugringinterface.cpp | 101 +++++++++++++++++ bfsdk/tests/test_gsl.cpp | 106 ++++++++++++++++++ bfsdk/tests/test_newdelete.cpp | 51 +++++++++ bfsysroot/CMakeLists.txt | 6 +- bfsysroot/bfsupport/CMakeLists.txt | 5 +- bfsysroot/bfsupport/crt/crt.cpp | 11 +- bfsysroot/bfsupport/dummy_main/dummy_main.cpp | 6 +- bfsysroot/bfsupport/pthread/pthread.cpp | 22 ++-- bfsysroot/bfunwind/CMakeLists.txt | 5 +- bfsysroot/bfunwind/src/dwarf4.cpp | 38 +++---- bfsysroot/bfunwind/src/eh_frame.cpp | 3 +- bfsysroot/bfunwind/src/ia64_cxx_abi.cpp | 6 +- .../src/debug_ring/tests/test_debug_ring.cpp | 2 +- 27 files changed, 485 insertions(+), 135 deletions(-) create mode 100644 bfsdk/tests/test_affinity.cpp create mode 100644 bfsdk/tests/test_benchmark.cpp create mode 100644 bfsdk/tests/test_debugringinterface.cpp create mode 100644 bfsdk/tests/test_gsl.cpp create mode 100644 bfsdk/tests/test_newdelete.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bff3bae2..bdb4304b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -682,18 +682,21 @@ 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}/bfelf_loader/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}/bfelf_loader/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 ) + if(BUILD_SHARED_LIBS) + add_custom_command(TARGET tidy COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfelf_loader/build --target tidy) + add_custom_command(TARGET tidy-all COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfelf_loader/build --target tidy-all) + endif() + if(NOT ENABLE_UNITTESTING) add_custom_command(TARGET tidy COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfvmm/build --target tidy) add_custom_command(TARGET tidy COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/bfsysroot/build --target tidy) diff --git a/bfsdk/CMakeLists.txt b/bfsdk/CMakeLists.txt index 734c0b162..a89813e70 100644 --- a/bfsdk/CMakeLists.txt +++ b/bfsdk/CMakeLists.txt @@ -23,6 +23,7 @@ ExternalProject_Add( DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/json/download SOURCE_DIR ${CMAKE_BINARY_DIR}/json/src BINARY_DIR ${CMAKE_BINARY_DIR}/json/build + UPDATE_DISCONNECTED 1 ) # ------------------------------------------------------------------------------ @@ -45,6 +46,7 @@ ExternalProject_Add( DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/gsl/download SOURCE_DIR ${CMAKE_BINARY_DIR}/gsl/src BINARY_DIR ${CMAKE_BINARY_DIR}/gsl/build + UPDATE_DISCONNECTED 1 ) # ------------------------------------------------------------------------------ @@ -69,6 +71,7 @@ if(ENABLE_UNITTESTING) DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/catch/download SOURCE_DIR ${CMAKE_BINARY_DIR}/catch/src BINARY_DIR ${CMAKE_BINARY_DIR}/catch/build + UPDATE_DISCONNECTED 1 ) endif() @@ -95,6 +98,7 @@ if(ENABLE_UNITTESTING) DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/hippomocks/download SOURCE_DIR ${CMAKE_BINARY_DIR}/hippomocks/src BINARY_DIR ${CMAKE_BINARY_DIR}/hippomocks/build + UPDATE_DISCONNECTED 1 ) endif() @@ -122,6 +126,7 @@ if(ENABLE_ASTYLE) DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/astyle/download SOURCE_DIR ${CMAKE_BINARY_DIR}/astyle/src BINARY_DIR ${CMAKE_BINARY_DIR}/astyle/build + UPDATE_DISCONNECTED 1 ) endif() diff --git a/bfsdk/cmake/CMakeOption_ASan.txt b/bfsdk/cmake/CMakeOption_ASan.txt index 39072afc8..87580d527 100644 --- a/bfsdk/cmake/CMakeOption_ASan.txt +++ b/bfsdk/cmake/CMakeOption_ASan.txt @@ -3,7 +3,6 @@ if(ENABLE_DYNAMIC_ASAN AND NOT WIN32) message(STATUS "Testing Config: Dynamic Analysis") set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -g") - set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -O1") set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fuse-ld=gold") set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fno-omit-frame-pointer") set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fsanitize=address") diff --git a/bfsdk/include/bfbenchmark.h b/bfsdk/include/bfbenchmark.h index 345622390..e19a200ab 100644 --- a/bfsdk/include/bfbenchmark.h +++ b/bfsdk/include/bfbenchmark.h @@ -44,7 +44,7 @@ uint64_t benchmark(T func) size_t g_page_allocs = 0; size_t g_nonpage_allocs = 0; -static void * +inline void * custom_new(std::size_t size) { if ((size & 0xFFF) == 0) { @@ -57,7 +57,7 @@ custom_new(std::size_t size) return malloc(size); } -static void +inline void custom_delete(void *ptr, std::size_t size) { bfignored(size); @@ -78,7 +78,7 @@ operator delete (void *ptr, std::size_t size) throw() void operator delete (void *ptr) throw() -{ custom_delete(ptr, 0); } +{ operator delete (ptr, static_cast(0)); } void operator delete[](void *ptr, std::size_t size) throw() @@ -86,9 +86,7 @@ operator delete[](void *ptr, std::size_t size) throw() void operator delete[](void *ptr) throw() -{ custom_delete(ptr, 0); } - -#endif +{ operator delete[](ptr, static_cast(0)); } inline void print_memory_stats() @@ -107,3 +105,5 @@ clear_memory_stats() g_page_allocs = 0; g_nonpage_allocs = 0; } + +#endif diff --git a/bfsdk/include/bfdebugringinterface.h b/bfsdk/include/bfdebugringinterface.h index 8ddf87fad..db62ce782 100644 --- a/bfsdk/include/bfdebugringinterface.h +++ b/bfsdk/include/bfdebugringinterface.h @@ -28,6 +28,7 @@ #define BFDEBUGRINGINTERFACE_H #include +#include #include #include @@ -130,31 +131,30 @@ debug_ring_read(struct debug_ring_resources_t *drr, char *str, uint64_t len) { uint64_t i; uint64_t spos; + uint64_t count; uint64_t content; - if (drr == 0 || str == 0 || len == 0) - { return 0; } + if (drr == 0 || str == 0 || len == 0) { + return 0; + } + + if (drr->spos > drr->epos) { + return 0; + } spos = drr->spos % DEBUG_RING_SIZE; content = drr->epos - drr->spos; - for (i = 0; i < content && i < len - 1; i++) { - if (spos == DEBUG_RING_SIZE) - { spos = 0; } - - if (drr->buf[spos] != '\0') - { str[i] = drr->buf[spos]; } - else { - i--; - content--; + for (i = 0, count = 0; i < content && i < len - 1; i++) { + if (drr->buf[spos] != '\0') { + str[count++] = drr->buf[spos]; } - spos++; + spos = ((spos + 1) % DEBUG_RING_SIZE); } str[i] = '\0'; - - return content; + return count; } #ifdef __cplusplus diff --git a/bfsdk/include/bfexception.h b/bfsdk/include/bfexception.h index 79337d50e..780acc88c 100644 --- a/bfsdk/include/bfexception.h +++ b/bfsdk/include/bfexception.h @@ -57,7 +57,7 @@ guard_exceptions(int64_t error_code, T func) return BF_BAD_ALLOC; } catch (std::exception &e) { - bfdebug_transaction(1, [&](std::string * msg) { + bfdebug_transaction(0, [&](std::string * msg) { bferror_lnbr(0, msg); bferror_brk1(0, msg); bferror_info(0, typeid(e).name(), msg); @@ -66,7 +66,7 @@ guard_exceptions(int64_t error_code, T func) }); } catch (...) { - bfdebug_transaction(1, [&](std::string * msg) { + bfdebug_transaction(0, [&](std::string * msg) { bferror_lnbr(0, msg); bferror_brk1(0, msg); bferror_info(0, "unknown exception", msg); diff --git a/bfsdk/include/bfgsl.h b/bfsdk/include/bfgsl.h index 9a7fd65bf..d7c4baf24 100644 --- a/bfsdk/include/bfgsl.h +++ b/bfsdk/include/bfgsl.h @@ -42,7 +42,9 @@ #include #else +#ifdef NEED_STD_LITE #include +#endif /// @cond @@ -54,13 +56,17 @@ #define gsl_unlikely(x) (x) #endif +#ifndef GSL_ABORT +#define GSL_ABORT abort +#endif + #define expects(cond) \ if (gsl_unlikely(!(cond))) { \ - std::terminate(); \ + GSL_ABORT(); \ } #define ensures(cond) \ if (gsl_unlikely(!(cond))) { \ - std::terminate(); \ + GSL_ABORT(); \ } /// @endcond @@ -159,4 +165,5 @@ at(const T *arr, size_t N, I index) } #endif + #endif diff --git a/bfsdk/include/bfnewdelete.h b/bfsdk/include/bfnewdelete.h index 0123ee2b8..d1529d5b2 100644 --- a/bfsdk/include/bfnewdelete.h +++ b/bfsdk/include/bfnewdelete.h @@ -38,7 +38,7 @@ size_t g_new_throws_bad_alloc = 0; #define aligned_alloc _aligned_malloc #endif -static void * +inline void * custom_new(std::size_t size) { if (size == g_new_throws_bad_alloc || size == 0xFFFFFFFFFFFFFFFF) { @@ -52,9 +52,12 @@ custom_new(std::size_t size) return malloc(size); } -static void -custom_delete(void *ptr) -{ free(ptr); } +inline void +custom_delete(void *ptr, std::size_t size) +{ + bfignored(size); + free(ptr); +} void * operator new[](std::size_t size) @@ -65,19 +68,19 @@ operator new (std::size_t size) { return custom_new(size); } void -operator delete (void *ptr, std::size_t /* size */) throw() -{ custom_delete(ptr); } +operator delete (void *ptr, std::size_t size) throw() +{ custom_delete(ptr, size); } void operator delete (void *ptr) throw() -{ custom_delete(ptr); } +{ operator delete (ptr, static_cast(0)); } void -operator delete[](void *ptr) throw() -{ custom_delete(ptr); } +operator delete[](void *ptr, std::size_t size) throw() +{ custom_delete(ptr, size); } void -operator delete[](void *ptr, std::size_t /* size */) throw() -{ custom_delete(ptr); } +operator delete[](void *ptr) throw() +{ operator delete[](ptr, static_cast(0)); } #endif diff --git a/bfsdk/scripts/bareflank_clang_tidy.sh b/bfsdk/scripts/bareflank_clang_tidy.sh index ae6ced91d..654e59354 100755 --- a/bfsdk/scripts/bareflank_clang_tidy.sh +++ b/bfsdk/scripts/bareflank_clang_tidy.sh @@ -70,8 +70,12 @@ OUTPUT=$PWD/.clang_tidy_results.txt NUM_CORES=$(grep -c ^processor /proc/cpuinfo) get_changed_files() { - pushd $1 > /dev/null - files=$(git diff --relative --name-only --diff-filter=ACM HEAD^ $PWD | grep -Ee "\.(cpp|h|c)$" || true) + pushd $2 > /dev/null + if [[ "$1" == "all" ]]; then + files=$(git ls-files | grep -Ee "\.(cpp|h|c)$" || true) + else + files=$(git diff --relative --name-only --diff-filter=ACM HEAD^ $PWD | grep -Ee "\.(cpp|h|c)$" || true) + fi popd > /dev/null } @@ -89,33 +93,22 @@ verify_analysis() { fi } -run_clang_tidy_all() { - run-clang-tidy-4.0.py \ - -clang-tidy-binary clang-tidy-4.0 \ - -header-filter="*.h" \ - -j=$NUM_CORES \ - -checks=$1 > $OUTPUT 2>&1 -} - -run_clang_tidy_diff() { - run-clang-tidy-4.0.py \ - -clang-tidy-binary clang-tidy-4.0 \ - -header-filter="*.h" \ - -j=$NUM_CORES \ - -checks=$1 \ - files $2 > $OUTPUT 2>&1 +run_clang_tidy_script() { + if [[ ! $2 == *"pthread.cpp" ]] && [[ ! $2 == *"syscall.cpp" ]]; then + run-clang-tidy-4.0.py \ + -clang-tidy-binary clang-tidy-4.0 \ + -header-filter="*.h" \ + -j=$NUM_CORES \ + -checks=$1 \ + files $2 > $OUTPUT 2>&1 + fi } analyze() { - if [[ "$1" == "all" ]]; then - run_clang_tidy_all $2 - verify_analysis "$3" - else - for f in $files; do - run_clang_tidy_diff $2 $f - verify_analysis "$3: $f" - done - fi + for f in $files; do + run_clang_tidy_script $2 $f + verify_analysis "$3: $f" + done } if [[ "$#" -lt 2 ]]; then @@ -139,14 +132,7 @@ if [[ ! "$1" == "all" ]] && [[ ! "$1" == "diff" ]]; then exit 1 fi -if [[ "$1" == "diff" ]]; then - get_changed_files $2 - - echo "Files undergoing static analysis:" - for f in $files; do - echo " - $f" - done -fi +get_changed_files $1 $2 # # Perform Checks diff --git a/bfsdk/scripts/compiler_wrapper.sh b/bfsdk/scripts/compiler_wrapper.sh index 1c6d3a3c8..d4f32137d 100755 --- a/bfsdk/scripts/compiler_wrapper.sh +++ b/bfsdk/scripts/compiler_wrapper.sh @@ -70,21 +70,21 @@ do i=$((i+1)) ;; - "-nostdinc-c") + "-DNOSTDINC_C") export DISABLE_LIB_C="true" export DISABLE_INCLUDE_C="true" ;; - "-nostdinc-c++") + "-DNOSTDINC_CXX") export DISABLE_LIB_CXX="true" export DISABLE_INCLUDE_CXX="true" ;; - "-nostdlib-c") + "-DNOSTDLIB_C") export DISABLE_LIB_C="true" ;; - "-nostdlib-c++") + "-DNOSTDLIB_CXX") export DISABLE_LIB_CXX="true" ;; @@ -169,19 +169,19 @@ do case $ARG in - "-nostdinc-c") + "-DNOSTDINC_C") continue ;; - "-nostdinc-c++") + "-DNOSTDINC_CXX") continue ;; - "-nostdlib-c") + "-DNOSTDLIB_C") continue ;; - "-nostdlib-c++") + "-DNOSTDLIB_CXX") continue ;; diff --git a/bfsdk/tests/CMakeLists.txt b/bfsdk/tests/CMakeLists.txt index c5e5c48cc..32f347261 100644 --- a/bfsdk/tests/CMakeLists.txt +++ b/bfsdk/tests/CMakeLists.txt @@ -21,13 +21,18 @@ macro(do_test str) add_test(test_${str} test_${str}) endmacro(do_test) +do_test(affinity) +do_test(benchmark) do_test(bitmanip) do_test(buffer) do_test(debug) +do_test(debugringinterface) do_test(errorcodes) do_test(exceptions) do_test(file) +do_test(gsl) do_test(json) +do_test(newdelete) do_test(shuffle) do_test(string) do_test(types) diff --git a/bfsdk/tests/test_affinity.cpp b/bfsdk/tests/test_affinity.cpp new file mode 100644 index 000000000..8424a06c5 --- /dev/null +++ b/bfsdk/tests/test_affinity.cpp @@ -0,0 +1,29 @@ +// +// Bareflank Hypervisor +// +// Copyright (C) 2015 Assured Information Security, Inc. +// Author: Rian Quinn +// Author: Brendan Kerrigan +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include +#include + +TEST_CASE("set_affinity") +{ + CHECK(set_affinity(0) == 0); + CHECK(set_affinity(31) == -1); +} diff --git a/bfsdk/tests/test_benchmark.cpp b/bfsdk/tests/test_benchmark.cpp new file mode 100644 index 000000000..8421e2f7b --- /dev/null +++ b/bfsdk/tests/test_benchmark.cpp @@ -0,0 +1,55 @@ +// +// Bareflank Hypervisor +// +// Copyright (C) 2015 Assured Information Security, Inc. +// Author: Rian Quinn +// Author: Brendan Kerrigan +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include +#include + +TEST_CASE("benchmark") +{ + CHECK(benchmark([] { + std::cout << "the answer is 42\n"; + }) != 0); +} + +TEST_CASE("non-array new/delete") +{ + std::make_unique(); +} + +TEST_CASE("array new/delete") +{ + clear_memory_stats(); + + std::make_unique(42); + std::make_unique(0x1000); + + auto page_allocs = g_page_allocs; + auto nonpage_allocs = g_nonpage_allocs; + + CHECK(page_allocs == 0x1000); + CHECK(nonpage_allocs == 42); +} + +TEST_CASE("memory stats") +{ + print_memory_stats(); + clear_memory_stats(); +} diff --git a/bfsdk/tests/test_buffer.cpp b/bfsdk/tests/test_buffer.cpp index a876548c1..bd510bf1c 100644 --- a/bfsdk/tests/test_buffer.cpp +++ b/bfsdk/tests/test_buffer.cpp @@ -170,3 +170,9 @@ TEST_CASE("resize") CHECK(buffer.size() == 2); CHECK(buffer.span()[0] == 'h'); } + +TEST_CASE("ostream") +{ + bfn::buffer buffer; + CHECK_NOTHROW(std::cout << buffer << '\n'); +} diff --git a/bfsdk/tests/test_debugringinterface.cpp b/bfsdk/tests/test_debugringinterface.cpp new file mode 100644 index 000000000..d319b6dbd --- /dev/null +++ b/bfsdk/tests/test_debugringinterface.cpp @@ -0,0 +1,101 @@ +// +// Bareflank Hypervisor +// +// Copyright (C) 2015 Assured Information Security, Inc. +// Author: Rian Quinn +// Author: Brendan Kerrigan +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include +#include + +char g_buf[DEBUG_RING_SIZE] = {}; +debug_ring_resources_t g_drr{}; + +TEST_CASE("debug_ring_read: invalid drr") +{ + CHECK(debug_ring_read(nullptr, static_cast(g_buf), DEBUG_RING_SIZE) == 0); +} + +TEST_CASE("debug_ring_read: invalid str") +{ + CHECK(debug_ring_read(&g_drr, nullptr, DEBUG_RING_SIZE) == 0); +} + +TEST_CASE("debug_ring_read: invalid len") +{ + CHECK(debug_ring_read(&g_drr, static_cast(g_buf), 0) == 0); +} + +TEST_CASE("debug_ring_read: invalid spos / epos") +{ + g_drr.spos = 42; + g_drr.epos = 0; + CHECK(debug_ring_read(&g_drr, static_cast(g_buf), DEBUG_RING_SIZE) == 0); +} + +TEST_CASE("debug_ring_read: no data") +{ + g_drr.spos = 0; + g_drr.epos = 0; + CHECK(debug_ring_read(&g_drr, static_cast(g_buf), DEBUG_RING_SIZE) == 0); +} + +TEST_CASE("debug_ring_read: content, but no read buffer") +{ + g_drr.spos = 0; + g_drr.epos = 42; + CHECK(debug_ring_read(&g_drr, static_cast(g_buf), 1) == 0); +} + +TEST_CASE("debug_ring_read: all 0") +{ + g_drr.spos = DEBUG_RING_SIZE - 42; + g_drr.epos = DEBUG_RING_SIZE + 42; + + auto view = gsl::make_span(g_drr.buf); + for (auto &elem : view) { + elem = 0; + } + + CHECK(debug_ring_read(&g_drr, static_cast(g_buf), DEBUG_RING_SIZE) == 0); +} + +TEST_CASE("debug_ring_read: wrap") +{ + g_drr.spos = DEBUG_RING_SIZE - 42; + g_drr.epos = DEBUG_RING_SIZE + 42; + + auto view = gsl::make_span(g_drr.buf); + for (auto &elem : view) { + elem = 'A'; + } + + CHECK(debug_ring_read(&g_drr, static_cast(g_buf), DEBUG_RING_SIZE) == 42 * 2); +} + +TEST_CASE("debug_ring_read: full") +{ + g_drr.spos = 0; + g_drr.epos = DEBUG_RING_SIZE; + + auto view = gsl::make_span(g_drr.buf); + for (auto &elem : view) { + elem = 'A'; + } + + CHECK(debug_ring_read(&g_drr, static_cast(g_buf), DEBUG_RING_SIZE) == DEBUG_RING_SIZE - 1); +} diff --git a/bfsdk/tests/test_gsl.cpp b/bfsdk/tests/test_gsl.cpp new file mode 100644 index 000000000..2dc65da0a --- /dev/null +++ b/bfsdk/tests/test_gsl.cpp @@ -0,0 +1,106 @@ +// +// Bareflank Hypervisor +// +// Copyright (C) 2015 Assured Information Security, Inc. +// Author: Rian Quinn +// Author: Brendan Kerrigan +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include +#include + +#define NEED_GSL_LITE +#define GSL_ABORT ut_abort + +void ut_abort() +{ } + +#include +#include + +#ifdef _HIPPOMOCKS__ENABLE_CFUNC_MOCKING_SUPPORT + +TEST_CASE("expects / ensures") +{ + ut_abort(); + + { + MockRepository mocks; + mocks.ExpectCallFunc(ut_abort); + mocks.ExpectCallFunc(ut_abort); + + expects(true == true); + ensures(true == true); + + expects(false == true); + ensures(false == true); + } + + // Note: + // + // Clang Tidy seems to have a bug and thinks that the Mock library + // has a dangling pointer if this is not done. This might be able to + // be removed when Clang Tidy is updated + // + HippoMocks::MockRepoInstanceHolder<0>::instance = nullptr; +} + +TEST_CASE("narrow cast") +{ + auto var = gsl::narrow_cast(42); +} + +TEST_CASE("array") +{ + int count = 0; + char buf[42] = {}; + + for (int i = 0; i < 42; i++) { + count += gsl::at(buf, i); + } +} + +TEST_CASE("const array") +{ + int count = 0; + const char buf[42] = {}; + + for (int i = 0; i < 42; i++) { + count += gsl::at(buf, i); + } +} + +TEST_CASE("array with provided limit") +{ + int count = 0; + char buf[42] = {}; + + for (int i = 0; i < 42; i++) { + count += gsl::at(static_cast(buf), 42, i); + } +} + +TEST_CASE("const array with provided limit") +{ + int count = 0; + const char buf[42] = {}; + + for (int i = 0; i < 42; i++) { + count += gsl::at(static_cast(buf), 42, i); + } +} + +#endif diff --git a/bfsdk/tests/test_newdelete.cpp b/bfsdk/tests/test_newdelete.cpp new file mode 100644 index 000000000..86f6cd44b --- /dev/null +++ b/bfsdk/tests/test_newdelete.cpp @@ -0,0 +1,51 @@ +// +// Bareflank Hypervisor +// +// Copyright (C) 2015 Assured Information Security, Inc. +// Author: Rian Quinn +// Author: Brendan Kerrigan +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include + +#include +#include + +#ifndef _WIN32 + +TEST_CASE("bad alloc") +{ + g_new_throws_bad_alloc = 42; + auto ___ = gsl::finally([] { + g_new_throws_bad_alloc = 0; + }); + + CHECK_THROWS(std::make_unique(42)); + CHECK_THROWS(std::make_unique(0xFFFFFFFFFFFFFFFF)); +} + +TEST_CASE("non-array new/delete") +{ + std::make_unique(); +} + +TEST_CASE("array new/delete") +{ + std::make_unique(42); + std::make_unique(0x1000); +} + +#endif diff --git a/bfsysroot/CMakeLists.txt b/bfsysroot/CMakeLists.txt index fcfb1eb38..d4af1213f 100644 --- a/bfsysroot/CMakeLists.txt +++ b/bfsysroot/CMakeLists.txt @@ -19,7 +19,7 @@ if(ENABLE_UNITTESTING) else() set(NEWLIB_CFLAGS - "${CMAKE_C_FLAGS} -nostdinc-c" + "${CMAKE_C_FLAGS} -DNOSTDINC_C" ) if(CMAKE_BUILD_TYPE STREQUAL "Release") @@ -130,7 +130,7 @@ else() -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_AR=${CMAKE_AR} -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} - -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} -nostdinc-c++ + "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} -DNOSTDINC_CXX" ) if(BUILD_SHARED_LIBS) @@ -178,7 +178,7 @@ else() -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_AR=${CMAKE_AR} -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} - -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} -nostdinc-c++ + "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} -DNOSTDINC_CXX" ) if(BUILD_SHARED_LIBS) diff --git a/bfsysroot/bfsupport/CMakeLists.txt b/bfsysroot/bfsupport/CMakeLists.txt index b2c515255..e489e5cf0 100644 --- a/bfsysroot/bfsupport/CMakeLists.txt +++ b/bfsysroot/bfsupport/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.6) project(bfsupport C CXX) +set(TIDY_EXCLUSIONS ,-cert-err34-c,-misc-misplaced-widening-cast,-cppcoreguidelines-no-malloc) include(${CMAKE_INSTALL_PREFIX}/cmake/CMakeGlobal_Project.txt) # ------------------------------------------------------------------------------ @@ -8,8 +9,8 @@ include(${CMAKE_INSTALL_PREFIX}/cmake/CMakeGlobal_Project.txt) # ------------------------------------------------------------------------------ if(CMAKE_TOOLCHAIN_FILE) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdlib-c") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib-c -nostdlib-c++") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNOSTDLIB_C") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNOSTDLIB_C -DNOSTDLIB_CXX") endif() # ------------------------------------------------------------------------------ diff --git a/bfsysroot/bfsupport/crt/crt.cpp b/bfsysroot/bfsupport/crt/crt.cpp index 739a4e713..2611ca217 100644 --- a/bfsysroot/bfsupport/crt/crt.cpp +++ b/bfsysroot/bfsupport/crt/crt.cpp @@ -18,6 +18,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #define NEED_GSL_LITE +#define NEED_STD_LITE #include #include @@ -26,8 +27,8 @@ #include #include -typedef void (*init_t)(); -typedef void (*fini_t)(); +using init_t = void (*)(); +using fini_t = void (*)(); int __attribute__((weak)) main(int argc, const char *argv[]) @@ -54,7 +55,7 @@ extern eh_frame_t __g_eh_frame_list[MAX_NUM_MODULES]; extern int __g_dwarf_sections_num; extern dwarf_sections_t __g_dwarf_sections[MAX_NUM_MODULES]; -EXPORT_SYM void *__dso_handle = 0; +EXPORT_SYM void *__dso_handle = nullptr; extern "C" void __bareflank_init(const section_info_t *info) noexcept @@ -151,9 +152,7 @@ _start_c(const crt_info_t *info) noexcept if (info->arg_type == 0 || info->request == BF_REQUEST_FINI) { for (auto i = 0; i < info->info_num; i++) { - auto sinfo = &gsl::at(info->info, i); - - __bareflank_fini(sinfo); + __bareflank_fini(&gsl::at(info->info, i)); } } diff --git a/bfsysroot/bfsupport/dummy_main/dummy_main.cpp b/bfsysroot/bfsupport/dummy_main/dummy_main.cpp index f2df23148..816c4784d 100644 --- a/bfsysroot/bfsupport/dummy_main/dummy_main.cpp +++ b/bfsysroot/bfsupport/dummy_main/dummy_main.cpp @@ -45,8 +45,8 @@ main(int argc, char *argv[]) catch (std::exception &) { } - return g_derived1.foo(gsl::narrow_cast(atoi(argv[0]))) + - g_derived2.foo(gsl::narrow_cast(atoi(argv[1]))); + return g_derived1.foo(gsl::narrow_cast(atoi(gsl::at(argv, static_cast(argc), 0)))) + + g_derived2.foo(gsl::narrow_cast(atoi(gsl::at(argv, static_cast(argc), 1)))); } extern "C" int64_t @@ -90,7 +90,7 @@ _malloc_r(struct _reent *ent, size_t size) { bfignored(ent); - auto *addr = &g_memory[g_cursor]; + auto *addr = &gsl::at(g_memory, g_cursor); g_cursor += size; return addr; diff --git a/bfsysroot/bfsupport/pthread/pthread.cpp b/bfsysroot/bfsupport/pthread/pthread.cpp index 84d973296..b33e6a760 100644 --- a/bfsysroot/bfsupport/pthread/pthread.cpp +++ b/bfsysroot/bfsupport/pthread/pthread.cpp @@ -61,7 +61,7 @@ void *threadSpecificData[MAX_THREAD_SPECIFIC_DATA] = {0}; extern "C" EXPORT_SYM int pthread_cond_broadcast(pthread_cond_t *cond) { - if (!cond) { + if (cond == nullptr) { return -EINVAL; } @@ -80,11 +80,11 @@ pthread_cond_destroy(pthread_cond_t *) extern "C" EXPORT_SYM int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { - if (attr) { + if (attr != nullptr) { ARG_UNSUPPORTED("attr"); } - if (!cond) { + if (cond == nullptr) { return -EINVAL; } @@ -109,7 +109,7 @@ pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *, const struct timespe extern "C" EXPORT_SYM int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { - if (!cond || !mutex) { + if (cond == nullptr || mutex == nullptr) { return -EINVAL; } @@ -162,11 +162,11 @@ pthread_key_create(pthread_key_t *key, void (*destructor)(void *)) { static int64_t g_keys = 0; - if (destructor) { + if (destructor != nullptr) { ARG_UNSUPPORTED("destructor"); } - if (!key) { + if (key == nullptr) { return -EINVAL; } @@ -192,11 +192,11 @@ pthread_mutex_destroy(pthread_mutex_t *) extern "C" EXPORT_SYM int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { - if (attr) { + if (attr != nullptr) { ARG_UNSUPPORTED("attr"); } - if (!mutex) { + if (mutex == nullptr) { return -EINVAL; } @@ -207,7 +207,7 @@ pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) extern "C" EXPORT_SYM int pthread_mutex_lock(pthread_mutex_t *mutex) { - if (!mutex) { + if (mutex == nullptr) { return -EINVAL; } @@ -226,7 +226,7 @@ pthread_mutex_trylock(pthread_mutex_t *) extern "C" EXPORT_SYM int pthread_mutex_unlock(pthread_mutex_t *mutex) { - if (!mutex) { + if (mutex == nullptr) { return -EINVAL; } @@ -259,7 +259,7 @@ pthread_mutexattr_settype(pthread_mutexattr_t *, int) extern "C" EXPORT_SYM int pthread_once(pthread_once_t *once, void (*init)(void)) { - if (!once || !init) { + if (once == nullptr || init == nullptr) { return -EINVAL; } diff --git a/bfsysroot/bfunwind/CMakeLists.txt b/bfsysroot/bfunwind/CMakeLists.txt index 0c683f3cf..fc32b1b7b 100644 --- a/bfsysroot/bfunwind/CMakeLists.txt +++ b/bfsysroot/bfunwind/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.6) project(bfunwind C CXX) +set(TIDY_EXCLUSIONS ,-cppcoreguidelines-pro*) include(${CMAKE_INSTALL_PREFIX}/cmake/CMakeGlobal_Project.txt) # ------------------------------------------------------------------------------ @@ -8,8 +9,8 @@ include(${CMAKE_INSTALL_PREFIX}/cmake/CMakeGlobal_Project.txt) # ------------------------------------------------------------------------------ if(CMAKE_TOOLCHAIN_FILE) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdlib-c") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib-c -nostdinc-c++") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNOSTDLIB_C") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNOSTDLIB_C -DNOSTDINC_CXX") endif() # ------------------------------------------------------------------------------ diff --git a/bfsysroot/bfunwind/src/dwarf4.cpp b/bfsysroot/bfunwind/src/dwarf4.cpp index 02664b8b6..a9b8faf8c 100644 --- a/bfsysroot/bfunwind/src/dwarf4.cpp +++ b/bfsysroot/bfunwind/src/dwarf4.cpp @@ -51,11 +51,7 @@ template T static get(char **p) class cfi_register { public: - cfi_register() : - m_index(0), - m_rule(rule_undefined), - m_value(0) - {} + cfi_register() = default; cfi_register(uint64_t index, register_rules rule, uint64_t value) : m_index(index), @@ -82,9 +78,9 @@ class cfi_register { m_value = value; } private: - uint64_t m_index; - register_rules m_rule; - uint64_t m_value; + uint64_t m_index{0}; + register_rules m_rule{rule_undefined}; + uint64_t m_value{0}; }; // ----------------------------------------------------------------------------- @@ -101,11 +97,7 @@ class cfi_cfa }; public: - cfi_cfa() : - m_value(0), - m_offset(0), - m_type(cfa_register) - {} + cfi_cfa() = default; uint64_t value() const { return m_value; } @@ -126,9 +118,9 @@ class cfi_cfa { m_type = type; } private: - uint64_t m_value; - int64_t m_offset; - cfi_cfa_type m_type; + uint64_t m_value{0}; + int64_t m_offset{0}; + cfi_cfa_type m_type{cfa_register}; }; // ----------------------------------------------------------------------------- @@ -138,8 +130,7 @@ class cfi_cfa class cfi_table_row { public: - cfi_table_row() : - m_arg_size(0) + cfi_table_row() { for (auto i = 0U; i < MAX_NUM_REGISTERS; i++) { m_registers[i].set_index(i); @@ -196,7 +187,7 @@ class cfi_table_row private: cfi_cfa m_cfa; - uint64_t m_arg_size; + uint64_t m_arg_size{0}; cfi_register m_registers[MAX_NUM_REGISTERS]; }; @@ -1227,7 +1218,7 @@ void private_parse_instruction(cfi_table_row *row, const ci_entry &cie, char **p, - uint64_t *l1, + const uint64_t *l1, uint64_t *l2, uint64_t pc_begin, register_state *state, @@ -1459,9 +1450,10 @@ private_parse_instructions(cfi_table_row *row, auto initialRow = *row; - while (p < end && l1 >= l2) - private_parse_instruction(row, cie, &p, &l1, &l2, pc_begin, state, - rememberIndex, rememberStack, &initialRow); + while (p < end && l1 >= l2) { + private_parse_instruction( + row, cie, &p, &l1, &l2, pc_begin, state, rememberIndex, rememberStack, &initialRow); + } } cfi_table_row diff --git a/bfsysroot/bfunwind/src/eh_frame.cpp b/bfsysroot/bfunwind/src/eh_frame.cpp index 296dc0692..9520f965d 100644 --- a/bfsysroot/bfunwind/src/eh_frame.cpp +++ b/bfsysroot/bfunwind/src/eh_frame.cpp @@ -282,7 +282,8 @@ ci_entry::non_virtual_parse(char *addr) m_augmentation_string = p; - while (*p++ != 0); + while (*p++ != 0) + { } m_code_alignment = dwarf4::decode_uleb128(&p); m_data_alignment = dwarf4::decode_sleb128(&p); diff --git a/bfsysroot/bfunwind/src/ia64_cxx_abi.cpp b/bfsysroot/bfunwind/src/ia64_cxx_abi.cpp index 0455efaf1..6684d3315 100644 --- a/bfsysroot/bfunwind/src/ia64_cxx_abi.cpp +++ b/bfsysroot/bfunwind/src/ia64_cxx_abi.cpp @@ -193,9 +193,9 @@ _Unwind_Resume(_Unwind_Exception *exception_object) extern "C" EXPORT_SYM void _Unwind_DeleteException(_Unwind_Exception *exception_object) { - if (exception_object->exception_cleanup != nullptr) - (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, - exception_object); + if (exception_object->exception_cleanup != nullptr) { + (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, exception_object); + } } extern "C" EXPORT_SYM uintptr_t diff --git a/bfvmm/src/debug_ring/tests/test_debug_ring.cpp b/bfvmm/src/debug_ring/tests/test_debug_ring.cpp index 7c8517c64..75c5ee03b 100644 --- a/bfvmm/src/debug_ring/tests/test_debug_ring.cpp +++ b/bfvmm/src/debug_ring/tests/test_debug_ring.cpp @@ -137,7 +137,7 @@ TEST_CASE("write: fill_dr") init_wb(DEBUG_RING_SIZE - 1); CHECK_NOTHROW(dr.write(static_cast(wb))); - CHECK(debug_ring_read(drr, static_cast(rb), DEBUG_RING_SIZE) == DEBUG_RING_SIZE); + CHECK(debug_ring_read(drr, static_cast(rb), DEBUG_RING_SIZE) == DEBUG_RING_SIZE - 1); CHECK(rb[DEBUG_RING_SIZE - 1] == '\0'); }