diff mbox series

lto-streamer: Ensure src_pwd is remapped

Message ID 20240306215435.914126-3-morten@linderud.pw
State New
Headers show
Series lto-streamer: Ensure src_pwd is remapped | expand

Commit Message

Morten Linderud March 6, 2024, 9:50 p.m. UTC
I've made an attempt at patching this issue as it produces unreproducible
unreproducible binaries for Golang. I don't know C/C++ and it's my first gcc
patch so please bear with me :)

-- >8 --

When -flto is used with line macros containing bare symbols instead of
absolute paths the full path is added but never remapped with
-ffile-prefix-map. This could produce unreproducible binaries.

Fixes: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108534

Signed-off-by: Morten Linderud <morten@linderud.pw>
---
 gcc/lto-streamer-in.cc  | 6 ++++--
 gcc/lto-streamer-out.cc | 6 ++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

Comments

Richard Biener March 7, 2024, 8:31 a.m. UTC | #1
On Wed, Mar 6, 2024 at 10:56 PM Morten Linderud <morten@linderud.pw> wrote:
>
> I've made an attempt at patching this issue as it produces unreproducible
> unreproducible binaries for Golang. I don't know C/C++ and it's my first gcc
> patch so please bear with me :)

I think this is a very fragile area - see
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109805

I would first appreciate an attempt to add testsuite coverage for
using any of the
-f*-map switches with LTO, esp. when multiple TUs are involved, possibly
different switches for different TUs at compile-time and with or without such
switches at link time.

Alternatively at least trying to document the expected behavior in
these circumstances.

I'll note your patch applies one set of remapping at TU compile time while the
other is applied at link time where it's not clear how it will behave if those
ever get out-of-sync.  It's also not clear why you need to remap both times
or whether the 'src_pwd' should have been remapped.

As said, a fragile (and known broken to some extent) area.

Thanks,
Richard.

> -- >8 --
>
> When -flto is used with line macros containing bare symbols instead of
> absolute paths the full path is added but never remapped with
> -ffile-prefix-map. This could produce unreproducible binaries.
>
> Fixes: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108534
>
> Signed-off-by: Morten Linderud <morten@linderud.pw>
> ---
>  gcc/lto-streamer-in.cc  | 6 ++++--
>  gcc/lto-streamer-out.cc | 6 ++++--
>  2 files changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/gcc/lto-streamer-in.cc b/gcc/lto-streamer-in.cc
> index ad0ca24007a..799f9eec478 100644
> --- a/gcc/lto-streamer-in.cc
> +++ b/gcc/lto-streamer-in.cc
> @@ -44,6 +44,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "debug.h"
>  #include "alloc-pool.h"
>  #include "toplev.h"
> +#include "file-prefix-map.h" /* remap_debug_filename()  */
>
>  /* Allocator used to hold string slot entries for line map streaming.  */
>  static struct object_allocator<struct string_slot> *string_slot_allocator;
> @@ -557,11 +558,12 @@ lto_location_cache::input_location_and_block (location_t *loc,
>         {
>           const char *pwd = bp_unpack_string (data_in, bp);
>           const char *src_pwd = get_src_pwd ();
> -         if (strcmp (pwd, src_pwd) == 0)
> +         const char *remapped_src_pwd = remap_debug_filename (src_pwd);
> +         if (strcmp (pwd, remapped_src_pwd) == 0)
>             stream_relative_path_prefix = NULL;
>           else
>             stream_relative_path_prefix
> -             = canon_relative_path_prefix (pwd, src_pwd);
> +             = canon_relative_path_prefix (pwd, remapped_src_pwd);
>         }
>        stream_file = canon_file_name (stream_relative_path_prefix,
>                                      bp_unpack_string (data_in, bp));
> diff --git a/gcc/lto-streamer-out.cc b/gcc/lto-streamer-out.cc
> index d4f728094ed..379e256a1e7 100644
> --- a/gcc/lto-streamer-out.cc
> +++ b/gcc/lto-streamer-out.cc
> @@ -230,8 +230,10 @@ lto_output_location_1 (struct output_block *ob, struct bitpack_d *bp,
>               ob->emit_pwd = false;
>             }
>           bp_pack_value (bp, stream_pwd, 1);
> -         if (stream_pwd)
> -           bp_pack_string (ob, bp, get_src_pwd (), true);
> +         if (stream_pwd){
> +           const char *remapped_pwd = remap_debug_filename (get_src_pwd ());
> +           bp_pack_string (ob, bp, remapped_pwd, true);
> +         }
>           bp_pack_string (ob, bp, remapped, true);
>           bp_pack_value (bp, xloc.sysp, 1);
>         }
> --
> 2.44.0
diff mbox series

Patch

diff --git a/gcc/lto-streamer-in.cc b/gcc/lto-streamer-in.cc
index ad0ca24007a..799f9eec478 100644
--- a/gcc/lto-streamer-in.cc
+++ b/gcc/lto-streamer-in.cc
@@ -44,6 +44,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "debug.h"
 #include "alloc-pool.h"
 #include "toplev.h"
+#include "file-prefix-map.h" /* remap_debug_filename()  */
 
 /* Allocator used to hold string slot entries for line map streaming.  */
 static struct object_allocator<struct string_slot> *string_slot_allocator;
@@ -557,11 +558,12 @@  lto_location_cache::input_location_and_block (location_t *loc,
 	{
 	  const char *pwd = bp_unpack_string (data_in, bp);
 	  const char *src_pwd = get_src_pwd ();
-	  if (strcmp (pwd, src_pwd) == 0)
+	  const char *remapped_src_pwd = remap_debug_filename (src_pwd);
+	  if (strcmp (pwd, remapped_src_pwd) == 0)
 	    stream_relative_path_prefix = NULL;
 	  else
 	    stream_relative_path_prefix
-	      = canon_relative_path_prefix (pwd, src_pwd);
+	      = canon_relative_path_prefix (pwd, remapped_src_pwd);
 	}
       stream_file = canon_file_name (stream_relative_path_prefix,
 				     bp_unpack_string (data_in, bp));
diff --git a/gcc/lto-streamer-out.cc b/gcc/lto-streamer-out.cc
index d4f728094ed..379e256a1e7 100644
--- a/gcc/lto-streamer-out.cc
+++ b/gcc/lto-streamer-out.cc
@@ -230,8 +230,10 @@  lto_output_location_1 (struct output_block *ob, struct bitpack_d *bp,
 	      ob->emit_pwd = false;
 	    }
 	  bp_pack_value (bp, stream_pwd, 1);
-	  if (stream_pwd)
-	    bp_pack_string (ob, bp, get_src_pwd (), true);
+	  if (stream_pwd){
+	    const char *remapped_pwd = remap_debug_filename (get_src_pwd ());
+	    bp_pack_string (ob, bp, remapped_pwd, true);
+	  }
 	  bp_pack_string (ob, bp, remapped, true);
 	  bp_pack_value (bp, xloc.sysp, 1);
 	}