Skip to content

Commit

Permalink
Add code for running Sanitizers
Browse files Browse the repository at this point in the history
  • Loading branch information
KIwabuchi committed Nov 28, 2023
1 parent 8a210d4 commit dec2272
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
2 changes: 2 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ add_metall_executable(allocator_aware_type allocator_aware_type.cpp)

add_metall_executable(logger logger.cpp)

add_metall_executable(concurrent concurrent.cpp)

if (BUILD_C)
add_c_executable(c_api c_api.c)
target_link_libraries(c_api PRIVATE metall_c)
Expand Down
47 changes: 47 additions & 0 deletions example/concurrent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 Lawrence Livermore National Security, LLC and other Metall
// Project Developers. See the top-level COPYRIGHT file for details.
//
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

/// \file concurrent.cpp
/// \brief This example demonstrates Metall's concurrency support.
/// Metall can be used in a multi-threaded environment.
/// Please see the API documentation of the manager class to find out which
/// functions are thread-safe.

#include <iostream>
#include <thread>

#include <metall/metall.hpp>

void metall_alloc(metall::manager& manager, const int tid) {
for (int i = 0; i < 10; ++i) {
if (tid % 2 == 0) {
manager.deallocate(manager.allocate(10));
} else {
manager.destroy_ptr(manager.construct<int>(metall::anonymous_instance)());
}
}
}

int main() {
{
metall::manager manager(metall::create_only, "/tmp/datastore");

{
std::thread t1(metall_alloc, std::ref(manager), 1);
std::thread t2(metall_alloc, std::ref(manager), 2);
std::thread t3(metall_alloc, std::ref(manager), 3);
std::thread t4(metall_alloc, std::ref(manager), 4);

t1.join();
t2.join();
t3.join();
t4.join();
}
assert(manager.check_sanity());
assert(manager.all_memory_deallocated());
}

return 0;
}
6 changes: 4 additions & 2 deletions example/concurrent_map.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2023 Lawrence Livermore National Security, LLC and other Metall
// Project Developers. See the top-level COPYRIGHT file for details.
//
// Created by Iwabuchi, Keita on 9/25/20.
//
// SPDX-License-Identifier: (Apache-2.0 OR MIT)


#include <iostream>
#include <thread>
Expand Down
69 changes: 69 additions & 0 deletions scripts/sanitizer/run_sanitizers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

# This script builds and runs example programs with various sanitizers.
# Usage example:
# cd metall # Metall root directory
# sh scripts/sanitizer/run_sanitizers.sh

# Change the following variables to your own settings
CC=clang
CXX=clang++
BOOST_ROOT="${HOME}/local/boost"
METALL_ROOT=".."

LOG_FILE=out-sanitizer.log

exec_cmd () {
echo "" >> ${LOG_FILE}
echo "------------------------------------------------------------" >> ${LOG_FILE}
echo "Command: " "$@" | tee -a ${LOG_FILE}
"$@" >> ${LOG_FILE} 2>&1
echo "------------------------------------------------------------" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
}

build () {
local target=$1
exec_cmd ${CXX} -I ${BOOST_ROOT} -I ${METALL_ROOT}/include -std=c++17 ${SANITIZER_BUILD_ARGS} ${target} -DMETALL_DEFAULT_CAPACITY=$((1024*1024*500))
}

run_sanitizer () {
SANITIZER_BUILD_ARGS=$1

# build "${METALL_ROOT}/example/simple.cpp"
# ./a.out 2>&1 | tee -a ${LOG_FILE}
# rm -f a.out

build "${METALL_ROOT}/example/concurrent.cpp"
echo "Running concurrent program"
./a.out 2>&1 | tee -a ${LOG_FILE}
rm -f a.out
}

main() {
mkdir build
cd build
rm -rf ${LOG_FILE}

echo ""
echo "====================================================================="
echo "Build with address sanitizer and leak sanitizer"
run_sanitizer "-fsanitize=address -fno-omit-frame-pointer -O1 -g"

echo ""
echo "====================================================================="
echo "Build with thread sanitizer"
run_sanitizer "-fsanitize=thread -fPIE -pie -O1 -g"

echo ""
echo "====================================================================="
echo "Build with memory sanitizer"
run_sanitizer "-fsanitize=memory -fsanitize-memory-track-origins -fPIE -pie -fno-omit-frame-pointer -g -O2"

echo ""
echo "====================================================================="
echo "Build with undefined behavior sanitizer"
run_sanitizer "-fsanitize=undefined -O1 -g"
}

main "$@"

0 comments on commit dec2272

Please sign in to comment.