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
TEST_F(HwasanDeathTest,UseAfterFree)39 TEST_F(HwasanDeathTest, UseAfterFree) {
40 EXPECT_DEATH(
41 {
42 void* m = malloc(1);
43 volatile char* x = const_cast<volatile char*>(reinterpret_cast<char*>(m));
44 *x = 1;
45 free(m);
46 *x = 2;
47 },
48 "use-after-free");
49 }
50
TEST_F(HwasanDeathTest,OutOfBounds)51 TEST_F(HwasanDeathTest, OutOfBounds) {
52 EXPECT_DEATH(
53 {
54 void* m = malloc(1);
55 volatile char* x = const_cast<volatile char*>(reinterpret_cast<char*>(m));
56 x[1] = 1;
57 },
58 "buffer-overflow");
59 }
60
61 // Check whether dlopen of /foo/bar.so checks /foo/hwasan/bar.so first.
TEST(HwasanTest,DlopenAbsolutePath)62 TEST(HwasanTest, DlopenAbsolutePath) {
63 std::string path = android::base::GetExecutableDirectory() + "/libtest_simple_hwasan.so";
64 ASSERT_EQ(0, access(path.c_str(), F_OK)); // Verify test setup.
65 std::string hwasan_path =
66 android::base::GetExecutableDirectory() + "/hwasan/libtest_simple_hwasan.so";
67 ASSERT_EQ(0, access(hwasan_path.c_str(), F_OK)); // Verify test setup.
68
69 void* handle = dlopen(path.c_str(), RTLD_NOW);
70 ASSERT_TRUE(handle != nullptr);
71 uint32_t* compiled_with_hwasan =
72 reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_compiled_with_hwasan"));
73 EXPECT_TRUE(*compiled_with_hwasan);
74 dlclose(handle);
75 }
76
TEST(HwasanTest,IsRunningWithHWasan)77 TEST(HwasanTest, IsRunningWithHWasan) {
78 EXPECT_TRUE(running_with_hwasan());
79 }
80