blob: ddf84cb415babdcf72892d65665cadcbdde2c58a [file] [log] [blame]
Florian Mayerc10d0642023-03-22 16:12:49 -07001/*
2 * Copyright (C) 2023 The Android Open Source Project
Elliott Hughes88ba3c12023-04-24 17:08:27 -07003 * All rights reserved.
Florian Mayerc10d0642023-03-22 16:12:49 -07004 *
Elliott Hughes88ba3c12023-04-24 17:08:27 -07005 * 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.
Florian Mayerc10d0642023-03-22 16:12:49 -070014 *
Elliott Hughes88ba3c12023-04-24 17:08:27 -070015 * 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.
Florian Mayerc10d0642023-03-22 16:12:49 -070027 */
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
37using HwasanDeathTest = SilentDeathTest;
38
Florian Mayer3577a932024-09-17 13:37:21 -070039
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
50TEST_F(HwasanDeathTest, MAYBE_UseAfterFree) {
Florian Mayerc10d0642023-03-22 16:12:49 -070051 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
62TEST_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.
Florian Mayer3577a932024-09-17 13:37:21 -070073TEST(HwasanTest, MAYBE_DlopenAbsolutePath) {
Florian Mayerc10d0642023-03-22 16:12:49 -070074 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
88TEST(HwasanTest, IsRunningWithHWasan) {
89 EXPECT_TRUE(running_with_hwasan());
90}