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

link/executable: lazy load symbol table #991

Merged
merged 1 commit into from
Mar 30, 2023

Conversation

mmat11
Copy link
Contributor

@mmat11 mmat11 commented Mar 28, 2023

@mmat11 mmat11 force-pushed the matt/lazy-load-symbols branch 2 times, most recently from d173315 to 056a6af Compare March 28, 2023 17:55
@@ -34,6 +35,9 @@ type Executable struct {
path string
// Parsed ELF and dynamic symbols' addresses.
addresses map[string]uint64

This comment was marked as resolved.

@mmat11 mmat11 force-pushed the matt/lazy-load-symbols branch from 056a6af to dd22162 Compare March 28, 2023 19:59
link/uprobe.go Outdated Show resolved Hide resolved
@@ -83,32 +86,10 @@ func OpenExecutable(path string) (*Executable, error) {
return nil, fmt.Errorf("path cannot be empty")
}

f, err := os.Open(path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't return an error when the given path doesn't exist, which seems suboptimal. We could stat the file, but then we'd get a TOCTOU because we later on need to Open and the binary might have changed. Does that matter? Seems like the tracefs api only deals with paths as well, which implies it has the same problem. Is there a way to avoid paths with tracefs?

One workaround would be to still Open here and put *os.File into Executable. But then we leak *os.File. Do we need Executable.Close()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about this yesterday :D

We don't return an error when the given path doesn't exist, which seems suboptimal. We could stat the file, but then we'd get a TOCTOU because we later on need to Open and the binary might have changed. Does that matter? Seems like the tracefs api only deals with paths as well, which implies it has the same problem. Is there a way to avoid paths with tracefs?

I think pmu has the same problem (perf event is opened on the given executable path), so if the binary changes it will break anyway (or, if the check has never been done, it will fail regardless)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, okay. And there is no API that takes an FD?

I'd still prefer to stat here in addition to your current approach so that we return an error early.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, okay. And there is no API that takes an FD?

https://man7.org/linux/man-pages/man2/perf_event_open.2.html this is the API we are using :D (ctrl+f uprobe_path)

I'd still prefer to stat here in addition to your current approach so that we return an error early.

ok, I'll add that check early

ex, err := OpenExecutable("/bin/bash")
c.Assert(err, qt.IsNil)
// Addresses must be empty, will be lazy loaded.
c.Assert(ex.addresses, qt.CmpEquals(), map[string]uint64{})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh, isn't CmpEquals() the same as Equals?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CmpEquals uses go-cmp's .Diff() while Equals uses ==

Copy link
Collaborator

@lmb lmb Mar 29, 2023

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍🏻 looks better

@mmat11 mmat11 force-pushed the matt/lazy-load-symbols branch from dd22162 to cd7bd95 Compare March 29, 2023 10:59
@mmat11 mmat11 force-pushed the matt/lazy-load-symbols branch from cd7bd95 to e176c3b Compare March 29, 2023 12:11
@lmb lmb merged commit a056764 into cilium:master Mar 30, 2023
@lmb
Copy link
Collaborator

lmb commented Mar 30, 2023

Thanks for the quick turn around! I've reworded your commit message to include context from the issue and the PR review.

@avivzgroundcover
Copy link

You people are awesome 👏 Thanks a lot for the quick change!

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