LCOV - code coverage report
Current view: top level - gcc/jit - jit-tempdir.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 98.4 % 64 63
Test Date: 2024-03-23 14:05:01 Functions: 100.0 % 4 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Managing temporary directories and their content within libgccjit.so
       2                 :             :    Copyright (C) 2014-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4                 :             : 
       5                 :             : This file is part of GCC.
       6                 :             : 
       7                 :             : GCC is free software; you can redistribute it and/or modify it
       8                 :             : under the terms of the GNU General Public License as published by
       9                 :             : the Free Software Foundation; either version 3, or (at your option)
      10                 :             : any later version.
      11                 :             : 
      12                 :             : GCC is distributed in the hope that it will be useful, but
      13                 :             : WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :             : General Public License for more details.
      16                 :             : 
      17                 :             : You should have received a copy of the GNU General Public License
      18                 :             : along with GCC; see the file COPYING3.  If not see
      19                 :             : <http://www.gnu.org/licenses/>.  */
      20                 :             : 
      21                 :             : #include "config.h"
      22                 :             : #include "system.h"
      23                 :             : #include "coretypes.h"
      24                 :             : 
      25                 :             : #include "jit-tempdir.h"
      26                 :             : 
      27                 :             : #ifdef _WIN32
      28                 :             : #include "jit-w32.h"
      29                 :             : #endif
      30                 :             : 
      31                 :             : #ifndef _WIN32
      32                 :             : /* Construct a tempdir path template suitable for use by mkdtemp
      33                 :             :    e.g. "/tmp/libgccjit-XXXXXX", but respecting the rules in
      34                 :             :    libiberty's choose_tempdir rather than hardcoding "/tmp/".
      35                 :             : 
      36                 :             :    The memory is allocated using malloc and must be freed.
      37                 :             :    Aborts the process if allocation fails. */
      38                 :             : 
      39                 :             : static char *
      40                 :        1178 : make_tempdir_path_template ()
      41                 :             : {
      42                 :        1178 :   const char *tmpdir_buf;
      43                 :        1178 :   size_t tmpdir_len;
      44                 :        1178 :   const char *file_template_buf;
      45                 :        1178 :   size_t file_template_len;
      46                 :        1178 :   char *result;
      47                 :             : 
      48                 :             :   /* The result of choose_tmpdir is a cached buffer within libiberty, so
      49                 :             :      we must *not* free it.  */
      50                 :        1178 :   tmpdir_buf = choose_tmpdir ();
      51                 :             : 
      52                 :             :   /* choose_tmpdir aborts on malloc failure.  */
      53                 :        1178 :   gcc_assert (tmpdir_buf);
      54                 :             : 
      55                 :        1178 :   tmpdir_len = strlen (tmpdir_buf);
      56                 :             :   /* tmpdir_buf should now have a dir separator as the final byte.  */
      57                 :        1178 :   gcc_assert (tmpdir_len > 0);
      58                 :        1178 :   gcc_assert (tmpdir_buf[tmpdir_len - 1] == DIR_SEPARATOR);
      59                 :             : 
      60                 :        1178 :   file_template_buf = "libgccjit-XXXXXX";
      61                 :        1178 :   file_template_len = strlen (file_template_buf);
      62                 :             : 
      63                 :        1178 :   result = XNEWVEC (char, tmpdir_len + file_template_len + 1);
      64                 :        1178 :   strcpy (result, tmpdir_buf);
      65                 :        1178 :   strcpy (result + tmpdir_len, file_template_buf);
      66                 :             : 
      67                 :        1178 :   return result;
      68                 :             : }
      69                 :             : #endif
      70                 :             : 
      71                 :             : /* The constructor for the jit::tempdir object.
      72                 :             :    The real work is done by the jit::tempdir::create method.  */
      73                 :             : 
      74                 :        1178 : gcc::jit::tempdir::tempdir (logger *logger, int keep_intermediates)
      75                 :             :   : log_user (logger),
      76                 :        1178 :     m_keep_intermediates (keep_intermediates),
      77                 :        1178 :     m_path_template (NULL),
      78                 :        1178 :     m_path_tempdir (NULL),
      79                 :        1178 :     m_path_c_file (NULL),
      80                 :        1178 :     m_path_s_file (NULL),
      81                 :        1178 :     m_path_so_file (NULL)
      82                 :             : {
      83                 :        1178 :   JIT_LOG_SCOPE (get_logger ());
      84                 :        1178 : }
      85                 :             : 
      86                 :             : /* Do the real work of creating the on-disk tempdir.
      87                 :             :    We do this here, rather than in the jit::tempdir constructor
      88                 :             :    so that we can handle failure without needing exceptions.  */
      89                 :             : 
      90                 :             : bool
      91                 :        1178 : gcc::jit::tempdir::create ()
      92                 :             : {
      93                 :        1178 :   JIT_LOG_SCOPE (get_logger ());
      94                 :             : 
      95                 :             : #ifdef _WIN32
      96                 :             :   m_path_tempdir = win_mkdtemp ();
      97                 :             : #else
      98                 :        1178 :   m_path_template = make_tempdir_path_template ();
      99                 :        1178 :   if (!m_path_template)
     100                 :             :     return false;
     101                 :             : 
     102                 :        1178 :   log ("m_path_template: %s", m_path_template);
     103                 :             : 
     104                 :             :   /* Create tempdir using mkdtemp.  This is created with 0700 perms and
     105                 :             :      is unique.  Hence no other (non-root) users should have access to
     106                 :             :      the paths within it.  */
     107                 :        1178 :   m_path_tempdir = mkdtemp (m_path_template);
     108                 :             : #endif
     109                 :             : 
     110                 :        1178 :   if (!m_path_tempdir)
     111                 :             :     return false;
     112                 :        1178 :   log ("m_path_tempdir: %s", m_path_tempdir);
     113                 :             : 
     114                 :        1178 :   m_path_c_file = concat (m_path_tempdir, "/fake.c", NULL);
     115                 :        1178 :   m_path_s_file = concat (m_path_tempdir, "/fake.s", NULL);
     116                 :        1178 :   m_path_so_file = concat (m_path_tempdir, "/fake.so", NULL);
     117                 :             : 
     118                 :             :   /* Success.  */
     119                 :        1178 :   return true;
     120                 :        1178 : }
     121                 :             : 
     122                 :             : /* The destructor for the jit::tempdir object, which
     123                 :             :    cleans up the filesystem directory and its contents
     124                 :             :    (unless keep_intermediates was set).  */
     125                 :             : 
     126                 :        1178 : gcc::jit::tempdir::~tempdir ()
     127                 :             : {
     128                 :        1178 :   JIT_LOG_SCOPE (get_logger ());
     129                 :             : 
     130                 :        1178 :   if (m_keep_intermediates)
     131                 :           0 :     fprintf (stderr, "intermediate files written to %s\n", m_path_tempdir);
     132                 :             :   else
     133                 :             :     {
     134                 :             :       /* Clean up .s/.so.  */
     135                 :        1178 :       if (m_path_s_file)
     136                 :             :         {
     137                 :        1178 :           log ("unlinking .s file: %s", m_path_s_file);
     138                 :        1178 :           unlink (m_path_s_file);
     139                 :             :         }
     140                 :        1178 :       if (m_path_so_file)
     141                 :             :         {
     142                 :        1178 :           log ("unlinking .so file: %s", m_path_so_file);
     143                 :        1178 :           unlink (m_path_so_file);
     144                 :             :         }
     145                 :             : 
     146                 :             :       /* Clean up any other tempfiles.  */
     147                 :             :       int i;
     148                 :             :       char *tempfile;
     149                 :        1239 :       FOR_EACH_VEC_ELT (m_tempfiles, i, tempfile)
     150                 :             :         {
     151                 :          61 :           log ("unlinking tempfile: %s", tempfile);
     152                 :          61 :           unlink (tempfile);
     153                 :             :         }
     154                 :             : 
     155                 :             :       /* The tempdir should now be empty; remove it.  */
     156                 :        1178 :       if (m_path_tempdir)
     157                 :             :         {
     158                 :        1178 :           log ("removing tempdir: %s", m_path_tempdir);
     159                 :        1178 :           rmdir (m_path_tempdir);
     160                 :             :         }
     161                 :             :     }
     162                 :             : 
     163                 :        1178 :   free (m_path_template);
     164                 :             :   /* m_path_tempdir aliases m_path_template, or is NULL, so don't
     165                 :             :      attempt to free it .  */
     166                 :        1178 :   free (m_path_c_file);
     167                 :        1178 :   free (m_path_s_file);
     168                 :        1178 :   free (m_path_so_file);
     169                 :             : 
     170                 :        1178 :   int i;
     171                 :        1178 :   char *tempfile;
     172                 :        1239 :   FOR_EACH_VEC_ELT (m_tempfiles, i, tempfile)
     173                 :          61 :     free (tempfile);
     174                 :        1178 : }
        

Generated by: LCOV version 2.0-1

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.