Skip to content

Commit

Permalink
Updated ClamBCRemovePointerPHIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa committed Aug 29, 2023
1 parent c12ffcc commit 5b92782
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 31 deletions.
1 change: 1 addition & 0 deletions libclambcc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ add_subdirectory(ClamBCRemoveUndefs)
add_subdirectory(ClamBCPreserveABIs)
add_subdirectory(ClamBCAnalyzer)
add_subdirectory(Common)
add_subdirectory(ClamBCRemovePointerPHIs)

72 changes: 72 additions & 0 deletions libclambcc/ClamBCRemovePointerPHIs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved.

#
# The clambcremovepointerphis object library
#
add_library(clambcremovepointerphis_obj OBJECT)
target_sources(clambcremovepointerphis_obj
PRIVATE
ClamBCRemovePointerPHIs.cpp
)

target_include_directories(clambcremovepointerphis_obj
PRIVATE
${CMAKE_BINARY_DIR} # For clambc-version.h (generated file)
. # For Common/clambc.h
.. # For clambc.h #TODO: change all passes to use "Common" and then delete this line.
${LLVM_INCLUDE_DIRS}
)

set_target_properties(clambcremovepointerphis_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}")

#
# For testing
#
#target_compile_definitions(clambcremovepointerphis_obj -DLOG_BEFORE_AFTER=1)

#
# The clambcremovepointerphis shared library.
#
add_library( clambcremovepointerphis SHARED )
target_link_libraries( clambcremovepointerphis
PUBLIC
clambcremovepointerphis_obj )
set_target_properties( clambcremovepointerphis PROPERTIES
VERSION ${LIBCLAMBC_VERSION}
SOVERSION ${LIBCLAMBC_SOVERSION} )

target_link_directories(clambcremovepointerphis PRIVATE ${LLVM_LIBRARY_DIRS})
target_link_libraries(clambcremovepointerphis PUBLIC ${LLVM_LIBS})

if(WIN32)
install(TARGETS clambcremovepointerphis DESTINATION .)

# Also install shared library (DLL) dependencies
install(CODE [[
file(GET_RUNTIME_DEPENDENCIES
LIBRARIES
$<TARGET_FILE:clambcremovepointerphis>
RESOLVED_DEPENDENCIES_VAR _r_deps
UNRESOLVED_DEPENDENCIES_VAR _u_deps
DIRECTORIES
${LLVM_LIBRARY_DIRS}
)
foreach(_file ${_r_deps})
string(TOLOWER ${_file} _file_lower)
if(NOT ${_file_lower} MATCHES "c:[\\/]windows[\\/]system32.*")
file(INSTALL
DESTINATION "${CMAKE_INSTALL_PREFIX}"
TYPE SHARED_LIBRARY
FOLLOW_SYMLINK_CHAIN
FILES "${_file}"
)
endif()
endforeach()
#message("UNRESOLVED_DEPENDENCIES_VAR: ${_u_deps}")
]])
else()
install(TARGETS clambcremovepointerphis DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()



50 changes: 42 additions & 8 deletions libclambcc/ClamBCRemovePointerPHIs/ClamBCRemovePointerPHIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <llvm/IR/Instructions.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Analysis/ValueTracking.h>
#include <llvm/Passes/PassPlugin.h>
#include <llvm/Passes/PassBuilder.h>

#include <llvm/IR/Dominators.h>

Expand All @@ -20,7 +22,7 @@ using namespace llvm;

namespace
{
class ClambcRemovePointerPHIs : public FunctionPass
class ClamBCRemovePointerPHIs : public PassInfoMixin<ClamBCRemovePointerPHIs>
{
protected:
Function *pFunc = nullptr;
Expand Down Expand Up @@ -283,16 +285,22 @@ class ClambcRemovePointerPHIs : public FunctionPass
}

public:
static char ID;
ClambcRemovePointerPHIs()
: FunctionPass(ID) {}
ClamBCRemovePointerPHIs(){}

virtual ~ClamBCRemovePointerPHIs(){}

#if 0
bool runOnFunction(Function &F) override
#else
virtual PreservedAnalyses run(Function & F, FunctionAnalysisManager & MAM)
#endif
{

pFunc = &F;
bool ret = false;

llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << "<END>\n";

std::vector<PHINode *> phis = gatherPHIs();
for (size_t i = 0; i < phis.size(); i++) {
PHINode *pn = phis[i];
Expand All @@ -302,14 +310,40 @@ class ClambcRemovePointerPHIs : public FunctionPass
}
}

return ret;
if (ret){
return PreservedAnalyses::none();
}
return PreservedAnalyses::all();
}

}; // end of class ClambcRemovePointerPHIs
}; // end of class ClamBCRemovePointerPHIs

} // end of anonymous namespace

