Blob Blame History Raw
commit f4abcc05fdba3f25890a9b30b71d511ccc906d46
Author: Mark Wielaard <mark@klomp.org>
Date:   Mon Jul 27 22:43:28 2020 +0200

    Incorrect call-graph tracking due to new _dl_runtime_resolve_xsave*
    
    Newer glibc have alternate ld.so _ld_runtime_resolve functions.
    Namely _dl_runtime_resolve_xsave and _dl_runtime_resolve_xsave'2
    
    This patch recognizes the xsave, xsvec and fxsave variants and
    changes callgrind so that any variant counts as _dl_runtime_resolve.
    
    Original patch by paulo.cesar.pereira.de.andrade@gmail.com
    https://bugs.kde.org/show_bug.cgi?id=415293

diff --git a/callgrind/fn.c b/callgrind/fn.c
index e9d8dd214..7cce1a0c7 100644
--- a/callgrind/fn.c
+++ b/callgrind/fn.c
@@ -30,8 +30,11 @@
 
 static fn_array current_fn_active;
 
-static Addr runtime_resolve_addr = 0;
-static int  runtime_resolve_length = 0;
+/* x86_64 defines 4 variants.  */
+#define MAX_RESOLVE_ADDRS 4
+static int  runtime_resolve_addrs = 0;
+static Addr runtime_resolve_addr[MAX_RESOLVE_ADDRS];
+static int  runtime_resolve_length[MAX_RESOLVE_ADDRS];
 
 // a code pattern is a list of tuples (start offset, length)
 struct chunk_t { int start, len; };
@@ -56,6 +59,9 @@ static Bool check_code(obj_node* obj,
     /* first chunk of pattern should always start at offset 0 and
      * have at least 3 bytes */
     CLG_ASSERT((pat->chunk[0].start == 0) && (pat->chunk[0].len >2));
+
+    /* and we cannot be called more than MAX_RESOLVE_ADDRS times */
+    CLG_ASSERT(runtime_resolve_addrs < MAX_RESOLVE_ADDRS);
     
     CLG_DEBUG(1, "check_code: %s, pattern %s, check %d bytes of [%x %x %x...]\n",
               obj->name, pat->name, pat->chunk[0].len, code[0], code[1], code[2]);
@@ -93,8 +99,9 @@ static Bool check_code(obj_node* obj,
 				 pat->name, obj->name + obj->last_slash_pos,
 				 addr - obj->start, addr, pat->len);
 		    
-		runtime_resolve_addr   = addr;
-		runtime_resolve_length = pat->len;
+		runtime_resolve_addr[runtime_resolve_addrs] = addr;
+		runtime_resolve_length[runtime_resolve_addrs] = pat->len;
+		runtime_resolve_addrs++;
 		return True;
 	    }
         }
@@ -138,8 +145,9 @@ static Bool search_runtime_resolve(obj_node* obj)
 	"x86-glibc2.8", 30, {{ 0,12 }, { 16,14 }, { 30,0}} };
 
     if (VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) return False;
-    if (check_code(obj, code, &pat)) return True;
-    if (check_code(obj, code_28, &pat_28)) return True;
+    Bool pat_p    = check_code(obj, code, &pat);
+    Bool pat_28_p = check_code(obj, code_28, &pat_28);
+    if (pat_p || pat_28_p) return True;
     return False;
 #endif
 
@@ -186,9 +194,98 @@ static Bool search_runtime_resolve(obj_node* obj)
     static struct pattern pat = {
 	"amd64-def", 110, {{ 0,62 }, { 66,44 }, { 110,0 }} };
 
