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

Skip relocations to sections when parsing elf #762

Closed
wants to merge 1 commit into from

Conversation

a-hamza-r
Copy link

@a-hamza-r a-hamza-r commented Oct 27, 2024

When programs contain a section that stores data, e.g., read-only data stored in the .rodata section, relocations to the data show up as relocation to the corresponding section, which results in an "Unresolved external symbol" error. We can skip when a relocation to a section is encountered to avoid such errors. This can be seen for the Cilium benchmark here: bpf_host.o

Summary by CodeRabbit

  • New Features

    • Enhanced error handling and control flow for ELF file processing.
    • Improved relocation processing by skipping irrelevant symbols.
  • Bug Fixes

    • Added runtime error checks for null map section data and invalid sizes.
    • Improved relocation value calculations to accommodate various map formats.
  • Refactor

    • Updated function signature for better input handling.

When programs contain a section that contain data, e.g., read-only data
stored in .rodata section, relocations to the data show up as
relocation to the corresponding section, which results in "Unresolved
external symbol" error. We can skip when a relocation to a section is
encountered to avoid such errors.

Signed-off-by: Ameer Hamza <[email protected]>
Copy link

coderabbitai bot commented Oct 27, 2024

Walkthrough

The changes in this pull request focus on enhancing error handling and control flow in the ELF file processing functions within src/asm_files.cpp. Key updates include modifications to the read_elf function to improve symbol type checks during relocation processing and refined error handling in the parse_map_sections function. Additionally, the relocate_map function has been updated to better calculate relocation values, ensuring compatibility with different map section formats and improving overall robustness.

Changes

File Change Summary
src/asm_files.cpp - Enhanced error handling and control flow in ELF file processing functions.
- Updated read_elf function signature to accept an input stream instead of a path.
- Added checks in parse_map_sections for null data and section size validity.
- Improved relocate_map function to handle relocation values and bounds checking more effectively.

Possibly related PRs

🐇 In the land of code where bunnies hop,
Enhancements made, we’ll never stop!
Error checks and flows refined,
ELF processing now aligned.
With every change, we leap with glee,
A safer world for you and me! 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 16e06cf and 99f385e.

📒 Files selected for processing (1)
  • src/asm_files.cpp (1 hunks)
🔇 Additional comments (1)
src/asm_files.cpp (1)

411-414: Correctly skipping relocations for section symbols

The added check appropriately skips relocations when the symbol type is ELFIO::STT_SECTION. This change prevents processing relocations to sections, addressing the "Unresolved external symbol" errors encountered with data sections, as described in the PR objectives.

src/asm_files.cpp Show resolved Hide resolved
@elazarg
Copy link
Collaborator

elazarg commented Oct 27, 2024

@Alan-Jowett can you review this?

@coveralls
Copy link

Coverage Status

coverage: 90.466% (-0.007%) from 90.473%
when pulling 99f385e on a-hamza-r:fix-unresolved-symbols
into 16e06cf on vbpf:main.

@Alan-Jowett
Copy link
Contributor

@Alan-Jowett can you review this?

Sure, happy to.

@Alan-Jowett
Copy link
Contributor

