Skip to content

Commit

Permalink
update(userspace/libsinp): allocate str buffer only when needed in fi…
Browse files Browse the repository at this point in the history
…lters

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
  • Loading branch information
jasondellaluce authored and poiana committed Feb 9, 2024
1 parent 8453c92 commit d5a0c9d
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 88 deletions.
100 changes: 58 additions & 42 deletions userspace/libsinsp/sinsp_filtercheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ limitations under the License.
#include <libsinsp/sinsp_filtercheck.h>
#include <libsinsp/value_parser.h>

#define STR_STORAGE_SIZE 1024

#ifndef _GNU_SOURCE
//
// Fallback implementation of memmem
Expand Down Expand Up @@ -861,10 +863,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
return NULL;
}

snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
prfmt, *(int8_t *)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_INT16:
if(print_format == PF_OCT)
{
Expand All @@ -885,10 +888,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
return NULL;
}

snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
prfmt, *(int16_t *)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_INT32:
if(print_format == PF_OCT)
{
Expand All @@ -909,10 +913,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
return NULL;
}

snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
prfmt, *(int32_t *)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_INT64:
case PT_PID:
case PT_ERRNO:
Expand All @@ -939,10 +944,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
prfmt = (char*)"%" PRId64;
}

snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
prfmt, *(int64_t *)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_L4PROTO: // This can be resolved in the future
case PT_UINT8:
if(print_format == PF_OCT)
Expand All @@ -964,10 +970,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
return NULL;
}

snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
prfmt, *(uint8_t *)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_PORT: // This can be resolved in the future
case PT_UINT16:
if(print_format == PF_OCT)
Expand All @@ -989,10 +996,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
return NULL;
}

snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
prfmt, *(uint16_t *)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_UINT32:
if(print_format == PF_OCT)
{
Expand All @@ -1013,10 +1021,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
return NULL;
}

snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
prfmt, *(uint32_t *)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_UINT64:
case PT_RELTIME:
case PT_ABSTIME:
Expand All @@ -1042,11 +1051,12 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
ASSERT(false);
return NULL;
}

snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),

m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
prfmt, *(uint64_t *)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_CHARBUF:
case PT_FSPATH:
case PT_FSRELPATH:
Expand All @@ -1060,10 +1070,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
}
else
{
auto copy_len = std::min(len, (uint32_t) sizeof(m_getpropertystr_storage));
memcpy(m_getpropertystr_storage, rawval, copy_len);
m_getpropertystr_storage[copy_len] = 0;
return m_getpropertystr_storage;
auto copy_len = std::min(len, (uint32_t) STR_STORAGE_SIZE);
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
memcpy(m_getpropertystr_storage.data(), rawval, copy_len);
m_getpropertystr_storage.data()[copy_len] = 0;
return m_getpropertystr_storage.data();
}
case PT_SOCKADDR:
ASSERT(false);
Expand All @@ -1081,14 +1092,15 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
return (char*)"false";
}
case PT_IPV4ADDR:
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
"%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8,
rawval[0],
rawval[1],
rawval[2],
rawval[3]);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_IPV6ADDR:
{
char address[INET6_ADDRSTRLEN];
Expand All @@ -1098,9 +1110,10 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
strlcpy(address, "<NA>", INET6_ADDRSTRLEN);
}

strlcpy(m_getpropertystr_storage, address, sizeof(m_getpropertystr_storage));
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
strlcpy(m_getpropertystr_storage.data(), address, STR_STORAGE_SIZE);

return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
}
case PT_IPADDR:
if(len == sizeof(struct in_addr))
Expand All @@ -1117,15 +1130,17 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval,
}

case PT_DOUBLE:
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
"%.1lf", *(double*)rawval);
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
case PT_IPNET:
snprintf(m_getpropertystr_storage,
sizeof(m_getpropertystr_storage),
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
snprintf(m_getpropertystr_storage.data(),
STR_STORAGE_SIZE,
"<IPNET>");
return m_getpropertystr_storage;
return m_getpropertystr_storage.data();
default:
ASSERT(false);
throw sinsp_exception("wrong param type " + std::to_string((long long) ptype));
Expand All @@ -1152,8 +1167,9 @@ char* sinsp_filter_check::tostring(sinsp_evt* evt)
res += rawval_to_string(val.ptr, m_field->m_type, m_field->m_print_format, val.len);
}
res += ")";
strlcpy(m_getpropertystr_storage, res.c_str(), sizeof(m_getpropertystr_storage));
return m_getpropertystr_storage;
m_getpropertystr_storage.resize(STR_STORAGE_SIZE);
strlcpy(m_getpropertystr_storage.data(), res.c_str(), STR_STORAGE_SIZE);
return m_getpropertystr_storage.data();
}
return rawval_to_string(m_extracted_values[0].ptr, m_field->m_type, m_field->m_print_format, m_extracted_values[0].len);
}
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/sinsp_filtercheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class sinsp_filter_check
inline uint8_t* filter_value_p(uint16_t i = 0) { return &m_val_storages[i][0]; }
inline std::vector<uint8_t>* filter_value(uint16_t i = 0) { return &m_val_storages[i]; }

char m_getpropertystr_storage[1024];
std::vector<char> m_getpropertystr_storage;
std::vector<std::vector<uint8_t>> m_val_storages;

std::vector<filter_value_t> m_vals;
Expand Down
Loading

0 comments on commit d5a0c9d

Please sign in to comment.