Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4497484ybx; Mon, 4 Nov 2019 14:23:38 -0800 (PST) X-Google-Smtp-Source: APXvYqxDGc1v4EZynCT24B3WJ0OhF9OE3o/OEJSXIdsAI8tbyqkZvI/RI8kcWBQfGQRVZeyCwD5z X-Received: by 2002:a17:907:4300:: with SMTP id oa24mr13100580ejb.8.1572906218606; Mon, 04 Nov 2019 14:23:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572906218; cv=none; d=google.com; s=arc-20160816; b=qS71mNUEbBYHfUp8O2QQO4fmiEf9rrigGm7t9aPr6NsZ5euqIkHEEMLiSICswqt7Pd fB6dsxBc4kazjeZy0UdZrf1Lga/vEkNYwL0oH6Laqc/XJIvxtNiw2HVN80F0ADlyIe3m vTG0VeYz53gtEOsMb45y+qlqE8qT9TMWIzevKsxFuqbj8y2y/n/b3iBulOlfe1N6ORQx lbAPApMk6JVjYjVJGpI4MBYLo6mtnKeQApRrqQTV8Ij5VRS/DSdNAB1YOMUPF7gBLIzA c/Sp3m/GkvfBIbzXLRCALoXHW0wBS0XMEHwcwUF8YYFfHj0AWD+T66MwpUsRYsqpvrXb JBhw== 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=ruLrMqxe3jFFVWTzAF3mT89METlbZfv5q7r0lTXrEHc=; b=UEWkDzD4J7NIjrXSkfP98meDiFheyYAg5ZrO6zPNBkmGxGf2KzJXnDX16GyESShsYS aCcSoYOmaRxKNJp46JVQSAPjMinPQGom9P+NXngR0LnnV2fL8N9ZT0+X53SRVCiGO4cu 9IcFbQ9vXGv0DpPXa2b9jwU11Lt/Y0jeuW5lcMKj5Cmen5tCQwrQ9Yn2ptpwyew/PeZi y70Zu+RzanxMWdgyzS2WVxXQVSgvFkJBnvFEaylm/yQ7NQFnMgF+adyFibvbMyHMNv0Q G54X0u0ZMd9ak+E35x9h1tUCTnfZFFb7OdcvZxNqp2hOTlcKXiXXMVA1A0vsi0bkc/u5 duWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uFfIyn3N; 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 h22si9540033edb.346.2019.11.04.14.23.15; Mon, 04 Nov 2019 14:23:38 -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=uFfIyn3N; 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 S2389172AbfKDWUf (ORCPT + 99 others); Mon, 4 Nov 2019 17:20:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:56648 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388155AbfKDV71 (ORCPT ); Mon, 4 Nov 2019 16:59:27 -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 92FE920650; Mon, 4 Nov 2019 21:59:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572904767; bh=VxPe53bJmI0+m8+ilMm7PTOAeAZoj1mvrbS4fD+q3hc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uFfIyn3NkH/rQGrzmhxX+pB1hGAPrXXCmZhjhTvZY3lM41yYfW7iW712i7kOlmAPW BTTPeTA6AeJVqv/pzzkqea2kRmnTjfj+4I/w68ijcgbziV01+mP8jwBrV3A53IpywQ hMT+v4TsQFnob2rUZyQMwj6tQ8nfDqvZ1jfBvvzg= 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 4.19 063/149] perf script brstackinsn: Fix recovery from LBR/binary mismatch Date: Mon, 4 Nov 2019 22:44:16 +0100 Message-Id: <20191104212141.179859359@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212126.090054740@linuxfoundation.org> References: <20191104212126.090054740@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 53c11fc0855ee..d20f851796c52 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -1021,7 +1021,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); @@ -1029,6 +1029,7 @@ static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp); 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) @@ -1036,6 +1037,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"); } /* @@ -1066,6 +1069,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