Re: [Ping] Port of VTV for Cygwin and MinGW

2015-02-02 Thread Patrick Wollgast
Hi,

after the missed bug at Linux with no VTV I checked everything again on
the trunk. I saw that I erroneously wrote in the changelog for
libvtv/aclocal.m4 regenerate and deleted the change from the patch. The
only change I made there in my working directory was the following.

Index: libvtv/aclocal.m4
===
--- libvtv/aclocal.m4   (Revision 220306)
+++ libvtv/aclocal.m4   (Arbeitskopie)
@@ -1006,6 +1006,7 @@ AC_SUBST([am__untar])
 m4_include([../config/acx.m4])
 m4_include([../config/depstand.m4])
 m4_include([../config/lead-dot.m4])
+m4_include([../config/lthostflags.m4])
 m4_include([../config/libstdc++-raw-cxx.m4])
 m4_include([../config/multi.m4])
 m4_include([../config/override.m4])

And then autoconf/automake again.


Something I missed during my last test, since Cygwin with gcc 4.9 and
the patch bootstrapped fine, is the following. One of the last changes
to the patch was to remove the implementation of mprotect in libvtv/
(copied from the MinGW port from libgcc2.c), because libgcc2.c
implements it for MinGW, and cygwin1.dll implements it for Cygwin.
However, PAGE_SIZE/PAGESIZE returns 0x1 on Cygwin on a 64bit PC/VM
(don't have a 32bit PC/VM but I assume that the value would be 0x1000
there). On Linux 64bit it returns 0x1000 and on Windows 64bit with
SYSTEM_INFO/dwPageSize also returns 0x1000. This causes mprotect of
Cygwin to fail for libvtv, since the passed address is checked for
alignment with PAGE_SIZE/PAGESIZE.

The solutions I come up with are:
- Set VTV_PAGE_SIZE to 0x1 on Cygwin with 64bit PCs/VMs. But
  this would set more than the desired section to be read/write.
  Practically the whole dll would be writable for the
  .vtable_map_vars section to be writable. Therefore I don't
  recommend this solution. The changes would be in
  include/vtv-change-permission.h, and various other files where
  sizes have to be changed.
- Add the mprotect implementation from libgcc2.c again for Cygwin
  in libvtv/. In libgcc2.c it isn't build for Cygwin. The changes
  would just be in libvtv/. I'd prefer this solution.


Patrick


Re: [Ping] Port of VTV for Cygwin and MinGW

2015-01-28 Thread Patrick Wollgast
Ping.

https://gcc.gnu.org/ml/gcc-patches/2015-01/msg01270.html

