From patchwork Tue Aug 22 20:34:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 1824366 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=VLZUFW0p; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RVh1G1FwNz1yg8 for ; Wed, 23 Aug 2023 06:35:12 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYY51-0002dD-E9; Tue, 22 Aug 2023 16:34:23 -0400 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 1qYY4z-0002bF-C2 for qemu-devel@nongnu.org; Tue, 22 Aug 2023 16:34:21 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qYY4x-0005EA-0x for qemu-devel@nongnu.org; Tue, 22 Aug 2023 16:34:21 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1bee82fad0fso31914915ad.2 for ; Tue, 22 Aug 2023 13:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692736457; x=1693341257; 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=dcsO2dVLbPUl+GizLMpXodQLIxyTwrwgsESLGfn+SH8=; b=VLZUFW0puMsbd5bEY4Jg2KILmem0iqqISnxTFNRXVTPGqFgmCli0q03YRfVJKAsMHH ktpNrL6fBoIOuQrj62rOb0TgrlT3N7+NJU+hZSs7Qlf8IlJLyfhjbgDRa2EZxhlJertR 5K0J/JS6dqz+aojWgPW6kLW4ByNQlI7k1HR+QRE/J99vDxbLyc5uwHnFhtr8Ca6M2n64 /HSsRKd5PKS8w4xfTux0az6O8TgIeOBBS3BdFhKtSNeHnvWv6it1E3fCvqVqDd1AW3M6 BP2U0DikWSlwb3GoMKJcIT2ZJLMMFKi+5YLKa49bzO2vak9+TAlYmCRymO6xXsdP4hxt 7cWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692736457; x=1693341257; 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=dcsO2dVLbPUl+GizLMpXodQLIxyTwrwgsESLGfn+SH8=; b=UpE97oomXayjV2KhBuX//lmika8xKqNvhCMHjw6Vq/2MmJPWDhJmFhAqcmFBIVma3d LMYMMeFQ5uOjx9IKPkFgW8PNQeD48hzYc21kOuAZqlLOcpylDdz9o4guWBVOQ0J5mq00 mpyc6HvVgSRFskzdSEkv9jAnZVk2hORM0pGF70Hiop/ghcTqpUbDj58NbcM2ooKZQx/+ 8drVb8uFDO5I5QhkJwm3rbpiXXatNhbqPb/zMAKJa8ZTFKVxx4BsWd6kJRztV3o67t+a fbuWCNF5dSRjKUPgbWweUtf1mdb88hLvbKul/XO6/9gJwbvd3LgRGp0m/b6WNTD5DzCF Ducg== X-Gm-Message-State: AOJu0Yw01fffsG93pGAapYJOYc3qUTpsj8xksobwKjEFFDFZCexqnC3i sKpX0HKPNngz7PPKBYKEF+eXGBULBtDKt+ibw4Q= X-Google-Smtp-Source: AGHT+IFDMhMe/74JyadU5Bv2WCqqyeq9KJZdivNwlA5cBAvpbVDhsWFS/8d1mGSrj4Qra+J0EE7X+A== X-Received: by 2002:a17:902:ab54:b0:1bc:2bd:8523 with SMTP id ij20-20020a170902ab5400b001bc02bd8523mr7431103plb.24.1692736457507; Tue, 22 Aug 2023 13:34:17 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:4e3c:f4a4:b92a:b5ab]) by smtp.gmail.com with ESMTPSA id i25-20020a633c59000000b00564aee22f33sm8311823pgn.14.2023.08.22.13.34.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 13:34:17 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: deller@gmx.de Subject: [PATCH v3 1/3] linux-user: Split out die_with_signal Date: Tue, 22 Aug 2023 13:34:13 -0700 Message-Id: <20230822203415.1184491-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230822203415.1184491-1-richard.henderson@linaro.org> References: <20230822203415.1184491-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62b.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 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Because we trap so many signals for use by the guest, we have to take extra steps to exit properly. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- linux-user/signal.c | 52 ++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/linux-user/signal.c b/linux-user/signal.c index 748a98f3e5..e1cd111a1b 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -694,6 +694,33 @@ void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr, } /* abort execution with signal */ +static G_NORETURN +void die_with_signal(int sig) +{ + struct sigaction act = { + .sa_handler = SIG_DFL, + }; + + /* + * The proper exit code for dying from an uncaught signal is -. + * The kernel doesn't allow exit() or _exit() to pass a negative value. + * To get the proper exit code we need to actually die from an uncaught + * signal. Here the default signal handler is installed, we send + * the signal and we wait for it to arrive. + */ + sigfillset(&act.sa_mask); + sigaction(sig, &act, NULL); + + kill(getpid(), sig); + + /* Make sure the signal isn't masked (reusing the mask inside of act). */ + sigdelset(&act.sa_mask, sig); + sigsuspend(&act.sa_mask); + + /* unreachable */ + abort(); +} + static G_NORETURN void dump_core_and_abort(CPUArchState *cpu_env, int target_sig) { @@ -701,7 +728,6 @@ void dump_core_and_abort(CPUArchState *cpu_env, int target_sig) CPUArchState *env = cpu->env_ptr; TaskState *ts = (TaskState *)cpu->opaque; int host_sig, core_dumped = 0; - struct sigaction act; host_sig = target_to_host_signal(target_sig); trace_user_dump_core_and_abort(env, target_sig, host_sig); @@ -725,29 +751,7 @@ void dump_core_and_abort(CPUArchState *cpu_env, int target_sig) } preexit_cleanup(cpu_env, 128 + target_sig); - - /* The proper exit code for dying from an uncaught signal is - * -. The kernel doesn't allow exit() or _exit() to pass - * a negative value. To get the proper exit code we need to - * actually die from an uncaught signal. Here the default signal - * handler is installed, we send ourself a signal and we wait for - * it to arrive. */ - sigfillset(&act.sa_mask); - act.sa_handler = SIG_DFL; - act.sa_flags = 0; - sigaction(host_sig, &act, NULL); - - /* For some reason raise(host_sig) doesn't send the signal when - * statically linked on x86-64. */ - kill(getpid(), host_sig); - - /* Make sure the signal isn't masked (just reuse the mask inside - of act) */ - sigdelset(&act.sa_mask, host_sig); - sigsuspend(&act.sa_mask); - - /* unreachable */ - abort(); + die_with_signal(host_sig); } /* queue a signal so that it will be send to the virtual CPU as soon From patchwork Tue Aug 22 20:34:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 1824367 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=yVWdYHiN; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RVh1G1jNhz1ygy for ; Wed, 23 Aug 2023 06:35:12 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYY52-0002dk-9Z; Tue, 22 Aug 2023 16:34:24 -0400 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 1qYY50-0002c8-HG for qemu-devel@nongnu.org; Tue, 22 Aug 2023 16:34:22 -0400 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qYY4x-0005EH-Pm for qemu-devel@nongnu.org; Tue, 22 Aug 2023 16:34:22 -0400 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-68a42d06d02so1933384b3a.0 for ; Tue, 22 Aug 2023 13:34:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692736458; x=1693341258; 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=rQp++5N0yIEsVmUaGN89LifNvR6oAeooLY4sPs12hmU=; b=yVWdYHiN0iMeyApJ2LM+nQ5ob8Nt3TpoIIJkbrHw43x5aXzcKBB21Shw4GowNq5Jif 6i1gTv13B5vbH0ZUrgiF3L2TZYm6o93cku8O81OKdk26C8P/n3PEOx2EpMQCKSRxlhgb SEH6IRb8GfAx0bxkZn0GeKkac8u7vRk9kUx1ntXdNQ+mepfPTO2dz4oN3BdizwO3vbKO VEFGgKkbOAeWO7MCqhfdx32ucsltKN91vdBK29GgRlZAxQMeOVyaGqRprCAyMY/K1sYx fLDAfF92JszHFh2n6F72lXU/Lh6vYLM+NNu7Otanxuuc41n5sb8BuZxWvcqB+vTAUno4 KdUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692736458; x=1693341258; 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=rQp++5N0yIEsVmUaGN89LifNvR6oAeooLY4sPs12hmU=; b=SINlxV/N8rid9kN3qS9GSIsDwNSxqlRGaj7Cs0+UHswH9TTJ8zS76Z/gfZAzvvvXrs 62ae+6+EOsx/anqC27+WSo1mOGG+QmxB7K06XcH+9acSbvu3/29xXgjbF0RAEg+qJylG JBrfFYZrQTlbBVBDAJ6PZUgPkWcWO6aMFTXh0BPWN1GDevJuTqeOS2fW99nRmgsgTuzf Lq83pg86aD8tJa7o6Wf1cN2MIzxPFvLmvjhzIj9DsVsSSyUCEnJZCxuFXpvIwCg27tsC JpfMUmUq7HGOMygk6OrXzcvftQucAyuo/ncVRcT19qI5C8DpuoaHvkwmh05xRgPA2ToE YxqQ== X-Gm-Message-State: AOJu0Yw5bhEaklGCUuEcsdDF1G0h63svHvSSC3R/aKLwsAFJdFsYGB9z AmLT8FFE4poM6fTOnPyXCWLNUOc+jYYovd2ElR8= X-Google-Smtp-Source: AGHT+IGhuNqrNjV8ArTwUbqS20Djdin7FgFsC22hOZrUk7/Qcs4B7AzuBPSrc8JhydcMZ2g+XDQEbg== X-Received: by 2002:a05:6a20:dc94:b0:140:4563:2243 with SMTP id ky20-20020a056a20dc9400b0014045632243mr7025205pzb.50.1692736458230; Tue, 22 Aug 2023 13:34:18 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:4e3c:f4a4:b92a:b5ab]) by smtp.gmail.com with ESMTPSA id i25-20020a633c59000000b00564aee22f33sm8311823pgn.14.2023.08.22.13.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 13:34:17 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: deller@gmx.de Subject: [PATCH v3 2/3] linux-user: Exit not abort in die_with_backtrace Date: Tue, 22 Aug 2023 13:34:14 -0700 Message-Id: <20230822203415.1184491-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230822203415.1184491-1-richard.henderson@linaro.org> References: <20230822203415.1184491-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::432; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x432.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 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This line is supposed to be unreachable, but if we're going to have it at all, SIGABRT via abort() is subject to the same signal peril that created this function in the first place. We can _exit immediately without peril. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- linux-user/signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux-user/signal.c b/linux-user/signal.c index e1cd111a1b..57fbf2f58b 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -718,7 +718,7 @@ void die_with_signal(int sig) sigsuspend(&act.sa_mask); /* unreachable */ - abort(); + _exit(EXIT_FAILURE); } static G_NORETURN From patchwork Tue Aug 22 20:34:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 1824368 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=ndptI9Lt; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RVh1G21zrz1yh4 for ; Wed, 23 Aug 2023 06:35:12 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYY53-0002eG-9X; Tue, 22 Aug 2023 16:34:25 -0400 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 1qYY50-0002cm-VB for qemu-devel@nongnu.org; Tue, 22 Aug 2023 16:34:22 -0400 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 1qYY4y-0005Ed-GV for qemu-devel@nongnu.org; Tue, 22 Aug 2023 16:34:22 -0400 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-68a6cd7c6a6so1003450b3a.1 for ; Tue, 22 Aug 2023 13:34:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692736459; x=1693341259; 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=WsaRliMGQZhQnMvBkKr3ovb5Yrpz1aO1dp2IimUflrE=; b=ndptI9LtTqvLHw/k9Ef+04SNc0KF1e4SqbSN4cAQcXHbknMIfPAeQDR/R/awh5ZoHi 9Yb8GN94vkZqm0BeJiKm4bjwUk1xJxueKVVZiFNbqGq2WWNikI0iSG+SAZYyOcTCYvI4 QKJmM4e7ByfEBkP9dopD0fdPbgOufHe7e3668mA/txIt2EjkixcWkp2b+oIQfSoSgHMw rsHKY4iOyGLj0f8yzTfNnQDk6aqMbSuSThVC1DSDryP7Y2iTDDnakhVN3Q2gXPp1bxqJ ePACBlOEmaPblnd5nPlfoxjipP5OiRM93NTgV5ae/wNuh6w0s1Cy/MWuMGPS40ygkfC+ df3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692736459; x=1693341259; 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=WsaRliMGQZhQnMvBkKr3ovb5Yrpz1aO1dp2IimUflrE=; b=XJBqdYkQtJ0mSfzVN0Jh+sce0VQd06CvM8NiXiyCdtVYciJ8AbTHln7p8BC9pQANgE ZGDXwwX5UOtOt2XPjFnJf5RAqEXij06cHulGdXFAuouYdzt3USnJHb8xGPDjDctJQHo9 piOUsPJCjXfCEorgIuLp8635FpTSskfir76gI44vDKImFkrCThXjcYy9wHWNfZzZ0ZNM dNrS14+iG+Ve0E/zY/Isc6uzD9z5COFUQ+xO7SfAVBBHy8NmS81VEpYlxIOrFKnBI+uN aYqN4G8DuQ6uEDP9YbJIYkdUGafgyFrvVbDUi4No6llbGWNcQdDKD0uzFHO9MQOZ6mlb +IbA== X-Gm-Message-State: AOJu0YwEPdPYgEKN+FNBMBXtYzvfzxnOwJlXcAPKBANpouryFrFvFSjD P/tAhGDkfqW2mzoxBUtnOYSPrzkrA6N/2wxAD60= X-Google-Smtp-Source: AGHT+IF6++pYlnkB1lGbhA55ryJYLu490q8fGJc9sC8ZGv6E79MGVATzoaKaVTHZbE0YFiC+49PWEg== X-Received: by 2002:a05:6a20:8e12:b0:148:c3ea:fb6c with SMTP id y18-20020a056a208e1200b00148c3eafb6cmr7827464pzj.36.1692736458951; Tue, 22 Aug 2023 13:34:18 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:4e3c:f4a4:b92a:b5ab]) by smtp.gmail.com with ESMTPSA id i25-20020a633c59000000b00564aee22f33sm8311823pgn.14.2023.08.22.13.34.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 13:34:18 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: deller@gmx.de Subject: [PATCH v3 3/3] linux-user: Detect and report host crashes Date: Tue, 22 Aug 2023 13:34:15 -0700 Message-Id: <20230822203415.1184491-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230822203415.1184491-1-richard.henderson@linaro.org> References: <20230822203415.1184491-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 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Helge Deller If there is an internal program error in the qemu source code which raises SIGSEGV or SIGBUS, we currently assume the signal belongs to the guest. With an artificial error introduced, we will now print QEMU internal SIGSEGV {si_code=1, si_addr=(nil)} QEMU v8.1.50 target x86_64 running /usr/bin/cat QEMU backtrace: ./qemu-x86_64(+0xf83d9) [0x55c48587a3d9] ./qemu-x86_64(+0xf8663) [0x55c48587a663] /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7f39eee42520] ./qemu-x86_64(+0x1132fa) [0x55c4858952fa] ./qemu-x86_64(+0x11396f) [0x55c48589596f] ./qemu-x86_64(+0x1140ef) [0x55c4858960ef] ./qemu-x86_64(+0x115796) [0x55c485897796] ./qemu-x86_64(+0x11f9dd) [0x55c4858a19dd] ./qemu-x86_64(+0x3f8da) [0x55c4857c18da] ./qemu-x86_64(+0xf3bfb) [0x55c485875bfb] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7f39eee29d90] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7f39eee29e40] ./qemu-x86_64(+0x39075) [0x55c4857bb075] Segmentation fault Signed-off-by: Helge Deller Message-Id: <20230812164314.352131-1-deller@gmx.de> [rth: Use in_code_gen_buffer and die_with_signal] Signed-off-by: Richard Henderson --- linux-user/signal.c | 54 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/linux-user/signal.c b/linux-user/signal.c index 57fbf2f58b..1ffd1354c3 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -23,6 +23,7 @@ #include #include +#include #include "qemu.h" #include "user-internals.h" @@ -32,6 +33,7 @@ #include "signal-common.h" #include "host-signal.h" #include "user/safe-syscall.h" +#include "tcg/tcg.h" static struct target_sigaction sigact_table[TARGET_NSIG]; @@ -785,6 +787,34 @@ static inline void rewind_if_in_safe_syscall(void *puc) } } +static G_NORETURN +void die_with_backtrace(siginfo_t *info) +{ + void *array[20]; + int size; + + fprintf(stderr, + "QEMU internal SIG%s {si_code=%d, si_addr=%p}\n" + "QEMU v" QEMU_VERSION " target " UNAME_MACHINE " running %s\n", + sigabbrev_np(info->si_signo), info->si_code, info->si_addr, + exec_path); + + size = backtrace(array, ARRAY_SIZE(array)); + if (size) { + char **strings = backtrace_symbols(array, size); + if (strings) { + fprintf(stderr, "QEMU backtrace:\n"); + for (int i = 0; i < size; ++i) { + fprintf(stderr, " %s\n", strings[i]); + } + free(strings); + } + } + + preexit_cleanup(thread_cpu->env_ptr, TARGET_SIGKILL); + die_with_signal(info->si_signo); +} + static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) { CPUArchState *env = thread_cpu->env_ptr; @@ -820,16 +850,28 @@ static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) is_write = host_signal_write(info, uc); access_type = adjust_signal_pc(&pc, is_write); + /* If this was a write to a TB protected page, restart. */ + if (is_write + && host_sig == SIGSEGV + && info->si_code == SEGV_ACCERR + && h2g_valid(host_addr) + && handle_sigsegv_accerr_write(cpu, sigmask, pc, guest_addr)) { + return; + } + + /* + * If the access was not on behalf of the guest, within the executable + * mapping of the generated code buffer, then it is a host bug. + */ + if (access_type != MMU_INST_FETCH + && !in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) { + die_with_backtrace(info); + } + if (host_sig == SIGSEGV) { bool maperr = true; if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) { - /* If this was a write to a TB protected page, restart. */ - if (is_write && - handle_sigsegv_accerr_write(cpu, sigmask, pc, guest_addr)) { - return; - } - /* * With reserved_va, the whole address space is PROT_NONE, * which means that we may get ACCERR when we want MAPERR.