Can anyone comment on what libbpf does in this scenario? I thought this got translated into a lddw with source register 3 (see https://www.ietf.org/archive/id/draft-ietf-bpf-isa-04.html#section-5.4).

The verifier should have the same support so that downstream VMs can handle this correctly.

@Alan-Jowett
Copy link
Contributor

Can anyone comment on what libbpf does in this scenario? I thought this got translated into a lddw with source register 3 (see https://www.ietf.org/archive/id/draft-ietf-bpf-isa-04.html#section-5.4).

The verifier should have the same support so that downstream VMs can handle this correctly.

The post condition should be that the destination register should contain a pointer to a read-only shared memory region (may need to be a new type?), with a size derived from the symbol information on the ELF. This would result in later code being able to be checked for dereferencing this region and reasoning about it.

Let me know if you want to collaborate on getting this right.

I think just skipping this is the wrong behavior though.

Copy link
Contributor

@Alan-Jowett Alan-Jowett left a comment

Choose a reason for hiding this comment

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

I don't think skipping the relocation is the correct behavior. Take a look at how libbpf handles this.

@Alan-Jowett
Copy link
Contributor

Example of the problem:

E:\ebpf-verifier>Debug\check.exe c:\Users\alanjo\Downloads\bpf_host.o 2/4
Unresolved external symbol  in section 2/4 at location 6
Unresolved external symbol  in section 2/4 at location 8
Unresolved external symbol  in section 2/4 at location 30
Unresolved external symbol  in section 2/4 at location 32
Unresolved external symbol  in section 2/4 at location 131
Unresolved external symbol  in section 2/4 at location 134
Unresolved external symbol  in section 2/4 at location 158
Unresolved external symbol  in section 2/4 at location 161
Unresolved external symbol  in section 2/4 at location 349
Unresolved external symbol  in section 2/4 at location 352
Unresolved external symbol  in section 2/4 at location 376
Unresolved external symbol  in section 2/4 at location 379
error: There are relocations in section but no maps sections in file c:\Users\alanjo\Downloads\bpf_host.o
Make sure to inline all function calls.
C:\Users\alanjo\Downloads>llvm-objdump bpf_host.o  --section=2/4 -Sl | more

bpf_host.o:     file format elf64-bpf

Disassembly of section 2/4:

0000000000000000 <tail_icmp6_handle_ns>:
llvm-objdump.exe: warning: 'bpf_host.o': failed to find source /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h
llvm-objdump.exenat_egress_ipv6():
warning: /hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h:339
':      0:       bf 17 00 00 00 00 00 00 r7 = r1bpf_host.o
failed to find source /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\include/bpf/ctx\skb.h
; __config_ROUTER_IP_1():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\include/bpf/ctx\skb.h:126
       1:       61 72 30 00 00 00 00 00 r2 = *(u32 *)(r7 + 0x30)
; __config_ROUTER_IP_2():
:      2:       b4 01 00 00 00 00 00 00 w1 = 0x0llvm-objdump.exe
; __config_HOST_IP_1():warning:
': /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\include/bpf/ctx\skb.h:120bpf_host.o
failed to find source /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\ipv6.h
       3:       63 17 30 00 00 00 00 00 *(u32 *)(r7 + 0x30) = r1
; __config_HOST_IP_2():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\include/bpf/ctx\skb.h:126
       4:       61 71 34 00 00 00 00 00 r1 = *(u32 *)(r7 + 0x34)
; test_cilium_ipcache():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h:297
       5:       63 1a 70 ff 00 00 00 00 *(u32 *)(r10 - 0x90) = r1
; __config_LXC_IP_1():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h:297
       6:       18 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r8 = 0x0 ll
; __config_LXC_IPV4():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h:297
       8:       18 06 00 00 04 00 00 00 00 00 00 00 00 00 00 00 r6 = 0x4 ll
; __config_SECLABEL_IPV6():
      10:       63 2a 80 ff 00 00 00 00 *(u32 *)(r10 - 0x80) = r2
; test_cilium_throttle():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h:299
      11:       04 02 00 00 30 00 00 00 w2 += 0x30
; __config_ENDPOINT_NETNS_COOKIE():
      12:       bf a3 00 00 00 00 00 00 r3 = r10
; __config_IPV6_MASQUERADE_1():
      13:       07 03 00 00 88 ff ff ff r3 += -0x78
; __config_IPV6_MASQUERADE_2():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h:299
      14:       bf 71 00 00 00 00 00 00 r1 = r7
; ____revalidate_data_pull():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h:299
      15:       b4 04 00 00 10 00 00 00 w4 = 0x10
; LBB21_2():
; /home/hamza/Desktop/ebpf-project/benchmark_repos/cilium/bpf\lib\icmp6.h:299
      16:       85 00 00 00 1a 00 00 00 call 0x1a
; test_cilium_lxc():
      17:       b4 09 00 00 7a ff ff ff w9 = -0x86
; LBB17_2():
C:\Users\alanjo\Downloads>llvm-objdump --reloc bpf_host.o --section .rel2/4

bpf_host.o:     file format elf64-bpf

RELOCATION RECORDS FOR [2/4]:
OFFSET           TYPE                     VALUE
0000000000000030 R_BPF_64_64              .rodata.config
0000000000000040 R_BPF_64_64              .rodata.config
00000000000000f0 R_BPF_64_64              .rodata.config
0000000000000100 R_BPF_64_64              .rodata.config
0000000000000418 R_BPF_64_64              .rodata.config
0000000000000430 R_BPF_64_64              .rodata.config
00000000000004f0 R_BPF_64_64              .rodata.config
0000000000000508 R_BPF_64_64              .rodata.config
00000000000007c0 R_BPF_64_64              test_cilium_lxc
0000000000000ae8 R_BPF_64_64              .rodata.config
0000000000000b00 R_BPF_64_64              .rodata.config
0000000000000bc0 R_BPF_64_64              .rodata.config
0000000000000bd8 R_BPF_64_64              .rodata.config
0000000000000f38 R_BPF_64_64              test_cilium_metrics
0000000000000fc8 R_BPF_64_64              test_cilium_metrics
0000000000000ff0 R_BPF_64_64              test_cilium_calls_65535

So, offset pc=6 -> offset=48 -> 0x30

This plus the disassembly shows this is a 64bit immediate load, with a relocation to the .rodat.config section.

C:\Users\alanjo\Downloads>llvm-objdump bpf_host.o --syms | findstr /i rodata
0000000000000000 l     O .rodata.config 0000000000000004 __config_THIS_INTERFACE_MAC_1
0000000000000004 l     O .rodata.config 0000000000000004 __config_THIS_INTERFACE_MAC_2
0000000000000008 l     O .rodata.config 0000000000000008 __config_ROUTER_IP_1
0000000000000010 l     O .rodata.config 0000000000000008 __config_ROUTER_IP_2
0000000000000050 l     O .rodata.config 0000000000000004 __config_SECLABEL_IPV6
000000000000004c l     O .rodata.config 0000000000000004 __config_SECLABEL_IPV4
0000000000000048 l     O .rodata.config 0000000000000004 __config_SECLABEL
0000000000000068 l     O .rodata.config 0000000000000008 __config_IPV6_MASQUERADE_1
0000000000000070 l     O .rodata.config 0000000000000008 __config_IPV6_MASQUERADE_2
0000000000000028 l     O .rodata.config 0000000000000004 __config_SECCTX_FROM_IPCACHE
0000000000000000 l    d  .rodata.config 0000000000000000 .rodata.config
0000000000000018 l     O .rodata.config 0000000000000008 __config_HOST_IP_1
0000000000000020 l     O .rodata.config 0000000000000008 __config_HOST_IP_2
0000000000000030 l     O .rodata.config 0000000000000008 __config_LXC_IP_1
0000000000000038 l     O .rodata.config 0000000000000008 __config_LXC_IP_2
0000000000000040 l     O .rodata.config 0000000000000004 __config_LXC_IPV4
0000000000000044 l     O .rodata.config 0000000000000002 __config_LXC_ID
0000000000000054 l     O .rodata.config 0000000000000004 __config_POLICY_VERDICT_LOG_FILTER
0000000000000058 l     O .rodata.config 0000000000000004 __config_THIS_INTERFACE_IFINDEX
0000000000000060 l     O .rodata.config 0000000000000008 __config_ENDPOINT_NETNS_COOKIE
C:\Users\alanjo\Downloads>llvm-objdump --section=.rodata.config --full-contents bpf_host.o

bpf_host.o:     file format elf64-bpf
Contents of section .rodata.config:
 0000 efbeadde dec00000 beef0000 00000001  ................
 0010 00000001 00010000 beef0000 00000001  ................
 0020 00000a00 020fffff 01000000 00000000  ................
 0030 beef0000 00000001 00000001 016582bc  .............e..
 0040 40302010 2a000000 ffff0f00 ffff0f00  @0 .*...........
 0050 ffff0f00 ffff0000 00000000 00000000  ................
 0060 ffffffff ffffffff 00000000 00000000  ................
 0070 00000000 00000000                    ........

So, the correct end to end behavior should be:

  1. Replace LDDW with source == 0 with LDDW with source == 3 (similar to the replacement done for maps).
  2. Assume a readonly region of size == size from that symbol.
  3. When generating pre/post conditions assume that this r8 (in this case) points to a region of shared memory of size N, where N is from the symbol info.

The JIT/AOT compiler can then convert this back to a LDDW and embed the address of the corresponding memory region.

For reference, see:
https://github.com/libbpf/libbpf/blob/09b9e83102eb8ab9e540d36b4559c55f3bcdb95d/src/skel_internal.h#L102C1-L123C4

This is turning into a huge PR comment, so I think I will create an issue listing the required behavior.

@Alan-Jowett
Copy link
Contributor

Filed #763 with details on how I think this could be implemented.

@a-hamza-r
Copy link
Author

Thanks @Alan-Jowett for all the effort. I am not super familiar with how maps are initially parsed by the tool, hence the first step (Replace LDDW with source == 0 with LDDW with source == 3) confuses me. I will spend some time trying to understand it. This documentation has been helpful in understanding relocations: https://docs.kernel.org/bpf/llvm_reloc.html

@Alan-Jowett
Copy link
Contributor

Thanks for the link. I would suggest adding this to the issue so it doesn't get lost.

The issue has links to how the code is handling relocation for maps today.

@elazarg
Copy link
Collaborator

elazarg commented Oct 29, 2024

A different approach seems to be needed.

Thanks @a-hamza-r! You are more than welcome to open a new PR if you continue to work on this.

@elazarg elazarg closed this Oct 29, 2024
@a-hamza-r
Copy link
Author

Thanks @elazarg. You're right.

@a-hamza-r a-hamza-r deleted the fix-unresolved-symbols branch November 20, 2024 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants