Skip to content

Commit

Permalink
Add exception sanitizer layer
Browse files Browse the repository at this point in the history
Add basic exception sanitizer layer that will call std::abort if an
exception is thrown.
  • Loading branch information
hdelan committed Oct 17, 2024
1 parent 7c19b36 commit c1be939
Show file tree
Hide file tree
Showing 7 changed files with 7,583 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,30 @@ def _mako_print_hpp(path, namespace, tags, version, revision, specs, meta):
specs=specs,
meta=meta)

"""
generates c/c++ files from the specification documents
"""
def _mako_exception_sanitizer_layer_cpp(path, namespace, tags, version, specs, meta):
dstpath = os.path.join(path, "exception_sanitizer")
os.makedirs(dstpath, exist_ok=True)

template = "exceptionddi.cpp.mako"
fin = os.path.join(templates_dir, template)

name = "%s_exceptionddi"%(namespace)
filename = "%s.cpp"%(name)
fout = os.path.join(dstpath, filename)

print("Generating %s..."%fout)
return util.makoWrite(
fin, fout,
name=name,
ver=version,
namespace=namespace,
tags=tags,
specs=specs,
meta=meta)

"""
Entry-point:
generates tools code
Expand Down Expand Up @@ -468,6 +492,9 @@ def generate_layers(path, section, namespace, tags, version, specs, meta):
loc += _mako_tracing_layer_cpp(layer_dstpath, namespace, tags, version, specs, meta)
print("TRACING Generated %s lines of code.\n"%loc)

loc += _mako_exception_sanitizer_layer_cpp(layer_dstpath, namespace, tags, version, specs, meta)
print("TRACING Generated %s lines of code.\n"%loc)

"""
Entry-point:
generates common utilities for unified_runtime
Expand Down
127 changes: 127 additions & 0 deletions scripts/templates/exceptionddi.cpp.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<%!
import re
from templates import helper as th
%><%
n=namespace
N=n.upper()
x=tags['$x']
X=x.upper()
handle_create_get_retain_release_funcs=th.get_handle_create_get_retain_release_functions(specs, n, tags)
%>/*
*
* Copyright (C) 2023-2024 Intel Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
* See LICENSE.TXT
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
* @file ${name}.cpp
*
*/
#include "${x}_exception_sanitizer_layer.hpp"

namespace ur_exception_sanitizer_layer
{
%for obj in th.get_adapter_functions(specs):
<%
func_name=th.make_func_name(n, tags, obj)
param_checks=th.make_param_checks(n, tags, obj, meta=meta).items()
first_errors = [X + "_RESULT_ERROR_INVALID_NULL_POINTER", X + "_RESULT_ERROR_INVALID_NULL_HANDLE"]
sorted_param_checks = sorted(param_checks, key=lambda pair: False if pair[0] in first_errors else True)
tracked_params = list(filter(lambda p: any(th.subt(n, tags, p['type']) in [hf['handle'], hf['handle'] + "*"] for hf in handle_create_get_retain_release_funcs), obj['params']))
%>
///////////////////////////////////////////////////////////////////////////////
/// @brief Intercept function for ${th.make_func_name(n, tags, obj)}
%if 'condition' in obj:
#if ${th.subt(n, tags, obj['condition'])}
%endif
__${x}dlllocal ${x}_result_t ${X}_APICALL
${func_name}(
%for line in th.make_param_lines(n, tags, obj):
${line}
%endfor
)
{${th.get_initial_null_set(obj)}
auto ${th.make_pfn_name(n, tags, obj)} = getContext()->${n}DdiTable.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)};

if( nullptr == ${th.make_pfn_name(n, tags, obj)} ) {
return ${X}_RESULT_ERROR_UNINITIALIZED;
}

${x}_result_t result = UR_RESULT_SUCCESS;
try {
result = ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} );
} catch (...) {
std::cerr << "Exception caught from adapter layer in " << __func__ << " aborting\n";
}

return result;
}
%if 'condition' in obj:
#endif // ${th.subt(n, tags, obj['condition'])}
%endif

