Skip to content
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

aya: add symbol lookup in associated debug files #938

Merged
merged 4 commits into from
May 9, 2024

Conversation

swananan
Copy link
Contributor

This change enhances the logic for symbol lookup in uprobe or uretprobe. If the symbol is not found in the original binary, the search continues in the debug file associated through the debuglink section. Before searching the symbol table, it compares the build IDs of the two files. The symbol lookup will only be terminated if both build IDs exist and do not match. This modification does not affect the existing symbol lookup logic.

Refs: #936

Copy link

netlify bot commented Apr 23, 2024

Deploy Preview for aya-rs-docs failed.

Built without sensitive environment variables

Name Link
🔨 Latest commit e6e1bfe
🔍 Latest deploy log https://app.netlify.com/sites/aya-rs-docs/deploys/663b5067d474480007525aed

@mergify mergify bot added the aya This is about aya (userspace) label Apr 23, 2024
@alessandrod
Copy link
Collaborator

Thanks! This looks great. Could you add a test for the debuglink resolution?

@alessandrod
Copy link
Collaborator

Thanks! This looks great. Could you add a test for the debuglink resolution?

Actually thinking of it, given our current integration-test setup this is probably harder than it should be. Please take a look but if it's too cumbersome I'm fine just smoke testing this locally for now.

@swananan
Copy link
Contributor Author

Thanks! This looks great. Could you add a test for the debuglink resolution?

Actually thinking of it, given our current integration-test setup this is probably harder than it should be. Please take a look but if it's too cumbersome I'm fine just smoke testing this locally for now.

I was initially planning to add UT, haha. However, I realized I needed to hook the std::fs::read function. If I use the object crate to prepare the testing file in memory (since preparing a real file might be too burdensome), and I'm unsure whether introducing a file mocking crate like fake-fs would be a good idea, especially since I am a new beginner to Rust and Aya.

I took a look at the integration test, but for this particular change, I think UT might be a more suitable choice because it doesn't affect the ebpf code part. If I am mistaken, please correct me.

If it's okay, I will add some UTs using the object and fake-fs crates as soon as possible.

@swananan swananan force-pushed the enhance_urpobe_symbol_lookup branch from c3754a2 to a24aac3 Compare April 24, 2024 10:58
@alessandrod
Copy link
Collaborator

I was initially planning to add UT, haha. However, I realized I needed to hook the std::fs::read function

unit test works! But why do you need to hook std::fs::read?

@swananan
Copy link
Contributor Author

I was initially planning to add UT, haha. However, I realized I needed to hook the std::fs::read function

unit test works! But why do you need to hook std::fs::read?

Since std::fs::read is called by resolve_symbol, I think I need to mock this function. This is because I don't intend to provide a real file path for resolve_symbol to process.

@alessandrod
Copy link
Collaborator

Since std::fs::read is called by resolve_symbol, I think I need to mock this function. This is because I don't intend to provide a real file path for resolve_symbol to process.

You could introduce a new resolve_symbol_data(...) and call it from resolve_symbol. resolve_symbol would fs::read then call resolve_symbol_data with the content. Then you could unit test resolve_symbol_data and not need fs access?

@swananan swananan force-pushed the enhance_urpobe_symbol_lookup branch from a24aac3 to 7e31e1f Compare April 27, 2024 16:22
@addisoncrump
Copy link
Contributor

Super cool. I work a lot with stripped binaries for which I have separate debug symbols, so this will be a real help.

@swananan
Copy link
Contributor Author

swananan commented May 4, 2024

@alessandrod could you review this pr? thanks

@alessandrod
Copy link
Collaborator

@alessandrod could you review this pr? thanks

Yep sorry been travelling this week, will take a look soon

Copy link
Collaborator

@alessandrod alessandrod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quality PR, thanks so much! Just a nit and please fix the lint issue


debug_obj_keeper = Some(debug_obj);
debug_obj_keeper.as_ref().map_or(
Err(ResolveSymbolError::Unknown(symbol.to_string())),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not unwrap here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I totally agree.

@swananan swananan force-pushed the enhance_urpobe_symbol_lookup branch 2 times, most recently from 269e5bf to dea1b64 Compare May 7, 2024 14:35
This change enhances the logic for symbol lookup in uprobe or uretprobe.
If the symbol is not found in the original binary, the search continues
in the debug file associated through the debuglink section. Before
searching the symbol table, it compares the build IDs of the two files.
The symbol lookup will only be terminated if both build IDs exist and do
not match. This modification does not affect the existing symbol lookup
logic.

Refs: aya-rs#936
@swananan swananan force-pushed the enhance_urpobe_symbol_lookup branch from dea1b64 to e6e1bfe Compare May 8, 2024 10:13
@swananan
Copy link
Contributor Author

swananan commented May 8, 2024

Hi @alessandrod this PR failed the public api check phase, but I am not sure if it's the root cause, could you help take a look? thanks!

The equivalent crate has a blanket implementation smh. We don't depend
on it directly it's pulled in by the object crate.
@addisoncrump
Copy link
Contributor

I think you can do cargo xtask public-api --bless or something similar to fix this.

Copy link

mergify bot commented May 8, 2024

Hey @alessandrod, this pull request changes the Aya Public API and requires your review.

@mergify mergify bot added api/needs-review Makes an API change that needs review test A PR that improves test cases or CI labels May 8, 2024
@alessandrod
Copy link
Collaborator

Hi @alessandrod this PR failed the public api check phase, but I am not sure if it's the root cause, could you help take a look? thanks!

I think adding the write feature to the object crate pulls in a new transitive dependency - the equivalent crate. That crate has a blanket impl (smh), which impacts some of our types that we use as keys in maps. I just pushed a commit which hopefully makes CI green.

@alessandrod
Copy link
Collaborator

Can you take a look at the test failure under miri?

The object::File::parse API requires parameter to be aligned with 8 bytes.
Adjusted the Vec in the tests with miri to meet this requirement.
@swananan
Copy link
Contributor Author

swananan commented May 8, 2024

Can you take a look at the test failure under miri?

I looked into this failure and found an exception, Invalid ELF header size or alignment, originating from the object crate. I checked the main_bytes generated by create_elf_with_build_id, and the ELF header size is 64, which seems correct.

However, further investigation into the object crate showed that it has a strict address alignment requirement, leading to a failure in the object::File::parse API (see this link). My understanding is that in Rust, a Vec's memory comes from the heap and should be aligned to 8 bytes on x64 architectures. However, since I'm new to Rust, I'm not entirely certain about this. Obviously, this alignment is not guaranteed with the miri interpreter (this is my first time hearing about miri, haha).

Given the strict limitation in the object crate, I decided to adjust the Vec to ensure 8-byte alignment in the tests as a workaround for miri (I am not sure if it's a good idea = =||). I've added a new commit with these changes. Could you please review it and let me know your thoughts?

@alessandrod alessandrod merged commit bde4b5f into aya-rs:main May 9, 2024
21 checks passed
@alessandrod
Copy link
Collaborator

Yep ELF files must be aligned. Thank you again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api/needs-review Makes an API change that needs review aya This is about aya (userspace) test A PR that improves test cases or CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants