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

dlopen: make error handling thread safe #431

Merged
merged 1 commit into from
Nov 3, 2023
Merged

Conversation

Luap99
Copy link
Contributor

@Luap99 Luap99 commented Nov 3, 2023

The dlerror() value is stored in thread local storage and thus checking the error from go code is not safe because go can schedule goroutines on any other thread at any time. This means it is possible that between the dlsym() or dlclose() call the code get moved to another thread and thus the dlerror() value is not what we expect. Instead because we read now from another thread it will be an error from a different call.

A test is added which without this fix is able to reproduce the problem somewhat reliably.

This issue was observed in the podman CI[1] which uses the sdjournal API.

[1] containers/podman#20569

The dlerror() value is stored in thread local storage and thus checking
the error from go code is not safe because go can schedule goroutines
on any other thread at any time. This means it is possible that between
the dlsym() or dlclose() call the code get moved to another thread and
thus the dlerror() value is not what we expect. Instead because we read
now from another thread it will be an error from a different call.

A test is added which without this fix is able to reproduce the
problem somewhat reliably.

This issue was observed in the podman CI[1] which uses the sdjournal
API.

[1] containers/podman#20569

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
@Luap99
Copy link
Contributor Author

Luap99 commented Nov 3, 2023

Example failure with the new test but without the locked thread:

--- FAIL: TestDlopenThreadSafety (1.69s)
    dlopen_test.go:99: GetSymbolPointer strlen failed unexpectedly: error resolving symbol "strlen": libstrange3.so: cannot open shared object file: No such file or directory
    dlopen_test.go:99: GetSymbolPointer strlen failed unexpectedly: error resolving symbol "strlen": libstrange1.so: cannot open shared object file: No such file or directory
FAIL
FAIL    github.com/coreos/go-systemd/v22/internal/dlopen        1.751s
FAIL

@cgwalters cgwalters merged commit 7d375ec into coreos:main Nov 3, 2023
8 checks passed
@Luap99 Luap99 deleted the dlerror branch November 3, 2023 13:26
@Luap99
Copy link
Contributor Author

Luap99 commented Nov 3, 2023

Thanks for the quick review/merge @cgwalters

@jlebon
Copy link
Member

jlebon commented Nov 3, 2023

Nice digging on this!

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.

3 participants