ENH: add stub function to codedFixedValueFvPatchScalarField for version checking

- eg, fixedValue10_<SHA1>, its existence indicates that the correct
  library has been loaded
This commit is contained in:
Mark Olesen
2011-02-24 08:54:12 +01:00
parent b0e39f71c3
commit 6eb982573c
5 changed files with 81 additions and 28 deletions

View File

@ -35,6 +35,20 @@ ${codeInclude}
namespace Foam
{
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
extern "C"
{
// unique function name that can be checked if the correct library version
// has been loaded
bool ${typeName}_${SHA1sum}()
{
return true;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
${typeName}FixedValueFvPatchScalarField::

View File

@ -35,7 +35,8 @@ License
#include "dlLibraryTable.H"
#include "OSspecific.H"
#include "Time.H"
#include "Pstream.H"
#include "PstreamReduceOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -70,18 +71,11 @@ bool Foam::functionEntries::codeStream::execute
Istream& is
)
{
if (isAdministrator())
{
FatalIOErrorIn
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "This code should not be executed by someone with administrator"
<< " rights due to security reasons." << endl
<< "(it writes a shared library which then gets loaded "
<< "using dlopen)"
<< exit(FatalIOError);
}
codeStreamTools::checkSecurity
(
"functionEntries::codeStream::execute(..)",
parentDict
);
// get code dictionary
// must reference parent for stringOps::expand to work nicely
@ -175,6 +169,7 @@ bool Foam::functionEntries::codeStream::execute
copyFiles[0].file() = fileCsrc;
copyFiles[0].set("codeInclude", codeInclude);
copyFiles[0].set("code", code);
copyFiles[0].set("SHA1sum", sha.str());
List<codeStreamTools::fileAndContent> filesContents(2);
@ -218,8 +213,9 @@ bool Foam::functionEntries::codeStream::execute
}
}
// bool dummy = true;
// reduce(dummy, orOp<bool>());
// all processes must wait for compile
bool dummy = true;
reduce(dummy, orOp<bool>());
if (!dlLibraryTable::open(libPath, false))
{

View File

@ -49,6 +49,28 @@ const Foam::fileName Foam::codeStreamTools::codeTemplateDirName
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
void Foam::codeStreamTools::checkSecurity
(
const char* title,
const dictionary& context
)
{
if (isAdministrator())
{
FatalIOErrorIn
(
title,
context
) << "This code should not be executed by someone with administrator"
<< " rights due to security reasons." << nl
<< "(it writes a shared library which then gets loaded "
<< "using dlopen)"
<< exit(FatalIOError);
}
}
Foam::fileName Foam::codeStreamTools::codePath(const word& subDirName)
{
return stringOps::expand("$FOAM_CASE/codeStream/" + subDirName);
@ -253,6 +275,13 @@ bool Foam::codeStreamTools::copyFilesContents(const fileName& dir) const
// variables mapping
HashTable<string> mapping(copyFiles_[i]);
mapping.set("typeName", name_);
// provide a zero digest if not otherwise specified
if (!mapping.found("SHA1sum"))
{
mapping.insert("SHA1sum", SHA1Digest().str());
}
copyAndExpand(is, os, mapping);
}

View File

@ -145,6 +145,13 @@ public:
// Member functions
//- Check security for creating dynamic code
static void checkSecurity
(
const char* title,
const dictionary& context
);
//- Local path for specified code name
// Expanded from \$FOAM_CASE/codeStream
static fileName codePath(const word& subDirName);

View File

@ -124,7 +124,9 @@ void Foam::codedFixedValueFvPatchScalarField::writeLibrary
// <<"new SHA1: " << sha << endl;
// (void) codeStreamTools::upToDate(codePath, sha)
// only use side-effect of writing SHA1Digest for now
(void) codeStreamTools::upToDate(codePath, sha);
// TODO: compile on-demand
if (true)
{
@ -152,6 +154,7 @@ void Foam::codedFixedValueFvPatchScalarField::writeLibrary
copyFiles[0].file() = fileCsrc;
copyFiles[0].set("codeInclude", codeInclude);
copyFiles[0].set("code", code);
copyFiles[0].set("SHA1sum", sha.str());
copyFiles[1].file() = fileHsrc;
@ -190,18 +193,11 @@ void Foam::codedFixedValueFvPatchScalarField::writeLibrary
void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
{
if (isAdministrator())
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::updateLibrary()",
dict_
) << "This code should not be executed by someone with administrator"
<< " rights due to security reasons." << endl
<< "(it writes a shared library which then gets loaded "
<< "using dlopen)"
<< exit(FatalIOError);
}
codeStreamTools::checkSecurity
(
"codedFixedValueFvPatchScalarField::updateLibrary()",
dict_
);
// write code into redirectType_ subdir
const fileName codePath = codeStreamTools::codePath(redirectType_);
@ -220,6 +216,16 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
void* lib = dlLibraryTable::findLibrary(libPath);
// TODO:
// calculate old/new SHA1 for code
// check if the correct library version was already loaded.
// Find the library handle.
//
// string signatureName(redirectType_ + "_" + sha().str());
// void (*signatureFunction)();
// signatureFunction = reinterpret_cast<void(*)()>(dlSym(lib, signatureName));
if (dict_.found("code"))
{
if (!lib)
@ -274,6 +280,7 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
}
}
// all processes must wait for compile
bool dummy = true;
reduce(dummy, orOp<bool>());