Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp8160947ybn; Tue, 1 Oct 2019 04:14:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqyGBcHczHcWnBhEuNo+VQH4D5ggzv/tyRIMqopimDBYFN1PKF/jVfNFhSeEGg8t1iEIZkjf X-Received: by 2002:a50:8ad1:: with SMTP id k17mr25209431edk.243.1569928498904; Tue, 01 Oct 2019 04:14:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569928498; cv=none; d=google.com; s=arc-20160816; b=dbY4B+PLp72636/WawdL+mFUnzWypzc2P+SzEaiFeZWcq6b1eW8GGbOeL1SzE3lRKa Ky5Tp3Z8ZtMAzbiYjKYYfYFTNIcfhzn28rt/ENMQ+vJvnPN0EvbYCfGHvBhsZDwrc1G3 k/7pExbUEiIxU4uYsYfAhN76+e/lPEUT2f4TRMeJSXLdBxsRTN/RPTA9mhT7+LSQCTPN hts8B5TiigWtXfSBY6BuHKXr1AU2aWCUZ5j0ZLtIj/fhu7vJF7GhQRq3oFDZ4t8P8+fr oqM4Sh70DA7lbZ/Z8FLCyedoujVDoWoWu0aIQhTeu1jRqQejV4elF1/RMilgpFiOBlHM eGgw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZFlkI5d1QrGYyXnqI4FH+x7tGHjkcmAKT1UnfS84yXk=; b=u3ILuMZjp5ycDpYpIUznfSJeULy0mJh1tBaPlBPooC5ar6iywm35G7f3qM9q4zRxcY JyDMKyUTQ/idj7UqMtb6yynLnRCqydMfCpP3YRbYb9E5/6qVv61Z7P1YRGId+h0rmEeA 5m9JfckwVT3y7eXPI2PCe4fwZxdgjVK/Gx1KH0ar/xPePqKDRyvpbEnDX+ygtbbWQw/J hXPtUiZ6oslKYeGqKAQQ5r59eKNf9GcrKQqnajuEqAKjkRIEPqDdzj3guabUQe2VXkRk RqTQAI62afIu+wUK9b/Z+CpWAXypRmo68w55bsdlxicaiLeTMO6GiXdUEVcyd9xcgKjV 2v8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cgbGRlJb; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b16si8225109ejb.165.2019.10.01.04.14.34; Tue, 01 Oct 2019 04:14:58 -0700 (PDT) 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=cgbGRlJb; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731876AbfJALNg (ORCPT + 99 others); Tue, 1 Oct 2019 07:13:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:35546 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731194AbfJALNf (ORCPT ); Tue, 1 Oct 2019 07:13:35 -0400 Received: from quaco.ghostprotocols.net (177.206.223.101.dynamic.adsl.gvt.net.br [177.206.223.101]) (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 E01CA222C6; Tue, 1 Oct 2019 11:13:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569928415; bh=OexZN4oQs5HfxhzlABRCLKxGM6AENf8BWG4B3Z4BhKM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cgbGRlJbaxl1Yx49NwUKEFMceDR5Sl6j4sNVk51h20/f9PSvZjhoWuYcJgwb9KOhc PLxJV4mlKghFh1q5/oImgtx3Q1NwD980u76bJdrflDz16Av+wZdguv2kDfY+6QbXS4 xjJKQL9C1f6SRkM3bJcW/948poNlKFJ/t7DMEmKs= From: Arnaldo Carvalho de Melo To: Ingo Molnar , Thomas Gleixner Cc: Jiri Olsa , Namhyung Kim , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Andi Kleen , Arnaldo Carvalho de Melo Subject: [PATCH 15/24] perf script brstackinsn: Fix recovery from LBR/binary mismatch Date: Tue, 1 Oct 2019 08:12:07 -0300 Message-Id: <20191001111216.7208-16-acme@kernel.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191001111216.7208-1-acme@kernel.org> References: <20191001111216.7208-1-acme@kernel.org> MIME-Version: 1.0 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 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 --- 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 286fc70d7402..67be8d31afab 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1063,7 +1063,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); @@ -1074,6 +1074,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) @@ -1083,6 +1084,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"); } /* @@ -1123,6 +1126,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.21.0