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

Adding GLOBAL keyword to LOAD action #985

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/core/PlumedMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,38 +1058,42 @@
}
}

void PlumedMain::load(const std::string& ss) {
void PlumedMain::load(const std::string& fileName, const bool loadGlobal) {
if(DLLoader::installed()) {
std::string s=ss;
size_t n=s.find_last_of(".");
std::string libName=fileName;
size_t n=libName.find_last_of(".");
std::string extension="";
std::string base=s;
if(n!=std::string::npos && n<s.length()-1) extension=s.substr(n+1);
if(n!=std::string::npos && n<s.length()) base=s.substr(0,n);
std::string base=libName;
if(n!=std::string::npos && n<libName.length()-1)
extension=libName.substr(n+1);
if(n!=std::string::npos && n<libName.length())
base=libName.substr(0,n);
if(extension=="cpp") {
// full path command, including environment setup
// this will work even if plumed is not in the execution path or if it has been
// installed with a name different from "plumed"
std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"\"/scripts/mklib.sh "+s;
std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"\"/scripts/mklib.sh "+libName;
log<<"Executing: "<<cmd;
if(comm.Get_size()>0) log<<" (only on master node)";
log<<"\n";
if(comm.Get_rank()==0) {
int ret=std::system(cmd.c_str());

Check failure

Code scanning / CodeQL

Uncontrolled data used in OS command Critical

This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
temporary object
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
temporary object
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
temporary object
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
temporary object
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
temporary object
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
temporary object
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
temporary object
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from [user input (an environmen
if(ret!=0) plumed_error() <<"An error happened while executing command "<<cmd<<"\n";
if(ret!=0)
plumed_error() <<"An error happened while executing command "<<cmd<<"\n";
}
comm.Barrier();
base="./"+base;
}
s=base+"."+config::getSoExt();
void *p=dlloader.load(s);
libName=base+"."+config::getSoExt();
void *p=dlloader.load(libName,loadGlobal);
if(!p) {
plumed_error()<<"I cannot load library " << ss << " " << dlloader.error();
plumed_error()<<"I cannot load library " << fileName << " " << dlloader.error();
}
log<<"Loading shared library "<<s.c_str()<<"\n";
log<<"Loading shared library "<<libName.c_str()<<"\n";
log<<"Here is the new list of available actions\n";
log<<actionRegister();
} else plumed_error()<<"While loading library "<< ss << " loading was not enabled, please check if dlopen was found at configure time";
} else
plumed_error()<<"While loading library "<< fileName << " loading was not enabled, please check if dlopen was found at configure time";
}

double PlumedMain::getBias() const {
Expand Down
2 changes: 1 addition & 1 deletion src/core/PlumedMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class PlumedMain:
/// Stop the run
void exit(int c=0);
/// Load a shared library
void load(const std::string&);
void load(const std::string&, bool=false);
/// Get the suffix string
const std::string & getSuffix()const;
/// Set the suffix string
Expand Down
5 changes: 4 additions & 1 deletion src/setup/Load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ PLUMED_REGISTER_ACTION(Load,"LOAD")
void Load::registerKeywords( Keywords& keys ) {
ActionSetup::registerKeywords(keys);
keys.add("compulsory","FILE","file to be loaded");
keys.addFlag("GLOBAL",false,"when selected the shared object is LOADed with RTLD_GLOBAL instead of RTLD_LOCAL");
}

Load::Load(const ActionOptions&ao):
Expand All @@ -114,8 +115,10 @@ Load::Load(const ActionOptions&ao):
{
std::string f;
parse("FILE",f);
bool loadWithGlobal=false;
parseFlag("GLOBAL",loadWithGlobal);
checkRead();
plumed.load(f);
plumed.load(f,loadWithGlobal);
}

}
Expand Down
8 changes: 6 additions & 2 deletions src/tools/DLLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ bool DLLoader::installed() {
}


void* DLLoader::load(const std::string&s) {
void* DLLoader::load(const std::string&s, const bool useGlobal) {
#ifdef __PLUMED_HAS_DLOPEN
void* p=dlopen(s.c_str(),RTLD_NOW|RTLD_LOCAL);
void* p=nullptr;
if (useGlobal)
p=dlopen(s.c_str(),RTLD_NOW|RTLD_GLOBAL);
else
p=dlopen(s.c_str(),RTLD_NOW|RTLD_LOCAL);
if(!p) {
lastError=dlerror();
} else {
Expand Down
18 changes: 9 additions & 9 deletions src/tools/DLLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ namespace PLMD {
class DLLoader {
std::stack<void*> handles;
std::string lastError;
/// Deleted copy constructor
/// Deleted copy constructor
DLLoader(const DLLoader&) = delete;
/// Deleted assignment
/// Deleted assignment
DLLoader&operator=(const DLLoader&) = delete;
public:
/// Default constructor
/// Default constructor
DLLoader();
/// Cleanup
/// Cleanup
~DLLoader();
/// Load a library, returning its handle
void* load(const std::string&);
/// Returns the last error in dynamic loader
/// Load a library, returning its handle
void* load(const std::string&, bool=false);
/// Returns the last error in dynamic loader
const std::string & error();
/// Returns true if the dynamic loader is available (on some systems it may not).
/// Returns true if the dynamic loader is available (on some systems it may not).
static bool installed();
};

}
} // namespace PLMD

#endif