Skip to content

Commit

Permalink
Reapply r250906 with many suggested updates from Rafael Espindola.
Browse files Browse the repository at this point in the history
The needed lld matching changes to be submitted immediately next,
but this revision will cause lld failures with this alone which is expected.

This removes the eating of the error in Archive::Child::getSize() when the characters
in the size field in the archive header for the member is not a number.  To do this we
have all of the needed methods return ErrorOr to push them up until we get out of lib.
Then the tools and can handle the error in whatever way is appropriate for that tool.

So the solution is to plumb all the ErrorOr stuff through everything that touches archives.
This include its iterators as one can create an Archive object but the first or any other
Child object may fail to be created due to a bad size field in its header.

Thanks to Lang Hames on the changes making child_iterator contain an
ErrorOr<Child> instead of a Child and the needed changes to ErrorOr.h to add
operator overloading for * and -> .

We don’t want to use llvm_unreachable() as it calls abort() and is produces a “crash”
and using report_fatal_error() to move the error checking will cause the program to
stop, neither of which are really correct in library code. There are still some uses of
these that should be cleaned up in this library code for other than the size field.

The test cases use archives with text files so one can see the non-digit character,
in this case a ‘%’, in the size field.

These changes will require corresponding changes to the lld project.  That will be
committed immediately after this change.  But this revision will cause lld failures
with this alone which is expected.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252192 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
enderby committed Nov 5, 2015
1 parent c18c3b8 commit 268709a
Show file tree
Hide file tree
Showing 18 changed files with 294 additions and 84 deletions.
28 changes: 18 additions & 10 deletions include/llvm/Object/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Archive : public Binary {
bool isThinMember() const;

public:
Child(const Archive *Parent, const char *Start);
Child(const Archive *Parent, const char *Start, std::error_code *EC);
Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);

bool operator ==(const Child &other) const {
Expand All @@ -75,7 +75,7 @@ class Archive : public Binary {
}

const Archive *getParent() const { return Parent; }
Child getNext() const;
ErrorOr<Child> getNext() const;

ErrorOr<StringRef> getName() const;
StringRef getRawName() const { return getHeader()->getName(); }
Expand All @@ -91,9 +91,9 @@ class Archive : public Binary {
return getHeader()->getAccessMode();
}
/// \return the size of the archive member without the header or padding.
uint64_t getSize() const;
ErrorOr<uint64_t> getSize() const;
/// \return the size in the archive header for this member.
uint64_t getRawSize() const;
ErrorOr<uint64_t> getRawSize() const;

ErrorOr<StringRef> getBuffer() const;
uint64_t getChildOffset() const;
Expand All @@ -105,24 +105,32 @@ class Archive : public Binary {
};

class child_iterator {
Child child;
ErrorOr<Child> child;

public:
child_iterator() : child(Child(nullptr, nullptr)) {}
child_iterator() : child(Child(nullptr, nullptr, nullptr)) {}
child_iterator(const Child &c) : child(c) {}
const Child *operator->() const { return &child; }
const Child &operator*() const { return child; }
child_iterator(std::error_code EC) : child(EC) {}
const ErrorOr<Child> *operator->() const { return &child; }
const ErrorOr<Child> &operator*() const { return child; }

bool operator==(const child_iterator &other) const {
return child == other.child;
// We ignore error states so that comparisions with end() work, which
// allows range loops.
if (child.getError() || other.child.getError())
return false;
return *child == *other.child;
}

bool operator!=(const child_iterator &other) const {
return !(*this == other);
}

// Code in loops with child_iterators must check for errors on each loop
// iteration. And if there is an error break out of the loop.
child_iterator &operator++() { // Preincrement
child = child.getNext();
assert(child && "Can't increment iterator with error");
child = child->getNext();
return *this;
}
};
Expand Down
9 changes: 9 additions & 0 deletions include/llvm/Support/ErrorOr.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ErrorOr {
typedef typename std::remove_reference<T>::type &reference;
typedef const typename std::remove_reference<T>::type &const_reference;
typedef typename std::remove_reference<T>::type *pointer;
typedef const typename std::remove_reference<T>::type *const_pointer;

public:
template <class E>
Expand Down Expand Up @@ -183,10 +184,14 @@ class ErrorOr {
return toPointer(getStorage());
}

const_pointer operator->() const { return toPointer(getStorage()); }

reference operator *() {
return *getStorage();
}

const_reference operator*() const { return *getStorage(); }

private:
template <class OtherT>
void copyConstruct(const ErrorOr<OtherT> &Other) {
Expand Down Expand Up @@ -246,10 +251,14 @@ class ErrorOr {
return Val;
}

const_pointer toPointer(const_pointer Val) const { return Val; }

pointer toPointer(wrap *Val) {
return &Val->get();
}

const_pointer toPointer(const wrap *Val) const { return &Val->get(); }

storage_type *getStorage() {
assert(!HasError && "Cannot get value when an error exists!");
return reinterpret_cast<storage_type*>(TStorage.buffer);
Expand Down
4 changes: 3 additions & 1 deletion lib/ExecutionEngine/MCJIT/MCJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,12 @@ RuntimeDyld::SymbolInfo MCJIT::findSymbol(const std::string &Name,
object::Archive *A = OB.getBinary();
// Look for our symbols in each Archive
object::Archive::child_iterator ChildIt = A->findSym(Name);
if (std::error_code EC = ChildIt->getError())
report_fatal_error(EC.message());
if (ChildIt != A->child_end()) {
// FIXME: Support nested archives?
ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr =
ChildIt->getAsBinary();
(*ChildIt)->getAsBinary();
if (ChildBinOrErr.getError())
continue;
std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
Expand Down
4 changes: 3 additions & 1 deletion lib/ExecutionEngine/Orc/OrcMCJITReplacement.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,12 @@ class OrcMCJITReplacement : public ExecutionEngine {
object::Archive *A = OB.getBinary();
// Look for our symbols in each Archive
object::Archive::child_iterator ChildIt = A->findSym(Name);
if (std::error_code EC = ChildIt->getError())
report_fatal_error(EC.message());
if (ChildIt != A->child_end()) {
// FIXME: Support nested archives?
ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr =
ChildIt->getAsBinary();
(*ChildIt)->getAsBinary();
if (ChildBinOrErr.getError())
continue;
std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
Expand Down
Loading

0 comments on commit 268709a

Please sign in to comment.