Florian Mayer | c10d064 | 2023-03-22 16:12:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
Elliott Hughes | 88ba3c1 | 2023-04-24 17:08:27 -0700 | [diff] [blame] | 3 | * All rights reserved. |
Florian Mayer | c10d064 | 2023-03-22 16:12:49 -0700 | [diff] [blame] | 4 | * |
Elliott Hughes | 88ba3c1 | 2023-04-24 17:08:27 -0700 | [diff] [blame] | 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. |
Florian Mayer | c10d064 | 2023-03-22 16:12:49 -0700 | [diff] [blame] | 14 | * |
Elliott Hughes | 88ba3c1 | 2023-04-24 17:08:27 -0700 | [diff] [blame] | 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. |
Florian Mayer | c10d064 | 2023-03-22 16:12:49 -0700 | [diff] [blame] | 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 | |
Florian Mayer | 3577a93 | 2024-09-17 13:37:21 -0700 | [diff] [blame] | 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 | |
| 50 | TEST_F(HwasanDeathTest, MAYBE_UseAfterFree) { |
Florian Mayer | c10d064 | 2023-03-22 16:12:49 -0700 | [diff] [blame] | 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 | |
| 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. |
Florian Mayer | 3577a93 | 2024-09-17 13:37:21 -0700 | [diff] [blame] | 73 | TEST(HwasanTest, MAYBE_DlopenAbsolutePath) { |
Florian Mayer | c10d064 | 2023-03-22 16:12:49 -0700 | [diff] [blame] | 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 | |
| 88 | TEST(HwasanTest, IsRunningWithHWasan) { |
| 89 | EXPECT_TRUE(running_with_hwasan()); |
| 90 | } |