From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 8B0D63858D20 for ; Fri, 22 Mar 2024 10:15:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B0D63858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8B0D63858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711102525; cv=none; b=NQ0clLbNC4zDwtKmSlc7x72/4KV/kuWoiGiXCanvfsGw82eXFkvXCgUVmX/pI1tVYhR0tctKu5mHVZOs9w5cAIujqkj4+t99ICnfqGZvoA6KUUqn7vDJ2T7J4NAfi1+kiCJMG0QOKT/Y5RSDKwBtrNl/lfwoPS1g7vzevyH+a38= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711102525; c=relaxed/simple; bh=sP2CkNlewMoLK8KSvcGkADUEyPgDMV05HWlhBOSin8c=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=woqsR3QgsWX8B5+bOHVSi5yN56PoNFylHqytHZgbUEUx7lF0385zpE92K12w6/GX0/3m42P1pfBUV2GmncOULLQJciUxNGtfu2PT90Hc9rHpm6WudIwg5ObyhYAd/VBYaWdldHAj+hHncL0NCYHP/MHGBsGMB7TXeY9KZm1Ao/U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0360083.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42M9xIJx010668; Fri, 22 Mar 2024 10:15:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : to : from : subject : content-type : content-transfer-encoding; s=pp1; bh=vvvV1Z6I3c10VqKCxPiCteR2FU6yldNtxQV4G0wT9dc=; b=Oblxz5CepYbcWLszhOtnBTkM8cuatBAJIVgImjUjNwCENiHG6YTcaYtAeavq1zyPHO2P zFUxjkvXpsHdZlFS0EbKeR+4xQ4Scw8+mucJiuhiIiQneHqhhQ9mieOeeuzyVJUyM0kg YziHqr3VAflv3rIpCoSv9w6nEc93LruYzzEW8G/vIpE5RQUIJIO8jfKQ5lu80WEnp1Q1 IWTlpVo5PbWoAV+dIZm9PeeWbkErefdyBtSX9IHttA2bzRI19vJCoGwAKsh2NXuruIgS 8wQUxfbIn+R//7PoLKm1ebR4YEVbsNz8w5JxXu3CMOVS2SypTIDdtqJA4sinuuH26vhO 9Q== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3x15d30bm3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 22 Mar 2024 10:15:13 +0000 Received: from m0360083.ppops.net (m0360083.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 42MAFCrj004157; Fri, 22 Mar 2024 10:15:12 GMT Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3x15d30bkx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 22 Mar 2024 10:15:12 +0000 Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 42M8UCMg032115; Fri, 22 Mar 2024 10:15:11 GMT Received: from smtprelay04.dal12v.mail.ibm.com ([172.16.1.6]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3x0x16k2pc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 22 Mar 2024 10:15:11 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay04.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 42MAF8h415860246 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 22 Mar 2024 10:15:10 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 83B5A5806A; Fri, 22 Mar 2024 10:15:08 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6B17058050; Fri, 22 Mar 2024 10:15:05 +0000 (GMT) Received: from [9.43.31.19] (unknown [9.43.31.19]) by smtpav04.wdc07v.mail.ibm.com (Postfix) with ESMTP; Fri, 22 Mar 2024 10:15:04 +0000 (GMT) Message-ID: <8e8dad73-43a6-4764-a496-b600e6a220e1@linux.ibm.com> Date: Fri, 22 Mar 2024 15:45:02 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Jakub Jelinek , "Kewen.Lin" , Segher Boessenkool , Peter Bergner , David Edelsohn , Michael Meissner , gcc-patches From: Ajit Agarwal Subject: [PATCH v2] rs6000: Stackoverflow in optimized code on PPC [PR100799] Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: AdlRJ6B870ARbyXA-3OT5jFGCNDeA9i1 X-Proofpoint-ORIG-GUID: 84T84rwqCeNh1ju8MjAy1w8mkIgun7yb X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-22_06,2024-03-21_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 phishscore=0 clxscore=1015 priorityscore=1501 impostorscore=0 bulkscore=0 adultscore=0 spamscore=0 malwarescore=0 lowpriorityscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403210000 definitions=main-2403220072 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,GIT_PATCH_0,KAM_MANYTO,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,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 List-Id: Hello All: This is version-2 of the patch with review comments addressed. When using FlexiBLAS with OpenBLAS we noticed corruption of the parameters passed to OpenBLAS functions. FlexiBLAS basically provides a BLAS interface where each function is a stub that forwards the arguments to a real BLAS lib, like OpenBLAS. Fixes the corruption of caller frame checking number of arguments is less than equal to GP_ARG_NUM_REG (8) excluding hidden unused DECLS. Bootstrapped and regtested for powerpc64-linux.gnu. Thanks & Regards Ajit rs6000: Stackoverflow in optimized code on PPC [PR100799] When using FlexiBLAS with OpenBLAS we noticed corruption of the parameters passed to OpenBLAS functions. FlexiBLAS basically provides a BLAS interface where each function is a stub that forwards the arguments to a real BLAS lib, like OpenBLAS. Fixes the corruption of caller frame checking number of arguments is less than equal to GP_ARG_NUM_REG (8) excluding hidden unused DECLS. 2024-03-22 Ajit Kumar Agarwal gcc/ChangeLog: PR rtk-optimization/100799 * config/rs6000/rs6000-calls.cc (rs6000_function_arg): Don't generate parameter save area if number of arguments passed less than equal to GP_ARG_NUM_REG (8) excluding hidden parameter. (init_cumulative_args): Check for hidden parameter in fortran routine and set the flag hidden_string_length and actual parameter passed excluding hidden unused DECLS. * config/rs6000/rs6000.h (rs6000_args): Add new field hidden_string_length and actual_parm_length. --- gcc/config/rs6000/rs6000-call.cc | 36 ++++++++++++++++++++++++++++++-- gcc/config/rs6000/rs6000.h | 7 +++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/gcc/config/rs6000/rs6000-call.cc b/gcc/config/rs6000/rs6000-call.cc index 1f8f93a2ee7..fd823c66ea2 100644 --- a/gcc/config/rs6000/rs6000-call.cc +++ b/gcc/config/rs6000/rs6000-call.cc @@ -64,7 +64,7 @@ #include "ppc-auxv.h" #include "targhooks.h" #include "opts.h" - +#include "tree-dfa.h" #include "rs6000-internal.h" #ifndef TARGET_PROFILE_KERNEL @@ -584,6 +584,31 @@ init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype, if (incoming || cum->prototype) cum->nargs_prototype = n_named_args; + /* When the buggy C/C++ wrappers call the function with fewer arguments + than it actually has and doesn't expect the parameter save area on the + caller side because of that while the callee expects it and the callee + actually stores something in the parameter save area, it corrupts + whatever is in the caller stack frame at that location. */ + unsigned int num_args = 0; + unsigned int hidden_length = 0; + + for (tree arg = DECL_ARGUMENTS (current_function_decl); + arg; arg = DECL_CHAIN (arg)) + { + num_args++; + if (DECL_HIDDEN_STRING_LENGTH (arg)) + { + tree parmdef = ssa_default_def (cfun, arg); + if (parmdef == NULL || has_zero_uses (parmdef)) + { + cum->hidden_string_length = 1; + hidden_length++; + } + } + } + + cum->actual_parm_length = num_args - hidden_length; + /* Check for a longcall attribute. */ if ((!fntype && rs6000_default_long_calls) || (fntype @@ -1857,7 +1882,14 @@ rs6000_function_arg (cumulative_args_t cum_v, const function_arg_info &arg) return rs6000_finish_function_arg (mode, rvec, k); } - else if (align_words < GP_ARG_NUM_REG) + /* When the buggy C/C++ wrappers call the function with fewer arguments + than it actually has and doesn't expect the parameter save area on the + caller side because of that while the callee expects it and the callee + actually stores something in the parameter save area, it corrupts + whatever is in the caller stack frame at that location. */ + else if (align_words < GP_ARG_NUM_REG + || (cum->hidden_string_length + && cum->actual_parm_length <= GP_ARG_NUM_REG)) { if (TARGET_32BIT && TARGET_POWERPC64) return rs6000_mixed_function_arg (mode, type, align_words); diff --git a/gcc/config/rs6000/rs6000.h b/gcc/config/rs6000/rs6000.h index 68bc45d65ba..60f23f33879 100644 --- a/gcc/config/rs6000/rs6000.h +++ b/gcc/config/rs6000/rs6000.h @@ -1490,6 +1490,13 @@ typedef struct rs6000_args int named; /* false for varargs params */ int escapes; /* if function visible outside tu */ int libcall; /* If this is a compiler generated call. */ + /* Actual parameter length ignoring hidden parameter. + This is done to C++ wrapper calling fortran procedures + which has hidden parameter that are not used. */ + unsigned int actual_parm_length; + /* Set if there is hidden parameters while calling C++ wrapper to + fortran procedure. */ + unsigned int hidden_string_length : 1; } CUMULATIVE_ARGS; /* Initialize a variable CUM of type CUMULATIVE_ARGS -- 2.39.3