Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4489158ybx; Mon, 4 Nov 2019 14:16:17 -0800 (PST) X-Google-Smtp-Source: APXvYqw93H/IyzRxmugnEtWfuF8WXhxjzBBp1tS7tb5j8PLm6jZwcp8wrYC5BRQGsmtPGIofmbyY X-Received: by 2002:a17:906:f90c:: with SMTP id lc12mr26672705ejb.208.1572905777018; Mon, 04 Nov 2019 14:16:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572905777; cv=none; d=google.com; s=arc-20160816; b=wqhcfYD/oFcp4yJ+48CedHhb75wKeYKTuFiBRsUoJbcSlpnlDKFWhZEghtMKJ9Q8Aw 4uLTY5npavxDzEGyCFJJuZ64f1+GH2t68HWqfOrRabRJjBlNTgrC3HQ7vDuIHTaUeWvR 2a1WVT48HZ+hoaQIRi6pEUv+dNvSzYirLWgbzOxqRIMyqNubA/n4hxWsx0zBwM+2AUdP ICLpX9MkCaAApxAUHx6RS8cyTkZjy/8c9rainIIHVB2/CEDBUl7s7+zOS8g8tiVF8p7s D8YztHC53bKt7jq19A0C8uBb1xALqgj/RIKLHAEKKHirlOI4bMpsoPSPpuuMrJZVwUeA SWyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NSLzGZrRWjEKY0l3NI9XADo44tr430Z/JU0aMocly1I=; b=gcmvGQ4CRUr0Set1NGFpR3xQeE7KVZg8C+GM2wfVhnz4yNj/NjJ2geLh6u4wbDGlFM 8KSXHn63YdBg9Fz0gFgUWsb2Ul4JXG59ZF7vK+lRTcYDHNf9y6ZdoTlP+Jx5RCEXn3gN 5qBUmr+OpsozD4t0yvVKzOOBFu1eKkFXsFuDeOSPvHTqDIF9XJf/zPivt0sJABRY7Mdh IWNkA+RGX4V9whOip84TMA7AxOBXasYfnWYg3fjV9Eux7/UEXpR5nWvjMQsp+MRVTRrX IEmTC7zz+s9uFBZb/iyyzjGqik8ltbLZhgGOiFnwPbmWQ83XqPZxG1DSHKt2zDU1jVya nKZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Ss3kOJ/h"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h90si8963372edd.178.2019.11.04.14.15.47; Mon, 04 Nov 2019 14:16:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Ss3kOJ/h"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389915AbfKDWGJ (ORCPT + 99 others); Mon, 4 Nov 2019 17:06:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:37662 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388887AbfKDWGB (ORCPT ); Mon, 4 Nov 2019 17:06:01 -0500 Received: from localhost (6.204-14-84.ripe.coltfrance.com [84.14.204.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 750432190F; Mon, 4 Nov 2019 22:06:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572905161; bh=ZZLlPeXBR+EVEvw0FmNfaS2VKB63dks1AB/dmDQjob4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ss3kOJ/hNphNygQwxgFGL6vVlhUw+0xgmd/TdHESim840liKFzSY6A+9Oopxksw+W N7ehAqq8WzWulWxdTJUNEDwn/nF8Jb2yUSMmUeUhCwWJQ9cTWOvKKRDcdWSP4UxM0W QhfWIBB//DpgPdzJ2xoW/Iw7qTGnfaYvXpRrtUkU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andi Kleen , Jiri Olsa , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 5.3 011/163] perf script brstackinsn: Fix recovery from LBR/binary mismatch Date: Mon, 4 Nov 2019 22:43:21 +0100 Message-Id: <20191104212141.304255192@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212140.046021995@linuxfoundation.org> References: <20191104212140.046021995@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andi Kleen [ Upstream commit e98df280bc2a499fd41d7f9e2d6733884de69902 ] When the LBR data and the instructions in a binary do not match the loop printing instructions could get confused and print a long stream of bogus instructions. The problem was that if the instruction decoder cannot decode an instruction it ilen wasn't initialized, so the loop going through the basic block would continue with the previous value. Harden the code to avoid such problems: - Make sure ilen is always freshly initialized and is 0 for bad instructions. - Do not overrun the code buffer while printing instructions - Print a warning message if the final jump is not on an instruction boundary. Signed-off-by: Andi Kleen Cc: Jiri Olsa Link: http://lore.kernel.org/lkml/20190927233546.11533-1-andi@firstfloor.org Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/builtin-script.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 0140ddb8dd0bd..c14a1cdad80c0 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1054,7 +1054,7 @@ static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, continue; insn = 0; - for (off = 0;; off += ilen) { + for (off = 0; off < (unsigned)len; off += ilen) { uint64_t ip = start + off; printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp); @@ -1065,6 +1065,7 @@ static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, printed += print_srccode(thread, x.cpumode, ip); break; } else { + ilen = 0; printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip, dump_insn(&x, ip, buffer + off, len - off, &ilen)); if (ilen == 0) @@ -1074,6 +1075,8 @@ static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, insn++; } } + if (off != (unsigned)len) + printed += fprintf(fp, "\tmismatch of LBR data and executable\n"); } /* @@ -1114,6 +1117,7 @@ static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, goto out; } for (off = 0; off <= end - start; off += ilen) { + ilen = 0; printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off, dump_insn(&x, start + off, buffer + off, len - off, &ilen)); if (ilen == 0) -- 2.20.1