[PATCH][Revisedx5] Enable libsanitizer on darwin

2012-11-23 Thread Jack Howarth
   The attached patch imports the missing mach_override/mach_override.h and
mach_override/mach_override.c files from llvm.org's compiler-rt at 

r168032 | glider | 2012-11-15 03:32:16 -0500 (Thu, 15 Nov 2012) | 3 lines

[ASan] Add the lea $imm(%rip),%rax instruction to mach_override.c
The need for this has been reported by Jack Howarth (howa...@bromo.med.uc.edu) 
who's porting ASan-Darwin to GCC

The patch adds darwin to the supported target list in configure.tgt and 
defines USING_MACH_OVERRIDE for darwin in configure.ac. The definition of 
USING_MACH_OVERRIDE is used in Makefile.am as the test for appending
mach_override/mach_override.c to libinterception_la_SOURCES. 
LINK_COMMAND_SPEC_A 
in gcc/config/darwin.h is modified to add an entry to handle fsanitize=address
so that the required linkages are used for libasan. The static linkage of 
libasan.a
in LINK_COMMAND_SPEC_A is handle separately for -static-libstdc++ (which 
requires
libstdc++.a) and the -static, -static-gcc and -static-gfortran cases. Tested at 
rr193756
on x86_64-apple-darwin12 for both -m32 and -m64 with the both use-after-free.c 
testcase 
and...

 make -k check RUNTESTFLAGS=asan.exp --target_board=unix'{-m32,-m64}'

without regressions.
  Jack
ps This version adds the requested importation of the LICENSE.txt file from 
llvm.org's
compiler-rt in interception/mach_override. It also sets TSAN_SUPPORTED=no in 
configure.tgt
for the 'x86_64-*-darwin* | i?86-*-darwin*' case to avoid building tsan support.
gcc/

2012-11-23  Jack Howarth howa...@bromo.med.uc.edu

* config/darwin.h (LINK_COMMAND_SPEC_A): Deal with -fsanitize=address.

libsanitizer/

2012-11-23  Kostya Serebryany k...@google.com
Jack Howarth howa...@bromo.med.uc.edu

* interception/mach_override/mach_override.c: Migrate from llvm.
* interception/mach_override/mach_override.h: Likewise.
* interception/mach_override/LICENSE.txt: Likewise.
* configure.tgt: Add darwin to supported targets.
* configure.ac: Define USING_MACH_OVERRIDE when on darwin.
* interception/Makefile.am: Compile mach_override.c when
USING_MACH_OVERRIDE defined.
* configure: Regenerated.
* interception/Makefile.in: Likewise.

