From patchwork Tue Feb 27 18:48:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776271 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308248wro; Tue, 27 Feb 2024 10:49:10 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUAGDOrhlvZ/SrCBF9nQxQ3KEUDOIrbCfDBZJb/yV8U4IfFLzcociUJ37MzFslO03bE1KD0+IuSYKPVb0FpsRLu X-Google-Smtp-Source: AGHT+IHBtCQZg818AMMANNeftK4y/duXLYh4MWMrhsadER728kvaTzgDihDVpTWmlvx1DK0FPubZ X-Received: by 2002:a0c:8bc9:0:b0:68f:5fd5:80cd with SMTP id a9-20020a0c8bc9000000b0068f5fd580cdmr2652597qvc.6.1709059749970; Tue, 27 Feb 2024 10:49:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059749; cv=none; d=google.com; s=arc-20160816; b=VXh8BMtdCkR9z55q7JNfmRHYOmP6rBV+cl64c32fRuLa/6hHyGqTXpkQ2BpnuxRDxi 3K3+ddicwT9N3wejLyfOA3XZB0IXdzgKW3CijSYaQo4ruWp6YTh2x2GK9Xedrcyvwiyd eq1QaRbzgiFfhClALbObteEaIH381RQ3K6LV1xVdoV3A5PUzUm9prUq2BGO/WKrnOUhL 5E+Qj96+IeMimlVFo2Q+m91LbfXN7BIbl1M89bz5WnOiZezjXNu5SQC32ZIiKwPVbM9V 1Ml9CCvueY1J0BovGWR63unoBYJjU1pgnFlnujPkZZpLNVoMHmt1TArOAtHfnB5aRIAy ihDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=jYBkGoMMC9wjPymAcLzdiqwt32NykwjmDLE/jmy3uYA=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=nwiFvPypPaRhQsiS8mdI3F2I03iAce+twiV8urPAA4j1qlX5G/m88PS4EU5kv0+8g3 L4pdgeuUA8BC2L/RW9/xtNTk0AwZEZkxZe7Goe9CtiBWZGjmIw6lg7GKfSTMPhijI+sD VSt1mRccsspGgrCE8g/wyQlXvCcHi0DCYG48aclVbawqVe3LOicnZbhcEKq9lP4d/979 34J8Lo5cJnOX6/gNyH/MQY3WsG7zA/x395skL0MRA6nIKUUgnIRuYZwTElxTNorCAnld 2qoWbxfmD61g9g5gAvZmK0+BhURV2hGW5H7q3aPVMZJ0SttWzDT0mJC6xWQgZ1SUuD00 kLPQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PTWOgijn; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id if4-20020a0562141c4400b0068f761b112esi8230165qvb.357.2024.02.27.10.49.09 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:49:09 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PTWOgijn; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VO-0006Ua-Ih; Tue, 27 Feb 2024 13:48:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VN-0006U2-5h for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:41 -0500 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VL-0001ST-JZ for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:40 -0500 Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-6e4957ff05cso1621046a34.2 for ; Tue, 27 Feb 2024 10:48:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059718; x=1709664518; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jYBkGoMMC9wjPymAcLzdiqwt32NykwjmDLE/jmy3uYA=; b=PTWOgijnVwgQfa5nMUkRjVR5K3R0he76rfrErF9KisHjaKTDaJFh0Z42IDGsZab/8M BVuQaio33mtUA9L2XCBiqJePIWohU8mGEmzK+Sm77RCFuexvrJmbfh5tg1MwJWviOU9W k1XlgdZQbJ5/X7Sn1CZLHbc3/x0bXVwzaOkAEEj5UGRtDDQHskp+t57GaMJUMyCRqAhv j4i0r8zHJu9hb6ljI3+LcQPynMMfk7mqmwSpLj8/xItp69QK/ZjVQFfJcvl320KU4q01 RBLFAfyQZsTwtGkc+JJJs0bhJFxxZTvshPwlCvJ66qLstYjC022lu8cp5GkAGUoTA0jP d30w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059718; x=1709664518; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jYBkGoMMC9wjPymAcLzdiqwt32NykwjmDLE/jmy3uYA=; b=NMqiNn9gC5p7+XMhCyXqd2KsL8XekO4/d9ya1k+0SJwjSLrxIbY80ft2fi0rEUiSJN fxF8ojgPZw0bgqa6QcD/SkGj1AOfJeB82TW0Fo93jsB07JXj7u9kWvkeQ6KhWplm4Mc6 sk245Dmr7biXOlDMuy00HL+yh5JF5RkQLx7UN9njatm/bUQgYNZFj0zldHskV22xIN/7 CJv4FSxoYjDrgrD8Cr0mdy49qTnc8zjUQuY/cx+YZDDWg3XRtjvTCJ4ztFsTizO0qOOV //0MnW5xBDgtkiODX2QToMds5RWKkPWXDmAH5PJl6SgiQ6I157tGkPSmrMFzXMv2bweV 35lQ== X-Gm-Message-State: AOJu0YyZo3sAnL6lT9hqFhNY5NdA2QJvayKg+rzde6j5m7D7WrvzLEeR irizTmGkV35R9LESl7VkyVXH1SumdCqF9b8Pf4yW12su2KZaFia7osksaQ53Q98VKDFkzAXiN/v j X-Received: by 2002:a05:6830:1390:b0:6e4:7a7b:700b with SMTP id d16-20020a056830139000b006e47a7b700bmr10732280otq.14.1709059718356; Tue, 27 Feb 2024 10:48:38 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:37 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 01/14] linux-user/elfload: Disable core dump if getrlimit fails Date: Tue, 27 Feb 2024 08:48:20 -1000 Message-Id: <20240227184833.193836-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::333; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x333.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Do not dump core at all if getrlimit fails; this ensures that dumpsize is valid throughout the function, not just for the initial test vs rlim_cur. Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index b8eef893d0..fb47fe39c9 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4673,7 +4673,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) return 0; } - if (getrlimit(RLIMIT_CORE, &dumpsize) == 0 && dumpsize.rlim_cur == 0) { + if (getrlimit(RLIMIT_CORE, &dumpsize) < 0 || dumpsize.rlim_cur == 0) { return 0; } From patchwork Tue Feb 27 18:48:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776276 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308642wro; Tue, 27 Feb 2024 10:50:14 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVhsLx1CyH6vBBQBqaiYURy7aPJUZ+Zz70uDNg0skwgUgWmPIYshzeG2Q1fni1pU2LATpgcGGbkkxbzxCJeTaVT X-Google-Smtp-Source: AGHT+IEW7bpR3BtomG8L6kJ4MxIMcL8N6j7Ik0CvHMTB7moSqgbtKjOZbIXOs4iLW6ctPeXPon+K X-Received: by 2002:a25:86c5:0:b0:dcf:b5ba:1403 with SMTP id y5-20020a2586c5000000b00dcfb5ba1403mr433121ybm.6.1709059814440; Tue, 27 Feb 2024 10:50:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059814; cv=none; d=google.com; s=arc-20160816; b=Y6XiURCJzrk35Q+NKtq0p/sCb3rz0zmFEhdNmz2AWtyBRuG7pfGJYItsSvpgqWuIRj dGHCU3PHfSiHbCAmSKQQ+NYvidKyQuXgpPI+0DZFiYVWSO//BjwpXLmsc3CBB2wXcu3y KP4WlRaaGWW6AfdzDybjF0aP7le5xC4oYoZU28xxzZPl4OIi+dOFG8DXGJUuwW5+hf0t AAV63IU4/gBZRTwVYAzw9Yo10lF5EBAV8CnZejzStgpN0Vs94lvG+/lG7uxvOXN+x1uy /Y9kkT406EFdEkoSv8CuK2S0fvsRr84sIIu5jDXJ0SEP+338oSKKlLTdxpy9VlDhI0tn sOVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MxsvOR0/a5Fiag9KhwB5MSJsMkkn1D/1jCzIg2T94l4=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=o99Z2uVAZb+900/YEW/yZLdStPeZyBknlCmCKhrgnle7zalcxhp804vqdZxoIRWHVl LiZHpG1ZXXsw+Jb9QgIvWPqJ1lCd7sF+mrgO1ObYekba/qXeAT5d/hUT6Q2oyVIg5Dmh /NMNHYg4vswvK4wuzkXAi0avWMaNodv/AWrEkcbshIc0lcwLavRdO9Gp2mmdw2FVLPgW qx8XcIMJLwtWtO5+NYhE0JkjKoPDoAMSg8JyzZoWof9Hy6gxJA+aejT/S6TSlF02YhlE 25VVB+AKkh9h8kMviIeU4GVq8bfPvTbwQm8nuoQqcPq5/qodvrt7bqWb+jZEtU84wGZX L1rg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J65mZqXG; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id t5-20020a05622a148500b0042e7f75e01esi6326407qtx.565.2024.02.27.10.50.14 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:50:14 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J65mZqXG; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VQ-0006VY-4F; Tue, 27 Feb 2024 13:48:44 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VO-0006UZ-EL for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:42 -0500 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VM-0001Sg-S5 for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:42 -0500 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-6e4670921a4so2609175b3a.0 for ; Tue, 27 Feb 2024 10:48:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059719; x=1709664519; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MxsvOR0/a5Fiag9KhwB5MSJsMkkn1D/1jCzIg2T94l4=; b=J65mZqXGCpH6CmPVl5mSGPssxRy4nwkWCiLpbJr547qkUFc6CEXd+sUxbB+KyZfdd3 dRqxp9VjFwkWajQfO/oprV8xJ6nwj25Tl8a6GKDqX62N1nVnYNC3yyhuoW0wug6+zPM4 qj3AHck2CLX7MjLF+d+tvBrRW6864cSBT6htUANIXigzVfMU2Jik62EpIRwM/hKdm/7l d46jr+ywRKgiFG8NNdxv5WRLGmUgIr5Mym3it+RZPKK97R0jHCX2FmfLcIDEylseMRRB RbRRH6Lj11TWIwR7ImfZX8xYB67yypiEtepN5rijz8F9gbpbDKraT6uKg99VMNpageVs Niiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059719; x=1709664519; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MxsvOR0/a5Fiag9KhwB5MSJsMkkn1D/1jCzIg2T94l4=; b=Duot5BLtyZyvrqHbp7/+EWZmgIRPMDfTv+xOaG+Qkci71V6XSLNxJksx73TH52jeeV 1u1P8ubxfT8oY3CHcu7sTDiynNHcjxShA8B+xJHojOMWfNUyvSs/HskuFLqV02L6Y4Ih p/uVwD7NqAp2oeUYEGmBU6QmdeiVVZAGB3yiy57ew50JKMmB92ikUi4eURqRXefPuP8R t6vI32xlvwXriaLGZyNcbEQlJucGq3rurWTg1S31gnIE/WRlMJNRKt/b6XM7P80pVWk2 DyO1fRxBZ7jkodfTNMy8ftgIF64JH3KLehxKvKDXUWbqfgJfB+glQuCEfYnpm9jDcTDU sv5Q== X-Gm-Message-State: AOJu0YxgpdgEw4NO0MLoW+/hRrS/MgoJXde76sTbABd+kzJF2LcdNYYI N09O+/v5peLCs1gaC1QLWdtA7DS3MrMI9wU2b1CjUv90/QMyLVzQd6j1KSFRuAbyhiBSy2HRkPT h X-Received: by 2002:a05:6a00:4fd5:b0:6e5:109b:959 with SMTP id le21-20020a056a004fd500b006e5109b0959mr8672528pfb.25.1709059719552; Tue, 27 Feb 2024 10:48:39 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:39 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 02/14] linux-user/elfload: Merge init_note_info and fill_note_info Date: Tue, 27 Feb 2024 08:48:21 -1000 Message-Id: <20240227184833.193836-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::433; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x433.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index fb47fe39c9..7b3a2c20f2 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4514,16 +4514,6 @@ static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env info->notes_size += note_size(&ets->notes[0]); } -static void init_note_info(struct elf_note_info *info) -{ - /* Initialize the elf_note_info structure so that it is at - * least safe to call free_note_info() on it. Must be - * called before calling fill_note_info(). - */ - memset(info, 0, sizeof (*info)); - QTAILQ_INIT(&info->thread_list); -} - static int fill_note_info(struct elf_note_info *info, long signr, const CPUArchState *env) { @@ -4532,6 +4522,9 @@ static int fill_note_info(struct elf_note_info *info, TaskState *ts = (TaskState *)cpu->opaque; int i; + memset(info, 0, sizeof (*info)); + QTAILQ_INIT(&info->thread_list); + info->notes = g_new0(struct memelfnote, NUMNOTES); if (info->notes == NULL) return (-ENOMEM); @@ -4665,8 +4658,6 @@ static int elf_core_dump(int signr, const CPUArchState *env) int segs = 0; int fd = -1; - init_note_info(&info); - errno = 0; if (prctl(PR_GET_DUMPABLE) == 0) { From patchwork Tue Feb 27 18:48:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776285 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp309168wro; Tue, 27 Feb 2024 10:51:36 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXrkEaxbrBtbDw8wmWYAVZ067Xch2fAsEqqoJrugOjLbE5QwJIU7UJiHkCb3cxJRzwkjzRd8T2MjvyEoYVaHo8Q X-Google-Smtp-Source: AGHT+IFdOgSR4PvHxVhpUX0b3EfRHcre2FXVQBmoZjfenXN51PryX+SKzu2DoaaRx9osWmfgZSaz X-Received: by 2002:a05:6808:220d:b0:3c1:931e:9aed with SMTP id bd13-20020a056808220d00b003c1931e9aedmr3540649oib.36.1709059895901; Tue, 27 Feb 2024 10:51:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059895; cv=none; d=google.com; s=arc-20160816; b=lxPtV+KsCbISSlwQ+CwCppMGkEvGIkdh9FxXM39xkgb3JUXdFPkXdRGwIL1enRcEoJ xb8qDVRUTtYO5qKeyfQX6q4UNwCSXDRRetEMj3oKvnFms5uFgaVhjGvhjSF8F/m8KQYL o922mrUXQwpXFvAJqMZ/Q8+qijzBppwI9NSZn9OUBMvYmztzvVVdySR/urGoOxnTfvbS yyBJ2SY4ckNT3i5pPIjrEJjapPIr0uIydBETGWE+fTPd0DDRFQ5n1w0xYfyJzNPDeAZO 0RtXMHZD35teDrSFmYPiWU0Xv1bxyCHuWkRFjVjagFkIG4XLnqj16E1DxxIC68lGybvu uu7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=PXNbhyM3M1ZvaxsPU8XRtEyhAyhYazBaocnAfgHjiNM=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=QaNoWBxcjtR5TxE2RSeEDS7TLOO2kuknQw6n3bqDeXvNYAJhTToSfeA6ptCbQoUMUI JWadYgPqlkxbCzwQQXkXATQl4XTRTcHyxfILnzcYhS4otFriu5kAJYa7v/gGJD9aqCE2 SP5aCbTJHknETFkmv22wdwhN2rvdrgvTyrYHZzBK/W5vvFw5iXqLjhq6Tuw2Jcc2r7Q0 57FFTNbBtNzqh9qizaHCffxq2946O+LLvF/8eHuSqFmrpjeTK/V5g8r8aheR/G+s8VXw 0ZPVQlnV6u+6QEXcVJXWXuO4RUx5t8VDEwOCjhGZgD0X5pDc0+tBJunbTqUE9oqgDcGY Bqlw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tZKCD9Ec; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id h5-20020a05620a244500b007873e889546si8977106qkn.129.2024.02.27.10.51.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:51:35 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tZKCD9Ec; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VR-0006WC-Jy; Tue, 27 Feb 2024 13:48:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VQ-0006VZ-4p for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:44 -0500 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VO-0001T1-Dm for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:43 -0500 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6e4ca46ab04so2862653b3a.3 for ; Tue, 27 Feb 2024 10:48:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059721; x=1709664521; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PXNbhyM3M1ZvaxsPU8XRtEyhAyhYazBaocnAfgHjiNM=; b=tZKCD9EcLCeJpd6jK0rsk/Llmj4h6oZbe6kdgW7K+BDGg1KAGtyznYuZKjs9LMVTtT UOFi/z/ceoQfLg1S4U+JlnV18R6WHmhflANj1sRjUNQXsHmhvWX648AKT45ty9KrupLJ hG+O9SiHsWUxpbZes47/CTPr2rv34kjB5YSFMguxEQZGkLf8bl5AAZm0Lh47Pfp9w8xh g2/h53CBS6pP+fEG7Hf7DLGNGH0KlDyhWSHRv1hXuXvRfVueOHe8yaEdSGRFecMPB5Dz CjWkcUGcbRIo5UmH5rs8byL4YlrPTwMfcqs7b8sY3jFWCeKa0EMo/gQdOcPKX+CW8p4G 3NPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059721; x=1709664521; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PXNbhyM3M1ZvaxsPU8XRtEyhAyhYazBaocnAfgHjiNM=; b=lzKJyFyt/BSZA7veABBLR2J+HmjQHIkuyQn6FByiZx+9hn2eWPScjptxI06ZWBLJy7 WQvTysdOXMHKTSqDyAtRqSo9nC7lsjbcBtw4CRh8RuEht9snJamS1DXlJWgujdNHVsyN nZ60nlJInxV17t6fiJGX0Ca5RtM1Bo8fgayu/bAPMcOvau3rIXv3DtfOl9ynHcy/QJv6 pm6f42SqpOAGy7g9kB1iDIZDxtTPyiJjihQj3qmuajhWvwtzcJEBEXHGuS+J/ydSjic4 dfhdzhW23OLqvnQYBsaBQX0pDDlNK0o8a1mk3LKCHEi96oNKyKJokv9eK6zJ7J5Z6Nln nohg== X-Gm-Message-State: AOJu0YyU33+/LNfZXjrbcrVk+kTXQNQ1U8M1q7d+RJ3Os69OxwEczHCK 8fwvceb8ZrexgywKf1+wbWyh/QE5MfvcuJpeFKEFByYW4V9I4iACXA3ZWsjPaHthvsTvV3JQAdQ / X-Received: by 2002:a05:6a00:ce:b0:6e5:2cc9:c898 with SMTP id e14-20020a056a0000ce00b006e52cc9c898mr7190892pfj.26.1709059720912; Tue, 27 Feb 2024 10:48:40 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:40 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 03/14] linux-user/elfload: Tidy fill_note_info and struct elf_note_info Date: Tue, 27 Feb 2024 08:48:22 -1000 Message-Id: <20240227184833.193836-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::436; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x436.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org In fill_note_info, there were unnecessary checks for success of g_new/g_malloc. But these structures do not need to be dyamically allocated at all, and can in fact be statically allocated within the parent structure. This removes all error paths from fill_note_info, so change the return type to void. Change type of signr to match both caller (elf_core_dump) and callee (fill_prstatus), which both use int for signr. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 48 +++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 7b3a2c20f2..cc43487a37 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4066,10 +4066,12 @@ struct elf_thread_status { int num_notes; }; +#define NUMNOTES 3 + struct elf_note_info { - struct memelfnote *notes; - struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */ - struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */ + struct memelfnote notes[NUMNOTES]; + struct target_elf_prstatus prstatus; /* NT_PRSTATUS */ + struct target_elf_prpsinfo psinfo; /* NT_PRPSINFO */ QTAILQ_HEAD(, elf_thread_status) thread_list; #if 0 @@ -4117,7 +4119,7 @@ static void fill_auxv_note(struct memelfnote *, const TaskState *); static void fill_elf_note_phdr(struct elf_phdr *, int, off_t); static size_t note_size(const struct memelfnote *); static void free_note_info(struct elf_note_info *); -static int fill_note_info(struct elf_note_info *, long, const CPUArchState *); +static void fill_note_info(struct elf_note_info *, int, const CPUArchState *); static void fill_thread_info(struct elf_note_info *, const CPUArchState *); static int dump_write(int, const void *, size_t); @@ -4514,44 +4516,33 @@ static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env info->notes_size += note_size(&ets->notes[0]); } -static int fill_note_info(struct elf_note_info *info, - long signr, const CPUArchState *env) +static void fill_note_info(struct elf_note_info *info, + int signr, const CPUArchState *env) { -#define NUMNOTES 3 CPUState *cpu = env_cpu((CPUArchState *)env); TaskState *ts = (TaskState *)cpu->opaque; - int i; memset(info, 0, sizeof (*info)); QTAILQ_INIT(&info->thread_list); - info->notes = g_new0(struct memelfnote, NUMNOTES); - if (info->notes == NULL) - return (-ENOMEM); - info->prstatus = g_malloc0(sizeof (*info->prstatus)); - if (info->prstatus == NULL) - return (-ENOMEM); - info->psinfo = g_malloc0(sizeof (*info->psinfo)); - if (info->prstatus == NULL) - return (-ENOMEM); - /* * First fill in status (and registers) of current thread * including process info & aux vector. */ - fill_prstatus(info->prstatus, ts, signr); - elf_core_copy_regs(&info->prstatus->pr_reg, env); + fill_prstatus(&info->prstatus, ts, signr); + elf_core_copy_regs(&info->prstatus.pr_reg, env); fill_note(&info->notes[0], "CORE", NT_PRSTATUS, - sizeof (*info->prstatus), info->prstatus); - fill_psinfo(info->psinfo, ts); + sizeof(info->prstatus), &info->prstatus); + fill_psinfo(&info->psinfo, ts); fill_note(&info->notes[1], "CORE", NT_PRPSINFO, - sizeof (*info->psinfo), info->psinfo); + sizeof(info->psinfo), &info->psinfo); fill_auxv_note(&info->notes[2], ts); info->numnote = 3; info->notes_size = 0; - for (i = 0; i < info->numnote; i++) + for (int i = 0; i < info->numnote; i++) { info->notes_size += note_size(&info->notes[i]); + } /* read and fill status of all threads */ WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock) { @@ -4562,8 +4553,6 @@ static int fill_note_info(struct elf_note_info *info, fill_thread_info(info, cpu_env(cpu)); } } - - return (0); } static void free_note_info(struct elf_note_info *info) @@ -4575,10 +4564,6 @@ static void free_note_info(struct elf_note_info *info) QTAILQ_REMOVE(&info->thread_list, ets, ets_link); g_free(ets); } - - g_free(info->prstatus); - g_free(info->psinfo); - g_free(info->notes); } static int write_note_info(struct elf_note_info *info, int fd) @@ -4694,8 +4679,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) goto out; /* fill in the in-memory version of notes */ - if (fill_note_info(&info, signr, env) < 0) - goto out; + fill_note_info(&info, signr, env); offset += sizeof (elf); /* elf header */ offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */ From patchwork Tue Feb 27 18:48:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776284 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp309107wro; Tue, 27 Feb 2024 10:51:22 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUoa8hO0Is+yf6pqg80F7oV6vCBcdXRzK8jP+1cFrswLE80iscU+43CIhhnt5Po3oen8GqP7I+1fYQeMel51cm2 X-Google-Smtp-Source: AGHT+IEd7tW7hfENBS5elUb5ALzzhgWX55ajHR9bRW7Qji3kW9HqKcBVEA9XNvK/VmQaLbOknQfm X-Received: by 2002:a05:622a:1755:b0:42e:80bb:f25b with SMTP id l21-20020a05622a175500b0042e80bbf25bmr8659320qtk.60.1709059882268; Tue, 27 Feb 2024 10:51:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059882; cv=none; d=google.com; s=arc-20160816; b=wsWST/E3bcZ7Oa/QdDEnlvfPKG5eFV6YC/bilHm9EwGYt8GT3fV64v24kSINeXtaNX NHB4U8DhUm+kq5s4LBs85z1HmSFL+U0XFQhmZPcBDkmzhiP+hwalugMapVaW7USgT7EI /uRsaKsQ+XFqf/SY0QCO/0rhda4lhtvg5oN3pQi0i68seuZyJvg2k3yaeKG8tmDqiMzN 97WsoIJdyFV6TLNFfJbs4wylY9o703X8fypO60FdJI7+PU3ICGLFeNHINoZUAvkMAUs3 /eS4rjVDQgupvF+WFJgLlHYlBKvvOax98S/WGgy6ItSUyufTrHCZ9rpJ8OZC8L73ryVw XMSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=keqqLzVNAo4uH25B4sgF/a/mk1kxuvixDsagaZwoXEA=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=UiB3Ag9nhDJe7kYob81vWT1R7gt65nWOMR1MvCnuDAGIvf3NIQ/wGG0/8jNRggfief W/2dAQMwHBAHDnOloDo89g9ewxEWMTN9oE+vJelK1EJHCWqAeh9saQZ02LM2//kB4wUt sTXJ52m9DM2tYNoWCkK00A/Jt1JzhKmBjCYbzPc0HOEJU43krUygwtZoSMpl3qFIeXDQ OJqQUepfniTd3x4S9sVlH24TDU/jIQFzISBT5hEaFj1JooPJRvRBWVaRo41jQ4MmZFba 5fyADni1rN2NjDa+ODe8opbnlghW8Hu4Z5MsvNPhlBXf9kESfYWsoPyDqR4wjOnldClN 6+PA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=OxK0o86D; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id k18-20020ac85fd2000000b0042e816dce4asi6174150qta.506.2024.02.27.10.51.22 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:51:22 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=OxK0o86D; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VT-0006WZ-8N; Tue, 27 Feb 2024 13:48:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VR-0006Vy-83 for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:45 -0500 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VP-0001TM-LJ for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:44 -0500 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6da202aa138so3146822b3a.2 for ; Tue, 27 Feb 2024 10:48:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059722; x=1709664522; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=keqqLzVNAo4uH25B4sgF/a/mk1kxuvixDsagaZwoXEA=; b=OxK0o86DootcJA/PGAIEqljBMZ2pcp5uozj74eTAIIF7l8CDGiujiSMd7d0rKtfGGW 8hLBmHrFV8r+wqEcV8LamQm/8isQ3IjYM/lzhfYZ05oEoenj6IQvCufH+HcFRKgjYY0F K6l9rPw9kjj3nDVj9NcfHIYzRq6wLezNaXVO33NG5jxgA6A2NcirkOsYJEAsoy3BfQSv hF7OJ2/EGRx4mFEFS6HRUwl3sH5iPdu14oimDHcyIYC/UVXcpmokFTAHkScktD/hVDlF FXbhLdcLlNvzuuqjZfy8fstYm/KsyEGy81g22kZgoPD451NEi3N7LMA68tnZcpD4rfv+ 384A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059722; x=1709664522; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=keqqLzVNAo4uH25B4sgF/a/mk1kxuvixDsagaZwoXEA=; b=bLBybGEm0OF3BSCZUfz5/fBIPPktopZoh0hiyTqmId5lGtYeLKCc2lFR/uezBos2Q8 tT3n3rO5yBNwk7S/lsp6liB2l7gtbveRH8ouH1uFAofyYL7k6CNQ5B/A+mH/ltFWILCa fAaft0bbXi4aKPVcZ5QX6+LoW9clqJDCKggtIYoRmDiFLs/wTZae15LndobPd1yN3EcP /Qb+fZ+LAC9DdUfIP8FV+jQuRGAG4mVCS4quLt0ZbUJaHTSr2pbHkDlHDDXTJivyVtfC 857PXcCvrvMz+SlFJSXPGmzi/fYwCxP4k7x0TXDPjilWZH4tnoCabW15nAwt3aDOWyNN 1aKA== X-Gm-Message-State: AOJu0Yx5EPQBNz8oqiI+5qpZR3Dlmuh2uvHfyP+QAv3sLSrfRTA+RXy6 PtBk2YjVQzIoBr05GcFQkS9i2n3nRUMWpaGb0Ursn97Pj/4Uwc2MkfauTycAaJzkl4TgLopN0cK B X-Received: by 2002:a05:6a00:987:b0:6e5:8d9:d59a with SMTP id u7-20020a056a00098700b006e508d9d59amr11052223pfg.16.1709059722234; Tue, 27 Feb 2024 10:48:42 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:41 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 04/14] linux-user/elfload: Stack allocate struct mm_struct Date: Tue, 27 Feb 2024 08:48:23 -1000 Message-Id: <20240227184833.193836-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::435; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x435.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Ignoring the fact that g_malloc cannot fail, the structure is quite small and might as well be allocated locally. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index cc43487a37..98b82b1a49 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4099,7 +4099,7 @@ struct mm_struct { int mm_count; /* number of mappings */ }; -static struct mm_struct *vma_init(void); +static void vma_init(struct mm_struct *); static void vma_delete(struct mm_struct *); static int vma_add_mapping(struct mm_struct *, target_ulong, target_ulong, abi_ulong); @@ -4174,17 +4174,10 @@ static inline void bswap_note(struct elf_note *en) { } * thread that received the signal is stopped. */ -static struct mm_struct *vma_init(void) +static void vma_init(struct mm_struct *mm) { - struct mm_struct *mm; - - if ((mm = g_malloc(sizeof (*mm))) == NULL) - return (NULL); - mm->mm_count = 0; QTAILQ_INIT(&mm->mm_mmap); - - return (mm); } static void vma_delete(struct mm_struct *mm) @@ -4195,7 +4188,6 @@ static void vma_delete(struct mm_struct *mm) QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link); g_free(vma); } - g_free(mm); } static int vma_add_mapping(struct mm_struct *mm, target_ulong start, @@ -4638,7 +4630,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) struct elfhdr elf; struct elf_phdr phdr; struct rlimit dumpsize; - struct mm_struct *mm = NULL; + struct mm_struct mm; off_t offset = 0, data_offset = 0; int segs = 0; int fd = -1; @@ -4664,11 +4656,10 @@ static int elf_core_dump(int signr, const CPUArchState *env) * set up structure containing this information. After * this point vma_xxx functions can be used. */ - if ((mm = vma_init()) == NULL) - goto out; + vma_init(&mm); - walk_memory_regions(mm, vma_walker); - segs = vma_get_mapping_count(mm); + walk_memory_regions(&mm, vma_walker); + segs = vma_get_mapping_count(&mm); /* * Construct valid coredump ELF header. We also @@ -4701,7 +4692,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) * Write program headers for memory regions mapped in * the target process. */ - for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) { + for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { (void) memset(&phdr, 0, sizeof (phdr)); phdr.p_type = PT_LOAD; @@ -4738,7 +4729,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) /* * Finally we can dump process memory into corefile as well. */ - for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) { + for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { abi_ulong addr; abi_ulong end; @@ -4767,8 +4758,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) out: free_note_info(&info); - if (mm != NULL) - vma_delete(mm); + vma_delete(&mm); (void) close(fd); if (errno != 0) From patchwork Tue Feb 27 18:48:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776280 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308961wro; Tue, 27 Feb 2024 10:50:58 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWE3S1pt4r2iDfelr7BRyzDnhN3lkNAq46eOoBDCbfAf5N0jfNK0E0UjgnTUNWKpWLrOX97hpWqp3UOrCFYAYS2 X-Google-Smtp-Source: AGHT+IG2dd91IARVvddZHEN9491lAHLbk59oagJ6iEUlnz48dte6RMugFj34gSTn00LMhQONwnau X-Received: by 2002:a05:6358:5713:b0:17b:759:65c2 with SMTP id a19-20020a056358571300b0017b075965c2mr14116829rwf.10.1709059858303; Tue, 27 Feb 2024 10:50:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059858; cv=none; d=google.com; s=arc-20160816; b=KajDLjt1GjjhvIIoVL8j9LG+EaqVvDzCJOGC1vFaZ/JjTiEXnUfzCjVRtVcYc4FjVk 8OBJFt7BJ554MpdDEKa9mxkkRL9p9hWvR1Gn0LyomUy7iagLe/7fNEM2mlwsX3WHpf7I 5CG+nPaxQoHH9+5gZQKkwjTpHz+0HmnFtXbN0lqEVErANlytFWf3pMtm5sbS23qI79Q5 QjhKviIUmtvPun2L1CTCGCha4fYx4ELuxNP3XY83ct6Ssyq/rOvms3al1YVkfkQLGxI5 ohGcA95P4dTslc3k4t6AyGnkc/97tVUMzRzh6cuKOPKupvNfFh7T8k52cuhnNRNFHLVu rj0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0IQWSjcLXc40VTcQlmdhU2o876C5B7NqB15Y8LB3+3A=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=DY/P113B86y3I1TUWxdWtoLc+xxvmXfLTS9NYvxJ/yXaH17wgNqMbT2G73VSZqdDYI SoanqPt624Yr11Hknk52EwduVpfHG/1OSboYVkGPkaK01UDO1kC/R/xQeADMWi4pwbNl nCwqwzuG/XmZnUPjT4vhEIBH8GV3i4lt3ylxCwIwvC/dNGAy9Ur6681xmhAhrpEnwja5 FWHI8MPa+GUnYpyWCXSfzA/FN0N+X0jf0UQElgfigbO1L9CFPDyzTcl/pgkgwkb5dWf6 wvhN12FisfEBzeoXe8AX4FreBI8pT9C/RQppBHz7u9zgK5Le9NB+9T9aep79NqyAAFus Yp9Q==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SaZFfP9r; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id q14-20020a056214194e00b0068fb883d21bsi8056221qvk.271.2024.02.27.10.50.58 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:50:58 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SaZFfP9r; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VT-0006X5-UJ; Tue, 27 Feb 2024 13:48:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VS-0006WP-BW for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:46 -0500 Received: from mail-ot1-x32c.google.com ([2607:f8b0:4864:20::32c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VQ-0001TX-VG for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:46 -0500 Received: by mail-ot1-x32c.google.com with SMTP id 46e09a7af769-6e445b4f80bso2587881a34.0 for ; Tue, 27 Feb 2024 10:48:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059723; x=1709664523; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0IQWSjcLXc40VTcQlmdhU2o876C5B7NqB15Y8LB3+3A=; b=SaZFfP9rtg11C3ruHczOU9+wbj+9BQi/geWUqoL1W6ZJZT3Eupta3nxSXayim1Krpb roRxh8n9iUjlGiOyebxG24VzxjYYOlYJDPcqI2es0tLJsXcvT8+VOLkCT36B1LNt8lIX rjVRl24JRgigYlB4OMCP4WWoUHE0nVkX59Bvb0C4wCclav+99mw6Ea+jCl8GQL+/KR3g BFJ8lNZ1TccAGeefkyRLbieLTmd5EHvcqCnRbeNCS7z9mb2K1TzLdHPi0dsKJP+OWKjz USGbef61hRO0JDczE43pUYcg0W2EBROWpCmsijtreHA+CLMiAuJgm/VVrLQ8FOqhUUAw O67g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059723; x=1709664523; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0IQWSjcLXc40VTcQlmdhU2o876C5B7NqB15Y8LB3+3A=; b=QNFg/Ky1TSVyzeNJ311HFDv1cfkz8+6GgL0T6VUTGetcA8AX58k1Yh0gzbzdwSIm4e vUZWSJKiWciP+xkr11Mv38Gi/tgu9AGOMhhSsAUuEUbuxuEG8xOB79I/nbs8cZrmGx2k j3pEC8Rsj5qvkKJaE8ukmk9vlXjA21+19MV7gbqSPYKIzMU8cs8NyolbPxqxKCcMgq1Y PW8p5HsS+qAObwephQ0Cuo27WUudr2wQRPE42vK6pRT6b3ChxpQg59QOaKHZLdQvTSkG f1ZjS8jF7RI79UnzHOP13e8P7Mkf+eNyICwLrXK5AxZWk3Bt2ghPqeyO596zN6eYBSiB C2MA== X-Gm-Message-State: AOJu0Yxi5KmwInBn8Iz06HjdZcPpOe58cJ/JU2EDcihNkB78AYTpyVyL U8sKQ7xiu21lz8yH/3tpbznHAEQ0RgxaFnXwPskIixbSVz20WaTmdF5Q5N8CA+P1Pl2n/4scl39 y X-Received: by 2002:a05:6870:700e:b0:21f:c705:7d8c with SMTP id u14-20020a056870700e00b0021fc7057d8cmr13325878oae.26.1709059723679; Tue, 27 Feb 2024 10:48:43 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:43 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 05/14] linux-user/elfload: Latch errno before cleanup in elf_core_dump Date: Tue, 27 Feb 2024 08:48:24 -1000 Message-Id: <20240227184833.193836-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::32c; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x32c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org On the off-chance that one of the cleanup functions changes errno, latch the errno that we want to return beforehand. Flush errno to 0 upon success, rather than at the beginning. No need to avoid negation of 0. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 98b82b1a49..39d9ef9acc 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4634,8 +4634,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) off_t offset = 0, data_offset = 0; int segs = 0; int fd = -1; - - errno = 0; + int ret; if (prctl(PR_GET_DUMPABLE) == 0) { return 0; @@ -4755,15 +4754,14 @@ static int elf_core_dump(int signr, const CPUArchState *env) goto out; } } + errno = 0; out: + ret = -errno; free_note_info(&info); vma_delete(&mm); - (void) close(fd); - - if (errno != 0) - return (-errno); - return (0); + close(fd); + return ret; } #endif /* USE_ELF_CORE_DUMP */ From patchwork Tue Feb 27 18:48:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776283 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp309063wro; Tue, 27 Feb 2024 10:51:13 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUkmS2ltFoQPAQ4HHEelRzumG/fE/4uR1mEs+3Hd2FO8+Ux+tQPr7i1r39Qke4lN5DAUlqQEW8mmZG0pM9xjESi X-Google-Smtp-Source: AGHT+IEX4a5nolqwfbvMlwjeaoS6tlh/tg7N92ENF5p9UKEQofo4f14h1Z/3cx2u/KHV0eyL3tBi X-Received: by 2002:ad4:5cc6:0:b0:68f:309d:986 with SMTP id iu6-20020ad45cc6000000b0068f309d0986mr3645209qvb.17.1709059873535; Tue, 27 Feb 2024 10:51:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059873; cv=none; d=google.com; s=arc-20160816; b=W6xX8H6VQV1XRLBfClNZFH3/wgo0C3jE6zaOvYq6S+rt5M3JTIztY6bq62Dgsd6AEC TCXmNW/POFTarm1QGIOGzz9ud65cwPl8jMf3PZKvOV3tvbcP2SZ004A6MbIw6MhvjiVN nC1i8cUogR4Qis6/v7Np+VBbTllR5KfCCwCfXvO9h/6yF65aZai2AMcpodQkboY+yFnh RLeAxEfRtxGmhbk7jQJsaXVRLEpudd9w1BdXHGVElWL7hKU9flLGIW30jqGkE4WqnjaH j8O83NDPCv6Lhxfunz3g8dkyugtmPrCAoJLa0/aq142pFIb/mzHKrU7+WLER2IYMvnBH QDIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HjNGmK53xTEM4PEP/wzxcWkZ/m9cBOwh2M6e53eVpiA=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=q/j+0+ythDMBKs+R+viPM4nLwNAXkRe0TdtNHhZALDEqER8F1IgG+rhQjOvor12WZw gOaBiK74FYctSVyMRDoWrICr/+QOFHxFOhwQ9Bly9RcQIm8rtwF5dGSfGyii2Gs9K/IL fQx42Ejz+XF68udv6BT2x1VRuwMn20SaFEocPmYh9fWp1RgM7bkNBLL5ZhlV29u9UI5X BK+xQim3DoMAn+YpzZpSF3bWQFmNUAbgKy0mKgROgqQdcULWYe03P5vPjRVm18PPg1nK +RR8hPFxCh2su9hKgi/CjzTeJnxhX7A54M6ySOdFOPhiKUEULBOU09nWYv3157FMzlBV ZVKg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=H8CbB2Ob; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id gv3-20020a056214262300b0068cb0aa2ec6si8330306qvb.518.2024.02.27.10.51.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:51:13 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=H8CbB2Ob; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VV-0006Xi-4I; Tue, 27 Feb 2024 13:48:49 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VT-0006Wb-Hs for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:47 -0500 Received: from mail-oi1-x231.google.com ([2607:f8b0:4864:20::231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VS-0001Tp-4p for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:47 -0500 Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3c1a7d51fb5so1209222b6e.1 for ; Tue, 27 Feb 2024 10:48:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059725; x=1709664525; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HjNGmK53xTEM4PEP/wzxcWkZ/m9cBOwh2M6e53eVpiA=; b=H8CbB2ObOaECqOHiTpuCP40k83kMXOXglRn98bVANnPu4CuOSbphs5JzntlE9p06XA DisI2QF3aWWfhMPD6GKsEiggz1z6iIZLWOBi+iNEU2Xnwdv9tkpEIGqpoTBg/0dxl5t4 B566IxnFyL5x3wJP4RcJYVLrfGxXPLr8y5fBRsOMYEEZv5PqRh0HdnyNNoJOnznZ1nxO jXjzfmrNSU7wlSlZYIHdti/0ylxRCaHTYnti0op0fDBnkoy/9ZXYZowEre/w3eE5YOwl QoawxWchR8tsrTQhzikZdOQXTbbVt0T/5nQ0TFkd0PXsxeUMPQR7BWCS9HV3ZKAtV8kI 0Lpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059725; x=1709664525; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HjNGmK53xTEM4PEP/wzxcWkZ/m9cBOwh2M6e53eVpiA=; b=JgLZunwSZi81ci/+f1ZQ5g0hnRRdzKvKChN1fOoDL/pS01Ip/ZDNPaB5ETM8VncEiT +3rMMlTJ9HwrL6j6661YeatsD8+b7wyAP4JCU9b3XWPUf+P+maubzKGpPKfJWoI1Xp5C Ik1AOrRuUlH6NW+QgHuGRJI2ob8Qa9lwmV6JMkDJe+GjRqzuM7yVYo8jmg3/O54fJ31/ Rz7hY6C/lzSdZ8sizf+kGu/SoRQknhBukHP43mVvtVuBPfSPpXVpza1WqqZeEhLUwLa/ Q2UKT/58NJS87LDzAl+1DKKmsutVQrPDh8JinUOJwnHn9a2aAWhrEHEs/kV+5ztXqIf3 wjzw== X-Gm-Message-State: AOJu0YyTkughH2cjgf8p8pG3N/taK18klJ93nU0TLYpactasJIGFrC+q XjUqtu8xBHnyFKOFcZTxDII63U49w8R+OZYpLDx1r51LGdSIQc5l07Lh6HynkhfEmjTYO80FDie a X-Received: by 2002:a05:6808:280e:b0:3bf:d775:616e with SMTP id et14-20020a056808280e00b003bfd775616emr2563878oib.53.1709059725010; Tue, 27 Feb 2024 10:48:45 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:44 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 06/14] linux-user/elfload: Open core file after vma_init Date: Tue, 27 Feb 2024 08:48:25 -1000 Message-Id: <20240227184833.193836-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::231; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x231.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Swap the ordering of vma_init and open. This will be necessary for further changes, and adjusts the error cleanup path. Narrow the scope of corefile, as the variable can be freed immediately after use in open(). Signed-off-by: Richard Henderson --- linux-user/elfload.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 39d9ef9acc..877799e9c7 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4625,7 +4625,6 @@ static int elf_core_dump(int signr, const CPUArchState *env) const CPUState *cpu = env_cpu((CPUArchState *)env); const TaskState *ts = (const TaskState *)cpu->opaque; struct vm_area_struct *vma = NULL; - g_autofree char *corefile = NULL; struct elf_note_info info; struct elfhdr elf; struct elf_phdr phdr; @@ -4644,12 +4643,6 @@ static int elf_core_dump(int signr, const CPUArchState *env) return 0; } - corefile = core_dump_filename(ts); - - if ((fd = open(corefile, O_WRONLY | O_CREAT, - S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) - return (-errno); - /* * Walk through target process memory mappings and * set up structure containing this information. After @@ -4657,6 +4650,15 @@ static int elf_core_dump(int signr, const CPUArchState *env) */ vma_init(&mm); + { + g_autofree char *corefile = core_dump_filename(ts); + fd = open(corefile, O_WRONLY | O_CREAT, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + } + if (fd < 0) { + goto out; + } + walk_memory_regions(&mm, vma_walker); segs = vma_get_mapping_count(&mm); From patchwork Tue Feb 27 18:48:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776282 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308974wro; Tue, 27 Feb 2024 10:51:00 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXXhkiPwrC4mp8Y49pVJD4dh9I2lbdQWup+zW13A8rAQ/MSSzJguF9GUA1AmMsVDg6QoW0NI695z9a9Da4Z2lFm X-Google-Smtp-Source: AGHT+IHAsEnwguWABNw4zcubWYBRSoxPQ+ZpvkViCya2Dg7KUuFv36JWrqtkh59XTsW1l3dzPLLl X-Received: by 2002:a05:6808:180e:b0:3c1:b603:d290 with SMTP id bh14-20020a056808180e00b003c1b603d290mr908828oib.25.1709059860011; Tue, 27 Feb 2024 10:51:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059859; cv=none; d=google.com; s=arc-20160816; b=bJ5vjGi36f7k/5LRTOXSXrQbzu9dE/GZmYYwqyYV6gVZBBLnj9bLXW5naJlQGV5Lgx KRAg17qh/PGkKPN6pb3xg0cQFlCOugAMGayTQPQK+02d/6A5/yBhBrPdaw8iCv/iTcEC Ws3P0kf7Oo6ujpaVCHJSdIRNIl1i0T0rOfFhR6+WhtI5qp9h56O+n1L9DWE0iYrUKELY qLYOeTEDEZNYW1zDW6Q4h4DVM6OZzyBlO4tdr7ME3/GoEo4p81vBJHE0HYknByuBi5h4 FdY8bPj3Ykx607BTKSPiFDMlxXKzoD6K+bKLUbsYWfIKgNFDmnztbpXBs5RoCCd33RXH XdzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zYkpQqoaMHDWxrt9KPP15xrK5H9LSGOtZoW36Ee0L04=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=QQdb6SsnZ3Glk0bVQGGaDc73b+YFuvmzQMj3qeYjMmZfJSIL/UvYcQ/e/NY67I6FQc iZz8J00U4vP4IwQOEyntbvHfeYge5qhMcsTAzM5OwMBjbbMsA66iJpcaVD46HTDhtf8/ zWLZzOW1hUOEqjl2AZoUanNGy5TPds6/uFBhFqG7igiZbmhEkNnfbCUmANdcGNBjN9lQ N7ZapQrtXhLB40KfyJjH8hMEjUz9+ySWvr4TMOd5gDfeZSjrAfbrhxU+WLOvhLTdL1q0 8hCLTr3gCxyAjXg82z25ItuP5tQBuogoqW7SDqxBqUoyu294qObvOYDmKHsq5cMnelaG O/EA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VA8hDsFt; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id l1-20020a05620a210100b007874f43dd5dsi7719091qkl.445.2024.02.27.10.50.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:50:59 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VA8hDsFt; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VW-0006YM-NC; Tue, 27 Feb 2024 13:48:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VV-0006Xh-0F for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:49 -0500 Received: from mail-oi1-x22f.google.com ([2607:f8b0:4864:20::22f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VT-0001U9-FV for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:48 -0500 Received: by mail-oi1-x22f.google.com with SMTP id 5614622812f47-3c1af1e8b7eso445288b6e.2 for ; Tue, 27 Feb 2024 10:48:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059726; x=1709664526; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zYkpQqoaMHDWxrt9KPP15xrK5H9LSGOtZoW36Ee0L04=; b=VA8hDsFtP/U5ZrVOFtU3Ar9rJaOaPmhzPwUj6uOh0SB5bc7mAJN+uFnI0GiTccwyJ+ mTrwRMP81OXovg9aYIRzXYnbsCZwerjMnyaDXi8X6URpMvgEAYI7znB6oV8brnJWdoWK rWtM64LlFJAuM8Cugkhvg6j4XRXLXi3NnqSC8YOdzxP3hgkcCuPhuI3H79Jk5Nobmw0s m3osep7lFdJ0eNTsZ9YO/SDtgq/RqrsU/jvWZHeiyrZAl6Ca8vu4JzsOAnm01LiFdDp5 w7yRLACAqp5G7OedZWCpql1Br9VTY31BHWeofdTS6ZLRgP8x4kP/uSVwqr4zjw1SEse9 u+Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059726; x=1709664526; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zYkpQqoaMHDWxrt9KPP15xrK5H9LSGOtZoW36Ee0L04=; b=Ri5/wioFFVC8qKQ6eF9v5lUZ6s2QR3c4GG0NBse32ufmFYCjOS8I5zTMn4DJorgEdJ EtbnMaLRF5WaC3NxV90wrVbfKX64M7Nu0vaS8wABZn2O/3NWNrHdq3mcyoIYKY37gpKD R6tDRtJuaHQEQ+jw9ZRBKXy0jQjMtUCNjg2EGjfepSiGRebY2ahfk+hWg15svcLzA7L5 iPD220KHKaGvXHfQn+R3w4dhEdzNeFhdL3cCkjXPLa28bH1WNs91Ygq6fxUxsKGIr2lS 3SuloKwbLz7V5NN5bMa9l4t+qLBYV5+E3YaaYktjsjI8JH+7PI8nsNITQeO0jfOZ5aYK 0RbA== X-Gm-Message-State: AOJu0YwQs/HIi+oV10yYXGt8qP+rmqOJDj83+83yjitsZIEkbnfv2qNz iTP7iIwPacC2d8TjOvTYCvR2hEnzpsqIunbPml5xiXyYfFfD+xt0hpfwM2S0+5cEgWavD8Zah0G U X-Received: by 2002:aca:130f:0:b0:3c1:6526:1342 with SMTP id e15-20020aca130f000000b003c165261342mr2910324oii.45.1709059726331; Tue, 27 Feb 2024 10:48:46 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:45 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 07/14] linux-user/elfload: Truncate core file on open Date: Tue, 27 Feb 2024 08:48:26 -1000 Message-Id: <20240227184833.193836-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::22f; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x22f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org While we usually create a new corefile, truncate otherwise. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 877799e9c7..16dd08a828 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4652,7 +4652,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) { g_autofree char *corefile = core_dump_filename(ts); - fd = open(corefile, O_WRONLY | O_CREAT, + fd = open(corefile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); } if (fd < 0) { From patchwork Tue Feb 27 18:48:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776272 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308252wro; Tue, 27 Feb 2024 10:49:10 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCU1yqMnizW9O74uifhza2Wl7XThAw/mnBvlU6aD6eDdLsJsd/pag1sPO6sAq0Yu7GYj41P6kYzmbopiLHyenJkH X-Google-Smtp-Source: AGHT+IFOOl6OACXxy0t9NtZwNtau4KeLcOK/t4lG7ocp9dBUdCjzLGczzF/MyrHEr4zHyzIUctAi X-Received: by 2002:ac8:5a86:0:b0:42e:7a39:fa4d with SMTP id c6-20020ac85a86000000b0042e7a39fa4dmr11343235qtc.19.1709059750297; Tue, 27 Feb 2024 10:49:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059750; cv=none; d=google.com; s=arc-20160816; b=SHHaO+SPo7VYd1hrybyGg5c1YzNgULiGxVbL51JV7XbI1yfxvLoS1+t8XzAqHQCO2e Btc+jnVlff4VTAskfjyGGeYRnNMnYdHiPB3pqan8NJYePm3As5FrunnA4e9EKDFFt5+b 93B3kHwOC1o5SEGilMH0l9wCtOXibGJEpXm9zI60WKV6KlEZuM2EJuXh27bL2uUPIx0P LzfH7wt4ojjPkTGs6o6lX55wTD3NFHNUxnFp9GnwX1b51gzgHDaeME279dbDIsfijjKJ sSPcOYF/ZONk4SV7DW6sEJuyorwQwZLjMAjL0V9l/BHbwTM8mSq9SUQ5da70KlyWNLSY sJZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7NxH+YjseDTpq8veEE2IUMndw319v6C2n3pI3OsSxXo=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=CgJtQkW5wh3qsC+i76vki+4EUQoyxVg7LuQVOUV88CGoAaat43/nybUtrUcmmEQ9uY 6dSvb7Mz9BIDn71dfGE5bs/eRhGfAnBaMMqZd8KGZNU1irvuaL32Sfotg8HGxjarWJJ0 ZW2tqaZVhjZzFEY0IjAShuQfHpCdUrnM56LK1h5ZBU7wjWdmKUoP2O8Z4aMK8FuYb4b2 Ya4HonA6gkuJe1WY7Dk+sqpOEA/oT6n2mCFD9RZ5yHxjqGyJG6CT9BkIUwq/KC4Pty8v YH+k7qBsTDBKq+hqOy+Yg1WMzrMhxysxczheJnwBq1l2Ul508uyKjLW664J7HHAxCvyd xeBQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HzMcsNAA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id g1-20020ac85d41000000b0042dcbe3ee25si9198782qtx.785.2024.02.27.10.49.10 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:49:10 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HzMcsNAA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VX-0006Ya-Ug; Tue, 27 Feb 2024 13:48:51 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VW-0006YC-Bu for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:50 -0500 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VU-0001US-RQ for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:50 -0500 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6e508725b64so1174325b3a.3 for ; Tue, 27 Feb 2024 10:48:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059727; x=1709664527; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7NxH+YjseDTpq8veEE2IUMndw319v6C2n3pI3OsSxXo=; b=HzMcsNAAMqZdnfye7m1h4b6Gsv7P0ePdXCS2W4PLNFdWeuLUHGk99Zwsnb2X+ZUZgE XdpsjQY99PlF8YKwM2OXhNcIiahA1me9dnE7ZqyoHni/h3OzGOMkGwyZBSjIa7c9W1ts Zwx2WByyI5TU13vmKO+85UIXzffUAURXZayBg9po2TLa/VP3U2EUG+y2/lAnw6MJX0Av PhWcjHb+QrYe+OndqDkGJUKs9ueX67z1wy1/EsvhOSH4ILZg0zK92d2YLGdJPNkRIBXE AjSGGjbfODo2Z3XbaNJJlxqH5p22M53i9ODPI00PP0YXSJ7UVvjn5fGiFre+OOfbL0hP angQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059727; x=1709664527; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7NxH+YjseDTpq8veEE2IUMndw319v6C2n3pI3OsSxXo=; b=lpag/myTUOFdyYDs1d0KWfi27MgwcE7QFuL9DupccU4oA/u2BJeMYVx0tA/QabbQhH 3fIccpcwbas9MgUOMOaLiPGW4tVYSm7F274qUNAAOqupnmrSyyBJ/AlZsXAe9GApS4eU HWqvFEwtMi6muUu4Wktdt2YaxOK4zkwz4J0GKCsPHi58JeYUkgPLz0+H6t6P/KBgSOqj IuWP2WbscCIXQpnrC7e9Fa6wTl3HLakOzBcTeS8Rz7oDu/Y/d3PZN5nVPerI14T0Gepp B/85lZU8xdO3rEeyZDU0b8PuOLukhv18x0q7XoD11Jphfobu2GE2xYoHr5qVmGphBqa3 QFrQ== X-Gm-Message-State: AOJu0YyPgB67ZMnlDqHyVO2cbsLKwnrkfo6LF6muwNXJlLUQV1W6y/bc SlqMSxTMV/UiKWivg8Eso/wuexM2qyEDPjRj4Luqe7be0KEgKlWV0xWfcGuT+aBbrfwxm/29hsz w X-Received: by 2002:a05:6a00:23d2:b0:6e4:f753:1e12 with SMTP id g18-20020a056a0023d200b006e4f7531e12mr10599709pfc.28.1709059727595; Tue, 27 Feb 2024 10:48:47 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:47 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 08/14] linux-user/elfload: Lock cpu list and mmap during elf_core_dump Date: Tue, 27 Feb 2024 08:48:27 -1000 Message-Id: <20240227184833.193836-9-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::435; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x435.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Do not allow changes to the set of cpus and memory regions while we are dumping core. Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 16dd08a828..6f9da721d7 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4537,13 +4537,11 @@ static void fill_note_info(struct elf_note_info *info, } /* read and fill status of all threads */ - WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock) { - CPU_FOREACH(cpu) { - if (cpu == thread_cpu) { - continue; - } - fill_thread_info(info, cpu_env(cpu)); + CPU_FOREACH(cpu) { + if (cpu == thread_cpu) { + continue; } + fill_thread_info(info, cpu_env(cpu)); } } @@ -4643,6 +4641,9 @@ static int elf_core_dump(int signr, const CPUArchState *env) return 0; } + cpu_list_lock(); + mmap_lock(); + /* * Walk through target process memory mappings and * set up structure containing this information. After @@ -4760,6 +4761,8 @@ static int elf_core_dump(int signr, const CPUArchState *env) out: ret = -errno; + mmap_unlock(); + cpu_list_unlock(); free_note_info(&info); vma_delete(&mm); close(fd); From patchwork Tue Feb 27 18:48:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776279 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308824wro; Tue, 27 Feb 2024 10:50:36 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUALgdVb2wys2UBq+mfa4WyuzgLWFoQdVSXxjeNrgnbt8hMkt/I6RKS1MJ6helinsoL2SYsgiYvXAez0x9cBM8I X-Google-Smtp-Source: AGHT+IGMk68PQJtr1+O4FZQL81oMow0q7oRn2tvarM8U8O0II6ATCMtvPrl/IYZ9iU68HwCJeYzu X-Received: by 2002:a05:622a:2ca:b0:42e:9745:376d with SMTP id a10-20020a05622a02ca00b0042e9745376dmr3913141qtx.19.1709059836575; Tue, 27 Feb 2024 10:50:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059836; cv=none; d=google.com; s=arc-20160816; b=HESjy8Y6eAmPQgJkWTRzzPKkyaNqvqljf0aouhk2Kzyb7MCoBpaRMCr27x2aFhTSu3 sT7R2s+LSN2klixX4WKnZwAi0xGYOZjumDZig/Uq2evagy7pSuoVoKe7pSEqSImY79aS HaKZ/iOWoUU0MJVJkXsahgJZgBPpXXuuufOsKoF0sM76RDFgTU3qQt/Lfv09pR9mg86u piC4Qm1PQjzkVTmEsgyxxhNou9q/OJZsSSjmmn6CY2dQPFKpO2azZiF3FFSRcHZ0gxZh yhvOXrb0F8/ovv4ttX84nLIdvxnOUkPv6bk5fsO0fEBBVhpSvdpBpdEbkBVYxlrTIuc2 49QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wXgOJaiR1gNovC/J+sG7U6tnXruxoEFoxj+FOnROhG4=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=uxI/67MwzcNFAhWKSf7LCyAwTDYjQqGGjTy6X6+Zc3AbOWZVgfX/+M0PNh5C/q01tr tJQVIzts6c5pCOhRkmrUpOJWKpfmbhoHSxjUteFixGqX7A7HbMakx6SMm5+/G4qU7Yrq +C0rPA0c0Ve4s6pz+DygTrBsY6GxpkQHR87ekekp31cG0L/5EnYmJxHN5PUiQQE1d7/j sNbQHITogGzB3+ffgFly4UsHiR0RofHFc5iRHqoPbHgE60b6PV3g7/e6LVgurCBKp+Ux dE5X9EqA6ROyAdAPW2g0XUPglUmOPiFJIuM6GyXW7uRrDZwD+bhraAnEJnhpJLViTOba ru5g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bxS0qcNF; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id a14-20020ac8720e000000b0042dd9365466si7683794qtp.208.2024.02.27.10.50.36 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:50:36 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bxS0qcNF; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2VZ-0006ZJ-IC; Tue, 27 Feb 2024 13:48:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2VX-0006YY-Pb for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:51 -0500 Received: from mail-oi1-x22a.google.com ([2607:f8b0:4864:20::22a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VW-0001Ur-1V for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:51 -0500 Received: by mail-oi1-x22a.google.com with SMTP id 5614622812f47-3c1adc90830so592722b6e.0 for ; Tue, 27 Feb 2024 10:48:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059729; x=1709664529; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wXgOJaiR1gNovC/J+sG7U6tnXruxoEFoxj+FOnROhG4=; b=bxS0qcNFchlcccwfkEFRMTRnEclanIzJPb9ESOhjyUU9coWZ6Gvnfb2JSsmvmJ2bHR sJxX+IUpWqIsnjAGSBUQKTRAmmIR61/OYVa8GfIp8ZyzOudwEheKaqpjIzTALBKMZiMk qKfSTo8UwPBwcqbKEjbn3gOTMGdkvFaEIiwkp13ytZ94epk26DnpUwGw8PRYhBWFCg40 wtz/SZ1I40uIdw+AKgUBcg7sjCv/NtykxEdvSL3dDo3c+7iHzwTIt/6+tW0IFBihqJvS qqQxs9BHcWjVbLgyYzPEVEohJ11b+jPX2rGyzeC0mGhIfooVRwXl4LDNfqX7zLx8wfFq IzoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059729; x=1709664529; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wXgOJaiR1gNovC/J+sG7U6tnXruxoEFoxj+FOnROhG4=; b=RdfcWO6gVWrqpBCJrCz3nAGUx430Dp5fW/tkxjb3G6PgxiZF57/DU14X7brxRwNwtV STJzuuU3XNAy6w30tdH+/QnMwDmCxPHlfkd9KjJK91dRPymL9IhXXB4mI1rL5WHUNzM0 3nKCfxjnPqWMUe4iblIggv7neqpVu4IaFagnD9W0bVmWStiJzyAd3YnBSVdPVgoqtx1y +Xq1LR3O29jQq1Zt2iwrMzOQwqkKj20yasV+RA/FfwsBliD2X9vprfWBS8mgk95Qyd4X dXGupmkMJodLGx3o0yFxEl7klwHRN1b0yJgyYqywD2s+fxX9U1O51RcUmGo7GW35tI9H F3SQ== X-Gm-Message-State: AOJu0YynwxN7/7+zdQcclZ49cSmPuswl3RK7CMW64FzTSuU2U5OOvwR0 TmHsja57I1VV1lN3QbO8v44P31PfcUpxXb0609Bz92uJOiBdrEAPlfZ6/+L4zwfC0HACEGhMhp+ / X-Received: by 2002:a05:6808:ec8:b0:3c1:83f8:35d6 with SMTP id q8-20020a0568080ec800b003c183f835d6mr3278919oiv.53.1709059728894; Tue, 27 Feb 2024 10:48:48 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:48 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 09/14] linux-user/elfload: Size corefile before opening Date: Tue, 27 Feb 2024 08:48:28 -1000 Message-Id: <20240227184833.193836-10-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::22a; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x22a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Verify the size of the corefile vs the rlimit before opening and creating the core file at all. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 83 +++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 6f9da721d7..bad01bd2ef 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4270,6 +4270,16 @@ static int vma_walker(void *priv, target_ulong start, target_ulong end, return (0); } +static size_t size_note(const char *name, size_t datasz) +{ + size_t namesz = strlen(name) + 1; + + namesz = ROUND_UP(namesz, 4); + datasz = ROUND_UP(datasz, 4); + + return sizeof(struct elf_note) + namesz + datasz; +} + static void fill_note(struct memelfnote *note, const char *name, int type, unsigned int sz, void *data) { @@ -4428,27 +4438,9 @@ static int dump_write(int fd, const void *ptr, size_t size) { const char *bufp = (const char *)ptr; ssize_t bytes_written, bytes_left; - struct rlimit dumpsize; - off_t pos; bytes_written = 0; - getrlimit(RLIMIT_CORE, &dumpsize); - if ((pos = lseek(fd, 0, SEEK_CUR))==-1) { - if (errno == ESPIPE) { /* not a seekable stream */ - bytes_left = size; - } else { - return pos; - } - } else { - if (dumpsize.rlim_cur <= pos) { - return -1; - } else if (dumpsize.rlim_cur == RLIM_INFINITY) { - bytes_left = size; - } else { - size_t limit_left=dumpsize.rlim_cur - pos; - bytes_left = limit_left >= size ? size : limit_left ; - } - } + bytes_left = size; /* * In normal conditions, single write(2) should do but @@ -4622,16 +4614,15 @@ static int elf_core_dump(int signr, const CPUArchState *env) { const CPUState *cpu = env_cpu((CPUArchState *)env); const TaskState *ts = (const TaskState *)cpu->opaque; - struct vm_area_struct *vma = NULL; + struct vm_area_struct *vma; struct elf_note_info info; struct elfhdr elf; struct elf_phdr phdr; struct rlimit dumpsize; struct mm_struct mm; - off_t offset = 0, data_offset = 0; - int segs = 0; + off_t offset, note_offset, data_offset; + int segs, cpus, ret; int fd = -1; - int ret; if (prctl(PR_GET_DUMPABLE) == 0) { return 0; @@ -4646,10 +4637,36 @@ static int elf_core_dump(int signr, const CPUArchState *env) /* * Walk through target process memory mappings and - * set up structure containing this information. After - * this point vma_xxx functions can be used. + * set up structure containing this information. */ vma_init(&mm); + walk_memory_regions(&mm, vma_walker); + segs = vma_get_mapping_count(&mm); + + cpus = 0; + CPU_FOREACH(cpu) { + cpus++; + } + + offset = sizeof(struct elfhdr); + offset += (segs + 1) * sizeof(struct elf_phdr); + note_offset = offset; + + offset += size_note("CORE", ts->info->auxv_len); + offset += size_note("CORE", sizeof(struct target_elf_prpsinfo)); + offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus; + offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE); + data_offset = offset; + + for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { + offset += vma_dump_size(vma); + } + + /* Do not dump if the corefile size exceeds the limit. */ + if (dumpsize.rlim_cur != RLIM_INFINITY && dumpsize.rlim_cur < offset) { + errno = 0; + goto out; + } { g_autofree char *corefile = core_dump_filename(ts); @@ -4660,9 +4677,6 @@ static int elf_core_dump(int signr, const CPUArchState *env) goto out; } - walk_memory_regions(&mm, vma_walker); - segs = vma_get_mapping_count(&mm); - /* * Construct valid coredump ELF header. We also * add one more segment for notes. @@ -4674,26 +4688,17 @@ static int elf_core_dump(int signr, const CPUArchState *env) /* fill in the in-memory version of notes */ fill_note_info(&info, signr, env); - offset += sizeof (elf); /* elf header */ - offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */ - /* write out notes program header */ - fill_elf_note_phdr(&phdr, info.notes_size, offset); + fill_elf_note_phdr(&phdr, info.notes_size, note_offset); - offset += info.notes_size; if (dump_write(fd, &phdr, sizeof (phdr)) != 0) goto out; - /* - * ELF specification wants data to start at page boundary so - * we align it here. - */ - data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE); - /* * Write program headers for memory regions mapped in * the target process. */ + offset = data_offset; for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { (void) memset(&phdr, 0, sizeof (phdr)); From patchwork Tue Feb 27 18:48:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776277 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308728wro; Tue, 27 Feb 2024 10:50:24 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWX2N9Eq5Wzy/xhTgrSUQRySV74NpPwFOKLzl+mC1QYO6GaZdTjBcPoqauk5q3XEROoMcXv/r3tEiZ+BkGABG63 X-Google-Smtp-Source: AGHT+IEd5TRk+SDPFFEqPKjh0P4jDQ+cDy+nROqKDh0aqESO32y4sXUIO0BBQ20IBAItDmh2fdkp X-Received: by 2002:a05:6214:2b0c:b0:68f:e19a:7633 with SMTP id jx12-20020a0562142b0c00b0068fe19a7633mr4129817qvb.50.1709059823826; Tue, 27 Feb 2024 10:50:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059823; cv=none; d=google.com; s=arc-20160816; b=bCGDJ3k90068bCv/uTJUvQC8pX2b5xG3Q5kNftKyE6aJHJFRQ9JnDXSBjyYL3Yjb+E Q3kYS9HX2RQhchOrnQENFYaMwdcxc48C41Yvo6fbyO/IfcNw1Y7jc6yzn7VO8DnskSn6 tzPc25h3FMZZgHf/Q3UqCHh37cffiQEiqXDP27L4WHCRZctz0n5QPvSVJObYwnkzYVtb dXs2RI+l5gtsLfiy2Wyb0BSCf3+yoHSSsDVaKlGx/7mR12aRjMlu7etGvPBCjY/iE56G INDLwM6ah80+XUODrx7nvSBVyFlTXkrzoa/3sMu4eAl/hkN7NA9XYqjuPb6nOJRa4VKN 8GVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WPZgpp/cCi7t1E47Pxo1jS8aeNgwNI8GgVuo9d5SLyc=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=BeGF3mxSM6HuUfSOrr4ZSiS5iNDyvMcqu0pEu80JTrsXDh+aoIq99ZG+Fcihq4nyjl t6O2P8o2jUA4tHqrr04vUpiOjVNfjOwYgShKN/fetMLwP0D5HQgvFnSxi8bDMxQeAvLH hneH6CQHD26cPRpdD8lZNjL0mo1SKF37d7xA4APkhyxU8pIDZ3PXsHVvQWpFp1ArxmqD a0iBoHEiAV1QZDwzFp7H7ewGs/wZmZ9etEscMOex6mV1eSDB40fk63uHjcyRHm9QGvBd PhOsWsISVYV5LsOkUtm+bpq1khd3vlu9qNEeD9M+FyWJYXqAa+zpzDz7OCYUwVDBhTGs 9URQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D1czXhJ6; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d5-20020ac85345000000b0042e78c37d6csi7091619qto.318.2024.02.27.10.50.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:50:23 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=D1czXhJ6; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2Vc-0006Zl-2J; Tue, 27 Feb 2024 13:48:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2Va-0006ZP-1W for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:54 -0500 Received: from mail-oi1-x231.google.com ([2607:f8b0:4864:20::231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VX-0001VN-GQ for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:53 -0500 Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3c19bc08f96so2645611b6e.2 for ; Tue, 27 Feb 2024 10:48:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059730; x=1709664530; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WPZgpp/cCi7t1E47Pxo1jS8aeNgwNI8GgVuo9d5SLyc=; b=D1czXhJ6QU+VZohBRAcyBBcPjE1DJEf8IiKz+Ng2jlX2W2ixIU3JhM2sIlUk5i4qLQ QmrDib3d5eE2lf/jRJoE6y/uKvtode5pZRsBbJgm6bXqCu3L6gRvNcOaK8wDf7i/yB2/ zfevhoFt6iQgj43+QFRrWUaZLeI/JIla8y59w2FImSyIysMWanM3Sk3O+SpJn8ujOyK9 fL8YleYkFhPOXqrvhiOXe8SUq2PSXePAPjlbVWNGHUw2CFPK7c1O0cVBTjpW5zIhLei5 mWZNFP1ItJrmZuUw89zEtp9hyZaoIw/Kv0n1UT/hdrd7qKU8T1Fpgc9BTSqLFerLRpcn dMYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059730; x=1709664530; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WPZgpp/cCi7t1E47Pxo1jS8aeNgwNI8GgVuo9d5SLyc=; b=NEhNg4yZU6S+d84zjNJdcxXToP4Jd/thF0cfLAo/7+IgYGiMJJc+xGNTs3fHnm4Lun DhX+ewprJTPESHtKOC03yC013Z+ZY/zLg3rpjQNCWNl13BaZ36/TmLiiW0Z6txG1Xini xCjiMVNusURhMy4i54uCfAWRcy6avlhYRNnJzCLOoukfVQTeA0OjgGIJoCPPQ6jrwmcL 1KhbN1ADSMOHoxzdeHwl+cXpBdpPxe8Xzs0+xt8zCgWeaseSBYazOuoDlJRRKnWl8SRE mgM1D2OYVXWD9Fjz2NIBQJ5lZcFyMU7C/LfF05jdXA5b3gOykoIhDY1zvJ+6V8bW/2TI BECg== X-Gm-Message-State: AOJu0YwV+YCdTexgsc9Q7EzfEfK2PsCTjWNS5Znde4N+jse1XyuTTUvm jOqSrYM6oZmbfXtbthqjGl88dkhWU2J9HAYT0KjQnL2ODqpsRM8tA5V9qHHSHCcENOG4v+KaSEr w X-Received: by 2002:a05:6808:1143:b0:3c1:9a60:9248 with SMTP id u3-20020a056808114300b003c19a609248mr3180500oiu.14.1709059730367; Tue, 27 Feb 2024 10:48:50 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:49 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 10/14] linux-user/elfload: Write corefile elf header in one block Date: Tue, 27 Feb 2024 08:48:29 -1000 Message-Id: <20240227184833.193836-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::231; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x231.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Fixes a bug in which write_note() wrote namesz_rounded and datasz_rounded bytes, even though name and data pointers contain only the unrounded number of bytes. Instead of many small writes, allocate a block to contain all of the elf headers and all of the notes. Copy the data into the block piecemeal and the write it to the file as a chunk. This also avoids the need to lseek forward for alignment. --- linux-user/elfload.c | 410 ++++++++++++------------------------------- 1 file changed, 113 insertions(+), 297 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index bad01bd2ef..b8d07d8054 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4002,18 +4002,6 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) * Example for ARM target is provided in this file. */ -/* An ELF note in memory */ -struct memelfnote { - const char *name; - size_t namesz; - size_t namesz_rounded; - int type; - size_t datasz; - size_t datasz_rounded; - void *data; - size_t notesz; -}; - struct target_elf_siginfo { abi_int si_signo; /* signal number */ abi_int si_code; /* extra code */ @@ -4053,40 +4041,6 @@ struct target_elf_prpsinfo { char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ }; -/* Here is the structure in which status of each thread is captured. */ -struct elf_thread_status { - QTAILQ_ENTRY(elf_thread_status) ets_link; - struct target_elf_prstatus prstatus; /* NT_PRSTATUS */ -#if 0 - elf_fpregset_t fpu; /* NT_PRFPREG */ - struct task_struct *thread; - elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */ -#endif - struct memelfnote notes[1]; - int num_notes; -}; - -#define NUMNOTES 3 - -struct elf_note_info { - struct memelfnote notes[NUMNOTES]; - struct target_elf_prstatus prstatus; /* NT_PRSTATUS */ - struct target_elf_prpsinfo psinfo; /* NT_PRPSINFO */ - - QTAILQ_HEAD(, elf_thread_status) thread_list; -#if 0 - /* - * Current version of ELF coredump doesn't support - * dumping fp regs etc. - */ - elf_fpregset_t *fpu; - elf_fpxregset_t *xfpu; - int thread_status_size; -#endif - int notes_size; - int numnote; -}; - struct vm_area_struct { target_ulong vma_start; /* start vaddr of memory region */ target_ulong vma_end; /* end vaddr of memory region */ @@ -4110,22 +4064,6 @@ static abi_ulong vma_dump_size(const struct vm_area_struct *); static int vma_walker(void *priv, target_ulong start, target_ulong end, unsigned long flags); -static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t); -static void fill_note(struct memelfnote *, const char *, int, - unsigned int, void *); -static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int); -static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *); -static void fill_auxv_note(struct memelfnote *, const TaskState *); -static void fill_elf_note_phdr(struct elf_phdr *, int, off_t); -static size_t note_size(const struct memelfnote *); -static void free_note_info(struct elf_note_info *); -static void fill_note_info(struct elf_note_info *, int, const CPUArchState *); -static void fill_thread_info(struct elf_note_info *, const CPUArchState *); - -static int dump_write(int, const void *, size_t); -static int write_note(struct memelfnote *, int); -static int write_note_info(struct elf_note_info *, int); - #ifdef BSWAP_NEEDED static void bswap_prstatus(struct target_elf_prstatus *prstatus) { @@ -4280,35 +4218,32 @@ static size_t size_note(const char *name, size_t datasz) return sizeof(struct elf_note) + namesz + datasz; } -static void fill_note(struct memelfnote *note, const char *name, int type, - unsigned int sz, void *data) +static void *fill_note(void **pptr, int type, const char *name, size_t datasz) { - unsigned int namesz; + void *ptr = *pptr; + struct elf_note *n = ptr; + size_t namesz = strlen(name) + 1; - namesz = strlen(name) + 1; - note->name = name; - note->namesz = namesz; - note->namesz_rounded = roundup(namesz, sizeof (int32_t)); - note->type = type; - note->datasz = sz; - note->datasz_rounded = roundup(sz, sizeof (int32_t)); + n->n_namesz = namesz; + n->n_descsz = datasz; + n->n_type = type; + bswap_note(n); - note->data = data; + ptr += sizeof(*n); + memcpy(ptr, name, namesz); - /* - * We calculate rounded up note size here as specified by - * ELF document. - */ - note->notesz = sizeof (struct elf_note) + - note->namesz_rounded + note->datasz_rounded; + namesz = ROUND_UP(namesz, 4); + datasz = ROUND_UP(datasz, 4); + + *pptr = ptr + namesz + datasz; + return ptr + namesz; } static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, uint32_t flags) { - (void) memset(elf, 0, sizeof(*elf)); + memcpy(elf->e_ident, ELFMAG, SELFMAG); - (void) memcpy(elf->e_ident, ELFMAG, SELFMAG); elf->e_ident[EI_CLASS] = ELF_CLASS; elf->e_ident[EI_DATA] = ELF_DATA; elf->e_ident[EI_VERSION] = EV_CURRENT; @@ -4326,95 +4261,79 @@ static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine, bswap_ehdr(elf); } -static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset) +static void fill_elf_note_phdr(struct elf_phdr *phdr, size_t sz, off_t offset) { phdr->p_type = PT_NOTE; phdr->p_offset = offset; - phdr->p_vaddr = 0; - phdr->p_paddr = 0; phdr->p_filesz = sz; - phdr->p_memsz = 0; - phdr->p_flags = 0; - phdr->p_align = 0; bswap_phdr(phdr, 1); } -static size_t note_size(const struct memelfnote *note) +static void fill_prstatus_note(void *data, const TaskState *ts, + CPUState *cpu, int signr) { - return (note->notesz); + /* + * Because note memory is only aligned to 4, and target_elf_prstatus + * may well have higher alignment requirements, fill locally and + * memcpy to the destination afterward. + */ + struct target_elf_prstatus prstatus = { + .pr_info.si_signo = signr, + .pr_cursig = signr, + .pr_pid = ts->ts_tid, + .pr_ppid = getppid(), + .pr_pgrp = getpgrp(), + .pr_sid = getsid(0), + }; + + elf_core_copy_regs(&prstatus.pr_reg, cpu_env(cpu)); + bswap_prstatus(&prstatus); + memcpy(data, &prstatus, sizeof(prstatus)); } -static void fill_prstatus(struct target_elf_prstatus *prstatus, - const TaskState *ts, int signr) -{ - (void) memset(prstatus, 0, sizeof (*prstatus)); - prstatus->pr_info.si_signo = prstatus->pr_cursig = signr; - prstatus->pr_pid = ts->ts_tid; - prstatus->pr_ppid = getppid(); - prstatus->pr_pgrp = getpgrp(); - prstatus->pr_sid = getsid(0); - - bswap_prstatus(prstatus); -} - -static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts) +static void fill_prpsinfo_note(void *data, const TaskState *ts) { + /* + * Because note memory is only aligned to 4, and target_elf_prpsinfo + * may well have higher alignment requirements, fill locally and + * memcpy to the destination afterward. + */ + struct target_elf_prpsinfo psinfo; char *base_filename; - unsigned int i, len; - - (void) memset(psinfo, 0, sizeof (*psinfo)); + size_t len; len = ts->info->env_strings - ts->info->arg_strings; - if (len >= ELF_PRARGSZ) - len = ELF_PRARGSZ - 1; - if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_strings, len)) { - return -EFAULT; + len = MIN(len, ELF_PRARGSZ); + memcpy(&psinfo.pr_psargs, g2h_untagged(ts->info->arg_strings), len); + for (size_t i = 0; i < len; i++) { + if (psinfo.pr_psargs[i] == 0) { + psinfo.pr_psargs[i] = ' '; + } } - for (i = 0; i < len; i++) - if (psinfo->pr_psargs[i] == 0) - psinfo->pr_psargs[i] = ' '; - psinfo->pr_psargs[len] = 0; - psinfo->pr_pid = getpid(); - psinfo->pr_ppid = getppid(); - psinfo->pr_pgrp = getpgrp(); - psinfo->pr_sid = getsid(0); - psinfo->pr_uid = getuid(); - psinfo->pr_gid = getgid(); + psinfo.pr_pid = getpid(); + psinfo.pr_ppid = getppid(); + psinfo.pr_pgrp = getpgrp(); + psinfo.pr_sid = getsid(0); + psinfo.pr_uid = getuid(); + psinfo.pr_gid = getgid(); base_filename = g_path_get_basename(ts->bprm->filename); /* * Using strncpy here is fine: at max-length, * this field is not NUL-terminated. */ - (void) strncpy(psinfo->pr_fname, base_filename, - sizeof(psinfo->pr_fname)); - + strncpy(psinfo.pr_fname, base_filename, sizeof(psinfo.pr_fname)); g_free(base_filename); - bswap_psinfo(psinfo); - return (0); + + bswap_psinfo(&psinfo); + memcpy(data, &psinfo, sizeof(psinfo)); } -static void fill_auxv_note(struct memelfnote *note, const TaskState *ts) +static void fill_auxv_note(void *data, const TaskState *ts) { - elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv; - elf_addr_t orig_auxv = auxv; - void *ptr; - int len = ts->info->auxv_len; - - /* - * Auxiliary vector is stored in target process stack. It contains - * {type, value} pairs that we need to dump into note. This is not - * strictly necessary but we do it here for sake of completeness. - */ - - /* read in whole auxv vector and copy it to memelfnote */ - ptr = lock_user(VERIFY_READ, orig_auxv, len, 0); - if (ptr != NULL) { - fill_note(note, "CORE", NT_AUXV, len, ptr); - unlock_user(ptr, auxv, len); - } + memcpy(data, g2h_untagged(ts->info->saved_auxv), ts->info->auxv_len); } /* @@ -4462,111 +4381,6 @@ static int dump_write(int fd, const void *ptr, size_t size) return (0); } -static int write_note(struct memelfnote *men, int fd) -{ - struct elf_note en; - - en.n_namesz = men->namesz; - en.n_type = men->type; - en.n_descsz = men->datasz; - - bswap_note(&en); - - if (dump_write(fd, &en, sizeof(en)) != 0) - return (-1); - if (dump_write(fd, men->name, men->namesz_rounded) != 0) - return (-1); - if (dump_write(fd, men->data, men->datasz_rounded) != 0) - return (-1); - - return (0); -} - -static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env) -{ - CPUState *cpu = env_cpu((CPUArchState *)env); - TaskState *ts = (TaskState *)cpu->opaque; - struct elf_thread_status *ets; - - ets = g_malloc0(sizeof (*ets)); - ets->num_notes = 1; /* only prstatus is dumped */ - fill_prstatus(&ets->prstatus, ts, 0); - elf_core_copy_regs(&ets->prstatus.pr_reg, env); - fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus), - &ets->prstatus); - - QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link); - - info->notes_size += note_size(&ets->notes[0]); -} - -static void fill_note_info(struct elf_note_info *info, - int signr, const CPUArchState *env) -{ - CPUState *cpu = env_cpu((CPUArchState *)env); - TaskState *ts = (TaskState *)cpu->opaque; - - memset(info, 0, sizeof (*info)); - QTAILQ_INIT(&info->thread_list); - - /* - * First fill in status (and registers) of current thread - * including process info & aux vector. - */ - fill_prstatus(&info->prstatus, ts, signr); - elf_core_copy_regs(&info->prstatus.pr_reg, env); - fill_note(&info->notes[0], "CORE", NT_PRSTATUS, - sizeof(info->prstatus), &info->prstatus); - fill_psinfo(&info->psinfo, ts); - fill_note(&info->notes[1], "CORE", NT_PRPSINFO, - sizeof(info->psinfo), &info->psinfo); - fill_auxv_note(&info->notes[2], ts); - info->numnote = 3; - - info->notes_size = 0; - for (int i = 0; i < info->numnote; i++) { - info->notes_size += note_size(&info->notes[i]); - } - - /* read and fill status of all threads */ - CPU_FOREACH(cpu) { - if (cpu == thread_cpu) { - continue; - } - fill_thread_info(info, cpu_env(cpu)); - } -} - -static void free_note_info(struct elf_note_info *info) -{ - struct elf_thread_status *ets; - - while (!QTAILQ_EMPTY(&info->thread_list)) { - ets = QTAILQ_FIRST(&info->thread_list); - QTAILQ_REMOVE(&info->thread_list, ets, ets_link); - g_free(ets); - } -} - -static int write_note_info(struct elf_note_info *info, int fd) -{ - struct elf_thread_status *ets; - int i, error = 0; - - /* write prstatus, psinfo and auxv for current thread */ - for (i = 0; i < info->numnote; i++) - if ((error = write_note(&info->notes[i], fd)) != 0) - return (error); - - /* write prstatus for each thread */ - QTAILQ_FOREACH(ets, &info->thread_list, ets_link) { - if ((error = write_note(&ets->notes[0], fd)) != 0) - return (error); - } - - return (0); -} - /* * Write out ELF coredump. * @@ -4615,14 +4429,13 @@ static int elf_core_dump(int signr, const CPUArchState *env) const CPUState *cpu = env_cpu((CPUArchState *)env); const TaskState *ts = (const TaskState *)cpu->opaque; struct vm_area_struct *vma; - struct elf_note_info info; - struct elfhdr elf; - struct elf_phdr phdr; struct rlimit dumpsize; struct mm_struct mm; off_t offset, note_offset, data_offset; + size_t note_size; int segs, cpus, ret; int fd = -1; + CPUState *cpu_iter; if (prctl(PR_GET_DUMPABLE) == 0) { return 0; @@ -4644,7 +4457,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) segs = vma_get_mapping_count(&mm); cpus = 0; - CPU_FOREACH(cpu) { + CPU_FOREACH(cpu_iter) { cpus++; } @@ -4655,6 +4468,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) offset += size_note("CORE", ts->info->auxv_len); offset += size_note("CORE", sizeof(struct target_elf_prpsinfo)); offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus; + note_size = offset - note_offset; offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE); data_offset = offset; @@ -4678,61 +4492,64 @@ static int elf_core_dump(int signr, const CPUArchState *env) } /* - * Construct valid coredump ELF header. We also - * add one more segment for notes. + * There is a fair amount of alignment padding within the notes + * as well as preceeding the process memory. Allocate a zeroed + * block to hold it all. Write all of the headers directly into + * this buffer and then write it out as a block. */ - fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0); - if (dump_write(fd, &elf, sizeof (elf)) != 0) - goto out; + { + g_autofree void *header = g_malloc0(data_offset); + void *hptr, *dptr; - /* fill in the in-memory version of notes */ - fill_note_info(&info, signr, env); + /* Create elf file header. */ + hptr = header; + fill_elf_header(hptr, segs + 1, ELF_MACHINE, 0); + hptr += sizeof(struct elfhdr); - /* write out notes program header */ - fill_elf_note_phdr(&phdr, info.notes_size, note_offset); + /* Create elf program headers. */ + fill_elf_note_phdr(hptr, note_size, note_offset); + hptr += sizeof(struct elf_phdr); - if (dump_write(fd, &phdr, sizeof (phdr)) != 0) - goto out; + offset = data_offset; + for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { + struct elf_phdr *phdr = hptr; - /* - * Write program headers for memory regions mapped in - * the target process. - */ - offset = data_offset; - for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { - (void) memset(&phdr, 0, sizeof (phdr)); + phdr->p_type = PT_LOAD; + phdr->p_offset = offset; + phdr->p_vaddr = vma->vma_start; + phdr->p_paddr = 0; + phdr->p_filesz = vma_dump_size(vma); + offset += phdr->p_filesz; + phdr->p_memsz = vma->vma_end - vma->vma_start; + phdr->p_flags = (vma->vma_flags & PROT_READ ? PF_R : 0) + | (vma->vma_flags & PROT_WRITE ? PF_W : 0) + | (vma->vma_flags & PROT_EXEC ? PF_X : 0); + phdr->p_align = ELF_EXEC_PAGESIZE; - phdr.p_type = PT_LOAD; - phdr.p_offset = offset; - phdr.p_vaddr = vma->vma_start; - phdr.p_paddr = 0; - phdr.p_filesz = vma_dump_size(vma); - offset += phdr.p_filesz; - phdr.p_memsz = vma->vma_end - vma->vma_start; - phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0; - if (vma->vma_flags & PROT_WRITE) - phdr.p_flags |= PF_W; - if (vma->vma_flags & PROT_EXEC) - phdr.p_flags |= PF_X; - phdr.p_align = ELF_EXEC_PAGESIZE; + bswap_phdr(phdr, 1); + hptr += sizeof(struct elf_phdr); + } - bswap_phdr(&phdr, 1); - if (dump_write(fd, &phdr, sizeof(phdr)) != 0) { + /* Create the notes. */ + dptr = fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len); + fill_auxv_note(dptr, ts); + + dptr = fill_note(&hptr, NT_PRPSINFO, "CORE", + sizeof(struct target_elf_prpsinfo)); + fill_prpsinfo_note(dptr, ts); + + CPU_FOREACH(cpu_iter) { + dptr = fill_note(&hptr, NT_PRSTATUS, "CORE", + sizeof(struct target_elf_prstatus)); + fill_prstatus_note(dptr, ts, cpu_iter, + cpu_iter == cpu ? signr : 0); + } + + if (dump_write(fd, header, data_offset) < 0) { goto out; } } - /* - * Next we write notes just after program headers. No - * alignment needed here. - */ - if (write_note_info(&info, fd) < 0) - goto out; - - /* align data to page boundary */ - if (lseek(fd, data_offset, SEEK_SET) != data_offset) - goto out; - /* * Finally we can dump process memory into corefile as well. */ @@ -4768,7 +4585,6 @@ static int elf_core_dump(int signr, const CPUArchState *env) ret = -errno; mmap_unlock(); cpu_list_unlock(); - free_note_info(&info); vma_delete(&mm); close(fd); return ret; From patchwork Tue Feb 27 18:48:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776275 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308466wro; Tue, 27 Feb 2024 10:49:47 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCV6jUi0QDcShM6usSNinThCHfWceRI04XyNwpgpK/03IjVdC1QtuYjoEWpylvkOb9AxDrLw+mvPo7S1a7d1Rc+g X-Google-Smtp-Source: AGHT+IGSJvUZT++xv4YF2thyNbH84dXOio0U2Ti9jslH21Vp66tyZOCRcJWPAyB+8UtfD7ixRNpe X-Received: by 2002:a05:622a:1487:b0:42e:7e53:f279 with SMTP id t7-20020a05622a148700b0042e7e53f279mr9597470qtx.55.1709059787069; Tue, 27 Feb 2024 10:49:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059787; cv=none; d=google.com; s=arc-20160816; b=rLcll2weuMFwCXm6WGI1TiagYxdMoFeJCrT2spWYMtxB55QLylvRt/OhfQxEaCkYjC RdYPciimwgY/n/OGMjNeSeN5lKIo4j3pGBHSgJZ399JW0tHE7aP3qkZpO2nSZYV0bkWP 2wICp1InvV7cCG8sNlhdU44KsCpabZ16onwMbQ4Ar7NemEGGQ4VQZo2GVg6XqUoyHc5u wfa7otaWIQgZdaJ3bJjCKnxH80nklmP5vYzMYyu4Fv3OyHjLRpzQ10hb0yHP3DkEFrMh 4dNXSHk2Yaf5CMFrRRALb6brrcoEO2EkCF4CD+SZ+5IRMR4a9KjIrQCAaWfBKUeguuWO m13g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tzO2W+WajMGPZKg0sOd9kLpT7E3B/88XAmc0t2vBAHU=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=uwSCxgZKkj3/05b5LPKkHvXJrWerKFO4Jf33Dklm+jdzWDOUZh6U1DwbjAFVDVSt5x wpMwZHcE6BP9PU08KHXmKt6QZnzIlriw2m9nHMF1UQtByhLgu3+jmqM2KzAQ74cgom1c UUJezs5Gn8pJqURcAvCsKrv15D6VgFCXwMi2Nafe0+y1p+GfiVyU9SRs7rOZN2IkUOCM d/pbm7B8ItWnbqpeuf+BNBqgY8pcT8U0DQKIAGrXIud1+tTc4VbNDUA3puC0ayeB3gFl KwQstsB/tdYmcoSniT3wgnP7DpS4YXrFC/sOrjhKCpMp+jzkNNLS0461CxAY0xHP+5my WuwA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nJ6ZO8FM; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id v22-20020ac85796000000b0042e80d8c6f6si6124266qta.559.2024.02.27.10.49.46 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:49:47 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nJ6ZO8FM; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2Ve-0006ac-Kd; Tue, 27 Feb 2024 13:48:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2Vd-0006Zm-6g for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:57 -0500 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2VZ-0001Ve-Oh for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:55 -0500 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6e53f76898fso1210354b3a.0 for ; Tue, 27 Feb 2024 10:48:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059731; x=1709664531; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tzO2W+WajMGPZKg0sOd9kLpT7E3B/88XAmc0t2vBAHU=; b=nJ6ZO8FMeMfEoNkbIMVQkx6Gh39+vyaQ4VJBox+JMPRy83EE/xU23mgzqX3eGpeX9u Y4m3MbK1NgPSZIdtqpaBJrhxELAMHRxcVScau7vgbj2BJeeGmrH//P280Lr7MbFDvKKR 3G3kIxP61Ps0oPiA1Cgk1OOTdm40AQXlOCR/gQDQMAUh47D0lSdLiF4xvh/3QnO2oQbn 56E6W+1NCDj9bJeCW2nTOwyuNpuECh9BHXAXfo1e+3Lh9uO3r76waCRAXKQAeUAl73OC T1dRDZkMy5cFEciqC+gwxIpOCJCLT2B8GBiubU1g9LI1s5skTpKWZ0vqqi0ZIIai8DU1 B39w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059731; x=1709664531; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tzO2W+WajMGPZKg0sOd9kLpT7E3B/88XAmc0t2vBAHU=; b=CHCz7wPPpXOjMowAOXs2rCNnmwAP2luC4UxOiDVwcDk52sXSJSZ/7+iLerBIDdVEVv rfCJldFJHl2P2C2cdJvJpuc0UsJkVmlO4iMpRPWkrG62WhG/WF5pbeD8+a5EBwrqD+XO zDifb5a7+2Qms8jZ4mhHGDTTUlJZ6J2eKmeifDiAw9vTlBGMmY5BFaiXJ2TcDf9H4gZU eTIjqfamFifBzDrUFZcgRsuMnjPdfR7MP2dTpevuEgJ/Q6X+8oLvoq2bdfLryjwP55Kw B7fRaD/6+5wh5RC7wLqeVGCWbNsgKGMLtkEFicKPG9NoOiENvzfT+SuodyRZ1Oox3/wA IfhA== X-Gm-Message-State: AOJu0Ywr2zsqZliRQujKLG7xSVfYrnIrHZzE6PQWqsZMZc2pvbwA1Jhh SJuZqVmUztPlGD1wdMm10xJKxlPAevnoY7cXZkvMNwfVzZWG+0Q1uU6gVAb2u+pDEZJIP2xBLKT L X-Received: by 2002:aa7:88d4:0:b0:6e5:456b:bff9 with SMTP id k20-20020aa788d4000000b006e5456bbff9mr4604881pff.12.1709059731642; Tue, 27 Feb 2024 10:48:51 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:51 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 11/14] linux-user/elfload: Write process memory to core file in larger chunks Date: Tue, 27 Feb 2024 08:48:30 -1000 Message-Id: <20240227184833.193836-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::434; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x434.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We do not need to copy pages from guest memory before writing them out. Because vmas are contiguous in host memory, we can write them in one go. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index b8d07d8054..491e754f72 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4551,32 +4551,13 @@ static int elf_core_dump(int signr, const CPUArchState *env) } /* - * Finally we can dump process memory into corefile as well. + * Finally write process memory into the corefile as well. */ for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { - abi_ulong addr; - abi_ulong end; + size_t size = vma_dump_size(vma); - end = vma->vma_start + vma_dump_size(vma); - - for (addr = vma->vma_start; addr < end; - addr += TARGET_PAGE_SIZE) { - char page[TARGET_PAGE_SIZE]; - int error; - - /* - * Read in page from target process memory and - * write it to coredump file. - */ - error = copy_from_user(page, addr, sizeof (page)); - if (error != 0) { - (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n", - addr); - errno = -error; - goto out; - } - if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0) - goto out; + if (size && dump_write(fd, g2h_untagged(vma->vma_start), size) < 0) { + goto out; } } errno = 0; From patchwork Tue Feb 27 18:48:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776278 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308781wro; Tue, 27 Feb 2024 10:50:30 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXEJ72a1pnltZKhDA1LDNqbrKPXkWVJwyPy3QkTJWvgp41fWNQCYMB8WEHJNkh5TbjSzaUXxFNmYrhWROzDD+0U X-Google-Smtp-Source: AGHT+IFGdFlhe5yKo/dDsQyMFskWOms08zPjqfaKpgmUBwR5KvZkUterCsb2vpk/uq1gvDcyb0t1 X-Received: by 2002:a05:620a:2981:b0:787:deab:762b with SMTP id r1-20020a05620a298100b00787deab762bmr3332285qkp.73.1709059830381; Tue, 27 Feb 2024 10:50:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059830; cv=none; d=google.com; s=arc-20160816; b=0jVrVvOls9/ACfNBPou4wV5uK+GYksVlwMVQmeR51ETGPJHoNCFgzJ7uDBhAvV9WHN uAblpqrriX91hquLRoKkTTY9X/59WYmhRlM5E6JDa+3Bdkp+SHddYh97iSMmXCVGtq1t vnkkCQDlIotig+yASR2KLd/50nc7srLS4i0Os+QHcH0ug5qhoL7PkNq51U862qcGEiCI nHr9PeXqPEH6DAKIdKZvpBHa1XSImf7L5ajt3qp33LZTBDyktUDm3Oz+W/R62uyDlCQ1 jPg6AiMVDYbacg4rvEyn1rYE5qcENDqfV42XMe/Vev+aeE7xAsV/hm1hROAh3hYxX6k1 D37A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HEYpUbYOjLF3aHdvtZVlKWz9YeEEY+0zYSzPOHYy/W8=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=MDFADhbSpdO63NXfT4+QPVsGNU0SaddrtyBcniY3bk0YAaNP/4pMeQE6BfPwWaGXeE 2nQwlgVL4Vec37ILPlZasMjoHfQka6i9pWXHkhJcurcQIhvnUAJ7oLUWx5+8f9x2DcqZ QtQj+zl/PKA9dS7p3OQRufokNc9ivHGj4a1zO0iagDdBohVYCSamARlfRS4XKj0iDLrA 7DPRYxGZnpJco57JGZ4mPXZ5xI8bQEM8MrAE4Zhx+NCD+tXyoSJSdBzhggmGhHVvGluu v4o0i5asj6EHjFAbpTDtwU+txIGVkxdGi0VCZ/3z4VKsYsK/B17QVdWtJZs26fKYot+a VXNg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pi2j9o6g; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id e22-20020a05620a209600b00787a8c79efdsi8057568qka.370.2024.02.27.10.50.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:50:30 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=pi2j9o6g; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2Vf-0006ao-CB; Tue, 27 Feb 2024 13:48:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2Vd-0006aD-D7 for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:57 -0500 Received: from mail-ot1-x334.google.com ([2607:f8b0:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2Vb-0001Vn-LS for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:57 -0500 Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-6e125818649so1982519a34.1 for ; Tue, 27 Feb 2024 10:48:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059733; x=1709664533; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HEYpUbYOjLF3aHdvtZVlKWz9YeEEY+0zYSzPOHYy/W8=; b=pi2j9o6gBo6MEgj+eyxSTpPgS+q05HwNC4LeS8wRzuLC5k/H+/cgZi0rIbCTxih//j nQyadiGio4UqWp4/swxAknKLr+slvGuBm19KTmg3tQ3g9oUlChqRUnm1d1ooZXA2UF+J HuMgE22ctaV37rergmKORHigd5v+EJ1+IInhwrUA9kf44Qbez1TbD8QdtWuX32raIaYq TdVx/WMItIqUZgG+ArMDllWCHu46eWmvwL0Pkbj01FD6ZYizQfAuZpzFAbGu84QYAAgh oxiCeQ+UucDcKpj6bmww4W2jBSB/dUb0X1MB5DPeL8v1PEECsvy3DU977MGib1bUpoRP xTIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059733; x=1709664533; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HEYpUbYOjLF3aHdvtZVlKWz9YeEEY+0zYSzPOHYy/W8=; b=h4zU6Vnk7NEqePkpp900xEGbkeoh8bwDF3nuI2qL47TNqEacb8432klxas4E8pK5F0 2Qm7I9UP+qTtxIQGkDkst3MMXgw8vqcHcapBnQ32DskfnqYk3jKotcREyrfzl57dx3qC 0oIElwagE9mbvsxsfxD6Pi8oNikTH5NcrZrWIJmRxdxS4CVjXtB5eg4dfqRbvEyr4a/a 0Q8LxxwNgHcP5PWUQYwGzj1DyWvyNCjC1Ii/iNDg3l4z6/0UHpiOBEjeII2ydI5jXrYm UOcYGdW/TaMFgmrsmwGw1K+szBSODl2ne5IrgCWpadz7+/0b+RkuLPyGS6xpThPWg0DO mZZg== X-Gm-Message-State: AOJu0YzdqDTI7WWuMVU/GyVl/NM9Rlh/wAZYi0lQXTAvE7R51fKzCNnA fX6roM1R68b0yO0l82JDlcpHYc/c1R6qVj7p3nxmYqvWxT9lNjtf0E6yhKuTi4ckgWnDE587CL9 1 X-Received: by 2002:a9d:6d8f:0:b0:6e2:e5a3:1732 with SMTP id x15-20020a9d6d8f000000b006e2e5a31732mr11607472otp.37.1709059732888; Tue, 27 Feb 2024 10:48:52 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:52 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 12/14] linux-user/elfload: Simplify vma_dump_size Date: Tue, 27 Feb 2024 08:48:31 -1000 Message-Id: <20240227184833.193836-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::334; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x334.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Use the flags that we've already saved in order to test accessibility. Use g2h_untagged and compare guest memory directly instead of copy_from_user. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 491e754f72..47b5ce3005 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4166,37 +4166,23 @@ static int vma_get_mapping_count(const struct mm_struct *mm) */ static abi_ulong vma_dump_size(const struct vm_area_struct *vma) { - /* if we cannot even read the first page, skip it */ - if (!access_ok_untagged(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE)) - return (0); + /* The area must be readable. */ + if (!(vma->vma_flags & PROT_READ)) { + return 0; + } /* * Usually we don't dump executable pages as they contain * non-writable code that debugger can read directly from - * target library etc. However, thread stacks are marked - * also executable so we read in first page of given region - * and check whether it contains elf header. If there is - * no elf header, we dump it. + * target library etc. If there is no elf header, we dump it. */ - if (vma->vma_flags & PROT_EXEC) { - char page[TARGET_PAGE_SIZE]; - - if (copy_from_user(page, vma->vma_start, sizeof (page))) { - return 0; - } - if ((page[EI_MAG0] == ELFMAG0) && - (page[EI_MAG1] == ELFMAG1) && - (page[EI_MAG2] == ELFMAG2) && - (page[EI_MAG3] == ELFMAG3)) { - /* - * Mappings are possibly from ELF binary. Don't dump - * them. - */ - return (0); - } + if (!(vma->vma_flags & PROT_WRITE) && + (vma->vma_flags & PROT_EXEC) && + memcmp(g2h_untagged(vma->vma_start), ELFMAG, SELFMAG) == 0) { + return 0; } - return (vma->vma_end - vma->vma_start); + return vma->vma_end - vma->vma_start; } static int vma_walker(void *priv, target_ulong start, target_ulong end, From patchwork Tue Feb 27 18:48:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776281 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308972wro; Tue, 27 Feb 2024 10:50:59 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUAjvDKIPSwkiUwfCYD4hZxnRmIoDLOUdgfVx6U5e6yoVBtyYC1N/FSbmFjf+fCVwXb/1iCE8JaMt8KJtdwTino X-Google-Smtp-Source: AGHT+IFL4hJ4up6llrS5CZSJvZ9EJzc19QaDI0X0++u31UEgbRCZKSP5xe686pzQYJ8T9HtuaJBF X-Received: by 2002:a05:6902:100a:b0:dcc:f2a4:153e with SMTP id w10-20020a056902100a00b00dccf2a4153emr340029ybt.46.1709059859606; Tue, 27 Feb 2024 10:50:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059859; cv=none; d=google.com; s=arc-20160816; b=XQSb4CRmv4JftaQk2D5BfYl2e/+ZPhGcRmDvhyagVJdTVqWW8HWNDTMGrmlf3xE7gT VwS5++k/lnNnlDzWIOls621MB8e007ANKLz1BHpJt/lZ6JnwLpx/yIPe+zqKIQP7uEub o9UqXhS3FzrQQSkR3LpH3HWXgUFuHSnNxiNQTREDniCnDuZh6FLVX1WKLA3digE9TVg3 HJqV5QvhZRYG4GkJSOxcERHusrV3GzYkrlvIGIWxqIE3Wgk8XgIeATCKWnlduAWu0Ir5 d5vE02nl9nGdXuimN6LAj2zncMPTyGqYKdO5OZl1u5Ftq0a7D8dFkj3zKnPiWmQF6B/z 0xgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mVP5ZXMph2vIaMrgjNZyRxPQx2YhCwiWw0i2ONELss0=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=mGVzNzGShEe7yJVSjUHV1O6BLsng6YEuT9q74HfzkYDcCofSw5+AYkRjHc7Lk7yd7o Da2uh8aoqtpvI2K3qIPVsTC+IVe7+HyIgTLJvAn4ECLBMrQHyQELKXRdcOJyGgdeulMD /OjF2vxZoIPP/5XFzR9j+/mR0fy+uKQXeI+soGoJG0XpjwmwJ5PkDpnHZuP/ymik4dPl B5bpoIpBu3P87IQDU/eEO5OeNKfYUIvmxlG1BFr7iv1reVjU7aShfRCuxIKI5PnSpYlt toGDB/guhGtOdD1zUe4o6pMmzt5SPI1IWbBZ6yBvU4EXh3mgZyNB5HOQH95vmECCteTf 2eew==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jUonchxB; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id b13-20020ac87fcd000000b0042e42a02531si8698608qtk.192.2024.02.27.10.50.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:50:59 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jUonchxB; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2Vh-0006bp-9V; Tue, 27 Feb 2024 13:49:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2Vf-0006at-3N for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:59 -0500 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2Vc-0001W6-Df for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:48:58 -0500 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-6e435542d41so3144896b3a.1 for ; Tue, 27 Feb 2024 10:48:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059734; x=1709664534; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mVP5ZXMph2vIaMrgjNZyRxPQx2YhCwiWw0i2ONELss0=; b=jUonchxBXfrSUgZCvU/gMSmnrOX8R+aToUhjCvv+aKLMma35dd4WNy1SQnxbb+Yzoo wWroeel3cAnOVXDM7BnfNf0cyGlK1Hi0SDkC5MZbJr9DsaLwWre30wch95vhMpsTpOCO 3K2RKSAPykzeVT8fp9jRujiKTfAXGEX2nsxpeZF/y3B//FHXz8j/wO8gNmahl5G6Yv/T QNWasrvLHjkoVXyVMeKOmGgBzU95PirffRSNB1y3aGLp5u2TsM9ReO9r5zpmI4XhqR0y Q3zKRctNZiESXfG0boE9xVgZrwJSVCD4k0Aman6UF2hm72vyQ1b762/ipjZZa8zOcpJ1 czZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059734; x=1709664534; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mVP5ZXMph2vIaMrgjNZyRxPQx2YhCwiWw0i2ONELss0=; b=KcOcSykjbbjROfn6/uEpqFOLitB5EgC/0PfEGPwvKjP2Ih77lLfrEjCy4w2k+wyjCr 0PxcoJqcpimo6kxHwDL0/XcAT35YGoo1QOLoWbRpthDVUzOLXu7YSIsHUAuz7lug8lqb 4w1pv42WKGPMzAzehkYnkMq/UTqqvKrq59Ak2v8EtaJwD7NO0di5q7csy2UialsDFiSR q2/n3syh9nzlqpBNZT57+RiDDpzqC/Zl4GUuuE2Um2gm8/JovQwXQ7L/MxrxSLu2v9PA W5Fz+HrLoeZ6TWIsRH7KyFU6Fs6ZX0A6oXmYZ5rsJZ2N8x+8h5oqbLW/GcF/eorNlbxR +5Cw== X-Gm-Message-State: AOJu0Yy19uirgv0wLdKEhsyG34MceaatDNysU5fBUEvNEwp2eYoiUO9O 0jLL1qi1Tqy7IvrlvXNGOOJhQ43VLbV4n0sVcYUhqKjagJWB9yHt8Wb9qswFjsWnzoH3wMOAeGQ G X-Received: by 2002:a05:6a00:1ac7:b0:6e4:d198:6d66 with SMTP id f7-20020a056a001ac700b006e4d1986d66mr13089692pfv.7.1709059734358; Tue, 27 Feb 2024 10:48:54 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:53 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 13/14] linux-user/elfload: Rely on walk_memory_regions for vmas Date: Tue, 27 Feb 2024 08:48:32 -1000 Message-Id: <20240227184833.193836-14-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Rather than creating new data structures for vma, rely on the IntervalTree used by walk_memory_regions. Use PAGE_* constants, per the page table api, rather than PROT_* constants, per the mmap api. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 213 ++++++++++++++++--------------------------- 1 file changed, 78 insertions(+), 135 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 47b5ce3005..ae0abc4931 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4041,29 +4041,6 @@ struct target_elf_prpsinfo { char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ }; -struct vm_area_struct { - target_ulong vma_start; /* start vaddr of memory region */ - target_ulong vma_end; /* end vaddr of memory region */ - abi_ulong vma_flags; /* protection etc. flags for the region */ - QTAILQ_ENTRY(vm_area_struct) vma_link; -}; - -struct mm_struct { - QTAILQ_HEAD(, vm_area_struct) mm_mmap; - int mm_count; /* number of mappings */ -}; - -static void vma_init(struct mm_struct *); -static void vma_delete(struct mm_struct *); -static int vma_add_mapping(struct mm_struct *, target_ulong, - target_ulong, abi_ulong); -static int vma_get_mapping_count(const struct mm_struct *); -static struct vm_area_struct *vma_first(const struct mm_struct *); -static struct vm_area_struct *vma_next(struct vm_area_struct *); -static abi_ulong vma_dump_size(const struct vm_area_struct *); -static int vma_walker(void *priv, target_ulong start, target_ulong end, - unsigned long flags); - #ifdef BSWAP_NEEDED static void bswap_prstatus(struct target_elf_prstatus *prstatus) { @@ -4105,69 +4082,14 @@ static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {} static inline void bswap_note(struct elf_note *en) { } #endif /* BSWAP_NEEDED */ -/* - * Minimal support for linux memory regions. These are needed - * when we are finding out what memory exactly belongs to - * emulated process. No locks needed here, as long as - * thread that received the signal is stopped. - */ - -static void vma_init(struct mm_struct *mm) -{ - mm->mm_count = 0; - QTAILQ_INIT(&mm->mm_mmap); -} - -static void vma_delete(struct mm_struct *mm) -{ - struct vm_area_struct *vma; - - while ((vma = vma_first(mm)) != NULL) { - QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link); - g_free(vma); - } -} - -static int vma_add_mapping(struct mm_struct *mm, target_ulong start, - target_ulong end, abi_ulong flags) -{ - struct vm_area_struct *vma; - - if ((vma = g_malloc0(sizeof (*vma))) == NULL) - return (-1); - - vma->vma_start = start; - vma->vma_end = end; - vma->vma_flags = flags; - - QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link); - mm->mm_count++; - - return (0); -} - -static struct vm_area_struct *vma_first(const struct mm_struct *mm) -{ - return (QTAILQ_FIRST(&mm->mm_mmap)); -} - -static struct vm_area_struct *vma_next(struct vm_area_struct *vma) -{ - return (QTAILQ_NEXT(vma, vma_link)); -} - -static int vma_get_mapping_count(const struct mm_struct *mm) -{ - return (mm->mm_count); -} - /* * Calculate file (dump) size of given memory region. */ -static abi_ulong vma_dump_size(const struct vm_area_struct *vma) +static size_t vma_dump_size(target_ulong start, target_ulong end, + unsigned long flags) { /* The area must be readable. */ - if (!(vma->vma_flags & PROT_READ)) { + if (!(flags & PAGE_READ)) { return 0; } @@ -4176,22 +4098,13 @@ static abi_ulong vma_dump_size(const struct vm_area_struct *vma) * non-writable code that debugger can read directly from * target library etc. If there is no elf header, we dump it. */ - if (!(vma->vma_flags & PROT_WRITE) && - (vma->vma_flags & PROT_EXEC) && - memcmp(g2h_untagged(vma->vma_start), ELFMAG, SELFMAG) == 0) { + if (!(flags & PAGE_WRITE_ORG) && + (flags & PAGE_EXEC) && + memcmp(g2h_untagged(start), ELFMAG, SELFMAG) == 0) { return 0; } - return vma->vma_end - vma->vma_start; -} - -static int vma_walker(void *priv, target_ulong start, target_ulong end, - unsigned long flags) -{ - struct mm_struct *mm = (struct mm_struct *)priv; - - vma_add_mapping(mm, start, end, flags); - return (0); + return end - start; } static size_t size_note(const char *name, size_t datasz) @@ -4367,6 +4280,61 @@ static int dump_write(int fd, const void *ptr, size_t size) return (0); } +typedef struct { + unsigned count; + size_t size; +} CountAndSizeRegions; + +static int wmr_count_and_size_regions(void *opaque, target_ulong start, + target_ulong end, unsigned long flags) +{ + CountAndSizeRegions *css = opaque; + + css->count++; + css->size += vma_dump_size(start, end, flags); + return 0; +} + +typedef struct { + struct elf_phdr *phdr; + off_t offset; +} FillRegionPhdr; + +static int wmr_fill_region_phdr(void *opaque, target_ulong start, + target_ulong end, unsigned long flags) +{ + FillRegionPhdr *d = opaque; + struct elf_phdr *phdr = d->phdr; + + phdr->p_type = PT_LOAD; + phdr->p_vaddr = start; + phdr->p_paddr = 0; + phdr->p_filesz = vma_dump_size(start, end, flags); + phdr->p_offset = d->offset; + d->offset += phdr->p_filesz; + phdr->p_memsz = end - start; + phdr->p_flags = (flags & PAGE_READ ? PF_R : 0) + | (flags & PAGE_WRITE_ORG ? PF_W : 0) + | (flags & PAGE_EXEC ? PF_X : 0); + phdr->p_align = ELF_EXEC_PAGESIZE; + + bswap_phdr(phdr, 1); + d->phdr = phdr + 1; + return 0; +} + +static int wmr_write_region(void *opaque, target_ulong start, + target_ulong end, unsigned long flags) +{ + int fd = *(int *)opaque; + size_t size = vma_dump_size(start, end, flags); + + if (!size) { + return 0; + } + return dump_write(fd, g2h_untagged(start), size); +} + /* * Write out ELF coredump. * @@ -4414,12 +4382,11 @@ static int elf_core_dump(int signr, const CPUArchState *env) { const CPUState *cpu = env_cpu((CPUArchState *)env); const TaskState *ts = (const TaskState *)cpu->opaque; - struct vm_area_struct *vma; struct rlimit dumpsize; - struct mm_struct mm; + CountAndSizeRegions css; off_t offset, note_offset, data_offset; size_t note_size; - int segs, cpus, ret; + int cpus, ret; int fd = -1; CPUState *cpu_iter; @@ -4438,9 +4405,8 @@ static int elf_core_dump(int signr, const CPUArchState *env) * Walk through target process memory mappings and * set up structure containing this information. */ - vma_init(&mm); - walk_memory_regions(&mm, vma_walker); - segs = vma_get_mapping_count(&mm); + memset(&css, 0, sizeof(css)); + walk_memory_regions(&css, wmr_count_and_size_regions); cpus = 0; CPU_FOREACH(cpu_iter) { @@ -4448,22 +4414,18 @@ static int elf_core_dump(int signr, const CPUArchState *env) } offset = sizeof(struct elfhdr); - offset += (segs + 1) * sizeof(struct elf_phdr); + offset += (css.count + 1) * sizeof(struct elf_phdr); note_offset = offset; offset += size_note("CORE", ts->info->auxv_len); offset += size_note("CORE", sizeof(struct target_elf_prpsinfo)); offset += size_note("CORE", sizeof(struct target_elf_prstatus)) * cpus; note_size = offset - note_offset; - offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE); - data_offset = offset; - - for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { - offset += vma_dump_size(vma); - } + data_offset = ROUND_UP(offset, ELF_EXEC_PAGESIZE); /* Do not dump if the corefile size exceeds the limit. */ - if (dumpsize.rlim_cur != RLIM_INFINITY && dumpsize.rlim_cur < offset) { + if (dumpsize.rlim_cur != RLIM_INFINITY + && dumpsize.rlim_cur < data_offset + css.size) { errno = 0; goto out; } @@ -4485,36 +4447,22 @@ static int elf_core_dump(int signr, const CPUArchState *env) */ { g_autofree void *header = g_malloc0(data_offset); + FillRegionPhdr frp; void *hptr, *dptr; /* Create elf file header. */ hptr = header; - fill_elf_header(hptr, segs + 1, ELF_MACHINE, 0); + fill_elf_header(hptr, css.count + 1, ELF_MACHINE, 0); hptr += sizeof(struct elfhdr); /* Create elf program headers. */ fill_elf_note_phdr(hptr, note_size, note_offset); hptr += sizeof(struct elf_phdr); - offset = data_offset; - for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { - struct elf_phdr *phdr = hptr; - - phdr->p_type = PT_LOAD; - phdr->p_offset = offset; - phdr->p_vaddr = vma->vma_start; - phdr->p_paddr = 0; - phdr->p_filesz = vma_dump_size(vma); - offset += phdr->p_filesz; - phdr->p_memsz = vma->vma_end - vma->vma_start; - phdr->p_flags = (vma->vma_flags & PROT_READ ? PF_R : 0) - | (vma->vma_flags & PROT_WRITE ? PF_W : 0) - | (vma->vma_flags & PROT_EXEC ? PF_X : 0); - phdr->p_align = ELF_EXEC_PAGESIZE; - - bswap_phdr(phdr, 1); - hptr += sizeof(struct elf_phdr); - } + frp.phdr = hptr; + frp.offset = data_offset; + walk_memory_regions(&frp, wmr_fill_region_phdr); + hptr = frp.phdr; /* Create the notes. */ dptr = fill_note(&hptr, NT_AUXV, "CORE", ts->info->auxv_len); @@ -4539,12 +4487,8 @@ static int elf_core_dump(int signr, const CPUArchState *env) /* * Finally write process memory into the corefile as well. */ - for (vma = vma_first(&mm); vma != NULL; vma = vma_next(vma)) { - size_t size = vma_dump_size(vma); - - if (size && dump_write(fd, g2h_untagged(vma->vma_start), size) < 0) { - goto out; - } + if (walk_memory_regions(&fd, wmr_write_region) < 0) { + goto out; } errno = 0; @@ -4552,7 +4496,6 @@ static int elf_core_dump(int signr, const CPUArchState *env) ret = -errno; mmap_unlock(); cpu_list_unlock(); - vma_delete(&mm); close(fd); return ret; } From patchwork Tue Feb 27 18:48:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 776274 Delivered-To: patch@linaro.org Received: by 2002:adf:a113:0:b0:33d:f458:43ce with SMTP id o19csp308341wro; Tue, 27 Feb 2024 10:49:29 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWPHznRm3IR84XbUKR2BBINH3ttO0mK1YGpdfSPWW79efD61RBCnCbptCxxrqZtUVNmCjyrWHMtQKDQe7oRFLkm X-Google-Smtp-Source: AGHT+IHf+W8Ld41ZM93GdEVZH52CnKZgrpLgQrEB2wa7n+f4+8Pm9scGjhprZBe4+SozjmENUj2V X-Received: by 2002:a05:622a:1a8d:b0:42e:8d15:41b8 with SMTP id s13-20020a05622a1a8d00b0042e8d1541b8mr5555349qtc.55.1709059769482; Tue, 27 Feb 2024 10:49:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709059769; cv=none; d=google.com; s=arc-20160816; b=eOghuKvXQQaJeLzcoeOIplvnNA7ADN2+BOrF7vjuo1gtT+WvnWAAY1rU3uQ9twX3No 8uF3ygSuTPLy/Ka+uMKF4a/flYnT22yReCazroPqxBjBhBU2z4TBlpUrjCgSslrR9r6l YOnFWdZRCn2dlNRkrvEvjJBGYTHlIysJSwn6+GtnxFCL+ZPzH+Ig5QEPOaIJhjzjcncX zOZWdNR6zYm6muWbDFzidxyKQr+tGTPIf3NPltFB9Yd5uk36WGc6l0GKsAARmqzC9rNn G5E6IaJcjZFUJsYZ9QKG+4z37KZhKp00eMqjUuagZPKnueyc7/4Xr37fzUq9px8rsrxG HdLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2XrEdDvQ88jaxWmSDzzj/y67fD3f8yoeJckZhxITx78=; fh=Vvliz9ENSk/yCY42nG87aL5dW+Zo0lduSx74U3lH2V8=; b=v8UQ8xuu6hLikuhYZlMvJKjN5nXXQJf0eGQ22XyljxQn2MC1ieHmzJhOSbdChshDAU Nq6BVKkNVJ4Yg8tV6o3FWC6fmS06j8S/GrrAcCCHL0HIGRofTWnbZ4slMKSVxn9+pYVz 7oJ8qbxSV/pr1hq/5iWQLOvAotycXs3GPN8Coz3i4hKqT+0UCImUulW48ZJU9aW2/zuV INg+xK89oD9ScPlIfLRgnqDT94vnZaGtB8ylBwDfpDiEGV4MgULBLF2pW7aJ7WVel5h7 BoC1VURgzpvucjTXefdVYHL0FW/3L7HVwZtwHscs0i2x9V1HX/+6lTIo7llw3lV69s6i k3jQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bk2bEhas; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id q9-20020a05622a030900b0042dbe58b542si8573262qtw.704.2024.02.27.10.49.29 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Feb 2024 10:49:29 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bk2bEhas; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rf2Vh-0006bt-LO; Tue, 27 Feb 2024 13:49:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rf2Vg-0006bT-Eg for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:49:00 -0500 Received: from mail-oi1-x235.google.com ([2607:f8b0:4864:20::235]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rf2Ve-0001WI-3p for qemu-devel@nongnu.org; Tue, 27 Feb 2024 13:49:00 -0500 Received: by mail-oi1-x235.google.com with SMTP id 5614622812f47-3c19bc08f96so2645678b6e.2 for ; Tue, 27 Feb 2024 10:48:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709059735; x=1709664535; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2XrEdDvQ88jaxWmSDzzj/y67fD3f8yoeJckZhxITx78=; b=bk2bEhaspsIlDdm37ktyEla0/+/heZjF5/vny1ifWJlHAJ4L76lWvtpPdBmnKaKpKB y93Rwe8e3yo+OVJnX2uvyZiP2V3AMqI6rPAHLLGcsqJn93UHXY5KYxBX79yBsb7rhjAs S3iSOH8VBJdJ3rLqHCgEPqsDtr8phsbwLdVtkOqjWl8OEFQ9aF2Uhq/bQoHj7DKqECZr BH89+pweLb/GdaS2rDClziAFdwxZaCI0NiK89QcNmJ8+CltCWKWa4oQe3Y6AxGcIV4EJ jFNOAGfdYQRKB+brsZSs9+38o9/R0AE4LN3Y153ph5dtydkKdWtzckm/pvhICPu/y9Td Qzrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709059735; x=1709664535; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2XrEdDvQ88jaxWmSDzzj/y67fD3f8yoeJckZhxITx78=; b=jKWh3n0MWM7CajGpKSOagmyThxoGV8gc5jSyMwTuIxjW4kcLyrHS7T7Yh3bXLXJDef A35UExPfa5UX6DfxdwmXwdKCG2d7fttv5o86JDE4+Eljsibup/Z6wNdskr0z2xlme8xD oJLAMkFGEitC60z0SesXZunPzpe96dSR7F7b9IdyxD7frQg+fEMbCIm8WaLGM5LSFt8w i/sNRiPDvpsl14hr8YkiZlb9aesq0O6Lo1aDilkI8WGTUJ35/Ra1fUMCh7J2tuhqdhOB XU6Hq4fOj7SDKlwkydl5PjPGVlKZQFUIOuNKr7U78Q4BLHgzeDjrseqc41/+Kk/qXuAE IQGg== X-Gm-Message-State: AOJu0YxGKXBBElc4pFs0IuiFd20RI2qMGt9shsxs1eZUXuyaYi1zKW03 7WL9fMpJTxzdsoCrXLvOWdf8zCZNU2ksrpAWjj2bcKiuSvxrncylamGRf6iwGDGIHYEp82QxMJF P X-Received: by 2002:a05:6808:14c7:b0:3bf:bf81:c700 with SMTP id f7-20020a05680814c700b003bfbf81c700mr3445815oiw.16.1709059735654; Tue, 27 Feb 2024 10:48:55 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id h26-20020aa786da000000b006e555d40951sm533465pfo.188.2024.02.27.10.48.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:48:55 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org Subject: [PATCH 14/14] linux-user/elfload: Unprotect regions before core dump Date: Tue, 27 Feb 2024 08:48:33 -1000 Message-Id: <20240227184833.193836-15-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240227184833.193836-1-richard.henderson@linaro.org> References: <20240227184833.193836-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::235; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x235.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org By unprotecting regions, we re-instate writability and unify regions that have been split, which may reduce the total number of regions. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index ae0abc4931..38bfc9ac67 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3963,6 +3963,8 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) } #ifdef USE_ELF_CORE_DUMP +#include "exec/translate-all.h" + /* * Definitions to generate Intel SVR4-like core files. * These mostly have the same names as the SVR4 types with "target_elf_" @@ -4280,6 +4282,23 @@ static int dump_write(int fd, const void *ptr, size_t size) return (0); } +static int wmr_page_unprotect_regions(void *opaque, target_ulong start, + target_ulong end, unsigned long flags) +{ + if ((flags & (PAGE_WRITE | PAGE_WRITE_ORG)) == PAGE_WRITE_ORG) { + size_t step = MAX(TARGET_PAGE_SIZE, qemu_host_page_size); + + while (1) { + page_unprotect(start, 0); + if (end - start <= step) { + break; + } + start += step; + } + } + return 0; +} + typedef struct { unsigned count; size_t size; @@ -4401,6 +4420,9 @@ static int elf_core_dump(int signr, const CPUArchState *env) cpu_list_lock(); mmap_lock(); + /* By unprotecting, we merge vmas that might be split. */ + walk_memory_regions(NULL, wmr_page_unprotect_regions); + /* * Walk through target process memory mappings and * set up structure containing this information.