From patchwork Wed May 16 11:53:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_Li=C5=A1ka?= X-Patchwork-Id: 914566 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-477757-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="olN/atgi"; 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 40mCX56r2Dz9s2k for ; Wed, 16 May 2018 21:54:11 +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 :subject:from:to:cc:references:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=sKKAi2x5eLfJTmvRd VO+U1qz+oC7WhC46TmVfsysv1r900UTurowIWwOJhxlP1IEpKNPr5w2umBY8Nesa VDTYFjHSQC9jBkBD+JfiQNo8nx4zR6ftSqb9dJgyoUQ54gkk8W6wCazG8C7th2PM gLnennaxF8mZiX8lePdekZfJRo= 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 :subject:from:to:cc:references:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=yJEx/mO32b/D6ZXfcA+g5UZ RTLQ=; b=olN/atgiFySe/npSMUBjFYxS95BBetwkTz+Qye0eZ7KsSjINxBQ9Z4Q LqTEKnVNkiZd6zMnPuHDdUIKt0/NMVnvRQbFvnYEoscIHQ7khInyLZxe7Sa86Ntl SAKdruHp9H39NwTEobyLt9O0PZArmGH1Zz9IDaXiNMqzpHBXzgc8= Received: (qmail 76715 invoked by alias); 16 May 2018 11:53:58 -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 76701 invoked by uid 89); 16 May 2018 11:53:57 -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=xmalloc 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; Wed, 16 May 2018 11:53:54 +0000 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 69787AEA9; Wed, 16 May 2018 11:53:52 +0000 (UTC) Subject: [PATCH] When using -fprofile-generate=/some/path mangle absolute path of file (PR lto/85759). From: =?utf-8?q?Martin_Li=C5=A1ka?= To: Jakub Jelinek , Martin Sebor Cc: GCC Patches , Richard Biener , Jan Hubicka References: <41f0b7e4-1a6b-3f5e-1e3a-750502e73043@suse.cz> <08c9b11b-6420-f056-ae27-38394662deca@gmail.com> <20171220174525.GK2353@tucnak> <3b996c3b-6dc5-12d3-26bf-0aa27977dc66@suse.cz> Message-ID: Date: Wed, 16 May 2018 13:53:51 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <3b996c3b-6dc5-12d3-26bf-0aa27977dc66@suse.cz> X-IsSubscribed: yes On 12/21/2017 10:13 AM, Martin Liška wrote: > On 12/20/2017 06:45 PM, Jakub Jelinek wrote: >> Another thing is that the "/" in there is wrong, so >> const char dir_separator_str[] = { DIR_SEPARATOR, '\0' }; >> char *b = concat (profile_data_prefix, dir_separator_str, pwd, NULL); >> needs to be used instead. > > This looks much nicer, I forgot about DIR_SEPARATOR. > >> Does profile_data_prefix have any dir separators stripped from the end? > > That's easy to achieve.. > >> Is pwd guaranteed to be relative in this case? > > .. however this is absolute path, which would be problematic on a DOC based FS. > Maybe we should do the same path mangling as we do for purpose of gcov: > > https://github.com/gcc-mirror/gcc/blob/master/gcc/gcov.c#L2424 Hi. I decided to implement that. Which means for: $ gcc -fprofile-generate=/tmp/myfolder empty.c -O2 && ./a.out we get following file: /tmp/myfolder/#home#marxin#Programming#testcases#tmp#empty.gcda That guarantees we have a unique file path. As seen in the PR it can produce a funny ICE. I've been testing the patch. Ready after it finishes tests? Martin > > What do you think about it? > Regarding the string manipulation: I'm not an expert, but work with string in C > is for me always a pain :) > > Martin > From 386a4561a4d1501e8959871791289e95f6a89af5 Mon Sep 17 00:00:00 2001 From: marxin Date: Wed, 16 Aug 2017 10:22:57 +0200 Subject: [PATCH] When using -fprofile-generate=/some/path mangle absolute path of file (PR lto/85759). gcc/ChangeLog: 2018-05-16 Martin Liska PR lto/85759 * coverage.c (coverage_init): Mangle full path name. * doc/invoke.texi: Document the change. * gcov-io.c (mangle_path): New. * gcov-io.h (mangle_path): Likewise. * gcov.c (mangle_name): Use mangle_path for path mangling. --- gcc/coverage.c | 20 ++++++++++++++++++-- gcc/doc/invoke.texi | 3 +++ gcc/gcov-io.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ gcc/gcov-io.h | 1 + gcc/gcov.c | 37 +------------------------------------ 5 files changed, 72 insertions(+), 38 deletions(-) diff --git a/gcc/coverage.c b/gcc/coverage.c index 32ef298a11f..6e621c3ff96 100644 --- a/gcc/coverage.c +++ b/gcc/coverage.c @@ -1227,8 +1227,24 @@ coverage_init (const char *filename) g->get_passes ()->get_pass_profile ()->static_pass_number; g->get_dumps ()->dump_start (profile_pass_num, NULL); - if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename)) - profile_data_prefix = getpwd (); + if (!IS_ABSOLUTE_PATH (filename)) + { + /* When a profile_data_prefix is provided, then mangle full path + of filename in order to prevent file path clashing. */ + if (profile_data_prefix) + { +#if HAVE_DOS_BASED_FILE_SYSTEM + const char separator = "\\"; +#else + const char *separator = "/"; +#endif + filename = concat (getpwd (), separator, filename, NULL); + filename = mangle_path (filename); + len = strlen (filename); + } + else + profile_data_prefix = getpwd (); + } if (profile_data_prefix) prefix_len = strlen (profile_data_prefix); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index ca3772bbebf..4859cec0ab5 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -11253,6 +11253,9 @@ and used by @option{-fprofile-use} and @option{-fbranch-probabilities} and its related options. Both absolute and relative paths can be used. By default, GCC uses the current directory as @var{path}, thus the profile data file appears in the same directory as the object file. +In order to prevent filename clashing, if object file name is not an absolute +path, we mangle absolute path of @file{@var{sourcename}.gcda} file and +use it as file name of a @file{.gcda} file. @item -fprofile-generate @itemx -fprofile-generate=@var{path} diff --git a/gcc/gcov-io.c b/gcc/gcov-io.c index 3fe1e613ebc..68660d6d3cf 100644 --- a/gcc/gcov-io.c +++ b/gcc/gcov-io.c @@ -576,6 +576,55 @@ gcov_read_counter (void) return value; } +/* Mangle filename path of BASE and output new allocated pointer with + mangled path. */ + +char * +mangle_path (char const *base) +{ + /* Convert '/' to '#', convert '..' to '^', + convert ':' to '~' on DOS based file system. */ + const char *probe; + char *buffer = (char *)xmalloc (strlen (base) + 10); + char *ptr = buffer; + +#if HAVE_DOS_BASED_FILE_SYSTEM + if (base[0] && base[1] == ':') + { + ptr[0] = base[0]; + ptr[1] = '~'; + ptr += 2; + base += 2; + } +#endif + for (; *base; base = probe) + { + size_t len; + + for (probe = base; *probe; probe++) + if (*probe == '/') + break; + len = probe - base; + if (len == 2 && base[0] == '.' && base[1] == '.') + *ptr++ = '^'; + else + { + memcpy (ptr, base, len); + ptr += len; + } + if (*probe) + { + *ptr++ = '#'; + probe++; + } + } + + /* Terminate the string. */ + *ptr = '\0'; + + return buffer; +} + /* We need to expose the below function when compiling for gcov-tool. */ #if !IN_LIBGCOV || defined (IN_GCOV_TOOL) diff --git a/gcc/gcov-io.h b/gcc/gcov-io.h index d6389c48908..cdf9f4cac3f 100644 --- a/gcc/gcov-io.h +++ b/gcc/gcov-io.h @@ -381,6 +381,7 @@ GCOV_LINKAGE void gcov_read_summary (struct gcov_summary *) ATTRIBUTE_HIDDEN; GCOV_LINKAGE const char *gcov_read_string (void); GCOV_LINKAGE void gcov_sync (gcov_position_t /*base*/, gcov_unsigned_t /*length */); +char *mangle_path (char const *base); #if !IN_GCOV /* Available outside gcov */ diff --git a/gcc/gcov.c b/gcc/gcov.c index 6bbfe33ca33..7c1950d5df9 100644 --- a/gcc/gcov.c +++ b/gcc/gcov.c @@ -2434,42 +2434,7 @@ mangle_name (char const *base, char *ptr) ptr += len; } else - { - /* Convert '/' to '#', convert '..' to '^', - convert ':' to '~' on DOS based file system. */ - const char *probe; - -#if HAVE_DOS_BASED_FILE_SYSTEM - if (base[0] && base[1] == ':') - { - ptr[0] = base[0]; - ptr[1] = '~'; - ptr += 2; - base += 2; - } -#endif - for (; *base; base = probe) - { - size_t len; - - for (probe = base; *probe; probe++) - if (*probe == '/') - break; - len = probe - base; - if (len == 2 && base[0] == '.' && base[1] == '.') - *ptr++ = '^'; - else - { - memcpy (ptr, base, len); - ptr += len; - } - if (*probe) - { - *ptr++ = '#'; - probe++; - } - } - } + ptr = mangle_path (base); return ptr; } -- 2.16.3