+    static UChar code_xsavec[] = {
+	/* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xc0,
+	/* 8*/ 0x48, 0x2b, 0x25, 0x00, 0x00, 0x00, 0x00, /* sub <i32>(%rip),%rsp */
+	/*15*/ 0x48,
+	/*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
+	/*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
+	/*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
+	/*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
+	/*48*/ 0x30, 0xb8, 0xee, 0x00, 0x00, 0x00, 0x31, 0xd2,
+	/*56*/ 0x48, 0x89, 0x94, 0x24, 0x50, 0x02, 0x00, 0x00,
+	/*64*/ 0x48, 0x89, 0x94, 0x24, 0x58, 0x02, 0x00, 0x00,
+	/*72*/ 0x48, 0x89, 0x94, 0x24, 0x60, 0x02, 0x00, 0x00,
+	/*80*/ 0x48, 0x89, 0x94, 0x24, 0x68, 0x02, 0x00, 0x00,
+	/*88*/ 0x48, 0x89, 0x94, 0x24, 0x70, 0x02, 0x00, 0x00,
+	/*96*/ 0x48, 0x89, 0x94, 0x24, 0x78, 0x02, 0x00, 0x00,
+	/*04*/ 0x0f, 0xc7, 0x64, 0x24, 0x40, 0x48, 0x8b, 0x73,
+	/*112*/0x10, 0x48, 0x8b, 0x7b, 0x08,
+	/*117*/0xe8, 0x00, 0x00, 0x00, 0x00,		/* callq <_dl_fixup> */
+	/*122*/0x49, 0x89, 0xc3, 0xb8, 0xee, 0x00,
+	/*128*/0x00, 0x00, 0x31, 0xd2, 0x0f, 0xae, 0x6c, 0x24,
+	/*136*/0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30, 0x4c, 0x8b,
+	/*144*/0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
+	/*152*/0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54,
+	/*160*/0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48,
+	/*168*/0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc, 0x48, 0x8b,
+	/*176*/0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18, 0xf2, 0x41,
+	/*184*/0xff, 0xe3 };
+    static struct pattern pat_xsavec = {
+	    "amd64-xsavec", 186, {{ 0,11 }, { 15,103 }, {122,64}, { 186,0 }} };
+
+    static UChar code_xsave[] = {
+	/* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xc0,
+	/* 8*/ 0x48, 0x2b, 0x25, 0x00, 0x00, 0x00, 0x00, /* sub <i32>(%rip),%rsp */
+	/*15*/ 0x48,
+	/*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
+	/*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
+	/*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
+	/*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
+	/*48*/ 0x30, 0xb8, 0xee, 0x00, 0x00, 0x00, 0x31, 0xd2,
+	/*56*/ 0x48, 0x89, 0x94, 0x24, 0x40, 0x02, 0x00, 0x00,
+	/*64*/ 0x48, 0x89, 0x94, 0x24, 0x48, 0x02, 0x00, 0x00,
+	/*72*/ 0x48, 0x89, 0x94, 0x24, 0x50, 0x02, 0x00, 0x00,
+	/*80*/ 0x48, 0x89, 0x94, 0x24, 0x58, 0x02, 0x00, 0x00,
+	/*88*/ 0x48, 0x89, 0x94, 0x24, 0x60, 0x02, 0x00, 0x00,
+	/*96*/ 0x48, 0x89, 0x94, 0x24, 0x68, 0x02, 0x00, 0x00,
+	/*104*/0x48, 0x89, 0x94, 0x24, 0x70, 0x02, 0x00, 0x00,
+	/*112*/0x48, 0x89, 0x94, 0x24, 0x78, 0x02, 0x00, 0x00,
+	/*120*/0x0f, 0xae, 0x64, 0x24, 0x40, 0x48, 0x8b, 0x73,
+	/*128*/0x10, 0x48, 0x8b, 0x7b, 0x08,
+	/*133*/0xe8, 0x00, 0x00, 0x00, 0x00,		/* callq <_dl_fixup> */
+	/*138*/0x49, 0x89, 0xc3, 0xb8, 0xee, 0x00,
+	/*144*/0x00, 0x00, 0x31, 0xd2, 0x0f, 0xae, 0x6c, 0x24,
+	/*152*/0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30, 0x4c, 0x8b,
+	/*160*/0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c, 0x24, 0x20,
+	/*168*/0x48, 0x8b, 0x74, 0x24, 0x18, 0x48, 0x8b, 0x54,
+	/*176*/0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24, 0x08, 0x48,
+	/*184*/0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc, 0x48, 0x8b,
+	/*192*/0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18, 0xf2, 0x41,
+	/*200*/0xff, 0xe3 };
+    static struct pattern pat_xsave = {
+	"amd64-xsave", 202, {{ 0,11 }, { 15,119 }, {138,64}, { 202,0 }} };
+
+    static UChar code_fxsave[] = {
+	/* 0*/ 0x53, 0x48, 0x89, 0xe3, 0x48, 0x83, 0xe4, 0xf0,
+	/* 8*/ 0x48, 0x81, 0xec, 0x40, 0x02, 0x00, 0x00, 0x48,
+	/*16*/ 0x89, 0x04, 0x24, 0x48, 0x89, 0x4c, 0x24, 0x08,
+	/*24*/ 0x48, 0x89, 0x54, 0x24, 0x10, 0x48, 0x89, 0x74,
+	/*32*/ 0x24, 0x18, 0x48, 0x89, 0x7c, 0x24, 0x20, 0x4c,
+	/*40*/ 0x89, 0x44, 0x24, 0x28, 0x4c, 0x89, 0x4c, 0x24,
+	/*48*/ 0x30, 0x0f, 0xae, 0x44, 0x24, 0x40, 0x48, 0x8b,
+	/*56*/ 0x73, 0x10, 0x48, 0x8b, 0x7b, 0x08,
+	/*62*/ 0xe8, 0x00, 0x00, 0x00, 0x00,		/* callq <_dl_fixup> */
+	/*67*/ 0x49, 0x89, 0xc3, 0x0f, 0xae,
+	/*72*/ 0x4c, 0x24, 0x40, 0x4c, 0x8b, 0x4c, 0x24, 0x30,
+	/*80*/ 0x4c, 0x8b, 0x44, 0x24, 0x28, 0x48, 0x8b, 0x7c,
+	/*88*/ 0x24, 0x20, 0x48, 0x8b, 0x74, 0x24, 0x18, 0x48,
+	/*96*/ 0x8b, 0x54, 0x24, 0x10, 0x48, 0x8b, 0x4c, 0x24,
+	/*104*/0x08, 0x48, 0x8b, 0x04, 0x24, 0x48, 0x89, 0xdc,
+	/*112*/0x48, 0x8b, 0x1c, 0x24, 0x48, 0x83, 0xc4, 0x18,
+	/*120*/0xf2, 0x41, 0xff, 0xe3 };
+    static struct pattern pat_fxsave = {
+	"amd64-fxsave", 124, {{ 0,63 }, { 67,57 }, { 124,0 }} };
+
     if ((VG_(strncmp)(obj->name, "/lib/ld", 7) != 0) &&
-	(VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0)) return False;
-    return check_code(obj, code, &pat);
+	(VG_(strncmp)(obj->name, "/lib64/ld", 9) != 0) &&
+	(VG_(strncmp)(obj->name, "/usr/lib/ld", 11) != 0) &&
+	(VG_(strncmp)(obj->name, "/usr/lib64/ld", 13) != 0)) return False;
+    Bool pat_p        = check_code(obj, code, &pat);
+    Bool pat_xsavec_p = check_code(obj, code_xsavec, &pat_xsavec);
+    Bool pat_xsave_p  = check_code(obj, code_xsave, &pat_xsave);
+    Bool pat_fxsave_p = check_code(obj, code_fxsave, &pat_fxsave);
+    if (pat_p || pat_xsavec_p || pat_xsave_p || pat_fxsave_p) return True;
 #endif
 
     /* For other platforms, no patterns known */
