Commit a6402fd9 authored by mtrofin's avatar mtrofin Committed by Commit bot

[wasm] Decoder had 2 representations for "end".

Removing "limit_", using just "end_".

BUG=

Review-Url: https://codereview.chromium.org/2613193002
Cr-Commit-Position: refs/heads/master@{#42105}
parent c8d8bf21
...@@ -34,7 +34,6 @@ class Decoder { ...@@ -34,7 +34,6 @@ class Decoder {
Decoder(const byte* start, const byte* end) Decoder(const byte* start, const byte* end)
: start_(start), : start_(start),
pc_(start), pc_(start),
limit_(end),
end_(end), end_(end),
error_pc_(nullptr), error_pc_(nullptr),
error_pt_(nullptr) {} error_pt_(nullptr) {}
...@@ -44,7 +43,7 @@ class Decoder { ...@@ -44,7 +43,7 @@ class Decoder {
inline bool check(const byte* base, unsigned offset, unsigned length, inline bool check(const byte* base, unsigned offset, unsigned length,
const char* msg) { const char* msg) {
DCHECK_GE(base, start_); DCHECK_GE(base, start_);
if ((base + offset + length) > limit_) { if ((base + offset + length) > end_) {
error(base, base + offset, "%s", msg); error(base, base + offset, "%s", msg);
return false; return false;
} }
...@@ -190,17 +189,17 @@ class Decoder { ...@@ -190,17 +189,17 @@ class Decoder {
if (checkAvailable(size)) { if (checkAvailable(size)) {
pc_ += size; pc_ += size;
} else { } else {
pc_ = limit_; pc_ = end_;
} }
} }
// Check that at least {size} bytes exist between {pc_} and {limit_}. // Check that at least {size} bytes exist between {pc_} and {end_}.
bool checkAvailable(int size) { bool checkAvailable(int size) {
intptr_t pc_overflow_value = std::numeric_limits<intptr_t>::max() - size; intptr_t pc_overflow_value = std::numeric_limits<intptr_t>::max() - size;
if (size < 0 || (intptr_t)pc_ > pc_overflow_value) { if (size < 0 || (intptr_t)pc_ > pc_overflow_value) {
error(pc_, nullptr, "reading %d bytes would underflow/overflow", size); error(pc_, nullptr, "reading %d bytes would underflow/overflow", size);
return false; return false;
} else if (pc_ < start_ || limit_ < (pc_ + size)) { } else if (pc_ < start_ || end_ < (pc_ + size)) {
error(pc_, nullptr, "expected %d bytes, fell off end", size); error(pc_, nullptr, "expected %d bytes, fell off end", size);
return false; return false;
} else { } else {
...@@ -241,11 +240,11 @@ class Decoder { ...@@ -241,11 +240,11 @@ class Decoder {
template <typename T> template <typename T>
T traceOffEnd() { T traceOffEnd() {
T t = 0; T t = 0;
for (const byte* ptr = pc_; ptr < limit_; ptr++) { for (const byte* ptr = pc_; ptr < end_; ptr++) {
TRACE("%02x ", *ptr); TRACE("%02x ", *ptr);
} }
TRACE("<end>\n"); TRACE("<end>\n");
pc_ = limit_; pc_ = end_;
return t; return t;
} }
...@@ -272,7 +271,6 @@ class Decoder { ...@@ -272,7 +271,6 @@ class Decoder {
void Reset(const byte* start, const byte* end) { void Reset(const byte* start, const byte* end) {
start_ = start; start_ = start;
pc_ = start; pc_ = start;
limit_ = end;
end_ = end; end_ = end;
error_pc_ = nullptr; error_pc_ = nullptr;
error_pt_ = nullptr; error_pt_ = nullptr;
...@@ -281,7 +279,7 @@ class Decoder { ...@@ -281,7 +279,7 @@ class Decoder {
bool ok() const { return error_msg_ == nullptr; } bool ok() const { return error_msg_ == nullptr; }
bool failed() const { return !ok(); } bool failed() const { return !ok(); }
bool more() const { return pc_ < limit_; } bool more() const { return pc_ < end_; }
const byte* start() { return start_; } const byte* start() { return start_; }
const byte* pc() { return pc_; } const byte* pc() { return pc_; }
...@@ -290,7 +288,6 @@ class Decoder { ...@@ -290,7 +288,6 @@ class Decoder {
protected: protected:
const byte* start_; const byte* start_;
const byte* pc_; const byte* pc_;
const byte* limit_;
const byte* end_; const byte* end_;
const byte* error_pc_; const byte* error_pc_;
const byte* error_pt_; const byte* error_pt_;
...@@ -308,7 +305,7 @@ class Decoder { ...@@ -308,7 +305,7 @@ class Decoder {
const int kMaxLength = (sizeof(IntType) * 8 + 6) / 7; const int kMaxLength = (sizeof(IntType) * 8 + 6) / 7;
const byte* ptr = base + offset; const byte* ptr = base + offset;
const byte* end = ptr + kMaxLength; const byte* end = ptr + kMaxLength;
if (end > limit_) end = limit_; if (end > end_) end = end_;
int shift = 0; int shift = 0;
byte b = 0; byte b = 0;
IntType result = 0; IntType result = 0;
...@@ -358,7 +355,7 @@ class Decoder { ...@@ -358,7 +355,7 @@ class Decoder {
const int kMaxLength = (sizeof(IntType) * 8 + 6) / 7; const int kMaxLength = (sizeof(IntType) * 8 + 6) / 7;
const byte* pos = pc_; const byte* pos = pc_;
const byte* end = pc_ + kMaxLength; const byte* end = pc_ + kMaxLength;
if (end > limit_) end = limit_; if (end > end_) end = end_;
IntType result = 0; IntType result = 0;
int shift = 0; int shift = 0;
......
...@@ -597,7 +597,7 @@ class WasmFullDecoder : public WasmDecoder { ...@@ -597,7 +597,7 @@ class WasmFullDecoder : public WasmDecoder {
// Decode local declarations, if any. // Decode local declarations, if any.
uint32_t entries = consume_u32v("local decls count"); uint32_t entries = consume_u32v("local decls count");
TRACE("local decls count: %u\n", entries); TRACE("local decls count: %u\n", entries);
while (entries-- > 0 && pc_ < limit_) { while (entries-- > 0 && pc_ < end_) {
uint32_t count = consume_u32v("local count"); uint32_t count = consume_u32v("local count");
if ((count + local_type_vec_.size()) > kMaxNumWasmLocals) { if ((count + local_type_vec_.size()) > kMaxNumWasmLocals) {
error(pc_ - 1, "local count too large"); error(pc_ - 1, "local count too large");
...@@ -634,8 +634,8 @@ class WasmFullDecoder : public WasmDecoder { ...@@ -634,8 +634,8 @@ class WasmFullDecoder : public WasmDecoder {
void DecodeFunctionBody() { void DecodeFunctionBody() {
TRACE("wasm-decode %p...%p (module+%d, %d bytes) %s\n", TRACE("wasm-decode %p...%p (module+%d, %d bytes) %s\n",
reinterpret_cast<const void*>(start_), reinterpret_cast<const void*>(start_),
reinterpret_cast<const void*>(limit_), baserel(pc_), reinterpret_cast<const void*>(end_), baserel(pc_),
static_cast<int>(limit_ - start_), builder_ ? "graph building" : ""); static_cast<int>(end_ - start_), builder_ ? "graph building" : "");
{ {
// Set up initial function block. // Set up initial function block.
...@@ -655,7 +655,7 @@ class WasmFullDecoder : public WasmDecoder { ...@@ -655,7 +655,7 @@ class WasmFullDecoder : public WasmDecoder {
} }
} }
if (pc_ >= limit_) return; // Nothing to do. if (pc_ >= end_) return; // Nothing to do.
while (true) { // decoding loop. while (true) { // decoding loop.
unsigned len = 1; unsigned len = 1;
...@@ -1246,9 +1246,9 @@ class WasmFullDecoder : public WasmDecoder { ...@@ -1246,9 +1246,9 @@ class WasmFullDecoder : public WasmDecoder {
} }
#endif #endif
pc_ += len; pc_ += len;
if (pc_ >= limit_) { if (pc_ >= end_) {
// End of code reached or exceeded. // End of code reached or exceeded.
if (pc_ > limit_ && ok()) error("Beyond end of code"); if (pc_ > end_ && ok()) error("Beyond end of code");
return; return;
} }
} // end decode loop } // end decode loop
...@@ -1823,19 +1823,19 @@ class WasmFullDecoder : public WasmDecoder { ...@@ -1823,19 +1823,19 @@ class WasmFullDecoder : public WasmDecoder {
} }
virtual void onFirstError() { virtual void onFirstError() {
limit_ = start_; // Terminate decoding loop. end_ = start_; // Terminate decoding loop.
builder_ = nullptr; // Don't build any more nodes. builder_ = nullptr; // Don't build any more nodes.
TRACE(" !%s\n", error_msg_.get()); TRACE(" !%s\n", error_msg_.get());
} }
BitVector* AnalyzeLoopAssignment(const byte* pc) { BitVector* AnalyzeLoopAssignment(const byte* pc) {
if (pc >= limit_) return nullptr; if (pc >= end_) return nullptr;
if (*pc != kExprLoop) return nullptr; if (*pc != kExprLoop) return nullptr;
BitVector* assigned = BitVector* assigned =
new (zone_) BitVector(static_cast<int>(local_type_vec_.size()), zone_); new (zone_) BitVector(static_cast<int>(local_type_vec_.size()), zone_);
int depth = 0; int depth = 0;
// Iteratively process all AST nodes nested inside the loop. // Iteratively process all AST nodes nested inside the loop.
while (pc < limit_ && ok()) { while (pc < end_ && ok()) {
WasmOpcode opcode = static_cast<WasmOpcode>(*pc); WasmOpcode opcode = static_cast<WasmOpcode>(*pc);
unsigned length = 1; unsigned length = 1;
switch (opcode) { switch (opcode) {
......
...@@ -180,14 +180,14 @@ class ModuleDecoder : public Decoder { ...@@ -180,14 +180,14 @@ class ModuleDecoder : public Decoder {
ModuleOrigin origin) ModuleOrigin origin)
: Decoder(module_start, module_end), module_zone(zone), origin_(origin) { : Decoder(module_start, module_end), module_zone(zone), origin_(origin) {
result_.start = start_; result_.start = start_;
if (limit_ < start_) { if (end_ < start_) {
error(start_, "end is less than start"); error(start_, "end is less than start");
limit_ = start_; end_ = start_;
} }
} }
virtual void onFirstError() { virtual void onFirstError() {
pc_ = limit_; // On error, terminate section decoding loop. pc_ = end_; // On error, terminate section decoding loop.
} }
void DumpModule(const ModuleResult& result) { void DumpModule(const ModuleResult& result) {
...@@ -200,7 +200,7 @@ class ModuleDecoder : public Decoder { ...@@ -200,7 +200,7 @@ class ModuleDecoder : public Decoder {
} }
} }
// File are named `HASH.{ok,failed}.wasm`. // File are named `HASH.{ok,failed}.wasm`.
size_t hash = base::hash_range(start_, limit_); size_t hash = base::hash_range(start_, end_);
char buf[32] = {'\0'}; char buf[32] = {'\0'};
#if V8_OS_WIN && _MSC_VER < 1900 #if V8_OS_WIN && _MSC_VER < 1900
#define snprintf sprintf_s #define snprintf sprintf_s
...@@ -209,7 +209,7 @@ class ModuleDecoder : public Decoder { ...@@ -209,7 +209,7 @@ class ModuleDecoder : public Decoder {
result.ok() ? "ok" : "failed"); result.ok() ? "ok" : "failed");
std::string name(buf); std::string name(buf);
if (FILE* wasm_file = base::OS::FOpen((path + name).c_str(), "wb")) { if (FILE* wasm_file = base::OS::FOpen((path + name).c_str(), "wb")) {
fwrite(start_, limit_ - start_, 1, wasm_file); fwrite(start_, end_ - start_, 1, wasm_file);
fclose(wasm_file); fclose(wasm_file);
} }
} }
...@@ -572,7 +572,7 @@ class ModuleDecoder : public Decoder { ...@@ -572,7 +572,7 @@ class ModuleDecoder : public Decoder {
function->code_end_offset = pc_offset() + size; function->code_end_offset = pc_offset() + size;
if (verify_functions) { if (verify_functions) {
ModuleBytesEnv module_env(module, nullptr, ModuleBytesEnv module_env(module, nullptr,
ModuleWireBytes(start_, limit_)); ModuleWireBytes(start_, end_));
VerifyFunctionBody(i + module->num_imported_functions, &module_env, VerifyFunctionBody(i + module->num_imported_functions, &module_env,
function); function);
} }
...@@ -651,7 +651,7 @@ class ModuleDecoder : public Decoder { ...@@ -651,7 +651,7 @@ class ModuleDecoder : public Decoder {
function->name_offset = 0; // ---- name function->name_offset = 0; // ---- name
function->name_length = 0; // ---- name length function->name_length = 0; // ---- name length
function->code_start_offset = off(pc_); // ---- code start function->code_start_offset = off(pc_); // ---- code start
function->code_end_offset = off(limit_); // ---- code end function->code_end_offset = off(end_); // ---- code end
if (ok()) VerifyFunctionBody(0, module_env, function); if (ok()) VerifyFunctionBody(0, module_env, function);
...@@ -729,7 +729,7 @@ class ModuleDecoder : public Decoder { ...@@ -729,7 +729,7 @@ class ModuleDecoder : public Decoder {
segment->source_offset = static_cast<uint32_t>(pc_ - start_); segment->source_offset = static_cast<uint32_t>(pc_ - start_);
// Validate the data is in the module. // Validate the data is in the module.
uint32_t module_limit = static_cast<uint32_t>(limit_ - start_); uint32_t module_limit = static_cast<uint32_t>(end_ - start_);
if (!IsWithinLimit(module_limit, segment->source_offset, if (!IsWithinLimit(module_limit, segment->source_offset,
segment->source_size)) { segment->source_size)) {
error(start, "segment out of bounds of module"); error(start, "segment out of bounds of module");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment