Skip to content

Commit

Permalink
[dynamorio] Add initial support for DynamoRIO on x86
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorcarlson committed Oct 25, 2022
1 parent e2009ce commit 010fce6
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 77 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -61,6 +61,8 @@ riscv/Speckle
xed_kit
mbuild
sde_kit
capstone
dynamorio

# Pin3 output
frontend/pin-frontend/obj-intel64
Expand Down
55 changes: 52 additions & 3 deletions Makefile
Expand Up @@ -4,12 +4,13 @@ CLEAN=$(findstring clean,$(MAKECMDGOALS))

STANDALONE=$(SIM_ROOT)/lib/sniper
PIN_FRONTEND=$(SIM_ROOT)/frontend/pin-frontend/obj-intel64/pin_frontend
DYNAMORIO_FRONTEND=$(SIM_ROOT)/frontend/dr-frontend/build/libdr-frontend.so
LIB_CARBON=$(SIM_ROOT)/lib/libcarbon_sim.a
LIB_PIN_SIM=$(SIM_ROOT)/pin/../lib/pin_sim.so
LIB_FOLLOW=$(SIM_ROOT)/pin/../lib/follow_execv.so
LIB_SIFT=$(SIM_ROOT)/sift/libsift.a
LIB_DECODER=$(SIM_ROOT)/decoder_lib/libdecoder.a
SIM_TARGETS=$(LIB_DECODER) $(LIB_CARBON) $(LIB_SIFT) $(LIB_PIN_SIM) $(LIB_FOLLOW) $(STANDALONE) $(PIN_FRONTEND)
SIM_TARGETS=$(LIB_DECODER) $(LIB_CARBON) $(LIB_SIFT) $(LIB_PIN_SIM) $(LIB_FOLLOW) $(STANDALONE) $(PIN_FRONTEND) $(DYNAMORIO_FRONTEND)

PYTHON2=python2

Expand All @@ -22,11 +23,24 @@ all: message dependencies $(SIM_TARGETS) configscripts
include common/Makefile.common

dependencies: package_deps sde_kit $(PIN_ROOT) pin xed python mcpat linux builddir showdebugstatus

BUILD_CAPSTONE ?=
ifeq ($(BUILD_ARM),1)
BUILD_CAPSTONE=1
else ifeq ($(BUILD_DYNAMORIO),1)
BUILD_CAPSTONE=1
endif

ifeq ($(BUILD_CAPSTONE),1)
dependencies: capstone
.PHONY: capstone
endif

ifeq ($(BUILD_DYNAMORIO),1)
dependencies: dynamorio
.PHONY: dynamorio
endif

$(SIM_TARGETS): dependencies

message:
Expand All @@ -36,6 +50,9 @@ ifneq ($(USE_PINPLAY), 1)
else
@echo -n " with Pinplay"
endif
ifeq ($(BUILD_DYNAMORIO),1)
@echo -n " and DynamoRIO"
endif
ifeq ($(BUILD_RISCV),1)
@echo -n " and RISCV"
endif
Expand All @@ -50,6 +67,15 @@ $(STANDALONE): $(LIB_CARBON) $(LIB_SIFT) $(LIB_DECODER)
$(PIN_FRONTEND):
@$(MAKE) $(MAKE_QUIET) -C $(SIM_ROOT)/frontend/pin-frontend

ifeq ($(BUILD_DYNAMORIO),1)
$(DYNAMORIO_FRONTEND): $(LIB_SIFT)
@if [ ! -e "$(SIM_ROOT)/frontend/dr-frontend/build/Makefile" ]; then mkdir -p $(SIM_ROOT)/frontend/dr-frontend/build && cd $(SIM_ROOT)/frontend/dr-frontend/build && cmake -DDEBUG=ON -DDynamoRIO_DIR=$(DYNAMORIO_INSTALL)/build/cmake .. ; fi
@$(MAKE) $(MAKE_QUIET) -C $(SIM_ROOT)/frontend/dr-frontend/build
else
$(DYNAMORIO_FRONTEND):
$(_CMD) true
endif

# Disable original frontend

#$(LIB_PIN_SIM): $(LIB_CARBON) $(LIB_SIFT) $(LIB_DECODER)
Expand All @@ -67,13 +93,30 @@ $(LIB_SIFT): $(LIB_CARBON)
$(LIB_DECODER): $(LIB_CARBON)
@$(MAKE) $(MAKE_QUIET) -C $(SIM_ROOT)/decoder_lib

