From patchwork Thu May 10 09:28:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Martin_Li=C5=A1ka?= X-Patchwork-Id: 911331 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-477471-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="aD6sldSX"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40hSZj2qntz9s3Z for ; Thu, 10 May 2018 19:28:28 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:cc:message-id:date:mime-version:content-type; q=dns; s=default; b=jINYvqHPn/piCwivJ32lk6QavHK5JULGK/PxEce100MDl1SHrK geI1LYKA32qKRPDwYu8YUG+R4q2CYuJf0NPHM0S7Bx+YQ16wyowdKdtZo3/0t7kx gvDe8qmi5mUZhcNDNyNVxLyiOxoKDiznBQqHz1TIWqfV6SwRk2BJbB/xs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:cc:message-id:date:mime-version:content-type; s= default; bh=hjzY19zyLCL4sQ9K6KLDxCfynIE=; b=aD6sldSXgjz/Q7XNlnwK MBMaEo49B/lmM785kPJxki9DmpwNMSNjRkR8MHRymcj6gGKiXQKRsdEHdRMEE7e8 aQ64jLeWCdqX9lZ4Tj+RqSa5BotQsjINSck+ILaK5QEWOoUYUcQ5u7aaVGpE2afA JlkmCfA/DVh/x8FPipigHJ8= Received: (qmail 18119 invoked by alias); 10 May 2018 09:28:20 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 18095 invoked by uid 89); 10 May 2018 09:28:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 10 May 2018 09:28:18 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id A8813AD53; Thu, 10 May 2018 09:28:15 +0000 (UTC) From: =?utf-8?q?Martin_Li=C5=A1ka?= Subject: [PATCH] Handle no_sanitize attribute values in the right way (PR sanitizer/85556). To: gcc-patches@gcc.gnu.org Cc: Jakub Jelinek Message-ID: <9beeb4c0-d059-ac6f-83a6-2e3669f34746@suse.cz> Date: Thu, 10 May 2018 11:28:15 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 X-IsSubscribed: yes Hi. Parsing of no_sanitize attribute now supports __attribute__((no_sanitize("address,undefined"))) which is wrong. And on the other hand this is not recognized: __attribute__((no_sanitize("address", "undefined"))) Patch can bootstrap on x86_64-linux-gnu and survives regression tests. Then I would like to backport that to GCC 8 branch. Ready to be installed? Martin gcc/ChangeLog: 2018-05-10 Martin Liska PR sanitizer/85556 * opts.c (parse_no_sanitize_attribute): Handle only a sinle option value. gcc/c-family/ChangeLog: 2018-05-10 Martin Liska PR sanitizer/85556 * c-attribs.c (handle_no_sanitize_attribute): Iterate all TREE_LIST values. gcc/testsuite/ChangeLog: 2018-05-10 Martin Liska PR sanitizer/85556 * c-c++-common/ubsan/attrib-6.c: New test. --- gcc/c-family/c-attribs.c | 20 +++++++++++-------- gcc/opts.c | 30 +++++++++++------------------ gcc/testsuite/c-c++-common/ubsan/attrib-6.c | 22 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/ubsan/attrib-6.c diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c index e0630885cca..744315eec86 100644 --- a/gcc/c-family/c-attribs.c +++ b/gcc/c-family/c-attribs.c @@ -403,7 +403,7 @@ const struct attribute_spec c_common_attribute_table[] = 0, 0, true, false, false, false, handle_no_address_safety_analysis_attribute, NULL }, - { "no_sanitize", 1, 1, true, false, false, false, + { "no_sanitize", 1, -1, true, false, false, false, handle_no_sanitize_attribute, NULL }, { "no_sanitize_address", 0, 0, true, false, false, false, handle_no_sanitize_address_attribute, NULL }, @@ -683,22 +683,26 @@ static tree handle_no_sanitize_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs) { + unsigned int flags = 0; *no_add_attrs = true; - tree id = TREE_VALUE (args); if (TREE_CODE (*node) != FUNCTION_DECL) { warning (OPT_Wattributes, "%qE attribute ignored", name); return NULL_TREE; } - if (TREE_CODE (id) != STRING_CST) + for (; args; args = TREE_CHAIN (args)) { - error ("no_sanitize argument not a string"); - return NULL_TREE; - } + tree id = TREE_VALUE (args); + if (TREE_CODE (id) != STRING_CST) + { + error ("no_sanitize argument not a string"); + return NULL_TREE; + } - char *string = ASTRDUP (TREE_STRING_POINTER (id)); - unsigned int flags = parse_no_sanitize_attribute (string); + char *string = ASTRDUP (TREE_STRING_POINTER (id)); + flags |= parse_no_sanitize_attribute (string); + } add_no_sanitize_value (*node, flags); diff --git a/gcc/opts.c b/gcc/opts.c index 33efcc0d6e7..f999dccd009 100644 --- a/gcc/opts.c +++ b/gcc/opts.c @@ -1748,33 +1748,25 @@ parse_sanitizer_options (const char *p, location_t loc, int scode, return flags; } -/* Parse string values of no_sanitize attribute passed in VALUE. - Values are separated with comma. */ +/* Parse string value of no_sanitize attribute passed in VALUE. */ unsigned int parse_no_sanitize_attribute (char *value) { unsigned int flags = 0; unsigned int i; - char *q = strtok (value, ","); - while (q != NULL) - { - for (i = 0; sanitizer_opts[i].name != NULL; ++i) - if (strcmp (sanitizer_opts[i].name, q) == 0) - { - flags |= sanitizer_opts[i].flag; - if (sanitizer_opts[i].flag == SANITIZE_UNDEFINED) - flags |= SANITIZE_UNDEFINED_NONDEFAULT; - break; - } - - if (sanitizer_opts[i].name == NULL) - warning (OPT_Wattributes, - "%<%s%> attribute directive ignored", q); + for (i = 0; sanitizer_opts[i].name != NULL; ++i) + if (strcmp (sanitizer_opts[i].name, value) == 0) + { + flags |= sanitizer_opts[i].flag; + if (sanitizer_opts[i].flag == SANITIZE_UNDEFINED) + flags |= SANITIZE_UNDEFINED_NONDEFAULT; + break; + } - q = strtok (NULL, ","); - } + if (sanitizer_opts[i].name == NULL) + warning (OPT_Wattributes, "%<%s%> attribute directive ignored", value); return flags; } diff --git a/gcc/testsuite/c-c++-common/ubsan/attrib-6.c b/gcc/testsuite/c-c++-common/ubsan/attrib-6.c new file mode 100644 index 00000000000..ac08dc219ec --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/attrib-6.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=undefined" } */ + +static void __attribute__((no_sanitize("foobar"))) +foo (void) { /* { dg-warning "attribute directive ignored" } */ +} + +static void __attribute__((no_sanitize("address,undefined"))) +foo2 (void) { /* { dg-warning ".address,undefined. attribute directive ignored" } */ +} + +static void __attribute__((no_sanitize("address", "undefined"))) +foo3 (void) { +} + +static void __attribute__((no_sanitize("address", "address", ""))) +foo4 (void) { /* { dg-warning ".. attribute directive ignored" } */ +} + +static void __attribute__((no_sanitize("address", "address", "address,address"))) +foo5 (void) { /* { dg-warning ".address,address. attribute directive ignored" } */ +}