Skip to content

Commit f7bd6d9

Browse files
committed
[UR] Add lifetime validation to validation layer
1 parent 1c5e22b commit f7bd6d9

File tree

4 files changed

+1027
-3
lines changed

4 files changed

+1027
-3
lines changed

scripts/templates/valddi.cpp.mako

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ namespace ur_validation_layer
7474

7575
}
7676

77+
%for tp in tracked_params:
78+
<%
79+
tp_input_handle_funcs = next((hf for hf in handle_create_get_retain_release_funcs if th.subt(n, tags, tp['type']) == hf['handle'] and "[in]" in tp['desc']), {})
80+
is_related_create_get_retain_release_func = any(func_name in funcs for funcs in tp_input_handle_funcs.values())
81+
%>
82+
%if tp_input_handle_funcs and not is_related_create_get_retain_release_func:
83+
if (context.enableLifetimeValidation && !refCountContext.isReferenceValid(${tp['name']})) {
84+
refCountContext.logInvalidReference(${tp['name']});
85+
}
86+
%endif
87+
%endfor
88+
7789
${x}_result_t result = ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} );
7890

7991
%for tp in tracked_params:
@@ -167,16 +179,22 @@ namespace ur_validation_layer
167179
if (enabledLayerNames.count(nameFullValidation)) {
168180
enableParameterValidation = true;
169181
enableLeakChecking = true;
182+
enableLifetimeValidation = true;
170183
} else {
171184
if (enabledLayerNames.count(nameParameterValidation)) {
172185
enableParameterValidation = true;
173186
}
174187
if (enabledLayerNames.count(nameLeakChecking)) {
175188
enableLeakChecking = true;
176189
}
190+
if (enabledLayerNames.count(nameLifetimeValidation)) {
191+
// Handle lifetime validation requires leak checking feature.
192+
enableLifetimeValidation = true;
193+
enableLeakChecking = true;
194+
}
177195
}
178196

179-
if(!enableParameterValidation && !enableLeakChecking) {
197+
if (!enableParameterValidation && !enableLeakChecking && !enableLifetimeValidation) {
180198
return result;
181199
}
182200

source/loader/layers/validation/ur_leak_check.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ struct RefCountContext {
115115

116116
void clear() { counts.clear(); }
117117

118+
bool isReferenceValid(void *ptr) { return counts.count(ptr) > 0; }
119+
118120
void logInvalidReferences() {
119121
for (auto &[ptr, refRuntimeInfo] : counts) {
120122
context.logger.error("Retained {} reference(s) to handle {}",
@@ -128,6 +130,10 @@ struct RefCountContext {
128130
}
129131
}
130132

133+
void logInvalidReference(void *ptr) {
134+
context.logger.error("There are no valid references to handle {}", ptr);
135+
}
136+
131137
} refCountContext;
132138

133139
} // namespace ur_validation_layer

0 commit comments

Comments
 (0)