atc_router/ffi/
expression.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use crate::ast::{BinaryOperator, Expression, LogicalExpression, Predicate};
use crate::ffi::ERR_BUF_MAX_LEN;
use crate::schema::Schema;
use bitflags::bitflags;
use std::cmp::min;
use std::ffi;
use std::os::raw::c_char;
use std::slice::from_raw_parts_mut;

use std::iter::Iterator;

struct PredicateIterator<'a> {
    stack: Vec<&'a Expression>,
}

impl<'a> PredicateIterator<'a> {
    fn new(expr: &'a Expression) -> Self {
        Self { stack: vec![expr] }
    }
}

impl<'a> Iterator for PredicateIterator<'a> {
    type Item = &'a Predicate;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(expr) = self.stack.pop() {
            match expr {
                Expression::Logical(l) => match l.as_ref() {
                    LogicalExpression::And(l, r) | LogicalExpression::Or(l, r) => {
                        self.stack.push(l);
                        self.stack.push(r);
                    }
                    LogicalExpression::Not(r) => {
                        self.stack.push(r);
                    }
                },
                Expression::Predicate(p) => return Some(p),
            }
        }
        None
    }
}

impl Expression {
    fn iter_predicates(&self) -> PredicateIterator {
        PredicateIterator::new(self)
    }
}

bitflags! {
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    #[repr(C)]
    pub struct BinaryOperatorFlags: u64 /* We can only have no more than 64 BinaryOperators */ {
        const EQUALS = 1 << 0;
        const NOT_EQUALS = 1 << 1;
        const REGEX = 1 << 2;
        const PREFIX = 1 << 3;
        const POSTFIX = 1 << 4;
        const GREATER = 1 << 5;
        const GREATER_OR_EQUAL = 1 << 6;
        const LESS = 1 << 7;
        const LESS_OR_EQUAL = 1 << 8;
        const IN = 1 << 9;
        const NOT_IN = 1 << 10;
        const CONTAINS = 1 << 11;

        const UNUSED = !(Self::EQUALS.bits()
            | Self::NOT_EQUALS.bits()
            | Self::REGEX.bits()
            | Self::PREFIX.bits()
            | Self::POSTFIX.bits()
            | Self::GREATER.bits()
            | Self::GREATER_OR_EQUAL.bits()
            | Self::LESS.bits()
            | Self::LESS_OR_EQUAL.bits()
            | Self::IN.bits()
            | Self::NOT_IN.bits()
            | Self::CONTAINS.bits());
    }
}

impl From<&BinaryOperator> for BinaryOperatorFlags {
    fn from(op: &BinaryOperator) -> Self {
        match op {
            BinaryOperator::Equals => Self::EQUALS,
            BinaryOperator::NotEquals => Self::NOT_EQUALS,
            BinaryOperator::Regex => Self::REGEX,
            BinaryOperator::Prefix => Self::PREFIX,
            BinaryOperator::Postfix => Self::POSTFIX,
            BinaryOperator::Greater => Self::GREATER,
            BinaryOperator::GreaterOrEqual => Self::GREATER_OR_EQUAL,
            BinaryOperator::Less => Self::LESS,
            BinaryOperator::LessOrEqual => Self::LESS_OR_EQUAL,
            BinaryOperator::In => Self::IN,
            BinaryOperator::NotIn => Self::NOT_IN,
            BinaryOperator::Contains => Self::CONTAINS,
        }
    }
}

pub const ATC_ROUTER_EXPRESSION_VALIDATE_OK: i64 = 0;
pub const ATC_ROUTER_EXPRESSION_VALIDATE_FAILED: i64 = 1;
pub const ATC_ROUTER_EXPRESSION_VALIDATE_BUF_TOO_SMALL: i64 = 2;

/// Validates an ATC expression against a schema and get its elements.
///
/// # Arguments
///
/// - `atc`: a C-style string representing the ATC expression.
/// - `schema`: a valid pointer to a [`Schema`] object, as returned by [`schema_new`].
/// - `fields_buf`: a buffer for storing the fields used in the expression.
/// - `fields_buf_len`: a pointer to the length of `fields_buf`.
/// - `fields_total`: a pointer for storing the total number of unique fields used in the expression.
/// - `operators`: a pointer for storing the bitflags representing used operators.
/// - `errbuf`: a buffer to store any error messages.
/// - `errbuf_len`: a pointer to the length of the error message buffer.
///
/// # Returns
///
/// An integer indicating the validation result:
/// - `ATC_ROUTER_EXPRESSION_VALIDATE_OK` (0): Validation succeeded.
/// - `ATC_ROUTER_EXPRESSION_VALIDATE_FAILED` (1): Validation failed; `errbuf` and `errbuf_len` will be updated with an error message.
/// - `ATC_ROUTER_EXPRESSION_VALIDATE_BUF_TOO_SMALL` (2): The provided `fields_buf` is too small.
///
/// If `fields_buf_len` indicates that `fields_buf` is sufficient, this function writes the used fields to `fields_buf`, each field terminated by `\0`.
/// It stores the total number of fields in `fields_total`.
///
/// If `fields_buf_len` indicates that `fields_buf` is insufficient, it returns `ATC_ROUTER_EXPRESSION_VALIDATE_BUF_TOO_SMALL`.
///
/// It writes the used operators as bitflags to `operators`.
/// Bitflags are defined by `BinaryOperatorFlags` and must exclude bits from `BinaryOperatorFlags::UNUSED`.
///
///
/// # Safety
///
/// Violating any of the following constraints results in undefined behavior:
///
/// - `atc` must be a valid pointer to a C-style string, properly aligned, and must not contain an internal `\0`.
/// - `schema` must be a valid pointer returned by [`schema_new`].
/// - `fields_buf`, must be valid for writing `fields_buf_len * size_of::<u8>()` bytes and properly aligned.
/// - `fields_buf_len` must be a valid pointer to write `size_of::<usize>()` bytes and properly aligned.
/// - `fields_total` must be a valid pointer to write `size_of::<usize>()` bytes and properly aligned.
/// - `operators` must be a valid pointer to write `size_of::<u64>()` bytes and properly aligned.
/// - `errbuf` must be valid for reading and writing `errbuf_len * size_of::<u8>()` bytes and properly aligned.
/// - `errbuf_len` must be a valid pointer for reading and writing `size_of::<usize>()` bytes and properly aligned.

