[PATCH] D155894: [BPF] Do not miscompile, allow external calls

Tamir Duberstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 24 16:51:27 PDT 2023


tamird added a comment.

Yeah, it's open source. All the code is in https://github.com/aya-rs/bpf-linker, and in particular the `LLVMTargetMachineEmitToFile` call is https://github.com/aya-rs/bpf-linker/blob/28bfade2a4c44911a8813b418e3bdfd39b9e3b6a/src/llvm/mod.rs#L279-L299

I've been mucking with this locally. I wrote a simple C program, that loads the IR and calls `LLVMTargetMachineEmitToFile`, and I can confirm that I *do not* see any warnings. Very strange. Here's the code: https://gist.github.com/tamird/e180a08d00a6572d95ae2035594ddd1e

I also modified `bpf-linker` to cycle the module to disk and back, and while I still see the error, the number of times it is emitted goes from 6 to 1 (!!).

  diff
  diff --git a/src/llvm/mod.rs b/src/llvm/mod.rs
  index 4cc4181..f2e66fe 100644
  --- a/src/llvm/mod.rs
  +++ b/src/llvm/mod.rs
  @@ -11,6 +11,7 @@ use std::{
   
   use libc::c_char as libc_char;
   use llvm_sys::bit_reader::*;
  +use llvm_sys::ir_reader::*;
   use llvm_sys::core::*;
   use llvm_sys::debuginfo::LLVMStripModuleDebugInfo;
   use llvm_sys::linker::LLVMLinkModules2;
  @@ -47,6 +48,7 @@ unsafe fn parse_command_line_options<T: AsRef<str>>(args: &[T], overview: &str)
           .collect::<Vec<_>>();
       let c_ptrs = c_args.iter().map(|s| s.as_ptr()).collect::<Vec<_>>();
       let overview = CString::new(overview).unwrap();
  +    error!("args={:?}, overview={:?}", c_args, overview);
       LLVMParseCommandLineOptions(c_ptrs.len() as i32, c_ptrs.as_ptr(), overview.as_ptr());
   }
   
  @@ -284,6 +286,28 @@ pub unsafe fn codegen(
   ) -> Result<(), String> {
       let mut message = Message::new();
   
  +    let path = "/home/tamird/src/aya/test/integration-ebpf/module.ll";
  +            // dump IR for the final linked module for debugging purposes
  +            let path = CString::new(path).unwrap();
  +
  +        write_ir(module, path.as_c_str()).unwrap();
  +
  +                    let mut buffer = ptr::null_mut();                                                 
  +            let ret = unsafe { LLVMCreateMemoryBufferWithContentsOfFile(path.as_ptr(), &mut buffer, ptr::null_mut()) };
  +            assert_eq!(ret, 0);                                                                                                                                                              
  +                                         
  +            let mut module = ptr::null_mut();
  +            let ret = unsafe { LLVMParseIRInContext(LLVMGetGlobalContext(), buffer, &mut module, ptr::null_mut()) };
  +            assert_eq!(ret, 0);                                                                                                                                                              
  +                                                                                                                                                                                             
  +            error!("buffer={:p}", buffer);    
  +
  +            let triple = "bpfel-unknown-none";
  +            let target = target_from_triple(CString::new(triple).unwrap().as_c_str()).unwrap();
  +            let tm = create_target_machine(target, triple, "", "").unwrap();
  +
       if LLVMTargetMachineEmitToFile(

Before:

  23:45:03 [ERROR] llvm: <unknown>:0:0: in function test_log i32 (ptr): A call to built-in function 'memset' is not supported.
   
   23:45:03 [ERROR] llvm: <unknown>:0:0: in function test_log i32 (ptr): A call to built-in function 'memset' is not supported.
   
   23:45:03 [ERROR] llvm: <unknown>:0:0: in function test_log i32 (ptr): A call to built-in function 'memset' is not supported.
   
   23:45:03 [ERROR] llvm: <unknown>:0:0: in function test_log i32 (ptr): A call to built-in function 'memset' is not supported.
   
   23:45:03 [ERROR] llvm: <unknown>:0:0: in function test_log i32 (ptr): A call to built-in function 'memset' is not supported.
   
   23:45:03 [ERROR] llvm: <unknown>:0:0: in function test_log i32 (ptr): A call to built-in function 'memset' is not supported.

After:

  23:51:04 [ERROR] buffer=0x5582fe80f5b0
  error: <unknown>:0:0: in function test_log i32 (ptr): A call to built-in function 'memset' is not supported.

I do not understand what's going on here.

  


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155894/new/

https://reviews.llvm.org/D155894



More information about the llvm-commits mailing list