sbpf-linker 0.1.9

Upstream BPF linker for SBPF V0/V3 programs
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use std::{
    env,
    ffi::CString,
    fs, io,
    path::{Component, Path, PathBuf},
    str::FromStr,
};

use bpf_linker::{
    Cpu, Linker, LinkerInput, LinkerOptions, OptLevel, OutputType,
};
use clap::{
    Parser,
    builder::{PathBufValueParser, TypedValueParser as _},
    error::ErrorKind,
};
use thiserror::Error;
use tracing::{Level, info};
use tracing_subscriber::{EnvFilter, fmt::MakeWriter, prelude::*};
use tracing_tree::HierarchicalLayer;

use sbpf_linker::{SbpfLinkerError, link_program};

#[derive(Debug, Error)]
enum CliError {
    #[error(
        "optimization level needs to be between 0-3, s or z (instead was `{0}`)"
    )]
    InvalidOptimization(String),
    #[error(
        "unknown emission type: `{0}` - expected one of: `llvm-bc`, `asm`, `llvm-ir`, `obj`"
    )]
    InvalidOutputType(String),

    #[error("SBPF Linker Error. Error detail: ({0}).")]
    SbpfLinkerError(#[from] SbpfLinkerError),
    #[error("Program Write Error. Error detail: ({msg}).")]
    ProgramWriteError { msg: String },
}

#[derive(Copy, Clone, Debug)]
struct CliOptLevel(OptLevel);

impl FromStr for CliOptLevel {
    type Err = CliError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(match s {
            "0" => OptLevel::No,
            "1" => OptLevel::Less,
            "2" => OptLevel::Default,
            "3" => OptLevel::Aggressive,
            "s" => OptLevel::Size,
            "z" => OptLevel::SizeMin,
            _ => return Err(CliError::InvalidOptimization(s.to_string())),
        }))
    }
}

#[derive(Copy, Clone, Debug)]
struct CliOutputType(OutputType);

impl FromStr for CliOutputType {
    type Err = CliError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(match s {
            "llvm-bc" => OutputType::Bitcode,
            "asm" => OutputType::Assembly,
            "llvm-ir" => OutputType::LlvmAssembly,
            "obj" => OutputType::Object,
            _ => return Err(CliError::InvalidOutputType(s.to_string())),
        }))
    }
}

fn parent_and_file_name(p: PathBuf) -> anyhow::Result<(PathBuf, PathBuf)> {
    let mut comps = p.components();
    let file_name = comps
        .next_back()
        .map(|p| match p {
            Component::Normal(p) => Ok(p),
            p => Err(anyhow::anyhow!("unexpected path component {:?}", p)),
        })
        .transpose()?
        .ok_or_else(|| anyhow::anyhow!("unexpected empty path"))?;
    let parent = comps.as_path();
    Ok((parent.to_path_buf(), Path::new(file_name).to_path_buf()))
}

#[derive(Debug, Parser)]
#[command(version)]
struct CommandLine {
    /// LLVM target triple. When not provided, the target is inferred from the inputs
    #[clap(long)]
    target: Option<CString>,

    /// Target BPF processor. Can be one of `generic`, `probe`, `v1`, `v2`, `v3`
    /// This will be ignored in sbpf linker since we want to default `cpu` to `v2` but with rustc always passing a `cpu` value
    /// We decide to add one more flag to override it
    #[clap(long, default_value = "generic")]
    cpu: Cpu,

    /// Override the target-cpu attribute to expose the desired CPU features to bpf-linker
    #[clap(long, default_value = "v2")]
    override_cpu_flag: Option<Cpu>,

    /// Enable or disable CPU features. The available features are: alu32, dummy, dwarfris.
    /// LLVM 22 builds also support allows-misaligned-mem-access. Use +feature to enable a
    /// feature, or -feature to disable it. For example
    /// --cpu-features=+allows-misaligned-mem-access,+alu32,-dwarfris
    #[clap(long, value_name = "features", default_value = "")]
    cpu_features: CString,

