From 156e5f19dfeadba5fd0dbef26f2b046dc2d03a70 Mon Sep 17 00:00:00 2001 From: caballa Date: Tue, 25 Oct 2022 12:36:13 -0600 Subject: [PATCH] fix(devirt): resolve only if type-compatible callee --- lib/seadsa/DsaCompleteCallGraph.cc | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/seadsa/DsaCompleteCallGraph.cc b/lib/seadsa/DsaCompleteCallGraph.cc index c390d2e7..7e7ec3c3 100644 --- a/lib/seadsa/DsaCompleteCallGraph.cc +++ b/lib/seadsa/DsaCompleteCallGraph.cc @@ -13,6 +13,7 @@ #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/CallPromotionUtils.h" #include "seadsa/AllocWrapInfo.hh" #include "seadsa/CallGraphUtils.hh" @@ -534,15 +535,18 @@ bool CompleteCallGraphAnalysis::runOnModule(Module &M) { for (const Value *v : alloc_sites) { if (const Function *fn = dyn_cast(v->stripPointerCastsAndAliases())) { - foundAtLeastOneCallee = true; - CallGraphNode *CGNCallee = (*m_complete_cg)[fn]; - assert(CGNCallee); - if (!hasEdge(CGNCaller, CGNCallee, CGNCB)) { - assert(cb); - CGNCaller->addCalledFunction(cb, CGNCallee); - m_callees[cs.getInstruction()].push_back(fn); - change = true; - } + // Check that the callbase and the callee are type-compatible + if (isLegalToPromote(*cb, const_cast(fn))) { + foundAtLeastOneCallee = true; + CallGraphNode *CGNCallee = (*m_complete_cg)[fn]; + assert(CGNCallee); + if (!hasEdge(CGNCaller, CGNCallee, CGNCB)) { + assert(cb); + CGNCaller->addCalledFunction(cb, CGNCallee); + m_callees[cs.getInstruction()].push_back(fn); + change = true; + } + } } }