Skip to content

Commit

Permalink
blah
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa committed Jan 18, 2024
1 parent d1effd4 commit 3308180
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 64 deletions.
68 changes: 5 additions & 63 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,67 +1,9 @@
# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved.

#
# The hello object library
#
add_library(hello_obj OBJECT)
target_sources(hello_obj
PRIVATE
HelloWorld/HelloWorld.cpp
)

target_include_directories(hello_obj
PRIVATE
../libclambcc # HACK: For Common/clambc.h
${LLVM_INCLUDE_DIRS}
)
#'PassManager' is using the 'new' passmanager. This was added
#for the upgrade to llvm 16, although the 'new' pass manager
#has been around a while.

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

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

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

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

if(WIN32)
install(TARGETS hello DESTINATION .)

# Also install shared library (DLL) dependencies
install(CODE [[
file(GET_RUNTIME_DEPENDENCIES
LIBRARIES
$<TARGET_FILE:hello>
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 hello DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
add_subdirectory(LegacyPassManager)
add_subdirectory(PassManager)
67 changes: 67 additions & 0 deletions examples/LegacyPassManager/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved.

#
# The hello object library
#
add_library(hello_obj OBJECT)
target_sources(hello_obj
PRIVATE
HelloWorld/HelloWorld.cpp
)

target_include_directories(hello_obj
PRIVATE
../libclambcc # HACK: For Common/clambc.h
${LLVM_INCLUDE_DIRS}
)

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

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

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

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

if(WIN32)
install(TARGETS hello DESTINATION .)

# Also install shared library (DLL) dependencies
install(CODE [[
file(GET_RUNTIME_DEPENDENCIES
LIBRARIES
$<TARGET_FILE:hello>
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 hello DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"

#include "Common/clambc.h"
//#include "Common/clambc.h"
using namespace llvm;

namespace {
Expand Down
129 changes: 129 additions & 0 deletions examples/PassManager/AnalysisPlugin/AnalysisPlugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Compile LLVM bytecode to ClamAV bytecode.
*
* Copyright (C) 2020-2023 Sourcefire, Inc.
*
* Authors: Andy Ragusa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

//#include "Common/clambc.h"
//#include "Common/ClamBCUtilities.h"

#include <llvm/IR/Module.h>
#include <llvm/IR/Instructions.h>
#include <llvm/Support/raw_ostream.h>

#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
#include <llvm/Analysis/FunctionPropertiesAnalysis.h>

#include <vector>

using namespace llvm;
using namespace std;

/* Modeled after CallGraphAnalysis */

namespace
{

class AnalysisResult {
public:
AnalysisResult(){
llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << ">" << "<END>\n";
}

};

class ExampleAnalysis : public AnalysisInfoMixin<ExampleAnalysis >
{

public:

friend AnalysisInfoMixin<ExampleAnalysis > ;
static AnalysisKey Key;


ExampleAnalysis(){
}

typedef AnalysisResult Result;

AnalysisResult run(llvm::Module & F, llvm::ModuleAnalysisManager & fam){

llvm::errs() << "<" << "Analysis::" << __LINE__ << ">" << "<END>\n";
return AnalysisResult();

}

};

AnalysisKey ExampleAnalysis::Key;

struct ExamplePass : public PassInfoMixin<ExamplePass >
{
protected:
Module *pMod = nullptr;
bool bChanged = false;

public:

virtual ~ExamplePass() {}

PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM)
{
pMod = &m;
llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << ">" << "Transform Pass" << "<END>\n";

MAM.getResult<ExampleAnalysis>(m);

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

return PreservedAnalyses::all();
}
}; // end of struct ExamplePass

} // end of anonymous namespace

// This part is the new way of registering your pass
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo() {
return {
LLVM_PLUGIN_API_VERSION, "ExamplePass", "v0.1",
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>) {
if(Name == "example-pass-with-analysis"){
FPM.addPass(ExamplePass());
return true;
}
return false;
}
);

PB.registerAnalysisRegistrationCallback(
[](ModuleAnalysisManager &mam) {
mam.registerPass([] () { return ExampleAnalysis(); } );
}
);
}
};
}



72 changes: 72 additions & 0 deletions examples/PassManager/AnalysisPlugin/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 analysisplugin object library
#
add_library(analysisplugin_obj OBJECT)
target_sources(analysisplugin_obj
PRIVATE
AnalysisPlugin.cpp
)

target_include_directories(analysisplugin_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(analysisplugin_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}")

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

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

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

if(WIN32)
install(TARGETS analysisplugin DESTINATION .)

# Also install shared library (DLL) dependencies
install(CODE [[
file(GET_RUNTIME_DEPENDENCIES
LIBRARIES
$<TARGET_FILE:analysisplugin>
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 analysisplugin DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()



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


add_subdirectory(AnalysisPlugin)
Loading

0 comments on commit 3308180

Please sign in to comment.