ecc-rs 0.3.3

A compiler to produce ebpf programs that can be run by ecli
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//!  SPDX-License-Identifier: MIT
//!
//! Copyright (c) 2023, eunomia-bpf
//! All rights reserved.
//!
extern crate clang;
use std::path::Path;
use std::result::Result::Ok;

use crate::config::*;
use anyhow::anyhow;
use anyhow::Result;
use clang::{documentation::CommentChild, *};
use serde_json::{json, Value};

fn parse_source_files<'a>(
    index: &'a Index<'a>,
    args: &'a Options,
    source_path: &'a str,
) -> Result<TranslationUnit<'a>> {
    let bpf_sys_include = get_bpf_sys_include(&args.compile_opts)?;
    let target_arch = get_target_arch(&args.compile_opts)?;
    let target_arch = String::from("-D__TARGET_ARCH_") + &target_arch;
    let eunomia_include = get_eunomia_include(args)?;
    let base_dir_include = get_base_dir_include(source_path)?;
    let mut compile_args = vec![
        "-g",
        "-O2",
        "-target bpf",
        "-Wno-unknown-attributes ",
        &target_arch,
    ];
    compile_args.append(&mut bpf_sys_include.split(' ').collect::<Vec<&str>>());
    compile_args.append(&mut eunomia_include.split(' ').collect::<Vec<&str>>());
    compile_args.push(&base_dir_include);
    compile_args.append(
        &mut args
            .compile_opts
            .parameters
            .additional_cflags
            .split(' ')
            .collect::<Vec<&str>>(),
    );

    // Parse a source file into a translation unit
    let tu = index
        .parser(source_path)
        .arguments(&compile_args)
        .parse()
        .unwrap();
    Ok(tu)
}

fn process_comment_child(child: CommentChild, value: &mut Value, default_cmd: &str) {
    match child {
        CommentChild::Paragraph(text) => {
            let mut cmd = String::from(default_cmd);
            for child in text {
                match child {
                    CommentChild::InlineCommand(command) => {
                        cmd = command.command;
                    }
                    _ => {
                        process_comment_child(child, value, &cmd);
                    }
                }
            }
        }
        CommentChild::Text(text) => {
            let text = text.trim();
            // skip blank text
            if text.is_empty() {
                return;
            }
            value[&default_cmd] = match serde_json::from_str::<Value>(text) {
                Ok(v) => v,
                Err(_) => {
                    // if text is not json, use it as string
                    print!("warning: text is not json: {}", text);
                    println!(" use it as a string");
                    json!(&text)
                }
            };
        }
        CommentChild::InlineCommand(command) => {
            value[&command.command] = json!(true);
        }
        CommentChild::BlockCommand(command) => {
            let mut text = String::default();
            for child in command.children {
                if let CommentChild::Text(t) = child {
                    text.push_str(&t);
                    text.push('\n');
                }
            }
            text = text.trim().to_string();
            value[&command.command] = json!(text);
        }
        _ => {}
    }
}

/// reslove bpf global variables
fn resolve_section_data_entities(entities: &Vec<Entity>, data_sec: &mut Value) {
    for var in data_sec["variables"].as_array_mut().unwrap() {
        for e in entities {
            if e.get_kind() != EntityKind::VarDecl {
                continue;
            }
            let name = if let Some(name) = e.get_name() {
                name
            } else {
                continue;
            };
            if name != var["name"].as_str().unwrap() {
                continue;
            }
            if let Some(comment) = e.get_parsed_comment() {
                let children = comment.get_children();
                for child in children {
                    process_comment_child(child, var, "description");
                }
            }
        }
    }
}

/// reslove bpf maps comments
fn resolve_map_entities(entities: &Vec<Entity>, map: &mut Value) {
    let mut last_var_entity = None;
    for e in entities {
        if e.get_kind() != EntityKind::VarDecl || last_var_entity.is_none() {
            last_var_entity = Some(e);
            continue;
        }
        let name = if let Some(name) = e.get_name() {
            name
        } else {
            last_var_entity = Some(e);
            continue;
        };
        if name != map["name"].as_str().unwrap() {
            last_var_entity = Some(e);
            continue;
        }
        // for maps like:
        //
        // /// @sample {"interval": 1000}
        // struct {
        //     __uint(type, BPF_MAP_TYPE_HASH);
        //     __uint(max_entries, 8192);
        //     __type(key, pid_t);
        //     __type(value, u64);
        // } exec_start SEC(".maps");
        //
        // we need to extract comments from the type declaration
        if let Some(comment) = last_var_entity.unwrap().get_parsed_comment() {
            let children = comment.get_children();
            for child in children {
                process_comment_child(child, map, "description");
            }
        }
        last_var_entity = Some(e);
    }
}