@@ -254,7 +351,7 @@ obj_node* new_obj_node(DebugInfo* di, obj_node* next)
 	i++;
    }
 
-   if (runtime_resolve_addr == 0) search_runtime_resolve(obj);
+   if (runtime_resolve_addrs == 0) search_runtime_resolve(obj);
 
    return obj;
 }
@@ -490,6 +587,7 @@ fn_node* CLG_(get_fn_node)(BB* bb)
     DebugInfo* di;
     UInt       line_num;
     fn_node*   fn;
+    Int        i;
 
     /* fn from debug info is idempotent for a BB */
     if (bb->fn) return bb->fn;
@@ -538,12 +636,14 @@ fn_node* CLG_(get_fn_node)(BB* bb)
     }
     if (0 == VG_(strcmp)(fnname, "_exit") && !exit_bb)
 	exit_bb = bb;
-    
-    if (runtime_resolve_addr && 
-	(bb_addr(bb) >= runtime_resolve_addr) &&
-	(bb_addr(bb) < runtime_resolve_addr + runtime_resolve_length)) {
-	/* BB in runtime_resolve found by code check; use this name */
-      fnname = "_dl_runtime_resolve";
+
+    for (i = 0; i < runtime_resolve_addrs; i++) {
+      if ((bb_addr(bb) >= runtime_resolve_addr[i]) &&
+	  (bb_addr(bb) < runtime_resolve_addr[i] + runtime_resolve_length[i])) {
+	  /* BB in runtime_resolve found by code check; use this name */
+	  fnname = "_dl_runtime_resolve";
+	  break;
+      }
     }
 
     /* get fn_node struct for this function */