Skip to content

Commit

Permalink
test: add support to run detector if tests on unix
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoessbauer committed Jan 8, 2020
1 parent 9b5179d commit ddab111
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 53 deletions.
9 changes: 9 additions & 0 deletions common/util/LibLoaderFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@ namespace util
return std::make_shared<UnixLibLoader>();
#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
6 changes: 1 addition & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
66 changes: 26 additions & 40 deletions test/include/detectorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@
*/

#include <detector/Detector.h>
#include <util/WindowsLibLoader.h>
#include <util/LibLoaderFactory.h>
#include <string>
#include <iostream>
#include <Windows.h>
#include <unordered_map>

static unsigned num_races = 0;
static Detector::Race last_race;

class DetectorTest : public ::testing::TestWithParam<const char*>{

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<std::string, std::shared_ptr<util::LibraryLoader>> _libs;
static std::unordered_map<std::string, Detector*> _detectors;

public:
Detector * detector;
Expand All @@ -44,46 +40,36 @@ class DetectorTest : public ::testing::TestWithParam<const char*>{
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();
}
};
16 changes: 8 additions & 8 deletions test/src/DetectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::shared_ptr<util::LibraryLoader>> DetectorTest::_libs;
std::unordered_map<std::string, Detector*> DetectorTest::_detectors;

TEST_P(DetectorTest, WR_Race) {
Detector::tls_t tls10;
Expand Down Expand Up @@ -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

0 comments on commit ddab111

Please sign in to comment.