From patchwork Sat Mar 18 16:51:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 71636 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp372942wrt; Sat, 18 Mar 2023 09:51:58 -0700 (PDT) X-Google-Smtp-Source: AK7set9FXx15yMe8/RiDZMTvEGzSh05ObgIoL7aLcRdfkejOlkbNhX04zepwgVgZ/W3vSPrfZ2Xw X-Received: by 2002:a17:906:39ce:b0:932:c1e2:9983 with SMTP id i14-20020a17090639ce00b00932c1e29983mr3546364eje.15.1679158318882; Sat, 18 Mar 2023 09:51:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679158318; cv=none; d=google.com; s=arc-20160816; b=p0p57szgqIv7sN33WZuSLwT3KY8kKr26voP94RMh6Hmvoqy0FVSEaUAxDM+6JKT0cc SGupRYZ+qvaj4vjWRg/f8j8bl2X5w6Duq9LvXsau4QYf/n6me3WDc4vk2WuhuGn9mgTM 8b/QdeRRFimpZk0gX8qTh/cbrkftL3LIOSd5fOxoeULjWTWYU/PniTko3Z8/5NBqLiwY 0KD1TxJ/pqdk/x7gTVzjBlXsGzcyKvgg5yCUsYFeYd67UOKnjq9EbvFnhUsZ1/5lMJwR 3f6ji7+ZcemKVCKABElGHp2dhIEM0kcNfx3x9HnD5vMAn01UnzZxhDpLOPgvPVsUYXHv Qxqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=0Gr3VPQ8Am9Pw+NxgTD+TiIt3Zn+CZgUD+6XGBylPhM=; b=KfXtGVHoUTGxjtBHTZ/p+yfvHwF/1smiIGDtCRofoCENcMkRkAN+/c97lC3vyA2ivJ TBTchY7l+/LQ/lnaEmZxZjtFH1Vwa6qdedcy0AVWvABjN2DDOb2ZJA96FGe3f58FF7Yy NYaTHxJWcZYvVmi3iuS7hM2FgflA3BPfIpTLe4L4A+L9eCP5CjcL4intaChnojkCisFr yCDmQFiBM1szlbC/OPaoz6La0DsP3ULF4+MJiUT1RMhnqR92cd4LOZOPLimZJDGM5g95 gnGp3MVP9u0x5RCO9+M8rxq6x4DMBfozzY/Q+IixFfx9/b/Rt3GfOLqhx6+UziG88VBU 60+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=yewPtVAL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id e25-20020a1709067e1900b008b20e95d474si5392679ejr.51.2023.03.18.09.51.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 09:51:58 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=yewPtVAL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 86359384D190 for ; Sat, 18 Mar 2023 16:51:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 86359384D190 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679158313; bh=0Gr3VPQ8Am9Pw+NxgTD+TiIt3Zn+CZgUD+6XGBylPhM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=yewPtVALiNRF2Zt+iY9kmTXQvWyeJBg/5wDccP0DyAJpcgcubCpifGVCUfNxT2gqq UuXCqs5Y0XT/+pJZHzI9SoEpoq8u3N13JFJtFMQYk34OSYpYg7v2NDOhVLpOz/fRDX UJ5zQIaqRp+xEnYZtvbgRV901QcaLPN7ylxQva2g= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8340E3858426 for ; Sat, 18 Mar 2023 16:51:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8340E3858426 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-177-asw88AYmPUCgeHPAauKg4A-1; Sat, 18 Mar 2023 12:51:03 -0400 X-MC-Unique: asw88AYmPUCgeHPAauKg4A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5DDC4800B23 for ; Sat, 18 Mar 2023 16:51:03 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 350186B590; Sat, 18 Mar 2023 16:51:03 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: fix ICE on certain longjmp calls [PR109094] Date: Sat, 18 Mar 2023 12:51:01 -0400 Message-Id: <20230318165101.3685516-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760725113262479190?= X-GMAIL-MSGID: =?utf-8?q?1760725113262479190?= PR analyzer/109094 reports an ICE in the analyzer seen on qemu's target/i386/tcg/translate.c The issue turned out to be that when handling a longjmp, the code to pop the frames was generating an svalue for the result_decl of any popped frame that had a non-void return type (and discarding it) leading to "uninit" poisoned_svalue_diagnostic instances being saved since the result_decl is only set by the greturn stmt. Later, when checking the feasibility of the path to these diagnostics, m_check_expr was evaluated in the context of the frame of the longjmp, leading to an attempt to evaluate the result_decl of each intervening frames whilst in the context of the topmost frame, leading to an assertion failure in frame_region::get_region_for_local here: 919 case RESULT_DECL: 920 gcc_assert (DECL_CONTEXT (expr) == m_fun->decl); 921 break; This patch updates the analyzer's longjmp implementation so that it doesn't attempt to generate svalues for the result_decls when popping frames, fixing the assertion failure (and presumably fixing "uninit" false positives in a release build). Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Analyzer integration tests shows no regressions. Pushed to trunk as r13-6749-g430d7d88c1a123. gcc/analyzer/ChangeLog: PR analyzer/109094 * region-model.cc (region_model::on_longjmp): Pass false for new "eval_return_svalue" param of pop_frame. (region_model::pop_frame): Add new "eval_return_svalue" param and use it to suppress the call to get_rvalue on the result when needed by on_longjmp. * region-model.h (region_model::pop_frame): Add new "eval_return_svalue" param. gcc/testsuite/ChangeLog: PR analyzer/109094 * gcc.dg/analyzer/setjmp-pr109094.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/region-model.cc | 15 ++++++-- gcc/analyzer/region-model.h | 3 +- .../gcc.dg/analyzer/setjmp-pr109094.c | 38 +++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/setjmp-pr109094.c diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 56beaa82f95..fb81d43f91b 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -1952,7 +1952,7 @@ region_model::on_longjmp (const gcall *longjmp_call, const gcall *setjmp_call, setjmp was called. */ gcc_assert (get_stack_depth () >= setjmp_stack_depth); while (get_stack_depth () > setjmp_stack_depth) - pop_frame (NULL, NULL, ctxt); + pop_frame (NULL, NULL, ctxt, false); gcc_assert (get_stack_depth () == setjmp_stack_depth); @@ -4679,6 +4679,10 @@ region_model::get_current_function () const If OUT_RESULT is non-null, copy any return value from the frame into *OUT_RESULT. + If EVAL_RETURN_SVALUE is false, then don't evaluate the return value. + This is for use when unwinding frames e.g. due to longjmp, to suppress + erroneously reporting uninitialized return values. + Purge the frame region and all its descendent regions. Convert any pointers that point into such regions into POISON_KIND_POPPED_STACK svalues. */ @@ -4686,7 +4690,8 @@ region_model::get_current_function () const void region_model::pop_frame (tree result_lvalue, const svalue **out_result, - region_model_context *ctxt) + region_model_context *ctxt, + bool eval_return_svalue) { gcc_assert (m_current_frame); @@ -4700,7 +4705,9 @@ region_model::pop_frame (tree result_lvalue, tree fndecl = m_current_frame->get_function ()->decl; tree result = DECL_RESULT (fndecl); const svalue *retval = NULL; - if (result && TREE_TYPE (result) != void_type_node) + if (result + && TREE_TYPE (result) != void_type_node + && eval_return_svalue) { retval = get_rvalue (result, ctxt); if (out_result) @@ -4712,6 +4719,8 @@ region_model::pop_frame (tree result_lvalue, if (result_lvalue && retval) { + gcc_assert (eval_return_svalue); + /* Compute result_dst_reg using RESULT_LVALUE *after* popping the frame, but before poisoning pointers into the old frame. */ const region *result_dst_reg = get_lvalue (result_lvalue, ctxt); diff --git a/gcc/analyzer/region-model.h b/gcc/analyzer/region-model.h index 197b5358678..fe3db0b0c98 100644 --- a/gcc/analyzer/region-model.h +++ b/gcc/analyzer/region-model.h @@ -341,7 +341,8 @@ class region_model function * get_current_function () const; void pop_frame (tree result_lvalue, const svalue **out_result, - region_model_context *ctxt); + region_model_context *ctxt, + bool eval_return_svalue = true); int get_stack_depth () const; const frame_region *get_frame_at_index (int index) const; diff --git a/gcc/testsuite/gcc.dg/analyzer/setjmp-pr109094.c b/gcc/testsuite/gcc.dg/analyzer/setjmp-pr109094.c new file mode 100644 index 00000000000..10591ce60a7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/setjmp-pr109094.c @@ -0,0 +1,38 @@ +/* Reduced from an ICE seen in qemu's target/i386/tcg/translate.c */ + +typedef long int __jmp_buf[8]; +struct __jmp_buf_tag { + __jmp_buf __jmpbuf; +}; +typedef struct __jmp_buf_tag sigjmp_buf[1]; + +extern int __sigsetjmp(sigjmp_buf env, int savesigs); +extern void siglongjmp(sigjmp_buf env, int val); + +typedef struct DisasContextBase { + int num_insns; +} DisasContextBase; + +typedef struct DisasContext { + DisasContextBase base; + sigjmp_buf jmpbuf; +} DisasContext; + +extern int translator_ldub(DisasContextBase *base, int); + +int advance_pc(DisasContext *s, int num_bytes) { + if (s->base.num_insns > 1) { + siglongjmp(s->jmpbuf, 2); + } + return 0; +} + +static inline int x86_ldub_code(DisasContext *s) { + return translator_ldub(&s->base, advance_pc(s, 1)); +} + +static void disas_insn(DisasContext *s) { + int b; + __sigsetjmp(s->jmpbuf, 0); + b = x86_ldub_code(s); +}