From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 116379 invoked by alias); 17 Aug 2015 18:01:09 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 116366 invoked by uid 89); 17 Aug 2015 18:01:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 17 Aug 2015 18:01:06 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 055CE915A3 for ; Mon, 17 Aug 2015 18:01:04 +0000 (UTC) Received: from redhat.com (ovpn-204-54.brq.redhat.com [10.40.204.54]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t7HI10O6029045 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Mon, 17 Aug 2015 14:01:04 -0400 Date: Mon, 17 Aug 2015 18:38:00 -0000 From: Marek Polacek To: GCC Patches Subject: [PATCH] Fix ICE with bogus posix_memalign call (PR middle-end/67222) Message-ID: <20150817180100.GI2093@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-SW-Source: 2015-08/txt/msg00919.txt.bz2 Here we were crashing on an invalid call to posix_memalign. The code in lower_builtin_posix_memalign assumed that the call had valid arguments. The reason the C FE doesn't reject this code is, in short, that int () is compatible with int (void **, size_t, size_t) and we use the former -- so convert_arguments doesn't complain. So I think let's validate the arguments in lower_stmt. I decided to give an error if we see an invalid usage of posix_memalign, since other code (e.g. alias machinery) assumes correct arguments as well. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2015-08-17 Marek Polacek PR middle-end/67222 * gimple-low.c: Include "builtins.h". (lower_stmt): Validate arguments of posix_memalign. * gcc.dg/torture/pr67222.c: New test. diff --git gcc/gimple-low.c gcc/gimple-low.c index d4697e2..03194f0 100644 --- gcc/gimple-low.c +++ gcc/gimple-low.c @@ -39,6 +39,7 @@ along with GCC; see the file COPYING3. If not see #include "langhooks.h" #include "gimple-low.h" #include "tree-nested.h" +#include "builtins.h" /* The differences between High GIMPLE and Low GIMPLE are the following: @@ -345,10 +346,22 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data) data->cannot_fallthru = false; return; } - else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN - && flag_tree_bit_ccp) + else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_POSIX_MEMALIGN) { - lower_builtin_posix_memalign (gsi); + if (gimple_call_num_args (stmt) != 3 + || !validate_gimple_arglist (dyn_cast (stmt), + POINTER_TYPE, INTEGER_TYPE, + INTEGER_TYPE, VOID_TYPE)) + { + error_at (gimple_location (stmt), "invalid arguments " + "to %qD", decl); + gsi_next (gsi); + return; + } + if (flag_tree_bit_ccp) + lower_builtin_posix_memalign (gsi); + else + gsi_next (gsi); return; } } diff --git gcc/testsuite/gcc.dg/torture/pr67222.c gcc/testsuite/gcc.dg/torture/pr67222.c index e69de29..cf39aa1 100644 --- gcc/testsuite/gcc.dg/torture/pr67222.c +++ gcc/testsuite/gcc.dg/torture/pr67222.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-Wno-implicit-function-declaration" } */ + +void +foo (void **p) +{ + posix_memalign (); /* { dg-error "invalid arguments" } */ + posix_memalign (p); /* { dg-error "invalid arguments" } */ + posix_memalign (0); /* { dg-error "invalid arguments" } */ + posix_memalign (p, 1); /* { dg-error "invalid arguments" } */ + posix_memalign (p, "foo"); /* { dg-error "invalid arguments" } */ + posix_memalign ("gnu", "gcc"); /* { dg-error "invalid arguments" } */ + posix_memalign (1, p); /* { dg-error "invalid arguments" } */ + posix_memalign (1, 2); /* { dg-error "invalid arguments" } */ + posix_memalign (1, 2, 3); /* { dg-error "invalid arguments" } */ + posix_memalign (p, p, p); /* { dg-error "invalid arguments" } */ + posix_memalign (p, "qui", 3); /* { dg-error "invalid arguments" } */ + posix_memalign (p, 1, 2); +} Marek