DYNAMORIO_GITID=246ddb28e7848b2d09d2b9909f99a6da9b2ce35e
DYNAMORIO_INSTALL=$(SIM_ROOT)/dynamorio
DYNAMORIO_INSTALL_DEP=$(DYNAMORIO_INSTALL)/CMakeLists.txt
$(DYNAMORIO_INSTALL_DEP):
$(_MSG) '[DOWNLO] dynamorio'
$(_CMD) git clone --quiet --recursive https://github.com/DynamoRIO/dynamorio.git $(DYNAMORIO_INSTALL)
$(_CMD) git -C $(DYNAMORIO_INSTALL) reset --quiet --hard $(DYNAMORIO_GITID)
$(_CMD) touch $(DYNAMORIO_INSTALL)/.autodownloaded

DYNAMORIO_BUILD_DEP=$(DYNAMORIO_INSTALL)/build/bin64/drrun
dynamorio: $(DYNAMORIO_BUILD_DEP)
$(DYNAMORIO_BUILD_DEP): $(DYNAMORIO_INSTALL_DEP)
$(_MSG) '[INSTAL] dynamorio'
$(_CMD) cd dynamorio && mkdir build && cd build && cmake -DDEBUG=ON ..
$(_CMD) $(MAKE) $(MAKE_QUIET) -C dynamorio/build

CAPSTONE_GITID=f9c6a90489be7b3637ff1c7298e45efafe7cf1b9
CAPSTONE_INSTALL=$(SIM_ROOT)/capstone
CAPSTONE_INSTALL_DEP=$(CAPSTONE_INSTALL)/arch/AArch64/ARMMappingInsnOp.inc
$(CAPSTONE_INSTALL_DEP):
$(_MSG) '[DOWNLO] capstone'
$(_CMD) git clone --quiet https://github.com/aquynh/capstone.git $(CAPSTONE_INSTALL)
$(_CMD) git -C $(CAPSTONE_INSTALL) reset --quiet --hard $(CAPSTONE_GITID)
$(_CMD) touch $(CAPSTONE_INSTALL)/.autodownloaded

CAPSTONE_BUILD_DEP=$(CAPSTONE_INSTALL)/libcapstone.so.4
capstone: $(CAPSTONE_BUILD_DEP)
Expand Down Expand Up @@ -174,9 +217,9 @@ configscripts: dependencies
@./tools/makerelativepath.py sde_home "$(SIM_ROOT)" "$(SDE_HOME)" >> config/sniper.py
@./tools/makerelativepath.py pin_home "$(SIM_ROOT)" "$(PIN_HOME)" >> config/sniper.py
@./tools/makerelativepath.py xed_home "$(SIM_ROOT)" "$(XED_HOME)" >> config/sniper.py
@./tools/makerelativepath.py dynamorio_home "$(SIM_ROOT)" "$(DR_HOME)" >> config/sniper.py
@./tools/makerelativepath.py dynamorio_home "$(SIM_ROOT)" "$(DYNAMORIO_INSTALL)/build" >> config/sniper.py
@if [ $$(which git) ]; then if [ -e "$(SIM_ROOT)/.git" ]; then echo "git_revision=\"$$(git --git-dir='$(SIM_ROOT)/.git' rev-parse HEAD)\"" >> config/sniper.py; fi ; fi
@./tools/makebuildscripts.py "$(SIM_ROOT)" "$(SDE_HOME)" "$(PIN_HOME)" "$(DR_HOME)" "$(CC)" "$(CXX)" "$(SNIPER_TARGET_ARCH)"
@./tools/makebuildscripts.py "$(SIM_ROOT)" "$(SDE_HOME)" "$(PIN_HOME)" "$(DYNAMORIO_INSTALL)/build" "$(CC)" "$(CXX)" "$(SNIPER_TARGET_ARCH)"

empty_config:
$(_MSG) '[CLEAN ] config'
Expand All @@ -195,13 +238,19 @@ clean: empty_config empty_deps
$(_CMD) $(MAKE) $(MAKE_QUIET) -C tools clean
$(_MSG) '[CLEAN ] frontend/pin-frontend'
$(_CMD) if [ -d "$(PIN_HOME)" ]; then $(MAKE) $(MAKE_QUIET) -C frontend/pin-frontend clean ; fi
$(_MSG) '[CLEAN ] frontend/dr-frontend'
$(_CMD) if [ -d "$(SIM_ROOT)/frontend/dr-frontend/build" ]; then rm -rf $(SIM_ROOT)/frontend/dr-frontend/build ; fi
$(_CMD) rm -f .build_os

