public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Prevent LTO wrappers to process a recursive execution
@ 2016-04-25 15:50 Martin Liška
  2016-04-25 19:30 ` Andi Kleen
  2016-06-23  4:58 ` Jeff Law
  0 siblings, 2 replies; 6+ messages in thread
From: Martin Liška @ 2016-04-25 15:50 UTC (permalink / raw)
  To: GCC Patches; +Cc: Jan Hubicka, Richard Biener

[-- Attachment #1: Type: text/plain, Size: 567 bytes --]

Hello.

To make LTO wrappers (gcc-nm, gcc-ar, gcc-ranlib) more smart, I would like to prevent execution
of the same binary by these wrapper. For LTO testing I symlink ar (nm, ranlib) to these wrappers instead
of hacking a build system to respect NM (AR, RANLIB) environment variables. The only problem with that solution
is that these wrappers recursively executes themselves as the first folder in PATH is set to the location with wrappers.

Following patch presents such recursion.

Patch can bootstrap&regtest on x86_64-linux-gnu.

Ready for trunk?
Thanks,
Martin

[-- Attachment #2: 0001-Prevent-LTO-wrappers-to-process-a-recursive-executio.patch --]
[-- Type: text/x-patch, Size: 2807 bytes --]

From dfe0486ad7babe3d6de349001d4790684dc94bfb Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Fri, 22 Apr 2016 17:57:23 +0200
Subject: [PATCH] Prevent LTO wrappers to process a recursive execution

gcc/ChangeLog:

2016-04-22  Martin Liska  <mliska@suse.cz>

	* file-find.c (remove_prefix): New function.
	* file-find.h (remove_prefix): Declare the function.
	* gcc-ar.c (main): Skip a folder of the wrapper if
	a wrapped binary would point to the same file.
---
 gcc/file-find.c | 35 +++++++++++++++++++++++++++++++++++
 gcc/file-find.h |  1 +
 gcc/gcc-ar.c    |  8 ++++++++
 3 files changed, 44 insertions(+)

diff --git a/gcc/file-find.c b/gcc/file-find.c
index 289ef28..1066da9 100644
--- a/gcc/file-find.c
+++ b/gcc/file-find.c
@@ -208,3 +208,38 @@ prefix_from_string (const char *p, struct path_prefix *pprefix)
     }
   free (nstore);
 }
+
+void
+remove_prefix (const char *prefix, struct path_prefix *pprefix)
+{
+  struct prefix_list *remove, **prev, **remove_prev = NULL;
+  int max_len = 0;
+
+  if (pprefix->plist)
+    {
+      prev = &pprefix->plist;
+      for (struct prefix_list *pl = pprefix->plist; pl->next; pl = pl->next)
+	{
+	  if (strcmp (prefix, pl->prefix) == 0)
+	    {
+	      remove = pl;
+	      remove_prev = prev;
+	      continue;
+	    }
+
+	  int l = strlen (pl->prefix);
+	  if (l > max_len)
+	    max_len = l;
+
+	  prev = &pl;
+	}
+
+      if (remove_prev)
+	{
+	  *remove_prev = remove->next;
+	  free (remove);
+	}
+
+      pprefix->max_len = max_len;
+    }
+}
diff --git a/gcc/file-find.h b/gcc/file-find.h
index 5ad9a5f..19a4746 100644
--- a/gcc/file-find.h
+++ b/gcc/file-find.h
@@ -41,6 +41,7 @@ extern void find_file_set_debug (bool);
 extern char *find_a_file (struct path_prefix *, const char *, int);
 extern void add_prefix (struct path_prefix *, const char *);
 extern void add_prefix_begin (struct path_prefix *, const char *);
+extern void remove_prefix (const char *prefix, struct path_prefix *);
 extern void prefix_from_env (const char *, struct path_prefix *);
 extern void prefix_from_string (const char *, struct path_prefix *);
 
diff --git a/gcc/gcc-ar.c b/gcc/gcc-ar.c
index 45ba361..a02dccb 100644
--- a/gcc/gcc-ar.c
+++ b/gcc/gcc-ar.c
@@ -194,6 +194,14 @@ main (int ac, char **av)
 #ifdef CROSS_DIRECTORY_STRUCTURE
       real_exe_name = concat (target_machine, "-", PERSONALITY, NULL);
 #endif
