OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stdio.h> | |
6 | |
7 #include "base/environment.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/files/file_util.h" | |
10 #include "base/path_service.h" | |
11 #include "base/win/windows_version.h" | |
12 #include "sandbox/win/tests/common/controller.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace sandbox { | |
16 | |
17 class AddressSanitizerTests : public ::testing::Test { | |
18 public: | |
19 void SetUp() { | |
20 env_.reset(base::Environment::Create()); | |
21 had_asan_options_ = env_->GetVar("ASAN_OPTIONS", &old_asan_options_); | |
22 } | |
23 | |
24 void TearDown() { | |
25 if (had_asan_options_) | |
26 ASSERT_TRUE(env_->SetVar("ASAN_OPTIONS", old_asan_options_)); | |
27 else | |
28 env_->UnSetVar("ASAN_OPTIONS"); | |
29 } | |
30 | |
31 protected: | |
32 scoped_ptr<base::Environment> env_; | |
33 bool had_asan_options_; | |
34 std::string old_asan_options_; | |
35 }; | |
36 | |
37 SBOX_TESTS_COMMAND int AddressSanitizerTests_Report(int argc, wchar_t** argv) { | |
38 volatile int idx = 42; | |
39 int *blah = new int[42]; | |
cpu_(ooo_6.6-7.5)
2015/02/04 20:59:55
state what errors we should see from asan, what wo
Timur Iskhodzhanov
2015/02/05 08:15:51
Done.
The memory leak was not important, but I've
| |
40 blah[idx] = 42; | |
41 return SBOX_TEST_FAILED; | |
42 } | |
43 | |
44 TEST_F(AddressSanitizerTests, TestAddressSanitizer) { | |
45 // This test is only supposed to work when using AddressSanitizer. | |
46 // However, ASan/Win is not on the CQ yet, so compiler breakages may get into | |
47 // the code unnoticed. To avoid that, we compile this test in all Windows | |
48 // builds, but only run the AddressSanitizer-specific part of the test when | |
49 // compiled with AddressSanitizer. | |
50 #if defined(ADDRESS_SANITIZER) | |
Timur Iskhodzhanov
2015/02/04 20:29:57
How about this?
Unfortunately, I don't think there
| |
51 bool asan_build = true; | |
52 #else | |
53 bool asan_build = false; | |
54 #endif | |
55 wchar_t temp_directory[MAX_PATH]; | |
56 wchar_t temp_file_name[MAX_PATH]; | |
57 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); | |
58 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); | |
cpu_(ooo_6.6-7.5)
2015/02/04 20:59:55
we have scoped temp directory temp file helpers i
Timur Iskhodzhanov
2015/02/05 08:15:51
Done, thanks for the suggestion!
| |
59 | |
60 SECURITY_ATTRIBUTES attrs = {}; | |
61 attrs.nLength = sizeof(attrs); | |
62 attrs.bInheritHandle = TRUE; | |
63 HANDLE file_handle = CreateFile( | |
64 temp_file_name, GENERIC_WRITE, | |
65 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, | |
66 &attrs, OPEN_EXISTING, 0, NULL); | |
67 ASSERT_NE(file_handle, INVALID_HANDLE_VALUE); | |
68 | |
69 TestRunner runner; | |
70 ASSERT_EQ(SBOX_ALL_OK, runner.GetPolicy()->SetStderrHandle(file_handle)); | |
71 | |
72 base::FilePath exe; | |
73 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe)); | |
74 base::FilePath pdb_path = exe.DirName().Append(L"*.pdb"); | |
75 ASSERT_TRUE(runner.AddFsRule(sandbox::TargetPolicy::FILES_ALLOW_READONLY, | |
76 pdb_path.value().c_str())); | |
Timur Iskhodzhanov
2015/02/04 20:29:57
Please note I actually run the ASan-inspecific par
| |
77 | |
78 env_->SetVar("ASAN_OPTIONS", "exitcode=123"); | |
79 if (asan_build) { | |
80 int result = runner.RunTest(L"AddressSanitizerTests_Report"); | |
81 EXPECT_EQ(123, result); | |
82 EXPECT_TRUE(::CloseHandle(file_handle)); | |
83 | |
84 std::string data; | |
85 ASSERT_TRUE(base::ReadFileToString(base::FilePath(temp_file_name), &data)); | |
86 // Redirection uses a feature that was added in Windows Vista. | |
87 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { | |
88 ASSERT_TRUE(strstr(data.c_str(), "ERROR: AddressSanitizer")) | |
89 << "There doesn't seem to be an ASan report:\n" << data; | |
90 ASSERT_TRUE(strstr(data.c_str(), "AddressSanitizerTests_Report")) | |
91 << "The ASan report doesn't appear to be symbolized:\n" << data; | |
92 ASSERT_TRUE(strstr(data.c_str(), strrchr(__FILE__, '\\'))) | |
93 << "The stack trace doesn't have a correct filename:\n" << data; | |
94 } else { | |
95 LOG(WARNING) << "Pre-Vista versions are not supported."; | |
96 } | |
97 } else { | |
98 LOG(WARNING) << "Not an AddressSanitizer build, skipping the run."; | |
99 } | |
100 | |
101 EXPECT_TRUE(::DeleteFile(temp_file_name)); | |
102 } | |
103 | |
104 } | |
OLD | NEW |