Skip to content

Commit

Permalink
SWDEV-362165 - Escape strings in the API function's arguments
Browse files Browse the repository at this point in the history
Strings ([const] char *, [const] char[]) passed as arguments to API
functions may not always contain printable characters. All string
arguments should be quoted and escaped in the trace logs.

Change-Id: Ie39058f2190048b1a0090df16d9ac6bc6507e28a
  • Loading branch information
lmoriche committed Oct 16, 2022
1 parent 8a575d8 commit b556f86
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
6 changes: 3 additions & 3 deletions plugin/file/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ class file_plugin_t {
<< ((record->op == HSA_API_ID_hsa_shut_down) ? record->begin_ns
: record->end_ns)
<< " " << record->process_id << ":" << record->thread_id << " "
<< hsa_api_data_pair_t(record->op, *data) << " :" << data->correlation_id
<< std::endl;
<< hsa_api_data_pair_t(record->op, *data) << " :" << std::dec
<< data->correlation_id << std::endl;
break;
}
case ACTIVITY_DOMAIN_HIP_API: {
Expand All @@ -265,7 +265,7 @@ class file_plugin_t {
*output_file << std::dec << record->begin_ns << ":" << record->end_ns << " "
<< record->process_id << ":" << record->thread_id << " "
<< hipApiString((hip_api_id_t)record->op, data) << kernel_name << " :"
<< data->correlation_id << std::endl;
<< std::dec << data->correlation_id << std::endl;
break;
}
default:
Expand Down
32 changes: 28 additions & 4 deletions script/gen_ostream_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@

header_basic = \
'namespace detail {\n' + \
'template <typename T>\n' + \
' inline static void print_escaped_string(std::ostream& out, const char *v, size_t len) {\n' + \
' out << \'"\'; \n' + \
' for (size_t i = 0; i < len && v[i]; ++i) {\n' + \
' if (std::isprint((unsigned char)v[i])) std::operator<<(out, v[i]);\n' + \
' else {\n' + \
' std::ios_base::fmtflags flags(out.flags());\n' + \
' out << "\\\\x" << std::setfill(\'0\') << std::setw(2) << std::hex << (unsigned int)(unsigned char)v[i];\n' + \
' out.flags(flags);\n' + \
' }\n' + \
' }\n' + \
' out << \'"\'; \n' + \
' }\n' + \
'\n' + \
' template <typename T>\n' + \
' inline static std::ostream& operator<<(std::ostream& out, const T& v) {\n' + \
' using std::operator<<;\n' + \
' static bool recursion = false;\n' + \
Expand All @@ -66,6 +79,15 @@
'\n' + \
' inline static std::ostream &operator<<(std::ostream &out, const char &v) {\n' + \
' out << (unsigned char)v;\n' + \
' return out;\n }\n' + \
'\n' + \
' template <size_t N>\n' + \
' inline static std::ostream &operator<<(std::ostream &out, const char (&v)[N]) {\n' + \
' print_escaped_string(out, v, N);\n' + \
' return out;\n }\n' + \
'\n' + \
' inline static std::ostream &operator<<(std::ostream &out, const char *v) {\n' + \
' print_escaped_string(out, v, strlen(v));\n' + \
' return out;\n }\n'

structs_analyzed = {}
Expand Down Expand Up @@ -120,9 +142,9 @@ def process_struct(file_handle, cppHeader_struct, cppHeader, parent_hier_name, a
indent = ""
str += " if (std::string(\"" + cppHeader_struct + "::" + name + "\").find(" + apiname.upper() + "_structs_regex" + ") != std::string::npos) {\n"
indent = " "
str += indent + " roctracer::" + apiname.lower() + "_support::detail::operator<<(out, \"" + name + "=\");\n"
str += indent + " std::operator<<(out, \"" + name + "=\");\n"
str += indent + " roctracer::" + apiname.lower() + "_support::detail::operator<<(out, v." + name + ");\n"
str += indent + " roctracer::" + apiname.lower() + "_support::detail::operator<<(out, \", \");\n"
str += indent + " std::operator<<(out, \", \");\n"
str += " }\n"
if "void" not in mtype:
global_str += str
Expand Down Expand Up @@ -166,7 +188,9 @@ def gen_cppheader(infilepath, outfilepath, rank):
'\n' + \
'#ifdef __cplusplus\n' + \
'#include <iostream>\n' + \
'#include <string>\n'
'#include <iomanip>\n' + \
'#include <string>\n' + \
'#include <cstring>\n'

output_filename_h.write(header_s)
output_filename_h.write('\n')
Expand Down

0 comments on commit b556f86

Please sign in to comment.