/// reslove bpf progs comments
fn resolve_progs_entities(entities: &Vec<Entity>, progs: &mut Value) {
    for e in entities {
        if e.get_kind() != EntityKind::FunctionDecl {
            continue;
        }
        let name = if let Some(name) = e.get_name() {
            name
        } else {
            continue;
        };
        if name != progs["name"].as_str().unwrap() {
            continue;
        }
        if let Some(comment) = e.get_parsed_comment() {
            let children = comment.get_children();
            for child in children {
                process_comment_child(child, progs, "description");
            }
        }
    }
}

/// parse the doc in LICENSE comment
fn resolve_doc_entities(entities: &Vec<Entity>, skel_json: &mut Value) {
    for e in entities {
        if e.get_kind() != EntityKind::VarDecl {
            continue;
        }
        let name = if let Some(name) = e.get_name() {
            name
        } else {
            continue;
        };
        if name != "LICENSE" {
            continue;
        }
        if let Some(comment) = e.get_parsed_comment() {
            let children = comment.get_children();
            let mut value = json!({});
            for child in children {
                process_comment_child(child, &mut value, "description");
            }
            skel_json["doc"] = value;
        }
    }
}

fn resolve_bpf_skel_entities(entities: &Vec<Entity>, bpf_skel_json: Value) -> Result<Value> {
    let mut new_skel_json = bpf_skel_json;
    if let Some(data_secs) = new_skel_json["data_sections"].as_array_mut() {
        // resolve comments for section data
        for data_sec in data_secs {
            resolve_section_data_entities(entities, data_sec);
        }
    }
    if let Some(map_secs) = new_skel_json["maps"].as_array_mut() {
        // resolve comments for section maps
        for map_sec in map_secs {
            resolve_map_entities(entities, map_sec);
        }
    }
    if let Some(progs_secs) = new_skel_json["progs"].as_array_mut() {
        // resolve comments for section progs
        for progs_sec in progs_secs {
            resolve_progs_entities(entities, progs_sec);
        }
    }
    resolve_doc_entities(entities, &mut new_skel_json);
    Ok(new_skel_json)
}

/// Get documentations from source file
pub fn parse_source_documents(
    args: &Options,
    source_path: &str,
    bpf_skel_json: Value,
) -> Result<Value> {
    // Acquire an instance of `Clang`
    let clang = match Clang::new() {
        Ok(clang) => clang,
        Err(e) => {
            return Err(anyhow!("Failed to create Clang instance: {}", e));
        }
    };
    // Create a new `Index`
    let index = Index::new(&clang, false, true);
    let _source_path = Path::new(source_path);
    let canonic_source_path = _source_path.canonicalize().unwrap();
    let tu = parse_source_files(&index, args, canonic_source_path.to_str().unwrap())?;

    // Get the entities in this translation unit
    let entities = tu
        .get_entity()
        .get_children()
        .into_iter()
        .filter(|e| {
            if let Some(location) = e.get_location() {
                if let Some(file) = location.get_file_location().file {
                    if file.get_path() == canonic_source_path {
                        return true;
                    }
                }
            }
            false
        })
        .collect::<Vec<_>>();

    // reslove comments for section data and functions, maps
    // find the entity with the same name as the names in the json skeleton
    let new_skel_json = resolve_bpf_skel_entities(&entities, bpf_skel_json)?;
    Ok(new_skel_json)
}

#[cfg(test)]
mod test {
    use std::{fs, path::Path};

    use serde_json::json;
    use tempfile::TempDir;

    use crate::config::{init_eunomia_workspace, CompileOptions, Options};

    use super::parse_source_documents;

    const TEMP_EUNOMIA_DIR: &str = "/tmp/eunomia";

