Skip to content

Commit

Permalink
[ORC] Add errors for missing and extraneous symbol definitions.
Browse files Browse the repository at this point in the history
This patch adds new errors and error checking to the ObjectLinkingLayer to
catch cases where a compiled or loaded object either:
(1) Contains definitions not covered by its responsibility set, or
(2) Is missing definitions that are covered by its responsibility set.

Proir to this patch providing the correct set of definitions was treated as
an API contract requirement, however this requires that the client be confident
in the correctness of the whole compiler / object-cache pipeline and results
in difficult-to-debug assertions upon failure. Treating this as a recoverable
error results in clearer diagnostics.

The performance overhead of this check is one comparison of densemap keys
(symbol string pointers) per linking object, which is minimal. If this overhead
ever becomes a problem we can add the check under a flag that can be turned off
if the client fully trusts the rest of the pipeline.
  • Loading branch information
lhames committed Feb 22, 2020
1 parent 56eb15a commit 8172689
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
38 changes: 38 additions & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/Core.h
Expand Up @@ -424,6 +424,44 @@ class SymbolsCouldNotBeRemoved : public ErrorInfo<SymbolsCouldNotBeRemoved> {
SymbolNameSet Symbols;
};

/// Errors of this type should be returned if a module fails to include
/// definitions that are claimed by the module's associated
/// MaterializationResponsibility. If this error is returned it is indicative of
/// a broken transformation / compiler / object cache.
class MissingSymbolDefinitions : public ErrorInfo<MissingSymbolDefinitions> {
public:
static char ID;

MissingSymbolDefinitions(std::string ModuleName, SymbolNameVector Symbols)
: ModuleName(std::move(ModuleName)), Symbols(std::move(Symbols)) {}
std::error_code convertToErrorCode() const override;
void log(raw_ostream &OS) const override;
const std::string &getModuleName() const { return ModuleName; }
const SymbolNameVector &getSymbols() const { return Symbols; }
private:
std::string ModuleName;
SymbolNameVector Symbols;
};

/// Errors of this type should be returned if a module contains definitions for
/// symbols that are not claimed by the module's associated
/// MaterializationResponsibility. If this error is returned it is indicative of
/// a broken transformation / compiler / object cache.
class UnexpectedSymbolDefinitions : public ErrorInfo<UnexpectedSymbolDefinitions> {
public:
static char ID;

UnexpectedSymbolDefinitions(std::string ModuleName, SymbolNameVector Symbols)
: ModuleName(std::move(ModuleName)), Symbols(std::move(Symbols)) {}
std::error_code convertToErrorCode() const override;
void log(raw_ostream &OS) const override;
const std::string &getModuleName() const { return ModuleName; }
const SymbolNameVector &getSymbols() const { return Symbols; }
private:
std::string ModuleName;
SymbolNameVector Symbols;
};

/// Tracks responsibility for materialization, and mediates interactions between
/// MaterializationUnits and JDs.
///
Expand Down
4 changes: 3 additions & 1 deletion llvm/include/llvm/ExecutionEngine/Orc/OrcError.h
Expand Up @@ -37,7 +37,9 @@ enum class OrcErrorCode : int {
UnexpectedRPCCall,
UnexpectedRPCResponse,
UnknownErrorCodeFromRemote,
UnknownResourceHandle
UnknownResourceHandle,
MissingSymbolDefinitions,
UnexpectedSymbolDefinitions,
};

std::error_code orcError(OrcErrorCode ErrCode);
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/Core.cpp
Expand Up @@ -144,6 +144,8 @@ namespace orc {
char FailedToMaterialize::ID = 0;
char SymbolsNotFound::ID = 0;
char SymbolsCouldNotBeRemoved::ID = 0;
char MissingSymbolDefinitions::ID = 0;
char UnexpectedSymbolDefinitions::ID = 0;

RegisterDependenciesFunction NoDependenciesToRegister =
RegisterDependenciesFunction();
Expand Down Expand Up @@ -352,6 +354,24 @@ void SymbolsCouldNotBeRemoved::log(raw_ostream &OS) const {
OS << "Symbols could not be removed: " << Symbols;
}

std::error_code MissingSymbolDefinitions::convertToErrorCode() const {
return orcError(OrcErrorCode::MissingSymbolDefinitions);
}

void MissingSymbolDefinitions::log(raw_ostream &OS) const {
OS << "Missing definitions in module " << ModuleName
<< ": " << Symbols;
}

std::error_code UnexpectedSymbolDefinitions::convertToErrorCode() const {
return orcError(OrcErrorCode::UnexpectedSymbolDefinitions);
}

void UnexpectedSymbolDefinitions::log(raw_ostream &OS) const {
OS << "Unexpected definitions in module " << ModuleName
<< ": " << Symbols;
}

AsynchronousSymbolQuery::AsynchronousSymbolQuery(
const SymbolLookupSet &Symbols, SymbolState RequiredState,
SymbolsResolvedCallback NotifyComplete)
Expand Down
30 changes: 30 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp
Expand Up @@ -148,6 +148,36 @@ class ObjectLinkingLayerJITLinkContext final : public JITLinkContext {
if (const auto &InitSym = MR.getInitializerSymbol())
InternedResult[InitSym] = JITEvaluatedSymbol();

{
// Check that InternedResult matches up with MR.getSymbols().
// This guards against faulty transformations / compilers / object caches.

if (InternedResult.size() > MR.getSymbols().size()) {
SymbolNameVector ExtraSymbols;
for (auto &KV : InternedResult)
if (!MR.getSymbols().count(KV.first))
ExtraSymbols.push_back(KV.first);
ES.reportError(
make_error<UnexpectedSymbolDefinitions>(G.getName(),
std::move(ExtraSymbols)));
MR.failMaterialization();
return;
}

SymbolNameVector MissingSymbols;
for (auto &KV : MR.getSymbols())
if (!InternedResult.count(KV.first))
MissingSymbols.push_back(KV.first);

if (!MissingSymbols.empty()) {
ES.reportError(
make_error<MissingSymbolDefinitions>(G.getName(),
std::move(MissingSymbols)));
MR.failMaterialization();
return;
}
}

if (auto Err = MR.notifyResolved(InternedResult)) {
Layer.getExecutionSession().reportError(std::move(Err));
MR.failMaterialization();
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/ExecutionEngine/OrcError/OrcError.cpp
Expand Up @@ -61,6 +61,10 @@ class OrcErrorCategory : public std::error_category {
"(Use StringError to get error message)";
case OrcErrorCode::UnknownResourceHandle:
return "Unknown resource handle";
case OrcErrorCode::MissingSymbolDefinitions:
return "MissingSymbolsDefinitions";
case OrcErrorCode::UnexpectedSymbolDefinitions:
return "UnexpectedSymbolDefinitions";
}
llvm_unreachable("Unhandled error code");
}
Expand Down

0 comments on commit 8172689

Please sign in to comment.