ramosian-glider / sanitizers

0 stars 0 forks source link

strlen() isn't replaced with the dynamic runtime on OS X #125

Closed ramosian-glider closed 9 years ago

ramosian-glider commented 9 years ago

Originally reported on Google Code with ID 124

$ cat t.c
#include <stdio.h>
#include <string.h>

int main() {
  char tmp[1024];
  sprintf(tmp, "#line %u", __LINE__);
  fprintf(stderr, "tmp: %p\n", tmp);
  fprintf(stderr, "tmp: '%s'\n", tmp);
  int len = strlen(tmp);
  fprintf(stderr, "after strlen call\n");  // to distinguish from strlen in fprintf
  fprintf(stderr, "strlen(tmp): %d\n", len);
  return 0;
}

$ Release+Asserts/bin/clang  t.c -o t -g
$ ./t
tmp: 0x7fff527f38c0
tmp: '#line 6'
after strlen call
strlen(tmp): 7

$ Release+Asserts/bin/clang -faddress-sanitizer t.c -o t -g
$ ./t
$ ./t
tmp: 0x7fff587c6760
tmp: '#line 6'
after strlen call
strlen(tmp): 4

It turns out that the ASan strlen wrapper isn't being called. Moreover, the function
being called returns an incorrect string length.

Reported by ramosian.glider on 2012-10-26 11:58:48

ramosian-glider commented 9 years ago

Reported by ramosian.glider on 2012-10-29 11:44:11

ramosian-glider commented 9 years ago

Reported by ramosian.glider on 2012-10-29 11:50:21

ramosian-glider commented 9 years ago
For clarity:

$ svn diff ../tools/clang/lib/Driver/ToolChains.cpp 
Index: ../tools/clang/lib/Driver/ToolChains.cpp
===================================================================
--- ../tools/clang/lib/Driver/ToolChains.cpp    (revision 166764)
+++ ../tools/clang/lib/Driver/ToolChains.cpp    (working copy)
@@ -366,10 +366,10 @@
     if (Args.hasArg(options::OPT_dynamiclib) ||
         Args.hasArg(options::OPT_bundle)) return;
     if (isTargetIPhoneOS()) {
-      getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
-        << "-faddress-sanitizer";
+      AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx_dynamic.dylib");
     } else {
-      AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
+      AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx_dynamic.dylib");
+      //AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");

       // The ASAN runtime library requires C++ and CoreFoundation.
       AddCXXStdlibLibArgs(Args, CmdArgs);
==============================================
$ Release+Asserts/bin/clang -faddress-sanitizer t.c  -o t
$ ASAN_OPTIONS=verbosity=1 ./t
==55249== Parsed ASAN_OPTIONS: verbosity=1
==55249== exec()-ing the program with
==55249== DYLD_INSERT_LIBRARIES=/Users/glider/src/asan/llvm/build/Release+Asserts/lib/clang/3.2/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
==55249== to enable ASan wrappers.
==55249== Set ASAN_OPTIONS=allow_reexec=0 to disable this.
==55249== Parsed ASAN_OPTIONS: verbosity=1
|| `[0x200000000000, 0x7fffffffffff]` || HighMem    ||
|| `[0x140000000000, 0x1fffffffffff]` || HighShadow ||
|| `[0x120000000000, 0x13ffffffffff]` || ShadowGap  ||
|| `[0x100000000000, 0x11ffffffffff]` || LowShadow  ||
|| `[0x000000000000, 0x0fffffffffff]` || LowMem     ||
MemToShadow(shadow): 0x120000000000 0x123fffffffff 0x128000000000 0x13ffffffffff
red_zone=64
malloc_context_size=30
SHADOW_SCALE: 3
SHADOW_GRANULARITY: 8
SHADOW_OFFSET: 100000000000
==55249== Installed the sigaction for signal 11
==55249== Installed the sigaction for signal 10
==55249== T0: stack [0x7fff5a4a5000,0x7fff5aca5000) size 0x800000; local=0x7fff5aca40c0
==55249== AddressSanitizer Init done
tmp: 0x7fff5aca4740
tmp: '#line 6'
after strlen call
strlen(tmp): 4

And even with the explicit DYLD_INSERT_LIBRARIES:
$ DYLD_INSERT_LIBRARIES=/Users/glider/src/asan/llvm/build/Release+Asserts/lib/clang/3.2/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
./t
tmp: 0x7fff567997a0
tmp: '#line 6'
after strlen call
strlen(tmp): 4

Reported by ramosian.glider on 2012-10-29 16:53:04

ramosian-glider commented 9 years ago
It turns out there's been a temporary file v.cc, which contained the incorrect implementation
of strlen.
It has got linked with the runtime, because it resided in the same directory as other
ASan files.

Reported by ramosian.glider on 2012-11-29 13:43:19

ramosian-glider commented 9 years ago
OMG

Reported by konstantin.s.serebryany on 2012-11-29 13:46:45

ramosian-glider commented 9 years ago
Adding Project:AddressSanitizer as part of GitHub migration.

Reported by ramosian.glider on 2015-07-30 09:13:40