| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/environment.h" | 9 #include "base/environment.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/win/scoped_handle.h" | 14 #include "base/win/scoped_handle.h" |
| 15 #include "base/win/windows_version.h" | 15 #include "base/win/windows_version.h" |
| 16 #include "sandbox/win/tests/common/controller.h" | 16 #include "sandbox/win/tests/common/controller.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace sandbox { | 19 namespace sandbox { |
| 20 | 20 |
| 21 class AddressSanitizerTests : public ::testing::Test { | 21 class AddressSanitizerTests : public ::testing::Test { |
| 22 public: | 22 public: |
| 23 void SetUp() override { | 23 void SetUp() override { |
| 24 env_.reset(base::Environment::Create()); | 24 env_ = base::Environment::Create(); |
| 25 had_asan_options_ = env_->GetVar("ASAN_OPTIONS", &old_asan_options_); | 25 had_asan_options_ = env_->GetVar("ASAN_OPTIONS", &old_asan_options_); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void TearDown() override { | 28 void TearDown() override { |
| 29 if (had_asan_options_) | 29 if (had_asan_options_) |
| 30 ASSERT_TRUE(env_->SetVar("ASAN_OPTIONS", old_asan_options_)); | 30 ASSERT_TRUE(env_->SetVar("ASAN_OPTIONS", old_asan_options_)); |
| 31 else | 31 else |
| 32 env_->UnSetVar("ASAN_OPTIONS"); | 32 env_->UnSetVar("ASAN_OPTIONS"); |
| 33 } | 33 } |
| 34 | 34 |
| 35 protected: | 35 protected: |
| 36 std::unique_ptr<base::Environment> env_; | 36 std::unique_ptr<base::Environment> env_; |
| 37 bool had_asan_options_; | 37 bool had_asan_options_; |
| 38 std::string old_asan_options_; | 38 std::string old_asan_options_; |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 SBOX_TESTS_COMMAND int AddressSanitizerTests_Report(int argc, wchar_t** argv) { | 41 SBOX_TESTS_COMMAND int AddressSanitizerTests_Report(int argc, wchar_t** argv) { |
| 42 // AddressSanitizer should detect an out of bounds write (heap buffer | 42 // AddressSanitizer should detect an out of bounds write (heap buffer |
| 43 // overflow) in this code. | 43 // overflow) in this code. |
| 44 volatile int idx = 42; | 44 volatile int idx = 42; |
| 45 int *volatile blah = new int[42]; | 45 int* volatile blah = new int[42]; |
| 46 blah[idx] = 42; | 46 blah[idx] = 42; |
| 47 delete [] blah; | 47 delete [] blah; |
| 48 return SBOX_TEST_FAILED; | 48 return SBOX_TEST_FAILED; |
| 49 } | 49 } |
| 50 | 50 |
| 51 TEST_F(AddressSanitizerTests, TestAddressSanitizer) { | 51 TEST_F(AddressSanitizerTests, TestAddressSanitizer) { |
| 52 // This test is only supposed to work when using AddressSanitizer. | 52 // This test is only supposed to work when using AddressSanitizer. |
| 53 // However, ASan/Win is not on the CQ yet, so compiler breakages may get into | 53 // However, ASan/Win is not on the CQ yet, so compiler breakages may get into |
| 54 // the code unnoticed. To avoid that, we compile this test in all Windows | 54 // the code unnoticed. To avoid that, we compile this test in all Windows |
| 55 // builds, but only run the AddressSanitizer-specific part of the test when | 55 // builds, but only run the AddressSanitizer-specific part of the test when |
| (...skipping 16 matching lines...) Expand all Loading... |
| 72 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, | 72 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, |
| 73 &attrs, OPEN_EXISTING, 0, NULL)); | 73 &attrs, OPEN_EXISTING, 0, NULL)); |
| 74 EXPECT_TRUE(tmp_handle.IsValid()); | 74 EXPECT_TRUE(tmp_handle.IsValid()); |
| 75 | 75 |
| 76 TestRunner runner; | 76 TestRunner runner; |
| 77 ASSERT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStderrHandle(tmp_handle.Get())); | 77 ASSERT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStderrHandle(tmp_handle.Get())); |
| 78 | 78 |
| 79 base::FilePath exe; | 79 base::FilePath exe; |
| 80 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe)); | 80 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe)); |
| 81 base::FilePath pdb_path = exe.DirName().Append(L"*.pdb"); | 81 base::FilePath pdb_path = exe.DirName().Append(L"*.pdb"); |
| 82 ASSERT_TRUE(runner.AddFsRule(sandbox::TargetPolicy::FILES_ALLOW_READONLY, | 82 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_READONLY, |
| 83 pdb_path.value().c_str())); | 83 pdb_path.value().c_str())); |
| 84 | 84 |
| 85 env_->SetVar("ASAN_OPTIONS", "exitcode=123"); | 85 env_->SetVar("ASAN_OPTIONS", "exitcode=123"); |
| 86 if (asan_build) { | 86 if (asan_build) { |
| 87 int result = runner.RunTest(L"AddressSanitizerTests_Report"); | 87 int result = runner.RunTest(L"AddressSanitizerTests_Report"); |
| 88 EXPECT_EQ(123, result); | 88 EXPECT_EQ(123, result); |
| 89 | 89 |
| 90 std::string data; | 90 std::string data; |
| 91 ASSERT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data)); | 91 ASSERT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data)); |
| 92 // Redirection uses a feature that was added in Windows Vista. | 92 // Redirection uses a feature that was added in Windows Vista. |
| 93 ASSERT_TRUE( | 93 ASSERT_TRUE( |
| 94 strstr(data.c_str(), "ERROR: AddressSanitizer: heap-buffer-overflow")) | 94 strstr(data.c_str(), "ERROR: AddressSanitizer: heap-buffer-overflow")) |
| 95 << "There doesn't seem to be an ASan report:\n" << data; | 95 << "There doesn't seem to be an ASan report:\n" << data; |
| 96 ASSERT_TRUE(strstr(data.c_str(), "AddressSanitizerTests_Report")) | 96 ASSERT_TRUE(strstr(data.c_str(), "AddressSanitizerTests_Report")) |
| 97 << "The ASan report doesn't appear to be symbolized:\n" << data; | 97 << "The ASan report doesn't appear to be symbolized:\n" << data; |
| 98 std::string source_file_basename(__FILE__); | 98 std::string source_file_basename(__FILE__); |
| 99 size_t last_slash = source_file_basename.find_last_of("/\\"); | 99 size_t last_slash = source_file_basename.find_last_of("/\\"); |
| 100 last_slash = last_slash == std::string::npos ? 0 : last_slash + 1; | 100 last_slash = last_slash == std::string::npos ? 0 : last_slash + 1; |
| 101 ASSERT_TRUE(strstr(data.c_str(), &source_file_basename[last_slash])) | 101 ASSERT_TRUE(strstr(data.c_str(), &source_file_basename[last_slash])) |
| 102 << "The stack trace doesn't have a correct filename:\n" << data; | 102 << "The stack trace doesn't have a correct filename:\n" << data; |
| 103 } else { | 103 } else { |
| 104 LOG(WARNING) << "Not an AddressSanitizer build, skipping the run."; | 104 LOG(WARNING) << "Not an AddressSanitizer build, skipping the run."; |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 } | 108 } // namespace sandbox |
| OLD | NEW |