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

[BOLT] Keep function which is directly called or has prologue #88244

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

yubingex007-a11y
Copy link
Contributor

No description provided.

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff da5a86b53e7d6e7ff7407b16c2c869894493ee99 a846f4f3f4089c516a41db3b64d4cf750bd505ff -- bolt/include/bolt/Core/BinaryFunction.h bolt/include/bolt/Rewrite/RewriteInstance.h bolt/lib/Core/BinaryFunction.cpp bolt/lib/Rewrite/RewriteInstance.cpp
View the diff from clang-format here.
diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index cd6ded17e4..bd1460580e 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -208,7 +208,7 @@ public:
   /// Mark injected functions
   bool IsInjected = false;
 
-  bool IsDirectCalled =false;
+  bool IsDirectCalled = false;
 
   using LSDATypeTableTy = SmallVector<uint64_t, 0>;
 
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index 2f7e6a8d36..0e7a19da93 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -1268,11 +1268,14 @@ Error BinaryFunction::disassemble() {
         bool IsCall = MIB->isCall(Instruction);
         const bool IsCondBranch = MIB->isConditionalBranch(Instruction);
         MCSymbol *TargetSymbol = nullptr;
-        if(IsCall) {
-          if (BinaryFunction *TargetFunc = BC.getBinaryFunctionContainingAddress(TargetAddress)) {
+        if (IsCall) {
+          if (BinaryFunction *TargetFunc =
+                  BC.getBinaryFunctionContainingAddress(TargetAddress)) {
             // direct call here.
             TargetFunc->IsDirectCalled = true;
-            LLVM_DEBUG(dbgs() << "[Disasm] Find function which is directly called: 0x" << Twine::utohexstr(TargetFunc->getAddress()) << "\n");
+            LLVM_DEBUG(dbgs()
+                       << "[Disasm] Find function which is directly called: 0x"
+                       << Twine::utohexstr(TargetFunc->getAddress()) << "\n");
           }
         }
         if (BC.MIB->isUnsupportedBranch(Instruction)) {
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index a0768ba870..7168804f8b 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -811,52 +811,65 @@ Error RewriteInstance::run() {
 }
 
 void RewriteInstance::keepDCPFunction() {
- BC->outs() << "keepDCPFunction!"<<"\n";
-  for(auto &BFI : BC->getBinaryFunctions()) {
+  BC->outs() << "keepDCPFunction!" << "\n";
+  for (auto &BFI : BC->getBinaryFunctions()) {
     BinaryFunction &BF = BFI.second;
-    bool hasPrologue =false;
+    bool hasPrologue = false;
     ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = BF.getData();
     assert(ErrorOrFunctionData && "function data is not available");
     ArrayRef<uint8_t> IData = *ErrorOrFunctionData;
-    // BC->outs()  << "Potential Function Entry Point: 0x" << Twine::utohexstr(IData[0]) << "\n";
-    // Check for common function prologue patterns
-    // push %rbp
-    // mov %rsp %rbp
-    if (BF.getSize()>=4 && (char)IData[0] == '\x55' && (char)IData[1] == '\x48' && (char)IData[2] == '\x89' && (char)IData[3] == '\xe5') {
-        LLVM_DEBUG(dbgs() << "[keepDCPFunction] Find function with prologue in: 0x" << Twine::utohexstr(BF.getAddress()) << "\n");
-        hasPrologue =true;
-    } else if (BF.getSize()>=4 && (char)IData[0] == '\xf3' && (char)IData[1] == '\x0f' && (char)IData[2] == '\x1e' && (char)IData[3] == '\xfa') {
-       LLVM_DEBUG(dbgs() <<  "[keepDCPFunction] Find function with prologue in: 0x" << Twine::utohexstr(BF.getAddress()) << "\n");
-        hasPrologue= true;
+    // BC->outs()  << "Potential Function Entry Point: 0x" <<
+    // Twine::utohexstr(IData[0]) << "\n"; Check for common function prologue
+    // patterns push %rbp mov %rsp %rbp
+    if (BF.getSize() >= 4 && (char)IData[0] == '\x55' &&
+        (char)IData[1] == '\x48' && (char)IData[2] == '\x89' &&
+        (char)IData[3] == '\xe5') {
+      LLVM_DEBUG(
+          dbgs() << "[keepDCPFunction] Find function with prologue in: 0x"
+                 << Twine::utohexstr(BF.getAddress()) << "\n");
+      hasPrologue = true;
+    } else if (BF.getSize() >= 4 && (char)IData[0] == '\xf3' &&
+               (char)IData[1] == '\x0f' && (char)IData[2] == '\x1e' &&
+               (char)IData[3] == '\xfa') {
+      LLVM_DEBUG(
+          dbgs() << "[keepDCPFunction] Find function with prologue in: 0x"
+                 << Twine::utohexstr(BF.getAddress()) << "\n");
+      hasPrologue = true;
     } else {
       hasPrologue = false;
     }
-   LLVM_DEBUG(
-    if(BF.IsDirectCalled) dbgs() << "[keepDCPFunction] Find function which is directly called: 0x" << Twine::utohexstr(BF.getAddress()) << "\n"
-    );
-    if(!BF.IsDirectCalled && !hasPrologue )
+    LLVM_DEBUG(if (BF.IsDirectCalled) dbgs()
+               << "[keepDCPFunction] Find function which is directly called: 0x"
+               << Twine::utohexstr(BF.getAddress()) << "\n");
+    if (!BF.IsDirectCalled && !hasPrologue)
       BF.setIgnored();
   }
 }
 void RewriteInstance::keepPrologueFunction() {
- BC->outs() << "keepPrologueFunction!"<<"\n";
-  for(auto &BFI : BC->getBinaryFunctions()) {
+  BC->outs() << "keepPrologueFunction!" << "\n";
+  for (auto &BFI : BC->getBinaryFunctions()) {
     BinaryFunction &BF = BFI.second;
     ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = BF.getData();
     assert(ErrorOrFunctionData && "function data is not available");
     ArrayRef<uint8_t> IData = *ErrorOrFunctionData;
-    // BC->outs()  << "Potential Function Entry Point: 0x" << Twine::utohexstr(IData[0]) << "\n";
-    // Check for common function prologue patterns
-    // push %rbp
-    // mov %rsp %rbp
-    if (BF.getSize()>=4 && (char)IData[0] == '\x55' && (char)IData[1] == '\x48' && (char)IData[2] == '\x89' && (char)IData[3] == '\xe5') {
-        LLVM_DEBUG(dbgs() << "[keepPrologueFunction] Find function with prologue in: 0x" << Twine::utohexstr(BF.getAddress()) << "\n");
-    } else if (BF.getSize()>=4 && (char)IData[0] == '\xf3' && (char)IData[1] == '\x0f' && (char)IData[2] == '\x1e' && (char)IData[3] == '\xfa') {
-        LLVM_DEBUG(dbgs() <<  "[keepPrologueFunction] Find function with prologue in: 0x" << Twine::utohexstr(BF.getAddress()) << "\n");
+    // BC->outs()  << "Potential Function Entry Point: 0x" <<
+    // Twine::utohexstr(IData[0]) << "\n"; Check for common function prologue
+    // patterns push %rbp mov %rsp %rbp
+    if (BF.getSize() >= 4 && (char)IData[0] == '\x55' &&
+        (char)IData[1] == '\x48' && (char)IData[2] == '\x89' &&
+        (char)IData[3] == '\xe5') {
+      LLVM_DEBUG(
+          dbgs() << "[keepPrologueFunction] Find function with prologue in: 0x"
+                 << Twine::utohexstr(BF.getAddress()) << "\n");
+    } else if (BF.getSize() >= 4 && (char)IData[0] == '\xf3' &&
+               (char)IData[1] == '\x0f' && (char)IData[2] == '\x1e' &&
+               (char)IData[3] == '\xfa') {
+      LLVM_DEBUG(
+          dbgs() << "[keepPrologueFunction] Find function with prologue in: 0x"
+                 << Twine::utohexstr(BF.getAddress()) << "\n");
     } else {
       BF.setIgnored();
     }
-
   }
 }
 

@yubingex007-a11y yubingex007-a11y changed the title Keep function which is directly called or has prologue [BOLT] Keep function which is directly called or has prologue Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants