diff --git a/libclambcc/CMakeLists.txt b/libclambcc/CMakeLists.txt index f2b2b92eaf..fc8c04ae00 100644 --- a/libclambcc/CMakeLists.txt +++ b/libclambcc/CMakeLists.txt @@ -5,4 +5,5 @@ add_subdirectory(ClamBCRemoveUndefs) add_subdirectory(ClamBCPreserveABIs) add_subdirectory(ClamBCAnalyzer) add_subdirectory(Common) +add_subdirectory(ClamBCRemovePointerPHIs) diff --git a/libclambcc/ClamBCRemovePointerPHIs/CMakeLists.txt b/libclambcc/ClamBCRemovePointerPHIs/CMakeLists.txt new file mode 100644 index 0000000000..60bbdc45e5 --- /dev/null +++ b/libclambcc/ClamBCRemovePointerPHIs/CMakeLists.txt @@ -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 + $ + 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() + + + diff --git a/libclambcc/ClamBCRemovePointerPHIs/ClamBCRemovePointerPHIs.cpp b/libclambcc/ClamBCRemovePointerPHIs/ClamBCRemovePointerPHIs.cpp index 4c8409641c..10c161bccd 100644 --- a/libclambcc/ClamBCRemovePointerPHIs/ClamBCRemovePointerPHIs.cpp +++ b/libclambcc/ClamBCRemovePointerPHIs/ClamBCRemovePointerPHIs.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include @@ -18,9 +20,9 @@ using namespace llvm; #include -namespace +namespace ClamBCRemovePointerPHIs { -class ClambcRemovePointerPHIs : public FunctionPass +class ClamBCRemovePointerPHIs : public PassInfoMixin { protected: Function *pFunc = nullptr; @@ -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() << "<" << __FILE__ << "::" << __FUNCTION__ << "::" << __LINE__ << "\n"; + std::vector phis = gatherPHIs(); for (size_t i = 0; i < phis.size(); i++) { PHINode *pn = phis[i]; @@ -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 +} // end of ClamBCRemovePointerPHIs namespace -char ClambcRemovePointerPHIs::ID = 0; -static RegisterPass X("clambc-remove-pointer-phis", "Remove PHI Nodes with pointers", +#if 0 +char ClamBCRemovePointerPHIs::ID = 0; +static RegisterPass 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) { + if(Name == "clambc-remove-pointer-phis"){ + FPM.addPass(ClamBCRemovePointerPHIs::ClamBCRemovePointerPHIs()); + return true; + } + return false; + } + ); + } + }; +} + +#endif diff --git a/libclambcc/Common/clambc.h b/libclambcc/Common/clambc.h index 3d790a1f9e..bda3761d9c 100644 --- a/libclambcc/Common/clambc.h +++ b/libclambcc/Common/clambc.h @@ -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__ << ">\n" +#endif + +#ifndef DEBUG_VALUE +#define DEBUG_VALUE(__value__) llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << ">" << *__value__ << "\n"; +#endif + #define BC_START_TID 69 #endif diff --git a/temp_delete_when_merge/run_opt.sh b/temp_delete_when_merge/run_opt.sh index 2c38258716..c6aa8154cd 100755 --- a/temp_delete_when_merge/run_opt.sh +++ b/temp_delete_when_merge/run_opt.sh @@ -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 @@ -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,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,clambc-preserve-abis" \ + --passes="clambc-remove-undefs,clambc-preserve-abis,default,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,clambc-preserve-abis,clambc-remove-pointer-phis" \ -# test.ll -o test.t.ll -#