f7f049fa46
* gcc/config/i386/cygwin.h (STARTFILE_SPEC): Add vtv_start.o, if -fvtable-verify=preinit/std is used. * gcc/config/i386/mingw-w64.h (STARTFILE_SPEC): Likewise. * gcc/config/i386/mingw32.h (STARTFILE_SPEC): Likewise. * gcc/config/i386/cygwin.h (ENDFILE_SPEC): Add vtv_end.o, if -fvtable-verify=preinit/std is used. * gcc/config/i386/mingw32.h (ENDFILE_SPEC): Likewise. * gcc/config/i386/cygwin.h (LIB_SPEC): Pass -lvtv and -lpsapi, if -fvtable-verify=preinit/std is used. * gcc/config/i386/mingw-w64.h (LIB_SPEC): Likewise. * gcc/config/i386/mingw32.h (LIB_SPEC): Likewise. * gcc/cp/vtable-class-hierarchy.c (vtv_generate_init_routine): Add check for not TARGET_PECOFF at the VTV_PREINIT_PRIORITY checks. * gcc/varasm.c (assemble_variable): Add code to properly set the comdat section and name for the .vtable_map_vars section in case the target is PE or COFF. * libgcc/Makefile.in: Move rules to build vtv_*.o out of the check for CUSTOM_CRTSTUFF. * libgcc/config.host (i[34567]86-*-cygwin*, x86_64-*-cygwin*, i[34567]86-*-mingw*) (x86_64-*-mingw*): Only add vtv_*.o to extra_parts if enable_vtable_verify. * libstdc++-v3/acinclude.m4: Define VTV_CYGMIN. * libstdc++-v3/configure: Regenerate. * libstdc++-v3/libsupc++/Makefile.am: Add vtv_sources only to libsupc___la_SOURCES and libsupc__convenience_la_SOURCES if VTV_CYGMIN is not set. * libstdc++-v3/libsupc++/Makefile.in: Regenerated. * libstdc++-v3/libsupc++/vtv_stubs.cc: Add none weak declaration of every function for Cygwin and MinGW. * libstdc++-v3/src/Makefile.am: Add libvtv.la to toolexeclib_LTLIBRARIES, if VTV_CYGMIN is set. Define libvtv_la_SOURCES, libvtv_la_LDFLAGS, libvtv_la_AM_CXXFLAGS and libvtv_la_LINK if VTV_CYGMIN is set. * libstdc++-v3/src/Makefile.in: Regenerate. * libvtv/Makefile.am : Add libvtv.la to toolexeclib_LTLIBRARIES, if VTV_CYGMIN is set. Define libvtv_la_LIBADD, libvtv_la_LDFLAGS, libvtv_stubs_la_LDFLAGS and libvtv_stubs_la_SOURCES if VTV_CYGMIN is set. Add obstac.c to libvtv_la_SOURCES if VTV_CYGMIN is set. * libvtv/Makefile.in : Regenerate. * libvtv/aclocal.m4 : Regenerate. * libvtv/configure : Regenerate. * libvtv/configure.ac : Add ACX_LT_HOST_FLAGS. Define VTV_CYGMIN. * libvtv/configure.tgt : (x86_64-*-cygwin*, i?86-*-cygwin*, x86_64-*-mingw*) (i?86-*-mingw*): Add to supported targets. * libvtv/vtv_fail.cc : Skip inclusion of execinfo.h on Cygwin and MinGW. (log_error_message): Skip calls to backtrace and backtrace_symbols_fd on Cygwin and MinGW. * libvtv/vtv_malloc.cc : Include windows.h and skip sys/mman.h inclusion on Cygwin and MinGW. Add sysconf port on Cygwin and MinGW. (obstack_chunk_alloc): Exchange call to mmap with call to VirtualAlloc on Cygwin and MinGW. (__vtv_malloc_init): Exchange call to sysconf with call to port of sysconf on Cygwin and MinGW. * libvtv/vtv_malloc.h : Declare mprotect and define PROT_READ and PROT_WRITE on Cygwin and MinGW. * libvtv/map.h : Include stdint.h on MinGW. * libvtv/rts.cc : Include windows.h, winternl.h and psapi.h, skip include of execinfo.h, sys/mman.h and link.h on Cygwin and MinGW. Add port of __fortify_fail on Cygwin and MinGW. Change ElfW (Addr) to uintptr_t on Cygwin and MinGW. (read_section_offset_and_length): Add port for Cygwin and MinGW (iterate_modules): New function. (vtv_unprotect_vtable_vars): Use iterate_modules instead of dl_iterate_phdr on Cygwin and MinGW. (vtv_protect_vtable_vars): Likewise. (count_all_pages): Likewise. (dl_iterate_phdr_count_pages): Don't build on Cygwin and MinGW. * libvtv/utils.cc : Include windows.h and skip execinfo.h inclusion on Cygwin and MinGW. (__vtv_open_log): Exchange call to getuid and getpid with GetCurrentProcessId and adjust call to snprintf accordingly on Cygwin and MinGW. Adjust calls to mkdir on MinGW. Adjust call to open on Cygwin and MinGW. (__vtv_add_to_log): Adjust call to snprintf on Cygwin and MinGW. (__vtv_log_verification_failure): Don't generate a backtrace on Cygwin and MinGW. From-SVN: r220232
192 lines
5.5 KiB
C++
192 lines
5.5 KiB
C++
/* Copyright (C) 2012-2013
|
|
Free Software Foundation
|
|
|
|
This file is part of GCC.
|
|
|
|
GCC is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3, or (at your option)
|
|
any later version.
|
|
|
|
GCC is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
3.1, as published by the Free Software Foundation.
|
|
|
|
You should have received a copy of the GNU General Public License and
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
/* This file is part of the vtable verication runtime library (see
|
|
comments in vtv_rts.cc for more information about vtable
|
|
verification). This file contains log file utilities. */
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#if defined (__CYGWIN__) || defined (__MINGW32__)
|
|
#include <windows.h>
|
|
#else
|
|
#include <execinfo.h>
|
|
#endif
|
|
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
|
|
#include "vtv_utils.h"
|
|
|
|
#ifndef HAVE_SECURE_GETENV
|
|
# ifdef HAVE___SECURE_GETENV
|
|
# define secure_getenv __secure_getenv
|
|
# else
|
|
# define secure_getenv getenv
|
|
# endif
|
|
#endif
|
|
|
|
static int vtv_failures_log_fd = -1;
|
|
|
|
/* This function takes the NAME of a log file to open, attempts to
|
|
open it in the logs_dir directory, and returns the resulting file
|
|
descriptor.
|
|
|
|
This function first checks to see if the user has specifed (via
|
|
the environment variable VTV_LOGS_DIR) a directory to use for the
|
|
vtable verification logs. If that fails, the function will open
|
|
the logs in the current directory.
|
|
*/
|
|
|
|
int
|
|
__vtv_open_log (const char *name)
|
|
{
|
|
char log_name[1024];
|
|
char log_dir[512];
|
|
#if defined (__CYGWIN__) || defined (__MINGW32__)
|
|
pid_t process_id = GetCurrentProcessId ();
|
|
#else
|
|
uid_t user_id = getuid ();
|
|
pid_t process_id = getpid ();
|
|
#endif
|
|
char *logs_prefix;
|
|
bool logs_dir_specified = false;
|
|
int fd = -1;
|
|
|
|
logs_prefix = secure_getenv ("VTV_LOGS_DIR");
|
|
if (logs_prefix && strlen (logs_prefix) > 0)
|
|
{
|
|
logs_dir_specified = true;
|
|
#ifdef __MINGW32__
|
|
mkdir (logs_prefix);
|
|
#else
|
|
mkdir (logs_prefix, S_IRWXU);
|
|
#endif
|
|
|
|
snprintf (log_dir, sizeof (log_dir), "%s/vtv_logs", logs_prefix);
|
|
|
|
#ifdef __MINGW32__
|
|
mkdir (log_dir);
|
|
#else
|
|
mkdir (log_dir, S_IRWXU);
|
|
#endif
|
|
#if defined (__CYGWIN__) || defined (__MINGW32__)
|
|
snprintf (log_name, sizeof (log_name), "%s_%d_%s", log_dir,
|
|
(unsigned) process_id, name);
|
|
fd = open (log_name, O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
|
|
#else
|
|
snprintf (log_name, sizeof (log_name), "%s/%d_%d_%s", log_dir,
|
|
(unsigned) user_id, (unsigned) process_id, name);
|
|
fd = open (log_name, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW,
|
|
S_IRWXU);
|
|
#endif
|
|
}
|
|
else
|
|
fd = dup (2);
|
|
|
|
if (fd == -1)
|
|
__vtv_add_to_log (2, "Cannot open log file %s %s\n", name,
|
|
strerror (errno));
|
|
return fd;
|
|
}
|
|
|
|
/* This function takes a file descriptor (FD) and a string (STR) and
|
|
tries to write the string to the file. */
|
|
|
|
static int
|
|
vtv_log_write (int fd, const char *str)
|
|
{
|
|
if (write (fd, str, strlen (str)) != -1)
|
|
return 0;
|
|
|
|
if (fd != 2) /* Make sure we dont get in a loop. */
|
|
__vtv_add_to_log (2, "Error writing to log: %s\n", strerror (errno));
|
|
return -1;
|
|
}
|
|
|
|
|
|
/* This function takes a file decriptor (LOG_FILE) and an output
|
|
format string (FORMAT), followed by zero or more print format
|
|
arguments (the same as fprintf, for example). It gets the current
|
|
process ID and PPID, pre-pends them to the formatted message, and
|
|
writes write it out to the log file referenced by LOG_FILE via calles
|
|
to vtv_log_write. */
|
|
|
|
int
|
|
__vtv_add_to_log (int log_file, const char * format, ...)
|
|
{
|
|
/* We dont want to dynamically allocate this buffer. This should be
|
|
more than enough in most cases. It if isn't we are careful not to
|
|
do a buffer overflow. */
|
|
char output[1024];
|
|
|
|
va_list ap;
|
|
va_start (ap, format);
|
|
|
|
#if defined (__CYGWIN__) || defined (__MINGW32__)
|
|
snprintf (output, sizeof (output), "VTV: PID=%ld ", GetCurrentProcessId ());
|
|
#else
|
|
snprintf (output, sizeof (output), "VTV: PID=%d PPID=%d ", getpid (),
|
|
getppid ());
|
|
#endif
|
|
vtv_log_write (log_file, output);
|
|
vsnprintf (output, sizeof (output), format, ap);
|
|
vtv_log_write (log_file, output);
|
|
va_end (ap);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* Open error logging file, if not already open, and write vtable
|
|
verification failure messages (LOG_MSG) to the log file. Also
|
|
generate a backtrace in the log file, if GENERATE_BACKTRACE is
|
|
set. */
|
|
|
|
void
|
|
__vtv_log_verification_failure (const char *log_msg, bool generate_backtrace)
|
|
{
|
|
if (vtv_failures_log_fd == -1)
|
|
vtv_failures_log_fd = __vtv_open_log ("vtable_verification_failures.log");
|
|
|
|
if (vtv_failures_log_fd == -1)
|
|
return;
|
|
|
|
__vtv_add_to_log (vtv_failures_log_fd, "%s", log_msg);
|
|
|
|
#if !defined (__CYGWIN__) && !defined (__MINGW32__)
|
|
if (generate_backtrace)
|
|
{
|
|
#define STACK_DEPTH 20
|
|
void *callers[STACK_DEPTH];
|
|
int actual_depth = backtrace (callers, STACK_DEPTH);
|
|
backtrace_symbols_fd (callers, actual_depth, vtv_failures_log_fd);
|
|
}
|
|
#endif
|
|
}
|