Skip to content

Commit 5094053

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

File tree

6 files changed

+46
-79
lines changed

6 files changed

+46
-79
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: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ limitations under the License.
1717
*/
1818

1919
#include <driver/ppm_events_public.h>
20-
#include <converter/types.h>
21-
#include <converter/results.h>
2220
#include <converter/table.h>
21+
#include <converter/results.h>
2322
#include <converter/debug_macro.h>
2423
#include <stdarg.h>
2524
#include <cstdio>
2625
#include <cassert>
27-
#include <unordered_map>
2826
#include <string>
2927
#include <stdexcept>
3028
#include <memory>
@@ -124,7 +122,6 @@ static void push_default_parameter(scap_evt *evt, uint16_t *params_offset, uint8
124122
// Otherwise we will access the wrong entry in the event table.
125123
const struct ppm_event_info *event_info = &(g_event_info[evt->type]);
126124
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);
128125
uint16_t lens_offset = sizeof(scap_evt) + param_num * sizeof(uint16_t);
129126

130127
PRINT_MESSAGE(
@@ -136,8 +133,11 @@ static void push_default_parameter(scap_evt *evt, uint16_t *params_offset, uint8
136133
*params_offset,
137134
lens_offset);
138135

139-
// If value is NULL, the len should be 0
140-
memcpy((char *)evt + *params_offset, ptr, len);
136+
// The default param will be always 0 so we just need to copy the right number of 0 bytes.
137+
// `uint64_t` should be enough for all the types considering that types like CHARBUF, BYTEBUF
138+
// have `len==0`
139+
uint64_t val = 0;
140+
memcpy((char *)evt + *params_offset, (char *)&val, len);
141141
*params_offset += len;
142142
memcpy((char *)evt + lens_offset, &len, sizeof(uint16_t));
143143
}
@@ -242,7 +242,7 @@ extern "C" void scap_clear_converter_storage() {
242242

243243
static conversion_result convert_event(scap_evt *new_evt,
244244
scap_evt *evt_to_convert,
245-
const conversion_info *ci,
245+
const conversion_info &ci,
246246
char *error) {
247247
/////////////////////////////
248248
// Dispatch the action
@@ -251,7 +251,7 @@ static conversion_result convert_event(scap_evt *new_evt,
251251
uint16_t params_offset = 0;
252252
int param_to_populate = 0;
253253

254-
switch(ci->action) {
254+
switch(ci.action) {
255255
case C_ACTION_SKIP:
256256
return CONVERSION_SKIP;
257257

@@ -262,22 +262,22 @@ static conversion_result convert_event(scap_evt *new_evt,
262262
case C_ACTION_ADD_PARAMS:
263263
memcpy(new_evt, evt_to_convert, sizeof(scap_evt));
264264
// The new number of params is the previous one plus the number of conversion instructions.
265-
new_evt->nparams = evt_to_convert->nparams + ci->instr.size();
265+
new_evt->nparams = evt_to_convert->nparams + ci.instr.size();
266266
params_offset = copy_old_params(new_evt, evt_to_convert);
267267
param_to_populate = evt_to_convert->nparams;
268268
break;
269269

270270
case C_ACTION_CHANGE_TYPE:
271271
memcpy(new_evt, evt_to_convert, sizeof(scap_evt));
272272
// The new number of params is the number of conversion instructions.
273-
new_evt->nparams = ci->instr.size();
274-
new_evt->type = ci->desired_type;
273+
new_evt->nparams = ci.instr.size();
274+
new_evt->type = ci.desired_type;
275275
params_offset = sizeof(scap_evt) + new_evt->nparams * sizeof(uint16_t);
276276
param_to_populate = 0;
277277
break;
278278

279279
default:
280-
snprintf(error, SCAP_LASTERR_SIZE, "Unhandled conversion action '%d'.", ci->action);
280+
snprintf(error, SCAP_LASTERR_SIZE, "Unhandled conversion action '%d'.", ci.action);
281281
return CONVERSION_ERROR;
282282
}
283283

@@ -293,10 +293,10 @@ static conversion_result convert_event(scap_evt *new_evt,
293293
bool used_enter_event = false;
294294

295295
// We iterate over the instructions
296-
for(int i = 0; i < ci->instr.size(); i++, param_to_populate++) {
296+
for(int i = 0; i < ci.instr.size(); i++, param_to_populate++) {
297297
PRINT_MESSAGE("Instruction n° %d. Param to populate: %d\n", i, param_to_populate);
298298

299-
switch(ci->instr[i].flags) {
299+
switch(ci.instr[i].flags) {
300300
case C_INSTR_FROM_DEFAULT:
301301
tmp_evt = NULL;
302302
break;
@@ -329,14 +329,14 @@ static conversion_result convert_event(scap_evt *new_evt,
329329

330330
case C_INSTR_FROM_OLD:
331331
tmp_evt = evt_to_convert;
332-
if(tmp_evt->nparams <= ci->instr[i].param_num) {
332+
if(tmp_evt->nparams <= ci.instr[i].param_num) {
333333
// todo!: this sounds like an error but let's see in the future. At the moment we
334334
// fail
335335
snprintf(error,
336336
SCAP_LASTERR_SIZE,
337337
"We want to take parameter '%d' from event '%d' but this event has only "
338338
"'%d' parameters!",
339-
ci->instr[i].param_num,
339+
ci.instr[i].param_num,
340340
tmp_evt->type,
341341
tmp_evt->nparams);
342342
return CONVERSION_ERROR;
@@ -347,8 +347,8 @@ static conversion_result convert_event(scap_evt *new_evt,
347347
snprintf(error,
348348
SCAP_LASTERR_SIZE,
349349
"Unknown instruction (flags: %d, param_num: %d).",
350-
ci->instr[i].flags,
351-
ci->instr[i].param_num);
350+
ci.instr[i].flags,
351+
ci.instr[i].param_num);
352352
return CONVERSION_ERROR;
353353
}
354354

@@ -359,7 +359,7 @@ static conversion_result convert_event(scap_evt *new_evt,
359359
tmp_evt,
360360
&params_offset,
361361
param_to_populate,
362-
ci->instr[i].param_num);
362+
ci.instr[i].param_num);
363363
}
364364
}
365365

@@ -400,5 +400,5 @@ extern "C" conversion_result scap_convert_event(scap_evt *new_evt,
400400
}
401401

402402
// If we reached this point we have for sure an entry in the conversion table.
403-
return convert_event(new_evt, evt_to_convert, &g_conversion_table[conv_key], error);
403+
return convert_event(new_evt, evt_to_convert, g_conversion_table.at(conv_key), error);
404404
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
/*
3+
Copyright (C) 2024 The Falco Authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
*/
18+
19+
#include <libscap/scap_const.h>
20+
#include <driver/ppm_events_public.h>
21+
#include <converter/table.h>
22+
23+
const std::unordered_map<conversion_key, conversion_info> g_conversion_table = {};

userspace/libscap/engine/savefile/converter/table.h

Lines changed: 2 additions & 4 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+
1819
#pragma once
1920

2021
#include <converter/types.h>
21-
#include <libscap/scap_const.h>
22-
#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+
extern const 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)