From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 5EAlAt9yNWbxAAUAWB0awg (envelope-from ) for ; Fri, 03 May 2024 19:27:27 -0400 Received: by simark.ca (Postfix, from userid 112) id E69D51E0C1; Fri, 3 May 2024 19:27:26 -0400 (EDT) Received: from server2.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 ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id B932F1E092 for ; Fri, 3 May 2024 19:27:24 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C632E384476A for ; Fri, 3 May 2024 23:27:23 +0000 (GMT) Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) by sourceware.org (Postfix) with ESMTPS id B7CEE38460A2 for ; Fri, 3 May 2024 23:27:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B7CEE38460A2 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B7CEE38460A2 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714778827; cv=none; b=iWpHVFdUUepyXS58cSWi/EZ1HrbFSaYUJyznJ5Jj9kRtvbtPRY18ePPInHMpnQP3YeC4pVfxVvu8umqZJiQYPyDDRQ0+hz+sthUMmH58r8eEs1W8Yq+4/Cx8Kcn2R4ubBVwer5JSQnE8Z8wHnCLAndZdVFDWlVDbQT3K+bnoPwU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714778827; c=relaxed/simple; bh=GP2d0h3dHftutovBFr1G/4UEYU5UBhC3EYhRFnJ++VY=; h=Message-ID:Date:MIME-Version:Subject:From:To; b=LkJHe1Xl9HLMf4feel5/zs7XkZuJOG/HIJ1sXw10+lykiW9tonuxZ3YXj2ISkxTH10nk+4KLxThWaYWmbQKTNAhvm3+jtN6yVNRV0QlXw3KJUpq5ukOuEZksTdlJIrNMkWbswVeMe3r+q7kBTemddQVdxBa7q7zDFKNvTr+I9S8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-41ba1ba55e9so2086345e9.3 for ; Fri, 03 May 2024 16:27:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714778824; x=1715383624; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LcWsFv8c+SIl4Hz1Hs5nponOYiJWFdEvG6Lf5nCVLMM=; b=cqSWH1+oOJeS+u7fgi3IT+ctIAiahthLYc6/x025oiMsg3+BBgBlGNWU9jKxQXRC3O cHj1u9H36UZ1z3znmq9QbGaWXaDmQ+Skm0k+NoVdvnlEnIo8lOkVnKQOu4aLx9Ts1jw2 Y7MMJ093DGIxkiw3o7z7DEvHlePmAizgIfOmJKwDuJzD6rFRCP5cQtMeEFdcr1tzZBdo xKIT+AZnLbYpXQZvJT+l/nP3yR0a4pGBNzXEtQcP8NXw2g2XzQ4fKi6IdJp+FrxJidwe SmcysBacvMgoYcAenH9De4LY13UmOo+I9RlPygA61xDLeaxs1xPGYZ8KoH+53yfV72bX kJ8g== X-Gm-Message-State: AOJu0Yx8qGZHgQTMbZrIZUsfdljp9e559ii0FiiibaBVwEhkopc3axWs CfJ9iCmMDn2fSHrAxnrYaj04G97/KQey+9w4ABCUzcwcru3iRB+L X-Google-Smtp-Source: AGHT+IHYtTYgFo/YT85C0t1bAf0/x2+PVAMB7psLekmWRbDSdHn9Y3AEIw8ylT6ansCqVl+gt7JW/Q== X-Received: by 2002:a05:600c:4f4b:b0:418:60d6:8622 with SMTP id m11-20020a05600c4f4b00b0041860d68622mr3317517wmq.18.1714778824232; Fri, 03 May 2024 16:27:04 -0700 (PDT) Received: from ?IPV6:2001:8a0:f93d:b900:18bd:4809:7583:abd1? ([2001:8a0:f93d:b900:18bd:4809:7583:abd1]) by smtp.gmail.com with ESMTPSA id y17-20020a5d4ad1000000b0034cfa17d74dsm4757635wrs.104.2024.05.03.16.27.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 03 May 2024 16:27:03 -0700 (PDT) Message-ID: <1b81045d-5353-49cf-b80e-f64498dd3562@palves.net> Date: Sat, 4 May 2024 00:26:49 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] [gdb/exp] Fix cast handling for indirection From: Pedro Alves To: Kevin Buettner Cc: gdb-patches@sourceware.org, Tom Tromey , Tom de Vries References: <20240502154902.22575-1-tdevries@suse.de> <20240502193145.5da7327d@f39-zbm-amd> <8734qyex0l.fsf@tromey.com> <51de396e-67fc-4451-a13e-091178d188f7@palves.net> <20240503103050.2ef46699@f39-zbm-amd> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org On 2024-05-04 00:17, Pedro Alves wrote: > On 2024-05-03 18:30, Kevin Buettner wrote: >> On Fri, 3 May 2024 17:04:41 +0100 >> Pedro Alves wrote: >> >>>> I was wondering if this patch causes gdb to accept some weird things >>>> that might have been rejected in the past, by introducing a hidden cast. >>>> Maybe print (char) *85732 does something surprising now. I'm not >>>> entirely sure if that's bad. >>> >>> I am totally surprised that: >>> >>> +# Regression test for PR31693. >>> +gdb_test "p (char)*a_loc ()" " = 97 'a'" >>> >>> this actually works, instead of telling the user: >>> >>> "'a_loc' has unknown return type; cast the call to its declared return type" >>> >>> It seems like a misfeature to me to assume that "char *" is the right type. >>> >>> Thus, I don't agree with the patch. >> >> Using a GDB built with Tom de Vries's patch, I see: >> >> (gdb) p *a_loc() >> 'a_loc' has unknown return type; cast the call to its declared return type >> >> This is the same as the pre-patch behavior. >> >> With Tom's patch, GDB now infers the function's return type, based >> on the cast: >> >> (gdb) p (char)*a_loc() >> $1 = 97 'a' >> > > But that is not what GDB told you to do. It told you to cast the _call_, > not the result of de-referencing the result of the call. > It is telling cast to the declared return type, which is "char *". I.e., > it is telling you to write: > > (gdb) p *(char *)a_loc() > > See 7022349d5c86 ("Stop assuming no-debug-info functions return int"). > > This cast here: > > (gdb) p (char)*a_loc() > > ... should not affect the call's return type. That is decided before > the * operator is involved. In the same way, this: > > (gdb) p (long long)*a_loc() > > should not result in gdb assuming that a_loc() returns a "long long *", > that it wrong. It should still error out with > > 'a_loc' has unknown return type; cast the call to its declared return type > > and so the user should write: > > (gdb) p (long long) *(char *)a_loc() > > and then with this last expression a proper sign extension is applied when > char is converted to long long, if char is signed. I.e., in steps: > > 1: (char *)a_loc() -> call, and get char * return value > 2: *(char *)a_loc() -> deref, and get char value > 3: (long long) *(char *)a_loc() -> sign extend char value -> long long value > I kind of emphasized the sign extension part above, but to be clear, with (gdb) p (long long)*a_loc() and gdb assuming that means a_loc returns "long long *", gdb incorrectly reads a 64-bit value off of the pointer address, which is totally bogus and would not be what gdb would do if it had debug info for a_loc(), in which case GDB would know that it returns char *, and thus would deref only one byte and behave like described above in the 1: 2: 3: steps. The behavior of the expression should not change like that depending on whether you have debug info. Thus, GDB should error out.