    /// Write output to <output>
    #[clap(short, long)]
    output: PathBuf,

    /// Output type. Can be one of `llvm-bc`, `asm`, `llvm-ir`, `obj`
    #[clap(long, default_value = "obj")]
    emit: Vec<CliOutputType>,

    /// Emit BTF information. Can get DWARF symbols only if BTF is enabled and if requested from `rustc` with `-C debuginfo=N`
    #[clap(long)]
    btf: bool,

    /// Permit automatic insertion of __bpf_trap calls.
    /// See: https://github.com/llvm/llvm-project/commit/ab391beb11f733b526b86f9df23734a34657d876
    #[clap(long)]
    allow_bpf_trap: bool,

    /// Add a directory to the library search path
    #[clap(short = 'L', number_of_values = 1)]
    _libs: Vec<PathBuf>,

    /// Optimization level. 0-3, s, or z
    #[clap(short = 'O', default_value = "2")]
    optimize: Vec<CliOptLevel>,

    /// Export the symbols specified in the file `path`. The symbols must be separated by new lines
    #[clap(long, value_name = "path")]
    export_symbols: Option<PathBuf>,

    /// Output logs to the given `path`
    #[clap(
        long,
        value_name = "path",
        value_parser = PathBufValueParser::new().try_map(parent_and_file_name),
    )]
    log_file: Option<(PathBuf, PathBuf)>,

    /// Set the log level. If not specified, no logging is used. Can be one of
    /// `error`, `warn`, `info`, `debug`, `trace`.
    #[clap(long, value_name = "level")]
    log_level: Option<Level>,

    /// Try hard to unroll loops. Useful when targeting kernels that don't support loops
    #[clap(long)]
    unroll_loops: bool,

    /// Ignore `noinline`/`#[inline(never)]`. Useful when targeting kernels that don't support function calls
    #[clap(long)]
    ignore_inline_never: bool,

    /// Dump the final IR module to the given `path` before generating the code
    #[clap(long, value_name = "path")]
    dump_module: Option<PathBuf>,

    /// Extra command line arguments to pass to LLVM
    #[clap(long, value_name = "args", use_value_delimiter = true, action = clap::ArgAction::Append)]
    llvm_args: Vec<CString>,

    /// Disable passing --bpf-expand-memcpy-in-order to LLVM.
    #[clap(long, default_value_t = true, hide = true, action = clap::ArgAction::Set)]
    disable_expand_memcpy_in_order: bool,

    /// Disable exporting memcpy, memmove, memset, memcmp and bcmp. Exporting
    /// those is commonly needed when LLVM does not manage to expand memory
    /// intrinsics to a sequence of loads and stores.
    #[clap(long)]
    disable_memory_builtins: bool,

    /// Input files. Can be object files or static libraries
    #[clap(required = true)]
    inputs: Vec<PathBuf>,

    /// Comma separated list of symbols to export. See also `--export-symbols`
    #[clap(long, value_name = "symbols", use_value_delimiter = true, action = clap::ArgAction::Append)]
    export: Vec<String>,

    /// Whether to treat LLVM errors as fatal.
    #[clap(long, action = clap::ArgAction::Set, default_value_t = true)]
    fatal_errors: bool,

    /// The options below are for wasm-ld compatibility
    #[clap(long = "debug", hide = true)]
    _debug: bool,

    /// Strips the `lib` prefix from the output file and places it in the `target/deploy` directory for deployment
    #[clap(long, default_value_t = true, hide = true, action = clap::ArgAction::Set)]
    deploy: bool,
}

