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

Clean NWNX binaries from LD_PRELOAD. #1711

Merged
merged 1 commit into from
Dec 4, 2023

Conversation

jhett12321
Copy link
Contributor

@jhett12321 jhett12321 commented Dec 4, 2023

NWNX is loaded/injected on linux by specifying the "LD_PRELOAD" environment variable before starting the server.

This has the unintended side effect of causing all child processes spawned by NWNX plugins to inherit LD_PRELOAD and attempt to preload NWNX_Core.so again, causing these processes to immediately crash

If NWNX attempts to load outside of the nwn process, it will immediately throw a linker error:

symbol lookup error: /nwn/nwnx/NWNX_Core.so: undefined symbol: _ZN11CAppManager12CreateServerEv

This PR adds a workaround that will remove all entries with a NWNX_ prefix from the process level environment variables.
User and system environment variables should remain untouched.

@mtijanic mtijanic merged commit 51162c5 into nwnxee:master Dec 4, 2023
4 checks passed
Comment on lines +149 to +162
void NWNXCore::CleanupPreload()
{
const auto* preload = std::getenv("LD_PRELOAD");
const std::regex regex("(([^: ]+)?NWNX_[^: ]+)");

std::string newPreload = preload;
while (std::regex_search(newPreload, regex))
{
newPreload = std::regex_replace(newPreload, regex, "");
}

setenv("LD_PRELOAD", newPreload.c_str(), true);
}

Copy link

Choose a reason for hiding this comment

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

I think, that such global changes in user-supplied environment requires some comment about reasons, because this is a very non-standard actions

Copy link
Member

Choose a reason for hiding this comment

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

Hi, you're right, sorry, it's easy to forget about context when you've been freshly discussing it elsewhere. For the record, the issue is that sub-processes launched by any nwnx plugin will inherit LD_PRELOAD and attempt to preload NWNX_Core.so. This only works when preloaded onto nwn (nwmain or nwserver), and other process startups will fail.
@jhett12321 can you please add a comment here as well? Maybe something in the readme too if you find a good place for it.

@jhett12321 jhett12321 deleted the clean-ld-preload branch December 5, 2023 16:47
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