From ddab11133ac47175341b4ba832554ce099062ea8 Mon Sep 17 00:00:00 2001 From: Felix Moessbauer Date: Wed, 8 Jan 2020 10:16:35 +0100 Subject: [PATCH] test: add support to run detector if tests on unix --- common/util/LibLoaderFactory.h | 9 +++++ test/CMakeLists.txt | 6 +--- test/include/detectorTest.h | 66 ++++++++++++++-------------------- test/src/DetectorTest.cpp | 16 ++++----- 4 files changed, 44 insertions(+), 53 deletions(-) diff --git a/common/util/LibLoaderFactory.h b/common/util/LibLoaderFactory.h index 55559ad..2745648 100644 --- a/common/util/LibLoaderFactory.h +++ b/common/util/LibLoaderFactory.h @@ -30,5 +30,14 @@ namespace util return std::make_shared(); #endif } + + /// return the filename extension of a shared module (e.g. '.dll') + static std::string getModuleExtension() { + #ifdef WIN32 + return ".dll"; + #else + return ".so"; + #endif + } }; } // namespace util diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0b6a308..b46f526 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -64,11 +64,7 @@ endif() ##### SETUP DRace UNIT TESTS ##### -if(DRACE_ENABLE_RUNTIME) - add_executable(${TEST_UNIT_TARGET} ${TEST_SOURCES_UNIT}) -else() - add_executable(${TEST_UNIT_TARGET}) -endif() +add_executable(${TEST_UNIT_TARGET} ${TEST_SOURCES_UNIT}) target_include_directories(${TEST_UNIT_TARGET} PRIVATE "include") target_link_libraries(${TEST_UNIT_TARGET} PRIVATE gtest gtest_main "drace-common") diff --git a/test/include/detectorTest.h b/test/include/detectorTest.h index 7f007d3..abc6f80 100644 --- a/test/include/detectorTest.h +++ b/test/include/detectorTest.h @@ -11,10 +11,10 @@ */ #include -#include +#include #include #include -#include +#include static unsigned num_races = 0; static Detector::Race last_race; @@ -22,13 +22,9 @@ static Detector::Race last_race; class DetectorTest : public ::testing::TestWithParam{ protected: - static util::WindowsLibLoader _libtsan; - static util::WindowsLibLoader _libdummy; - static util::WindowsLibLoader _libfasttrack; - - static Detector * _dettsan; - static Detector * _detdummy; - static Detector * _detfasttrack; + /// map to store instances of dll / so loader objects + static std::unordered_map> _libs; + static std::unordered_map _detectors; public: Detector * detector; @@ -44,46 +40,36 @@ class DetectorTest : public ::testing::TestWithParam{ num_races = 0; last_race = {}; - if (std::string(GetParam()).compare("tsan") == 0) { - detector = _dettsan; - } - else if (std::string(GetParam()).compare("dummy") == 0) { - detector = _detdummy; - } - else if (std::string(GetParam()).compare("fasttrack") == 0) { - detector = _detfasttrack; + // we bind the detector lazy, i.e. if it is not loaded yet, load it + auto det_it = _detectors.find(GetParam()); + if(det_it == _detectors.end()){ + auto it = _libs.emplace(GetParam(), util::LibLoaderFactory::getLoader()); + std::string name = "drace.detector." + std::string(GetParam()) + util::LibLoaderFactory::getModuleExtension(); + + auto & loader =*(it.first->second); + if(!loader.load(name.c_str())) + std::cerr <<"could not load detector" << std::endl; + decltype(CreateDetector)* create_detector = loader["CreateDetector"]; + det_it = _detectors.emplace(GetParam(), create_detector()).first; + + detector = det_it->second; + const char * _argv = "drace-tests.exe"; + detector->init(1, &_argv, callback); + } else { + detector = det_it->second; } } ~DetectorTest() { detector = nullptr; } - // TSAN can only be initialized once, even after a finalize static void SetUpTestCase() { - _libtsan.load("drace.detector.tsan.dll"); - _libdummy.load("drace.detector.dummy.dll"); - - //to make this work copy dynamorio.dll in the test folder - _libfasttrack.load("drace.detector.fasttrack.standalone.dll"); - - - decltype(CreateDetector)* create_tsan = _libtsan["CreateDetector"]; - decltype(CreateDetector)* create_dummy = _libdummy["CreateDetector"]; - decltype(CreateDetector)* create_fasttrack = _libfasttrack["CreateDetector"]; - - _dettsan = create_tsan(); - _detdummy = create_dummy(); - _detfasttrack = create_fasttrack(); - - const char * _argv = "drace-tests.exe"; - _dettsan->init(1, &_argv, callback); - _detdummy->init(1, &_argv, callback); - _detfasttrack->init(1, &_argv, callback); } static void TearDownTestCase() { - _dettsan->finalize(); - _detdummy->finalize(); - _detfasttrack->finalize(); + for(auto & d : _detectors){ + d.second->finalize(); + } + _libs.clear(); } }; diff --git a/test/src/DetectorTest.cpp b/test/src/DetectorTest.cpp index 25ae2ba..17caeae 100644 --- a/test/src/DetectorTest.cpp +++ b/test/src/DetectorTest.cpp @@ -12,12 +12,8 @@ #include "gtest/gtest.h" #include "detectorTest.h" -util::WindowsLibLoader DetectorTest::_libtsan; -util::WindowsLibLoader DetectorTest::_libdummy; -util::WindowsLibLoader DetectorTest::_libfasttrack; -Detector * DetectorTest::_dettsan; -Detector * DetectorTest::_detdummy; -Detector * DetectorTest::_detfasttrack; +std::unordered_map> DetectorTest::_libs; +std::unordered_map DetectorTest::_detectors; TEST_P(DetectorTest, WR_Race) { Detector::tls_t tls10; @@ -386,6 +382,10 @@ TEST_F(DetectorTest, ShadowMemory) { #endif // Setup value-parameterized tests +#ifdef WIN32 INSTANTIATE_TEST_CASE_P(Interface, - DetectorTest, - ::testing::Values("fasttrack", "tsan")); + DetectorTest, ::testing::Values("fasttrack.standalone", "tsan")); +#else +INSTANTIATE_TEST_CASE_P(Interface, + DetectorTest, ::testing::Values("fasttrack.standalone")); +#endif