--- /dev/null   2012-11-16 10:24:58.0 -0500
+++ libsanitizer/interception/mach_override/mach_override.c 2012-11-16 
10:26:42.0 -0500
@@ -0,0 +1,970 @@
+/***
+   mach_override.c
+   Copyright (c) 2003-2009 Jonathan 'Wolf' Rentzsch: 
http://rentzsch.com
+   Some rights reserved: 
http://opensource.org/licenses/mit-license.php
+
+   
***/
+#ifdef __APPLE__
+
+#include mach_override.h
+
+#include mach-o/dyld.h
+#include mach/mach_host.h
+#include mach/mach_init.h
+#include mach/vm_map.h
+#include sys/mman.h
+
+#include CoreServices/CoreServices.h
+
+//#define DEBUG_DISASM 1
+#undef DEBUG_DISASM
+
+/**
+*  
+*  Constants
+*  
+**/
+#pragma mark   -
+#pragma mark   (Constants)
+
+#if defined(__ppc__) || defined(__POWERPC__)
+
+static
+long kIslandTemplate[] = {
+   0x9001FFFC, //  stw r0,-4(SP)
+   0x3C00DEAD, //  lis r0,0xDEAD
+   0x6000BEEF, //  ori r0,r0,0xBEEF
+   0x7C0903A6, //  mtctr   r0
+   0x8001FFFC, //  lwz r0,-4(SP)
+   0x6000, //  nop ; optionally replaced
+   0x4E800420  //  bctr
+};
+
+#define kAddressHi 3
+#define kAddressLo 5
+#define kInstructionHi 10
+#define kInstructionLo 11
+
+#elif defined(__i386__) 
+
+#define kOriginalInstructionsSize 16
+
+static
+unsigned char kIslandTemplate[] = {
+   // kOriginalInstructionsSize nop instructions so that we 
+   // should have enough space to host original instructions 
+   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 
+   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
+   // Now the real jump instruction
+   0xE9, 0xEF, 0xBE, 0xAD, 0xDE
+};
+
+#define kInstructions  0
+#define kJumpAddresskInstructions + kOriginalInstructionsSize + 1
+#elif defined(__x86_64__)
+
+#define kOriginalInstructionsSize 32
+
+#define kJumpAddresskOriginalInstructionsSize + 6
+
+static
+unsigned char kIslandTemplate[] = {
+   // kOriginalInstructionsSize nop instructions so that we 
+   // should have enough space to host original instructions 
+   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 
+   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
+   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 
+   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
+   // Now the real jump instruction
+   0xFF, 0x25, 0x00, 0x00, 0x00, 0x00,
+

Re: [PATCH][Revisedx5] Enable libsanitizer on darwin

2012-11-23 Thread Alexander Potapenko
The mach_override path looks good to me. I don't have enough knowledge
of GCC buildsystem yet to review the rest.

On Fri, Nov 23, 2012 at 8:17 PM, Jack Howarth howa...@bromo.med.uc.edu wrote:
The attached patch imports the missing mach_override/mach_override.h and
 mach_override/mach_override.c files from llvm.org's compiler-rt at

 r168032 | glider | 2012-11-15 03:32:16 -0500 (Thu, 15 Nov 2012) | 3 lines

 [ASan] Add the lea $imm(%rip),%rax instruction to mach_override.c
 The need for this has been reported by Jack Howarth 
 (howa...@bromo.med.uc.edu) who's porting ASan-Darwin to GCC

 The patch adds darwin to the supported target list in configure.tgt and
 defines USING_MACH_OVERRIDE for darwin in configure.ac. The definition of
 USING_MACH_OVERRIDE is used in Makefile.am as the test for appending
 mach_override/mach_override.c to libinterception_la_SOURCES. 
 LINK_COMMAND_SPEC_A
 in gcc/config/darwin.h is modified to add an entry to handle fsanitize=address
 so that the required linkages are used for libasan. The static linkage of 
 libasan.a
 in LINK_COMMAND_SPEC_A is handle separately for -static-libstdc++ (which 
 requires
 libstdc++.a) and the -static, -static-gcc and -static-gfortran cases. Tested 
 at rr193756
 on x86_64-apple-darwin12 for both -m32 and -m64 with the both 
 use-after-free.c testcase
 and...

  make -k check RUNTESTFLAGS=asan.exp --target_board=unix'{-m32,-m64}'

 without regressions.
   Jack
 ps This version adds the requested importation of the LICENSE.txt file from 
 llvm.org's
 compiler-rt in interception/mach_override. It also sets TSAN_SUPPORTED=no in 
 configure.tgt
 for the 'x86_64-*-darwin* | i?86-*-darwin*' case to avoid building tsan 
 support.



-- 
Alexander Potapenko
Software Engineer
Google Moscow


Re: [PATCH][Revisedx5] Enable libsanitizer on darwin

2012-11-23 Thread Konstantin Serebryany
Looks great.
(I am not an expert in the build system either, but the changes look trivial).

Thanks!

--kcc

On Fri, Nov 23, 2012 at 8:29 PM, Alexander Potapenko gli...@google.com wrote:
 The mach_override path looks good to me. I don't have enough knowledge
 of GCC buildsystem yet to review the rest.

 On Fri, Nov 23, 2012 at 8:17 PM, Jack Howarth howa...@bromo.med.uc.edu 
 wrote:
The attached patch imports the missing mach_override/mach_override.h and
 mach_override/mach_override.c files from llvm.org's compiler-rt at

 r168032 | glider | 2012-11-15 03:32:16 -0500 (Thu, 15 Nov 2012) | 3 lines

 [ASan] Add the lea $imm(%rip),%rax instruction to mach_override.c
 The need for this has been reported by Jack Howarth 
 (howa...@bromo.med.uc.edu) who's porting ASan-Darwin to GCC

 The patch adds darwin to the supported target list in configure.tgt and
 defines USING_MACH_OVERRIDE for darwin in configure.ac. The definition of
 USING_MACH_OVERRIDE is used in Makefile.am as the test for appending
 mach_override/mach_override.c to libinterception_la_SOURCES. 
 LINK_COMMAND_SPEC_A
 in gcc/config/darwin.h is modified to add an entry to handle 
 fsanitize=address
 so that the required linkages are used for libasan. The static linkage of 
 libasan.a
 in LINK_COMMAND_SPEC_A is handle separately for -static-libstdc++ (which 
 requires
 libstdc++.a) and the -static, -static-gcc and -static-gfortran cases. Tested 
 at rr193756
 on x86_64-apple-darwin12 for both -m32 and -m64 with the both 
 use-after-free.c testcase
 and...

  make -k check RUNTESTFLAGS=asan.exp --target_board=unix'{-m32,-m64}'

 without regressions.
   Jack
 ps This version adds the requested importation of the LICENSE.txt file from 
 llvm.org's
 compiler-rt in interception/mach_override. It also sets TSAN_SUPPORTED=no in 
 configure.tgt
 for the 'x86_64-*-darwin* | i?86-*-darwin*' case to avoid building tsan 
 support.



 --
 Alexander Potapenko
 Software Engineer
 Google Moscow