Commit c5b2f153 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Add support and unittests for decoding signed LEB128.

R=bradnelson@chromium.org,ahaas@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1746653002

Cr-Commit-Position: refs/heads/master@{#34368}
parent 2b29b630
......@@ -99,13 +99,31 @@ class Decoder {
}
DCHECK_LE(ptr - (base + offset), kMaxDiff);
*length = static_cast<int>(ptr - (base + offset));
if (ptr == end && (b & 0x80)) {
error(base, ptr, msg);
return 0;
if (ptr == end) {
if (*length == kMaxDiff && (b & 0xF0) != 0) {
error(base, ptr, "extra bits in LEB128");
return 0;
}
if ((b & 0x80) != 0) {
error(base, ptr, msg);
return 0;
}
}
return result;
}
// Reads a variable-length signed integer (little endian).
int32_t checked_read_i32v(const byte* base, int offset, int* length,
const char* msg = "expected SLEB128") {
uint32_t result = checked_read_u32v(base, offset, length, msg);
if (*length == 5) return bit_cast<int32_t>(result);
if (*length > 0) {
int shift = 32 - 7 * *length;
return bit_cast<int32_t>(result << shift) >> shift;
}
return 0;
}
// Reads a single 16-bit unsigned integer (little endian).
inline uint16_t read_u16(const byte* ptr) {
DCHECK(ptr >= start_ && (ptr + 2) <= end_);
......@@ -232,12 +250,6 @@ class Decoder {
}
}
bool RangeOk(const byte* pc, int length) {
if (pc < start_ || pc_ >= limit_) return false;
if ((pc + length) >= limit_) return false;
return true;
}
void error(const char* msg) { error(pc_, nullptr, msg); }
void error(const byte* pc, const char* msg) { error(pc, nullptr, msg); }
......@@ -287,8 +299,8 @@ class Decoder {
result.start = start_;
result.error_pc = error_pc_;
result.error_pt = error_pt_;
result.error_msg = error_msg_;
error_msg_.Reset(nullptr);
// transfer ownership of the error to the result.
result.error_msg.Reset(error_msg_.Detach());
} else {
result.error_code = kSuccess;
}
......@@ -310,6 +322,9 @@ class Decoder {
bool ok() const { return error_pc_ == nullptr; }
bool failed() const { return error_pc_ != nullptr; }
const byte* start() { return start_; }
const byte* pc() { return pc_; }
protected:
const byte* start_;
const byte* pc_;
......
......@@ -277,4 +277,28 @@
#define FUNC_INDEX(v) U16_LE(v)
#define NAME_OFFSET(v) U32_LE(v)
#define MASK_7 ((1 << 7) - 1)
#define MASK_14 ((1 << 14) - 1)
#define MASK_21 ((1 << 21) - 1)
#define MASK_28 ((1 << 28) - 1)
#define U32V_1(x) static_cast<byte>(x & MASK_7)
#define U32V_2(x) \
static_cast<byte>((x & MASK_7) | 0x80), static_cast<byte>((x >> 7) & MASK_7)
#define U32V_3(x) \
static_cast<byte>((x & MASK_7) | 0x80), \
static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \
static_cast<byte>((x >> 14) & MASK_7)
#define U32V_4(x) \
static_cast<byte>((x & MASK_7) | 0x80), \
static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \
static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \
static_cast<byte>((x >> 21) & MASK_7)
#define U32V_5(x) \
static_cast<byte>((x & MASK_7) | 0x80), \
static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \
static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \
static_cast<byte>(((x >> 21) & MASK_7) | 0x80), \
static_cast<byte>((x >> 28) & 0xF)
#endif // V8_WASM_MACRO_GEN_H_
......@@ -118,6 +118,7 @@
'test-utils.h',
'test-utils.cc',
'wasm/ast-decoder-unittest.cc',
'wasm/decoder-unittest.cc',
'wasm/encoder-unittest.cc',
'wasm/loop-assignment-analysis-unittest.cc',
'wasm/module-decoder-unittest.cc',
......
This diff is collapsed.
This diff is collapsed.
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