LCOV - code coverage report
Current view: top level - gcc/jit - jit-tempdir.c (source / functions) Hit Total Coverage
Test: gcc.info Lines: 57 58 98.3 %
Date: 2020-03-28 11:57:23 Functions: 4 4 100.0 %
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-2020 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                 :            : 
      28                 :            : /* Construct a tempdir path template suitable for use by mkdtemp
      29                 :            :    e.g. "/tmp/libgccjit-XXXXXX", but respecting the rules in
      30                 :            :    libiberty's choose_tempdir rather than hardcoding "/tmp/".
      31                 :            : 
      32                 :            :    The memory is allocated using malloc and must be freed.
      33                 :            :    Aborts the process if allocation fails. */
      34                 :            : 
      35                 :            : static char *
      36                 :        888 : make_tempdir_path_template ()
      37                 :            : {
      38                 :        888 :   const char *tmpdir_buf;
      39                 :        888 :   size_t tmpdir_len;
      40                 :        888 :   const char *file_template_buf;
      41                 :        888 :   size_t file_template_len;
      42                 :        888 :   char *result;
      43                 :            : 
      44                 :            :   /* The result of choose_tmpdir is a cached buffer within libiberty, so
      45                 :            :      we must *not* free it.  */
      46                 :        888 :   tmpdir_buf = choose_tmpdir ();
      47                 :            : 
      48                 :            :   /* choose_tmpdir aborts on malloc failure.  */
      49                 :        888 :   gcc_assert (tmpdir_buf);
      50                 :            : 
      51                 :        888 :   tmpdir_len = strlen (tmpdir_buf);
      52                 :            :   /* tmpdir_buf should now have a dir separator as the final byte.  */
      53                 :        888 :   gcc_assert (tmpdir_len > 0);
      54                 :        888 :   gcc_assert (tmpdir_buf[tmpdir_len - 1] == DIR_SEPARATOR);
      55                 :            : 
      56                 :        888 :   file_template_buf = "libgccjit-XXXXXX";
      57                 :        888 :   file_template_len = strlen (file_template_buf);
      58                 :            : 
      59                 :        888 :   result = XNEWVEC (char, tmpdir_len + file_template_len + 1);
      60                 :        888 :   strcpy (result, tmpdir_buf);
      61                 :        888 :   strcpy (result + tmpdir_len, file_template_buf);
      62                 :            : 
      63                 :        888 :   return result;
      64                 :            : }
      65                 :            : 
      66                 :            : /* The constructor for the jit::tempdir object.
      67                 :            :    The real work is done by the jit::tempdir::create method.  */
      68                 :            : 
      69                 :        888 : gcc::jit::tempdir::tempdir (logger *logger, int keep_intermediates)
      70                 :            :   : log_user (logger),
      71                 :            :     m_keep_intermediates (keep_intermediates),
      72                 :            :     m_path_template (NULL),
      73                 :            :     m_path_tempdir (NULL),
      74                 :            :     m_path_c_file (NULL),
      75                 :            :     m_path_s_file (NULL),
      76                 :        888 :     m_path_so_file (NULL)
      77                 :            : {
      78                 :        888 :   JIT_LOG_SCOPE (get_logger ());
      79                 :        888 : }
      80                 :            : 
      81                 :            : /* Do the real work of creating the on-disk tempdir.
      82                 :            :    We do this here, rather than in the jit::tempdir constructor
      83                 :            :    so that we can handle failure without needing exceptions.  */
      84                 :            : 
      85                 :            : bool
      86                 :        888 : gcc::jit::tempdir::create ()
      87                 :            : {
      88                 :       1776 :   JIT_LOG_SCOPE (get_logger ());
      89                 :            : 
      90                 :        888 :   m_path_template = make_tempdir_path_template ();
      91                 :        888 :   if (!m_path_template)
      92                 :            :     return false;
      93                 :            : 
      94                 :        888 :   log ("m_path_template: %s", m_path_template);
      95                 :            : 
      96                 :            :   /* Create tempdir using mkdtemp.  This is created with 0700 perms and
      97                 :            :      is unique.  Hence no other (non-root) users should have access to
      98                 :            :      the paths within it.  */
      99                 :        888 :   m_path_tempdir = mkdtemp (m_path_template);
     100                 :        888 :   if (!m_path_tempdir)
     101                 :            :     return false;
     102                 :        888 :   log ("m_path_tempdir: %s", m_path_tempdir);
     103                 :            : 
     104                 :        888 :   m_path_c_file = concat (m_path_tempdir, "/fake.c", NULL);
     105                 :        888 :   m_path_s_file = concat (m_path_tempdir, "/fake.s", NULL);
     106                 :        888 :   m_path_so_file = concat (m_path_tempdir, "/fake.so", NULL);
     107                 :            : 
     108                 :            :   /* Success.  */
     109                 :        888 :   return true;
     110                 :            : }
     111                 :            : 
     112                 :            : /* The destructor for the jit::tempdir object, which
     113                 :            :    cleans up the filesystem directory and its contents
     114                 :            :    (unless keep_intermediates was set).  */
     115                 :            : 
     116                 :        924 : gcc::jit::tempdir::~tempdir ()
     117                 :            : {
     118                 :        888 :   JIT_LOG_SCOPE (get_logger ());
     119                 :            : 
     120                 :        888 :   if (m_keep_intermediates)
     121                 :          0 :     fprintf (stderr, "intermediate files written to %s\n", m_path_tempdir);
     122                 :            :   else
     123                 :            :     {
     124                 :            :       /* Clean up .s/.so.  */
     125                 :        888 :       if (m_path_s_file)
     126                 :            :         {
     127                 :        888 :           log ("unlinking .s file: %s", m_path_s_file);
     128                 :        888 :           unlink (m_path_s_file);
     129                 :            :         }
     130                 :        888 :       if (m_path_so_file)
     131                 :            :         {
     132                 :        888 :           log ("unlinking .so file: %s", m_path_so_file);
     133                 :        888 :           unlink (m_path_so_file);
     134                 :            :         }
     135                 :            : 
     136                 :            :       /* Clean up any other tempfiles.  */
     137                 :            :       int i;
     138                 :            :       char *tempfile;
     139                 :        944 :       FOR_EACH_VEC_ELT (m_tempfiles, i, tempfile)
     140                 :            :         {
     141                 :         56 :           log ("unlinking tempfile: %s", tempfile);
     142                 :         56 :           unlink (tempfile);
     143                 :            :         }
     144                 :            : 
     145                 :            :       /* The tempdir should now be empty; remove it.  */
     146                 :        888 :       if (m_path_tempdir)
     147                 :            :         {
     148                 :        888 :           log ("removing tempdir: %s", m_path_tempdir);
     149                 :        888 :           rmdir (m_path_tempdir);
     150                 :            :         }
     151                 :            :     }
     152                 :            : 
     153                 :        888 :   free (m_path_template);
     154                 :            :   /* m_path_tempdir aliases m_path_template, or is NULL, so don't
     155                 :            :      attempt to free it .  */
     156                 :        888 :   free (m_path_c_file);
     157                 :        888 :   free (m_path_s_file);
     158                 :        888 :   free (m_path_so_file);
     159                 :            : 
     160                 :        888 :   int i;
     161                 :        888 :   char *tempfile;
     162                 :        980 :   FOR_EACH_VEC_ELT (m_tempfiles, i, tempfile)
     163                 :         56 :     free (tempfile);
     164                 :        888 : }

Generated by: LCOV version 1.0

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 --enable-host-shared. GCC test suite is run with the built compiler.