/// Returns a [`HierarchicalLayer`](tracing_tree::HierarchicalLayer) for the
/// given `writer`.
fn tracing_layer<W>(writer: W) -> HierarchicalLayer<W>
where
    W: for<'writer> MakeWriter<'writer> + 'static,
{
    const TRACING_IDENT: usize = 2;
    HierarchicalLayer::new(TRACING_IDENT)
        .with_indent_lines(true)
        .with_writer(writer)
}

fn process_cli_options<I>(args: I) -> anyhow::Result<CommandLine>
where
    I: Iterator<Item = String>,
{
    let cli: CommandLine = match Parser::try_parse_from(args) {
        Ok(cli) => cli,
        Err(err) => match err.kind() {
            ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => {
                print!("{err}");
                return Err(err.into());
            }
            _ => return Err(err.into()),
        },
    };
    let mut cpu_features = cli.cpu_features;

    let misalignment_bytes = b"allows-misaligned-mem-access";
    if !cpu_features
        .as_bytes()
        .windows(misalignment_bytes.len())
        .any(|w| w == misalignment_bytes)
    {
        let mut bytes = cpu_features.into_bytes();
        if !bytes.is_empty() {
            bytes.push(b',');
        }

        bytes.extend_from_slice(b"+allows-misaligned-mem-access");
        cpu_features = CString::new(bytes).unwrap();
    }

    let mut llvm_args = cli.llvm_args;
    if !llvm_args
        .iter()
        .any(|arg| arg.as_bytes().starts_with(b"-bpf-stack-size"))
    {
        llvm_args.push(CString::new("-bpf-stack-size=4096").unwrap());
    }

    let cpu = cli.override_cpu_flag.unwrap();

    Ok(CommandLine {
        target: cli.target,
        override_cpu_flag: cli.override_cpu_flag,
        cpu,
        cpu_features,
        output: cli.output,
        emit: cli.emit,
        btf: cli.btf,
        allow_bpf_trap: cli.allow_bpf_trap,
        _libs: cli._libs,
        optimize: cli.optimize,
        export_symbols: cli.export_symbols,
        log_file: cli.log_file,
        log_level: cli.log_level,
        unroll_loops: cli.unroll_loops,
        ignore_inline_never: cli.ignore_inline_never,
        dump_module: cli.dump_module,
        llvm_args,
        disable_expand_memcpy_in_order: cli.disable_expand_memcpy_in_order,
        disable_memory_builtins: cli.disable_memory_builtins,
        inputs: cli.inputs,
        export: cli.export,
        fatal_errors: cli.fatal_errors,
        _debug: cli._debug,
        deploy: cli.deploy,
    })
}