    fn create_args() -> Options {
        let tmp_workspace = TempDir::new().unwrap();
        init_eunomia_workspace(&tmp_workspace).unwrap();
        let tmp_dir = Path::new(TEMP_EUNOMIA_DIR);
        let tmp_dir = tmp_dir.join("test_compile_bpf");
        fs::create_dir_all(&tmp_dir).unwrap();
        fs::write(
            tmp_dir.join("other_header.h"),
            include_str!("../test/other_header.h"),
        )
        .unwrap();
        let source_path = tmp_dir.join("client.bpf.c");
        fs::write(&source_path, include_str!("../test/client.bpf.c")).unwrap();
        let event_path = tmp_dir.join("event.h");
        fs::write(&event_path, include_str!("../test/event.h")).unwrap();
        Options {
            tmpdir: tmp_workspace,
            compile_opts: CompileOptions {
                source_path: source_path.to_str().unwrap().to_string(),
                ..Default::default()
            },
        }
    }

    #[test]
    fn test_parse_variables() {
        let args = create_args();

        let test_case_res = json!({
            "name": ".rodata",
            "variables": [
                {
                    "description": "min duration for a process to be considered",
                    "name": "min_duration_ns",
                    "type": "unsigned long long"
                },
                {
                    "cmdarg":{
                        "default": 0,
                        "short": "p",
                        "long": "pid",
                    },
                    "description": "target pid to trace",
                    "name": "target_pid",
                    "type": "int"
                }
            ]
        });
        let skel = parse_source_documents(
            &args,
            args.compile_opts.source_path.as_str(),
            json!({"data_sections": [
                {
                    "name": ".rodata",
                    "variables": [
                        {
                            "name": "min_duration_ns",
                            "type": "unsigned long long"
                        },
                        {
                            "name": "target_pid",
                            "type": "int"
                        }
                    ]
                }
            ],
            "maps":[], "progs":[]}),
        );
        let skel = match skel {
            Ok(skel) => skel,
            Err(e) => {
                if e.to_string()
                    != "Failed to create Clang instance: an instance of `Clang` already exists"
                {
                    panic!("failed to parse source documents: {}", e);
                }
                return;
            }
        };
        let rodata = &skel["data_sections"][0];
        assert_eq!(rodata, &test_case_res);
    }

    #[test]
    fn test_parse_progss() {
        let args = create_args();
        let test_case_res = json!([{
            "attach": "tp/sched/sched_process_exec",
            "link": true,
            "name": "handle_exec",
            "flag":"called when a process starts"
        },
        {
            "attach": "tp/sched/sched_process_exit",
            "description": "called when a process ends",
            "link": true,
            "name": "handle_exit",
        }]);
        let skel = parse_source_documents(
            &args,
            args.compile_opts.source_path.as_str(),
            json!({"progs": [
                {
                    "attach": "tp/sched/sched_process_exec",
                    "link": true,
                    "name": "handle_exec"
                },
                {
                    "attach": "tp/sched/sched_process_exit",
                    "link": true,
                    "name": "handle_exit"
                }
            ],"maps":[], "data_sections":[]}),
        );
        let skel = match skel {
            Ok(skel) => skel,
            Err(e) => {
                if e.to_string()
                    != "Failed to create Clang instance: an instance of `Clang` already exists"
                {
                    panic!("failed to parse source documents: {}", e);
                }
                return;
            }
        };
        let handle_exec = &skel["progs"];
        assert_eq!(handle_exec, &test_case_res);
    }

    #[test]
    fn test_parse_maps() {
        let test_case_res = json!({
            "ident": "exec_start",
            "name": "exec_start",
            "sample": {"interval": 1000}
        });
        let args = create_args();
        let skel = parse_source_documents(
            &args,
            args.compile_opts.source_path.as_str(),
            json!({"maps": [
                {
                    "ident": "exec_start",
                    "name": "exec_start",
                }
            ], "progs":[], "data_sections":[]}),
        );
        let skel = match skel {
            Ok(skel) => skel,
            Err(e) => {
                if e.to_string()
                    != "Failed to create Clang instance: an instance of `Clang` already exists"
                {
                    panic!("failed to parse source documents: {}", e);
                }
                return;
            }
        };
        let exec_start = &skel["maps"][0];
        assert_eq!(exec_start, &test_case_res);
    }

    #[test]
    fn test_parse_empty() {
        let args = create_args();
        let _ = parse_source_documents(&args, args.compile_opts.source_path.as_str(), json!({}));
    }
}