Commit 58c3c22e authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Assume no decoder error if validate is false

Minor performance optimization: A {WasmDecoder} instantiated with
{validate == false} does not need to check {decoder->ok()}.

R=titzer@chromium.org

Change-Id: Ieac8b18432453e1cfe9ee66a15a5e2145570436e
Reviewed-on: https://chromium-review.googlesource.com/1057567Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53156}
parent 62546505
...@@ -347,7 +347,7 @@ template <Decoder::ValidateFlag validate> ...@@ -347,7 +347,7 @@ template <Decoder::ValidateFlag validate>
class BranchTableIterator { class BranchTableIterator {
public: public:
unsigned cur_index() { return index_; } unsigned cur_index() { return index_; }
bool has_next() { return decoder_->ok() && index_ <= table_count_; } bool has_next() { return VALIDATE(decoder_->ok()) && index_ <= table_count_; }
uint32_t next() { uint32_t next() {
DCHECK(has_next()); DCHECK(has_next());
index_++; index_++;
...@@ -687,7 +687,7 @@ class WasmDecoder : public Decoder { ...@@ -687,7 +687,7 @@ class WasmDecoder : public Decoder {
if (decoder->failed()) return false; if (decoder->failed()) return false;
TRACE("local decls count: %u\n", entries); TRACE("local decls count: %u\n", entries);
while (entries-- > 0 && decoder->ok() && decoder->more()) { while (entries-- > 0 && VALIDATE(decoder->ok()) && decoder->more()) {
uint32_t count = decoder->consume_u32v("local count"); uint32_t count = decoder->consume_u32v("local count");
if (decoder->failed()) return false; if (decoder->failed()) return false;
...@@ -746,7 +746,7 @@ class WasmDecoder : public Decoder { ...@@ -746,7 +746,7 @@ class WasmDecoder : public Decoder {
BitVector* assigned = new (zone) BitVector(locals_count, zone); BitVector* assigned = new (zone) BitVector(locals_count, 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 < decoder->end() && decoder->ok()) { while (pc < decoder->end() && VALIDATE(decoder->ok())) {
WasmOpcode opcode = static_cast<WasmOpcode>(*pc); WasmOpcode opcode = static_cast<WasmOpcode>(*pc);
unsigned length = 1; unsigned length = 1;
switch (opcode) { switch (opcode) {
...@@ -786,7 +786,7 @@ class WasmDecoder : public Decoder { ...@@ -786,7 +786,7 @@ class WasmDecoder : public Decoder {
if (depth <= 0) break; if (depth <= 0) break;
pc += length; pc += length;
} }
return decoder->ok() ? assigned : nullptr; return VALIDATE(decoder->ok()) ? assigned : nullptr;
} }
inline bool Validate(const byte* pc, inline bool Validate(const byte* pc,
...@@ -1163,19 +1163,20 @@ class WasmDecoder : public Decoder { ...@@ -1163,19 +1163,20 @@ class WasmDecoder : public Decoder {
}; };
#define CALL_INTERFACE(name, ...) interface_.name(this, ##__VA_ARGS__) #define CALL_INTERFACE(name, ...) interface_.name(this, ##__VA_ARGS__)
#define CALL_INTERFACE_IF_REACHABLE(name, ...) \ #define CALL_INTERFACE_IF_REACHABLE(name, ...) \
do { \ do { \
DCHECK(!control_.empty()); \ DCHECK(!control_.empty()); \
if (this->ok() && control_.back().reachable()) { \ if (VALIDATE(this->ok()) && control_.back().reachable()) { \
interface_.name(this, ##__VA_ARGS__); \ interface_.name(this, ##__VA_ARGS__); \
} \ } \
} while (false) } while (false)
#define CALL_INTERFACE_IF_PARENT_REACHABLE(name, ...) \ #define CALL_INTERFACE_IF_PARENT_REACHABLE(name, ...) \
do { \ do { \
DCHECK(!control_.empty()); \ DCHECK(!control_.empty()); \
if (this->ok() && (control_.size() == 1 || control_at(1)->reachable())) { \ if (VALIDATE(this->ok()) && \
interface_.name(this, ##__VA_ARGS__); \ (control_.size() == 1 || control_at(1)->reachable())) { \
} \ interface_.name(this, ##__VA_ARGS__); \
} \
} while (false) } while (false)
template <Decoder::ValidateFlag validate, typename Interface> template <Decoder::ValidateFlag validate, typename Interface>
...@@ -1246,9 +1247,10 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -1246,9 +1247,10 @@ class WasmFullDecoder : public WasmDecoder<validate> {
if (FLAG_trace_wasm_decode_time) { if (FLAG_trace_wasm_decode_time) {
double ms = decode_timer.Elapsed().InMillisecondsF(); double ms = decode_timer.Elapsed().InMillisecondsF();
PrintF("wasm-decode %s (%0.3f ms)\n\n", this->ok() ? "ok" : "failed", ms); PrintF("wasm-decode %s (%0.3f ms)\n\n",
VALIDATE(this->ok()) ? "ok" : "failed", ms);
} else { } else {
TRACE("wasm-decode %s\n\n", this->ok() ? "ok" : "failed"); TRACE("wasm-decode %s\n\n", VALIDATE(this->ok()) ? "ok" : "failed");
} }
return true; return true;
...@@ -1505,7 +1507,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -1505,7 +1507,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
if (!LookupBlockType(&imm)) break; if (!LookupBlockType(&imm)) break;
auto cond = Pop(0, kWasmI32); auto cond = Pop(0, kWasmI32);
PopArgs(imm.sig); PopArgs(imm.sig);
if (!this->ok()) break; if (!VALIDATE(this->ok())) break;
auto* if_block = PushIf(); auto* if_block = PushIf();
SetBlockType(if_block, imm); SetBlockType(if_block, imm);
CALL_INTERFACE_IF_REACHABLE(If, cond, if_block); CALL_INTERFACE_IF_REACHABLE(If, cond, if_block);
...@@ -1983,7 +1985,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -1983,7 +1985,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
#endif #endif
this->pc_ += len; this->pc_ += len;
} // end decode loop } // end decode loop
if (this->pc_ > this->end_ && this->ok()) this->error("Beyond end of code"); if (!VALIDATE(this->pc_ == this->end_) && this->ok()) {
this->error("Beyond end of code");
}
} }
void EndControl() { void EndControl() {
......
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