distclean: clean
$(_MSG) '[DISTCL] Pin kit'
$(_CMD) if [ -e "$(PIN_HOME)/.autodownloaded" ]; then rm -rf $(PIN_HOME); fi
$(_MSG) '[DISTCL] SDE kit'
$(_CMD) if [ -e "$(SDE_HOME)/.autodownloaded" ]; then rm -rf $(SDE_HOME); fi
$(_MSG) '[DISTCL] Capstone'
$(_CMD) if [ -e "$(CAPSTONE_INSTALL)/.autodownloaded" ]; then rm -rf $(CAPSTONE_INSTALL); fi
$(_MSG) '[DISTCL] DynamoRIO'
$(_CMD) if [ -e "$(DYNAMORIO_INSTALL)/.autodownloaded" ]; then rm -rf $(DYNAMORIO_INSTALL); fi
$(_MSG) '[DISTCL] python_kit'
$(_CMD) rm -rf python_kit
$(_MSG) '[DISTCL] McPAT'
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile-ubuntu-16.04
Expand Up @@ -14,6 +14,7 @@ RUN apt-get update && apt-get install -y \
RUN apt-get update && apt-get install -y \
automake \
build-essential \
cmake \
curl \
wget \
libboost-dev \
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile-ubuntu-18.04
Expand Up @@ -14,6 +14,7 @@ RUN apt-get update && apt-get install -y \
RUN apt-get update && apt-get install -y \
automake \
build-essential \
cmake \
curl \
wget \
libboost-dev \
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile-ubuntu-20.04
Expand Up @@ -18,6 +18,7 @@ RUN apt-get update && apt-get install -y \
RUN apt-get update && apt-get install -y \
automake \
build-essential \
cmake \
curl \
wget \
libboost-dev \
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile-ubuntu-22.04
Expand Up @@ -19,6 +19,7 @@ RUN apt-get update && apt-get install -y \
RUN apt-get update && apt-get install -y \
automake \
build-essential \
cmake \
curl \
wget \
libboost-dev \
Expand Down
54 changes: 15 additions & 39 deletions frontend/dr-frontend/CMakeLists.txt
Expand Up @@ -29,42 +29,30 @@
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.

cmake_minimum_required(VERSION 2.6)

#include(CheckCXXCompilerFlag)
#CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
#CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
#if(COMPILER_SUPPORTS_CXX11)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
#elseif(COMPILER_SUPPORTS_CXX0X)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
#else()
# message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
#endif()

message("SNIPER_HOME:" $ENV{SNIPER_HOME})
if(NOT DEFINED ENV{SNIPER_HOME})
message(FATAL_ERROR "Please define SNIPER_HOME environment variable")
endif()
cmake_minimum_required(VERSION 2.6...2.8.12)

project(dr-frontend)

get_filename_component(SNIPER_FE_HOME ${CMAKE_CURRENT_LIST_DIR} PATH)
get_filename_component(SNIPER_HOME ${SNIPER_FE_HOME} PATH)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c\\+\\+11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g")

set(SNIPER_FRONTEND $ENV{SNIPER_HOME}/frontend)
set(SNIPER_SIFT $ENV{SNIPER_HOME}/dynamorio/clients/dr-frontend/sift)
set(SNIPER_FRONTEND ${SNIPER_HOME}/frontend)
set(SNIPER_SIFT ${SNIPER_HOME}/sift)

include_directories(${SNIPER_FRONTEND} ${SNIPER_SIFT})

set(HEADER_FILES ${SNIPER_FRONTEND}/frontend.h ${SNIPER_FRONTEND}/frontend_threads.h ${SNIPER_FRONTEND}/frontend_defs.h ${SNIPER_FRONTEND}/bbv_count.h ${SNIPER_FRONTEND}/frontend_callbacks.h ${SNIPER_FRONTEND}/frontend_control.h ${SNIPER_FRONTEND}/frontend_options.h ${SNIPER_FRONTEND}/frontend_syscall.h ${SNIPER_FRONTEND}/frontend_utils.h)

#todo: include options and defs headers

#find_package(DynamoRIO)
#if (NOT DynamoRIO_FOUND)
# message(FATAL_ERROR "DynamoRIO package required to build")
#endif(NOT DynamoRIO_FOUND)
#
find_package(DynamoRIO)
if (NOT DynamoRIO_FOUND)
message(FATAL_ERROR "DynamoRIO package required to build")
endif(NOT DynamoRIO_FOUND)

