From 617ccb41c782711c253674abd42d26e7afdf1888 Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Thu, 23 Jul 2015 19:34:39 +0000
Subject: [PATCH] Remove bad assertion; check for null segment pointer that
 might be returned from IRDB before dereferencing.

Former-commit-id: 73f02c6b3396d18c43bc8e6335574aab4d4db335
---
 src/base/SMPBasicBlock.cpp | 32 ++++++++++++++++++++++----------
 src/base/SMPProgram.cpp    |  5 +++--
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/base/SMPBasicBlock.cpp b/src/base/SMPBasicBlock.cpp
index 6c1ae0ad..e2043bd3 100644
--- a/src/base/SMPBasicBlock.cpp
+++ b/src/base/SMPBasicBlock.cpp
@@ -439,7 +439,7 @@ list<SMPBasicBlock *>::iterator SMPBasicBlock::GetFallThroughSucc(void) {
 			if (FirstSuccAddr > LastAddr) { // block comes after this block; candidate for fall-through
 				STARS_ea_t NewAddrDiff = FirstSuccAddr - LastAddr;
 #if 1
-				if (NewAddrDiff == LastInst->GetSize()) {
+				if (NewAddrDiff == LastInst->GetSize()) {  // Use STARSInstruction::GetNextInstruction  !!!!****!!!!
 					FallThroughSuccIter = SuccIter;
 					break;
 				}
@@ -852,17 +852,29 @@ STARS_ea_t SMPBasicBlock::GetDefAddrFromUseAddr(const STARSOpndTypePtr &UseOp, S
 			if (DefAddr == STARS_BADADDR) { // not found yet
 				// Must be a global name defined in the Phi functions or LiveIn and UpExposed to the block, or a non-stack mem access.
 				bool UseFP = this->GetFunc()->UsesFramePointer();
-				assert(!LocalName || (!MDIsDataFlowOpnd(UseOp, UseFP)));
-				set<SMPPhiFunction, LessPhi>::iterator PhiIter = this->FindPhi(UseOp);
-				if (PhiIter != this->GetLastPhi()) {
-					// Found a Phi function for UseOp; does it DEF our SSANum?
-					int PhiDefSSANum = PhiIter->GetDefSSANum();
-					if (PhiDefSSANum == SSANum) {
-						DefAddr = this->GetNumber() + STARS_PSEUDO_ID_MIN;  // BlockNum is signal that the Def has no real addr, is in Phi
-					}
+				if (LocalName && MDIsDataFlowOpnd(UseOp, UseFP)) {
+					// Should have found DefAddr by now. Why were we called in the first place?
+					SMP_msg("\nERROR in GetDefAddrFromUseAddr() for UseAddr: %llx\n", (unsigned long long) InstAddr);
+					SMP_msg("UseOp: "); PrintOperand(UseOp); SMP_msg("\n");
+					SMP_msg("LocalName: %d  IsDataFlowOpnd: %d\n", LocalName, MDIsDataFlowOpnd(UseOp, UseFP));
+					SMP_msg("UseFP: %d LiveIn: %d Killed: %d\n", UseFP, this->IsLiveIn(UseOp), this->IsVarKill(UseOp));
+#if 0				// Just leave DefAddr set to STARS_BADADDR and return.
+					assert(!LocalName || (!MDIsDataFlowOpnd(UseOp, UseFP)));
+#endif
+
 				}
 				else {
-					DefAddr = STARS_LIVEIN_PSEUDO_ID; // pseudo-def-addr for LiveIn, not in Phi, UpExposed
+					set<SMPPhiFunction, LessPhi>::iterator PhiIter = this->FindPhi(UseOp);
+					if (PhiIter != this->GetLastPhi()) {
+						// Found a Phi function for UseOp; does it DEF our SSANum?
+						int PhiDefSSANum = PhiIter->GetDefSSANum();
+						if (PhiDefSSANum == SSANum) {
+							DefAddr = this->GetNumber() + STARS_PSEUDO_ID_MIN;  // BlockNum is signal that the Def has no real addr, is in Phi
+						}
+					}
+					else {
+						DefAddr = STARS_LIVEIN_PSEUDO_ID; // pseudo-def-addr for LiveIn, not in Phi, UpExposed
+					}
 				}
 			}
 		}
diff --git a/src/base/SMPProgram.cpp b/src/base/SMPProgram.cpp
index fa6ade6e..474e56cd 100644
--- a/src/base/SMPProgram.cpp
+++ b/src/base/SMPProgram.cpp
@@ -281,9 +281,10 @@ void SMPProgram::Analyze(ProfilerInformation *pi, FILE *AnnotFile, FILE *InfoAnn
 			continue;
 		}
 
-#if 1
+#if 1   // IRDB functions might not map to segments, might return nuLL seg.
+		//  Might want an XTRN attribute for Functions instead of segments.
 		STARS_Segment_t *seg = SMP_getseg(FuncInfo->get_startEA());
-		if (seg->IsXternSegment() /* ->type == SEG_XTRN */) {
+		if ((NULL != seg) && seg->IsXternSegment() /* ->type == SEG_XTRN */) {
 			SMP_msg("Skipping SEG_XTRN func: FuncIndex %zu \n", FuncIndex);
 			continue;
 		}
-- 
GitLab