-
Notifications
You must be signed in to change notification settings - Fork 52
feat: add support for LLVM IR files in the linker #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BretasArthur1
wants to merge
4
commits into
aya-rs:main
Choose a base branch
from
BretasArthur1:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−27
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
232e0e1
feat: add support for LLVM IR files in the linker
BretasArthur1 bb10416
refactor: improve input handling in linker and update Cargo.toml depe…
BretasArthur1 6195153
chore: remove unnecessary check
BretasArthur1 c8c6f9a
refactor: change `detect_input_type` to recieve `InputType` as parame…
BretasArthur1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #![expect(unused_crate_dependencies, reason = "used in lib/bin")] | ||
|
|
||
| use std::{ | ||
| env, fs, | ||
| path::{Path, PathBuf}, | ||
| process::Command, | ||
| }; | ||
|
|
||
| fn linker_path() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_bpf-linker")) | ||
| } | ||
|
|
||
| fn create_test_ir_file(dir: &Path, name: &str) -> PathBuf { | ||
| let ir_path = dir.join(format!("{}.ll", name)); | ||
| let ir_content = format!( | ||
| r#"; ModuleID = '{name}' | ||
| source_filename = "{name}" | ||
| target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128" | ||
| target triple = "bpf" | ||
|
|
||
| define i32 @test_{name}(i32 %x) #0 {{ | ||
| entry: | ||
| %result = add i32 %x, 1 | ||
| ret i32 %result | ||
| }} | ||
|
|
||
| attributes #0 = {{ noinline nounwind optnone }} | ||
|
|
||
| !llvm.module.flags = !{{!0}} | ||
| !0 = !{{i32 1, !"wchar_size", i32 4}} | ||
| "# | ||
| ); | ||
| fs::write(&ir_path, ir_content).expect("Failed to write test IR file"); | ||
| ir_path | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_link_ir_file() { | ||
| let temp_dir = tempfile::tempdir().expect("Failed to create temp dir"); | ||
| let ir_file = create_test_ir_file(temp_dir.path(), "alessandro"); | ||
| let output_file = temp_dir.path().join("output.o"); | ||
|
|
||
| let output = Command::new(linker_path()) | ||
| .arg("--export") | ||
| .arg(format!("test_{}", "alessandro")) | ||
| .arg(&ir_file) | ||
| .arg("-o") | ||
| .arg(&output_file) | ||
| .output() | ||
| .expect("Failed to execute bpf-linker"); | ||
|
|
||
| if !output.status.success() { | ||
| eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout)); | ||
| eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr)); | ||
| panic!("bpf-linker failed with status: {}", output.status); | ||
| } | ||
|
|
||
| assert!( | ||
| output_file.exists(), | ||
| "Output file should exist: {:?}", | ||
| output_file | ||
| ); | ||
| assert!( | ||
| output_file.metadata().unwrap().len() > 0, | ||
| "Output file should not be empty" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_invalid_ir_file() { | ||
| let temp_dir = tempfile::tempdir().expect("Failed to create temp dir"); | ||
|
|
||
| let valid_ir_file = create_test_ir_file(temp_dir.path(), "alessandro"); | ||
|
|
||
| let valid_content = fs::read_to_string(valid_ir_file).expect("Failed to read valid IR file"); | ||
|
|
||
| // Corrupting IR content | ||
| let invalid_content = valid_content | ||
| .replace("define", "defXne") | ||
|
||
| .replace("add i32", "adX i32") | ||
| .replace("; ModuleID = 'alessandro'", ": ModuleXX = 'corrupted'"); | ||
|
|
||
| let invalid_ir_file = temp_dir.path().join("corrupted.ll"); | ||
|
|
||
| fs::write(&invalid_ir_file, invalid_content).expect("Failed to write invalid IR file"); | ||
|
|
||
| let output_file = temp_dir.path().join("output.o"); | ||
|
|
||
| let output = Command::new(linker_path()) | ||
| .arg(&invalid_ir_file) | ||
| .arg("-o") | ||
| .arg(&output_file) | ||
| .output() | ||
| .expect("Failed to execute bpf-linker"); | ||
|
|
||
| // Should fail with corrupted IR | ||
| assert!( | ||
| !output.status.success(), | ||
| "bpf-linker should fail with corrupted IR. stderr: {}", | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ); | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're leaking this you need to call LLVMDisposeMemoryBuffer before returning