From cc845dccdbc8e0b9890ec7bba5c84913c9ab718a Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Thu, 12 Jun 2025 15:43:08 +0200 Subject: [PATCH] rust: bindgen GenericVarFree Ticket: 7667 --- rust/src/applayer.rs | 21 +++++++++++++++------ rust/src/core.rs | 18 ------------------ rust/sys/src/sys.rs | 13 +++++++++++++ src/bindgen.h | 1 + src/detect.c | 2 +- src/flow-bit.c | 22 +++++++++++----------- src/flow-util.h | 4 ++-- src/host-bit.c | 2 +- src/ippair-bit.c | 2 +- src/rust-context.c | 2 -- src/rust-context.h | 2 -- src/util-var.c | 10 +++++----- src/util-var.h | 4 +++- 13 files changed, 53 insertions(+), 50 deletions(-) diff --git a/rust/src/applayer.rs b/rust/src/applayer.rs index 45ec6dd6d4..c7a434c60e 100644 --- a/rust/src/applayer.rs +++ b/rust/src/applayer.rs @@ -18,7 +18,7 @@ //! Parser registration functions and common interface module. use std; -use crate::core::{self,AppLayerEventType, GenericVar, STREAM_TOSERVER}; +use crate::core::{self,AppLayerEventType, STREAM_TOSERVER}; use crate::direction::Direction; use crate::filecontainer::FileContainer; use crate::flow::Flow; @@ -29,9 +29,14 @@ use crate::core::StreamingBufferConfig; // Make the AppLayerEvent derive macro available to users importing // AppLayerEvent from this module. pub use suricata_derive::AppLayerEvent; -use suricata_sys::sys::{AppLayerParserState, AppProto, DetectEngineState, AppLayerDecoderEvents}; +use suricata_sys::sys::{ + AppLayerDecoderEvents, AppLayerParserState, AppProto, DetectEngineState, GenericVar, +}; #[cfg(not(test))] -use suricata_sys::sys::{SCDetectEngineStateFree, SCAppLayerDecoderEventsFreeEvents, SCAppLayerDecoderEventsSetEventRaw}; +use suricata_sys::sys::{ + SCAppLayerDecoderEventsFreeEvents, SCAppLayerDecoderEventsSetEventRaw, SCDetectEngineStateFree, + SCGenericVarFree, +}; /// Cast pointer to a variable, as a mutable reference to an object /// @@ -171,24 +176,28 @@ pub unsafe extern "C" fn SCAppLayerTxDataCleanup(txd: *mut AppLayerTxData) { } impl AppLayerTxData { + #[cfg(not(test))] pub fn cleanup(&mut self) { - #[cfg(not(test))] if !self.de_state.is_null() { unsafe { SCDetectEngineStateFree(self.de_state); } } - #[cfg(not(test))] if !self.events.is_null() { unsafe { SCAppLayerDecoderEventsFreeEvents(&mut self.events); } } if !self.txbits.is_null() { - core::sc_generic_var_free(self.txbits); + unsafe { + SCGenericVarFree(self.txbits); + } } } + #[cfg(test)] + pub fn cleanup(&mut self) {} + /// Create new AppLayerTxData for a transaction that covers both /// directions. pub fn new() -> Self { diff --git a/rust/src/core.rs b/rust/src/core.rs index 621942014d..d127bb4a9f 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -25,9 +25,6 @@ use suricata_sys::sys::SCAppLayerParserTriggerRawStreamInspection; use crate::filecontainer::*; use crate::flow::Flow; -/// Opaque C types. -pub enum GenericVar {} - #[repr(C)] #[derive(Debug, PartialEq, Eq, Clone, Copy)] #[allow(non_camel_case_types)] @@ -122,9 +119,6 @@ pub type SCFileContainerRecycle = extern "C" fn ( file_container: &FileContainer, sbcfg: &StreamingBufferConfig); -pub type GenericVarFreeFunc = - extern "C" fn(gvar: *mut GenericVar); - // A Suricata context that is passed in from C. This is alternative to // using functions from Suricata directly, so they can be wrapped so // Rust unit tests will still compile when they are not linked @@ -145,8 +139,6 @@ pub struct SuricataContext { pub FileAppendData: SCFileAppendDataById, pub FileAppendGAP: SCFileAppendGAPById, pub FileContainerRecycle: SCFileContainerRecycle, - - GenericVarFree: GenericVarFreeFunc, } #[allow(non_snake_case)] @@ -176,16 +168,6 @@ pub extern "C" fn SCRustInit(context: &'static SuricataContext) init_ffi(context); } -/// GenericVarFree wrapper. -pub fn sc_generic_var_free(gvar: *mut GenericVar) -{ - unsafe { - if let Some(c) = SC { - (c.GenericVarFree)(gvar); - } - } -} - /// SCAppLayerParserTriggerRawStreamInspection wrapper #[cfg(not(test))] pub(crate) fn sc_app_layer_parser_trigger_raw_stream_inspection(flow: *mut Flow, direction: i32) { diff --git a/rust/sys/src/sys.rs b/rust/sys/src/sys.rs index 2e55b3d0d3..bd04c3a7d8 100644 --- a/rust/sys/src/sys.rs +++ b/rust/sys/src/sys.rs @@ -846,6 +846,19 @@ extern "C" { extern "C" { pub fn SCFileFlowFlagsToFlags(flow_file_flags: u16, direction: u8) -> u16; } +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct GenericVar_ { + #[doc = "< variable type, uses detection sm_type"] + pub type_: u16, + pub pad: [u8; 2usize], + pub idx: u32, + pub next: *mut GenericVar_, +} +pub type GenericVar = GenericVar_; +extern "C" { + pub fn SCGenericVarFree(arg1: *mut GenericVar); +} extern "C" { pub fn SCFlowGetLastTimeAsParts(flow: *const Flow, secs: *mut u64, usecs: *mut u64); } diff --git a/src/bindgen.h b/src/bindgen.h index 02b0312928..fd1fe044c0 100644 --- a/src/bindgen.h +++ b/src/bindgen.h @@ -54,6 +54,7 @@ #include "util-mpm.h" #include "util-file.h" +#include "util-var.h" #include "flow-bindgen.h" diff --git a/src/detect.c b/src/detect.c index a86d99973a..31b55c305e 100644 --- a/src/detect.c +++ b/src/detect.c @@ -984,7 +984,7 @@ static DetectRunScratchpad DetectRunSetup(const DetectEngineCtx *de_ctx, pflow->sgh_toclient = NULL; pflow->de_ctx_version = de_ctx->version; - GenericVarFree(pflow->flowvar); + SCGenericVarFree(pflow->flowvar); pflow->flowvar = NULL; DetectEngineStateResetTxs(pflow); diff --git a/src/flow-bit.c b/src/flow-bit.c index f481220e64..4cf9a6bf13 100644 --- a/src/flow-bit.c +++ b/src/flow-bit.c @@ -161,7 +161,7 @@ static int FlowBitTest01 (void) FlowBit *fb = FlowBitGet(&f, 0); FAIL_IF_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -173,7 +173,7 @@ static int FlowBitTest02 (void) FlowBit *fb = FlowBitGet(&f, 0); FAIL_IF_NOT_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -192,7 +192,7 @@ static int FlowBitTest03 (void) fb = FlowBitGet(&f, 0); FAIL_IF_NOT_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -209,7 +209,7 @@ static int FlowBitTest04 (void) FlowBit *fb = FlowBitGet(&f, 0); FAIL_IF_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -226,7 +226,7 @@ static int FlowBitTest05 (void) FlowBit *fb = FlowBitGet(&f, 1); FAIL_IF_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -243,7 +243,7 @@ static int FlowBitTest06 (void) FlowBit *fb = FlowBitGet(&f, 2); FAIL_IF_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -260,7 +260,7 @@ static int FlowBitTest07 (void) FlowBit *fb = FlowBitGet(&f, 3); FAIL_IF_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -282,7 +282,7 @@ static int FlowBitTest08 (void) fb = FlowBitGet(&f, 0); FAIL_IF_NOT_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -304,7 +304,7 @@ static int FlowBitTest09 (void) fb = FlowBitGet(&f, 1); FAIL_IF_NOT_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -326,7 +326,7 @@ static int FlowBitTest10 (void) fb = FlowBitGet(&f, 2); FAIL_IF_NOT_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } @@ -348,7 +348,7 @@ static int FlowBitTest11 (void) fb = FlowBitGet(&f, 3); FAIL_IF_NOT_NULL(fb); - GenericVarFree(f.flowvar); + SCGenericVarFree(f.flowvar); PASS; } diff --git a/src/flow-util.h b/src/flow-util.h index 4af2e4eafe..65ca475e12 100644 --- a/src/flow-util.h +++ b/src/flow-util.h @@ -111,7 +111,7 @@ (f)->thread_id[1] = 0; \ (f)->sgh_toserver = NULL; \ (f)->sgh_toclient = NULL; \ - GenericVarFree((f)->flowvar); \ + SCGenericVarFree((f)->flowvar); \ (f)->flowvar = NULL; \ RESET_COUNTERS((f)); \ } while (0) @@ -121,7 +121,7 @@ FlowCleanupAppLayer((f)); \ \ FLOWLOCK_DESTROY((f)); \ - GenericVarFree((f)->flowvar); \ + SCGenericVarFree((f)->flowvar); \ } while (0) /** \brief check if a memory alloc would fit in the memcap diff --git a/src/host-bit.c b/src/host-bit.c index 1940fbd13c..e03293aa09 100644 --- a/src/host-bit.c +++ b/src/host-bit.c @@ -43,7 +43,7 @@ static HostStorageId host_bit_id = { .id = -1 }; /**< Host storage id for bits * static void HostBitFreeAll(void *store) { GenericVar *gv = store; - GenericVarFree(gv); + SCGenericVarFree(gv); } void HostBitInitCtx(void) diff --git a/src/ippair-bit.c b/src/ippair-bit.c index 5252912223..5173b9312b 100644 --- a/src/ippair-bit.c +++ b/src/ippair-bit.c @@ -43,7 +43,7 @@ static IPPairStorageId g_ippair_bit_storage_id = { .id = -1 }; /**< IPPair stora static void XBitFreeAll(void *store) { GenericVar *gv = store; - GenericVarFree(gv); + SCGenericVarFree(gv); } void IPPairBitInitCtx(void) diff --git a/src/rust-context.c b/src/rust-context.c index bd077a2d8a..475fc65e9f 100644 --- a/src/rust-context.c +++ b/src/rust-context.c @@ -35,8 +35,6 @@ const SuricataContext suricata_context = { FileAppendDataById, FileAppendGAPById, FileContainerRecycle, - - GenericVarFree, }; const SuricataContext *SCGetContext(void) diff --git a/src/rust-context.h b/src/rust-context.h index 6f02110a08..dd8a35ea27 100644 --- a/src/rust-context.h +++ b/src/rust-context.h @@ -52,8 +52,6 @@ typedef struct SuricataContext_ { int (*FileAppendGAPById)(FileContainer *, const StreamingBufferConfig *, uint32_t track_id, const uint8_t *data, uint32_t data_len); void (*FileContainerRecycle)(FileContainer *ffc, const StreamingBufferConfig *); - - void (*GenericVarFree)(GenericVar *); } SuricataContext; extern const SuricataContext suricata_context; diff --git a/src/util-var.c b/src/util-var.c index 6ef3fe638f..af14a792c5 100644 --- a/src/util-var.c +++ b/src/util-var.c @@ -45,7 +45,7 @@ void XBitFree(XBit *fb) SCFree(fb); } -void GenericVarFree(GenericVar *gv) +void SCGenericVarFree(GenericVar *gv) { if (gv == NULL) return; @@ -57,14 +57,14 @@ void GenericVarFree(GenericVar *gv) case DETECT_FLOWBITS: { FlowBit *fb = (FlowBit *)gv; - //printf("GenericVarFree: fb %p, removing\n", fb); + // printf("SCGenericVarFree: fb %p, removing\n", fb); FlowBitFree(fb); break; } case DETECT_XBITS: { XBit *fb = (XBit *)gv; - //printf("GenericVarFree: fb %p, removing\n", fb); + // printf("SCGenericVarFree: fb %p, removing\n", fb); XBitFree(fb); break; } @@ -86,13 +86,13 @@ void GenericVarFree(GenericVar *gv) } default: { - SCLogDebug("GenericVarFree unknown type %" PRIu32, gv->type); + SCLogDebug("SCGenericVarFree unknown type %" PRIu32, gv->type); DEBUG_VALIDATE_BUG_ON(1); break; } } - GenericVarFree(next_gv); + SCGenericVarFree(next_gv); } void GenericVarAppend(GenericVar **list, GenericVar *gv) diff --git a/src/util-var.h b/src/util-var.h index 498a1f0cd0..7f4e7bfe2d 100644 --- a/src/util-var.h +++ b/src/util-var.h @@ -57,6 +57,7 @@ typedef struct GenericVar_ { struct GenericVar_ *next; } GenericVar; +#ifndef SURICATA_BINDGEN_H typedef struct XBit_ { uint16_t type; /* type, DETECT_XBITS in this case */ uint8_t pad[2]; @@ -66,6 +67,7 @@ typedef struct XBit_ { } XBit; void XBitFree(XBit *); +#endif // A list of variables we try to resolve while parsing configuration file. // Helps to detect recursive declarations. @@ -76,7 +78,7 @@ typedef struct ResolvedVariable_ { typedef TAILQ_HEAD(, ResolvedVariable_) ResolvedVariablesList; -void GenericVarFree(GenericVar *); +void SCGenericVarFree(GenericVar *); void GenericVarAppend(GenericVar **, GenericVar *); void GenericVarRemove(GenericVar **, GenericVar *);