From e665f187e427752ac723983223269c18c201a63f Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Wed, 5 Aug 2015 02:59:17 +0000
Subject: [PATCH] Continue improving stack frame analysis.

Former-commit-id: 8472d809f18a3569ec729ca0270b24f8f36afaf1
---
 src/base/SMPFunction.cpp                      | 23 +++++++++++++++++++
 ...ed-save-busybox.psexe.annot.REMOVED.git-id |  2 +-
 ...rted-save-nginx.psexe.annot.REMOVED.git-id |  2 +-
 ...ed-save-openssl.psexe.annot.REMOVED.git-id |  2 +-
 4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index 30425487..fa641c17 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -1935,6 +1935,11 @@ void SMPFunction::SetStackFrameInfo(void) {
 //  saving of registers other than EBP happens AFTER local stack allocation.
 //  A Windows version of the function would expect to see the pushing
 //  of ESI and EDI AFTER the sub esp,34h statement.
+
+// How many irrelevant insts in the first basic block should we see before deciding that frame allocation is finished?
+#define STARS_POST_CALL_NONALLOC_INSTR_LIMIT 10  // after function call seen
+#define STARS_NONALLOC_INSTR_LIMIT 30            // after this many irrelevant insts even if no calls
+
 bool SMPFunction::MDFixFrameInfo(void) {
 	int SavedRegsSize = 0;
 	int OtherPushesSize = 0;  // besides callee-saved regs
@@ -1945,6 +1950,8 @@ bool SMPFunction::MDFixFrameInfo(void) {
 	bool FoundAllocInstr = false; // found simple alloc, e.g. subtraction from stack pointer
 	bool DebugFlag = (0 == strcmp("__libc_csu_init", this->GetFuncName()));
 	bool CallSeen = false;
+	uint16_t PostCallNonAllocInstructions = 0;
+	uint16_t NonAllocatingInstructions = 0;
 	STARS_ea_t CallAddr = STARS_BADADDR;
 	STARS_ea_t SaveAddr = this->GetStartAddr(); // keeps most recent best value for stack frame allocation instr
 	list<pair<STARS_ea_t, int> > AllocPointsList; // list of pair<InstAddr, bytes allocated on stack at that addr>
@@ -2102,6 +2109,22 @@ bool SMPFunction::MDFixFrameInfo(void) {
 				ESPintoEBP = true;
 			}
 		}
+		else {
+			++NonAllocatingInstructions;
+			if (CallSeen) {
+				++PostCallNonAllocInstructions;
+				if (STARS_POST_CALL_NONALLOC_INSTR_LIMIT <= PostCallNonAllocInstructions) {
+					SMP_msg("INFO: Stopping search of first block after call for frame allocations at %llx\n",
+						(unsigned long long) InstAddr);
+					break;
+				}
+			}
+			if (STARS_NONALLOC_INSTR_LIMIT <= NonAllocatingInstructions) {
+				SMP_msg("INFO: Stopping search of first block for frame allocations at %llx\n",
+					(unsigned long long) InstAddr);
+				break;
+			}
+		}
 	} // end for all instructions in the first basic block
 
 	this->LocalVarsSize = (STARS_asize_t) AllocValue + (STARS_asize_t) OtherAllocsSize;
diff --git a/tests/commit/trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id b/tests/commit/trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id
index 5a877f18..235faeff 100644
--- a/tests/commit/trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id
+++ b/tests/commit/trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id
@@ -1 +1 @@
-08b231581017a39f0e53a0b3072765de12e090be
\ No newline at end of file
+b2dc2595edb765cb3b9e6bfdf6d7dc7af5b28601
\ No newline at end of file
diff --git a/tests/commit/trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id b/tests/commit/trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id
index 3c735a86..492ae954 100644
--- a/tests/commit/trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id
+++ b/tests/commit/trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id
@@ -1 +1 @@
-4f6eabd018db2b90ccac7e3be3a98c3729f40c28
\ No newline at end of file
+9d1acf8d25ff0689b7a3211816c1f673b84854c4
\ No newline at end of file
diff --git a/tests/commit/trimmed-sorted-save-openssl.psexe.annot.REMOVED.git-id b/tests/commit/trimmed-sorted-save-openssl.psexe.annot.REMOVED.git-id
index dee4ffb4..c81df73f 100644
--- a/tests/commit/trimmed-sorted-save-openssl.psexe.annot.REMOVED.git-id
+++ b/tests/commit/trimmed-sorted-save-openssl.psexe.annot.REMOVED.git-id
@@ -1 +1 @@
-f5dadddb8f6dd2f81e75244dee3650f9ec4d24b1
\ No newline at end of file
+2efb0ff9a9b3cb592176ae3a969a388e396cc512
\ No newline at end of file
-- 
GitLab