fn main() -> anyhow::Result<()> {
    let args = env::args().map(|arg| {
        if arg == "-flavor" { "--flavor".to_string() } else { arg }
    });

    let cli = process_cli_options(args)?;

    let CommandLine {
        cpu,
        cpu_features,
        target,
        output,
        btf,
        allow_bpf_trap,
        export_symbols,
        log_file,
        log_level,
        llvm_args,
        unroll_loops,
        ignore_inline_never,
        dump_module,
        disable_expand_memcpy_in_order,
        disable_memory_builtins,
        inputs,
        export,
        fatal_errors,
        deploy,
        ..
    } = cli;

    let _guard = {
        let filter = EnvFilter::from_default_env();
        let filter = match log_level {
            None => filter,
            Some(log_level) => filter.add_directive(log_level.into()),
        };
        let subscriber_registry = tracing_subscriber::registry().with(filter);
        match log_file {
            Some((parent, file_name)) => {
                let file_appender =
                    tracing_appender::rolling::never(parent, file_name);
                let (non_blocking, guard) =
                    tracing_appender::non_blocking(file_appender);
                let subscriber = subscriber_registry
                    .with(tracing_layer(io::stdout))
                    .with(tracing_layer(non_blocking));
                tracing::subscriber::set_global_default(subscriber)?;
                Some(guard)
            }
            None => {
                let subscriber =
                    subscriber_registry.with(tracing_layer(io::stderr));
                tracing::subscriber::set_global_default(subscriber)?;
                None
            }
        }
    };

    info!("command line: {:?}", env::args().collect::<Vec<_>>().join(" "));

    let export_symbols = export_symbols.map(fs::read_to_string).transpose()?;

    let export_symbols = export_symbols
        .as_deref()
        .into_iter()
        .flat_map(str::lines)
        .chain(export.iter().map(String::as_str));

    let output_type = match *cli.emit.as_slice() {
        [] => unreachable!("emit has a default value"),
        [CliOutputType(output_type), ..] => output_type,
    };

    let optimize = match *cli.optimize.as_slice() {
        [] => unreachable!("optimize has a default value"),
        [.., CliOptLevel(optimize)] => optimize,
    };

    let mut linker = Linker::new(LinkerOptions {
        target,
        cpu,
        cpu_features,
        optimize,
        unroll_loops,
        ignore_inline_never,
        llvm_args,
        disable_expand_memcpy_in_order,
        disable_memory_builtins,
        btf,
        allow_bpf_trap,
    });

    if let Some(path) = dump_module {
        linker.set_dump_module_path(path);
    }

    let inputs =
        inputs.iter().map(|p| LinkerInput::new_from_file(p.as_path()));

    linker.link_to_file(inputs, &output, output_type, export_symbols)?;

    print!("{:?}", output);

    if fatal_errors && linker.has_errors() {
        return Err(anyhow::anyhow!(
            "LLVM issued diagnostic with error severity"
        ));
    }

    let program = std::fs::read(&output).unwrap();
    let bytecode = link_program(&program)?;

    let src_name = std::path::Path::new(&output)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("main");

    let output_path = std::path::Path::new(&output)
        .parent()
        .unwrap_or_else(|| std::path::Path::new("."))
        .join(format!("{src_name}.so"));
    std::fs::write(&output_path, &bytecode)
        .map_err(|e| CliError::ProgramWriteError { msg: e.to_string() })?;

    // Remove "lib" from the artifact and put it in target/deploy
    if deploy {
        let final_object = src_name.strip_prefix("lib").unwrap_or(src_name);
        let deploy_path = PathBuf::from("target").join("deploy");
        std::fs::create_dir_all(&deploy_path).map_err(|e| {
            CliError::ProgramWriteError {
                msg: format!("failed to create deploy directory: {e}"),
            }
        })?;
        let deploy_file = deploy_path.join(format!("{final_object}.so"));
        std::fs::write(&deploy_file, &bytecode).map_err(|e| {
            CliError::ProgramWriteError {
                msg: format!("failed to write deploy artifact: {e}"),
            }
        })?;
    }

    Ok(())
}

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

    #[test]
    fn test_export_input_args() {
        let args = [
            "sbpf-linker",
            "--export",
            "foo",
            "--export",
            "bar",
            "symbols.o",
            "rcgu.o",
            "-L",
            "target/debug/deps",
            "-L",
            "target/debug",
            "-L",
            "/home/foo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib",
            "-o",
            "/tmp/bin.s",
            "--target=bpf",
            "--emit=asm",
        ]
        .into_iter()
        .map(|s| s.to_string());
        let CommandLine {
            cpu,
            disable_expand_memcpy_in_order,
            deploy,
            cpu_features,
            llvm_args,
            ..
        } = process_cli_options(args).unwrap();
        assert!(matches!(cpu, Cpu::V2));
        assert!(disable_expand_memcpy_in_order);
        assert!(deploy);

        assert_eq!(cpu_features.to_bytes(), b"+allows-misaligned-mem-access");
        assert!(
            llvm_args
                .iter()
                .any(|a| a.to_str().unwrap() == "-bpf-stack-size=4096")
        );
    }

    #[test]
    fn test_explicit_overrides_of_default_flags() {
        let args = [
            "sbpf-linker",
            "input.o",
            "-o",
            "/tmp/bin.so",
            "--override-cpu-flag=v3",
            "--emit=llvm-ir",
            "--deploy=false",
            "--fatal-errors=false",
            "--disable-expand-memcpy-in-order=false",
        ]
        .into_iter()
        .map(|s| s.to_string());
        let CommandLine {
            cpu,
            emit,
            deploy,
            fatal_errors,
            disable_expand_memcpy_in_order,
            output,
            ..
        } = process_cli_options(args).unwrap();

        assert!(matches!(cpu, Cpu::V3));
        assert_eq!(emit.len(), 1);
        assert!(matches!(emit[0], CliOutputType(OutputType::LlvmAssembly)));
        assert!(!deploy);
        assert!(!fatal_errors);
        assert!(!disable_expand_memcpy_in_order);
        assert_eq!(output, PathBuf::from("/tmp/bin.so"));
    }

    #[test]
    fn test_boolean_and_optional_flags() {
        let args = [
            "sbpf-linker",
            "input.o",
            "-o",
            "/tmp/bin.o",
            "--target=bpfel-unknown-none",
            "--btf",
            "--allow-bpf-trap",
            "--unroll-loops",
            "--override-cpu-flag=v1",
            "--ignore-inline-never",
            "--disable-memory-builtins",
            "--log-level=debug",
            "--export-symbols=/tmp/exports.txt",
            "--dump-module=/tmp/module.ll",
        ]
        .into_iter()
        .map(|s| s.to_string());
        let CommandLine {
            cpu,
            target,
            btf,
            allow_bpf_trap,
            unroll_loops,
            ignore_inline_never,
            disable_memory_builtins,
            log_level,
            export_symbols,
            dump_module,
            inputs,
            ..
        } = process_cli_options(args).unwrap();

        assert_eq!(
            target.as_deref().map(|t| t.to_bytes()),
            Some(b"bpfel-unknown-none".as_slice())
        );
        assert!(btf);
        assert!(allow_bpf_trap);
        assert!(unroll_loops);
        assert!(matches!(cpu, Cpu::V1));
        assert!(ignore_inline_never);
        assert!(disable_memory_builtins);
        assert_eq!(log_level, Some(Level::DEBUG));
        assert_eq!(export_symbols, Some(PathBuf::from("/tmp/exports.txt")));
        assert_eq!(dump_module, Some(PathBuf::from("/tmp/module.ll")));
        assert_eq!(inputs, vec![PathBuf::from("input.o")]);
    }

    #[test]
    fn test_misalignment_feature_not_duplicated_when_already_present() {
        let args = [
            "sbpf-linker",
            "input.o",
            "-o",
            "/tmp/bin.o",
            "--cpu-features=-allows-misaligned-mem-access,+alu32",
        ]
        .into_iter()
        .map(|s| s.to_string());
        let CommandLine { cpu_features, .. } =
            process_cli_options(args).unwrap();

        assert_eq!(
            cpu_features.to_bytes(),
            b"-allows-misaligned-mem-access,+alu32"
        );
    }

    #[test]
    fn test_override_cpu() {
        let args = [
            "sbpf-linker",
            "input.o",
            "-o",
            "/tmp/bin.o",
            "--cpu=v1",
            "--override-cpu-flag=v3",
        ]
        .into_iter()
        .map(|s| s.to_string());
        let CommandLine { cpu, .. } = process_cli_options(args).unwrap();
        assert!(matches!(cpu, Cpu::V3));
    }

    #[test]
    fn test_cpu_flag_is_ignored_without_override() {
        let args = ["sbpf-linker", "input.o", "-o", "/tmp/bin.o", "--cpu=v3"]
            .into_iter()
            .map(|s| s.to_string());
        let CommandLine { cpu, .. } = process_cli_options(args).unwrap();
        assert!(matches!(cpu, Cpu::V2));
    }
}