add_library(dr-frontend SHARED
dr_frontend.cc
${SNIPER_FRONTEND}/bbv_count.cc
Expand All @@ -73,21 +61,14 @@ add_library(dr-frontend SHARED
# sniper's libraries and others
find_library(libz z)
target_link_libraries(dr-frontend
$ENV{SNIPER_HOME}/dynamorio/clients/dr-frontend/sift/libsift.a
${SNIPER_HOME}/sift/libsift.a
${libz})

configure_DynamoRIO_client(dr-frontend)
use_DynamoRIO_extension(dr-frontend drmgr)
use_DynamoRIO_extension(dr-frontend droption)
use_DynamoRIO_extension(dr-frontend drreg)
use_DynamoRIO_extension(dr-frontend drutil)
#use_DynamoRIO_extension(drltrace drwrap)
#use_DynamoRIO_extension(drltrace drx)
# We keep our shared libs in the lib dir, not the bin dir:
place_shared_lib_in_lib_dir(dr-frontend)

# ensure we rebuild if includes change
add_dependencies(dr-frontend api_headers)

# Provide a hint for how to use the client
if (NOT DynamoRIO_INTERNAL OR NOT "${CMAKE_GENERATOR}" MATCHES "Ninja")
Expand All @@ -98,8 +79,6 @@ if (NOT DynamoRIO_INTERNAL OR NOT "${CMAKE_GENERATOR}" MATCHES "Ninja")
VERBATIM)
endif ()

install_target(dr-frontend ${INSTALL_CLIENTS_LIB})

set(INSTALL_DRLTRACE_CONFIG ${INSTALL_CLIENTS_BASE})

if (X64)
Expand All @@ -115,6 +94,3 @@ file(APPEND ${CONFIG} "# client tool path\n")
file(APPEND ${CONFIG} "CLIENT_REL=${INSTALL_CLIENTS_LIB}/${LIB_PFX}dr-frontend${LIB_EXT}\n")
file(APPEND ${CONFIG} "# client tool options\n")
file(APPEND ${CONFIG} "TOOL_OP=\n")

DR_install(FILES "${CONFIG}" DESTINATION ${INSTALL_DRLTRACE_CONFIG})
register_tool_file("dr-frontend")
6 changes: 3 additions & 3 deletions frontend/dr-frontend/dr_fe_control.tcc
Expand Up @@ -3,14 +3,14 @@ namespace frontend

template <> void FrontendControl<DRFrontend>::free_thread_data(size_t thread_data_size)
{
std::cerr << "Going to free" << std::endl;
//std::cerr << "Going to free" << std::endl;
dr_custom_free(NULL, (dr_alloc_flags_t) 0, m_thread_data, thread_data_size);
std::cerr << "Thread data freed" << std::endl;
//std::cerr << "Thread data freed" << std::endl;
}

template <> void FrontendControl<DRFrontend>::getCode(uint8_t* dst, const uint8_t* src, uint32_t size)
{
FECopy<DRFrontend>::__DR_safeCopy(dst, src, size);
}

} // end namespace frontend
} // end namespace frontend
12 changes: 6 additions & 6 deletions frontend/dr-frontend/dr_fe_lock.tcc
Expand Up @@ -4,29 +4,29 @@ namespace frontend
inline FELock<DRFrontend>::FELock()
{
this->the_lock = dr_mutex_create();
std::cerr << "Lock created" << std::endl;
//std::cerr << "Lock created" << std::endl;
}

inline FELock<DRFrontend>::~FELock()
{
std::cerr << "Going to destroy Lock" << std::endl;
//std::cerr << "Going to destroy Lock" << std::endl;
dr_mutex_destroy(this->the_lock);
std::cerr << "Lock destroyed" << std::endl;
//std::cerr << "Lock destroyed" << std::endl;

}

inline void FELock<DRFrontend>::acquire_lock(threadid_t tid)
{
dr_mutex_lock(this->the_lock);
std::cerr << "Lock acquired" << std::endl;
//std::cerr << "Lock acquired" << std::endl;

}

inline void FELock<DRFrontend>::release_lock()
{
dr_mutex_unlock(this->the_lock);
std::cerr << "Lock released" << std::endl;
//std::cerr << "Lock released" << std::endl;

}

} // end namespace frontend
} // end namespace frontend

0 comments on commit 010fce6

Please sign in to comment.