char ClambcRemovePointerPHIs::ID = 0;
static RegisterPass<ClambcRemovePointerPHIs> X("clambc-remove-pointer-phis", "Remove PHI Nodes with pointers",
#if 0
char ClamBCRemovePointerPHIs::ID = 0;
static RegisterPass<ClamBCRemovePointerPHIs> X("clambc-remove-pointer-phis", "Remove PHI Nodes with pointers",
false /* Only looks at CFG */,
false /* Analysis Pass */);
#else
// This part is the new way of registering your pass
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo() {
return {
LLVM_PLUGIN_API_VERSION, "ClamBCRemovePointerPHIs", "v0.1",
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, FunctionPassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>) {
if(Name == "clambc-remove-pointer-phis"){
FPM.addPass(ClamBCRemovePointerPHIs());
return true;
}
return false;
}
);
}
};
}

#endif
8 changes: 8 additions & 0 deletions libclambcc/Common/clambc.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,13 @@ enum bc_global {
#define DEBUGERR llvm::errs() << "<" << __FILE__ << "::" << __FUNCTION__ << "::" << __LINE__ << ">"
#endif //DEBUGERR

#ifndef DEBUG_WHERE
#define DEBUG_WHERE llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << "><END>\n"
#endif

#ifndef DEBUG_VALUE
#define DEBUG_VALUE(__value__) llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << ">" << *__value__ << "<END>\n";
#endif

#define BC_START_TID 69
#endif
48 changes: 25 additions & 23 deletions temp_delete_when_merge/run_opt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@



clang-16 -S \
-fno-discard-value-names \
--language=c \
-emit-llvm \
-Werror=unused-command-line-argument \
-Xclang \
-disable-O0-optnone \
-o test.ll \
../../testing/BC.Img.Exploit.CVE_2017_3124-6335443-1.c \
-I ../../../build/install/bin/../include \
-include bytecode.h \
-D__CLAMBC__
#clang-16 -S \
# -fno-discard-value-names \
# --language=c \
# -emit-llvm \
# -Werror=unused-command-line-argument \
# -Xclang \
# -disable-O0-optnone \
# -o test.ll \
# ../../testing/BC.Img.Exploit.CVE_2017_3124-6335443-1.c \
# -I ../../../build/install/bin/../include \
# -include bytecode.h \
## -D__CLAMBC__


clang-16 -S -emit-llvm -O0 -Xclang -disable-O0-optnone ../../testing/test.c
clang-16 -S -fno-discard-value-names -emit-llvm -O0 -Xclang -disable-O0-optnone ../temp_delete_when_merge/testing/test.c



Expand Down Expand Up @@ -66,23 +66,25 @@ clang-16 -S -emit-llvm -O0 -Xclang -disable-O0-optnone ../../testing/test.c

#There are warnings about not being able to load libclambccommon.so, but I
#can add print statements to functions in that library and have them print, so ???
#opt-16 -S \
# --load libclambcc/Common/libclambccommon.so \
# --load-pass-plugin libclambcc/ClamBCRemoveUndefs/libclambcremoveundefs.so \
# --load-pass-plugin libclambcc/ClamBCPreserveABIs/libclambcpreserveabis.so \
# --load-pass-plugin libclambcc/ClamBCAnalyzer/libclambcanalyzer.so \
# --load-pass-plugin libclambcc/ClamBCRemovePointerPHIs/libclambcremovepointerphis.so \
# --passes="-mem2reg"\
# --passes="clambc-remove-undefs,clambc-preserve-abis,default<O3>,clambc-preserve-abis" \
# test.ll -o test.t.ll

opt-16 -S \
--load libclambcc/Common/libclambccommon.so \
--load-pass-plugin libclambcc/ClamBCRemoveUndefs/libclambcremoveundefs.so \
--load-pass-plugin libclambcc/ClamBCPreserveABIs/libclambcpreserveabis.so \
--load-pass-plugin libclambcc/ClamBCAnalyzer/libclambcanalyzer.so \
--load-pass-plugin libclambcc/ClamBCRemovePointerPHIs/libclambcremovepointerphis.so \
--passes="-mem2reg"\
--passes="clambc-remove-undefs,clambc-preserve-abis,default<O3>,clambc-preserve-abis" \
--passes="clambc-remove-undefs,clambc-preserve-abis,default<O3>,clambc-preserve-abis,function(clambc-remove-pointer-phis)" \
test.ll -o test.t.ll

#opt-16 -S \
# --load libclambcc/Common/libclambccommon.so \
# --load-pass-plugin libclambcc/ClamBCRemoveUndefs/libclambcremoveundefs.so \
# --load-pass-plugin libclambcc/ClamBCPreserveABIs/libclambcpreserveabis.so \
# --load-pass-plugin libclambcc/ClamBCAnalyzer/libclambcanalyzer.so \
# --passes="-mem2reg"\
# --passes="clambc-remove-undefs,clambc-preserve-abis,default<O3>,clambc-preserve-abis,clambc-remove-pointer-phis" \
# test.ll -o test.t.ll
#


0 comments on commit 5b92782

Please sign in to comment.