mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
866392472f
Fix various edge cases when a binary with no functions was analyzed
144 lines
5.2 KiB
C++
144 lines
5.2 KiB
C++
//
|
|
// This file is distributed under the MIT License. See LICENSE.md for details.
|
|
//
|
|
|
|
#include "revng/EarlyFunctionAnalysis/FunctionMetadata.h"
|
|
#include "revng/EarlyFunctionAnalysis/FunctionMetadataCache.h"
|
|
#include "revng/Model/Binary.h"
|
|
#include "revng/Pipeline/Location.h"
|
|
#include "revng/Pipeline/Pipe.h"
|
|
#include "revng/Pipeline/RegisterContainerFactory.h"
|
|
#include "revng/Pipeline/RegisterPipe.h"
|
|
#include "revng/Pipes/FunctionStringMap.h"
|
|
#include "revng/Pipes/Kinds.h"
|
|
#include "revng/Pipes/ModelGlobal.h"
|
|
#include "revng/Yield/CrossRelations.h"
|
|
#include "revng/Yield/Pipes/ProcessCallGraph.h"
|
|
#include "revng/Yield/Pipes/YieldCallGraph.h"
|
|
#include "revng/Yield/Pipes/YieldCallGraphSlice.h"
|
|
#include "revng/Yield/SVG.h"
|
|
|
|
namespace revng::pipes {
|
|
|
|
void ProcessCallGraph::run(pipeline::Context &Context,
|
|
const pipeline::LLVMContainer &TargetList,
|
|
CrossRelationsFileContainer &OutputFile) {
|
|
// Access the model
|
|
const auto &Model = getModelFromContext(Context);
|
|
|
|
// Access the llvm module
|
|
const llvm::Module &Module = TargetList.getModule();
|
|
|
|
// Gather function metadata
|
|
SortedVector<efa::FunctionMetadata> Metadata;
|
|
for (const auto &LLVMFunction : FunctionTags::Isolated.functions(&Module))
|
|
Metadata.insert(*::detail::extractFunctionMetadata(&LLVMFunction));
|
|
revng_assert(Metadata.size() == Model->Functions.size());
|
|
|
|
// Gather the relations
|
|
yield::CrossRelations Relations(Metadata, *Model);
|
|
|
|
// Serialize the output
|
|
std::error_code ErrorCode;
|
|
llvm::raw_fd_ostream Stream(OutputFile.getOrCreatePath(), ErrorCode);
|
|
if (ErrorCode)
|
|
revng_abort(ErrorCode.message().c_str());
|
|
|
|
llvm::yaml::Output YamlStream(Stream);
|
|
YamlStream << Relations;
|
|
|
|
Stream.flush();
|
|
if ((ErrorCode = Stream.error()))
|
|
revng_abort(ErrorCode.message().c_str());
|
|
}
|
|
|
|
void ProcessCallGraph::print(const pipeline::Context &,
|
|
llvm::raw_ostream &OS,
|
|
llvm::ArrayRef<std::string>) const {
|
|
OS << *revng::ResourceFinder.findFile("bin/revng") << " magic ^_^\n";
|
|
}
|
|
|
|
void YieldCallGraph::run(pipeline::Context &Context,
|
|
const CrossRelationsFileContainer &Input,
|
|
CallGraphSVGFileContainer &Output) {
|
|
// Access the model
|
|
const auto &Model = revng::getModelFromContext(Context);
|
|
|
|
// Open the input file.
|
|
auto MaybeInputPath = Input.path();
|
|
revng_assert(MaybeInputPath.has_value());
|
|
auto MaybeBuffer = llvm::MemoryBuffer::getFile(MaybeInputPath.value());
|
|
revng_assert(MaybeBuffer);
|
|
llvm::yaml::Input YAMLInput(**MaybeBuffer);
|
|
|
|
// Deserialize the graph data.
|
|
yield::CrossRelations Relations;
|
|
YAMLInput >> Relations;
|
|
|
|
// Convert the graph to SVG.
|
|
auto Result = yield::svg::callGraph(Relations, *Model);
|
|
|
|
// Print the result.
|
|
Output.setContent(std::move(Result));
|
|
}
|
|
|
|
void YieldCallGraph::print(const pipeline::Context &,
|
|
llvm::raw_ostream &OS,
|
|
llvm::ArrayRef<std::string>) const {
|
|
OS << *revng::ResourceFinder.findFile("bin/revng") << " magic ^_^\n";
|
|
}
|
|
|
|
void YieldCallGraphSlice::run(pipeline::Context &Context,
|
|
const pipeline::LLVMContainer &TargetList,
|
|
const CrossRelationsFileContainer &Input,
|
|
CallGraphSliceSVGStringMap &Output) {
|
|
// Access the model
|
|
const auto &Model = revng::getModelFromContext(Context);
|
|
|
|
// Open the input file.
|
|
auto MaybeInputPath = Input.path();
|
|
revng_assert(MaybeInputPath.has_value());
|
|
auto MaybeBuffer = llvm::MemoryBuffer::getFile(MaybeInputPath.value());
|
|
revng_assert(MaybeBuffer);
|
|
llvm::yaml::Input YAMLInput(**MaybeBuffer);
|
|
|
|
// Deserialize the graph data.
|
|
yield::CrossRelations Relations;
|
|
YAMLInput >> Relations;
|
|
|
|
// Access the llvm module
|
|
const llvm::Module &Module = TargetList.getModule();
|
|
FunctionMetadataCache Cache;
|
|
for (const auto &LLVMFunction : FunctionTags::Isolated.functions(&Module)) {
|
|
auto &Metadata = Cache.getFunctionMetadata(&LLVMFunction);
|
|
auto ModelFunctionIterator = Model->Functions.find(Metadata.Entry);
|
|
revng_assert(ModelFunctionIterator != Model->Functions.end());
|
|
|
|
// Slice the graph for the current function and convert it to SVG
|
|
Output.insert_or_assign(Metadata.Entry,
|
|
yield::svg::callGraphSlice(Metadata.Entry,
|
|
Relations,
|
|
*Model));
|
|
}
|
|
}
|
|
|
|
void YieldCallGraphSlice::print(const pipeline::Context &,
|
|
llvm::raw_ostream &OS,
|
|
llvm::ArrayRef<std::string>) const {
|
|
OS << *revng::ResourceFinder.findFile("bin/revng") << " magic ^_^\n";
|
|
}
|
|
using namespace pipeline;
|
|
|
|
static RegisterDefaultConstructibleContainer<CrossRelationsFileContainer> X1;
|
|
static RegisterDefaultConstructibleContainer<CallGraphSVGFileContainer> X2;
|
|
static RegisterFunctionStringMap<CallGraphSliceSVGStringMap> X3;
|
|
|
|
static pipeline::RegisterRole
|
|
Role("BinaryCrossRelations", kinds::BinaryCrossRelationsRole);
|
|
|
|
static pipeline::RegisterPipe<ProcessCallGraph> ProcessPipe;
|
|
static pipeline::RegisterPipe<YieldCallGraph> YieldPipe;
|
|
static pipeline::RegisterPipe<YieldCallGraphSlice> YieldSlicePipe;
|
|
|
|
} // end namespace revng::pipes
|