From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id efcQKp+P9mgxlgAAWB0awg (envelope-from ) for ; Mon, 20 Oct 2025 15:38:07 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cpuh6LBv; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id A6E0B1E0BC; Mon, 20 Oct 2025 15:38:07 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 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 8BBE01E047 for ; Mon, 20 Oct 2025 15:38:06 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 37F413858C36 for ; Mon, 20 Oct 2025 19:38:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 37F413858C36 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cpuh6LBv Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 10BDE3858CD1 for ; Mon, 20 Oct 2025 19:36:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 10BDE3858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 10BDE3858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1760988974; cv=none; b=iyveBktJ0IF9KlCIEL5Lvf9kAfVFBbSOgbEO4Hl56Q98hcnwNtqtlpsBneCGVHjORZZtEGIPI9zgC+WaAxDF5LOlPYh0e0skBIH+WQ/UsKPDMc8TIlhjCew6o3b0slxGUm1UVYTJssyxwzFe/5XPy0mitvVV3oWjSao5iExiOYk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1760988974; c=relaxed/simple; bh=bmgYtjiZWzh35jIBXEkZkkBGUHh4tVUne8TfocUC1ho=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=evv03L40YMu61bVUnsYKwbJziNHTDN/HrgD79RSba4kaGWaYnr5+JYgt6X+383I6UctfXYaq1JZOKxwhSyEF8c1IxNCgxmNKk2opAPIW9LXb6MfgzHxULptRewAGpTPJPjtobSGeyiwqsyekqdS5pgmqAH8TYs4ny3XUeluXww0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 10BDE3858CD1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760988973; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HM8VJzYQgeOyj+MWKUcJ4iNsJ1Cked2RdLZCjsCEArE=; b=cpuh6LBvaHa2XTyDhEDPMjyie1nLxB+x4yyuejXtLKweffTj+lxRA3Gr0BhpgGTr29m4mH wwP8txsTANNJvczuaymdHXUO21HqTpjqL9HfJPo3WRz96VQRP5GN4lRGQVS8u+UU02tKwd 6QHqxQGYw9deJl/aSMG3rC+HBpbQOBg= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-134-yCD2iWiOP4mcNblnryE41w-1; Mon, 20 Oct 2025 15:36:12 -0400 X-MC-Unique: yCD2iWiOP4mcNblnryE41w-1 X-Mimecast-MFC-AGG-ID: yCD2iWiOP4mcNblnryE41w_1760988971 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 817D819560B2 for ; Mon, 20 Oct 2025 19:36:11 +0000 (UTC) Received: from keiths-thinkpadp1gen7.rmtuswa.csb (unknown [10.22.64.94]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E0D8830001BC for ; Mon, 20 Oct 2025 19:36:10 +0000 (UTC) From: Keith Seitz To: gdb-patches@sourceware.org Subject: [PATCH v2] infcall: Add support for integer literals as reference function paramters Date: Mon, 20 Oct 2025 12:36:09 -0700 Message-ID: In-Reply-To: <92575c2eb6805095b41ebbe62ba99b81e4e5dd63.1760629738.git.keiths@redhat.com> References: <92575c2eb6805095b41ebbe62ba99b81e4e5dd63.1760629738.git.keiths@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: uTBymDlFOKM_t-fFSJkDiiH2H-klxw_BqF9iHKyNZ0I_1760988971 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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 This patch attempts to mitigate the shortcomings of passing literals to inferior function calls requiring references. The specific use case here is std::map's operator[]: std::map int_map; int_map[1] = 10; (gdb) print int_map[1] Attempt to take address of value not located in memory. This is occurring because while value_coerce_to_target understands that some values need to be allocated and copied to the inferior's memory, it only considers the actual parsed type of the argument value, ignoring the actual type of the function parameter. That is, in this specific case, the value's parsed type is TYPE_CODE_INT, but the function requires TYPE_CODE_REF. We need to account for the reference. I uncovered one small hiccup while implementing this change. It appears that Fortran compilers use a hidden first argument which is used to pass results from functions back to callers, and these are implemented behind the scenes as references, marked in the debug info as DW_TAG_reference. These should not be subject to this logic, and I have therefore limited this to types defined in C++. On the plus side, the last remaining failure in c++/15372 is now fixed, and that bug could be closed. With this patch, we can now print map entries with integer keys: (gdb) print int_map[1] $1 = (std::map, std::allocator > >::mapped_type &) @0x41f2d4: 10 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=15372 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25957 --- gdb/infcall.c | 2 +- gdb/testsuite/gdb.cp/ref-params.cc | 97 ++++++++++++++- gdb/testsuite/gdb.cp/ref-params.exp | 122 +++++++++++++++++++ gdb/testsuite/gdb.cp/rvalue-ref-overload.exp | 1 - gdb/valops.c | 25 ++-- gdb/value.h | 3 +- 6 files changed, 238 insertions(+), 12 deletions(-) diff --git a/gdb/infcall.c b/gdb/infcall.c index c4b4c8f0bea..823a2b96591 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -263,7 +263,7 @@ value_arg_coerce (struct gdbarch *gdbarch, struct value *arg, this point, we could allocate arguments on the stack instead of calling malloc if we knew that their addresses would not be saved by the called function. */ - arg = value_coerce_to_target (arg); + arg = value_coerce_to_target (arg, type); switch (type->code ()) { diff --git a/gdb/testsuite/gdb.cp/ref-params.cc b/gdb/testsuite/gdb.cp/ref-params.cc index cd53adb834f..ea3e14302eb 100644 --- a/gdb/testsuite/gdb.cp/ref-params.cc +++ b/gdb/testsuite/gdb.cp/ref-params.cc @@ -60,7 +60,69 @@ int mf2(MultiChild& C) return mf1(C); } -int main(void) +/* A class to be used by functions and methods exercising c++/25957. */ +class TestClass +{ + public: + int value; + + TestClass (int v) : value (v) {} + + int + const_ref_method (const int &x) const + { + return value + x; + } + + int + const_ref_method (const TestClass &obj) const + { + return value + obj.value; + } + + int + ref_method (int &x) + { + return value + 2 * x; + } + + int + ref_method (TestClass &obj) + { + return value + 2 * obj.value; + } +}; + +/* Define globals to be used by functions and methods exercising c++/25957. */ +int global_int = 42; +TestClass global_obj (10); + +/* Helper functions to test reference parameter behavior for c++/25957. */ +int +const_ref_func (const int &x) +{ + return x * 2; +} + +int +const_ref_func (const TestClass &obj) +{ + return obj.value * 2; +} + +int +ref_func (int &x) +{ + return x + 1; +} + +int +ref_func (TestClass &obj) +{ + return obj.value + 1; +} + +int main(void) { Child Q(42); Child& QR = Q; @@ -76,5 +138,36 @@ int main(void) mf2(MQ); /* Set breakpoint MQ here. */ - return 0; + TestClass obj (5); + int local_var = 15; + + /* Prevent compiler from optimizing away the function and method calls. */ + int dummy_int = 99; + (void) const_ref_func (dummy_int); + (void) const_ref_func (global_int); + (void) const_ref_func (obj); + (void) const_ref_func (global_obj); + (void) ref_func (dummy_int); + (void) ref_func (global_int); + (void) ref_func (obj); + (void) ref_func (global_obj); + (void) obj.const_ref_method (dummy_int); + (void) obj.const_ref_method (global_int); + (void) obj.const_ref_method (obj); + (void) obj.const_ref_method (global_obj); + (void) obj.ref_method (dummy_int); + (void) obj.ref_method (global_int); + (void) obj.ref_method (obj); + (void) obj.ref_method (global_obj); + (void) global_obj.const_ref_method (dummy_int); + (void) global_obj.const_ref_method (global_int); + (void) global_obj.const_ref_method (obj); + (void) global_obj.const_ref_method (global_obj); + (void) global_obj.ref_method (dummy_int); + (void) global_obj.ref_method (global_int); + (void) global_obj.ref_method (obj); + (void) global_obj.ref_method (global_obj); + + /* Breakpoint here for c++/25957 testing. */ + return 0; /* breakpoint-here */ } diff --git a/gdb/testsuite/gdb.cp/ref-params.exp b/gdb/testsuite/gdb.cp/ref-params.exp index 4f2cbb5dace..96571963140 100644 --- a/gdb/testsuite/gdb.cp/ref-params.exp +++ b/gdb/testsuite/gdb.cp/ref-params.exp @@ -63,3 +63,125 @@ gdb_test "print f1(MQR)" ".* = 53" gdb_test "print mf1(MQR)" ".* = 106" gdb_test "print mf2(MQR)" ".* = 106" gdb_test "print f3(Q.id)" ".* = 42" + +# Inferior function call tests which have reference arguments. +# https://sourceware.org/bugzilla/show_bug.cgi?id=25957 +gdb_start_again "breakpoint-here" + +# Test functions taking const reference parameter. +gdb_test "print const_ref_func(10)" \ + "= 20" \ + "call function with const ref param and literal" + +gdb_test "print const_ref_func(global_int)" \ + "= 84" \ + "call function with const ref param and global variable" + +gdb_test "print const_ref_func(local_var)" \ + "= 30" \ + "call function with const ref param and local variable" + +gdb_test "print const_ref_func(obj)" \ + "= 10" \ + "call function with const ref param and object" + +gdb_test "print const_ref_func(global_obj)" \ + "= 20" \ + "call function with const ref param and global object" + +# Test functions taking non-const reference parameter. +gdb_test "print ref_func(10)" \ + "Cannot resolve function ref_func to any overloaded instance" \ + "call function with non-const ref param and literal" + +gdb_test "print ref_func(global_int)" \ + " = 43" \ + "call function with non-const ref param and global variable" + +gdb_test "print ref_func(local_var)" \ + " = 16" \ + "call function with non-const ref param and local variable" + +gdb_test "print ref_func(obj)" \ + "= 6" \ + "call function with non-const ref param and object" + +gdb_test "print ref_func(global_obj)" \ + "= 11" \ + "call function with non-const ref param and global object" + +# Test methods taking constant reference parameter. +gdb_test "print obj.const_ref_method(5)" \ + "= 10" \ + "call const method with const ref param and literal" + +gdb_test "print obj.const_ref_method(global_int)" \ + "= 47" \ + "call const method with const ref param and global variable" + +gdb_test "print obj.const_ref_method(local_var)" \ + "= 20" \ + "call const method with const ref param and local variable" + +gdb_test "print obj.const_ref_method (obj)" \ + "= 10" \ + "call method with const ref param and object" + +# Test methods taking non-const reference parameters. +gdb_test "print obj.ref_method(5)" \ + "Cannot resolve method TestClass::ref_method to any overloaded instance" \ + "call method with non-const ref param and literal" + +gdb_test "print obj.ref_method(global_int)" \ + "= 89" \ + "cal method with non-const ref param and global variable" + +gdb_test "print obj.ref_method(local_var)" \ + " = 35" \ + "call method with non-const ref param and local variable" + +gdb_test "print obj.ref_method(obj)" \ + "= 15" \ + "call method with non-const ref param and object" + +# Test global_obj methods taking constant reference parameter. +gdb_test "print global_obj.const_ref_method(5)" \ + "= 15" \ + "call global const method with const ref param and literal" + +gdb_test "print global_obj.const_ref_method(global_int)" \ + "= 52" \ + "call global const method with const ref param and global variable" + +gdb_test "print global_obj.const_ref_method(local_var)" \ + "= 25" \ + "call global const method with const ref param and local variable" + +gdb_test "print global_obj.const_ref_method(obj)" \ + "= 15" \ + "call global method with const ref param and object" + +gdb_test "print global_obj.const_ref_method(global_obj)" \ + "= 20" \ + "call global method with const ref param and global object" + +# Test global_obj methods taking non-const reference parameters. +gdb_test "print global_obj.ref_method(5)" \ + "Cannot resolve method TestClass::ref_method to any overloaded instance" \ + "call global method with non-const ref param and literal" + +gdb_test "print global_obj.ref_method(global_int)" \ + "= 94" \ + "call global method with non-const ref param and global variable" + +gdb_test "print global_obj.ref_method(local_var)" \ + "= 40" \ + "call global method with non-const ref param and local variable" + +gdb_test "print global_obj.ref_method(obj)" \ + "= 20" \ + "call global method with non-const ref param and object" + +gdb_test "print global_obj.ref_method(global_obj)" \ + "= 30" \ + "call global method with non-const ref param and global object" \ No newline at end of file diff --git a/gdb/testsuite/gdb.cp/rvalue-ref-overload.exp b/gdb/testsuite/gdb.cp/rvalue-ref-overload.exp index f4d55be92ef..8d8fe0bee99 100644 --- a/gdb/testsuite/gdb.cp/rvalue-ref-overload.exp +++ b/gdb/testsuite/gdb.cp/rvalue-ref-overload.exp @@ -64,7 +64,6 @@ gdb_test "print f (i)" "1" "lvalue reference overload" gdb_test "print f (ci)" "2" "lvalue reference to const overload" -setup_kfail "c++/15372" "*-*-*" gdb_test "print f (3)" "3" "rvalue reference overload" gdb_test "print g (i)" \ diff --git a/gdb/valops.c b/gdb/valops.c index fa87546770a..78f411f7211 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -1464,18 +1464,29 @@ value_must_coerce_to_target (struct value *val) storage, and this function copies them to the target. */ struct value * -value_coerce_to_target (struct value *val) +value_coerce_to_target (struct value *val, struct type *param_type) { LONGEST length; CORE_ADDR addr; - if (!value_must_coerce_to_target (val)) - return val; + if (param_type != nullptr && param_type->language () == language_cplus + && TYPE_IS_REFERENCE (param_type)) + { + length = check_typedef (val->type ())->length (); + addr = allocate_space_in_inferior (length); + write_memory (addr, val->contents ().data (), length); + return value_ref (value_at_lazy (val->type (), addr), + param_type->code ()); + } + else if (value_must_coerce_to_target (val)) + { + length = check_typedef (val->type ())->length (); + addr = allocate_space_in_inferior (length); + write_memory (addr, val->contents ().data (), length); + return value_at_lazy (val->type (), addr); + } - length = check_typedef (val->type ())->length (); - addr = allocate_space_in_inferior (length); - write_memory (addr, val->contents ().data (), length); - return value_at_lazy (val->type (), addr); + return val; } /* Given a value which is an array, return a value which is a pointer diff --git a/gdb/value.h b/gdb/value.h index b9d2809bead..eecfc729714 100644 --- a/gdb/value.h +++ b/gdb/value.h @@ -1278,7 +1278,8 @@ extern LONGEST value_ptrdiff (struct value *arg1, struct value *arg2); extern bool value_must_coerce_to_target (struct value *arg1); -extern struct value *value_coerce_to_target (struct value *arg1); +extern struct value *value_coerce_to_target (struct value *arg1, + struct type *param_type = nullptr); extern struct value *value_coerce_array (struct value *arg1); base-commit: 2c3d37c44b70a6e45a43045fcf07e8e739573c49 -- 2.51.0