Jun 26, 2018

[C++][gcc] Useful GCC address sanitizer checks not enabled by default

reference:
https://kristerw.blogspot.com/2018/06/useful-gcc-address-sanitizer-checks-not.html


$ gcc -O -fsanitize=address file.c
$ env ASAN_OPTIONS="detect_stack_use_after_return=1" ./a.out
int *ptr;

__attribute__((noinline))
void foo(void)
{
  int a;
  ptr = &a;
}

int main(void)
{
  foo();
  return *ptr;  // Error
}


Pointer comparison:
$ gcc -fsanitize=address,pointer-compare file.c
$ env ASAN_OPTIONS="detect_invalid_pointer_pairs=1" ./a.out
#include <stdlib.h>

int main(void)
{
  char *p = malloc(42);
  char *q = malloc(42);

  int tmp = p < q;  // Error

  free(p);
  free(q);

  return tmp;
}


Pointer subtraction:
$ gcc -O -fsanitize=address,pointer-subtract file.c
$ env ASAN_OPTIONS="detect_invalid_pointer_pairs=1" ./a.out
#include <stdlib.h>

int main(void)
{
  char *p = malloc(42);
  char *q = malloc(42);

  int tmp = p - q;  // Error

  free(p);
  free(q);

  return tmp;
}


vector boundary check:
$ g++ -O -fsanitize=address -D_GLIBCXX_SANITIZE_VECTOR file.cpp
#include <vector>

int main()
{
  std::vector<int> v(2);
  int* p = v.data();
  v.pop_back();
  return p[1];  // Error
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.