Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(621)

Side by Side Diff: src/wasm/decoder.h

Issue 1746653002: [wasm] Add support and unittests for decoding signed LEB128. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/wasm/wasm-macro-gen.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_WASM_DECODER_H_ 5 #ifndef V8_WASM_DECODER_H_
6 #define V8_WASM_DECODER_H_ 6 #define V8_WASM_DECODER_H_
7 7
8 #include "src/base/smart-pointers.h" 8 #include "src/base/smart-pointers.h"
9 #include "src/flags.h" 9 #include "src/flags.h"
10 #include "src/signature.h" 10 #include "src/signature.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 byte b = 0; 92 byte b = 0;
93 uint32_t result = 0; 93 uint32_t result = 0;
94 while (ptr < end) { 94 while (ptr < end) {
95 b = *ptr++; 95 b = *ptr++;
96 result = result | ((b & 0x7F) << shift); 96 result = result | ((b & 0x7F) << shift);
97 if ((b & 0x80) == 0) break; 97 if ((b & 0x80) == 0) break;
98 shift += 7; 98 shift += 7;
99 } 99 }
100 DCHECK_LE(ptr - (base + offset), kMaxDiff); 100 DCHECK_LE(ptr - (base + offset), kMaxDiff);
101 *length = static_cast<int>(ptr - (base + offset)); 101 *length = static_cast<int>(ptr - (base + offset));
102 if (ptr == end && (b & 0x80)) { 102 if (ptr == end) {
103 error(base, ptr, msg); 103 if (*length == kMaxDiff && (b & 0xF0) != 0) {
104 return 0; 104 error(base, ptr, "extra bits in LEB128");
105 return 0;
106 }
107 if ((b & 0x80) != 0) {
108 error(base, ptr, msg);
109 return 0;
110 }
105 } 111 }
106 return result; 112 return result;
107 } 113 }
108 114
115 // Reads a variable-length signed integer (little endian).
116 int32_t checked_read_i32v(const byte* base, int offset, int* length,
117 const char* msg = "expected SLEB128") {
118 uint32_t result = checked_read_u32v(base, offset, length, msg);
119 if (*length == 5) return bit_cast<int32_t>(result);
120 if (*length > 0) {
121 int shift = 32 - 7 * *length;
122 return bit_cast<int32_t>(result << shift) >> shift;
123 }
124 return 0;
125 }
126
109 // Reads a single 16-bit unsigned integer (little endian). 127 // Reads a single 16-bit unsigned integer (little endian).
110 inline uint16_t read_u16(const byte* ptr) { 128 inline uint16_t read_u16(const byte* ptr) {
111 DCHECK(ptr >= start_ && (ptr + 2) <= end_); 129 DCHECK(ptr >= start_ && (ptr + 2) <= end_);
112 #if V8_TARGET_LITTLE_ENDIAN && UNALIGNED_ACCESS_OK 130 #if V8_TARGET_LITTLE_ENDIAN && UNALIGNED_ACCESS_OK
113 return *reinterpret_cast<const uint16_t*>(ptr); 131 return *reinterpret_cast<const uint16_t*>(ptr);
114 #else 132 #else
115 uint16_t b0 = ptr[0]; 133 uint16_t b0 = ptr[0];
116 uint16_t b1 = ptr[1]; 134 uint16_t b1 = ptr[1];
117 return (b1 << 8) | b0; 135 return (b1 << 8) | b0;
118 #endif 136 #endif
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // Check that at least {size} bytes exist between {pc_} and {limit_}. 243 // Check that at least {size} bytes exist between {pc_} and {limit_}.
226 bool checkAvailable(int size) { 244 bool checkAvailable(int size) {
227 if (pc_ < start_ || (pc_ + size) > limit_) { 245 if (pc_ < start_ || (pc_ + size) > limit_) {
228 error(pc_, nullptr, "expected %d bytes, fell off end", size); 246 error(pc_, nullptr, "expected %d bytes, fell off end", size);
229 return false; 247 return false;
230 } else { 248 } else {
231 return true; 249 return true;
232 } 250 }
233 } 251 }
234 252
235 bool RangeOk(const byte* pc, int length) {
236 if (pc < start_ || pc_ >= limit_) return false;
237 if ((pc + length) >= limit_) return false;
238 return true;
239 }
240
241 void error(const char* msg) { error(pc_, nullptr, msg); } 253 void error(const char* msg) { error(pc_, nullptr, msg); }
242 254
243 void error(const byte* pc, const char* msg) { error(pc, nullptr, msg); } 255 void error(const byte* pc, const char* msg) { error(pc, nullptr, msg); }
244 256
245 // Sets internal error state. 257 // Sets internal error state.
246 void error(const byte* pc, const byte* pt, const char* format, ...) { 258 void error(const byte* pc, const byte* pt, const char* format, ...) {
247 if (ok()) { 259 if (ok()) {
248 #if DEBUG 260 #if DEBUG
249 if (FLAG_wasm_break_on_decoder_error) { 261 if (FLAG_wasm_break_on_decoder_error) {
250 base::OS::DebugBreak(); 262 base::OS::DebugBreak();
(...skipping 29 matching lines...) Expand all
280 292
281 // Converts the given value to a {Result}, copying the error if necessary. 293 // Converts the given value to a {Result}, copying the error if necessary.
282 template <typename T> 294 template <typename T>
283 Result<T> toResult(T val) { 295 Result<T> toResult(T val) {
284 Result<T> result; 296 Result<T> result;
285 if (error_pc_) { 297 if (error_pc_) {
286 result.error_code = kError; 298 result.error_code = kError;
287 result.start = start_; 299 result.start = start_;
288 result.error_pc = error_pc_; 300 result.error_pc = error_pc_;
289 result.error_pt = error_pt_; 301 result.error_pt = error_pt_;
290 result.error_msg = error_msg_; 302 // transfer ownership of the error to the result.
291 error_msg_.Reset(nullptr); 303 result.error_msg.Reset(error_msg_.Detach());
292 } else { 304 } else {
293 result.error_code = kSuccess; 305 result.error_code = kSuccess;
294 } 306 }
295 result.val = val; 307 result.val = val;
296 return result; 308 return result;
297 } 309 }
298 310
299 // Resets the boundaries of this decoder. 311 // Resets the boundaries of this decoder.
300 void Reset(const byte* start, const byte* end) { 312 void Reset(const byte* start, const byte* end) {
301 start_ = start; 313 start_ = start;
302 pc_ = start; 314 pc_ = start;
303 limit_ = end; 315 limit_ = end;
304 end_ = end; 316 end_ = end;
305 error_pc_ = nullptr; 317 error_pc_ = nullptr;
306 error_pt_ = nullptr; 318 error_pt_ = nullptr;
307 error_msg_.Reset(nullptr); 319 error_msg_.Reset(nullptr);
308 } 320 }
309 321
310 bool ok() const { return error_pc_ == nullptr; } 322 bool ok() const { return error_pc_ == nullptr; }
311 bool failed() const { return error_pc_ != nullptr; } 323 bool failed() const { return error_pc_ != nullptr; }
312 324
325 const byte* start() { return start_; }
326 const byte* pc() { return pc_; }
327
313 protected: 328 protected:
314 const byte* start_; 329 const byte* start_;
315 const byte* pc_; 330 const byte* pc_;
316 const byte* limit_; 331 const byte* limit_;
317 const byte* end_; 332 const byte* end_;
318 const byte* error_pc_; 333 const byte* error_pc_;
319 const byte* error_pt_; 334 const byte* error_pt_;
320 base::SmartArrayPointer<char> error_msg_; 335 base::SmartArrayPointer<char> error_msg_;
321 }; 336 };
322 337
323 #undef TRACE 338 #undef TRACE
324 } // namespace wasm 339 } // namespace wasm
325 } // namespace internal 340 } // namespace internal
326 } // namespace v8 341 } // namespace v8
327 342
328 #endif // V8_WASM_DECODER_H_ 343 #endif // V8_WASM_DECODER_H_
OLDNEW
« no previous file with comments | « no previous file | src/wasm/wasm-macro-gen.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698