Skip to content

Commit 9e6923f

Browse files
committed
wip
1 parent ae7dea6 commit 9e6923f

File tree

5 files changed

+275
-0
lines changed

5 files changed

+275
-0
lines changed

test/layers/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ add_subdirectory(validation)
88
if(UR_ENABLE_TRACING)
99
add_subdirectory(tracing)
1010
endif()
11+
12+
if(UR_ENABLE_SANITIZER)
13+
add_subdirectory(sanitizer)
14+
endif()

test/layers/sanitizer/CMakeLists.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (C) 2023-2024 Intel Corporation
2+
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
# See LICENSE.TXT
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
set(UR_VALIDATION_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
7+
set(VAL_TEST_PREFIX validation_test)
8+
9+
function(add_validation_test_executable name)
10+
add_ur_executable(${VAL_TEST_PREFIX}-${name}
11+
${ARGN})
12+
target_link_libraries(${VAL_TEST_PREFIX}-${name}
13+
PRIVATE
14+
${PROJECT_NAME}::loader
15+
${PROJECT_NAME}::headers
16+
${PROJECT_NAME}::testing
17+
${PROJECT_NAME}::mock
18+
GTest::gtest_main)
19+
endfunction()
20+
21+
function(set_validation_test_properties name)
22+
set_tests_properties(${name} PROPERTIES LABELS "validation")
23+
set_property(TEST ${name} PROPERTY ENVIRONMENT
24+
"UR_ENABLE_LAYERS=UR_LAYER_FULL_VALIDATION"
25+
"UR_ADAPTERS_FORCE_LOAD=\"$<TARGET_FILE:ur_adapter_mock>\""
26+
"UR_LOG_VALIDATION=level:debug\;flush:debug\;output:stdout")
27+
endfunction()
28+
29+
function(add_validation_test name)
30+
add_validation_test_executable(${name} ${ARGN})
31+
32+
add_test(NAME ${name}
33+
COMMAND ${VAL_TEST_PREFIX}-${name}
34+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
35+
36+
set_validation_test_properties(${name})
37+
endfunction()
38+
39+
function(add_validation_match_test name)
40+
add_validation_test_executable(${name} ${ARGN})
41+
42+
add_test(NAME ${name}
43+
COMMAND ${CMAKE_COMMAND}
44+
-D MODE=stdout
45+
-D TEST_FILE=$<TARGET_FILE:${VAL_TEST_PREFIX}-${name}>
46+
-D MATCH_FILE=${CMAKE_CURRENT_SOURCE_DIR}/${name}.out.match
47+
-P ${PROJECT_SOURCE_DIR}/cmake/match.cmake
48+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
49+
50+
set_validation_test_properties(${name})
51+
endfunction()
52+
53+
add_validation_test(parameters parameters.cpp)
54+
add_validation_match_test(leaks leaks.out.match leaks.cpp)
55+
add_validation_match_test(leaks_mt leaks_mt.out.match leaks_mt.cpp)
56+
add_validation_match_test(lifetime lifetime.out.match lifetime.cpp)

test/layers/sanitizer/fixtures.hpp

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright (C) 2023-2024 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See LICENSE.TXT
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
#ifndef UR_VALIDATION_TEST_HELPERS_H
7+
#define UR_VALIDATION_TEST_HELPERS_H
8+
9+
#include <atomic>
10+
#include <gtest/gtest.h>
11+
12+
#include <ur_api.h>
13+
#include <ur_mock_helpers.hpp>
14+
15+
struct urTest : ::testing::Test {
16+
17+
void SetUp() override {
18+
ASSERT_EQ(urLoaderConfigCreate(&loader_config), UR_RESULT_SUCCESS);
19+
ASSERT_EQ(urLoaderConfigEnableLayer(loader_config,
20+
"UR_LAYER_FULL_VALIDATION"),
21+
UR_RESULT_SUCCESS);
22+
ur_device_init_flags_t device_flags = 0;
23+
ASSERT_EQ(urLoaderInit(device_flags, loader_config), UR_RESULT_SUCCESS);
24+
}
25+
26+
void TearDown() override {
27+
if (loader_config) {
28+
ASSERT_EQ(urLoaderConfigRelease(loader_config), UR_RESULT_SUCCESS);
29+
}
30+
ASSERT_EQ(urLoaderTearDown(), UR_RESULT_SUCCESS);
31+
}
32+
33+
ur_loader_config_handle_t loader_config = nullptr;
34+
};
35+
36+
struct valAdaptersTest : urTest {
37+
38+
void SetUp() override {
39+
urTest::SetUp();
40+
41+
uint32_t adapter_count;
42+
ASSERT_EQ(urAdapterGet(0, nullptr, &adapter_count), UR_RESULT_SUCCESS);
43+
ASSERT_GT(adapter_count, 0);
44+
adapters.resize(adapter_count);
45+
ASSERT_EQ(urAdapterGet(adapter_count, adapters.data(), nullptr),
46+
UR_RESULT_SUCCESS);
47+
}
48+
49+
void TearDown() override {
50+
for (auto adapter : adapters) {
51+
ASSERT_EQ(urAdapterRelease(adapter), UR_RESULT_SUCCESS);
52+
}
53+
urTest::TearDown();
54+
}
55+
56+
std::vector<ur_adapter_handle_t> adapters;
57+
};
58+
59+
struct valAdapterTest : valAdaptersTest {
60+
61+
void SetUp() override {
62+
valAdaptersTest::SetUp();
63+
adapter = adapters[0]; // TODO - which to choose?
64+
}
65+
66+
ur_adapter_handle_t adapter;
67+
};
68+
69+
struct valPlatformsTest : valAdaptersTest {
70+
71+
void SetUp() override {
72+
valAdaptersTest::SetUp();
73+
74+
uint32_t count;
75+
ASSERT_EQ(urPlatformGet(adapters.data(),
76+
static_cast<uint32_t>(adapters.size()), 0,
77+
nullptr, &count),
78+
UR_RESULT_SUCCESS);
79+
ASSERT_GT(count, 0);
80+
platforms.resize(count);
81+
ASSERT_EQ(urPlatformGet(adapters.data(),
82+
static_cast<uint32_t>(adapters.size()), count,
83+
platforms.data(), nullptr),
84+
UR_RESULT_SUCCESS);
85+
}
86+
87+
std::vector<ur_platform_handle_t> platforms;
88+
};
89+
90+
struct valPlatformTest : valPlatformsTest {
91+
92+
void SetUp() override {
93+
valPlatformsTest::SetUp();
94+
ASSERT_GE(platforms.size(), 1);
95+
platform = platforms[0]; // TODO - which to choose?
96+
}
97+
98+
ur_platform_handle_t platform;
99+
};
100+
101+
struct valAllDevicesTest : valPlatformTest {
102+
103+
void SetUp() override {
104+
valPlatformTest::SetUp();
105+
106+
uint32_t count = 0;
107+
if (urDeviceGet(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count) ||
108+
count == 0) {
109+
FAIL() << "Failed to get devices";
110+
}
111+
112+
devices.resize(count);
113+
if (urDeviceGet(platform, UR_DEVICE_TYPE_ALL, count, devices.data(),
114+
nullptr)) {
115+
FAIL() << "Failed to get devices";
116+
}
117+
}
118+
119+
void TearDown() override {
120+
for (auto device : devices) {
121+
ASSERT_EQ(urDeviceRelease(device), UR_RESULT_SUCCESS);
122+
}
123+
valPlatformTest::TearDown();
124+
}
125+
126+
std::vector<ur_device_handle_t> devices;
127+
};
128+
129+
// We use this to avoid segfaults in the mock adapter when we're doing stuff
130+
// like double releases in the leak detection tests.
131+
inline ur_result_t genericSuccessCallback(void *) { return UR_RESULT_SUCCESS; };
132+
133+
// This returns valid (non-null) handles that we can safely leak.
134+
inline ur_result_t fakeContext_urContextCreate(void *pParams) {
135+
static std::atomic_int handle = 42;
136+
auto params = *static_cast<ur_context_create_params_t *>(pParams);
137+
// There are two casts because windows doesn't implicitly extend the 32 bit
138+
// result of atomic_int::operator++.
139+
**params.pphContext =
140+
reinterpret_cast<ur_context_handle_t>(static_cast<uintptr_t>(handle++));
141+
return UR_RESULT_SUCCESS;
142+
}
143+
144+
struct valDeviceTest : valAllDevicesTest {
145+
146+
void SetUp() override {
147+
valAllDevicesTest::SetUp();
148+
ASSERT_GE(devices.size(), 1);
149+
device = devices[0];
150+
mock::getCallbacks().set_replace_callback("urContextRetain",
151+
&genericSuccessCallback);
152+
mock::getCallbacks().set_replace_callback("urContextRelease",
153+
&genericSuccessCallback);
154+
mock::getCallbacks().set_replace_callback("urContextCreate",
155+
&fakeContext_urContextCreate);
156+
}
157+
158+
void TearDown() override {
159+
mock::getCallbacks().resetCallbacks();
160+
valAllDevicesTest::TearDown();
161+
}
162+
ur_device_handle_t device;
163+
};
164+
165+
struct valDeviceTestMultithreaded : valDeviceTest,
166+
public ::testing::WithParamInterface<int> {
167+
void SetUp() override {
168+
valDeviceTest::SetUp();
169+
170+
threadCount = GetParam();
171+
}
172+
173+
int threadCount;
174+
};
175+
176+
#endif // UR_VALIDATION_TEST_HELPERS_H

test/layers/sanitizer/lifetime.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (C) 2023-2024 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See LICENSE.TXT
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
#include "fixtures.hpp"
7+
8+
TEST_F(urTest, testUrAdapterHandleLifetimeExpectFail) {
9+
size_t size = 0;
10+
ur_adapter_handle_t adapter = (ur_adapter_handle_t)0xC0FFEE;
11+
ur_adapter_info_t info_type = UR_ADAPTER_INFO_BACKEND;
12+
urAdapterGetInfo(adapter, info_type, 0, nullptr, &size);
13+
}
14+
15+
TEST_F(valAdapterTest, testUrAdapterHandleLifetimeExpectSuccess) {
16+
size_t size = 0;
17+
ur_adapter_info_t info_type = UR_ADAPTER_INFO_BACKEND;
18+
urAdapterGetInfo(adapter, info_type, 0, nullptr, &size);
19+
}
20+
21+
TEST_F(valAdapterTest, testUrAdapterHandleTypeMismatchExpectFail) {
22+
size_t size = 0;
23+
// Use valid adapter handle with incorrect cast.
24+
ur_device_handle_t device = (ur_device_handle_t)adapter;
25+
ur_device_info_t info_type = UR_DEVICE_INFO_BACKEND_RUNTIME_VERSION;
26+
urDeviceGetInfo(device, info_type, 0, nullptr, &size);
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{IGNORE}}
2+
[ RUN ] urTest.testUrAdapterHandleLifetimeExpectFail
3+
<VALIDATION>[ERROR]: There are no valid references to handle {{[0-9xa-fA-F]+}}
4+
{{IGNORE}}
5+
[ RUN ] valAdapterTest.testUrAdapterHandleLifetimeExpectSuccess
6+
<VALIDATION>[DEBUG]: Reference count for handle {{[0-9xa-fA-F]+}} changed to 1
7+
{{^(?!.*There are no valid references to handle).*$}}
8+
{{IGNORE}}
9+
[ RUN ] valAdapterTest.testUrAdapterHandleTypeMismatchExpectFail
10+
<VALIDATION>[DEBUG]: Reference count for handle {{[0-9xa-fA-F]+}} changed to 1
11+
<VALIDATION>[ERROR]: There are no valid references to handle {{[0-9xa-fA-F]+}}
12+
{{IGNORE}}

0 commit comments

Comments
 (0)