From nobody Sun Jun 14 03:57:21 2026 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C890230BEC for ; Mon, 4 May 2026 03:51:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866699; cv=none; b=bLqaxv47eiuSg6q6h1rIaFfu6QH0Y9p0/nKYbFAJV8yzbGvaspQLZ56dl0fAL+4w4zsv4iF3vxgJB76VAef2H/kukUPqVzmbkNXe/RRryoMmXoSElBHKnl3qWEtjjTAQ+WieFouxhI6EhkY1tbj1fE08QwIzv9twKflDR5G2GkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866699; c=relaxed/simple; bh=c9ejlYjPiFjsPhz/yehQpWJw1IyWFFw1CL0F74SsqP4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GLc3ImvjKtFNDfryNsN7zGLpuKFhJg2DHF0GCxKFOeI8L+2oYBNAcctmA+8HrFZ7unRfXlCDAAEM51wMxvJ5zpOZbKTyeDlZZqJ/eIX5CQVVucyMXCRPV7IuUzUf4dc486pirD/me+i4JD+ZCu/UVvRhZNi9Q1AnmgmqT5wy8lw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PADPgoBz; arc=none smtp.client-ip=74.125.82.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PADPgoBz" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-2ee1da7a13fso3374219eec.1 for ; Sun, 03 May 2026 20:51:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777866697; x=1778471497; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vkt4oU+hEVEUN5xjbaGhASHDDjx1y3R0jVDB1yo0F0U=; b=PADPgoBz0VfMrNkC4imxyS0dIXQd8JvFiysJtmCV0bnkEsPSlbdYUkLGHoNgEJ5dRB 7QiCJrAL/oqZ3YiTX+RjagrC3XliaWG7w/3qbMF2eXNy+TrHVjf3ft8UKPtZfZwJ28SF wZCwkMUk6mXzPYL9nIHsElw0c18KgdexgiIpZbdHXwNZ3T7tSo/JJXkbWH66d9Lw5uAn yEm7a/fB2vzLHhUt4QoCmDdHuJscdef02ZDC7eiDBszenJ7KL518ApmKG3uitLLX/Ut0 RdiFIiAzWCiZ9slNaf7lL0qYLBA6zuDSpUWrZwv8LrIrV42KBhAiDeLIUnmQTd5C8BGu buFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777866697; x=1778471497; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Vkt4oU+hEVEUN5xjbaGhASHDDjx1y3R0jVDB1yo0F0U=; b=ozEEbvMISfUm3qwWgYdOQGvARB/namAoXyi0ACvxQWru+QDISbUyYHYBoQsNYGAeGW h2M0iJjVgaRAym58eaPFLnVYbHtZWCYP7ogGs5v5y2oskUaiEDqp98sfVEAdufVu2bIH Sa7iGIlaNt2QEXVLgE5Qx/TXNHKoYWZJj5GwjagY3tMh8DC84pW9wVrFwqyX4z3X5Qd2 RR6tqZI6gzZbVDmJnTwBZbrVswccdIgXMmRQw5Ud97FNmlkiIIIWya7flgmUbLmeOhPb zXGpewFdkujVIHnD1VVI24ANP2Vyt9StsnESYNgcFLicuQUD2gj/vp2xfPsqUSedV/dx PwLg== X-Forwarded-Encrypted: i=1; AFNElJ9HrwztjBkLSEUCxVSShn4hehN6QL0M9nTx2/KMU3jBrpT2Uh8OAQB0MnN7rgitHUI5UoPGazZifDNJ1Fo=@vger.kernel.org X-Gm-Message-State: AOJu0YxA/5q0qO66qq7FdpcFnzNXhtTPRmmPc8rGTsvCr++IcI3pUyYQ 1LqQHt8qpnxW/2z1oXA1kN7JVqSLy8SGcfR3siXR/PnmB/5WpWOzZovjO+wROjbUgaz3ngbbMsr tfaXdDYqReQ== X-Received: from dlec12-n2.prod.google.com ([2002:a05:701b:428c:20b0:12b:fba9:5eb0]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6628:b0:122:33e:6d41 with SMTP id a92af1059eb24-12dfd84327fmr3550268c88.23.1777866697027; Sun, 03 May 2026 20:51:37 -0700 (PDT) Date: Sun, 3 May 2026 20:51:22 -0700 In-Reply-To: <20260504035125.1851720-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260425020529.3246331-1-irogers@google.com> <20260504035125.1851720-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260504035125.1851720-2-irogers@google.com> Subject: [PATCH v3 1/4] perf sched: Add missing mmap2 handler in timehist From: Ian Rogers To: acme@kernel.org, gmx@google.com, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf_sched__timehist() registers event handlers for options using the sched->tool struct. It registers handlers for MMAP, COMM, EXIT, FORK, etc. but completely omits registering a handler for MMAP2 events. Failing to register both MMAP and MMAP2 handlers causes modern systems (which primarily output MMAP2 records) to silently drop VMA map mappings. This results in uninitialized machine/thread mapping structures, making it impossible to resolve shared library instruction pointers (IPs) to dynamic symbols/DSOs during timehist callchain analysis. Fix this by correctly registering perf_event__process_mmap2 in sched->tool inside perf_sched__timehist(). Assisted-by: Gemini-CLI:Google Gemini 3 Fixes: 5bbfec0ad93c ("perf sched: Implement timehist option") Signed-off-by: Ian Rogers --- tools/perf/builtin-sched.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 555247568e7a..241c2f808f7b 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -3299,6 +3299,7 @@ static int perf_sched__timehist(struct perf_sched *sc= hed) */ sched->tool.sample =3D perf_timehist__process_sample; sched->tool.mmap =3D perf_event__process_mmap; + sched->tool.mmap2 =3D perf_event__process_mmap2; sched->tool.comm =3D perf_event__process_comm; sched->tool.exit =3D perf_event__process_exit; sched->tool.fork =3D perf_event__process_fork; --=20 2.54.0.545.g6539524ca2-goog From nobody Sun Jun 14 03:57:21 2026 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00DF923FC41 for ; Mon, 4 May 2026 03:51:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866701; cv=none; b=DqtgGavYle05gM+mkbIDvuHg08vF6XqHxZJguVBfdsNFfynLVoEvrWYoE2wTE/n9a1B+1whoQfKS1ag3thRcpy8LcrLhlkBpFQeYfvTDDOJy0fQGmjVouWYGpc4mmcWC1t5N5G5vmDFOqvRPxedIrU7/eF5EwcTCcpuRpu/Hltw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866701; c=relaxed/simple; bh=oO73p1xRPTCaH7zWY9uPtQL6ZrNpPDEb1CrIWcRHrM0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MXVZVhD9MMwrnxdP4hhN05OwUabajUgczYFjeVLiHjcaRcwo90ty130ZH2t7HJAqoBuiyNuGv77pMbcwkUEzbYWK26tM6zj5cK5sG7j+Q4IC0eywWtBmEMerDOWMbvO8pR5oaK4DesyWcijG1K7fHZCSvpR/qKYOIqmx/NUQJ+w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=njgijQdj; arc=none smtp.client-ip=74.125.82.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="njgijQdj" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-2c0f6593ef5so5228968eec.1 for ; Sun, 03 May 2026 20:51:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777866699; x=1778471499; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=hdtzf9eHR5G9u38wQE27Ez1yBHseJb6vJmHAUnBL/S4=; b=njgijQdjeI+S156AYjrQQ0RWaO9hI+kZi9ftc6cIL11QF+PC+O4qVjc2Y9bBt+OtAb xHzQ5H0hu8hgcuYtV1Io2+78YlhS3KznOWyTqOOKwZRgLr+y3W2wpK60ZayHhkETITtg 1QQVTo7KdbnGwbChXGzjsDPuhoO2iHnQ3XGr3fnxSOQnAXZRsm5699kapFbh+jCSQ5eq aYVfghpAAWWi6IY2yRq/5HW0wDES+LMJ2afflzbIBsHuCHyqz8fvIJkgu/9J+AXASJ0b jTe85KyK2kL9JNKBjFP+pW1zR8ij3sNKpcycjPYcVzh5x2/mK40MOgguI/F6bwcbyM2S eA3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777866699; x=1778471499; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=hdtzf9eHR5G9u38wQE27Ez1yBHseJb6vJmHAUnBL/S4=; b=Dwv7sUmIvm2Mo7N8J7684rZp78daDGqMAp712G0dN1nXGjFKNAvbwLsoHMvJ8diIDO 6dmpy9pKLqwf72b7gjAj4T8tZ6OfWtG2/8O7hoYJxvqjJAJymDXPxSql866X9lVmizu0 IJswB39yh5nwvG/8Vidkd6Inzci0LgMyP6uOL6n9xq96b5lh1uEAK9uVnjP9StudO7vH j401Y+k/VUpkwF2MPOKZTupPm9poH8o9M7xOGBHANC895+3Lf1dwvNb0GB/XaQdjqL+e pihI36fTNVXGLAYJvrWK6e3/TpRv0tBkhb9d/zSKT9Mh5q8lf/JPl3aUqyDpYfbnN8FC xNPg== X-Forwarded-Encrypted: i=1; AFNElJ8fdfpcyhAT/hlsde74BbHNVQySclR31b9aDE8y9JQ+BQ5T+dbNhJqXqgpelK4FFFOJwEkzl0V/JaPOSHc=@vger.kernel.org X-Gm-Message-State: AOJu0YzXQ0w7F68htX4OEATdWqEeYPjGpYVa1XCMxM81+Zi0UczNsNLJ mINmpiytdQVFKHAB89apYtwGVbusO0JhPC+Ed/2wiZP93YcQIhy6MjotLmGwM5nYwWIyL+2Trwf Rq+wWiKskVA== X-Received: from dldz12-n1.prod.google.com ([2002:a05:701b:418c:10b0:12c:91ef:91f4]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:e1e:b0:128:d20a:2f40 with SMTP id a92af1059eb24-12dfd79e773mr4170199c88.8.1777866699093; Sun, 03 May 2026 20:51:39 -0700 (PDT) Date: Sun, 3 May 2026 20:51:23 -0700 In-Reply-To: <20260504035125.1851720-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260425020529.3246331-1-irogers@google.com> <20260504035125.1851720-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260504035125.1851720-3-irogers@google.com> Subject: [PATCH v3 2/4] perf tool: Fix missing schedstat delegates and dont_split_sample_group in delegate_tool From: Ian Rogers To: acme@kernel.org, gmx@google.com, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" delegate_tool was missing the delegate overrides for schedstat_cpu and schedstat_domain. As a result, when allocated with zalloc, these callbacks defaulted to NULL, causing a segmentation fault crash if any schedstat events were delivered during event processing. Fix this by adding delegate_schedstat_cpu and delegate_schedstat_domain via the CREATE_DELEGATE_OP2 macro, and ensuring delegate_tool__init correctly registers them. Additionally, delegate_tool__init completely omitted copying the dont_split_sample_group property from the delegate. This would cause wrapper tools to default the flag to false, which corrupts piped event processing (e.g., in perf inject) by triggering duplicate event deliveries on split sample values in deliver_sample_group(). Similarly, perf_tool__init() omitted the initialization of this boolean field. On stack-allocated tools that rely on this initializer (like intel-tpebs or __cmd_evlist), this could result in uninitialized stack garbage evaluating to true=E2=80=94silently dropping non-leader event members in deliver_sample_group(). Fix both issues by properly copying the field in delegate_tool__init and initializing it to false in perf_tool__init. Assisted-by: Gemini-CLI:Google Gemini 3 Fixes: 6331b2669359 ("perf tool: Add a delegate_tool that just delegates ac= tions to another tool") Fixes: 79bcd34e0f3d ("perf inject: Fix leader sampling inserting additional= samples") Signed-off-by: Ian Rogers --- tools/perf/util/tool.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c index 013c7839e2cf..ff2150517b75 100644 --- a/tools/perf/util/tool.c +++ b/tools/perf/util/tool.c @@ -285,6 +285,7 @@ void perf_tool__init(struct perf_tool *tool, bool order= ed_events) tool->no_warn =3D false; tool->show_feat_hdr =3D SHOW_FEAT_NO_HEADER; tool->merge_deferred_callchains =3D true; + tool->dont_split_sample_group =3D false; =20 tool->sample =3D process_event_sample_stub; tool->mmap =3D process_event_stub; @@ -433,6 +434,8 @@ CREATE_DELEGATE_OP2(stat_config); CREATE_DELEGATE_OP2(stat_round); CREATE_DELEGATE_OP2(thread_map); CREATE_DELEGATE_OP2(time_conv); +CREATE_DELEGATE_OP2(schedstat_cpu); +CREATE_DELEGATE_OP2(schedstat_domain); CREATE_DELEGATE_OP2(tracing_data); =20 #define CREATE_DELEGATE_OP3(name) \ @@ -470,6 +473,7 @@ void delegate_tool__init(struct delegate_tool *tool, st= ruct perf_tool *delegate) tool->tool.no_warn =3D delegate->no_warn; tool->tool.show_feat_hdr =3D delegate->show_feat_hdr; tool->tool.merge_deferred_callchains =3D delegate->merge_deferred_callcha= ins; + tool->tool.dont_split_sample_group =3D delegate->dont_split_sample_group; =20 tool->tool.sample =3D delegate_sample; tool->tool.read =3D delegate_read; @@ -516,4 +520,6 @@ void delegate_tool__init(struct delegate_tool *tool, st= ruct perf_tool *delegate) tool->tool.bpf_metadata =3D delegate_bpf_metadata; tool->tool.compressed =3D delegate_compressed; tool->tool.auxtrace =3D delegate_auxtrace; + tool->tool.schedstat_cpu =3D delegate_schedstat_cpu; + tool->tool.schedstat_domain =3D delegate_schedstat_domain; } --=20 2.54.0.545.g6539524ca2-goog From nobody Sun Jun 14 03:57:21 2026 Received: from mail-dl1-f74.google.com (mail-dl1-f74.google.com [74.125.82.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C76E23E334 for ; Mon, 4 May 2026 03:51:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866704; cv=none; b=EEwcyKVRr7woZsj6oT+9q4PROnKhP0tLsXljt6HIC0TM2Fv228f/oykzIogXnV1pp3vNd8P7pRelP37PgHeeaoOfbEUACwojfxk693WhItMoTKyhklz3hH+8Mrptq6N5FLF4xbriQ1WW9UIjAK+hEeqo7IrIRVKJh9jKTg/DNl4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866704; c=relaxed/simple; bh=63MRj0tNcWABNhp7wZ3Z4yVqVtPdk7at4V1UDrhqrbg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tmYfte1cQ8Fd64kU12H+dnMfKhgf6FU3cmtWeNJYSr1aTsEZVrP+OI9F4kl9otr3WrTo4TeKrzGauw34lA95WLlCxpj75wg5yAdVDN4iw1UIycrrt5FfqvImh0B3SDZkOScYEpNmdB8klYxjwP9I0CiZYFPEFkrnv6os0DeRFKg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nmDIXEfL; arc=none smtp.client-ip=74.125.82.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nmDIXEfL" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-12dfe9c058eso8551555c88.0 for ; Sun, 03 May 2026 20:51:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777866701; x=1778471501; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xta1FzPzN3AU5MrchRSfTxFTrHMMfKWJl+S3js9j/b4=; b=nmDIXEfLD7xis2umb7muPMGIXLDL3OfzlkZy5g7ZMDJx1EYkxin74yd24MgNb2RabF PSafs9IPT+p+tG5hjOuhR8fnlpflv3Ie5uHZQoOOEgxNWh9rOVUyfyzoAxhlW+XSlwX2 vXlGW8hOGZQVGpXvbZxH2yS8s5WpBHQtRwyLgAch5pAEZbTFT6gwTBvSlCHC+9yWcV4G J6fecrTq8LlE9sit71PFiE1b+d0BG08HOLaR1uq+6Mun8LDv7qbTrk+sMcXkPhmi/+Wp Mt6iQ69oZ7hr+B3Vth2y7lHoSf4OTFGIS7gUzpsiUwr3leQrnAFNjW3alOMsawQ0n6mw fL1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777866701; x=1778471501; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xta1FzPzN3AU5MrchRSfTxFTrHMMfKWJl+S3js9j/b4=; b=qS17xKPuXvcLoWipT/JEKJcbq/8aeW2mZ3RG/Mx6E+ghsPpSzmrIWcfrehlgcbE2sR X7zYMy6Tmpyt6TB6o9AYng9oWUQRcYPEkILyX6YnABToDtBlBeHCDjQ3IjoY45UZv4Ji tJnnlSUPvm08b9Vfd6NjnXYUZm1hqdU0MRNWeuHDk01n+OIw2l+0PhaOBbDk5+KfyW4O fmhiqsR2XEiIqsXu1HmOQ0oE1Yw3alFokLaXp/kJeiX0k6S/UjzZAh/IVUQVRDZy/krT HPqxyVvsdBip3z0pqsdSk4StfAx8BDpGbMLYkNtR+9LAT9rbAKC/NQ9Xt6MevAyzeOpG PUvA== X-Forwarded-Encrypted: i=1; AFNElJ9aoga0bT6EUaiMG9290+s4DF9iToQFcWl5tGV46+7i+rfozFWF09XavXOx2+bwDHeqnGucOhxFlULvCPo=@vger.kernel.org X-Gm-Message-State: AOJu0YzDN3BuV1a+bK8gWprmxuY3+6Fn59/u+P+HOAK8s5+vr5FSZwDC 9t0oYE+QX8CqQFCkVN2bo1W6e7bzbmPNokplJpdm3reJXlZMzhWh4K4Pq3vIAZ5fFsOpeEzmtcV fZKyF2B3KJA== X-Received: from dlb33.prod.google.com ([2002:a05:7022:621:b0:12d:b27b:7405]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:258f:b0:128:d438:cad with SMTP id a92af1059eb24-12dfd7da280mr4256196c88.18.1777866701159; Sun, 03 May 2026 20:51:41 -0700 (PDT) Date: Sun, 3 May 2026 20:51:24 -0700 In-Reply-To: <20260504035125.1851720-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260425020529.3246331-1-irogers@google.com> <20260504035125.1851720-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260504035125.1851720-4-irogers@google.com> Subject: [PATCH v3 3/4] perf inject/aslr: Add aslr tool to remap/obfuscate virtual addresses From: Ian Rogers To: acme@kernel.org, gmx@google.com, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If perf.data files are taken from one machine to another they make leak virtual addresses and so weaken ASLR on the machine they are coming from. Add a '--aslr' option for perf inject that remaps all virtual addresses, or drops data/events, so that the virtual address information isn't leaked. When events are not known/handled by the tool they are dropped. This makes the tool conservative and it should never leak ASLR information, but it means virtual address remapping is needed for cases like auxtrace. To ensure comprehensive robustness and security, this tool: - Employs guest namespace isolation by utilizing 'struct machines' to safely interleave host and unprivileged KVM guest virtual address mappings. - Resolves VMA split map failures (caused by maps__fixup_overlap_and_insert) consistently by anchoring mappings on DSO and memory invariants. - Guards against integer overflows in branch stack loops via subtraction-ba= sed bounds arithmetic. - Prevents heap buffer overflows by computing safe word limits on userspace stacks and dynamic hardware tracing (AUX) sizes. - Prevents key collisions/ABA lookups by correctly managing DSO reference c= ounts (dso__get/put). - Cleans up error paths to avoid inconsistent hashmap mappings on OOM failu= res. - Optimizes performance by removing redundant hot-path memory allocations. - Cleanly advances session readers past dropped auxtrace streams. Assisted-by: Gemini-CLI:Google Gemini 3 Signed-off-by: Ian Rogers Co-developed-by: Gabriel Marin Signed-off-by: Gabriel Marin --- v3: Combine split-map fixes, guest namespaces, bounds checks, OOM rollbacks, hot path optimization, safe dso references, and I/O stream error handli= ng from v3/v4 development. Drop raw auxtrace events. Fix thread reference = leaks in event handlers. Fix 32-bit truncation bug in hashmaps using u64* val= ues. Prevent leaking uninitialized heap memory by zeroing copy buffer. Corre= ct bitmask checks for branch stack flags. Avoid PMU configuration corrupti= on. v2: First review feedback adjustments. --- tools/perf/builtin-inject.c | 18 +- tools/perf/util/Build | 1 + tools/perf/util/aslr.c | 1157 +++++++++++++++++++++++++++++++++++ tools/perf/util/aslr.h | 10 + 4 files changed, 1185 insertions(+), 1 deletion(-) create mode 100644 tools/perf/util/aslr.c create mode 100644 tools/perf/util/aslr.h diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index f174bc69cec4..6e6bf6b67956 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -8,6 +8,7 @@ */ #include "builtin.h" =20 +#include "util/aslr.h" #include "util/color.h" #include "util/dso.h" #include "util/vdso.h" @@ -123,6 +124,7 @@ struct perf_inject { bool in_place_update_dry_run; bool copy_kcore_dir; bool convert_callchain; + bool aslr; const char *input_name; struct perf_data output; u64 bytes_written; @@ -2564,6 +2566,8 @@ int cmd_inject(int argc, const char **argv) " instance has a subdir"), OPT_BOOLEAN(0, "convert-callchain", &inject.convert_callchain, "Generate callchains using DWARF and drop register/stack data"), + OPT_BOOLEAN(0, "aslr", &inject.aslr, + "Remap virtual memory addresses similar to ASLR"), OPT_END() }; const char * const inject_usage[] =3D { @@ -2571,6 +2575,7 @@ int cmd_inject(int argc, const char **argv) NULL }; bool ordered_events; + struct perf_tool *tool =3D &inject.tool; =20 if (!inject.itrace_synth_opts.set) { /* Disable eager loading of kernel symbols that adds overhead to perf in= ject. */ @@ -2684,12 +2689,21 @@ int cmd_inject(int argc, const char **argv) inject.tool.schedstat_domain =3D perf_event__repipe_op2_synth; inject.tool.dont_split_sample_group =3D true; inject.tool.merge_deferred_callchains =3D false; - inject.session =3D __perf_session__new(&data, &inject.tool, + if (inject.aslr) { + tool =3D aslr_tool__new(&inject.tool); + if (!tool) { + ret =3D -ENOMEM; + goto out_close_output; + } + } + inject.session =3D __perf_session__new(&data, tool, /*trace_event_repipe=3D*/inject.output.is_pipe, /*host_env=3D*/NULL); =20 if (IS_ERR(inject.session)) { ret =3D PTR_ERR(inject.session); + if (inject.aslr) + aslr_tool__delete(tool); goto out_close_output; } =20 @@ -2789,6 +2803,8 @@ int cmd_inject(int argc, const char **argv) strlist__delete(inject.known_build_ids); zstd_fini(&(inject.session->zstd_data)); perf_session__delete(inject.session); + if (inject.aslr) + aslr_tool__delete(tool); out_close_output: if (!inject.in_place_update) perf_data__close(&inject.output); diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 70cc91d00804..65b96f3b87e2 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -6,6 +6,7 @@ perf-util-y +=3D arm64-frame-pointer-unwind-support.o perf-util-y +=3D addr2line.o perf-util-y +=3D addr_location.o perf-util-y +=3D annotate.o +perf-util-y +=3D aslr.o perf-util-y +=3D blake2s.o perf-util-y +=3D block-info.o perf-util-y +=3D block-range.o diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c new file mode 100644 index 000000000000..32548352e1e5 --- /dev/null +++ b/tools/perf/util/aslr.c @@ -0,0 +1,1157 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "aslr.h" + +#include "addr_location.h" +#include "debug.h" +#include "event.h" +#include "evsel.h" +#include "machine.h" +#include "map.h" +#include "thread.h" +#include "tool.h" +#include "session.h" +#include "data.h" +#include "dso.h" + +#include /* page_size */ +#include +#include +#include +#include +#include + +static int skipn(int fd, u64 n) +{ + char buf[4096]; + ssize_t ret; + + while (n > 0) { + ret =3D read(fd, buf, (n < (u64)sizeof(buf) ? n : (u64)sizeof(buf))); + if (ret <=3D 0) + return ret; + n -=3D ret; + } + + return 0; +} + +/** + * struct remap_addresses_key - Key for mapping original addresses to rema= pped ones. + * @dso: Pointer to the DSO (Dynamic Shared Object) associated with the ma= pping. + * @invariant: Unique offset invariant within the VMA (Virtual Memory Area= ). + * Calculated as `start - pgoff`. This value remains constant = when + * perf's internal `maps__fixup_overlap_and_insert` splits a m= ap into + * fragmented VMA pieces due to overlapping events, allowing u= s to + * resolve split maps consistently back to the original VMA. + * @pid: Process ID associated with the mapping. + */ +struct remap_addresses_key { + struct dso *dso; + u64 invariant; + pid_t pid; +}; + +struct aslr_mapping { + struct list_head node; + u64 orig_start; + u64 len; + u64 remap_start; +}; + +struct aslr_tool { + /** @tool: The tool implemented here and a pointer to a delegate to proce= ss the data. */ + struct delegate_tool tool; + /** @machines: The machines with the input, not remapped, virtual address= layout. */ + struct machines machines; + /** @event_copy: Buffer used to create an event to pass to the delegate. = */ + char event_copy[PERF_SAMPLE_MAX_SIZE]; + /** @remap_addresses: mapping from remap_addresses_key to remapped addres= s. */ + struct hashmap remap_addresses; + /** @top_addresses: mapping from process to max remapped address. */ + struct hashmap top_addresses; + /** @first_kernel_mapping: flag indicating if we are still to process any= kernel mapping. */ + bool first_kernel_mapping; +}; + +static const pid_t kernel_pid =3D -1; + +/* Start remapping user processes from a small non-zero offset. */ +static const u64 user_space_start =3D 0x200000; +static const u64 kernel_space_start =3D 0xffff800010000000; + +static size_t remap_addresses__hash(long _key, void *ctx __maybe_unused) +{ + struct remap_addresses_key *key =3D (struct remap_addresses_key *)_key; + + return (size_t)key->dso ^ key->invariant ^ key->pid; +} + +static bool remap_addresses__equal(long _key1, long _key2, void *ctx __may= be_unused) +{ + struct remap_addresses_key *key1 =3D (struct remap_addresses_key *)_key1; + struct remap_addresses_key *key2 =3D (struct remap_addresses_key *)_key2; + + return key1->dso =3D=3D key2->dso && + key1->invariant =3D=3D key2->invariant && + key1->pid =3D=3D key2->pid; +} + +static size_t top_addresses__hash(long key, void *ctx __maybe_unused) +{ + return key; +} + +static bool top_addresses__equal(long key1, long key2, void *ctx __maybe_u= nused) +{ + return key1 =3D=3D key2; +} + +static u64 round_up_to_page_size(u64 addr) +{ + return (addr + page_size - 1) & ~((u64)page_size - 1); +} + +static u64 aslr_tool__remap_address(struct aslr_tool *aslr, + struct thread *aslr_thread, + u8 cpumode, + u64 addr) +{ + struct addr_location al; + struct remap_addresses_key key; + u64 *remapped_invariant_ptr =3D NULL; + u64 remap_addr =3D 0; + u8 effective_cpumode =3D cpumode; + + if (!aslr_thread) + return 0; /* No thread. */ + + addr_location__init(&al); + if (!thread__find_map(aslr_thread, cpumode, addr, &al)) { + /* + * If lookup fails with specified cpumode, try fallback to the other spa= ce + * to be robust against bad cpumode in samples. + */ + effective_cpumode =3D (cpumode =3D=3D PERF_RECORD_MISC_KERNEL) ? + PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL; + if (!thread__find_map(aslr_thread, effective_cpumode, addr, &al)) { + addr_location__exit(&al); + return 0; /* No mmap. */ + } + } + + key.dso =3D map__dso(al.map); + key.invariant =3D map__start(al.map) - map__pgoff(al.map); + key.pid =3D effective_cpumode =3D=3D PERF_RECORD_MISC_KERNEL ? kernel_pid= : aslr_thread->pid_; + + if (hashmap__find(&aslr->remap_addresses, &key, &remapped_invariant_ptr))= { + remap_addr =3D *remapped_invariant_ptr + map__pgoff(al.map) + + (addr - map__start(al.map)); + } else { + pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%l= x) for pid=3D%d\n", + addr, map__start(al.map), map__size(al.map), key.pid); + } + + addr_location__exit(&al); + return remap_addr; +} + +static u64 aslr_tool__remap_mapping(struct aslr_tool *aslr, + struct thread *aslr_thread, + u8 cpumode, + u64 start, u64 len, u64 pgoff) +{ + struct addr_location al; + struct remap_addresses_key key; + u64 remap_addr =3D 0; + u64 *remapped_invariant_ptr =3D NULL; + u64 *max_addr_ptr =3D NULL; + bool is_contiguous =3D false; + bool first_mapping =3D false; + bool key_found =3D false; + + if (!aslr_thread) + return 0; /* No thread. */ + + addr_location__init(&al); + if (thread__find_map(aslr_thread, cpumode, start, &al)) + key.dso =3D map__dso(al.map); + else + key.dso =3D NULL; + + key.invariant =3D start - pgoff; + key.pid =3D cpumode =3D=3D PERF_RECORD_MISC_KERNEL ? kernel_pid : aslr_th= read->pid_; + + if (hashmap__find(&aslr->remap_addresses, &key, &remapped_invariant_ptr))= { + remap_addr =3D *remapped_invariant_ptr + pgoff; + key_found =3D true; + } else { + struct addr_location prev_al; + + addr_location__init(&prev_al); + if (thread__find_map(aslr_thread, cpumode, start - 1, &prev_al)) { + if (map__start(prev_al.map) + map__size(prev_al.map) =3D=3D start) { + is_contiguous =3D true; + } else { + pr_debug("Previous mmap [%lx, %lx] overlaps current map [%lx, %lx]\n", + map__start(prev_al.map), + map__start(prev_al.map) + map__size(prev_al.map), + start, start+len); + } + } + addr_location__exit(&prev_al); + + if (!hashmap__find(&aslr->top_addresses, key.pid, &max_addr_ptr)) { + first_mapping =3D true; + remap_addr =3D (cpumode =3D=3D PERF_RECORD_MISC_KERNEL ? + kernel_space_start : user_space_start); + } else { + remap_addr =3D *max_addr_ptr; + } + + remap_addr =3D round_up_to_page_size(remap_addr); + if (!is_contiguous && !first_mapping) + remap_addr +=3D page_size; + + { + struct remap_addresses_key *new_key =3D malloc(sizeof(*new_key)); + u64 *new_val =3D malloc(sizeof(u64)); + + if (!new_key || !new_val) { + free(new_key); + free(new_val); + addr_location__exit(&al); + return 0; + } + *new_key =3D key; + new_key->dso =3D dso__get(key.dso); + *new_val =3D remap_addr - pgoff; + + if (hashmap__add(&aslr->remap_addresses, new_key, new_val) !=3D 0) { + dso__put(new_key->dso); + free(new_key); + free(new_val); + addr_location__exit(&al); + return 0; + } + } + } + + /* Update top_addresses */ + { + u64 *new_max =3D malloc(sizeof(u64)); + u64 *old_val =3D NULL; + int err; + + if (!new_max) { + struct remap_addresses_key *old_key =3D NULL; + u64 *old_val_remap =3D NULL; + + if (!key_found) { + hashmap__delete(&aslr->remap_addresses, &key, + &old_key, &old_val_remap); + if (old_key) + dso__put(old_key->dso); + free(old_key); + free(old_val_remap); + } + addr_location__exit(&al); + return 0; + } + *new_max =3D remap_addr + len; + + if (hashmap__find(&aslr->top_addresses, key.pid, &max_addr_ptr)) { + if (*max_addr_ptr > *new_max) + *new_max =3D *max_addr_ptr; + } + + err =3D hashmap__insert(&aslr->top_addresses, key.pid, new_max, + (first_mapping && !key_found) ? + HASHMAP_ADD : HASHMAP_UPDATE, + NULL, &old_val); + if (err) { + struct remap_addresses_key *old_key =3D NULL; + u64 *old_val_remap =3D NULL; + + free(new_max); + if (!key_found) { + hashmap__delete(&aslr->remap_addresses, &key, + &old_key, &old_val_remap); + if (old_key) + dso__put(old_key->dso); + free(old_key); + free(old_val_remap); + } + addr_location__exit(&al); + return 0; + } + free(old_val); + } + + addr_location__exit(&al); + return remap_addr; +} + +static u64 aslr_tool__remap_ksymbol(struct aslr_tool *aslr, + struct thread *aslr_thread, + u64 addr, u32 len) +{ + struct addr_location al; + struct remap_addresses_key key; + u64 remap_addr =3D 0; + u64 *remapped_invariant_ptr =3D NULL; + u64 *max_addr_ptr =3D NULL; + bool first_mapping =3D false; + + if (!aslr_thread) + return 0; /* No thread. */ + + addr_location__init(&al); + if (thread__find_map(aslr_thread, PERF_RECORD_MISC_KERNEL, addr, &al)) + key.dso =3D map__dso(al.map); + else + key.dso =3D NULL; + + key.invariant =3D addr; /* pgoff is 0 for ksymbols */ + key.pid =3D aslr_thread->pid_; + + if (hashmap__find(&aslr->remap_addresses, &key, &remapped_invariant_ptr))= { + remap_addr =3D *remapped_invariant_ptr; + addr_location__exit(&al); + return remap_addr; + } + + if (!hashmap__find(&aslr->top_addresses, key.pid, &max_addr_ptr)) { + first_mapping =3D true; + remap_addr =3D kernel_space_start; + } else { + remap_addr =3D *max_addr_ptr; + } + + remap_addr =3D round_up_to_page_size(remap_addr) + page_size; + + { + struct remap_addresses_key *new_key =3D malloc(sizeof(*new_key)); + u64 *new_val =3D malloc(sizeof(u64)); + + if (!new_key || !new_val) { + free(new_key); + free(new_val); + addr_location__exit(&al); + return 0; + } + *new_key =3D key; + new_key->dso =3D dso__get(key.dso); + *new_val =3D remap_addr; + + if (hashmap__add(&aslr->remap_addresses, new_key, new_val) < 0) { + dso__put(new_key->dso); + free(new_key); + free(new_val); + addr_location__exit(&al); + return 0; + } + } + + { + u64 *new_max =3D malloc(sizeof(u64)); + u64 *old_val =3D NULL; + int err; + + if (!new_max) { + struct remap_addresses_key *old_key =3D NULL; + u64 *old_val_remap =3D NULL; + + hashmap__delete(&aslr->remap_addresses, &key, &old_key, &old_val_remap); + if (old_key) + dso__put(old_key->dso); + free(old_key); + free(old_val_remap); + addr_location__exit(&al); + return 0; + } + *new_max =3D remap_addr + len; + + if (hashmap__find(&aslr->top_addresses, key.pid, &max_addr_ptr)) { + if (*max_addr_ptr > *new_max) + *new_max =3D *max_addr_ptr; + } + + err =3D hashmap__insert(&aslr->top_addresses, key.pid, new_max, + first_mapping ? + HASHMAP_ADD : HASHMAP_UPDATE, + NULL, &old_val); + if (err) { + struct remap_addresses_key *old_key =3D NULL; + u64 *old_val_remap =3D NULL; + + free(new_max); + hashmap__delete(&aslr->remap_addresses, &key, &old_key, &old_val_remap); + if (old_key) + dso__put(old_key->dso); + free(old_key); + free(old_val_remap); + addr_location__exit(&al); + return 0; + } + free(old_val); + } + + addr_location__exit(&al); + return remap_addr; +} + + +static int aslr_tool__process_mmap(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + union perf_event *new_event; + u8 cpumode; + struct thread *thread; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + new_event =3D (union perf_event *)aslr->event_copy; + cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_mmap(tool, event, sample, aslr_machine); + if (err) + return err; + + thread =3D machine__findnew_thread(aslr_machine, event->mmap.pid, event->= mmap.tid); + if (!thread) + return -ENOMEM; + memcpy(&new_event->mmap, &event->mmap, event->mmap.header.size); + /* Remaps the mmap.start. */ + new_event->mmap.start =3D aslr_tool__remap_mapping(aslr, thread, cpumode, + event->mmap.start, + event->mmap.len, + event->mmap.pgoff); + if (aslr->first_kernel_mapping && cpumode =3D=3D PERF_RECORD_MISC_KERNEL)= { + /* + * If this is the first kernel image, we need to adjust the pgoff by a + * similar delta. + */ + new_event->mmap.pgoff =3D event->mmap.pgoff - event->mmap.start + + new_event->mmap.start; + aslr->first_kernel_mapping =3D false; + } + err =3D delegate->mmap(delegate, new_event, sample, machine); + thread__put(thread); + return err; +} + +static int aslr_tool__process_mmap2(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + union perf_event *new_event; + u8 cpumode; + struct thread *thread; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + new_event =3D (union perf_event *)aslr->event_copy; + cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_mmap2(tool, event, sample, aslr_machine); + if (err) + return err; + + thread =3D machine__findnew_thread(aslr_machine, event->mmap2.pid, event-= >mmap2.tid); + if (!thread) + return -ENOMEM; + memcpy(&new_event->mmap2, &event->mmap2, event->mmap2.header.size); + /* Remaps the mmap.start. */ + new_event->mmap2.start =3D aslr_tool__remap_mapping(aslr, thread, cpumode, + event->mmap2.start, + event->mmap2.len, + event->mmap2.pgoff); + if (aslr->first_kernel_mapping && cpumode =3D=3D PERF_RECORD_MISC_KERNEL)= { + /* + * If this is the first kernel image, we need to adjust the pgoff by a + * similar delta. + */ + new_event->mmap2.pgoff =3D event->mmap2.pgoff - event->mmap2.start + + new_event->mmap2.start; + aslr->first_kernel_mapping =3D false; + } + err =3D delegate->mmap2(delegate, new_event, sample, machine); + thread__put(thread); + return err; +} + +static int aslr_tool__process_comm(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_comm(tool, event, sample, aslr_machine); + if (err) + return err; + + return delegate->comm(delegate, event, sample, machine); +} + +static int aslr_tool__process_fork(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_fork(tool, event, sample, aslr_machine); + if (err) + return err; + + return delegate->fork(delegate, event, sample, machine); +} + +static int aslr_tool__process_exit(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_exit(tool, event, sample, aslr_machine); + if (err) + return err; + + return delegate->exit(delegate, event, sample, machine); +} + +static int aslr_tool__process_text_poke(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + union perf_event *new_event; + u8 cpumode; + struct thread *thread; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + new_event =3D (union perf_event *)aslr->event_copy; + cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + + thread =3D machine__findnew_thread(aslr_machine, sample->pid, sample->tid= ); + if (!thread) + return -ENOMEM; + memcpy(&new_event->text_poke, &event->text_poke, event->text_poke.header.= size); + new_event->text_poke.addr =3D aslr_tool__remap_address(aslr, thread, cpum= ode, + event->text_poke.addr); + + err =3D delegate->text_poke(delegate, new_event, sample, machine); + + thread__put(thread); + return err; +} + +static int aslr_tool__process_ksymbol(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + union perf_event *new_event; + struct thread *thread; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + new_event =3D (union perf_event *)aslr->event_copy; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_ksymbol(tool, event, sample, aslr_machine); + if (err) + return err; + + thread =3D machine__findnew_thread(aslr_machine, kernel_pid, 0); + if (!thread) + return -ENOMEM; + memcpy(&new_event->ksymbol, &event->ksymbol, event->ksymbol.header.size); + /* Remaps the ksymbol.start */ + new_event->ksymbol.addr =3D aslr_tool__remap_ksymbol(aslr, thread, + event->ksymbol.addr, event->ksymbol.len); + + err =3D delegate->ksymbol(delegate, new_event, sample, machine); + thread__put(thread); + return err; +} + +static int aslr_tool__process_sample(const struct perf_tool *tool, union p= erf_event *event, + struct perf_sample *sample, + struct evsel *evsel, struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + int ret; + u64 sample_type; + struct thread *thread; + struct machine *aslr_machine; + __u64 max_i; + __u64 max_j; + union perf_event *new_event; + struct perf_sample new_sample; + __u64 *in_array, *out_array; + u8 cpumode; + u64 addr; + size_t i; + size_t j; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + ret =3D -EFAULT; + sample_type =3D evsel->core.attr.sample_type; + max_i =3D (event->header.size - sizeof(struct perf_event_header)) / sizeo= f(__u64); + max_j =3D (PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_event_header)) / siz= eof(__u64); + new_event =3D (union perf_event *)aslr->event_copy; + cpumode =3D sample->cpumode; + i =3D 0; + j =3D 0; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + + thread =3D machine__findnew_thread(aslr_machine, sample->pid, sample->tid= ); + + if (!thread) + return -ENOMEM; + + if (max_i > PERF_SAMPLE_MAX_SIZE / sizeof(u64)) + goto out_put; + + + + new_event->sample.header =3D event->sample.header; + + in_array =3D &event->sample.array[0]; + out_array =3D &new_event->sample.array[0]; + +#define CHECK_BOUNDS(required_i, required_j) \ + do { \ + if (i + (required_i) > max_i || j + (required_j) > max_j) { \ + ret =3D -EFAULT; \ + goto out_put; \ + } \ + } while (0) + +#define COPY_U64() \ + do { \ + CHECK_BOUNDS(1, 1); \ + out_array[j++] =3D in_array[i++]; \ + } while (0) + +#define REMAP_U64(addr_field) \ + do { \ + CHECK_BOUNDS(1, 1); \ + out_array[j++] =3D aslr_tool__remap_address(aslr, thread, cpumode, addr_= field); \ + i++; \ + } while (0) + + if (sample_type & PERF_SAMPLE_IDENTIFIER) + COPY_U64(); /* id */ + if (sample_type & PERF_SAMPLE_IP) + REMAP_U64(sample->ip); + if (sample_type & PERF_SAMPLE_TID) + COPY_U64(); /* pid, tid */ + if (sample_type & PERF_SAMPLE_TIME) + COPY_U64(); /* time */ + if (sample_type & PERF_SAMPLE_ADDR) + REMAP_U64(sample->addr); + if (sample_type & PERF_SAMPLE_ID) + COPY_U64(); /* id */ + if (sample_type & PERF_SAMPLE_STREAM_ID) + COPY_U64(); /* stream_id */ + if (sample_type & PERF_SAMPLE_CPU) + COPY_U64(); /* cpu, res */ + if (sample_type & PERF_SAMPLE_PERIOD) + COPY_U64(); /* period */ + if (sample_type & PERF_SAMPLE_READ) { + if ((evsel->core.attr.read_format & PERF_FORMAT_GROUP) =3D=3D 0) { + COPY_U64(); /* value */ + if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) + COPY_U64(); /* time_enabled */ + if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) + COPY_U64(); /* time_running */ + if (evsel->core.attr.read_format & PERF_FORMAT_ID) + COPY_U64(); /* id */ + if (evsel->core.attr.read_format & PERF_FORMAT_LOST) + COPY_U64(); /* lost */ + } else { + u64 nr; + + CHECK_BOUNDS(1, 1); + nr =3D out_array[j++] =3D in_array[i++]; + if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) + COPY_U64(); /* time_enabled */ + if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) + COPY_U64(); /* time_running */ + for (u64 cntr =3D 0; cntr < nr; cntr++) { + COPY_U64(); /* value */ + if (evsel->core.attr.read_format & PERF_FORMAT_ID) + COPY_U64(); /* id */ + if (evsel->core.attr.read_format & PERF_FORMAT_LOST) + COPY_U64(); /* lost */ + } + } + } + if (sample_type & PERF_SAMPLE_CALLCHAIN) { + u64 nr; + + CHECK_BOUNDS(1, 1); + nr =3D out_array[j++] =3D in_array[i++]; + + for (u64 cntr =3D 0; cntr < nr; cntr++) { + CHECK_BOUNDS(1, 1); + addr =3D in_array[i++]; + if (addr >=3D PERF_CONTEXT_MAX) { + out_array[j++] =3D addr; + switch (addr) { + case PERF_CONTEXT_HV: + cpumode =3D PERF_RECORD_MISC_HYPERVISOR; + break; + case PERF_CONTEXT_KERNEL: + cpumode =3D PERF_RECORD_MISC_KERNEL; + break; + case PERF_CONTEXT_USER: + cpumode =3D PERF_RECORD_MISC_USER; + break; + case PERF_CONTEXT_GUEST: + cpumode =3D PERF_RECORD_MISC_GUEST_KERNEL; + break; + case PERF_CONTEXT_GUEST_KERNEL: + cpumode =3D PERF_RECORD_MISC_GUEST_KERNEL; + break; + case PERF_CONTEXT_GUEST_USER: + cpumode =3D PERF_RECORD_MISC_GUEST_USER; + break; + case PERF_CONTEXT_USER_DEFERRED: + /* + * Immediately followed by a 64-bit + * stitching cookie. Skip/Copy it! + */ + CHECK_BOUNDS(1, 1); + out_array[j++] =3D in_array[i++]; + cntr++; + break; + default: + pr_debug("invalid callchain context: %"PRIx64"\n", addr); + ret =3D 0; + goto out_put; + } + continue; + } + out_array[j++] =3D aslr_tool__remap_address(aslr, thread, cpumode, addr= ); + } + } + if (sample_type & PERF_SAMPLE_RAW) { + size_t bytes =3D sizeof(u32) + sample->raw_size; + size_t u64_words =3D (bytes + 7) / 8; + + if (i + u64_words > max_i || j + u64_words > max_j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], bytes); + i +=3D u64_words; + j +=3D u64_words; + /* + * TODO: certain raw samples can be remapped, such as + * tracepoints by examining their fields. + */ + pr_debug("Dropping raw samples as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_BRANCH_STACK) { + u64 nr; + + CHECK_BOUNDS(1, 1); + nr =3D out_array[j++] =3D in_array[i++]; + + if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX) + COPY_U64(); /* hw_idx */ + + if (nr > (ULLONG_MAX / 3)) { + ret =3D -EFAULT; + goto out_put; + } + if (nr * 3 > max_i - i || nr * 3 > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + for (u64 cntr =3D 0; cntr < nr; cntr++) { + out_array[j++] =3D aslr_tool__remap_address(aslr, thread, + sample->cpumode, + in_array[i++]); /* from */ + out_array[j++] =3D aslr_tool__remap_address(aslr, thread, + sample->cpumode, + in_array[i++]); /* to */ + out_array[j++] =3D in_array[i++]; /* flags */ + } + if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) { + if (nr > max_i - i || nr > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], nr * sizeof(u64)); + i +=3D nr; + j +=3D nr; + /* TODO: confirm branch counters don't leak ASLR information. */ + pr_debug("Dropping sample branch counters as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + } + if (sample_type & PERF_SAMPLE_REGS_USER) { + u64 abi; + + COPY_U64(); /* abi */ + abi =3D out_array[j-1]; + if (abi !=3D PERF_SAMPLE_REGS_ABI_NONE) { + u64 nr =3D hweight64(evsel->core.attr.sample_regs_user); + + if (nr > max_i - i || nr > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], nr * sizeof(u64)); + i +=3D nr; + j +=3D nr; + } + /* TODO: can this be less conservative? */ + pr_debug("Dropping regs user sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_STACK_USER) { + u64 size; + + CHECK_BOUNDS(1, 1); + size =3D out_array[j++] =3D in_array[i++]; + if (size > 0) { + size_t u64_words =3D size / 8 + (size % 8 ? 1 : 0); + + if (u64_words > max_i - i || u64_words > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], size); + i +=3D u64_words; + j +=3D u64_words; + + COPY_U64(); /* dyn_size */ + } + /* TODO: can this be less conservative? */ + pr_debug("Dropping stack user sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) + COPY_U64(); /* perf_sample_weight */ + if (sample_type & PERF_SAMPLE_DATA_SRC) + COPY_U64(); /* data_src */ + if (sample_type & PERF_SAMPLE_TRANSACTION) + COPY_U64(); /* transaction */ + if (sample_type & PERF_SAMPLE_REGS_INTR) { + u64 abi; + + COPY_U64(); /* abi */ + abi =3D out_array[j-1]; + if (abi !=3D PERF_SAMPLE_REGS_ABI_NONE) { + u64 nr =3D hweight64(evsel->core.attr.sample_regs_intr); + + if (nr > max_i - i || nr > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], nr * sizeof(u64)); + i +=3D nr; + j +=3D nr; + } + /* TODO: can this be less conservative? */ + pr_debug("Dropping interrupt register sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_PHYS_ADDR) { + COPY_U64(); /* phys_addr */ + /* TODO: can this be less conservative? */ + pr_debug("Dropping physical address sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_CGROUP) + COPY_U64(); /* cgroup */ + if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) + COPY_U64(); /* data_page_size */ + if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) + COPY_U64(); /* code_page_size */ + + if (sample_type & PERF_SAMPLE_AUX) { + u64 size; + + CHECK_BOUNDS(1, 1); + size =3D out_array[j++] =3D in_array[i++]; + if (size > 0) { + size_t u64_words =3D size / 8 + (size % 8 ? 1 : 0); + + if (u64_words > max_i - i || u64_words > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], size); + i +=3D u64_words; + j +=3D u64_words; + } + /* TODO: can this be less conservative? */ + pr_debug("Dropping aux sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + + if (evsel__is_offcpu_event(evsel)) { + /* TODO: can this be less conservative? */ + pr_debug("Dropping off-CPU sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + + new_event->sample.header.size =3D sizeof(struct perf_event_header) + j * = sizeof(u64); + + perf_sample__init(&new_sample, /*all=3D*/ true); + ret =3D evsel__parse_sample(evsel, new_event, &new_sample); + if (ret) { + perf_sample__exit(&new_sample); + goto out_put; + } + + ret =3D delegate->sample(delegate, new_event, &new_sample, evsel, machine= ); + perf_sample__exit(&new_sample); + +out_put: + thread__put(thread); + return ret; +} + +#undef CHECK_BOUNDS +#undef COPY_U64 +#undef REMAP_U64 + + +static int aslr_tool__process_attr(const struct perf_tool *tool, + union perf_event *event, + struct evlist **pevlist) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + union perf_event *new_event; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + new_event =3D (union perf_event *)aslr->event_copy; + + memcpy(&new_event->attr, &event->attr, event->attr.header.size); + if (new_event->attr.attr.type =3D=3D PERF_TYPE_BREAKPOINT) + new_event->attr.attr.bp_addr =3D 0; /* Conservatively remove addresses.= */ + + return delegate->attr(delegate, new_event, pevlist); +} + +static s64 aslr_tool__process_auxtrace(const struct perf_tool *tool __mayb= e_unused, + struct perf_session *session, + union perf_event *event) +{ + if (perf_data__is_pipe(session->data)) { + int err =3D skipn(perf_data__fd(session->data), event->auxtrace.size); + + if (err < 0) + return err; + } + return event->auxtrace.size; +} + +static int aslr_tool__process_auxtrace_info(const struct perf_tool *tool _= _maybe_unused, + struct perf_session *session __maybe_unused, + union perf_event *event __maybe_unused) +{ + return 0; +} + +static int aslr_tool__process_auxtrace_error(const struct perf_tool *tool = __maybe_unused, + struct perf_session *session __maybe_unused, + union perf_event *event __maybe_unused) +{ + return 0; +} + +static void aslr_tool__init(struct aslr_tool *aslr, struct perf_tool *dele= gate) +{ + delegate_tool__init(&aslr->tool, delegate); + aslr->tool.tool.ordered_events =3D true; + + machines__init(&aslr->machines); + + hashmap__init(&aslr->remap_addresses, + remap_addresses__hash, remap_addresses__equal, + /*ctx=3D*/NULL); + hashmap__init(&aslr->top_addresses, + top_addresses__hash, top_addresses__equal, + /*ctx=3D*/NULL); + aslr->first_kernel_mapping =3D true; + + aslr->tool.tool.sample =3D aslr_tool__process_sample; + /* read - reads a counter, okay to delegate. */ + aslr->tool.tool.mmap =3D aslr_tool__process_mmap; + aslr->tool.tool.mmap2 =3D aslr_tool__process_mmap2; + aslr->tool.tool.comm =3D aslr_tool__process_comm; + aslr->tool.tool.fork =3D aslr_tool__process_fork; + aslr->tool.tool.exit =3D aslr_tool__process_exit; + /* namesspaces, cgroup, lost, lost_sample, aux, */ + /* itrace_start, aux_output_hw_id, context_switch, throttle, unthrottle */ + /* - no virtual addresses. */ + aslr->tool.tool.ksymbol =3D aslr_tool__process_ksymbol; + /* bpf - no virtual address. */ + aslr->tool.tool.text_poke =3D aslr_tool__process_text_poke; + aslr->tool.tool.attr =3D aslr_tool__process_attr; + /* event_update, tracing_data, finished_round, build_id, id_index, */ + /* event_update, tracing_data, finished_round, build_id, id_index, */ + /* auxtrace_info, auxtrace_error, time_conv, thread_map, cpu_map, */ + /* stat_config, stat, feature, finished_init, bpf_metadata, compressed, */ + /* auxtrace - no virtual addresses. */ + aslr->tool.tool.auxtrace =3D aslr_tool__process_auxtrace; + aslr->tool.tool.auxtrace_info =3D aslr_tool__process_auxtrace_info; + aslr->tool.tool.auxtrace_error =3D aslr_tool__process_auxtrace_error; +} + +struct perf_tool *aslr_tool__new(struct perf_tool *delegate) +{ + struct aslr_tool *aslr =3D zalloc(sizeof(*aslr)); + + if (!aslr) + return NULL; + + aslr_tool__init(aslr, delegate); + return &aslr->tool.tool; +} + +void aslr_tool__delete(struct perf_tool *tool) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct hashmap_entry *cur; + size_t bkt; + + if (!tool) + return; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + + hashmap__for_each_entry(&aslr->remap_addresses, cur, bkt) { + struct remap_addresses_key *key =3D (struct remap_addresses_key *)cur->p= key; + + if (key) + dso__put(key->dso); + zfree(&cur->pkey); + zfree(&cur->pvalue); + } + hashmap__for_each_entry(&aslr->top_addresses, cur, bkt) { + zfree(&cur->pvalue); + } + + hashmap__clear(&aslr->remap_addresses); + hashmap__clear(&aslr->top_addresses); + machines__exit(&aslr->machines); + free(aslr); +} diff --git a/tools/perf/util/aslr.h b/tools/perf/util/aslr.h new file mode 100644 index 000000000000..ea984d82681f --- /dev/null +++ b/tools/perf/util/aslr.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_ASLR_H +#define __PERF_ASLR_H + +struct perf_tool; + +struct perf_tool *aslr_tool__new(struct perf_tool *delegate); +void aslr_tool__delete(struct perf_tool *aslr); + +#endif /* __PERF_ASLR_H */ --=20 2.54.0.545.g6539524ca2-goog From nobody Sun Jun 14 03:57:21 2026 Received: from mail-dl1-f74.google.com (mail-dl1-f74.google.com [74.125.82.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4539E23C8AE for ; Mon, 4 May 2026 03:51:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866706; cv=none; b=eudXSSoKqDJLHD5yTHsJZ3gRg+LCS8YaBayyIwTlx9cd+aeEy8ocGnVG168BBkIv1PIaxR07wj8VabU8bXms/fiOqjV1ozPTFJ4taykgZ1CiqrH+M/tSENJgRQdnxfM8kBD0fSyESQTUD4tRs4LRibwdr4c3W0gpJi4N7QnWgzM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777866706; c=relaxed/simple; bh=QyFcM88uFzx8pRnt6O+TUgQvIHrG2uCDRqN+BLtSVwo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bzSvvKrv3efHGKKIH0lxhrymp6BLvrir9l8rGqy47tPpcj1xcFbbqcY3U31NV+sKmNkNEDm8M26WwJAa1PIGIb0keYszXiJNdb6GnjmXtxlfWnFCLiag5xUo2Hbmna9c0QPccMeV+lEDnz0X69n2/C9WtwXrHjRbx4zQRCZDaR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Y8d3CpkG; arc=none smtp.client-ip=74.125.82.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Y8d3CpkG" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-12c726f4055so6101416c88.1 for ; Sun, 03 May 2026 20:51:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777866703; x=1778471503; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=o4zjjwnheQuipa7JdIuX+je4jCogvp4p9A3GIGkvi3M=; b=Y8d3CpkGoqiLd17VWmJSOmKIBj/eS3B24kRu7m7nr78YlvutC6LSc8jNf/jU4a7TIG nUl2qOg8fbLncAsHRaG5daxUwcT/9U05uYlmQ8Hzh6KlSHbNYM0WvunfpnJXJwB1oTHx 6dphrMpAZLaSrYEtWN1VQh+TaA6Geh/X/j3lkq63dNrABzabag7QKrNwhtSM5KXMkgoZ nyfpXVrfE3TC5F3brOktzMQSILZa+bh88oVnZPl1wxDn7rOhIdeES9+vvIwk313k6N6y GNnN5FBKUM0b5P4rPi2Of5/rEiLKyjcu7YwKDsA3DHcz1tGTtMPeaO5Op7AyzVXWEpwC X6NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777866703; x=1778471503; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=o4zjjwnheQuipa7JdIuX+je4jCogvp4p9A3GIGkvi3M=; b=r3Dp/UzpB0FYucpj3soZ/EEmaO3dzZgybNSUvKkWs1sV0MqU8zcnBfNqeOr4nXeiDx gGK5nPBuxgD9TFQM0XczobTSXXlQT+KFMyEpIBumG2ZJx+gTroRsuNV4m5yPZlN4MIEU sVwdyDTl3o9qFxo/MYyk8+Uc/ed/ShueaO2hWly1Cs8XrqgIASpHJrkMwY3wm9cKXeDt 8eZorA54RkiDPAcsIv4QTa+tsUdQXWYpzm4Z9KAj07m51mDORvLNMWGaeqe514N1PUlv G4UsBgq9r1MyDJrcYZEvC6WUP7/KsrKr77ZBZ/juvw1z7BAqpcurxCP/5NzG88qaPTrG klxQ== X-Forwarded-Encrypted: i=1; AFNElJ+osQGU6txSwIOf+Bt0Kx7wlwP/gXvCPOVX6u/qLaQHh+gHGKVbGHZWZJCbgWgHEy+mIL0MclHOZybqyH0=@vger.kernel.org X-Gm-Message-State: AOJu0YygBduFQD9IJNHJEWbewwbtkbwvoZ9kxbil2JTTdWw5MPWC7Lgt wpzi4B8C41Syy/l5/D1RWHuRYbMJrJuNmbQveemkerpf0t0BAPSpbjWo3xmXfxNvzTdngE9wXjM 978Ubm4+g5A== X-Received: from dlae25.prod.google.com ([2002:a05:701b:2319:b0:12d:c516:f51]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:61a:b0:12d:ce34:3fa0 with SMTP id a92af1059eb24-12dfd856b43mr3648296c88.32.1777866703246; Sun, 03 May 2026 20:51:43 -0700 (PDT) Date: Sun, 3 May 2026 20:51:25 -0700 In-Reply-To: <20260504035125.1851720-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260425020529.3246331-1-irogers@google.com> <20260504035125.1851720-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260504035125.1851720-5-irogers@google.com> Subject: [PATCH v3 4/4] perf test: Add inject ASLR test From: Ian Rogers To: acme@kernel.org, gmx@google.com, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new shell test `inject_aslr.sh` to verify the `perf inject --aslr` feature. The test covers: - Basic address remapping for user space samples. - Pipe mode coverage for `perf record` piped into `perf inject --aslr`. - Callchain address remapping. - Consistency of `perf report` output before and after injection. - Pipe mode report consistency. - Dropping of samples that leak ASLR info (physical addresses). - Kernel address remapping (skipping gracefully if permissions restrict recording the kernel map). - Kernel report consistency with address normalization. The test suite is hardened with: - Global 'set -o pipefail' pipeline checks to catch failures in perf script. - Safe awk processing loop closures that consume whole streams to avoid SIG= PIPE signal aborts. - False success assertions to verify callchain data isn't dynamically dropp= ed. - Graceful error paths on empty sample records. - Multi-arch support (32-bit and 64-bit address normalizations). Assisted-by: Gemini-CLI:Google Gemini 3 Signed-off-by: Ian Rogers --- v3: Harden script with pipefail, SIGPIPE awk pipeline fixes, callchain empty data asserts, baseline sample verification, and grep report abort protections. Ensure grep report filters have || true suffixes. Reorder set -e/pipefail to prevent stack leaks in mktemp failures. v2: Add sum comparison for kernel overhead and 32-bit math corrections. Add awk with gsub for trailing dots and brackets normalizations. Trap EXIT, prevent race conditions and avoid hardcoded perf binary. --- tools/perf/tests/shell/inject_aslr.sh | 423 ++++++++++++++++++++++++++ 1 file changed, 423 insertions(+) create mode 100755 tools/perf/tests/shell/inject_aslr.sh diff --git a/tools/perf/tests/shell/inject_aslr.sh b/tools/perf/tests/shell= /inject_aslr.sh new file mode 100755 index 000000000000..fa7dd3b4c411 --- /dev/null +++ b/tools/perf/tests/shell/inject_aslr.sh @@ -0,0 +1,423 @@ +#!/bin/bash +# perf inject --aslr test +# SPDX-License-Identifier: GPL-2.0 + +set -e +set -o pipefail + +shelldir=3D$(dirname "$0") +# shellcheck source=3Dlib/perf_has_symbol.sh +. "${shelldir}"/lib/perf_has_symbol.sh + +sym=3D"noploop" + +skip_test_missing_symbol ${sym} + +# Create global temp directory +temp_dir=3D$(mktemp -d /tmp/perf-test-aslr.XXXXXXXXXX) +data=3D"${temp_dir}/perf.data" +data2=3D"${temp_dir}/perf.data2" + +prog=3D"perf test -w noploop" +[ "$(uname -m)" =3D "s390x" ] && prog=3D"$prog 3" +err=3D0 + + + +cleanup() { + # Check if temp_dir is set and looks sane before removing + if [[ "${temp_dir}" =3D~ ^/tmp/perf-test-aslr\. ]]; then + rm -rf "${temp_dir}" + fi +} + +trap_cleanup() { + cleanup + exit 1 +} + +trap cleanup EXIT +trap trap_cleanup TERM INT + +get_noploop_addr() { + local file=3D$1 + perf script -i "$file" | awk ' + BEGIN { found=3D0 } + { + for (i=3D1; i<=3DNF; i++) { + if ($i ~ /noploop\+/) { + if (!found) { + print $(i-1) + found=3D1 + } + } + } + }' +} + +test_basic_aslr() { + echo "Test basic ASLR remapping" + local data + data=3D$(mktemp "${temp_dir}/perf.data.basic.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.basic.XXXXXX") + + perf record -e task-clock:u -o "${data}" ${prog} + perf inject -v --aslr -i "${data}" -o "${data2}" + + orig_addr=3D$(get_noploop_addr "${data}") + new_addr=3D$(get_noploop_addr "${data2}") + + echo "Basic ASLR: orig_addr=3D$orig_addr, new_addr=3D$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Basic ASLR test [Failed - no noploop samples in original file]" + err=3D1 + elif [ -z "$new_addr" ]; then + echo "Basic ASLR test [Failed - could not find remapped address]" + err=3D1 + elif [ "$orig_addr" =3D "$new_addr" ]; then + echo "Basic ASLR test [Failed - addresses are not remapped]" + err=3D1 + else + echo "Basic ASLR test [Success]" + fi +} + +test_pipe_aslr() { + echo "Test pipe mode ASLR remapping" + local data + data=3D$(mktemp "${temp_dir}/perf.data.pipe.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.pipe.XXXXXX") + + # Use tee to save the original pipe data for comparison + perf record -e task-clock:u -o - ${prog} | tee "${data}" | perf inject -= -aslr -o "${data2}" + + orig_addr=3D$(get_noploop_addr "${data}") + new_addr=3D$(get_noploop_addr "${data2}") + + echo "Pipe ASLR: orig_addr=3D$orig_addr, new_addr=3D$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Pipe ASLR test [Failed - no noploop samples in original file]" + err=3D1 + elif [ -z "$new_addr" ]; then + echo "Pipe ASLR test [Failed - could not find remapped address]" + err=3D1 + elif [ "$orig_addr" =3D "$new_addr" ]; then + echo "Pipe ASLR test [Failed - addresses are not remapped]" + err=3D1 + else + echo "Pipe ASLR test [Success]" + fi +} + +test_callchain_aslr() { + echo "Test Callchain ASLR remapping" + local data + data=3D$(mktemp "${temp_dir}/perf.data.callchain.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.callchain.XXXXXX") + + perf record -g -e task-clock:u -o "${data}" ${prog} + perf inject --aslr -i "${data}" -o "${data2}" + + orig_addr=3D$(get_noploop_addr "${data}") + new_addr=3D$(get_noploop_addr "${data2}") + + echo "Callchain ASLR: orig_addr=3D$orig_addr, new_addr=3D$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Callchain ASLR test [Failed - no noploop samples in original fil= e]" + err=3D1 + elif [ -z "$new_addr" ]; then + echo "Callchain ASLR test [Failed - could not find remapped address]" + err=3D1 + elif [ "$orig_addr" =3D "$new_addr" ]; then + echo "Callchain ASLR test [Failed - addresses are not remapped]" + err=3D1 + else + # Extract callchain addresses (indented lines starting with hex addres= ses) + orig_callchain=3D$(perf script -i "${data}" | awk '/^[[:space:]]+[0-9a= -f]+/ {print $1}') + new_callchain=3D$(perf script -i "${data2}" | awk '/^[[:space:]]+[0-9a= -f]+/ {print $1}') + + if [ -z "$orig_callchain" ]; then + echo "Callchain ASLR test [Failed - no callchain samples in original= file]" + err=3D1 + elif [ -z "$new_callchain" ]; then + echo "Callchain ASLR test [Failed - callchain data was dropped]" + err=3D1 + elif [ "$orig_callchain" =3D "$new_callchain" ]; then + echo "Callchain ASLR test [Failed - callchain addresses were not rem= apped]" + err=3D1 + else + echo "Callchain ASLR test [Success]" + fi + fi +} + +test_report_aslr() { + echo "Test perf report consistency" + local data + data=3D$(mktemp "${temp_dir}/perf.data.report.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.report.XXXXXX") + local data_clean + data_clean=3D$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + + perf record -e task-clock:u -o "${data}" ${prog} + # Use -b to inject build-ids and force ordered events processing in both + perf inject -b -i "${data}" -o "${data_clean}" + perf inject -v -b --aslr -i "${data}" -o "${data2}" + + local report1=3D"${temp_dir}/report1" + local report2=3D"${temp_dir}/report2" + local report1_clean=3D"${temp_dir}/report1.clean" + local report2_clean=3D"${temp_dir}/report2.clean" + local diff_file=3D"${temp_dir}/diff" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${data2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' | sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "Report ASLR test [Failed - no samples captured]" + err=3D1 + elif [ -s "${diff_file}" ]; then + echo "Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=3D1 + else + echo "Report ASLR test [Success]" + fi +} + +test_pipe_report_aslr() { + echo "Test pipe mode perf report consistency" + local data + data=3D$(mktemp "${temp_dir}/perf.data.pipe_report.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.pipe_report.XXXXXX") + local data_clean + data_clean=3D$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + + # Use tee to save the original pipe data, then process it with inject -b + perf record -e task-clock:u -o - ${prog} | \ + tee "${data}" | \ + perf inject -b --aslr -o "${data2}" + perf inject -b -i "${data}" -o "${data_clean}" + + local report1=3D"${temp_dir}/report1" + local report2=3D"${temp_dir}/report2" + local report1_clean=3D"${temp_dir}/report1.clean" + local report2_clean=3D"${temp_dir}/report2.clean" + local diff_file=3D"${temp_dir}/diff" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${data2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' | sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "Pipe Report ASLR test [Failed - no samples captured]" + err=3D1 + elif [ -s "${diff_file}" ]; then + echo "Pipe Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=3D1 + else + echo "Pipe Report ASLR test [Success]" + fi +} + +test_dropped_samples() { + echo "Test dropped samples (phys-data)" + local data + data=3D$(mktemp "${temp_dir}/perf.data.dropped.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.dropped.XXXXXX") + + # Check if --phys-data is supported by recording a short run + if ! perf record -e task-clock:u --phys-data -o "${data}" -- sleep 0.1 >= /dev/null 2>&1; then + echo "Skipping dropped samples test as --phys-data is not supported" + return + fi + + perf record -e task-clock:u --phys-data -o "${data}" ${prog} + perf inject --aslr -i "${data}" -o "${data2}" + + # Verify that the original file actually contained samples! + orig_samples=3D$(perf script -i "${data}" | wc -l) + if [ "$orig_samples" -eq 0 ]; then + echo "Dropped samples test [Failed - no samples in original file]" + err=3D1 + else + # Verify that samples are dropped. + samples_count=3D$(perf script -i "${data2}" | wc -l) + + if [ "$samples_count" -gt 0 ]; then + echo "Dropped samples test [Failed - samples were not dropped]" + err=3D1 + else + echo "Dropped samples test [Success]" + fi + fi +} + +test_kernel_aslr() { + echo "Test kernel ASLR remapping" + local kdata + kdata=3D$(mktemp "${temp_dir}/perf.data.kernel.XXXXXX") + local kdata2 + kdata2=3D$(mktemp "${temp_dir}/perf.data2.kernel.XXXXXX") + local log_file + log_file=3D$(mktemp "${temp_dir}/kernel_record.log.XXXXXX") + + # Try to record kernel samples + if ! perf record -e task-clock:k -o "${kdata}" ${prog} > "${log_file}" 2= >&1; then + echo "Skipping kernel ASLR test as recording failed (maybe no permissi= ons)" + return + fi + + # Check for warning about kernel map restriction + if grep -q "Couldn't record kernel reference relocation symbol" "${log_f= ile}"; then + echo "Skipping kernel ASLR test as kernel map could not be recorded (p= ermissions restricted)" + return + fi + + perf inject -v --aslr -i "${kdata}" -o "${kdata2}" + + # Check if kernel addresses are remapped. + # Find the field that ends with :k: (the event name) and take the next f= ield! + orig_addr=3D$(perf script -i "${kdata}" | awk ' + BEGIN { found=3D0 } + { + for (i=3D1; i "${log_file}" 2= >&1; then + echo "Skipping kernel report test as recording failed (maybe no permis= sions)" + return + fi + + # Check for warning about kernel map restriction + if grep -q "Couldn't record kernel reference relocation symbol" "${log_f= ile}"; then + echo "Skipping kernel report test as kernel map could not be recorded = (permissions restricted)" + return + fi + + # Use -b to inject build-ids and force ordered events processing in both + perf inject -b -i "${kdata}" -o "${data_clean}" + perf inject -v -b --aslr -i "${kdata}" -o "${kdata2}" + + local report1=3D"${temp_dir}/report_kernel1" + local report2=3D"${temp_dir}/report_kernel2" + local report1_clean=3D"${temp_dir}/report_kernel1.clean" + local report2_clean=3D"${temp_dir}/report_kernel2.clean" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${kdata2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' > "${report2_clean}" || true + + # Normalize kernel DSOs and addresses in clean reports + # This allows kernel modules to be either a module or kernel.kallsyms + local report1_norm=3D"${temp_dir}/report_kernel1.norm" + local report2_norm=3D"${temp_dir}/report_kernel2.norm" + + awk '{gsub(/\[[a-zA-Z0-9_.-]{2,}\](\.[a-zA-Z0-9_]+)?/, "[kernel]", $0); \ + print}' "${report1_clean}" > "${report1_norm}" + awk '{gsub(/\[[a-zA-Z0-9_.-]{2,}\](\.[a-zA-Z0-9_]+)?/, "[kernel]", $0); \ + print}' "${report2_clean}" > "${report2_norm}" + + # Calculate sum of percentages for [kernel] samples + sum1=3D$(awk '/\[kernel\]/ {sum +=3D $1} END {print sum}' "${report1_nor= m}") + sum2=3D$(awk '/\[kernel\]/ {sum +=3D $1} END {print sum}' "${report2_nor= m}") + + echo "Kernel report sums: sum1=3D$sum1, sum2=3D$sum2" + + # Compare sums with tolerance + if [ -z "$sum1" ] || [ -z "$sum2" ]; then + echo "Kernel Report ASLR test [Failed - no kernel data]" + err=3D1 + elif awk -v s1=3D"$sum1" -v s2=3D"$sum2" \ + 'BEGIN {diff =3D s1 - s2; if (diff < 0) diff =3D -diff; if (diff < 0.0= 5) exit 0; else exit 1}'; then + echo "Kernel Report ASLR test [Success]" + else + echo "Kernel Report ASLR test [Failed - sums differ too much]" + err=3D1 + fi +} + +test_basic_aslr +test_pipe_aslr +test_callchain_aslr +test_report_aslr +test_pipe_report_aslr +test_dropped_samples +test_kernel_aslr +test_kernel_report_aslr + +cleanup +exit $err --=20 2.54.0.545.g6539524ca2-goog