%endfor
%for tbl in th.get_pfntables(specs, meta, n, tags):
///////////////////////////////////////////////////////////////////////////////
/// @brief Exported function for filling application's ${tbl['name']} table
/// with current process' addresses
///
/// @returns
/// - ::${X}_RESULT_SUCCESS
/// - ::${X}_RESULT_ERROR_INVALID_NULL_POINTER
/// - ::${X}_RESULT_ERROR_UNSUPPORTED_VERSION
${X}_DLLEXPORT ${x}_result_t ${X}_APICALL
${tbl['export']['name']}(
%for line in th.make_param_lines(n, tags, tbl['export']):
${line}
%endfor
)
{
auto& dditable = ur_exception_sanitizer_layer::getContext()->${n}DdiTable.${tbl['name']};

if( nullptr == pDdiTable )
return ${X}_RESULT_ERROR_INVALID_NULL_POINTER;

if (UR_MAJOR_VERSION(ur_exception_sanitizer_layer::getContext()->version) != UR_MAJOR_VERSION(version) ||
UR_MINOR_VERSION(ur_exception_sanitizer_layer::getContext()->version) > UR_MINOR_VERSION(version))
return ${X}_RESULT_ERROR_UNSUPPORTED_VERSION;

${x}_result_t result = ${X}_RESULT_SUCCESS;

%for obj in tbl['functions']:
%if 'condition' in obj:
#if ${th.subt(n, tags, obj['condition'])}
%endif
dditable.${th.append_ws(th.make_pfn_name(n, tags, obj), 43)} = pDdiTable->${th.make_pfn_name(n, tags, obj)};
pDdiTable->${th.append_ws(th.make_pfn_name(n, tags, obj), 41)} = ur_exception_sanitizer_layer::${th.make_func_name(n, tags, obj)};
%if 'condition' in obj:
#else
dditable.${th.append_ws(th.make_pfn_name(n, tags, obj), 43)} = nullptr;
pDdiTable->${th.append_ws(th.make_pfn_name(n, tags, obj), 41)} = nullptr;
#endif
%endif

%endfor
return result;
}

%endfor
${x}_result_t
context_t::init(ur_dditable_t *dditable,
const std::set<std::string> &enabledLayerNames,
codeloc_data) {
${x}_result_t result = ${X}_RESULT_SUCCESS;

return result;
}

${x}_result_t context_t::tearDown() {
return ${X}_RESULT_SUCCESS;
}

} // namespace ur_exception_sanitizer_layer
9 changes: 9 additions & 0 deletions source/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ if(UR_ENABLE_SANITIZER)
endif()
endif()

if(UR_ENABLE_EXCEPTION_SANITIZER)
target_sources(ur_loader
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/layers/exception_sanitizer/ur_exception_sanitizer_layer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/exception_sanitizer/ur_exception_sanitizer_layer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/exception_sanitizer/ur_exceptionddi.cpp
)
endif()

target_include_directories(ur_loader PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer"
"${CMAKE_CURRENT_SOURCE_DIR}/../"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
*
* Copyright (C) 2023 Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
* See LICENSE.TXT
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
* @file ur_exception_sanitizer_layer.h
*
*/

#include "ur_exception_sanitizer_layer.hpp"

namespace ur_exception_sanitizer_layer {

context_t *getContext() { return context_t::get_direct(); }

} // namespace ur_exception_sanitizer_layer
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* Copyright (C) 2023 Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
* See LICENSE.TXT
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
* @file ur_exception_sanitizer_layer.h
*
*/

#ifndef UR_EXCEPTION_SANITIZER_LAYER_H
#define UR_EXCEPTION_SANITIZER_LAYER_H 1

#include "ur_ddi.h"
#include "ur_proxy_layer.hpp"
#include "ur_util.hpp"

#define EXCEPTION_SANITIZER_COMP_NAME "exception sanitizer layer"

namespace ur_exception_sanitizer_layer {

///////////////////////////////////////////////////////////////////////////////
class __urdlllocal context_t : public proxy_layer_context_t,
public AtomicSingleton<context_t> {
public:
ur_dditable_t urDdiTable = {};
codeloc_data codelocData;

context_t() = default;
~context_t() = default;

ur_result_t init(ur_dditable_t *dditable,
const std::set<std::string> &enabledLayerNames,
codeloc_data codelocData) override;
ur_result_t tearDown() override { return UR_RESULT_SUCCESS; }

private:
inline static const std::string name = "UR_LAYER_EXCEPTION_SANITIZER";
};

context_t *getContext();

} // namespace ur_exception_sanitizer_layer

#endif /* UR_EXCEPTION_SANITIZER_LAYER_H */
Loading

0 comments on commit c1be939

Please sign in to comment.