Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp99951lqp; Fri, 12 Apr 2024 11:33:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUT3TNvl7yVNyjPs+WYWApxE9HgNHTqCVjwjEIFzptjtZT2Otm7hGaiGq2g94ht39va/y7+aTiT9wBKmWIG5cZmQXYnV7hNfb6ic0vvQA== X-Google-Smtp-Source: AGHT+IFB2RgYo1DvDWTEloQ9BFn3jpQ4+GdBNs6KTceD9IHlp0yW2BsArExj+8ijprLiK8kXp66Q X-Received: by 2002:a05:6a00:1307:b0:6ea:be74:a228 with SMTP id j7-20020a056a00130700b006eabe74a228mr3595513pfu.28.1712946827801; Fri, 12 Apr 2024 11:33:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712946827; cv=pass; d=google.com; s=arc-20160816; b=HjIvkMDyNKSPeFVLlBTK0K9hP8D4UDLuK1Lgb7uA4DDWZ0yZHcoWA289wSibTV3fbl JddHDGYDXBxemHap/WOL+pPdXqW8zND+Xq3KKRG3gXjmdalpc34AbxmjsIlk4lQPKdFD 3qdbeZpOHxPff0Qp3CCSIOREx8d7LXemGHxSFNlt5h3O6+i4/baGoGcoGhkJaP/t1KW4 Dkm4NNLyCkC+uCZqwGoBEbTTsWwrz12u60YyGCh0TrVlGkmKgHE/GNrQr4v7rPLj04gz V5yTihp6evu60IxRmYoRXu6ctjm5T4nGqlejjltWT0Sn/pXoYDLcQpPnZB7bvsD18W2d 3mNw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=jCMaYBmhn5ba8332U3twE0BBWLMkZD1lXCn8xTP8l+U=; fh=hyuVp9Oha2tDvtTgvqWs4SbegtByEqnlyHtQxda/lgg=; b=qMCDpgldBD7WU8u4iRNemnapQzBNBGRct46ZiwHUzFuE7KZ9O/B2OJxOiwo4LxuR2U EUkkdoc3b8FlZKhecPquPglrrvJznUykpxDa/LowG5IHcaW6Ud7FF/PVdPYYHJ50r4mS A2+7Yz7iseTaF0o+lY9Cegy6a8del99dkw+rkeauZC6q4HrhDuUV2MKYsJWgW00gky/6 DneM+9Zzcx3yB993a8CrkPnVT5q9MPSS1ps7PnFw1IHEn5ZoLNtiy1iw103KAT+pBwR9 jo1WsUyj8Ma89MsFNPAk3LJTasrstZws+58mL6bEIC83uhz+qKW24MRr4l8en9xQXiWK wcFA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pKc1bG6x; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-143208-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143208-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id fa19-20020a056a002d1300b006e6a5a72271si3904577pfb.403.2024.04.12.11.33.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 11:33:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-143208-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pKc1bG6x; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-143208-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143208-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 763E5284B47 for ; Fri, 12 Apr 2024 18:33:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0888914EC59; Fri, 12 Apr 2024 18:33:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pKc1bG6x" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07D7114F13A; Fri, 12 Apr 2024 18:33:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712946793; cv=none; b=HnzOrTqnFfFMNyJzHicxoIavCoGlZWust7iABs5jjF1NN8Je8VFjO2ELvr8EWeYYLTbAWuOUX7Om+p73hGiRWUtI9qIxUQfgYuxsL8cWEiIATCMrzdRDqbKUFi3TIa8Qc24GA24YwEnk2UNLruc0RL9ittzoeAV5pGWBxqIBvxQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712946793; c=relaxed/simple; bh=MdGKWRljF8Qw40speV6mfVp91ivhhrN0E6WVpTJ1KU8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YinOAQdX9N20oGGHdekpe+Rjgsn7v8C8tauNNGj5yafCQIDo0DTsBE3GIAQbIHMLVlbvPPzQBDM1lpbjuaneAQcpkffMSNR/PSRS+mDp22PrwCzBsb8MZkgbSqEQ0DczgStGP1IZtei3UH8X8+x/6RJNwp5DpAY6BnCtMxW1VpA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pKc1bG6x; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A59EC32781; Fri, 12 Apr 2024 18:33:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712946792; bh=MdGKWRljF8Qw40speV6mfVp91ivhhrN0E6WVpTJ1KU8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pKc1bG6xBUUOOwZfvk+GES4RLab54aBAQYX5QKGkQKyT4DXmN4B+nNUmoiFHs5/t9 l8AdYR5YjDmtdW3+h23XK/eKqYvQRImgOGvaawwhSk/U+rNQozdkP3Vu0BEDtYp18t BWrnih5lmEhINYQSYIOAp5ULFee/vrapuwoNLZm3CxECaNXulv8HKOU+9ae2MdVEjI ZPXmBTKkY3gt9ZrApbBjBcr3Y+oS+ofAgis+6GoqM9asJ0m3+PraBjJF/TOV0BsxJ4 VzmScnmdbU4W1WMrlcrSN8Y6PCFIhFP+T8YupllWYVo7OiWCM7j0fn2ZxJ6IytpAGq p/TnWClbb3GvQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Masami Hiramatsu Subject: [PATCH 2/4] perf dwarf-aux: Check pointer offset when checking variables Date: Fri, 12 Apr 2024 11:33:08 -0700 Message-ID: <20240412183310.2518474-3-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog In-Reply-To: <20240412183310.2518474-1-namhyung@kernel.org> References: <20240412183310.2518474-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In match_var_offset(), it checks the offset range with the target type only for non-pointer types. But it also needs to check the pointer types with the target type. This is because there can be more than one pointer variables located in the same register. Let's look at the following example. It's looking up a variable for reg3 at tcp_get_info+0x62. It found "sk" variable but it wasn't the right one since it accesses beyond the target type (struct 'sock' in this case) size. ----------------------------------------------------------- find data type for 0x7bc(reg3) at tcp_get_info+0x62 CU for net/ipv4/tcp.c (die:0x7b5f516) frame base: cfa=0 fbreg=6 offset: 1980 is bigger than size: 760 check variable "sk" failed (die: 0x7b92b2c) variable location: reg3 type='struct sock' size=0x2f8 (die:0x7b63c3a) Actually there was another variable "tp" in the function and it's located at the same (reg3) because it's just type-casted like below. void tcp_get_info(struct sock *sk, struct tcp_info *info) { const struct tcp_sock *tp = tcp_sk(sk); ... The struct tcp_sock contains the struct sock at offset 0 so it can just use the same address as a pointer to tcp_sock. That means it should match variables correctly by checking the offset and size. Actually it cannot distinguish if the offset was smaller than the size of the original struct sock. But I think it's fine as they are the same at that part. So let's check the target type size and retry if it doesn't match. Now it succeeded to find the correct variable. ----------------------------------------------------------- find data type for 0x7bc(reg3) at tcp_get_info+0x62 CU for net/ipv4/tcp.c (die:0x7b5f516) frame base: cfa=0 fbreg=6 found "tp" in scope=1/1 (die: 0x7b92b16) type_offset=0x7bc variable location: reg3 type='struct tcp_sock' size=0xa68 (die:0x7b81380) Fixes: bc10db8eb895 ("perf annotate-data: Support stack variables") Cc: Masami Hiramatsu Signed-off-by: Namhyung Kim --- tools/perf/util/dwarf-aux.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 7dad99ee3ff3..b361fd7ebd56 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1361,7 +1361,7 @@ struct find_var_data { #define DWARF_OP_DIRECT_REGS 32 static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *data, - u64 addr_offset, u64 addr_type) + u64 addr_offset, u64 addr_type, bool is_pointer) { Dwarf_Die type_die; Dwarf_Word size; @@ -1375,6 +1375,12 @@ static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *data, if (die_get_real_type(die_mem, &type_die) == NULL) return false; + if (is_pointer && dwarf_tag(&type_die) == DW_TAG_pointer_type) { + /* Get the target type of the pointer */ + if (die_get_real_type(&type_die, &type_die) == NULL) + return false; + } + if (dwarf_aggregate_size(&type_die, &size) < 0) return false; @@ -1442,31 +1448,38 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg) if (data->is_fbreg && ops->atom == DW_OP_fbreg && data->offset >= (int)ops->number && check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->offset, ops->number)) + match_var_offset(die_mem, data, data->offset, ops->number, + /*is_pointer=*/false)) return DIE_FIND_CB_END; /* Only match with a simple case */ if (data->reg < DWARF_OP_DIRECT_REGS) { /* pointer variables saved in a register 0 to 31 */ if (ops->atom == (DW_OP_reg0 + data->reg) && - check_allowed_ops(ops, nops)) + check_allowed_ops(ops, nops) && + match_var_offset(die_mem, data, data->offset, 0, + /*is_pointer=*/true)) return DIE_FIND_CB_END; /* Local variables accessed by a register + offset */ if (ops->atom == (DW_OP_breg0 + data->reg) && check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->offset, ops->number)) + match_var_offset(die_mem, data, data->offset, ops->number, + /*is_pointer=*/false)) return DIE_FIND_CB_END; } else { /* pointer variables saved in a register 32 or above */ if (ops->atom == DW_OP_regx && ops->number == data->reg && - check_allowed_ops(ops, nops)) + check_allowed_ops(ops, nops) && + match_var_offset(die_mem, data, data->offset, 0, + /*is_pointer=*/true)) return DIE_FIND_CB_END; /* Local variables accessed by a register + offset */ if (ops->atom == DW_OP_bregx && data->reg == ops->number && check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->offset, ops->number2)) + match_var_offset(die_mem, data, data->offset, ops->number2, + /*is_poitner=*/false)) return DIE_FIND_CB_END; } } @@ -1528,7 +1541,8 @@ static int __die_find_var_addr_cb(Dwarf_Die *die_mem, void *arg) continue; if (check_allowed_ops(ops, nops) && - match_var_offset(die_mem, data, data->addr, ops->number)) + match_var_offset(die_mem, data, data->addr, ops->number, + /*is_pointer=*/false)) return DIE_FIND_CB_END; } return DIE_FIND_CB_SIBLING; -- 2.44.0.683.g7961c838ac-goog