Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ramosian-glider opened this issue Aug 31, 2015 · 6 comments
Closed

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

ramosian-glider opened this issue Aug 31, 2015 · 6 comments

Comments

@ramosian-glider
Copy link
Member

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
Copy link
Member Author

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

  • Labels added: Priority-High
  • Labels removed: Priority-Medium

@ramosian-glider
Copy link
Member Author

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

@ramosian-glider
Copy link
Member Author

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
Copy link
Member Author

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

  • Status changed: Invalid

@ramosian-glider
Copy link
Member Author

OMG

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

@ramosian-glider
Copy link
Member Author

Adding Project:AddressSanitizer as part of GitHub migration.

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

  • Labels added: ProjectAddressSanitizer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant