From patchwork Mon Feb 19 23:12:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 86009 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 647693858C60 for ; Mon, 19 Feb 2024 23:12:47 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 21E993858C53 for ; Mon, 19 Feb 2024 23:12:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 21E993858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 21E993858C53 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=1708384332; cv=none; b=lTITgBo9LlOJrBH5rKkFyjyN3Pzcz3eyrCkW+MbKoje14S8QosIY71Wap086aLF1+zFFyTE3CTc5AfdDN79av1ktHwU3c0NRb2tJyKmIa4DGgtCVWpSnzuZvBDvqtF3jQBqdEbRbJ9/8TOTUtkDc+ufZtBIF8ogFcjjW4fJ8nmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708384332; c=relaxed/simple; bh=Bc7C0HDuSF7tQ8+aF1N1PF0vRKyibYcWZ3VnS8exxEU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=yEj2LI0U0VxqpLJeMSZY6odmOxdUDin4gSFk6MP8CkL7Ez72XNVMk+bSchtoxC7LIe6zNwV90cbJu5GJqt5/vfWQM84J62yaYQ6EJER5rKSDcXKoPZUh6jsWSnh+UPIkSQB6Tq2yCVgyl7N47HVSTtKlzCfRB+1wzeQqNi7Zy68= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708384330; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=RcANWT0zfLjgQOkEtDbqaI1MMSQnRBScK+Lp2Mrdbqk=; b=cRIWeeFMS0b9sGnWJDA78756TcEstiuWTBso80TdrsvHF5OFck0VI9uMuwZc3gOLWJ1gMi SksR/IIndkemcucDWzXFltJ5Nyv2Hyp8+REMuHz8LhJMNnG83qSax6wbvpMu19mCweTFcu dVYFSd9qNDgvij/sFX081HePhYKQlXs= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-252-mzQQyhRTMw-yDL_RNTprCA-1; Mon, 19 Feb 2024 18:12:09 -0500 X-MC-Unique: mzQQyhRTMw-yDL_RNTprCA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id B791E3C28646 for ; Mon, 19 Feb 2024 23:12:08 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.34.1]) by smtp.corp.redhat.com (Postfix) with ESMTP id 87715C03485; Mon, 19 Feb 2024 23:12:08 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: fix -Wanalyzer-va-arg-type-mismatch false +ve on int types [PR111289] Date: Mon, 19 Feb 2024 18:12:07 -0500 Message-Id: <20240219231207.3065657-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r14-9076-g5651ad62b08096. gcc/analyzer/ChangeLog: PR analyzer/111289 * varargs.cc (representable_in_integral_type_p): New. (va_arg_compatible_types_p): Add "arg_sval" param. Handle integer types. (kf_va_arg::impl_call_pre): Pass arg_sval to va_arg_compatible_types_p. gcc/testsuite/ChangeLog: PR analyzer/111289 * c-c++-common/analyzer/stdarg-pr111289-int.c: New test. * c-c++-common/analyzer/stdarg-pr111289-ptr.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/varargs.cc | 38 ++++++++-- .../analyzer/stdarg-pr111289-int.c | 69 +++++++++++++++++++ .../analyzer/stdarg-pr111289-ptr.c | 39 +++++++++++ 3 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-int.c create mode 100644 gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-ptr.c diff --git a/gcc/analyzer/varargs.cc b/gcc/analyzer/varargs.cc index ac0e1cc1af3..3348121a0ef 100644 --- a/gcc/analyzer/varargs.cc +++ b/gcc/analyzer/varargs.cc @@ -950,13 +950,43 @@ public: } }; -/* Return true if it's OK to copy a value from ARG_TYPE to LHS_TYPE via +static bool +representable_in_integral_type_p (const svalue &sval, const_tree type) +{ + gcc_assert (INTEGRAL_TYPE_P (type)); + + if (tree cst = sval.maybe_get_constant ()) + return wi::fits_to_tree_p (wi::to_wide (cst), type); + + return true; +} + +/* Return true if it's OK to copy ARG_SVAL from ARG_TYPE to LHS_TYPE via va_arg (where argument promotion has already happened). */ static bool -va_arg_compatible_types_p (tree lhs_type, tree arg_type) +va_arg_compatible_types_p (tree lhs_type, tree arg_type, const svalue &arg_sval) { - return compat_types_p (arg_type, lhs_type); + if (compat_types_p (arg_type, lhs_type)) + return true; + + /* It's OK if both types are integer types, where one is signed and the + other type the corresponding unsigned type, when the value is + representable in both types. */ + if (INTEGRAL_TYPE_P (lhs_type) + && INTEGRAL_TYPE_P (arg_type) + && TYPE_UNSIGNED (lhs_type) != TYPE_UNSIGNED (arg_type) + && TYPE_PRECISION (lhs_type) == TYPE_PRECISION (arg_type) + && representable_in_integral_type_p (arg_sval, lhs_type) + && representable_in_integral_type_p (arg_sval, arg_type)) + return true; + + /* It's OK if one type is a pointer to void and the other is a + pointer to a character type. + This is handled by compat_types_p. */ + + /* Otherwise the types are not compatible. */ + return false; } /* If AP_SVAL is a pointer to a var_arg_region, return that var_arg_region. @@ -1022,7 +1052,7 @@ kf_va_arg::impl_call_pre (const call_details &cd) const { tree lhs_type = cd.get_lhs_type (); tree arg_type = arg_sval->get_type (); - if (va_arg_compatible_types_p (lhs_type, arg_type)) + if (va_arg_compatible_types_p (lhs_type, arg_type, *arg_sval)) cd.maybe_set_lhs (arg_sval); else { diff --git a/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-int.c b/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-int.c new file mode 100644 index 00000000000..33d83169c3e --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-int.c @@ -0,0 +1,69 @@ +#include +#include +#include + +typedef unsigned int mode_t; + +extern void openat (int, const char *, int, mode_t); + +/* Signed vs unsigned of same integral type. */ + +static void +test_1 (char const *name, ...) +{ + va_list arg; + va_start (arg, name); + + mode_t mode = va_arg (arg, mode_t); /* { dg-bogus "-Wanalyzer-va-arg-type-mismatch" } */ + + va_end (arg); + openat (-42, name, 0, mode); +} + +void +call_test_1 () +{ + test_1 ("nonexist.ent/", 0600); +} + +/* Not the same size: small enough for int promotion. */ + +int16_t global_2; + +static void +test_2 (char const *name, ...) +{ + va_list arg; + va_start (arg, name); + + global_2 = va_arg (arg, int16_t); /* { dg-warning "promoted to 'int'" } */ + + va_end (arg); +} + +void +call_test_2 () +{ + test_2 ("nonexist.ent/", 42); +} + +/* Not the same size: too big for int promotion. */ + +long long global_3; + +static void +test_3 (char const *name, ...) +{ + va_list arg; + va_start (arg, name); + + global_3 = va_arg (arg, long long); /* { dg-warning "'va_arg' expected 'long long int' but received 'int' for variadic argument 1 of 'arg'" } */ + + va_end (arg); +} + +void +call_test_3 () +{ + test_3 ("nonexist.ent/", 42); +} diff --git a/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-ptr.c b/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-ptr.c new file mode 100644 index 00000000000..7bdbf256d59 --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-ptr.c @@ -0,0 +1,39 @@ +#include + +static void * +test_1 (const char *fmt, ...) +{ + va_list arg; + va_start (arg, fmt); + + void *p = va_arg (arg, void *); /* { dg-bogus "-Wanalyzer-va-arg-type-mismatch" } */ + + va_end (arg); + + return p; +} + +void * +call_test_1 () +{ + return test_1 ("fmt", "foo"); +} + +static char * +test_2 (const char *fmt, ...) +{ + va_list arg; + va_start (arg, fmt); + + char *p = va_arg (arg, char *); /* { dg-bogus "-Wanalyzer-va-arg-type-mismatch" } */ + + va_end (arg); + + return p; +} + +char * +call_test_2 (void *q) +{ + return test_2 ("fmt", q); +}