Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused global __Copyright #26

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clambcc/clambc-compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def createInputSourceFile(clangLLVM: ClangLLVM, name: str, args: list, options:

def optimize(clangLLVM: ClangLLVM, inFile: str, outFile: str, sigFile: str, inputSourceFile: str, standardCompiler: bool) -> int:

internalizeAPIList = "_Z10entrypointv,entrypoint,__clambc_kind,__clambc_virusname_prefix,__clambc_virusnames,__clambc_filesize,__clambc_match_counts,__clambc_match_offsets,__clambc_pedata,__Copyright"
internalizeAPIList = "_Z10entrypointv,entrypoint,__clambc_kind,__clambc_virusname_prefix,__clambc_virusnames,__clambc_filesize,__clambc_match_counts,__clambc_match_offsets,__clambc_pedata"
if standardCompiler:
internalizeAPIList += ",main"

Expand All @@ -509,6 +509,7 @@ def optimize(clangLLVM: ClangLLVM, inFile: str, outFile: str, sigFile: str, inpu
f' -clambc-preserve-abis' #add fake function calls that use all of
#the arguments so that O3 doesn't change
#the argument lists
f' -clambc-copyright-handler'
f' -O3'
f' -clambc-preserve-abis' #remove fake function calls because O3 has already run
f' -clambc-remove-pointer-phis'
Expand Down
1 change: 1 addition & 0 deletions libclambcc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target_sources(clambcc_obj
ClamBCLowering/ClamBCLowering.cpp
ClamBCVerifier/ClamBCVerifier.cpp
ClamBCLogicalCompiler/ClamBCLogicalCompiler.cpp
ClamBCCopyrightHandler/ClamBCCopyrightHandler.cpp
ClamBCRebuild/ClamBCRebuild.cpp
ClamBCTrace/ClamBCTrace.cpp
ClamBCModule/ClamBCModule.cpp
Expand Down
115 changes: 115 additions & 0 deletions libclambcc/ClamBCCopyrightHandler/ClamBCCopyrightHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Compile LLVM bytecode to logical signatures.
*
* Copyright (C) 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 "ClamBCModule.h"
#include <llvm/Support/DataTypes.h>
#include "../Common/bytecode_api.h"
#include "clambc.h"
#include "ClamBCDiagnostics.h"
#include "ClamBCModule.h"
#include "ClamBCCommon.h"
#include "ClamBCUtilities.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Analysis/ConstantFolding.h"
#include <llvm/IR/DebugInfo.h>
#include "llvm/Analysis/ValueTracking.h"
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/Module.h>
#include <llvm/Pass.h>
//#include <llvm/IR/PassManager.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/CallSite.h>
#include <llvm/IR/ConstantRange.h>
#include "llvm/Support/Debug.h"
#include <llvm/IR/InstIterator.h>
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/raw_ostream.h"
#include <llvm/Support/Process.h>
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/IPO.h"
#include <llvm/IR/Type.h>
//#include <llvm/IR/DataLayout.h>
#include <llvm/Transforms/Utils.h>
#include <llvm/LinkAllPasses.h>

#define DEBUG_TYPE "copyrighthandler"

using namespace llvm;

namespace
{

class ClamBCCopyrightHandler : public ModulePass
{
public:
static char ID;
ClamBCCopyrightHandler()
: ModulePass(ID) {}

virtual bool runOnModule(Module &M);

private:
llvm::Module *pMod;
};

char ClamBCCopyrightHandler::ID = 0;
RegisterPass<ClamBCCopyrightHandler> X("clambc-copyright-handler",
"ClamAV Copyright Handler");

bool ClamBCCopyrightHandler::runOnModule(Module &M)
{
pMod = &M;
bool bRet = false;

GlobalVariable *gCopyright = pMod->getGlobalVariable("__Copyright");
std::string copyright;
if (gCopyright && gCopyright->hasDefinitiveInitializer()) {
Constant *C = gCopyright->getInitializer();
StringRef c;
if (!getConstantStringInfo(C, c)) {
ClamBCStop("Failed to extract copyright string\n", pMod);
}
copyright = c.str();
}

if (copyright.length()) {
NamedMDNode *Node = M.getOrInsertNamedMetadata("clambc.copyright");
MDString *S = MDString::get(M.getContext(), llvm::StringRef(copyright));
MDNode *N = MDNode::get(M.getContext(), S);
Node->addOperand(N);
bRet = true;
}

return bRet;
}

} // namespace
const PassInfo *const ClamBCCopyrightHandlerID = &X;

llvm::ModulePass *createClamBCCopyrightHandler()
{
return new ClamBCCopyrightHandler();
}
14 changes: 10 additions & 4 deletions libclambcc/ClamBCWriter/ClamBCWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ class ClamBCOutputWriter
printNumber(Out, 0, false);
continue;
}

Constant *pConst = llvm::cast<Constant>(*I);
// type of constant
uint16_t id = pAnalyzer->getTypeID((*I)->getType());
Expand Down Expand Up @@ -540,9 +541,11 @@ class ClamBCOutputWriter
printModuleHeader(*pMod, pAnalyzer, maxLineLength + 1);
OutReal << Out.str();

//MemoryBuffer *MB = nullptr;
const char *start = NULL;
std::string copyright = pAnalyzer->getCopyright();
const char *start = NULL;

NamedMDNode *copyrightNode = pMod->getNamedMetadata("clambc.copyright");
std::string copyright = copyrightNode ? cast<MDString>(copyrightNode->getOperand(0)->getOperand(0))->getString() : "";

if (copyright.length()) {
start = copyright.c_str();
} else {
Expand Down Expand Up @@ -593,6 +596,10 @@ class ClamBCOutputWriter
linelength = 0;
}
} while (c);
//OutReal doesn't appear to add the last newline unless there is something
//after it, so there is no newline at the end of the source code printout
//without this.
OutReal << "\n";
}

void dumpTypes(llvm::raw_ostream &OS)
Expand Down Expand Up @@ -743,7 +750,6 @@ class ClamBCWriter : public ModulePass, public InstVisitor<ClamBCWriter>

bool runOnModule(Module &m)
{

pMod = &m;
pAnalyzer = &getAnalysis<ClamBCAnalyzer>();
pOutputWriter = ClamBCOutputWriter::createClamBCOutputWriter(outFile, pMod, pAnalyzer);
Expand Down