Skip to content

Commit

Permalink
[UR] Add lifetime validation to validation layer
Browse files Browse the repository at this point in the history
  • Loading branch information
kswiecicki committed Dec 4, 2023
1 parent e20c7df commit 5e75b54
Show file tree
Hide file tree
Showing 4 changed files with 1,548 additions and 1 deletion.
20 changes: 20 additions & 0 deletions scripts/templates/valddi.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ namespace ur_validation_layer

}

if( context.enableLifetimeValidation ) {
%for tp in tracked_params:
<%
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']), {})
is_related_create_get_retain_release_func = any(func_name in funcs for funcs in tp_input_handle_funcs.values())
%>
%if tp_input_handle_funcs and not is_related_create_get_retain_release_func:
if (!refCountContext.isReferenceValid(${tp['name']})) {
refCountContext.logInvalidReference(${tp['name']});
return UR_RESULT_ERROR_INVALID_ARGUMENT;
}
%endif
%endfor
}

${x}_result_t result = ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} );

%for tp in tracked_params:
Expand Down Expand Up @@ -167,13 +182,18 @@ namespace ur_validation_layer
if (enabledLayerNames.count(nameFullValidation)) {
enableParameterValidation = true;
enableLeakChecking = true;
enableLifetimeValidation = true;
} else {
if (enabledLayerNames.count(nameParameterValidation)) {
enableParameterValidation = true;
}
if (enabledLayerNames.count(nameLeakChecking)) {
enableLeakChecking = true;
}
if (enabledLayerNames.count(nameLifetimeValidation)) {
// Handle lifetime validation requires leak checking feature.
enableLifetimeValidation = enableLeakChecking;
}
}

if(!enableParameterValidation && !enableLeakChecking) {
Expand Down
6 changes: 6 additions & 0 deletions source/loader/layers/validation/ur_leak_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ struct RefCountContext {

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

bool isReferenceValid(void *ptr) { return counts.count(ptr) > 0; }

void logInvalidReferences() {
for (auto &[ptr, refRuntimeInfo] : counts) {
context.logger.error("Retained {} reference(s) to handle {}",
Expand All @@ -128,6 +130,10 @@ struct RefCountContext {
}
}

void logInvalidReference(void *ptr) {
context.logger.error("There are no valid references to handle {}", ptr);
}

} refCountContext;

} // namespace ur_validation_layer
Expand Down
Loading

0 comments on commit 5e75b54

Please sign in to comment.