xref: /aosp_15_r20/bionic/tests/hwasan_test.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <dlfcn.h>
30 #include <stdlib.h>
31 
32 #include <gtest/gtest.h>
33 
34 #include <android-base/silent_death_test.h>
35 #include <android-base/test_utils.h>
36 
37 using HwasanDeathTest = SilentDeathTest;
38 
39 
40 #ifdef HWASAN_TEST_STATIC
41 #define MAYBE_DlopenAbsolutePath DISABLED_DlopenAbsolutePath
42 // TODO(fmayer): figure out why uaf is misclassified as out of bounds for
43 // static executables.
44 #define MAYBE_UseAfterFree DISABLED_UseAfterFree
45 #else
46 #define MAYBE_DlopenAbsolutePath DlopenAbsolutePath
47 #define MAYBE_UseAfterFree UseAfterFree
48 #endif
49 
TEST_F(HwasanDeathTest,MAYBE_UseAfterFree)50 TEST_F(HwasanDeathTest, MAYBE_UseAfterFree) {
51   EXPECT_DEATH(
52       {
53         void* m = malloc(1);
54         volatile char* x = const_cast<volatile char*>(reinterpret_cast<char*>(m));
55         *x = 1;
56         free(m);
57         *x = 2;
58       },
59       "use-after-free");
60 }
61 
TEST_F(HwasanDeathTest,OutOfBounds)62 TEST_F(HwasanDeathTest, OutOfBounds) {
63   EXPECT_DEATH(
64       {
65         void* m = malloc(1);
66         volatile char* x = const_cast<volatile char*>(reinterpret_cast<char*>(m));
67         x[1] = 1;
68       },
69       "buffer-overflow");
70 }
71 
72 // Check whether dlopen of /foo/bar.so checks /foo/hwasan/bar.so first.
TEST(HwasanTest,MAYBE_DlopenAbsolutePath)73 TEST(HwasanTest, MAYBE_DlopenAbsolutePath) {
74   std::string path = android::base::GetExecutableDirectory() + "/libtest_simple_hwasan.so";
75   ASSERT_EQ(0, access(path.c_str(), F_OK));  // Verify test setup.
76   std::string hwasan_path =
77       android::base::GetExecutableDirectory() + "/hwasan/libtest_simple_hwasan.so";
78   ASSERT_EQ(0, access(hwasan_path.c_str(), F_OK));  // Verify test setup.
79 
80   void* handle = dlopen(path.c_str(), RTLD_NOW);
81   ASSERT_TRUE(handle != nullptr);
82   uint32_t* compiled_with_hwasan =
83       reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_compiled_with_hwasan"));
84   EXPECT_TRUE(*compiled_with_hwasan);
85   dlclose(handle);
86 }
87 
TEST(HwasanTest,IsRunningWithHWasan)88 TEST(HwasanTest, IsRunningWithHWasan) {
89   EXPECT_TRUE(running_with_hwasan());
90 }
91