+      /* Do not search original location in the same folder.  */
+      char *exe_folder = lrealpath (av[0]);
+      exe_folder[strlen (exe_folder) - strlen (lbasename (exe_folder))] = '\0';
+      char *location = concat (exe_folder, PERSONALITY, NULL);
+
+      if (access (location, X_OK) == 0)
+	remove_prefix (exe_folder, &path);
+
       exe_name = find_a_file (&path, real_exe_name, X_OK);
       if (!exe_name)
 	{
-- 
2.8.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Prevent LTO wrappers to process a recursive execution
  2016-04-25 15:50 [PATCH] Prevent LTO wrappers to process a recursive execution Martin Liška
@ 2016-04-25 19:30 ` Andi Kleen
  2016-04-26  7:58   ` Martin Liška
  2016-06-23  4:58 ` Jeff Law
  1 sibling, 1 reply; 6+ messages in thread
From: Andi Kleen @ 2016-04-25 19:30 UTC (permalink / raw)
  To: Martin Liška; +Cc: GCC Patches, Jan Hubicka, Richard Biener

Martin Liška <mliska@suse.cz> writes:
>  #endif
> +      /* Do not search original location in the same folder.  */
> +      char *exe_folder = lrealpath (av[0]);
> +      exe_folder[strlen (exe_folder) - strlen (lbasename (exe_folder))] = '\0';
> +      char *location = concat (exe_folder, PERSONALITY, NULL);

Does that really work? When the executable is found in $PATH
av[0] does not contain the full path name. But you seem to assume
it does?

-Andi

> +
> +      if (access (location, X_OK) == 0)
> +	remove_prefix (exe_folder, &path);

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Prevent LTO wrappers to process a recursive execution
  2016-04-25 19:30 ` Andi Kleen
@ 2016-04-26  7:58   ` Martin Liška
  0 siblings, 0 replies; 6+ messages in thread
From: Martin Liška @ 2016-04-26  7:58 UTC (permalink / raw)
  To: Andi Kleen; +Cc: GCC Patches, Jan Hubicka, Richard Biener

On 04/25/2016 09:30 PM, Andi Kleen wrote:
> Does that really work? When the executable is found in $PATH
> av[0] does not contain the full path name. But you seem to assume
> it does?
> 
> -Andi

Hi.

Well, it should be resolved by lrealpath. There's usage from my machine:

marxin@marxinbox:~> which nm
/home/marxin/bin/gcc2/bin/nm
marxin@marxinbox:~> strace -f -s512 nm /tmp/a.o 2>&1 | grep exec
execve("/home/marxin/bin/gcc2/bin/nm", ["nm", "/tmp/a.o"], [/* 97 vars */]) = 0
[pid  5288] execve("/home/marxin/bin/gcc2/bin/nm", ["/home/marxin/bin/gcc2/bin/nm", "--plugin", "/home/marxin/bin/gcc2/lib/gcc/x86_64-pc-linux-gnu/7.0.0/liblto_plugin.so", "/tmp/a.o"], [/* 97 vars */] <unfinished ...>
[pid  5288] <... execve resumed> )      = 0
[pid  5289] execve("/usr/bin/nm", ["/usr/bin/nm", "--plugin", "/home/marxin/bin/gcc2/lib/gcc/x86_64-pc-linux-gnu/7.0.0/liblto_plugin.so", "--plugin", "/home/marxin/bin/gcc2/lib/gcc/x86_64-pc-linux-gnu/7.0.0/liblto_plugin.so", "/tmp/a.o"], [/* 97 vars */] <unfinished ...>
[pid  5289] <... execve resumed> )      = 0

marxin@marxinbox:~> l /home/marxin/bin/gcc2/bin/ | grep nm
-rwxr-xr-x 2 marxin users  141880 Apr 26 09:53 gcc-nm*
lrwxrwxrwx 1 marxin users       6 Apr 26 09:31 nm -> gcc-nm*

Martin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Prevent LTO wrappers to process a recursive execution
  2016-04-25 15:50 [PATCH] Prevent LTO wrappers to process a recursive execution Martin Liška
  2016-04-25 19:30 ` Andi Kleen
@ 2016-06-23  4:58 ` Jeff Law
  2016-06-23  8:23   ` Martin Liška
  1 sibling, 1 reply; 6+ messages in thread
From: Jeff Law @ 2016-06-23  4:58 UTC (permalink / raw)
  To: Martin Liška, GCC Patches; +Cc: Jan Hubicka, Richard Biener

On 04/25/2016 09:49 AM, Martin Liška wrote:
> Hello.
>
> To make LTO wrappers (gcc-nm, gcc-ar, gcc-ranlib) more smart, I would like to prevent execution
> of the same binary by these wrapper. For LTO testing I symlink ar (nm, ranlib) to these wrappers instead
> of hacking a build system to respect NM (AR, RANLIB) environment variables. The only problem with that solution
> is that these wrappers recursively executes themselves as the first folder in PATH is set to the location with wrappers.
>
> Following patch presents such recursion.
>
> Patch can bootstrap&regtest on x86_64-linux-gnu.
>
> Ready for trunk?
> Thanks,
> Martin
>
>
> 0001-Prevent-LTO-wrappers-to-process-a-recursive-executio.patch
>
>
> From dfe0486ad7babe3d6de349001d4790684dc94bfb Mon Sep 17 00:00:00 2001
> From: marxin <mliska@suse.cz>
> Date: Fri, 22 Apr 2016 17:57:23 +0200
> Subject: [PATCH] Prevent LTO wrappers to process a recursive execution
>
> gcc/ChangeLog:
>
> 2016-04-22  Martin Liska  <mliska@suse.cz>
>
> 	* file-find.c (remove_prefix): New function.
> 	* file-find.h (remove_prefix): Declare the function.
> 	* gcc-ar.c (main): Skip a folder of the wrapper if
> 	a wrapped binary would point to the same file.
Is this still something you want to pursue?  It looks pretty reasonable 
and one could make an argument that it's a good idea in and of itself.

jeff

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Prevent LTO wrappers to process a recursive execution
  2016-06-23  4:58 ` Jeff Law
@ 2016-06-23  8:23   ` Martin Liška
  2016-07-06 21:49     ` Jeff Law
  0 siblings, 1 reply; 6+ messages in thread
From: Martin Liška @ 2016-06-23  8:23 UTC (permalink / raw)
  To: Jeff Law, GCC Patches; +Cc: Jan Hubicka, Richard Biener

On 06/23/2016 06:57 AM, Jeff Law wrote:
> Is this still something you want to pursue?  It looks pretty reasonable and one could make an argument that it's a good idea in and of itself.
> 
> jeff

Yeah, I would like to install the patch :) Can I take your reply as signal that it's accepted?

Thanks,
Martin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Prevent LTO wrappers to process a recursive execution
  2016-06-23  8:23   ` Martin Liška
@ 2016-07-06 21:49     ` Jeff Law
  0 siblings, 0 replies; 6+ messages in thread
From: Jeff Law @ 2016-07-06 21:49 UTC (permalink / raw)
  To: Martin Liška, GCC Patches; +Cc: Jan Hubicka, Richard Biener

On 06/23/2016 02:23 AM, Martin Liška wrote:
> On 06/23/2016 06:57 AM, Jeff Law wrote:
>> Is this still something you want to pursue?  It looks pretty reasonable and one could make an argument that it's a good idea in and of itself.
>>
>> jeff
>
> Yeah, I would like to install the patch :) Can I take your reply as signal that it's accepted?
Yes.  It looked reasonable to me.

jeff

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-07-06 21:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-25 15:50 [PATCH] Prevent LTO wrappers to process a recursive execution Martin Liška
2016-04-25 19:30 ` Andi Kleen
2016-04-26  7:58   ` Martin Liška
2016-06-23  4:58 ` Jeff Law
2016-06-23  8:23   ` Martin Liška
2016-07-06 21:49     ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).