#[no_mangle]
pub unsafe extern "C" fn expression_validate(
    atc: *const u8,
    schema: &Schema,
    fields_buf: *mut u8,
    fields_buf_len: *mut usize,
    fields_total: *mut usize,
    operators: *mut u64,
    errbuf: *mut u8,
    errbuf_len: *mut usize,
) -> i64 {
    use std::collections::HashSet;

    use crate::parser::parse;
    use crate::semantics::Validate;

    let atc = ffi::CStr::from_ptr(atc as *const c_char).to_str().unwrap();
    let errbuf = from_raw_parts_mut(errbuf, ERR_BUF_MAX_LEN);

    // Parse the expression
    let result = parse(atc).map_err(|e| e.to_string());
    if let Err(e) = result {
        let errlen = min(e.len(), *errbuf_len);
        errbuf[..errlen].copy_from_slice(&e.as_bytes()[..errlen]);
        *errbuf_len = errlen;
        return ATC_ROUTER_EXPRESSION_VALIDATE_FAILED;
    }
    // Unwrap is safe since we've already checked for error
    let ast = result.unwrap();

    // Validate expression with schema
    if let Err(e) = ast.validate(schema).map_err(|e| e.to_string()) {
        let errlen = min(e.len(), *errbuf_len);
        errbuf[..errlen].copy_from_slice(&e.as_bytes()[..errlen]);
        *errbuf_len = errlen;
        return ATC_ROUTER_EXPRESSION_VALIDATE_FAILED;
    }

    // Iterate over predicates to get fields and operators
    let mut ops = BinaryOperatorFlags::empty();
    let mut existed_fields = HashSet::new();
    let mut total_fields_length = 0;
    let mut fields_buf_ptr = fields_buf;
    *fields_total = 0;

    for pred in ast.iter_predicates() {
        ops |= BinaryOperatorFlags::from(&pred.op);

        let field = pred.lhs.var_name.as_str();

        if existed_fields.insert(field) {
            // Fields is not existed yet.
            // Unwrap is safe since `field` cannot contain '\0' as `atc` must not contain any internal `\0`.
            let field = ffi::CString::new(field).unwrap();
            let field_slice = field.as_bytes_with_nul();
            let field_len = field_slice.len();

            *fields_total += 1;
            total_fields_length += field_len;

            if *fields_buf_len < total_fields_length {
                return ATC_ROUTER_EXPRESSION_VALIDATE_BUF_TOO_SMALL;
            }

            let fields_buf = from_raw_parts_mut(fields_buf_ptr, field_len);
            fields_buf.copy_from_slice(field_slice);
            fields_buf_ptr = fields_buf_ptr.add(field_len);
        }
    }

    *operators = ops.bits();

    ATC_ROUTER_EXPRESSION_VALIDATE_OK
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::Type;

    fn expr_validate_on(
        schema: &Schema,
        atc: &str,
        fields_buf_size: usize,
    ) -> Result<(Vec<String>, usize, u64), (i64, String)> {
        let atc = ffi::CString::new(atc).unwrap();
        let mut errbuf = vec![b'X'; ERR_BUF_MAX_LEN];
        let mut errbuf_len = ERR_BUF_MAX_LEN;

        let mut fields_buf = vec![0u8; fields_buf_size];
        let mut fields_buf_len = fields_buf.len();
        let mut fields_total = 0;
        let mut operators = 0u64;

        let result = unsafe {
            expression_validate(
                atc.as_bytes().as_ptr(),
                &schema,
                fields_buf.as_mut_ptr(),
                &mut fields_buf_len,
                &mut fields_total,
                &mut operators,
                errbuf.as_mut_ptr(),
                &mut errbuf_len,
            )
        };

        match result {
            ATC_ROUTER_EXPRESSION_VALIDATE_OK => {
                let mut fields = Vec::<String>::with_capacity(fields_total);
                let mut p = 0;
                for _ in 0..fields_total {
                    let field = unsafe { ffi::CStr::from_ptr(fields_buf[p..].as_ptr().cast()) };
                    let len = field.to_bytes().len() + 1;
                    fields.push(field.to_string_lossy().to_string());
                    p += len;
                }
                assert_eq!(fields_buf_len, p, "Fields buffer length mismatch");
                fields.sort();
                Ok((fields, fields_buf_len, operators))
            }
            ATC_ROUTER_EXPRESSION_VALIDATE_FAILED => {
                let err = String::from_utf8(errbuf[..errbuf_len].to_vec()).unwrap();
                Err((result, err))
            }
            ATC_ROUTER_EXPRESSION_VALIDATE_BUF_TOO_SMALL => Err((result, String::new())),
            _ => panic!("Unknown error code"),
        }
    }

    #[test]
    fn test_expression_validate_success() {
        let atc = r##"net.protocol ~ "^https?$" && net.dst.port == 80 && (net.src.ip not in 10.0.0.0/16 || net.src.ip in 10.0.1.0/24) && http.path contains "hello""##;

        let mut schema = Schema::default();
        schema.add_field("net.protocol", Type::String);
        schema.add_field("net.dst.port", Type::Int);
        schema.add_field("net.src.ip", Type::IpAddr);
        schema.add_field("http.path", Type::String);

        let result = expr_validate_on(&schema, atc, 47);

        assert!(result.is_ok(), "Validation failed");
        let (fields, fields_buf_len, ops) = result.unwrap(); // Unwrap is safe since we've already asserted it
        assert_eq!(
            ops,
            (BinaryOperatorFlags::EQUALS
                | BinaryOperatorFlags::REGEX
                | BinaryOperatorFlags::IN
                | BinaryOperatorFlags::NOT_IN
                | BinaryOperatorFlags::CONTAINS)
                .bits(),
            "Operators mismatch"
        );
        assert_eq!(
            fields,
            vec![
                "http.path".to_string(),
                "net.dst.port".to_string(),
                "net.protocol".to_string(),
                "net.src.ip".to_string()
            ],
            "Fields mismatch"
        );
        assert_eq!(fields_buf_len, 47, "Fields buffer length mismatch");
    }

    #[test]
    fn test_expression_validate_failed_parse() {
        let atc = r##"net.protocol ~ "^https?$" && net.dst.port == 80 && (net.src.ip not in 10.0.0.0/16 || net.src.ip in 10.0.1.0) && http.path contains "hello""##;

        let mut schema = Schema::default();
        schema.add_field("net.protocol", Type::String);
        schema.add_field("net.dst.port", Type::Int);
        schema.add_field("net.src.ip", Type::IpAddr);
        schema.add_field("http.path", Type::String);

        let result = expr_validate_on(&schema, atc, 1024);

        assert!(result.is_err(), "Validation unexcepted success");
        let (err_code, err_message) = result.unwrap_err(); // Unwrap is safe since we've already asserted it
        assert_eq!(
            err_code, ATC_ROUTER_EXPRESSION_VALIDATE_FAILED,
            "Error code mismatch"
        );
        assert_eq!(
            err_message,
            "In/NotIn operators only supports IP in CIDR".to_string(),
            "Error message mismatch"
        );
    }

    #[test]
    fn test_expression_validate_failed_validate() {
        let atc = r##"net.protocol ~ "^https?$" && net.dst.port == 80 && (net.src.ip not in 10.0.0.0/16 || net.src.ip in 10.0.1.0/24) && http.path contains "hello""##;

        let mut schema = Schema::default();
        schema.add_field("net.protocol", Type::String);
        schema.add_field("net.dst.port", Type::Int);
        schema.add_field("net.src.ip", Type::IpAddr);

        let result = expr_validate_on(&schema, atc, 1024);

        assert!(result.is_err(), "Validation unexcepted success");
        let (err_code, err_message) = result.unwrap_err(); // Unwrap is safe since we've already asserted it
        assert_eq!(
            err_code, ATC_ROUTER_EXPRESSION_VALIDATE_FAILED,
            "Error code mismatch"
        );
        assert_eq!(
            err_message,
            "Unknown LHS field".to_string(),
            "Error message mismatch"
        );
    }

    #[test]
    fn test_expression_validate_buf_too_small() {
        let atc = r##"net.protocol ~ "^https?$" && net.dst.port == 80 && (net.src.ip not in 10.0.0.0/16 || net.src.ip in 10.0.1.0/24) && http.path contains "hello""##;

        let mut schema = Schema::default();
        schema.add_field("net.protocol", Type::String);
        schema.add_field("net.dst.port", Type::Int);
        schema.add_field("net.src.ip", Type::IpAddr);
        schema.add_field("http.path", Type::String);

        let result = expr_validate_on(&schema, atc, 46);

        assert!(result.is_err(), "Validation failed");
        let (err_code, _) = result.unwrap_err(); // Unwrap is safe since we've already asserted it
        assert_eq!(
            err_code, ATC_ROUTER_EXPRESSION_VALIDATE_BUF_TOO_SMALL,
            "Error code mismatch"
        );
    }
}