Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp667958lqt; Mon, 18 Mar 2024 22:56:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXQc4thcj8dJuYQx6RwOA8c8zuqZuD4M0UHU+4HrEcKMkDAmjofLQMRGAwI2boi2F914VOTaemhZYtOwZsmdnt0hdP5W9Zdsgk2WRmWWA== X-Google-Smtp-Source: AGHT+IEt8WuCW0jHbemVmmSB7LxghtHVkh+ro6gH63EzbljfVr9498N1JhlGog+i7up0/SQL8XRQ X-Received: by 2002:a17:902:6847:b0:1e0:95c:d104 with SMTP id f7-20020a170902684700b001e0095cd104mr1896066pln.2.1710827788779; Mon, 18 Mar 2024 22:56:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710827788; cv=pass; d=google.com; s=arc-20160816; b=EyBXsaDUHJ1ntfRzQbLhShy5DLj7mMDG40W31tOY+e659Mo+SXN1kvMnkXCLCCx4J9 PYkRdRt+LNpdw9vzUi/lYAIfzff09Rbm6dVNmKoMyWLzCbt13aLWSc0l8l4Sb6+5H62h ZJMUSZj+ixiDvitmvVBSdz4kfeON6WeFDE657j1ZbWPr6kKHbEATAyeT9vmuh/UndTcS ys6FgBPIhkBBD4K6jLCvtgNqh5afwklx3JZk5poK5qtIzXpdu9+rM940Gpe1eyPGBkau oqd/YsaokPEeaeOf6LTXsMLAUQGfx4ZrUPxcjedH6PJsv4ND8w41mM0TC354sQPr34+b FLvw== 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=wc9NKYsg6Fui/7UKBahlj+1hF8rxiKJHPgVAvok1MuM=; fh=44lErbL5kvNUjSV6UDWgBEjJyOJpjjMpxi6K+HFi+Bc=; b=bcSGXbToVZIZpOxLo0fLouMK6eKBwyuyI92Oy2zI0A7dX3c8ykxG9bJQcahzI6qRIy LeyX6CfG4dOBT2E1nHjD42w+X/LstxZEzWRQ8APDh0EOxhBaZAFxDQNkn+HY6aQc0dGW EZ/vvRMtUcL5NQ8Am6LSVnWtVuZfZHln9vuKiAz3Pf0yovU+bwXEFKCnsLKur3C/Iudh RIu+odTojWqycFbkhj23/fxukIWcdAY9s+lnEpe8bScYVNxejJ34sZc9mVBl0jwnhS2e KMrnI49VSlNWTOJxeTkmJas3Fe3UOs5FVPApOtlZEp48yAawVM8h2doD2uBbuXmJ6eco Dj7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KvlOHNZU; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-107047-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107047-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. [139.178.88.99]) by mx.google.com with ESMTPS id i3-20020a170902c94300b001db603f6474si2876222pla.170.2024.03.18.22.56.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 22:56:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-107047-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KvlOHNZU; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-107047-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107047-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 72208282394 for ; Tue, 19 Mar 2024 05:56:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A56F57FBAF; Tue, 19 Mar 2024 05:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KvlOHNZU" 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 8973B80637; Tue, 19 Mar 2024 05:51:30 +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=1710827490; cv=none; b=J1OdMLFaaVhNJT9Ui2ndyUNbSXazklMwVfhphehxjgixEL9TBOInEofcgpieyicEcT5Gy79Gz9i+yR0ZDGw+0jtMIbtMSL3RXPrvd9/rjBta58gaGKFHl6U83fU3w/hzjU9fPbfEnA41Lh7hb9cG80gh1yoj8UYyOkp55gBk/Uk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710827490; c=relaxed/simple; bh=/JGap4Y9xwXTO8JArBzhog8K1/fRwK77w+sFtNgYcGA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AqIokfdHKxRIUDE5h0rgSkVxPb9LzMvl2bJ9gqGhNAqcynI4JOIF+AgtiBGya/0Idr2h0/LVvb6XgA9+zRFVt02uxypfzTpb8MpTWP1S8//w4ovbGPZGfhrp8f3hziVCuXF1WdjsN+ce+RaH445sAddMkCzxDP62w9bl1A4g2CA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KvlOHNZU; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED039C433B2; Tue, 19 Mar 2024 05:51:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710827490; bh=/JGap4Y9xwXTO8JArBzhog8K1/fRwK77w+sFtNgYcGA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KvlOHNZUM6VJvVdD/A2qsN9TS31Hqzq87keTkHjIVjM4gAf/5CY57+BeCXNyudI4d xoWk+WpqXIxzKFxuSUk64SzMC86MmM2dUQ45WvjGndGVlY/VX9TIRv7Zwwsa3r/5jZ 1Dp3hVFYHAJMsMiGFx1A8d5WXrAu90QNAhELOwN9L2D1C0mhYGTl48kGboQ12yUTU3 ibCfyuY1GYq5j1lFtIuJfrYaTY1nCp/c9biVXcTvZ9e4f1BeI5H35YfZsiBz/PU3Vn Rvq5QzNls305NqXgZu4kUYsNBg6i8OO1U2GOVwPPIeZdIlbMmlektA5pH1dzfOsqzs RvTb0hbTVj9Kw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Subject: [PATCH 21/23] perf annotate-data: Add stack canary type Date: Mon, 18 Mar 2024 22:51:13 -0700 Message-ID: <20240319055115.4063940-22-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog In-Reply-To: <20240319055115.4063940-1-namhyung@kernel.org> References: <20240319055115.4063940-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 When the stack protector is enabled, compiler would generate code to check stack overflow with a special value called 'stack carary' at runtime. On x86_64, GCC hard-codes the stack canary as %gs:40. While there's a definition of fixed_percpu_data in asm/processor.h, it seems that the header is not included everywhere and many places it cannot find the type info. As it's in the well-known location (at %gs:40), let's add a pseudo stack canary type to handle it specially. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 46 +++++++++++++++++++++++++++++++++ tools/perf/util/annotate-data.h | 1 + tools/perf/util/annotate.c | 25 ++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index bd10a576cfbf..633fe125fcd8 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -30,6 +30,7 @@ enum type_state_kind { TSR_KIND_PERCPU_BASE, TSR_KIND_CONST, TSR_KIND_POINTER, + TSR_KIND_CANARY, }; #define pr_debug_dtp(fmt, ...) \ @@ -62,6 +63,9 @@ static void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind) pr_info(" pointer"); /* it also prints the type info */ break; + case TSR_KIND_CANARY: + pr_info(" stack canary\n"); + return; case TSR_KIND_TYPE: default: break; @@ -676,6 +680,15 @@ static void update_insn_state_x86(struct type_state *state, */ var_addr = src->offset; + if (var_addr == 40) { + tsr->kind = TSR_KIND_CANARY; + tsr->ok = true; + + pr_debug_dtp("mov [%x] stack canary -> reg%d\n", + insn_offset, dst->reg1); + return; + } + if (!get_global_var_type(cu_die, dloc, ip, var_addr, &offset, &type_die) || !die_get_member_type(&type_die, offset, &type_die)) { @@ -991,6 +1004,16 @@ static void delete_var_types(struct die_var_type *var_types) } } +/* should match to is_stack_canary() in util/annotate.c */ +static void setup_stack_canary(struct data_loc_info *dloc) +{ + if (arch__is(dloc->arch, "x86")) { + dloc->op->segment = INSN_SEG_X86_GS; + dloc->op->imm = true; + dloc->op->offset = 40; + } +} + /* It's at the target address, check if it has a matching type */ static bool check_matching_type(struct type_state *state, struct data_loc_info *dloc, int reg, @@ -1038,6 +1061,11 @@ static bool check_matching_type(struct type_state *state, if (stack == NULL) return false; + if (stack->kind == TSR_KIND_CANARY) { + setup_stack_canary(dloc); + return false; + } + *type_die = stack->type; /* Update the type offset from the start of slot */ dloc->type_offset -= stack->offset; @@ -1062,6 +1090,11 @@ static bool check_matching_type(struct type_state *state, if (stack == NULL) return false; + if (stack->kind == TSR_KIND_CANARY) { + setup_stack_canary(dloc); + return false; + } + *type_die = stack->type; /* Update the type offset from the start of slot */ dloc->type_offset -= fboff + stack->offset; @@ -1102,6 +1135,19 @@ static bool check_matching_type(struct type_state *state, return true; } + if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_CANARY) { + pr_debug_dtp(" stack canary\n"); + + /* + * This is a saved value of the stack canary which will be handled + * in the outer logic when it returns failure here. Pretend it's + * from the stack canary directly. + */ + setup_stack_canary(dloc); + + return false; + } + if (map__dso(dloc->ms->map)->kernel && arch__is(dloc->arch, "x86")) { u64 addr; int offset; diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h index ae0f87aed804..1b5a152163b5 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -73,6 +73,7 @@ struct annotated_data_type { extern struct annotated_data_type unknown_type; extern struct annotated_data_type stackop_type; +extern struct annotated_data_type canary_type; /** * struct data_loc_info - Data location information diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index e4121acb4f88..64e54ff1aa1d 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -118,6 +118,13 @@ struct annotated_data_type stackop_type = { }, }; +struct annotated_data_type canary_type = { + .self = { + .type_name = (char *)"(stack canary)", + .children = LIST_HEAD_INIT(canary_type.self.children), + }, +}; + static int arch__grow_instructions(struct arch *arch) { struct ins *new_instructions; @@ -3803,6 +3810,18 @@ static bool is_stack_operation(struct arch *arch, struct disasm_line *dl) return false; } +static bool is_stack_canary(struct arch *arch, struct annotated_op_loc *loc) +{ + /* On x86_64, %gs:40 is used for stack canary */ + if (arch__is(arch, "x86")) { + if (loc->segment == INSN_SEG_X86_GS && loc->imm && + loc->offset == 40) + return true; + } + + return false; +} + u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, struct disasm_line *dl) { @@ -3929,6 +3948,12 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) } mem_type = find_data_type(&dloc); + + if (mem_type == NULL && is_stack_canary(arch, op_loc)) { + mem_type = &canary_type; + dloc.type_offset = 0; + } + if (mem_type) istat->good++; else -- 2.44.0.291.gc1ea87d7ee-goog