Skip to content

Commit d408cf7

Browse files
Andreagit97FedeDP
andcommitted
cleanup: address review comments
Signed-off-by: Andrea Terzolo <[email protected]> Co-authored-by: Federico Di Pierro <[email protected]>
1 parent 1775efd commit d408cf7

File tree

5 files changed

+9
-62
lines changed

5 files changed

+9
-62
lines changed

userspace/libscap/engine/savefile/converter/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# Since we have circular dependencies between libscap and the savefile engine, make this library
1717
# always static (directly linked into libscap)
18-
add_library(scap_savefile_converter STATIC converter.cpp)
18+
add_library(scap_savefile_converter STATIC converter.cpp table.cpp)
1919

2020
add_dependencies(scap_savefile_converter uthash)
2121
target_include_directories(

userspace/libscap/engine/savefile/converter/converter.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ limitations under the License.
1919
#include <driver/ppm_events_public.h>
2020
#include <converter/types.h>
2121
#include <converter/results.h>
22-
#include <converter/table.h>
2322
#include <converter/debug_macro.h>
2423
#include <stdarg.h>
2524
#include <cstdio>
@@ -29,6 +28,8 @@ limitations under the License.
2928
#include <stdexcept>
3029
#include <memory>
3130

31+
extern std::unordered_map<conversion_key, conversion_info> g_conversion_table;
32+
3233
typedef std::shared_ptr<scap_evt> safe_scap_evt_t;
3334

3435
static inline safe_scap_evt_t safe_scap_evt(scap_evt *evt) {
@@ -124,7 +125,6 @@ static void push_default_parameter(scap_evt *evt, uint16_t *params_offset, uint8
124125
// Otherwise we will access the wrong entry in the event table.
125126
const struct ppm_event_info *event_info = &(g_event_info[evt->type]);
126127
uint16_t len = scap_get_size_bytes_from_type(event_info->params[param_num].type);
127-
char *ptr = scap_get_default_value_from_type(event_info->params[param_num].type);
128128
uint16_t lens_offset = sizeof(scap_evt) + param_num * sizeof(uint16_t);
129129

130130
PRINT_MESSAGE(
@@ -136,8 +136,11 @@ static void push_default_parameter(scap_evt *evt, uint16_t *params_offset, uint8
136136
*params_offset,
137137
lens_offset);
138138

139-
// If value is NULL, the len should be 0
140-
memcpy((char *)evt + *params_offset, ptr, len);
139+
// The default param will be always 0 so we just need to copy the right number of 0 bytes.
140+
// `uint64_t` should be enough for all the types considering that types like CHARBUF, BYTEBUF
141+
// have `len==0`
142+
static uint64_t val = 0;
143+
memcpy((char *)evt + *params_offset, (char *)&val, len);
141144
*params_offset += len;
142145
memcpy((char *)evt + lens_offset, &len, sizeof(uint16_t));
143146
}

userspace/libscap/engine/savefile/converter/table.h renamed to userspace/libscap/engine/savefile/converter/table.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
1717
*/
18-
#pragma once
1918

2019
#include <converter/types.h>
2120
#include <libscap/scap_const.h>
2221
#include <driver/ppm_events_public.h>
23-
2422
#include <unordered_map>
2523

26-
static std::unordered_map<conversion_key, conversion_info> g_conversion_table = {};
24+
std::unordered_map<conversion_key, conversion_info> g_conversion_table = {};

userspace/libscap/scap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,6 @@ int32_t scap_event_encode_params_v(struct scap_sized_buffer event_buf,
856856
uint32_t n,
857857
va_list args);
858858
uint8_t scap_get_size_bytes_from_type(enum ppm_param_type t);
859-
char* scap_get_default_value_from_type(enum ppm_param_type t);
860859
bool scap_compare_events(scap_evt* curr, scap_evt* expected, char* error);
861860
scap_evt* scap_create_event_v(char* error,
862861
uint64_t ts,

userspace/libscap/scap_event.c

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -410,59 +410,6 @@ uint8_t scap_get_size_bytes_from_type(enum ppm_param_type t) {
410410
return 0;
411411
}
412412

413-
char *scap_get_default_value_from_type(enum ppm_param_type t) {
414-
static uint8_t default_uint8 = 0;
415-
static uint16_t default_uint16 = 0;
416-
static uint32_t default_uint32 = 0;
417-
static uint64_t default_uint64 = 0;
418-
switch(t) {
419-
case PT_INT8:
420-
case PT_UINT8:
421-
case PT_FLAGS8:
422-
case PT_ENUMFLAGS8:
423-
return (char *)&default_uint8;
424-
case PT_INT16:
425-
case PT_UINT16:
426-
case PT_FLAGS16:
427-
case PT_ENUMFLAGS16:
428-
case PT_SYSCALLID:
429-
return (char *)&default_uint16;
430-
case PT_INT32:
431-
case PT_UINT32:
432-
case PT_FLAGS32:
433-
case PT_ENUMFLAGS32:
434-
case PT_UID:
435-
case PT_GID:
436-
case PT_MODE:
437-
return (char *)&default_uint32;
438-
case PT_INT64:
439-
case PT_UINT64:
440-
case PT_RELTIME:
441-
case PT_ABSTIME:
442-
case PT_ERRNO:
443-
case PT_FD:
444-
case PT_PID:
445-
return (char *)&default_uint64;
446-
// Should be ok to return NULL since the len will be 0
447-
case PT_BYTEBUF:
448-
case PT_CHARBUF:
449-
case PT_SOCKADDR:
450-
case PT_SOCKTUPLE:
451-
case PT_FDLIST:
452-
case PT_FSPATH:
453-
case PT_CHARBUFARRAY:
454-
case PT_CHARBUF_PAIR_ARRAY:
455-
case PT_FSRELPATH:
456-
case PT_DYN:
457-
return NULL;
458-
default:
459-
// We forgot to handle something
460-
ASSERT(false);
461-
break;
462-
}
463-
return 0;
464-
}
465-
466413
// This should be only used for testing purposes in production we should use directly `memcmp` for
467414
// the whole event
468415
bool scap_compare_events(scap_evt *curr, scap_evt *expected, char *error) {

0 commit comments

Comments
 (0)