]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
lto-wrapper.cc: Delete offload_names temp files in case of error [PR106686]
authorTobias Burnus <tobias@codesourcery.com>
Mon, 22 Aug 2022 07:53:25 +0000 (09:53 +0200)
committerTobias Burnus <tobias@codesourcery.com>
Mon, 22 Aug 2022 07:53:25 +0000 (09:53 +0200)
Usually, the caller takes care of the .o files for the offload compilers
(suffix: ".target.o"). However, if an error occurs during processing
(e.g. fatal error by lto1), they were not deleted.

gcc/ChangeLog:

PR lto/106686
* lto-wrapper.cc (free_array_of_ptrs): Move before tool_cleanup.
(tool_cleanup): Unlink offload_names.
(compile_offload_image): Take filename argument to set it early.
(compile_images_for_offload_targets): Update call; set
offload_names to NULL after freeing the array.

(cherry picked from commit e228683b244c6afbe3bdfef7ba607fbda813bd0f)

gcc/ChangeLog.omp
gcc/lto-wrapper.cc

index 22be02a69f89da8676a567d866482ef11a526c9c..a665cc19ef0c62b6c893982bb434ff511df52dd8 100644 (file)
@@ -1,3 +1,15 @@
+2022-08-22  Tobias Burnus  <tobias@codesourcery.com>
+
+       Backport from mainline:
+       2022-08-22  Tobias Burnus  <tobias@codesourcery.com>
+
+       PR lto/106686
+       * lto-wrapper.cc (free_array_of_ptrs): Move before tool_cleanup.
+       (tool_cleanup): Unlink offload_names.
+       (compile_offload_image): Take filename argument to set it early.
+       (compile_images_for_offload_targets): Update call; set
+       offload_names to NULL after freeing the array.
+
 2022-08-19  Tobias Burnus  <tobias@codesourcery.com>
 
        Backport from mainline:
index d070829bacb55ed5f4a7e960c9bfa51ffc50f269..9301c5a15f4c4089f6a7aba9007a2cf930b29607 100644 (file)
@@ -86,6 +86,25 @@ static bool xassembler_options_error = false;
 
 const char tool_name[] = "lto-wrapper";
 
+/* Auxiliary function that frees elements of PTR and PTR itself.
+   N is number of elements to be freed.  If PTR is NULL, nothing is freed.
+   If an element is NULL, subsequent elements are not freed.  */
+
+static void **
+free_array_of_ptrs (void **ptr, unsigned n)
+{
+  if (!ptr)
+    return NULL;
+  for (unsigned i = 0; i < n; i++)
+    {
+      if (!ptr[i])
+       break;
+      free (ptr[i]);
+    }
+  free (ptr);
+  return NULL;
+}
+
 /* Delete tempfiles.  Called from utils_cleanup.  */
 
 void
@@ -111,6 +130,12 @@ tool_cleanup (bool)
       if (output_names[i])
        maybe_unlink (output_names[i]);
     }
+  if (offload_names)
+    {
+      for (i = 0; offload_names[i]; i++)
+       maybe_unlink (offload_names[i]);
+      free_array_of_ptrs ((void **) offload_names, i);
+    }
 }
 
 static void
@@ -623,25 +648,6 @@ merge_and_complain (vec<cl_decoded_option> &decoded_options,
       }
 }
 
-/* Auxiliary function that frees elements of PTR and PTR itself.
-   N is number of elements to be freed.  If PTR is NULL, nothing is freed.
-   If an element is NULL, subsequent elements are not freed.  */
-
-static void **
-free_array_of_ptrs (void **ptr, unsigned n)
-{
-  if (!ptr)
-    return NULL;
-  for (unsigned i = 0; i < n; i++)
-    {
-      if (!ptr[i])
-       break;
-      free (ptr[i]);
-    }
-  free (ptr);
-  return NULL;
-}
-
 /* Parse STR, saving found tokens into PVALUES and return their number.
    Tokens are assumed to be delimited by ':'.  If APPEND is non-null,
    append it to every token we find.  */
@@ -905,13 +911,13 @@ access_check (const char *name, int mode)
 /* Prepare a target image for offload TARGET, using mkoffload tool from
    COMPILER_PATH.  Return the name of the resultant object file.  */
 
-static char *
+static const char *
 compile_offload_image (const char *target, const char *compiler_path,
                       unsigned in_argc, char *in_argv[],
                       vec<cl_decoded_option> compiler_opts,
-                      vec<cl_decoded_option> linker_opts)
+                      vec<cl_decoded_option> linker_opts,
+                      char **filename)
 {
-  char *filename = NULL;
   char *dumpbase;
   char **argv;
   char *suffix
@@ -919,6 +925,7 @@ compile_offload_image (const char *target, const char *compiler_path,
   strcpy (suffix, "/accel/");
   strcat (suffix, target);
   strcat (suffix, "/mkoffload");
+  *filename = NULL;
 
   char **paths = NULL;
   unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
@@ -947,9 +954,9 @@ compile_offload_image (const char *target, const char *compiler_path,
 
   /* Generate temporary output file name.  */
   if (save_temps)
-    filename = concat (dumpbase, ".o", NULL);
+    *filename = concat (dumpbase, ".o", NULL);
   else
-    filename = make_temp_file (".target.o");
+    *filename = make_temp_file (".target.o");
 
   struct obstack argv_obstack;
   obstack_init (&argv_obstack);
@@ -959,7 +966,7 @@ compile_offload_image (const char *target, const char *compiler_path,
   if (verbose)
     obstack_ptr_grow (&argv_obstack, "-v");
   obstack_ptr_grow (&argv_obstack, "-o");
-  obstack_ptr_grow (&argv_obstack, filename);
+  obstack_ptr_grow (&argv_obstack, *filename);
 
   /* Append names of input object files.  */
   for (unsigned i = 0; i < in_argc; i++)
@@ -983,7 +990,7 @@ compile_offload_image (const char *target, const char *compiler_path,
   obstack_free (&argv_obstack, NULL);
 
   free_array_of_ptrs ((void **) paths, n_paths);
-  return filename;
+  return *filename;
 }
 
 
@@ -1013,10 +1020,9 @@ compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
   offload_names = XCNEWVEC (char *, num_targets + 1);
   for (unsigned i = 0; i < num_targets; i++)
     {
-      offload_names[next_name_entry]
-       = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
-                                compiler_opts, linker_opts);
-      if (!offload_names[next_name_entry])
+      if (!compile_offload_image (names[i], compiler_path, in_argc, in_argv,
+                                 compiler_opts, linker_opts,
+                                 &offload_names[next_name_entry]))
 #if OFFLOAD_DEFAULTED
        continue;
 #else
@@ -1806,6 +1812,7 @@ cont1:
          for (i = 0; offload_names[i]; i++)
            printf ("%s\n", offload_names[i]);
          free_array_of_ptrs ((void **) offload_names, i);
+         offload_names = NULL;
        }
     }