Skip to content

Commit

Permalink
cleanup(libsinsp): introduce param->as<std::string>(), add error for …
Browse files Browse the repository at this point in the history
…unsupported types

Signed-off-by: Luca Guerra <luca@guerra.sh>
  • Loading branch information
LucaGuerra committed Jun 4, 2024
1 parent c6ff3d0 commit 0d73ac1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion userspace/libsinsp/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2150,7 +2150,7 @@ void sinsp_evt::save_enter_event_params(sinsp_evt* enter_evt)
param = enter_evt->get_param_by_name(pname);
if(param)
{
std::string val {param->as<std::string_view>()};
std::string val = param->as<std::string>();
m_enter_path_param[pname] = val;
}
}
Expand Down
29 changes: 27 additions & 2 deletions userspace/libsinsp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ class SINSP_PUBLIC sinsp_evt_param
/*!
\brief Interpret the parameter as a specific type, like:
- Fixed size values (uint32_t, int8_t ..., e.g. param->as<uint32_t>())
- String-like types (NUL-terminated strings) with std::string_view (e.g. param->as<std::string_view>())
that can be used to instantiate an std::string and can also be accessed with .data() to get a pointer to a NUL-terminated string
- String-like types (NUL-terminated strings) with either:
- std::string_view (e.g. param->as<std::string_view>()) to access the original string bytes or a NULL string
- std::string (e.g. param->as<std::string>()) to obtain a copy of the string or an empty string if the parameter was NULL
- NUL-separated arrays of strings (e.g. "first\0second\0third\0") with std::vector<std::string>
*/
template<class T>
inline T as() const
Expand All @@ -119,6 +121,9 @@ class SINSP_PUBLIC sinsp_evt_param
template<class T>
inline T get_event_param_as(const sinsp_evt_param& param)
{
static_assert(std::is_fundamental_v<T>,
"event parameter cast (e.g. evt->get_param(N)->as<T>()) unsupported for this type. Implement it or see the available definitions in " __FILE__);

T ret;

if (param.m_len != sizeof(T))
Expand Down Expand Up @@ -153,6 +158,26 @@ inline std::string_view get_event_param_as<std::string_view>(const sinsp_evt_par
return {param.m_val, string_len};
}

template<>
inline std::string get_event_param_as<std::string>(const sinsp_evt_param& param)
{
if (param.m_len == 0)
{
return "";
}

size_t string_len = strnlen(param.m_val, param.m_len);
// We expect the parameter to be exactly one null-terminated string
if (param.m_len != string_len + 1)
{
// By moving this error string building operation to a separate function
// the compiler is more likely to inline this entire function.
param.throw_invalid_len_error(string_len + 1);
}

return std::string(param.m_val);
}

template<>
inline std::vector<std::string> get_event_param_as<std::vector<std::string>>(const sinsp_evt_param& param)
{
Expand Down
12 changes: 6 additions & 6 deletions userspace/libsinsp/parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ void sinsp_parser::parse_clone_exit_caller(sinsp_evt *evt, int64_t child_tid)
child_tinfo->m_vpid = child_tinfo->m_pid;

/* exe */
child_tinfo->m_exe = evt->get_param(1)->as<std::string_view>();
child_tinfo->m_exe = evt->get_param(1)->as<std::string>();

/* args */
child_tinfo->set_args(evt->get_param(2)->as<std::vector<std::string>>());
Expand All @@ -1248,7 +1248,7 @@ void sinsp_parser::parse_clone_exit_caller(sinsp_evt *evt, int64_t child_tid)
case PPME_SYSCALL_VFORK_17_X:
case PPME_SYSCALL_VFORK_20_X:
case PPME_SYSCALL_CLONE3_X:
child_tinfo->m_comm = evt->get_param(13)->as<std::string_view>();
child_tinfo->m_comm = evt->get_param(13)->as<std::string>();
break;
default:
ASSERT(false);
Expand Down Expand Up @@ -1660,7 +1660,7 @@ void sinsp_parser::parse_clone_exit_child(sinsp_evt *evt)
*/

/* exe */
child_tinfo->m_exe = evt->get_param(1)->as<std::string_view>();
child_tinfo->m_exe = evt->get_param(1)->as<std::string>();

/* comm */
switch(etype)
Expand All @@ -1678,7 +1678,7 @@ void sinsp_parser::parse_clone_exit_child(sinsp_evt *evt)
case PPME_SYSCALL_VFORK_17_X:
case PPME_SYSCALL_VFORK_20_X:
case PPME_SYSCALL_CLONE3_X:
child_tinfo->m_comm = evt->get_param(13)->as<std::string_view>();
child_tinfo->m_comm = evt->get_param(13)->as<std::string>();
break;
default:
ASSERT(false);
Expand Down Expand Up @@ -2067,7 +2067,7 @@ void sinsp_parser::parse_execve_exit(sinsp_evt *evt)
case PPME_SYSCALL_EXECVE_19_X:
case PPME_SYSCALL_EXECVEAT_X:
// Get the comm
evt->get_tinfo()->m_comm = evt->get_param(13)->as<std::string_view>();
evt->get_tinfo()->m_comm = evt->get_param(13)->as<std::string>();
break;
default:
ASSERT(false);
Expand Down Expand Up @@ -4446,7 +4446,7 @@ void sinsp_parser::parse_getcwd_exit(sinsp_evt *evt)
return;
}

std::string cwd = std::string(evt->get_param(1)->as<std::string_view>());
std::string cwd = evt->get_param(1)->as<std::string>();

#ifdef _DEBUG
if(cwd != "/")
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/sinsp_filtercheck_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ uint8_t *sinsp_filter_check_event::extract_abspath(sinsp_evt *evt, uint32_t *len
// Get the file path directly from the ring buffer.
// concatenate_paths takes care of resolving the path
//
m_strstorage = sinsp_utils::concatenate_paths("", evt->get_param(3)->as<std::string_view>());
m_strstorage = sinsp_utils::concatenate_paths("", evt->get_param(3)->as<std::string>());

RETURN_EXTRACT_STRING(m_strstorage);
}
Expand Down Expand Up @@ -1708,7 +1708,7 @@ uint8_t* sinsp_filter_check_event::extract_single(sinsp_evt *evt, uint32_t* len,

if(etype == PPME_INFRASTRUCTURE_EVENT_E)
{
std::string descstr{evt->get_param(2)->as<std::string_view>()};
std::string descstr{evt->get_param(2)->as<std::string>()};
vector<string> elements = sinsp_split(descstr, ';');
for(string ute : elements)
{
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/sinsp_filtercheck_fd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ bool sinsp_filter_check_fd::extract_fdname_from_creator(sinsp_evt *evt, uint32_t
}
case PPME_SYSCALL_OPEN_BY_HANDLE_AT_X:
{
m_tstr = evt->get_param(3)->as<std::string_view>();
m_tstr = evt->get_param(3)->as<std::string>();

if(sanitize_strings)
{
Expand Down
10 changes: 5 additions & 5 deletions userspace/libsinsp/test/events_param.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ TEST_F(sinsp_with_test_input, charbuf_empty_param)

// this, and the following similar checks, verify that the internal state is set as we need right now.
// if the internal state changes we can remove or update this check
ASSERT_STREQ(evt->get_param(1)->as<std::string_view>().data(), "<NA>");
ASSERT_EQ(evt->get_param(1)->as<std::string>(), "<NA>");

/* `PPME_SYSCALL_CREAT_E` is a simple event that uses a `PT_FSPATH`
* A `NULL` `PT_FSPATH` param is always converted to `<NA>`.
*/
evt = add_event_advance_ts(increasing_ts(), 1, PPME_SYSCALL_CREAT_E, 2, NULL, 0);
ASSERT_EQ(get_field_as_string(evt, "evt.arg.name"), "<NA>");

ASSERT_STREQ(evt->get_param(0)->as<std::string_view>().data(), "<NA>");
ASSERT_EQ(evt->get_param(0)->as<std::string>(), "<NA>");

int64_t dirfd = 0;

Expand All @@ -61,7 +61,7 @@ TEST_F(sinsp_with_test_input, charbuf_empty_param)
evt = add_event_advance_ts(increasing_ts(), 1, PPME_SYSCALL_EXECVEAT_E, 3, dirfd, NULL, 0);
ASSERT_EQ(get_field_as_string(evt, "evt.arg.pathname"), "<NA>");

ASSERT_STREQ(evt->get_param(1)->as<std::string_view>().data(), "<NA>");
ASSERT_EQ(evt->get_param(1)->as<std::string>(), "<NA>");
}

/* Assert that a `PT_CHARBUF` with `len==1` (just the `\0`) is not changed. */
Expand All @@ -81,7 +81,7 @@ TEST_F(sinsp_with_test_input, param_charbuf_len_1)
evt = add_event_advance_ts(increasing_ts(), 1, PPME_SYSCALL_CHDIR_X, 2, test_errno, "");
ASSERT_EQ(get_field_as_string(evt, "evt.arg.path"), "");

ASSERT_STREQ(evt->get_param(1)->as<std::string_view>().data(), "");
ASSERT_EQ(evt->get_param(1)->as<std::string>(), "");
}

/* Assert that a "(NULL)" `PT_CHARBUF` param is converted to `<NA>`
Expand All @@ -101,7 +101,7 @@ TEST_F(sinsp_with_test_input, charbuf_NULL_param)
evt = add_event_advance_ts(increasing_ts(), 1, PPME_SYSCALL_CHDIR_X, 2, test_errno, "(NULL)");
ASSERT_EQ(get_field_as_string(evt, "evt.arg.path"), "<NA>");

ASSERT_STREQ(evt->get_param(1)->as<std::string_view>().data(), "<NA>");
ASSERT_EQ(evt->get_param(1)->as<std::string>(), "<NA>");
}

/* Assert that an empty `PT_BYTEBUF` param is NOT converted to `<NA>` */
Expand Down

0 comments on commit 0d73ac1

Please sign in to comment.