platform_bionic/tests/hwasan_test.cpp
Florian Mayer c10d064b5c Introduce hwasan mode for linker
This mode instructs the linker to search for libraries in hwasan
subdirectories of all library search paths. This is set up to contain a
hwasan-enabled copy of libc, which is needed for HWASan programs to
operate. There are two ways this mode can be enabled:

* for native binaries, by using the linker_hwasan64 symlink as its
  interpreter
* for apps: by setting the LD_HWASAN environment variable in wrap.sh

Bug: 276930343
Change-Id: I0f4117a50091616f26947fbe37a28ee573b97ad0
2023-04-14 01:33:30 -07:00

67 lines
2.1 KiB
C++

/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dlfcn.h>
#include <stdlib.h>
#include <gtest/gtest.h>
#include <android-base/silent_death_test.h>
#include <android-base/test_utils.h>
using HwasanDeathTest = SilentDeathTest;
TEST_F(HwasanDeathTest, UseAfterFree) {
EXPECT_DEATH(
{
void* m = malloc(1);
volatile char* x = const_cast<volatile char*>(reinterpret_cast<char*>(m));
*x = 1;
free(m);
*x = 2;
},
"use-after-free");
}
TEST_F(HwasanDeathTest, OutOfBounds) {
EXPECT_DEATH(
{
void* m = malloc(1);
volatile char* x = const_cast<volatile char*>(reinterpret_cast<char*>(m));
x[1] = 1;
},
"buffer-overflow");
}
// Check whether dlopen of /foo/bar.so checks /foo/hwasan/bar.so first.
TEST(HwasanTest, DlopenAbsolutePath) {
std::string path = android::base::GetExecutableDirectory() + "/libtest_simple_hwasan.so";
ASSERT_EQ(0, access(path.c_str(), F_OK)); // Verify test setup.
std::string hwasan_path =
android::base::GetExecutableDirectory() + "/hwasan/libtest_simple_hwasan.so";
ASSERT_EQ(0, access(hwasan_path.c_str(), F_OK)); // Verify test setup.
void* handle = dlopen(path.c_str(), RTLD_NOW);
ASSERT_TRUE(handle != nullptr);
uint32_t* compiled_with_hwasan =
reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_compiled_with_hwasan"));
EXPECT_TRUE(*compiled_with_hwasan);
dlclose(handle);
}
TEST(HwasanTest, IsRunningWithHWasan) {
EXPECT_TRUE(running_with_hwasan());
}