Bug 1165833 - LUL testing: import gtest/gmock based Dwarf CFI tests from toolkit/crashreporter/google-breakpad. r=jimb.

This commit is contained in:
Julian Seward 2015-06-15 15:47:43 +02:00
parent 9b1ac68ebb
commit 14bc4ccfab
6 changed files with 3456 additions and 4 deletions

View File

@ -1062,7 +1062,7 @@ class CallFrameInfo::Handler {
// report errors or warn about problems in the data it is parsing.
// These messages are sent to the message sink |aLog| provided to the
// constructor.
class CallFrameInfo::Reporter final {
class CallFrameInfo::Reporter {
public:
// Create an error reporter which attributes troubles to the section
// named SECTION in FILENAME.

View File

@ -16,7 +16,7 @@
// LUL needs a callback for its logging sink.
static void
gtest_logging_sink_for_LUL(const char* str) {
gtest_logging_sink_for_LulIntegration(const char* str) {
if (DEBUG_LUL_TEST == 0) {
return;
}
@ -32,11 +32,11 @@ gtest_logging_sink_for_LUL(const char* str) {
}
}
TEST(LUL, unwind_consistency) {
TEST(LulIntegration, unwind_consistency) {
// Set up LUL and get it to read unwind info for libxul.so, which is
// all we care about here, plus (incidentally) practically every
// other object in the process too.
lul::LUL* lul = new lul::LUL(gtest_logging_sink_for_LUL);
lul::LUL* lul = new lul::LUL(gtest_logging_sink_for_LulIntegration);
read_procmaps(lul);
// Run unwind tests and receive information about how many there

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,491 @@
// Copyright (c) 2010, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
// Derived from:
// test_assembler.cc: Implementation of google_breakpad::TestAssembler.
// See test_assembler.h for details.
// Derived from:
// cfi_assembler.cc: Implementation of google_breakpad::CFISection class.
// See cfi_assembler.h for details.
#include "LulTestInfrastructure.h"
namespace lul_test {
namespace test_assembler {
using std::back_insert_iterator;
Label::Label() : value_(new Binding()) { }
Label::Label(uint64_t value) : value_(new Binding(value)) { }
Label::Label(const Label &label) {
value_ = label.value_;
value_->Acquire();
}
Label::~Label() {
if (value_->Release()) delete value_;
}
Label &Label::operator=(uint64_t value) {
value_->Set(NULL, value);
return *this;
}
Label &Label::operator=(const Label &label) {
value_->Set(label.value_, 0);
return *this;
}
Label Label::operator+(uint64_t addend) const {
Label l;
l.value_->Set(this->value_, addend);
return l;
}
Label Label::operator-(uint64_t subtrahend) const {
Label l;
l.value_->Set(this->value_, -subtrahend);
return l;
}
// When NDEBUG is #defined, assert doesn't evaluate its argument. This
// means you can't simply use assert to check the return value of a
// function with necessary side effects.
//
// ALWAYS_EVALUATE_AND_ASSERT(x) evaluates x regardless of whether
// NDEBUG is #defined; when NDEBUG is not #defined, it further asserts
// that x is true.
#ifdef NDEBUG
#define ALWAYS_EVALUATE_AND_ASSERT(x) x
#else
#define ALWAYS_EVALUATE_AND_ASSERT(x) assert(x)
#endif
uint64_t Label::operator-(const Label &label) const {
uint64_t offset;
ALWAYS_EVALUATE_AND_ASSERT(IsKnownOffsetFrom(label, &offset));
return offset;
}
bool Label::IsKnownConstant(uint64_t *value_p) const {
Binding *base;
uint64_t addend;
value_->Get(&base, &addend);
if (base != NULL) return false;
if (value_p) *value_p = addend;
return true;
}
bool Label::IsKnownOffsetFrom(const Label &label, uint64_t *offset_p) const
{
Binding *label_base, *this_base;
uint64_t label_addend, this_addend;
label.value_->Get(&label_base, &label_addend);
value_->Get(&this_base, &this_addend);
// If this and label are related, Get will find their final
// common ancestor, regardless of how indirect the relation is. This
// comparison also handles the constant vs. constant case.
if (this_base != label_base) return false;
if (offset_p) *offset_p = this_addend - label_addend;
return true;
}
Label::Binding::Binding() : base_(this), addend_(), reference_count_(1) { }
Label::Binding::Binding(uint64_t addend)
: base_(NULL), addend_(addend), reference_count_(1) { }
Label::Binding::~Binding() {
assert(reference_count_ == 0);
if (base_ && base_ != this && base_->Release())
delete base_;
}
void Label::Binding::Set(Binding *binding, uint64_t addend) {
if (!base_ && !binding) {
// We're equating two constants. This could be okay.
assert(addend_ == addend);
} else if (!base_) {
// We are a known constant, but BINDING may not be, so turn the
// tables and try to set BINDING's value instead.
binding->Set(NULL, addend_ - addend);
} else {
if (binding) {
// Find binding's final value. Since the final value is always either
// completely unconstrained or a constant, never a reference to
// another variable (otherwise, it wouldn't be final), this
// guarantees we won't create cycles here, even for code like this:
// l = m, m = n, n = l;
uint64_t binding_addend;
binding->Get(&binding, &binding_addend);
addend += binding_addend;
}
// It seems likely that setting a binding to itself is a bug
// (although I can imagine this might turn out to be helpful to
// permit).
assert(binding != this);
if (base_ != this) {
// Set the other bindings on our chain as well. Note that this
// is sufficient even though binding relationships form trees:
// All binding operations traverse their chains to the end, and
// all bindings related to us share some tail of our chain, so
// they will see the changes we make here.
base_->Set(binding, addend - addend_);
// We're not going to use base_ any more.
if (base_->Release()) delete base_;
}
// Adopt BINDING as our base. Note that it should be correct to
// acquire here, after the release above, even though the usual
// reference-counting rules call for acquiring first, and then
// releasing: the self-reference assertion above should have
// complained if BINDING were 'this' or anywhere along our chain,
// so we didn't release BINDING.
if (binding) binding->Acquire();
base_ = binding;
addend_ = addend;
}
}
void Label::Binding::Get(Binding **base, uint64_t *addend) {
if (base_ && base_ != this) {
// Recurse to find the end of our reference chain (the root of our
// tree), and then rewrite every binding along the chain to refer
// to it directly, adjusting addends appropriately. (This is why
// this member function isn't this-const.)
Binding *final_base;
uint64_t final_addend;
base_->Get(&final_base, &final_addend);
if (final_base) final_base->Acquire();
if (base_->Release()) delete base_;
base_ = final_base;
addend_ += final_addend;
}
*base = base_;
*addend = addend_;
}
template<typename Inserter>
static inline void InsertEndian(test_assembler::Endianness endianness,
size_t size, uint64_t number, Inserter dest) {
assert(size > 0);
if (endianness == kLittleEndian) {
for (size_t i = 0; i < size; i++) {
*dest++ = (char) (number & 0xff);
number >>= 8;
}
} else {
assert(endianness == kBigEndian);
// The loop condition is odd, but it's correct for size_t.
for (size_t i = size - 1; i < size; i--)
*dest++ = (char) ((number >> (i * 8)) & 0xff);
}
}
Section &Section::Append(Endianness endianness, size_t size, uint64_t number) {
InsertEndian(endianness, size, number,
back_insert_iterator<string>(contents_));
return *this;
}
Section &Section::Append(Endianness endianness, size_t size,
const Label &label) {
// If this label's value is known, there's no reason to waste an
// entry in references_ on it.
uint64_t value;
if (label.IsKnownConstant(&value))
return Append(endianness, size, value);
// This will get caught when the references are resolved, but it's
// nicer to find out earlier.
assert(endianness != kUnsetEndian);
references_.push_back(Reference(contents_.size(), endianness, size, label));
contents_.append(size, 0);
return *this;
}
#define ENDIANNESS_L kLittleEndian
#define ENDIANNESS_B kBigEndian
#define ENDIANNESS(e) ENDIANNESS_ ## e
#define DEFINE_SHORT_APPEND_NUMBER_ENDIAN(e, bits) \
Section &Section::e ## bits(uint ## bits ## _t v) { \
InsertEndian(ENDIANNESS(e), bits / 8, v, \
back_insert_iterator<string>(contents_)); \
return *this; \
}
#define DEFINE_SHORT_APPEND_LABEL_ENDIAN(e, bits) \
Section &Section::e ## bits(const Label &v) { \
return Append(ENDIANNESS(e), bits / 8, v); \
}
// Define L16, B32, and friends.
#define DEFINE_SHORT_APPEND_ENDIAN(e, bits) \
DEFINE_SHORT_APPEND_NUMBER_ENDIAN(e, bits) \
DEFINE_SHORT_APPEND_LABEL_ENDIAN(e, bits)
DEFINE_SHORT_APPEND_LABEL_ENDIAN(L, 8);
DEFINE_SHORT_APPEND_LABEL_ENDIAN(B, 8);
DEFINE_SHORT_APPEND_ENDIAN(L, 16);
DEFINE_SHORT_APPEND_ENDIAN(L, 32);
DEFINE_SHORT_APPEND_ENDIAN(L, 64);
DEFINE_SHORT_APPEND_ENDIAN(B, 16);
DEFINE_SHORT_APPEND_ENDIAN(B, 32);
DEFINE_SHORT_APPEND_ENDIAN(B, 64);
#define DEFINE_SHORT_APPEND_NUMBER_DEFAULT(bits) \
Section &Section::D ## bits(uint ## bits ## _t v) { \
InsertEndian(endianness_, bits / 8, v, \
back_insert_iterator<string>(contents_)); \
return *this; \
}
#define DEFINE_SHORT_APPEND_LABEL_DEFAULT(bits) \
Section &Section::D ## bits(const Label &v) { \
return Append(endianness_, bits / 8, v); \
}
#define DEFINE_SHORT_APPEND_DEFAULT(bits) \
DEFINE_SHORT_APPEND_NUMBER_DEFAULT(bits) \
DEFINE_SHORT_APPEND_LABEL_DEFAULT(bits)
DEFINE_SHORT_APPEND_LABEL_DEFAULT(8)
DEFINE_SHORT_APPEND_DEFAULT(16);
DEFINE_SHORT_APPEND_DEFAULT(32);
DEFINE_SHORT_APPEND_DEFAULT(64);
Section &Section::LEB128(long long value) {
while (value < -0x40 || 0x3f < value) {
contents_ += (value & 0x7f) | 0x80;
if (value < 0)
value = (value >> 7) | ~(((unsigned long long) -1) >> 7);
else
value = (value >> 7);
}
contents_ += value & 0x7f;
return *this;
}
Section &Section::ULEB128(uint64_t value) {
while (value > 0x7f) {
contents_ += (value & 0x7f) | 0x80;
value = (value >> 7);
}
contents_ += value;
return *this;
}
Section &Section::Align(size_t alignment, uint8_t pad_byte) {
// ALIGNMENT must be a power of two.
assert(((alignment - 1) & alignment) == 0);
size_t new_size = (contents_.size() + alignment - 1) & ~(alignment - 1);
contents_.append(new_size - contents_.size(), pad_byte);
assert((contents_.size() & (alignment - 1)) == 0);
return *this;
}
bool Section::GetContents(string *contents) {
// For each label reference, find the label's value, and patch it into
// the section's contents.
for (size_t i = 0; i < references_.size(); i++) {
Reference &r = references_[i];
uint64_t value;
if (!r.label.IsKnownConstant(&value)) {
fprintf(stderr, "Undefined label #%zu at offset 0x%zx\n", i, r.offset);
return false;
}
assert(r.offset < contents_.size());
assert(contents_.size() - r.offset >= r.size);
InsertEndian(r.endianness, r.size, value, contents_.begin() + r.offset);
}
contents->clear();
std::swap(contents_, *contents);
references_.clear();
return true;
}
} // namespace test_assembler
} // namespace lul_test
namespace lul_test {
CFISection &CFISection::CIEHeader(uint64_t code_alignment_factor,
int data_alignment_factor,
unsigned return_address_register,
uint8_t version,
const string &augmentation,
bool dwarf64) {
assert(!entry_length_);
entry_length_ = new PendingLength();
in_fde_ = false;
if (dwarf64) {
D32(kDwarf64InitialLengthMarker);
D64(entry_length_->length);
entry_length_->start = Here();
D64(eh_frame_ ? kEHFrame64CIEIdentifier : kDwarf64CIEIdentifier);
} else {
D32(entry_length_->length);
entry_length_->start = Here();
D32(eh_frame_ ? kEHFrame32CIEIdentifier : kDwarf32CIEIdentifier);
}
D8(version);
AppendCString(augmentation);
ULEB128(code_alignment_factor);
LEB128(data_alignment_factor);
if (version == 1)
D8(return_address_register);
else
ULEB128(return_address_register);
return *this;
}
CFISection &CFISection::FDEHeader(Label cie_pointer,
uint64_t initial_location,
uint64_t address_range,
bool dwarf64) {
assert(!entry_length_);
entry_length_ = new PendingLength();
in_fde_ = true;
fde_start_address_ = initial_location;
if (dwarf64) {
D32(0xffffffff);
D64(entry_length_->length);
entry_length_->start = Here();
if (eh_frame_)
D64(Here() - cie_pointer);
else
D64(cie_pointer);
} else {
D32(entry_length_->length);
entry_length_->start = Here();
if (eh_frame_)
D32(Here() - cie_pointer);
else
D32(cie_pointer);
}
EncodedPointer(initial_location);
// The FDE length in an .eh_frame section uses the same encoding as the
// initial location, but ignores the base address (selected by the upper
// nybble of the encoding), as it's a length, not an address that can be
// made relative.
EncodedPointer(address_range,
DwarfPointerEncoding(pointer_encoding_ & 0x0f));
return *this;
}
CFISection &CFISection::FinishEntry() {
assert(entry_length_);
Align(address_size_, lul::DW_CFA_nop);
entry_length_->length = Here() - entry_length_->start;
delete entry_length_;
entry_length_ = NULL;
in_fde_ = false;
return *this;
}
CFISection &CFISection::EncodedPointer(uint64_t address,
DwarfPointerEncoding encoding,
const EncodedPointerBases &bases) {
// Omitted data is extremely easy to emit.
if (encoding == lul::DW_EH_PE_omit)
return *this;
// If (encoding & lul::DW_EH_PE_indirect) != 0, then we assume
// that ADDRESS is the address at which the pointer is stored --- in
// other words, that bit has no effect on how we write the pointer.
encoding = DwarfPointerEncoding(encoding & ~lul::DW_EH_PE_indirect);
// Find the base address to which this pointer is relative. The upper
// nybble of the encoding specifies this.
uint64_t base;
switch (encoding & 0xf0) {
case lul::DW_EH_PE_absptr: base = 0; break;
case lul::DW_EH_PE_pcrel: base = bases.cfi + Size(); break;
case lul::DW_EH_PE_textrel: base = bases.text; break;
case lul::DW_EH_PE_datarel: base = bases.data; break;
case lul::DW_EH_PE_funcrel: base = fde_start_address_; break;
case lul::DW_EH_PE_aligned: base = 0; break;
default: abort();
};
// Make ADDRESS relative. Yes, this is appropriate even for "absptr"
// values; see gcc/unwind-pe.h.
address -= base;
// Align the pointer, if required.
if ((encoding & 0xf0) == lul::DW_EH_PE_aligned)
Align(AddressSize());
// Append ADDRESS to this section in the appropriate form. For the
// fixed-width forms, we don't need to differentiate between signed and
// unsigned encodings, because ADDRESS has already been extended to 64
// bits before it was passed to us.
switch (encoding & 0x0f) {
case lul::DW_EH_PE_absptr:
Address(address);
break;
case lul::DW_EH_PE_uleb128:
ULEB128(address);
break;
case lul::DW_EH_PE_sleb128:
LEB128(address);
break;
case lul::DW_EH_PE_udata2:
case lul::DW_EH_PE_sdata2:
D16(address);
break;
case lul::DW_EH_PE_udata4:
case lul::DW_EH_PE_sdata4:
D32(address);
break;
case lul::DW_EH_PE_udata8:
case lul::DW_EH_PE_sdata8:
D64(address);
break;
default:
abort();
}
return *this;
};
} // namespace lul_test

View File

@ -0,0 +1,666 @@
// -*- mode: C++ -*-
// Copyright (c) 2010, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
// Derived from:
// cfi_assembler.h: Define CFISection, a class for creating properly
// (and improperly) formatted DWARF CFI data for unit tests.
// Derived from:
// test-assembler.h: interface to class for building complex binary streams.
// To test the Breakpad symbol dumper and processor thoroughly, for
// all combinations of host system and minidump processor
// architecture, we need to be able to easily generate complex test
// data like debugging information and minidump files.
//
// For example, if we want our unit tests to provide full code
// coverage for stack walking, it may be difficult to persuade the
// compiler to generate every possible sort of stack walking
// information that we want to support; there are probably DWARF CFI
// opcodes that GCC never emits. Similarly, if we want to test our
// error handling, we will need to generate damaged minidumps or
// debugging information that (we hope) the client or compiler will
// never produce on its own.
//
// google_breakpad::TestAssembler provides a predictable and
// (relatively) simple way to generate complex formatted data streams
// like minidumps and CFI. Furthermore, because TestAssembler is
// portable, developers without access to (say) Visual Studio or a
// SPARC assembler can still work on test data for those targets.
#ifndef LUL_TEST_INFRASTRUCTURE_H
#define LUL_TEST_INFRASTRUCTURE_H
#include <string>
#include <vector>
using std::string;
using std::vector;
namespace lul_test {
namespace test_assembler {
// A Label represents a value not yet known that we need to store in a
// section. As long as all the labels a section refers to are defined
// by the time we retrieve its contents as bytes, we can use undefined
// labels freely in that section's construction.
//
// A label can be in one of three states:
// - undefined,
// - defined as the sum of some other label and a constant, or
// - a constant.
//
// A label's value never changes, but it can accumulate constraints.
// Adding labels and integers is permitted, and yields a label.
// Subtracting a constant from a label is permitted, and also yields a
// label. Subtracting two labels that have some relationship to each
// other is permitted, and yields a constant.
//
// For example:
//
// Label a; // a's value is undefined
// Label b; // b's value is undefined
// {
// Label c = a + 4; // okay, even though a's value is unknown
// b = c + 4; // also okay; b is now a+8
// }
// Label d = b - 2; // okay; d == a+6, even though c is gone
// d.Value(); // error: d's value is not yet known
// d - a; // is 6, even though their values are not known
// a = 12; // now b == 20, and d == 18
// d.Value(); // 18: no longer an error
// b.Value(); // 20
// d = 10; // error: d is already defined.
//
// Label objects' lifetimes are unconstrained: notice that, in the
// above example, even though a and b are only related through c, and
// c goes out of scope, the assignment to a sets b's value as well. In
// particular, it's not necessary to ensure that a Label lives beyond
// Sections that refer to it.
class Label {
public:
Label(); // An undefined label.
explicit Label(uint64_t value); // A label with a fixed value
Label(const Label &value); // A label equal to another.
~Label();
Label &operator=(uint64_t value);
Label &operator=(const Label &value);
Label operator+(uint64_t addend) const;
Label operator-(uint64_t subtrahend) const;
uint64_t operator-(const Label &subtrahend) const;
// We could also provide == and != that work on undefined, but
// related, labels.
// Return true if this label's value is known. If VALUE_P is given,
// set *VALUE_P to the known value if returning true.
bool IsKnownConstant(uint64_t *value_p = NULL) const;
// Return true if the offset from LABEL to this label is known. If
// OFFSET_P is given, set *OFFSET_P to the offset when returning true.
//
// You can think of l.KnownOffsetFrom(m, &d) as being like 'd = l-m',
// except that it also returns a value indicating whether the
// subtraction is possible given what we currently know of l and m.
// It can be possible even if we don't know l and m's values. For
// example:
//
// Label l, m;
// m = l + 10;
// l.IsKnownConstant(); // false
// m.IsKnownConstant(); // false
// uint64_t d;
// l.IsKnownOffsetFrom(m, &d); // true, and sets d to -10.
// l-m // -10
// m-l // 10
// m.Value() // error: m's value is not known
bool IsKnownOffsetFrom(const Label &label, uint64_t *offset_p = NULL) const;
private:
// A label's value, or if that is not yet known, how the value is
// related to other labels' values. A binding may be:
// - a known constant,
// - constrained to be equal to some other binding plus a constant, or
// - unconstrained, and free to take on any value.
//
// Many labels may point to a single binding, and each binding may
// refer to another, so bindings and labels form trees whose leaves
// are labels, whose interior nodes (and roots) are bindings, and
// where links point from children to parents. Bindings are
// reference counted, allowing labels to be lightweight, copyable,
// assignable, placed in containers, and so on.
class Binding {
public:
Binding();
explicit Binding(uint64_t addend);
~Binding();
// Increment our reference count.
void Acquire() { reference_count_++; };
// Decrement our reference count, and return true if it is zero.
bool Release() { return --reference_count_ == 0; }
// Set this binding to be equal to BINDING + ADDEND. If BINDING is
// NULL, then set this binding to the known constant ADDEND.
// Update every binding on this binding's chain to point directly
// to BINDING, or to be a constant, with addends adjusted
// appropriately.
void Set(Binding *binding, uint64_t value);
// Return what we know about the value of this binding.
// - If this binding's value is a known constant, set BASE to
// NULL, and set ADDEND to its value.
// - If this binding is not a known constant but related to other
// bindings, set BASE to the binding at the end of the relation
// chain (which will always be unconstrained), and set ADDEND to the
// value to add to that binding's value to get this binding's
// value.
// - If this binding is unconstrained, set BASE to this, and leave
// ADDEND unchanged.
void Get(Binding **base, uint64_t *addend);
private:
// There are three cases:
//
// - A binding representing a known constant value has base_ NULL,
// and addend_ equal to the value.
//
// - A binding representing a completely unconstrained value has
// base_ pointing to this; addend_ is unused.
//
// - A binding whose value is related to some other binding's
// value has base_ pointing to that other binding, and addend_
// set to the amount to add to that binding's value to get this
// binding's value. We only represent relationships of the form
// x = y+c.
//
// Thus, the bind_ links form a chain terminating in either a
// known constant value or a completely unconstrained value. Most
// operations on bindings do path compression: they change every
// binding on the chain to point directly to the final value,
// adjusting addends as appropriate.
Binding *base_;
uint64_t addend_;
// The number of Labels and Bindings pointing to this binding.
// (When a binding points to itself, indicating a completely
// unconstrained binding, that doesn't count as a reference.)
int reference_count_;
};
// This label's value.
Binding *value_;
};
// Conventions for representing larger numbers as sequences of bytes.
enum Endianness {
kBigEndian, // Big-endian: the most significant byte comes first.
kLittleEndian, // Little-endian: the least significant byte comes first.
kUnsetEndian, // used internally
};
// A section is a sequence of bytes, constructed by appending bytes
// to the end. Sections have a convenient and flexible set of member
// functions for appending data in various formats: big-endian and
// little-endian signed and unsigned values of different sizes;
// LEB128 and ULEB128 values (see below), and raw blocks of bytes.
//
// If you need to append a value to a section that is not convenient
// to compute immediately, you can create a label, append the
// label's value to the section, and then set the label's value
// later, when it's convenient to do so. Once a label's value is
// known, the section class takes care of updating all previously
// appended references to it.
//
// Once all the labels to which a section refers have had their
// values determined, you can get a copy of the section's contents
// as a string.
//
// Note that there is no specified "start of section" label. This is
// because there are typically several different meanings for "the
// start of a section": the offset of the section within an object
// file, the address in memory at which the section's content appear,
// and so on. It's up to the code that uses the Section class to
// keep track of these explicitly, as they depend on the application.
class Section {
public:
explicit Section(Endianness endianness = kUnsetEndian)
: endianness_(endianness) { };
// A base class destructor should be either public and virtual,
// or protected and nonvirtual.
virtual ~Section() { };
// Return the default endianness of this section.
Endianness endianness() const { return endianness_; }
// Append the SIZE bytes at DATA to the end of this section. Return
// a reference to this section.
Section &Append(const string &data) {
contents_.append(data);
return *this;
};
// Append SIZE copies of BYTE to the end of this section. Return a
// reference to this section.
Section &Append(size_t size, uint8_t byte) {
contents_.append(size, (char) byte);
return *this;
}
// Append NUMBER to this section. ENDIANNESS is the endianness to
// use to write the number. SIZE is the length of the number in
// bytes. Return a reference to this section.
Section &Append(Endianness endianness, size_t size, uint64_t number);
Section &Append(Endianness endianness, size_t size, const Label &label);
// Append SECTION to the end of this section. The labels SECTION
// refers to need not be defined yet.
//
// Note that this has no effect on any Labels' values, or on
// SECTION. If placing SECTION within 'this' provides new
// constraints on existing labels' values, then it's up to the
// caller to fiddle with those labels as needed.
Section &Append(const Section &section);
// Append the contents of DATA as a series of bytes terminated by
// a NULL character.
Section &AppendCString(const string &data) {
Append(data);
contents_ += '\0';
return *this;
}
// Append VALUE or LABEL to this section, with the given bit width and
// endianness. Return a reference to this section.
//
// The names of these functions have the form <ENDIANNESS><BITWIDTH>:
// <ENDIANNESS> is either 'L' (little-endian, least significant byte first),
// 'B' (big-endian, most significant byte first), or
// 'D' (default, the section's default endianness)
// <BITWIDTH> is 8, 16, 32, or 64.
//
// Since endianness doesn't matter for a single byte, all the
// <BITWIDTH>=8 functions are equivalent.
//
// These can be used to write both signed and unsigned values, as
// the compiler will properly sign-extend a signed value before
// passing it to the function, at which point the function's
// behavior is the same either way.
Section &L8(uint8_t value) { contents_ += value; return *this; }
Section &B8(uint8_t value) { contents_ += value; return *this; }
Section &D8(uint8_t value) { contents_ += value; return *this; }
Section &L16(uint16_t), &L32(uint32_t), &L64(uint64_t),
&B16(uint16_t), &B32(uint32_t), &B64(uint64_t),
&D16(uint16_t), &D32(uint32_t), &D64(uint64_t);
Section &L8(const Label &label), &L16(const Label &label),
&L32(const Label &label), &L64(const Label &label),
&B8(const Label &label), &B16(const Label &label),
&B32(const Label &label), &B64(const Label &label),
&D8(const Label &label), &D16(const Label &label),
&D32(const Label &label), &D64(const Label &label);
// Append VALUE in a signed LEB128 (Little-Endian Base 128) form.
//
// The signed LEB128 representation of an integer N is a variable
// number of bytes:
//
// - If N is between -0x40 and 0x3f, then its signed LEB128
// representation is a single byte whose value is N.
//
// - Otherwise, its signed LEB128 representation is (N & 0x7f) |
// 0x80, followed by the signed LEB128 representation of N / 128,
// rounded towards negative infinity.
//
// In other words, we break VALUE into groups of seven bits, put
// them in little-endian order, and then write them as eight-bit
// bytes with the high bit on all but the last.
//
// Note that VALUE cannot be a Label (we would have to implement
// relaxation).
Section &LEB128(long long value);
// Append VALUE in unsigned LEB128 (Little-Endian Base 128) form.
//
// The unsigned LEB128 representation of an integer N is a variable
// number of bytes:
//
// - If N is between 0 and 0x7f, then its unsigned LEB128
// representation is a single byte whose value is N.
//
// - Otherwise, its unsigned LEB128 representation is (N & 0x7f) |
// 0x80, followed by the unsigned LEB128 representation of N /
// 128, rounded towards negative infinity.
//
// Note that VALUE cannot be a Label (we would have to implement
// relaxation).
Section &ULEB128(uint64_t value);
// Jump to the next location aligned on an ALIGNMENT-byte boundary,
// relative to the start of the section. Fill the gap with PAD_BYTE.
// ALIGNMENT must be a power of two. Return a reference to this
// section.
Section &Align(size_t alignment, uint8_t pad_byte = 0);
// Return the current size of the section.
size_t Size() const { return contents_.size(); }
// Return a label representing the start of the section.
//
// It is up to the user whether this label represents the section's
// position in an object file, the section's address in memory, or
// what have you; some applications may need both, in which case
// this simple-minded interface won't be enough. This class only
// provides a single start label, for use with the Here and Mark
// member functions.
//
// Ideally, we'd provide this in a subclass that actually knows more
// about the application at hand and can provide an appropriate
// collection of start labels. But then the appending member
// functions like Append and D32 would return a reference to the
// base class, not the derived class, and the chaining won't work.
// Since the only value here is in pretty notation, that's a fatal
// flaw.
Label start() const { return start_; }
// Return a label representing the point at which the next Appended
// item will appear in the section, relative to start().
Label Here() const { return start_ + Size(); }
// Set *LABEL to Here, and return a reference to this section.
Section &Mark(Label *label) { *label = Here(); return *this; }
// If there are no undefined label references left in this
// section, set CONTENTS to the contents of this section, as a
// string, and clear this section. Return true on success, or false
// if there were still undefined labels.
bool GetContents(string *contents);
private:
// Used internally. A reference to a label's value.
struct Reference {
Reference(size_t set_offset, Endianness set_endianness, size_t set_size,
const Label &set_label)
: offset(set_offset), endianness(set_endianness), size(set_size),
label(set_label) { }
// The offset of the reference within the section.
size_t offset;
// The endianness of the reference.
Endianness endianness;
// The size of the reference.
size_t size;
// The label to which this is a reference.
Label label;
};
// The default endianness of this section.
Endianness endianness_;
// The contents of the section.
string contents_;
// References to labels within those contents.
vector<Reference> references_;
// A label referring to the beginning of the section.
Label start_;
};
} // namespace test_assembler
} // namespace lul_test
namespace lul_test {
using lul::DwarfPointerEncoding;
using lul_test::test_assembler::Endianness;
using lul_test::test_assembler::Label;
using lul_test::test_assembler::Section;
class CFISection: public Section {
public:
// CFI augmentation strings beginning with 'z', defined by the
// Linux/IA-64 C++ ABI, can specify interesting encodings for
// addresses appearing in FDE headers and call frame instructions (and
// for additional fields whose presence the augmentation string
// specifies). In particular, pointers can be specified to be relative
// to various base address: the start of the .text section, the
// location holding the address itself, and so on. These allow the
// frame data to be position-independent even when they live in
// write-protected pages. These variants are specified at the
// following two URLs:
//
// http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/dwarfext.html
// http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
//
// CFISection leaves the production of well-formed 'z'-augmented CIEs and
// FDEs to the user, but does provide EncodedPointer, to emit
// properly-encoded addresses for a given pointer encoding.
// EncodedPointer uses an instance of this structure to find the base
// addresses it should use; you can establish a default for all encoded
// pointers appended to this section with SetEncodedPointerBases.
struct EncodedPointerBases {
EncodedPointerBases() : cfi(), text(), data() { }
// The starting address of this CFI section in memory, for
// DW_EH_PE_pcrel. DW_EH_PE_pcrel pointers may only be used in data
// that has is loaded into the program's address space.
uint64_t cfi;
// The starting address of this file's .text section, for DW_EH_PE_textrel.
uint64_t text;
// The starting address of this file's .got or .eh_frame_hdr section,
// for DW_EH_PE_datarel.
uint64_t data;
};
// Create a CFISection whose endianness is ENDIANNESS, and where
// machine addresses are ADDRESS_SIZE bytes long. If EH_FRAME is
// true, use the .eh_frame format, as described by the Linux
// Standards Base Core Specification, instead of the DWARF CFI
// format.
CFISection(Endianness endianness, size_t address_size,
bool eh_frame = false)
: Section(endianness), address_size_(address_size), eh_frame_(eh_frame),
pointer_encoding_(lul::DW_EH_PE_absptr),
encoded_pointer_bases_(), entry_length_(NULL), in_fde_(false) {
// The 'start', 'Here', and 'Mark' members of a CFISection all refer
// to section offsets.
start() = 0;
}
// Return this CFISection's address size.
size_t AddressSize() const { return address_size_; }
// Return true if this CFISection uses the .eh_frame format, or
// false if it contains ordinary DWARF CFI data.
bool ContainsEHFrame() const { return eh_frame_; }
// Use ENCODING for pointers in calls to FDEHeader and EncodedPointer.
void SetPointerEncoding(DwarfPointerEncoding encoding) {
pointer_encoding_ = encoding;
}
// Use the addresses in BASES as the base addresses for encoded
// pointers in subsequent calls to FDEHeader or EncodedPointer.
// This function makes a copy of BASES.
void SetEncodedPointerBases(const EncodedPointerBases &bases) {
encoded_pointer_bases_ = bases;
}
// Append a Common Information Entry header to this section with the
// given values. If dwarf64 is true, use the 64-bit DWARF initial
// length format for the CIE's initial length. Return a reference to
// this section. You should call FinishEntry after writing the last
// instruction for the CIE.
//
// Before calling this function, you will typically want to use Mark
// or Here to make a label to pass to FDEHeader that refers to this
// CIE's position in the section.
CFISection &CIEHeader(uint64_t code_alignment_factor,
int data_alignment_factor,
unsigned return_address_register,
uint8_t version = 3,
const string &augmentation = "",
bool dwarf64 = false);
// Append a Frame Description Entry header to this section with the
// given values. If dwarf64 is true, use the 64-bit DWARF initial
// length format for the CIE's initial length. Return a reference to
// this section. You should call FinishEntry after writing the last
// instruction for the CIE.
//
// This function doesn't support entries that are longer than
// 0xffffff00 bytes. (The "initial length" is always a 32-bit
// value.) Nor does it support .debug_frame sections longer than
// 0xffffff00 bytes.
CFISection &FDEHeader(Label cie_pointer,
uint64_t initial_location,
uint64_t address_range,
bool dwarf64 = false);
// Note the current position as the end of the last CIE or FDE we
// started, after padding with DW_CFA_nops for alignment. This
// defines the label representing the entry's length, cited in the
// entry's header. Return a reference to this section.
CFISection &FinishEntry();
// Append the contents of BLOCK as a DW_FORM_block value: an
// unsigned LEB128 length, followed by that many bytes of data.
CFISection &Block(const string &block) {
ULEB128(block.size());
Append(block);
return *this;
}
// Append ADDRESS to this section, in the appropriate size and
// endianness. Return a reference to this section.
CFISection &Address(uint64_t address) {
Section::Append(endianness(), address_size_, address);
return *this;
}
// Append ADDRESS to this section, using ENCODING and BASES. ENCODING
// defaults to this section's default encoding, established by
// SetPointerEncoding. BASES defaults to this section's bases, set by
// SetEncodedPointerBases. If the DW_EH_PE_indirect bit is set in the
// encoding, assume that ADDRESS is where the true address is stored.
// Return a reference to this section.
//
// (C++ doesn't let me use default arguments here, because I want to
// refer to members of *this in the default argument expression.)
CFISection &EncodedPointer(uint64_t address) {
return EncodedPointer(address, pointer_encoding_, encoded_pointer_bases_);
}
CFISection &EncodedPointer(uint64_t address, DwarfPointerEncoding encoding) {
return EncodedPointer(address, encoding, encoded_pointer_bases_);
}
CFISection &EncodedPointer(uint64_t address, DwarfPointerEncoding encoding,
const EncodedPointerBases &bases);
// Restate some member functions, to keep chaining working nicely.
CFISection &Mark(Label *label) { Section::Mark(label); return *this; }
CFISection &D8(uint8_t v) { Section::D8(v); return *this; }
CFISection &D16(uint16_t v) { Section::D16(v); return *this; }
CFISection &D16(Label v) { Section::D16(v); return *this; }
CFISection &D32(uint32_t v) { Section::D32(v); return *this; }
CFISection &D32(const Label &v) { Section::D32(v); return *this; }
CFISection &D64(uint64_t v) { Section::D64(v); return *this; }
CFISection &D64(const Label &v) { Section::D64(v); return *this; }
CFISection &LEB128(long long v) { Section::LEB128(v); return *this; }
CFISection &ULEB128(uint64_t v) { Section::ULEB128(v); return *this; }
private:
// A length value that we've appended to the section, but is not yet
// known. LENGTH is the appended value; START is a label referring
// to the start of the data whose length was cited.
struct PendingLength {
Label length;
Label start;
};
// Constants used in CFI/.eh_frame data:
// If the first four bytes of an "initial length" are this constant, then
// the data uses the 64-bit DWARF format, and the length itself is the
// subsequent eight bytes.
static const uint32_t kDwarf64InitialLengthMarker = 0xffffffffU;
// The CIE identifier for 32- and 64-bit DWARF CFI and .eh_frame data.
static const uint32_t kDwarf32CIEIdentifier = ~(uint32_t)0;
static const uint64_t kDwarf64CIEIdentifier = ~(uint64_t)0;
static const uint32_t kEHFrame32CIEIdentifier = 0;
static const uint64_t kEHFrame64CIEIdentifier = 0;
// The size of a machine address for the data in this section.
size_t address_size_;
// If true, we are generating a Linux .eh_frame section, instead of
// a standard DWARF .debug_frame section.
bool eh_frame_;
// The encoding to use for FDE pointers.
DwarfPointerEncoding pointer_encoding_;
// The base addresses to use when emitting encoded pointers.
EncodedPointerBases encoded_pointer_bases_;
// The length value for the current entry.
//
// Oddly, this must be dynamically allocated. Labels never get new
// values; they only acquire constraints on the value they already
// have, or assert if you assign them something incompatible. So
// each header needs truly fresh Label objects to cite in their
// headers and track their positions. The alternative is explicit
// destructor invocation and a placement new. Ick.
PendingLength *entry_length_;
// True if we are currently emitting an FDE --- that is, we have
// called FDEHeader but have not yet called FinishEntry.
bool in_fde_;
// If in_fde_ is true, this is its starting address. We use this for
// emitting DW_EH_PE_funcrel pointers.
uint64_t fde_start_address_;
};
} // namespace lul_test
#endif // LUL_TEST_INFRASTRUCTURE_H

View File

@ -7,6 +7,8 @@
if CONFIG['OS_TARGET'] in ('Android', 'Linux'):
UNIFIED_SOURCES += [
'LulTest.cpp',
'LulTestDwarf.cpp',
'LulTestInfrastructure.cpp',
]
LOCAL_INCLUDES += [