[ASan] turn on leak checking for ASan tests and fix a few discovered leaks

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@190274 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/lit_tests/TestCases/Linux/glob.cc b/lib/asan/lit_tests/TestCases/Linux/glob.cc
index a375f1b..123768b 100644
--- a/lib/asan/lit_tests/TestCases/Linux/glob.cc
+++ b/lib/asan/lit_tests/TestCases/Linux/glob.cc
@@ -22,6 +22,7 @@
   assert(globbuf.gl_pathc == 2);
   printf("%zu\n", strlen(globbuf.gl_pathv[0]));
   printf("%zu\n", strlen(globbuf.gl_pathv[1]));
+  globfree(&globbuf);
   printf("PASS\n");
   // CHECK: PASS
   return 0;
diff --git a/lib/asan/lit_tests/TestCases/Linux/interception_readdir_r_test.cc b/lib/asan/lit_tests/TestCases/Linux/interception_readdir_r_test.cc
index 28c7df4..198e1f3 100644
--- a/lib/asan/lit_tests/TestCases/Linux/interception_readdir_r_test.cc
+++ b/lib/asan/lit_tests/TestCases/Linux/interception_readdir_r_test.cc
@@ -33,6 +33,7 @@
     ++count;
   } while (result != NULL);
   fprintf(stderr, "read %d entries\n", count);
+  closedir(d);
   // CHECK: test1: reading the {{.*}} directory...
   // CHECK-NOT: stack-buffer-overflow
   // CHECK: read {{.*}} entries
@@ -51,6 +52,7 @@
     ++count;
   } while (result64 != NULL);
   fprintf(stderr, "read %d entries\n", count);
+  closedir(d);
   // CHECK: test2: reading the {{.*}} directory...
   // CHECK-NOT: stack-buffer-overflow
   // CHECK: read {{.*}} entries
diff --git a/lib/asan/lit_tests/Unit/lit.site.cfg.in b/lib/asan/lit_tests/Unit/lit.site.cfg.in
index 0db0940..a45870c 100644
--- a/lib/asan/lit_tests/Unit/lit.site.cfg.in
+++ b/lib/asan/lit_tests/Unit/lit.site.cfg.in
@@ -11,3 +11,6 @@
 # it as build directory with ASan unit tests.
 config.test_exec_root = "@ASAN_BINARY_DIR@/tests"
 config.test_source_root = config.test_exec_root
+
+if config.host_os == 'Linux':
+  config.environment['ASAN_OPTIONS'] = 'detect_leaks=1'
diff --git a/lib/asan/lit_tests/lit.cfg b/lib/asan/lit_tests/lit.cfg
index 16e3565..5c129c5 100644
--- a/lib/asan/lit_tests/lit.cfg
+++ b/lib/asan/lit_tests/lit.cfg
@@ -82,6 +82,10 @@
 
 config.available_features.add("asan-" + config.bits + "-bits")
 
+# Turn on leak detection on 64-bit Linux.
+if config.host_os == 'Linux' and config.bits == '64':
+  config.environment['ASAN_OPTIONS'] = 'detect_leaks=1'
+
 # Default test suffixes.
 config.suffixes = ['.c', '.cc', '.cpp']
 
diff --git a/lib/asan/tests/asan_noinst_test.cc b/lib/asan/tests/asan_noinst_test.cc
index 9d8a09c..0ec5b55 100644
--- a/lib/asan/tests/asan_noinst_test.cc
+++ b/lib/asan/tests/asan_noinst_test.cc
@@ -654,6 +654,7 @@
       }
     }
   }
+  free(arr);
 }
 
 TEST(AddressSanitizerInterface, PoisonedRegion) {
diff --git a/lib/asan/tests/asan_str_test.cc b/lib/asan/tests/asan_str_test.cc
index 17bef55..7810112 100644
--- a/lib/asan/tests/asan_str_test.cc
+++ b/lib/asan/tests/asan_str_test.cc
@@ -70,6 +70,7 @@
   memcpy(heap_string, L"Hello, World!", hello_size);
   EXPECT_EQ(hello_len, Ident(wcslen(heap_string)));
   EXPECT_DEATH(Ident(wcslen(heap_string + 14)), RightOOBReadMessage(0));
+  free(heap_string);
 }
 
 #ifndef __APPLE__
diff --git a/lib/lit.common.unit.cfg b/lib/lit.common.unit.cfg
index ccc3ffa..2bd8f37 100644
--- a/lib/lit.common.unit.cfg
+++ b/lib/lit.common.unit.cfg
@@ -15,6 +15,13 @@
 # Setup test suffixes.
 config.suffixes = []
 
+# Tweak PATH to include llvm tools dir.
+llvm_tools_dir = getattr(config, 'llvm_tools_dir', None)
+if (not llvm_tools_dir) or (not os.path.exists(llvm_tools_dir)):
+  lit_config.fatal("Invalid llvm_tools_dir config attribute: %r" % llvm_tools_dir)
+path = os.path.pathsep.join((llvm_tools_dir, config.environment['PATH']))
+config.environment['PATH'] = path
+
 # Propagate the temp directory. Windows requires this because it uses \Windows\
 # if none of these are present.
 if 'TMP' in os.environ:
diff --git a/lib/lit.common.unit.configured.in b/lib/lit.common.unit.configured.in
index 88eeed1..430816b 100644
--- a/lib/lit.common.unit.configured.in
+++ b/lib/lit.common.unit.configured.in
@@ -8,6 +8,7 @@
 config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
 config.compiler_rt_src_root = "@COMPILER_RT_SOURCE_DIR@"
 config.llvm_build_mode = "@LLVM_BUILD_MODE@"
+config.host_os = "@HOST_OS@"
 
 # LLVM tools dir and build mode can be passed in lit parameters,
 # so try to apply substitution.