On 15.01.2015 22:50, Patrick Wollgast wrote:
 On 15.01.2015 17:01, Ian Lance Taylor wrote:
 On Wed, Jan 14, 2015 at 11:54 PM, Patrick Wollgast
 patrick.wollg...@rub.de wrote:
 Is there something I'm still supposed to do, since I don't have write
 access and this was the last part missing an OK?

 Somebody with write access will need to commit the patch for you.  You
 should send a new clean patch including all the changes, along with
 updated ChangeLog entries.

 Ian

 
 For the clean patch I co'ed the latest version of the trunk again and
 applied my patch. It applies correctly, except for two changes:
 
 patching file libgcc/Makefile.in
 Hunk #1 succeeded at 995 with fuzz 2 (offset 9 lines).
 Hunk #2 succeeded at 1020 (offset 17 lines).
 
 # This is a version of crtbegin for -static links.
 crtbeginT$(objext): $(srcdir)/crtstuff.c
  $(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $ -DCRT_BEGIN
 -DCRTSTUFFT_O
 endif

 ifeq ($(enable_vtable_verify),yes)
 # These are used in vtable verification; see comments in source files
 for
 # more details.
 
 I had to move the endif down, since something was added before
 ifeq ($(enable_vtable_verify),yes) inside the if.
 
 # This is a version of crtbegin for -static links.
 crtbeginT$(objext): $(srcdir)/crtstuff.c
  $(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $ -DCRT_BEGIN
 -DCRTSTUFFT_O

 # crtoffloadbegin and crtoffloadend contain symbols, that mark the
 begin and
 # the end of tables with addresses, required for offloading.
 crtoffloadbegin$(objext): $(srcdir)/offloadstuff.c
  $(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $ -DCRT_BEGIN

 crtoffloadend$(objext): $(srcdir)/offloadstuff.c
  $(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $ -DCRT_END
 endif

 ifeq ($(enable_vtable_verify),yes)
 # These are used in vtable verification; see comments in source files
 for
 # more details.
 
 
 patching file libgcc/config.host
 Hunk #1 succeeded at 621 (offset 6 lines).
 Hunk #2 succeeded at 640 (offset 6 lines).
 Hunk #3 succeeded at 660 (offset 6 lines).
 Hunk #4 succeeded at 1198 with fuzz 2 (offset 495 lines).
 
  tmake_file=${tmake_file} ${tmake_eh_file} ${tmake_dlldir_file}
 i386/t-slibgcc-cygming i386/t-mingw32 t-dfprules i386/t-crtfm i386/t-chkstk
  extra_parts=$extra_parts crtfastmath.o
 
 The last two lines were changed to the following two lines.
 
  tmake_file=${tmake_file} ${tmake_eh_file} ${tmake_dlldir_file}
 i386/t-slibgcc-cygming i386/t-cygming i386/t-mingw32 t-dfprules
 i386/t-crtfm i386/t-chkstk
  extra_parts=$extra_parts crtbegin.o crtend.o crtfastmath.o
 
 And therefore Hunk #4, which follows these lines, wasn't applied correctly.
 
 These two parts were corrected in vtv_cygmin_clean.patch. For
 convenience I also added vtv_cygmin_unclean.patch, which is the patch
 from my last mail.
 
 Regards,
 Patrick
 


Re: [Ping] Port of VTV for Cygwin and MinGW

2015-01-15 Thread Patrick Wollgast
On 15.01.2015 17:01, Ian Lance Taylor wrote:
 On Wed, Jan 14, 2015 at 11:54 PM, Patrick Wollgast
 patrick.wollg...@rub.de wrote:
 Is there something I'm still supposed to do, since I don't have write
 access and this was the last part missing an OK?
 
 Somebody with write access will need to commit the patch for you.  You
 should send a new clean patch including all the changes, along with
 updated ChangeLog entries.
 
 Ian
 

For the clean patch I co'ed the latest version of the trunk again and
applied my patch. It applies correctly, except for two changes:

patching file libgcc/Makefile.in
Hunk #1 succeeded at 995 with fuzz 2 (offset 9 lines).
Hunk #2 succeeded at 1020 (offset 17 lines).

 # This is a version of crtbegin for -static links.
 crtbeginT$(objext): $(srcdir)/crtstuff.c
   $(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $ -DCRT_BEGIN
 -DCRTSTUFFT_O
 endif

 ifeq ($(enable_vtable_verify),yes)
 # These are used in vtable verification; see comments in source files
 for
 # more details.

I had to move the endif down, since something was added before
ifeq ($(enable_vtable_verify),yes) inside the if.

 # This is a version of crtbegin for -static links.
 crtbeginT$(objext): $(srcdir)/crtstuff.c
   $(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $ -DCRT_BEGIN
 -DCRTSTUFFT_O

 # crtoffloadbegin and crtoffloadend contain symbols, that mark the
 begin and
 # the end of tables with addresses, required for offloading.
 crtoffloadbegin$(objext): $(srcdir)/offloadstuff.c
   $(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $ -DCRT_BEGIN

 crtoffloadend$(objext): $(srcdir)/offloadstuff.c
   $(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $ -DCRT_END
 endif

 ifeq ($(enable_vtable_verify),yes)
 # These are used in vtable verification; see comments in source files
 for
 # more details.


patching file libgcc/config.host
Hunk #1 succeeded at 621 (offset 6 lines).
Hunk #2 succeeded at 640 (offset 6 lines).
Hunk #3 succeeded at 660 (offset 6 lines).
Hunk #4 succeeded at 1198 with fuzz 2 (offset 495 lines).

   tmake_file=${tmake_file} ${tmake_eh_file} ${tmake_dlldir_file}
i386/t-slibgcc-cygming i386/t-mingw32 t-dfprules i386/t-crtfm i386/t-chkstk
   extra_parts=$extra_parts crtfastmath.o

The last two lines were changed to the following two lines.

   tmake_file=${tmake_file} ${tmake_eh_file} ${tmake_dlldir_file}
i386/t-slibgcc-cygming i386/t-cygming i386/t-mingw32 t-dfprules
i386/t-crtfm i386/t-chkstk
   extra_parts=$extra_parts crtbegin.o crtend.o crtfastmath.o

And therefore Hunk #4, which follows these lines, wasn't applied correctly.

These two parts were corrected in vtv_cygmin_clean.patch. For
convenience I also added vtv_cygmin_unclean.patch, which is the patch
from my last mail.

Regards,
Patrick
* 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

Re: [Ping] Port of VTV for Cygwin and MinGW

2015-01-14 Thread Patrick Wollgast
On 14.01.2015 20:00, Ian Lance Taylor wrote:
 On Thu, Jan 8, 2015 at 12:33 PM, Patrick Wollgast
 patrick.wollg...@rub.de wrote:
 A short recap again:

 Latest patch, changelog and a test program (further information about
 the program in the mail):
 https://gcc.gnu.org/ml/gcc-patches/2014-11/msg03368.html
 
 In that patch, the change to varasm.c looks wrong if neither
 OBJECT_FORMAT_ELF nor TARGET_PECOFF are defined.  It looks like you've
 dropped the switch_to_section call in that case.
 
 Ian
 

You're right. It should have been '#else' again, instead of 'else'
before the switch_to_section call.

Regards,
Patrick
Index: gcc/config/i386/cygwin.h
===
--- gcc/config/i386/cygwin.h	(Revision 214408)
+++ gcc/config/i386/cygwin.h	(Arbeitskopie)
@@ -41,12 +41,18 @@ along with GCC; see the file COPYING3.
 #define STARTFILE_SPEC \
   %{!shared: %{!mdll: crt0%O%s \
   %{pg:gcrt0%O%s}}}\
-  %{shared:crtbeginS.o%s;:crtbegin.o%s}
+  %{shared:crtbeginS.o%s;:crtbegin.o%s} \
+  %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_start.o%s; \
+fvtable-verify=std:vtv_start.o%s}
 
 #undef ENDFILE_SPEC
 #define ENDFILE_SPEC \
   %{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s}\
%{!shared:%:if-exists(default-manifest.o%s)}\
+   %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_end.o%s; \
+fvtable-verify=std:vtv_end.o%s} \
crtend.o%s
 
 /* Normally, -lgcc is not needed since everything in it is in the DLL, but we
@@ -81,6 +87,8 @@ along with GCC; see the file COPYING3.
   %{pthread: } \
   -lcygwin \
   %{mwindows:-lgdi32 -lcomdlg32} \
+  %{fvtable-verify=preinit:-lvtv -lpsapi; \
+fvtable-verify=std:-lvtv -lpsapi} \
   -ladvapi32 -lshell32 -luser32 -lkernel32
 
 /* To implement C++ function replacement we always wrap the cxx
Index: gcc/config/i386/mingw-w64.h
===
--- gcc/config/i386/mingw-w64.h	(Revision 214408)
+++ gcc/config/i386/mingw-w64.h	(Arbeitskopie)
@@ -32,7 +32,10 @@ along with GCC; see the file COPYING3.
   %{!shared:%{!mdll:%{!municode:crt2%O%s}}} \
   %{!shared:%{!mdll:%{municode:crt2u%O%s}}} \
   %{pg:gcrt2%O%s} \
-  crtbegin.o%s
+  crtbegin.o%s \
+  %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_start.o%s; \
+fvtable-verify=std:vtv_start.o%s}
 
 /* Enable multilib.  */
 
@@ -43,6 +46,8 @@ along with GCC; see the file COPYING3.
 #define LIB_SPEC %{pg:-lgmon} %{ SPEC_PTHREAD1 :-lpthread}  \
 		 %{ SPEC_PTHREAD2 : }  \
 		 %{mwindows:-lgdi32 -lcomdlg32}  \
+ %{fvtable-verify=preinit:-lvtv -lpsapi; \
+fvtable-verify=std:-lvtv -lpsapi}  \
 		 -ladvapi32 -lshell32 -luser32 -lkernel32
 
 #undef SPEC_32
Index: gcc/config/i386/mingw32.h
===
--- gcc/config/i386/mingw32.h	(Revision 214408)
+++ gcc/config/i386/mingw32.h	(Arbeitskopie)
@@ -91,6 +91,8 @@ along with GCC; see the file COPYING3.
 #define LIB_SPEC %{pg:-lgmon} %{ SPEC_PTHREAD1 :-lpthread}  \
 		 %{ SPEC_PTHREAD2 : }  \
 		 %{mwindows:-lgdi32 -lcomdlg32}  \
+ %{fvtable-verify=preinit:-lvtv -lpsapi; \
+fvtable-verify=std:-lvtv -lpsapi}  \
  -ladvapi32 -lshell32 -luser32 -lkernel32
 
 /* Weak symbols do not get resolved if using a Windows dll import lib.
@@ -143,12 +145,18 @@ along with GCC; see the file COPYING3.
 #undef STARTFILE_SPEC
 #define STARTFILE_SPEC %{shared|mdll:dllcrt2%O%s} \
   %{!shared:%{!mdll:crt2%O%s}} %{pg:gcrt2%O%s} \
-  crtbegin.o%s
+  crtbegin.o%s \
+  %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_start.o%s; \
+fvtable-verify=std:vtv_start.o%s}
 
 #undef ENDFILE_SPEC
 #define ENDFILE_SPEC \
   %{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
%{!shared:%:if-exists(default-manifest.o%s)}\
+   %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_end.o%s; \
+fvtable-verify=std:vtv_end.o%s} \
   crtend.o%s
 
 /* Override startfile prefix defaults.  */
Index: gcc/cp/vtable-class-hierarchy.c
===
--- gcc/cp/vtable-class-hierarchy.c	(Revision 214408)
+++ gcc/cp/vtable-class-hierarchy.c	(Arbeitskopie)
@@ -1182,7 +1182,11 @@ vtv_generate_init_routine (void)
   TREE_STATIC (vtv_fndecl) = 1;
   TREE_USED (vtv_fndecl) = 1;
   DECL_PRESERVE_P (vtv_fndecl) = 1;
+#if defined (TARGET_PECOFF)
+  if (flag_vtable_verify == VTV_PREINIT_PRIORITY  !TARGET_PECOFF)
+#else
   if (flag_vtable_verify == VTV_PREINIT_PRIORITY)
+#endif
 DECL_STATIC_CONSTRUCTOR (vtv_fndecl) = 0;
 
   gimplify_function_tree (vtv_fndecl);
@@ -1190,7 +1194,11 @@ vtv_generate_init_routine (void)
 
   cgraph_process_new_functions ();
 
+#if defined (TARGET_PECOFF)
+  if (flag_vtable_verify == VTV_PREINIT_PRIORITY  !TARGET_PECOFF)
+#else
   if (flag_vtable_verify == VTV_PREINIT_PRIORITY)
+#endif
 assemble_vtv_preinit_initializer (vtv_fndecl

Re: [Ping] Port of VTV for Cygwin and MinGW

2015-01-14 Thread Patrick Wollgast
On 15.01.2015 00:52, Ian Lance Taylor wrote:
 On Wed, Jan 14, 2015 at 12:28 PM, Patrick Wollgast
 patrick.wollg...@rub.de wrote:
 On 14.01.2015 20:00, Ian Lance Taylor wrote:
 On Thu, Jan 8, 2015 at 12:33 PM, Patrick Wollgast
 patrick.wollg...@rub.de wrote:
 A short recap again:

 Latest patch, changelog and a test program (further information about
 the program in the mail):
 https://gcc.gnu.org/ml/gcc-patches/2014-11/msg03368.html

 In that patch, the change to varasm.c looks wrong if neither
 OBJECT_FORMAT_ELF nor TARGET_PECOFF are defined.  It looks like you've
 dropped the switch_to_section call in that case.

 Ian


 You're right. It should have been '#else' again, instead of 'else'
 before the switch_to_section call.
 
 OK, the patches to varasm.c and cp/vtable-class-hierarchy.c are OK.
 
 Thanks.
 
 Ian
 

Thanks to all the reviewers!

Is there something I'm still supposed to do, since I don't have write
access and this was the last part missing an OK?

Regards,
Patrick


Re: [Ping] Port of VTV for Cygwin and MinGW

2015-01-08 Thread Patrick Wollgast
A short recap again:

Latest patch, changelog and a test program (further information about
the program in the mail):
https://gcc.gnu.org/ml/gcc-patches/2014-11/msg03368.html


Approved:
* gcc/config/i386/*
* libgcc/*
* libstdc++-v3/*
* libvtv/*  (Some changes made to three of these files.
 Listed in 'Not approved'.)


Not approved:
For the following two files I added checks, if TARGET_PECOFF is defined
( https://gcc.gnu.org/ml/gcc-patches/2014-10/msg00815.html )
* gcc/cp/vtable-class-hierarchy.c
* gcc/varasm.c

Reasons for changes in the following files stated in
https://gcc.gnu.org/ml/gcc-patches/2014-10/msg00815.html and in the mail
of the latest patch.

Removed implementation of mprotect.
* libvtv/vtv_malloc.cc
Added extern C to the prototype of mprotect.
* libvtv/vtv_malloc.h
Exchanged call to TerminateProcess with call to abort in __fortify_fail.
* libvtv/vtv_rts.cc

Has been removed from the most recent patch. Just listed for completeness.
* libiberty/obstack.c


Regards,
Patrick


Re: [Ping] Port of VTV for Cygwin and MinGW

2015-01-04 Thread Patrick Wollgast
On 10.12.2014 17:37, Patrick Wollgast wrote:
 Ping.
 
 https://gcc.gnu.org/ml/gcc-patches/2014-11/msg03368.html
 
 On 27.11.2014 10:42, Patrick Wollgast wrote:
 On 12.11.2014 19:40, Kai Tietz wrote:
 TerminateProcess is actually bad, as it doesn't call any of the atexit
 handlers.  You simply nuke the process off.  For cygwin this behavior
 is inacceptable.  Why a classical abort, or a classical exit call
 cause for you that issues?  It seems to me more related to some other
 thing you try to paper over by this.


 It turns out the test program made some trouble. I rewrote it to the
 attached program (virtual_func_test_min_AW.cpp). I changed obstack.c and
 vtv_rts.cc to the C-runtime functions. For testing I used a program just
 containing an abort and all three tests in the attached test program.
 The call stack, passed parameters and behavior matched at the crucial
 parts (tested again on MinGW 32/64bit).


 Regarding the question, why I reimplemented mprotect, I also haven't
 changed anything in the patch but answered the question.

 And this doesn't make it better.  It is present in the static part of
 libgcc.  Have you tried to declare it with extern C (for C++ case)
 and simply use it?
 Cygwin provides its own version too.  So there seems to me no real
 need to re-implement it.


 You're right. I was stuck with the idea of importing it dynamically, but
 changed it to extern C now.

 Regards,
 Patrick



Re: [Ping] Port of VTV for Cygwin and MinGW

2014-12-10 Thread Patrick Wollgast
Ping.

https://gcc.gnu.org/ml/gcc-patches/2014-11/msg03368.html

On 27.11.2014 10:42, Patrick Wollgast wrote:
 On 12.11.2014 19:40, Kai Tietz wrote:
 TerminateProcess is actually bad, as it doesn't call any of the atexit
 handlers.  You simply nuke the process off.  For cygwin this behavior
 is inacceptable.  Why a classical abort, or a classical exit call
 cause for you that issues?  It seems to me more related to some other
 thing you try to paper over by this.

 
 It turns out the test program made some trouble. I rewrote it to the
 attached program (virtual_func_test_min_AW.cpp). I changed obstack.c and
 vtv_rts.cc to the C-runtime functions. For testing I used a program just
 containing an abort and all three tests in the attached test program.
 The call stack, passed parameters and behavior matched at the crucial
 parts (tested again on MinGW 32/64bit).
 

 Regarding the question, why I reimplemented mprotect, I also haven't
 changed anything in the patch but answered the question.

 And this doesn't make it better.  It is present in the static part of
 libgcc.  Have you tried to declare it with extern C (for C++ case)
 and simply use it?
 Cygwin provides its own version too.  So there seems to me no real
 need to re-implement it.

 
 You're right. I was stuck with the idea of importing it dynamically, but
 changed it to extern C now.
 
 Regards,
 Patrick
 


Re: [Ping] Port of VTV for Cygwin and MinGW

2014-11-27 Thread Patrick Wollgast
On 12.11.2014 19:40, Kai Tietz wrote:
 TerminateProcess is actually bad, as it doesn't call any of the atexit
 handlers.  You simply nuke the process off.  For cygwin this behavior
 is inacceptable.  Why a classical abort, or a classical exit call
 cause for you that issues?  It seems to me more related to some other
 thing you try to paper over by this.
 

It turns out the test program made some trouble. I rewrote it to the
attached program (virtual_func_test_min_AW.cpp). I changed obstack.c and
vtv_rts.cc to the C-runtime functions. For testing I used a program just
containing an abort and all three tests in the attached test program.
The call stack, passed parameters and behavior matched at the crucial
parts (tested again on MinGW 32/64bit).

 
 Regarding the question, why I reimplemented mprotect, I also haven't
 changed anything in the patch but answered the question.
 
 And this doesn't make it better.  It is present in the static part of
 libgcc.  Have you tried to declare it with extern C (for C++ case)
 and simply use it?
 Cygwin provides its own version too.  So there seems to me no real
 need to re-implement it.
 

You're right. I was stuck with the idea of importing it dynamically, but
changed it to extern C now.

Regards,
Patrick
* 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 

Re: [Ping] Port of VTV for Cygwin and MinGW

2014-11-12 Thread Patrick Wollgast
Ping for the question below.

On 30.10.2014 15:29, Patrick Wollgast wrote:
 Since I haven't heard back for quite a while, I wanted to ask what the
 current stat of the patch is.
 
 Is the patch from the last mail approved (
 https://gcc.gnu.org/ml/gcc-patches/2014-10/msg01524.html ), or should
 the matters discussed further?
 
 regards,
 Patrick
 


Re: [Ping] Port of VTV for Cygwin and MinGW

2014-11-12 Thread Patrick Wollgast
 
 I don't think you have addressed all of the comments I made in the
 comment, do you?
 
 Regards,
 Kai
 

I added the three checks, if TARGET_PECOFF is defined, and fixed the
whitespace issues.

For the questions regarding C-runtime/Win32 functions I haven't changed
anything in the patch but explained why I used those functions.

 Why you use instead of C-runtime exit/abort-functions the
 platform-functions to terminate the process.  This looks to me like
 useless change.  For cygwin this might be even wrong in some aspects.
 What is the reasoning for this change?


 I haven't encountered crashes in obstack.c itself, but there were
 problems in vtv_rts.cc with abort() on MinGW 32bit. The following stack
 traces were taken at MinGW 32bit. Most of the time the process had to be
 stopped in the process manager because a wrong process handle was passed
 to NtTerminateProcess. This was tested and occurred on Windows 7 64bit
 and Windows 8.1 64bit. To be sure to avoid this issue the calls have
 also been exchanged in obstack.c.

 With abort(), correct process handle, postmortem debugger triggert.

  0003 0028fe98 ntdll!NtTerminateProcess+0xc
 0003 77e8f3b0  ntdll!RtlExitUserProcess+0x6d
 0003 0028f934 74f85472 KERNEL32!ExitProcessImplementation+0x12
 0003 11e9bfd9 00409000 msvcrt!exit+0x32
 0003 0001  msvcrt!flushall+0x2e9
 0003 00010001 0065 msvcrt!exit+0x11
 6efcf294 0080 0028ffcc msvcrt!abort+0xf3
 00560f70 000d 0001 libvtv_0!Z14__fortify_failPKc+0x18
 7ffde000 0028ffdc 77568f8b test_std+0x13de
 7ffde000 138a1dee  KERNEL32!BaseThreadInitThunk+0xe
  7755dad3  ntdll!__RtlUserThreadStart+0x20
 004014e0 7ffde000  ntdll!_RtlUserThreadStart+0x1b

 With abort(), wrong process handle, NtTerminateProcess returns instead
 of ending the own process. This case happens most of the time.

  0003 0028fe98 ntdll!NtTerminateProcess+0x5
 0003 77e8f3b0  ntdll!RtlExitUserProcess+0x35
 0003 0028f934 74f85472 KERNEL32!ExitProcessImplementation+0x12
 0003 9f9f5ea3 00409000 msvcrt!exit+0x32
 0003 0001  msvcrt!flushall+0x2e9
 0003 00010001 0040 msvcrt!exit+0x11
 6efcf294 0080 0028ffcc msvcrt!abort+0xf3
 00701060 001e 0001 libvtv_0!Z14__fortify_failPKc+0x18
 7ffde000 0028ffdc 77568f8b image0040+0x13de
 7ffde000 9defd39c  KERNEL32!BaseThreadInitThunk+0xe
  7755dac5  ntdll!__RtlUserThreadStart+0x20
 004014e0 7ffde000  ntdll!_RtlUserThreadStart+0x1b

 TerminateProcess. Everything's fine on MinGW x86, x86-64 (both gcc 5.0),
 Cygwin x86-64 (gcc 4.9.0).

  022f 0028feb8 ntdll!NtTerminateProcess+0x5
  022f 00409000 KERNELBASE!TerminateProcess+0x27
 00731060 001e 0001 libvtv_0!Z14__fortify_failPKc+0x2a
 7ffde000 0028ffdc 77568f8b image0040+0x13de
 7ffde000 e2645ec6  KERNEL32!BaseThreadInitThunk+0xe
  7755dae7  ntdll!__RtlUserThreadStart+0x20
 004014e0 7ffde000  ntdll!_RtlUserThreadStart+0x1b

Regarding the question, why I reimplemented mprotect, I also haven't
changed anything in the patch but answered the question.

 Another note I have about re-implementation of mprotect in ---
 libvtv/vtv_malloc.cc.  Why you need that?  it is already part of
 libgcc for mingw.  And for cygwin this function is part of cygwin's
 library itself.  So why re-implementing it here?


 It is already part of libgcc for MinGW, but it can neither be found in
 the exports of the dll, nor can the function prototype be found in any
 header files. Therefore I get unknown reference errors if I don't
 re-implement it. I checked the exports of several compilations for this.

 * self compiled MinGW
 * MinGW installed from the Arch Linux repositories
 *

http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Automated%20Builds/mingw-w64-bin_x86_64-linux_20131228.tar.bz2/download

 If I'm missing something here let me know.

I think this was everything you addressed.

Regards,
Patrick


Re: [Ping] Port of VTV for Cygwin and MinGW

2014-10-30 Thread Patrick Wollgast
Since I haven't heard back for quite a while, I wanted to ask what the
current stat of the patch is.

Is the patch from the last mail approved (
https://gcc.gnu.org/ml/gcc-patches/2014-10/msg01524.html ), or should
the matters discussed further?

regards,
Patrick


Re: [Ping] Port of VTV for Cygwin and MinGW

2014-10-16 Thread Patrick Wollgast
On 09.10.2014 16:42, Kai Tietz wrote:
 Not approved:
 * gcc/cp/vtable-class-hierarchy.c
 
 Index: gcc/cp/vtable-class-hierarchy.c
 ===
 --- gcc/cp/vtable-class-hierarchy.c(Revision 214408)
 +++ gcc/cp/vtable-class-hierarchy.c(Arbeitskopie)
 @@ -1182,7 +1182,7 @@ vtv_generate_init_routine (void)
TREE_STATIC (vtv_fndecl) = 1;
TREE_USED (vtv_fndecl) = 1;
DECL_PRESERVE_P (vtv_fndecl) = 1;
 -  if (flag_vtable_verify == VTV_PREINIT_PRIORITY)
 +  if (flag_vtable_verify == VTV_PREINIT_PRIORITY  !TARGET_PECOFF)
 
 You need to check that TARGET_PECOFF is defined.  Otherwise you break
 compilation for none i386 targets.
 
  DECL_STATIC_CONSTRUCTOR (vtv_fndecl) = 0;
 
gimplify_function_tree (vtv_fndecl);
 @@ -1190,7 +1190,7 @@ vtv_generate_init_routine (void)
 
cgraph_process_new_functions ();
 
 -  if (flag_vtable_verify == VTV_PREINIT_PRIORITY)
 +  if (flag_vtable_verify == VTV_PREINIT_PRIORITY  !TARGET_PECOFF)
 
 See above. Likewise
  assemble_vtv_preinit_initializer (vtv_fndecl);
 
  }
 
 
 * gcc/varasm.c
 
 Index: gcc/varasm.c
 ===
 --- gcc/varasm.c(Revision 214408)
 +++ gcc/varasm.c(Arbeitskopie)
 @@ -2165,6 +2165,33 @@ assemble_variable (tree decl, int top_le
   DECL_NAME (decl));
in_section = sect;
  #else
 +  /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
 + Therefore the following check is used.
 + In case a the target is PE or COFF a comdat group section
 + is created, e.g. .vtable_map_vars$foo. The linker places
 + everything in .vtable_map_vars at the end.
 +
 + A fix could be made in
 + gcc/config/i386/winnt.c: i386_pe_unique_section. */
 +  if (TARGET_PECOFF)
 
 You need to test, if TARGET_PECOFF is defined!
 
 +  {
 +char *name;
 +
 +if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
 +  name = ACONCAT ((sect-named.name, $,
 +   IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
 +else
 +  name = ACONCAT ((sect-named.name, $,
 +   IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
 +   NULL));
 +
 +targetm.asm_out.named_section (name,
 + sect-named.common.flags
 +   | SECTION_LINKONCE,
 
 Here it seems to me that you have some whitespace issues,
 
 +   DECL_NAME (decl));
 +in_section = sect;
 +}
 +else
switch_to_section (sect);
  #endif
 

This has been changed in the attached patch.

 
 * libgcc/Makefile.in
 
 Looks ok to me.
 
 * libgcc/config.host
 
 Looks fine to me, too.
 
 * libiberty/obstack.c
 
 Why you use instead of C-runtime exit/abort-functions the
 platform-functions to terminate the process.  This looks to me like
 useless change.  For cygwin this might be even wrong in some aspects.
 What is the reasoning for this change?
 

I haven't encountered crashes in obstack.c itself, but there were
problems in vtv_rts.cc with abort() on MinGW 32bit. The following stack
traces were taken at MinGW 32bit. Most of the time the process had to be
stopped in the process manager because a wrong process handle was passed
to NtTerminateProcess. This was tested and occurred on Windows 7 64bit
and Windows 8.1 64bit. To be sure to avoid this issue the calls have
also been exchanged in obstack.c.

With abort(), correct process handle, postmortem debugger triggert.

 0003 0028fe98 ntdll!NtTerminateProcess+0xc
0003 77e8f3b0  ntdll!RtlExitUserProcess+0x6d
0003 0028f934 74f85472 KERNEL32!ExitProcessImplementation+0x12
0003 11e9bfd9 00409000 msvcrt!exit+0x32
0003 0001  msvcrt!flushall+0x2e9
0003 00010001 0065 msvcrt!exit+0x11
6efcf294 0080 0028ffcc msvcrt!abort+0xf3
00560f70 000d 0001 libvtv_0!Z14__fortify_failPKc+0x18
7ffde000 0028ffdc 77568f8b test_std+0x13de
7ffde000 138a1dee  KERNEL32!BaseThreadInitThunk+0xe
 7755dad3  ntdll!__RtlUserThreadStart+0x20
004014e0 7ffde000  ntdll!_RtlUserThreadStart+0x1b

With abort(), wrong process handle, NtTerminateProcess returns instead
of ending the own process. This case happens most of the time.

 0003 0028fe98 ntdll!NtTerminateProcess+0x5
0003 77e8f3b0  ntdll!RtlExitUserProcess+0x35
0003 0028f934 74f85472 KERNEL32!ExitProcessImplementation+0x12
0003 9f9f5ea3 00409000 msvcrt!exit+0x32
0003 0001  msvcrt!flushall+0x2e9
0003 00010001 0040 msvcrt!exit+0x11
6efcf294 0080 0028ffcc msvcrt!abort+0xf3
00701060 001e 0001 libvtv_0!Z14__fortify_failPKc+0x18
7ffde000 0028ffdc 77568f8b image0040+0x13de
7ffde000 9defd39c  

Re: [Ping] Port of VTV for Cygwin and MinGW

2014-10-09 Thread Patrick Wollgast
On 27.09.2014 12:50, Kai Tietz wrote:
 Hi Patrick,
 
 the mingw/cygwin part your patch looks fine to me.  Nevertheless I
 have one question regarding to you.  Do you have FSF papers for gcc
 already?  As I asked an overseer and he didn't found you on the list.
 
 Regards,
 Kai
 

The papers FSF have been taken care of, and the signed papers have been
exchanged.


A short recap:

Mail with the latest patch and changelog:
https://gcc.gnu.org/ml/gcc-patches/2014-09/msg02200.html

Approved:
* gcc/config/i386/*
* libstdc++-v3/*
* libvtv/*

Not approved:
* gcc/cp/vtable-class-hierarchy.c
* gcc/varasm.c
* libgcc/Makefile.in
* libgcc/config.host
* libiberty/obstack.c

Regards,
Patrick


Re: [Ping] Port of VTV for Cygwin and MinGW

2014-09-24 Thread Patrick Wollgast
On 23.09.2014 12:22, Jonathan Wakely wrote:
 On 19/09/14 00:23 +0200, Patrick Wollgast wrote:
 Index: libstdc++-v3/acinclude.m4
 ===
 --- libstdc++-v3/acinclude.m4(Revision 214408)
 +++ libstdc++-v3/acinclude.m4(Arbeitskopie)
 @@ -2321,7 +2321,17 @@ AC_DEFUN([GLIBCXX_ENABLE_VTABLE_VERIFY],
   AC_MSG_RESULT([$enable_vtable_verify])

   if test $enable_vtable_verify = yes; then
 +case ${target_os} in
 +  cygwin*|mingw32*)
 +VTV_CXXFLAGS=-fvtable-verify=std
 -Wl,-lvtv,-u_vtable_map_vars_start,-u_vtable_map_vars_end
 +vtv_cygmin=yes
 +;;
 +  *)
 VTV_CXXFLAGS=-fvtable-verify=std
 -Wl,-u_vtable_map_vars_start,-u_vtable_map_vars_end
 +vtv_cygmin=no
 +;;
 +esac
 +AM_CONDITIONAL(VTV_CYGMIN, test $vtv_cygmin = yes)
 VTV_PCH_CXXFLAGS=-fvtable-verify=std
 VTV_CXXLINKFLAGS=-L${toplevel_builddir}/libvtv/.libs -Wl,--rpath
 -Wl,${toplevel_builddir}/libvtv/.libs   
   else
 
 The lines between case and esac should be indented by four more
 spaces.
 
 Apart from that the libstdc++ changes are OK and I'll approve them.
 

I've changed the indentation at the newly attached patch.


On 23.09.2014 08:16, Caroline Tice wrote:
 Ok, your patch looks OK to me, but I can only approve the libvtv file
 changes.  The changes in the other files also seem ok to me, but
 someone else will have to approve the modifications in them:

 gcc/config/i386/cygwin.h
 gcc/config/i386/mingw-w64.h
 gcc/config/i386/mingw32.h
 gcc/cp/vtable-class-hierarchy.c
 gcc/varasm.c

 libgcc/Makefile.in
 libgcc/config.host

 libiberty/obstack.c

 libstdc++-v3/acinclude.m4
 libstdc++-v3/libsupc++/Makefile.am
 libstdc++-v3/libsupc++/vtv_stubs.cc

I've added Ian Lance Taylor to cc of this mail.

Thank you both for the reviews!

regards
Patrick
Index: gcc/config/i386/cygwin.h
===
--- gcc/config/i386/cygwin.h	(Revision 214408)
+++ gcc/config/i386/cygwin.h	(Arbeitskopie)
@@ -41,12 +41,18 @@ along with GCC; see the file COPYING3.
 #define STARTFILE_SPEC \
   %{!shared: %{!mdll: crt0%O%s \
   %{pg:gcrt0%O%s}}}\
-  %{shared:crtbeginS.o%s;:crtbegin.o%s}
+  %{shared:crtbeginS.o%s;:crtbegin.o%s} \
+  %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_start.o%s; \
+fvtable-verify=std:vtv_start.o%s}
 
 #undef ENDFILE_SPEC
 #define ENDFILE_SPEC \
   %{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s}\
%{!shared:%:if-exists(default-manifest.o%s)}\
+   %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_end.o%s; \
+fvtable-verify=std:vtv_end.o%s} \
crtend.o%s
 
 /* Normally, -lgcc is not needed since everything in it is in the DLL, but we
@@ -81,6 +87,8 @@ along with GCC; see the file COPYING3.
   %{pthread: } \
   -lcygwin \
   %{mwindows:-lgdi32 -lcomdlg32} \
+  %{fvtable-verify=preinit:-lvtv -lpsapi; \
+fvtable-verify=std:-lvtv -lpsapi} \
   -ladvapi32 -lshell32 -luser32 -lkernel32
 
 /* To implement C++ function replacement we always wrap the cxx
Index: gcc/config/i386/mingw-w64.h
===
--- gcc/config/i386/mingw-w64.h	(Revision 214408)
+++ gcc/config/i386/mingw-w64.h	(Arbeitskopie)
@@ -32,7 +32,10 @@ along with GCC; see the file COPYING3.
   %{!shared:%{!mdll:%{!municode:crt2%O%s}}} \
   %{!shared:%{!mdll:%{municode:crt2u%O%s}}} \
   %{pg:gcrt2%O%s} \
-  crtbegin.o%s
+  crtbegin.o%s \
+  %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_start.o%s; \
+fvtable-verify=std:vtv_start.o%s}
 
 /* Enable multilib.  */
 
@@ -43,6 +46,8 @@ along with GCC; see the file COPYING3.
 #define LIB_SPEC %{pg:-lgmon} %{ SPEC_PTHREAD1 :-lpthread}  \
 		 %{ SPEC_PTHREAD2 : }  \
 		 %{mwindows:-lgdi32 -lcomdlg32}  \
+ %{fvtable-verify=preinit:-lvtv -lpsapi; \
+fvtable-verify=std:-lvtv -lpsapi}  \
 		 -ladvapi32 -lshell32 -luser32 -lkernel32
 
 #undef SPEC_32
Index: gcc/config/i386/mingw32.h
===
--- gcc/config/i386/mingw32.h	(Revision 214408)
+++ gcc/config/i386/mingw32.h	(Arbeitskopie)
@@ -91,6 +91,8 @@ along with GCC; see the file COPYING3.
 #define LIB_SPEC %{pg:-lgmon} %{ SPEC_PTHREAD1 :-lpthread}  \
 		 %{ SPEC_PTHREAD2 : }  \
 		 %{mwindows:-lgdi32 -lcomdlg32}  \
+ %{fvtable-verify=preinit:-lvtv -lpsapi; \
+fvtable-verify=std:-lvtv -lpsapi}  \
  -ladvapi32 -lshell32 -luser32 -lkernel32
 
 /* Weak symbols do not get resolved if using a Windows dll import lib.
@@ -143,12 +145,18 @@ along with GCC; see the file COPYING3.
 #undef STARTFILE_SPEC
 #define STARTFILE_SPEC %{shared|mdll:dllcrt2%O%s} \
   %{!shared:%{!mdll:crt2%O%s}} %{pg:gcrt2%O%s} \
-  crtbegin.o%s
+  crtbegin.o%s \
+  %{fvtable-verify=none:%s; \
+fvtable-verify=preinit:vtv_start.o%s; \
+fvtable-verify=std:vtv_start.o%s}
 
 #undef ENDFILE_SPEC
 #define ENDFILE_SPEC \
   %{Ofast|ffast-math|funsafe-math

Re: [Ping] Port of VTV for Cygwin and MinGW

2014-09-18 Thread Patrick Wollgast
Added Benjamin De Kosnik as a c++ runtime libs maintainer and Kai Tietz
as Windows/Cygwin/MinGW maintainer.

 In changes to gcc/config/i386/cygwin.h  mingw-w64.h and mingw32.h, you 
 forgot to handle the fvtable-verify=preinit options.  
 fvtable-veriy=preinit should cause vtv_start_preinit.o to be added to the 
 STARTFILE_SPEC and vtv_end_preinit.o to be added to the ENDFILE_SPEC (as in  
 gcc/config/gnu-user.h).  I expect you will also need to add it to your 
 LIB_SPEC definitions in those config files.


Like discussed via email I set preinit=std. This required additional
changes in gcc/cp/vtable-class-hierarchy.c.

 in libgcc/config.host, the indentation looks wrong on the line 660 (where 
 you add the extra parts for vtable verification for the case 
 i[34567]86-*-mingw*). It also looks wrong on line 709 (again, adding extra 
 parts, for case x86_64-*-mingw*).  The rest of the changes in that file 
 look ok, but someone else will need to approve them.


Indentation fixed.

 The changes in libgcc/Makefile.in and gcc/varasm.c look ok to me, but 
 someone will will have to approve them since I don't have authority to 
 approve changes there.

 in libstdc++-v3/libsupc++:

 Your change in Makefile.am looks ok to me; your changes in vtv_stubs.cc look 
 ok, except that you appear to have messed up the indentations in the 
 function headers (both for the declarations and the actual functions).  Also 
 there is a typo in your comment:  'build' should be 'built'.  But 
 content-wise, the change looks fine.  Again, someone else will have to 
 actually approve these changes since I do not have that authority.


Typo and indentation fixed.

 in libstdc++-v3/src/Makefile.am also looks ok to me; someone else will have 
 to give final approval.


 in libvtv/Makefile.am, you need to fix the indentation at line 64 
 (vtv_stubs.cc):

 vtv_stubs_sources = \
 vtv_start.c \
 vtv_stubs.cc \
 vtv_end.c


Indentation fixed.

 the rest of the changes in that file look good.

 Why did you make a copy of obstack.c in libvtv rather than using the one in 
 libiberty?  It would be better not to make a second copy of the source file 
 if that can be avoided...


I removed obstack.c from libvtv and added the changes from
libvtv/obstack.c to libiberty/obstack.c with #ifdefs.

 in libvtv/vtv_malloc.cc:

 lines 207-213:  Fix the indentation on the second line of call to 
 VirtualAlloc.
 #if defined (__CYGWIN__) || defined (__MINGW32__)
   if ((allocated = VirtualAlloc(NULL, size,  MEM_RESERVE|MEM_COMMIT,
 PAGE_READWRITE)) == 0)
 #else
   if ((allocated = mmap (NULL, size, PROT_READ | PROT_WRITE,
  MAP_PRIVATE | MAP_ANONYMOUS,  -1, 0)) == 0)
 #endif

 Remove extra blank line at line 216.


Line removed and indentation fixed.

 in libvtv/vtv_rts.cc:

 Your version of the function read_section_offset_and_length has several 
 lines that exceed the 80 character limit; please fix that.  Your function 
 iterate_modules  also has one or two lines that are too long, and it needs a 
 function comment.


Character per line are now correct and comment added.

 in libvtv/vtv_utils.cc:

 In the function __vtv_open_log, you seem to have the following code twice:

 #ifdef __MINGW32__
   mkdir (logs_prefix);
 #else
   mkdir (logs_prefix, S_IRWXU);
 #endif

 was there a reason for this, or is this an accident (in which case the 
 second occurrence should be removed)?


Should have been 'log_dir' instead of 'logs_prefix' the 2nd time. Fixed now.

regards
Patrick
* 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.


* libiberty/obstack.c: Include windows.h on Cygwin and MinGW.
Exchange abort with TerminateProcess on Cygwin and MinGW.
Exchange exit with ExitProcess on Cygwin and MinGW.


* libstdc++-v3/acinclude.m4: Define VTV_CYGMIN.
* libstdc++-v3/configure: Regenerate.

* 

[Ping] Port of VTV for Cygwin and MinGW

2014-09-10 Thread Patrick Wollgast
Ping for https://gcc.gnu.org/ml/gcc-patches/2014-08/msg02559.html

Also added Caroline Tice, as libvtv maintainer, to cc and attached
virtual_func_test_min_UAF.cpp, which I forgot in the original mail.

Patrick

On 28.08.2014 13:03, Patrick Wollgast wrote:
 This patch contains a port of VTV -fvtable-verify=std for Cygwin and MinGW.
 
 Since weak symbols on Windows and Linux are implemented differently, and
 VTV should have the possibility to be switched on and off, the structure
 of the feature had to be modified.
 On Linux libstdc++ contains the weak stub functions of VTV. For Cygwin
 and MinGW they have been removed, due to the difference of weak symbols.
 On Linux and on Windows libstdc++ itself gets build with
 -fvtable-verify=std. Since libvtv gets build after libstdc++, and
 libstdc++ doesn't contain the stub functions any more, 'undefined
 reference' errors are thrown during linking of libstdc++. To prevent
 these errors during the linking process a libvtv-0.dll gets build from
 the stub functions before libstdc++-6.dll is linked.
 At the end of the build process two VTV dlls have been build. One is
 called libvtv-0.dll, containing the real functions, the other is called
 libvtv_stubs-0.dll, containing the stub functions. Depending on whether
 libvtv-0.dll is first found in the dll search path or
 libvtv_stubs-0.dll, renamed to libvtv-0.dll, the real functions or the
 stub functions are used.
 
 Testing:
 The test builds were configured the following way:
 Linux 64bit (from patched and unpatched trunk):
 /path/to/configure --prefix=/prefix/gcc-vtv-bin-64
 --enable-libstdcxx-threads --enable-vtable-verify=yes
 MinGW 32bit cross compiled:
 /path/to/configure --target=i686-w64-mingw32
 --prefix=/prefix/mingw-vtv-bin-32 --with-gnu-ld --with-gnu-as
 --enable-fully-dynamic-string --disable-multilib
 --enable-libstdcxx-threads --enable-vtable-verify=yes
 MinGW 64bit cross compiled:
 /path/to/configure --target=x86_64-w64-mingw32
 --prefix=/prefix/mingw-vtv-bin-64 --with-gnu-ld --with-gnu-as
 --enable-fully-dynamic-string --disable-multilib
 --enable-libstdcxx-threads --enable-vtable-verify=yes
 Cygwin 64bit:
 /path/to/configure --enable-languages=c,c++ --enable-libstdcxx-threads
 --enable-vtable-verify=yes
 
 At Linux the patched and unpatched version resulted in the same number
 of passed tests with 'make check-target-libvtv'.
 
 Since MinGW was cross compiled the test cases couldn't be built and run
 with 'make check-target-libvtv'. Therefore they were built with the
 attached makefiles and tested afterwards on Windows 7 64bit. Some test
 cases contain Linux specific parts and weren't tested. See the makefiles
 for further information. Additionally virtual_func_test_min_UAF.cpp was
 also built and tested. All built tests passed.
 
 Cygwin was just tested on gcc 4.9.0, because the current trunk isn't
 building for me. Even the clean trunk without the patch attached to this
 mail. On Cygwin with gcc 4.9.0 VTV worked.
 
 Besides the test cases Botan was also built and tested (gcc 4.9.0) with
 MinGW 32bit and VTV.
 
 regards
 

-- 
Beste Grüße,
Patrick
#includeiostream
#includestdio.h

/*
 * Demonstrates an use after free c++ internally by deleting an object
 * and resetting its vtable pointing to legitimate functions
 */

// set pointer size and integer size dependent on architechture
#if __x86_64__
/* 64-bit */
const char* arch = x86_64;
int ptrSize = 8;
typedef long long myInt;// 8 byte
#else
/* 32-bit */
const char* arch = x86;
int ptrSize = 4;
typedef int myInt;  // 4 byte
#endif
//

using namespace std;


// BASE CLASS
class Addition{
protected:
// member variables
int s1,s2;
public:
// virtual member function will be implemented in derived class
virtual int add(int a,int b){
};
int i;
};
//

// inherit new class
class Add: public Addition{
public:
// constructor:
/*
Add(int a, int b){
s1 = a;
s2 = b;
};
*/
void setvals(int a, int b){
s1 = a;
s2 = b;
}
// implement virtual function
virtual int TRIGGER(){
couts1 + s2\n;
};
int j;
};
//

// use this function in vtable which will be injected
void inject(){
printf(%s\n,I've been executed);
}

int main(){

printf(We're on a %s architecture\n\n,arch);

// create object of inherited class
//Add* A = new Add(4,5);
Add* A = new Add;   //heap allocation happens here
A-setvals(4,5);


// add attributes
A-i = 6;
A-j = 7;

// get vtable address
myInt vtablePTR = NULL;
vtablePTR = *((myInt*)A);

printf(vtable: %.16x\n, vtablePTR);
//function 1
printf(%.16x : %.16x\n, vtablePTR, *((myInt*)vtablePTR));
//function 2
printf(%.16x : %.16x\n, vtablePTR + ptrSize, *((myInt*)(vtablePTR + ptrSize)));

// call instantiated virtual function (legitimate

Port of VTV for Cygwin and MinGW

2014-08-28 Thread Patrick Wollgast
This patch contains a port of VTV -fvtable-verify=std for Cygwin and MinGW.

Since weak symbols on Windows and Linux are implemented differently, and
VTV should have the possibility to be switched on and off, the structure
of the feature had to be modified.
On Linux libstdc++ contains the weak stub functions of VTV. For Cygwin
and MinGW they have been removed, due to the difference of weak symbols.
On Linux and on Windows libstdc++ itself gets build with
-fvtable-verify=std. Since libvtv gets build after libstdc++, and
libstdc++ doesn't contain the stub functions any more, 'undefined
reference' errors are thrown during linking of libstdc++. To prevent
these errors during the linking process a libvtv-0.dll gets build from
the stub functions before libstdc++-6.dll is linked.
At the end of the build process two VTV dlls have been build. One is
called libvtv-0.dll, containing the real functions, the other is called
libvtv_stubs-0.dll, containing the stub functions. Depending on whether
libvtv-0.dll is first found in the dll search path or
libvtv_stubs-0.dll, renamed to libvtv-0.dll, the real functions or the
stub functions are used.

Testing:
The test builds were configured the following way:
Linux 64bit (from patched and unpatched trunk):
/path/to/configure --prefix=/prefix/gcc-vtv-bin-64
--enable-libstdcxx-threads --enable-vtable-verify=yes
MinGW 32bit cross compiled:
/path/to/configure --target=i686-w64-mingw32
--prefix=/prefix/mingw-vtv-bin-32 --with-gnu-ld --with-gnu-as
--enable-fully-dynamic-string --disable-multilib
--enable-libstdcxx-threads --enable-vtable-verify=yes
MinGW 64bit cross compiled:
/path/to/configure --target=x86_64-w64-mingw32
--prefix=/prefix/mingw-vtv-bin-64 --with-gnu-ld --with-gnu-as
--enable-fully-dynamic-string --disable-multilib
--enable-libstdcxx-threads --enable-vtable-verify=yes
Cygwin 64bit:
/path/to/configure --enable-languages=c,c++ --enable-libstdcxx-threads
--enable-vtable-verify=yes

At Linux the patched and unpatched version resulted in the same number
of passed tests with 'make check-target-libvtv'.

Since MinGW was cross compiled the test cases couldn't be built and run
with 'make check-target-libvtv'. Therefore they were built with the
attached makefiles and tested afterwards on Windows 7 64bit. Some test
cases contain Linux specific parts and weren't tested. See the makefiles
for further information. Additionally virtual_func_test_min_UAF.cpp was
also built and tested. All built tests passed.

Cygwin was just tested on gcc 4.9.0, because the current trunk isn't
building for me. Even the clean trunk without the patch attached to this
mail. On Cygwin with gcc 4.9.0 VTV worked.

Besides the test cases Botan was also built and tested (gcc 4.9.0) with
MinGW 32bit and VTV.

regards
* config/i386/cygwin.h (STARTFILE_SPEC): Add vtv_start.o,
if -fvtable-verify=std is used.
* config/i386/mingw-w64.h (STARTFILE_SPEC): Likewise.
* config/i386/mingw32.h (STARTFILE_SPEC): Likewise.
* config/i386/cygwin.h (ENDFILE_SPEC): Add vtv_end.o,
if -fvtable-verify=std is used.
* config/i386/mingw32.h (ENDFILE_SPEC): Likewise.
* config/i386/cygwin.h (LIB_SPEC): Pass -lvtv and -lpsapi,
if -fvtable-verify=std is used.
* config/i386/mingw-w64.h (LIB_SPEC): Likewise.
* config/i386/mingw32.h (LIB_SPEC): Likewise.


* 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/obstack.c : New file.
* libvtv/vtv_fail.cc :