adda9a5022
More changes for HPHP to help make it clang friendly ~~~hphp/compiler/expression/constant_expression.h ~~~hphp/compiler/expression/function_call.h rfind returns a size_t/unsigned int ~~~hphp/runtime/base/server/http_protocol.cpp Switched to std::to_string. Assuming [] was not intended here ~~~hphp/runtime/base/ref_data.h These fields were accessed in a public manner, assuming public was intended instead of private ~~~hphp/runtime/base/variable_serializer.cpp Switched to using [] and & to make clang happy. Assuming this was to either take or drop the first char. ~~~hphp/runtime/ext/asio/asio_external_thread_event_queue.h ~~~hphp/runtime/ext/asio/asio_external_thread_event_queue.cpp Cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression. This is been moved to a macro as a temporary fix. +++hphp/runtime/ext/ext_misc.cpp Added std::atomic to supress warnings ~~~hphp/runtime/vm/jit/simplifier.cpp Chosen constructor is explicit in copy-initialization ~~~hphp/runtime/vm/jit/translator-asm-helpers.S Ambiguous instructions require an explicit suffix Changed cmp to cmpl ~~~hphp/runtime/vm/jit/translator-x64-helpers.cpp Clang does not support global register variables +++hphp/runtime/vm/unwind.cpp describeFault was only used when DEBUG or USE_TRACE was defined ~~~hphp/runtime/vm/verifier/check_unit.cpp Made fmt pointer const to avoid string format issues/warnings ~~~hphp/util/stack_trace.cpp Clang does not support variable-length arrays. Uniqe_ptr is used instead to take advantage runtime-sized arrays, a restriced form of variable-length arrays ~~~hphp/util/thread_local.h Clang seems to be supporting the __thread attribute, or at the very least it is not complaining about it. ~~~hphp/util/tiny_vector.h Clang does not like the flexible array here, since T is not always POD. I have reimplemented the array here by just sticking one value in the struct and calculating the offset from its address manually. Alterinatively, we could change the the non-POD types to be pointers, or we could edit their implemenations. +++hphp/util/util.h Created a template for the union, A function declared with the constexpr specifier cannot contain type declarations that do not define classes or enumerations +++hphp/runtime/vm/jit/x64-util.h Added a TODO The way hphp/runtime/vm/jit/x64-util.h is currently implemented, it only works if USE_GCC_FAST_TLS is defined
223 linhas
6.8 KiB
C++
223 linhas
6.8 KiB
C++
/*
|
|
+----------------------------------------------------------------------+
|
|
| HipHop for PHP |
|
|
+----------------------------------------------------------------------+
|
|
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
|
|
+----------------------------------------------------------------------+
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
| available through the world-wide-web at the following url: |
|
|
| http://www.php.net/license/3_01.txt |
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
+----------------------------------------------------------------------+
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "hphp/runtime/vm/verifier/check.h"
|
|
#include "hphp/runtime/vm/verifier/cfg.h"
|
|
#include "hphp/runtime/vm/verifier/util.h"
|
|
#include "hphp/runtime/vm/verifier/pretty.h"
|
|
|
|
namespace HPHP {
|
|
namespace Verifier {
|
|
|
|
class UnitChecker {
|
|
public:
|
|
UnitChecker(const Unit*, bool verbose);
|
|
~UnitChecker() {}
|
|
bool verify();
|
|
|
|
private:
|
|
template<class T> bool checkLiteral(size_t, const T*, const char*);
|
|
bool checkStrings();
|
|
bool checkArrays();
|
|
bool checkSourceLocs();
|
|
bool checkPreClasses();
|
|
bool checkFuncs();
|
|
bool checkBytecode();
|
|
bool checkMetadata();
|
|
|
|
private:
|
|
template<class... Args>
|
|
void error(const char* const fmt, Args&&... args) {
|
|
verify_error(m_unit, nullptr, fmt, std::forward<Args>(args)...);
|
|
}
|
|
|
|
private:
|
|
const Unit* m_unit;
|
|
bool m_verbose;
|
|
};
|
|
|
|
bool checkUnit(const Unit* unit, bool verbose) {
|
|
if (verbose) {
|
|
printf("verifying unit from %s\n", unit->filepath()->data());
|
|
}
|
|
return UnitChecker(unit, verbose).verify();
|
|
}
|
|
|
|
// Unit contents to check:
|
|
// 1. bc
|
|
// 2. bc_meta
|
|
// 3. lines
|
|
// 4. UnitLitStr table
|
|
// 5. UnitArray table
|
|
// 6. UnitSourceLoc table
|
|
// 8. Classes
|
|
// 9. Functions
|
|
|
|
UnitChecker::UnitChecker(const Unit* unit, bool verbose)
|
|
: m_unit(unit), m_verbose(verbose) {
|
|
}
|
|
|
|
bool UnitChecker::verify() {
|
|
return checkStrings() &&
|
|
checkArrays() &&
|
|
//checkSourceLocs() &&
|
|
//checkPreClasses() &&
|
|
checkBytecode() &&
|
|
//checkMetadata() &&
|
|
checkFuncs();
|
|
}
|
|
|
|
template<class LitType>
|
|
bool UnitChecker::checkLiteral(size_t id,
|
|
const LitType* lt,
|
|
const char* what) {
|
|
bool ok = true;
|
|
if (!lt) {
|
|
error("null %s id %zu in unit %s\n", what, id,
|
|
m_unit->md5().toString().c_str());
|
|
ok = false;
|
|
}
|
|
if (!lt->isStatic()) {
|
|
error("non-static %s id %zu in unit %s\n", what, id,
|
|
m_unit->md5().toString().c_str());
|
|
ok = false;
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
bool UnitChecker::checkStrings() {
|
|
bool ok = true;
|
|
for (size_t i = 0, n = m_unit->numLitstrs(); i < n; ++i) {
|
|
ok &= checkLiteral(i, m_unit->lookupLitstrId(i), "string");
|
|
}
|
|
return ok;
|
|
// Notes
|
|
// * Any string in repo can be null. repo litstrId is checked on load
|
|
// then discarded. Each Litstr entry becomes a static string.
|
|
// Strings are hash-commoned so presumably only one can be null per unit.
|
|
// string_data_hash and string_data_same both crash/assert on null.
|
|
// * If DB has dups then UnitEmitter commons them - spec should outlaw
|
|
// dups because UE will assert if db has dups with different ids.
|
|
// StringData statically keeps a map of loaded static strings
|
|
// * UE keeps a (String->id) mapping and assigns dups the same id, plus
|
|
// a table of litstrs indexed by id. Unit stores them as
|
|
// m_namedInfo, a vector<StringData,NamedEntity=null> of pairs.
|
|
// * are null characters allowed inside the string?
|
|
// * are strings utf8-encoded?
|
|
}
|
|
|
|
bool UnitChecker::checkArrays() {
|
|
bool ok = true;
|
|
for (size_t i = 0, n = m_unit->numArrays(); i < n; ++i) {
|
|
ok &= checkLiteral(i, m_unit->lookupArrayId(i), "array");
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
/**
|
|
* Check that every byte in the unit's bytecode is inside exactly one
|
|
* function's code region.
|
|
*/
|
|
bool UnitChecker::checkBytecode() {
|
|
bool ok = true;
|
|
typedef std::map<Offset, const Func*> FuncMap; // ordered!
|
|
FuncMap funcs;
|
|
for (AllFuncs i(m_unit); !i.empty();) {
|
|
const Func* f = i.popFront();
|
|
if (f->past() <= f->base()) {
|
|
if (!f->isAbstract() || f->past() < f->base()) {
|
|
error("func size <= 0 [%d:%d] in unit %s\n",
|
|
f->base(), f->past(), m_unit->md5().toString().c_str());
|
|
ok = false;
|
|
continue;
|
|
}
|
|
}
|
|
if (f->base() < 0 || f->past() > m_unit->bclen()) {
|
|
error("function region [%d:%d] out of unit %s bounds [%d:%d]\n",
|
|
f->base(), f->past(), m_unit->md5().toString().c_str(),
|
|
0, m_unit->bclen());
|
|
ok = false;
|
|
continue;
|
|
}
|
|
if (funcs.find(f->base()) != funcs.end()) {
|
|
error("duplicate function-base at %d in unit %s\n",
|
|
f->base(), m_unit->md5().toString().c_str());
|
|
ok = false;
|
|
continue;
|
|
}
|
|
funcs.insert(FuncMap::value_type(f->base(), f));
|
|
}
|
|
// iterate funcs in offset order, checking for holes and overlap
|
|
if (funcs.empty()) {
|
|
error("unit %s must have at least one func\n",
|
|
m_unit->md5().toString().c_str());
|
|
return false;
|
|
}
|
|
Offset last_past = 0;
|
|
for (FuncMap::iterator i = funcs.begin(), e = funcs.end(); i != e; ) {
|
|
const Func* f = (*i).second; ++i;
|
|
if (f->base() < last_past) {
|
|
error("function overlap [%d:%d] in unit %s\n",
|
|
f->base(), last_past, m_unit->md5().toString().c_str());
|
|
ok = false;
|
|
} else if (f->base() > last_past) {
|
|
error("dead bytecode space [%d:%d] in unit %s\n",
|
|
last_past, f->base(), m_unit->md5().toString().c_str());
|
|
ok = false;
|
|
}
|
|
last_past = f->past();
|
|
if (i == e && last_past != m_unit->bclen()) {
|
|
error("dead bytecode [%d:%d] at end of unit %s\n",
|
|
last_past, m_unit->bclen(), m_unit->md5().toString().c_str());
|
|
ok = false;
|
|
}
|
|
}
|
|
return ok;
|
|
// Notes
|
|
// 1. Bytecode regions for every function must not overlap and must exactly
|
|
// divide up the bytecode of the whole unit.
|
|
// 2. Its not an error for an abstract function to have zero size.
|
|
}
|
|
|
|
bool UnitChecker::checkFuncs() {
|
|
const Func* pseudo = 0;
|
|
bool multi = false;
|
|
|
|
bool ok = true;
|
|
for (AllFuncs i(m_unit); !i.empty();) {
|
|
if (i.front()->isPseudoMain()) {
|
|
if (pseudo) {
|
|
multi = true;
|
|
error("%s", "unit should have exactly one pseudo-main\n");
|
|
ok = false;
|
|
}
|
|
pseudo = i.front();
|
|
}
|
|
ok &= checkFunc(i.popFront(), m_verbose);
|
|
}
|
|
|
|
if (!multi && m_unit->getMain() != pseudo) {
|
|
error("%s", "funcs and unit disagree on what is the pseudo-main\n");
|
|
ok = false;
|
|
}
|
|
|
|
return ok;
|
|
}
|
|
|
|
}} // HPHP::Verifier
|