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

A solution for an issue with dlopen #1156

Open
Iximiel opened this issue Nov 18, 2024 · 0 comments
Open

A solution for an issue with dlopen #1156

Iximiel opened this issue Nov 18, 2024 · 0 comments

Comments

@Iximiel
Copy link
Member

Iximiel commented Nov 18, 2024

Hi all,

TL;DR: if you have a strange segfault on getenv calls, try to run plumed after export PLUMED_LOAD_DLCLOSE=no (thanks, @GiovanniBussi )

I was working on the interface to plumed for external codes and I found this curious behaviour:
When you check that plumed is available to use and plumed it is not found the next time you call getenv to look for a non-existent environment variable you get a segmentation fault 😨

Luckily this happens only with a determinate software configuration (this is fun) and can be solved by simply exporting export PLUMED_LOAD_DLCLOSE=no before running the plumed-patched program

The minimal code to reproduce the issue is this spoiler
#define __PLUMED_WRAPPER_FORTRAN 0
#define __PLUMED_WRAPPER_LINK_RUNTIME 1
#define __PLUMED_WRAPPER_EXTERN 0
#define __PLUMED_WRAPPER_CXX 1
#define __PLUMED_WRAPPER_LIBCXX11 1
#define __PLUMED_WRAPPER_LIBCXX17 1
#define __PLUMED_WRAPPER_IMPLEMENTATION 1
#define __PLUMED_HAS_DLOPEN

#include "Plumed.h"
#include <cstdlib>
#include <iostream>

void mygetenv(std::string var) {
    auto val = getenv(var.c_str());
    if (val)
       std::cout << var << ": "<< val << "\n";
    else
       std::cout << var << " not FOUND!!\n";
}

int main() {
    mygetenv("PLUMED_KERNEL");
    mygetenv("PLUMED_KERNEL_");
    std::cout << "Plumed is " << (PLMD::Plumed::installed()?"":"not ") << "installed\n";
    mygetenv("PLUMED_KERNEL");
    mygetenv("PLUMED_KERNEL_");
    return 0;
}
all: test

test: test.o
	g++ -o test test.o -ldl
test.o: test.cpp Plumed.h
	g++ -c test.cpp

Plumed.h:
	wget https://raw.githubusercontent.com/plumed/plumed2/refs/tags/v2.10b/src/wrapper/Plumed.h

clean:
	rm -fv *.o test
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

No branches or pull requests

1 participant