From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id o9Z4KS4Kr1+nAgAAWB0awg (envelope-from ) for ; Fri, 13 Nov 2020 17:35:26 -0500 Received: by simark.ca (Postfix, from userid 112) id 9BE4F1F08B; Fri, 13 Nov 2020 17:35:26 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 8EE4F1E58D for ; Fri, 13 Nov 2020 17:35:25 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0EA27395184D; Fri, 13 Nov 2020 22:35:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0EA27395184D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1605306925; bh=CtRMjd8BKdpWEl5MP1FAjfNeVsRjweadhSgeZSw2zAo=; h=Subject:To:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=QVW/RApogUW1ArfEf0fKYsEmcfltc1K6ITlNidYDMfHgh1zFxKhBI4HcYzgB484la 9n84I6cZVn4ki4GMNTYNK6fC3k15SnVWb1wdIeQx4r4WPUH2Py+R7p3zsS6AgsRLPq 8ixs46uhWB8sNYWxF/s1eEd18oLHRR1T3U5JypHE= Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by sourceware.org (Postfix) with ESMTPS id 3FCF83854802 for ; Fri, 13 Nov 2020 22:35:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3FCF83854802 Received: by mail-qt1-x842.google.com with SMTP id g17so8078415qts.5 for ; Fri, 13 Nov 2020 14:35:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=CtRMjd8BKdpWEl5MP1FAjfNeVsRjweadhSgeZSw2zAo=; b=fvNTMmTJNbMSgN1cFP4egRPLVzEkWjZFVn1ZHFGQX/2e5/3hMes6CXNO175lxmogsF rVFaTNejEljdY9pn0B/tCOxpnNTb9rKNTyH5c369pdIIdzPX6jJnJHxORqkEn/ppYZ6C OL47hh/XS62zSYCFI44AXJYJSHxebryn3Roh0dR4UcNeZYMM6WNHIr4YGx3g9X7hzazi orH4wXMKN+l+R1PdBQOrG0yRzjwAC2IqYRN8/l4UhUBmfoNM56R/5ca69USeFn1y3xP1 lw6drV4hShz8LgB7MEKXBexASH9FZUR2xX5TWvX/n6bw5wI2XPQf/l9eozH91P+F/xP2 F6FQ== X-Gm-Message-State: AOAM531cUIlEdJjW67rTM39CCcxL/BHnLzUqdmDokM2O7y0dNbOTynuT DRHIPdAr2h6qvCE4B32V4CaC08sMTDA0ZQ== X-Google-Smtp-Source: ABdhPJyPB3Q3VwYt3S/x4MlDWYLWKeScODtdpPWeU/PPH/KWX6ucvEhGvIAJeSsZOqnxSYB0mdXBlA== X-Received: by 2002:ac8:6ec1:: with SMTP id f1mr4289175qtv.9.1605306921625; Fri, 13 Nov 2020 14:35:21 -0800 (PST) Received: from ?IPv6:2804:7f0:8284:1487:4444:11b5:bb49:6ae0? ([2804:7f0:8284:1487:4444:11b5:bb49:6ae0]) by smtp.gmail.com with ESMTPSA id q70sm7836580qka.87.2020.11.13.14.35.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 13 Nov 2020 14:35:20 -0800 (PST) Subject: Re: [PATCH] gdb/arm: avoid undefined behavior shift when decoding immediate value To: Simon Marchi , gdb-patches@sourceware.org References: <20201109204945.1313866-1-simon.marchi@efficios.com> Message-ID: Date: Fri, 13 Nov 2020 19:35:17 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20201109204945.1313866-1-simon.marchi@efficios.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Luis Machado via Gdb-patches Reply-To: Luis Machado Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" On 11/9/20 5:49 PM, Simon Marchi via Gdb-patches wrote: > From: Simon Marchi > > When loading the code file provided in PR 26828 and GDB is build with > UBSan, we get: > > Core was generated by `./Foo'. > Program terminated with signal SIGABRT, Aborted. > #0 0xb6c3809c in pthread_cond_wait () from /home/simark/build/binutils-gdb/gdb/repo/lib/libpthread.so.0 > [Current thread is 1 (LWP 29367)] > (gdb) bt > /home/simark/src/binutils-gdb/gdb/arm-tdep.c:1551:30: runtime error: shift exponent 32 is too large for 32-bit type 'unsigned int' > > The sequence of instructions at pthread_cond_wait, in the > libpthread.so.0 library, contains this instruction with an immediate > constant with a "rotate amount" of 0: > > e24dd044 sub sp, sp, #68 ; 0x44 > > Since it shifts by "32 - rotate amount", arm_analyze_prologue does a 32 > bit shift of a 32 bit type, which is caught by UBSan. > > Fix it by factoring out the decoding of immediates in a new function, > arm_expand_immediate. > > I added a selftest for arm_analyze_prologue that replicates the > instruction sequence. Without the fix, it crashes GDB if it is build > with --enable-ubsan. > > I initially wanted to re-use the abstract_memory_reader class already in > arm-tdep.c, used to make arm_process_record testable. However, > arm_process_record and arm_analyze_prologue don't use the same kind of > memory reading functions. arm_process_record uses a function that > returns an error status on failure while arm_analyze_prologue uses one > that throws an exception. Since i didn't want to introduce any other > behavior change, I decided to just introduce a separate interface > (arm_instruction_reader). It is derived from > abstract_instruction_reader in aarch64-tdep.c. > > gdb/ChangeLog: > > PR gdb/26835 > * arm-tdep.c (class arm_instruction_reader): New. > (target_arm_instruction_reader): New. > (arm_analyze_prologue): Add instruction reader parameter and use > it. Use arm_expand_immediate. > (class target_arm_instruction_reader): Adjust. > (arm_skip_prologue): Adjust. > (arm_expand_immediate): New. > (arm_scan_prologue): Adjust. > (arm_analyze_prologue_test): New. > (class test_arm_instruction_reader): New. > > Change-Id: Ieb1c1799bd66f8c7421384f44f5c2777b578ff8d > --- > gdb/arm-tdep.c | 144 +++++++++++++++++++++++++++++++++++++++++-------- > 1 file changed, 121 insertions(+), 23 deletions(-) > > diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c > index 82e8ec4df49c..7f47654233dd 100644 > --- a/gdb/arm-tdep.c > +++ b/gdb/arm-tdep.c > @@ -285,10 +285,34 @@ struct arm_prologue_cache > struct trad_frame_saved_reg *saved_regs; > }; > > -static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch, > - CORE_ADDR prologue_start, > - CORE_ADDR prologue_end, > - struct arm_prologue_cache *cache); > +namespace { > + > +/* Abstract class to read ARM instructions from memory. */ > + > +class arm_instruction_reader > +{ > +public: > + /* Read a 4 bytes instruction bytes from memory using the BYTE_ORDER The comment above reads a bit funny.