Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "test/unittests/test-utils.h" |
| 6 | |
| 7 | #include "src/v8.h" |
| 8 | |
| 9 | #include "src/wasm/ast-decoder.h" |
| 10 | #include "src/wasm/encoder.h" |
| 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | namespace wasm { |
| 15 | |
| 16 | class EncoderTest : public TestWithZone { |
| 17 | protected: |
| 18 | void AddLocal(WasmFunctionBuilder* f, LocalType type) { |
| 19 | uint16_t index = f->AddLocal(type); |
| 20 | const std::vector<uint8_t>& out_index = UnsignedLEB128From(index); |
| 21 | std::vector<uint8_t> code; |
| 22 | code.push_back(kExprGetLocal); |
| 23 | for (size_t i = 0; i < out_index.size(); i++) { |
| 24 | code.push_back(out_index.at(i)); |
| 25 | } |
| 26 | uint32_t local_indices[] = {1}; |
| 27 | f->EmitCode(&code[0], static_cast<uint32_t>(code.size()), local_indices, 1); |
| 28 | } |
| 29 | |
| 30 | void CheckReadValue(uint8_t* leb_value, uint32_t expected_result, |
| 31 | int expected_length, |
| 32 | ReadUnsignedLEB128ErrorCode expected_error_code) { |
| 33 | int length; |
| 34 | uint32_t result; |
| 35 | ReadUnsignedLEB128ErrorCode error_code = |
| 36 | ReadUnsignedLEB128Operand(leb_value, leb_value + 5, &length, &result); |
| 37 | CHECK_EQ(error_code, expected_error_code); |
| 38 | if (error_code == 0) { |
| 39 | CHECK_EQ(result, expected_result); |
| 40 | CHECK_EQ(length, expected_length); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void CheckWriteValue(uint32_t input, int length, uint8_t* vals) { |
| 45 | const std::vector<uint8_t> result = UnsignedLEB128From(input); |
| 46 | CHECK_EQ(result.size(), length); |
| 47 | for (int i = 0; i < length; i++) { |
| 48 | CHECK_EQ(result.at(i), vals[i]); |
| 49 | } |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { |
| 55 | Zone zone; |
| 56 | WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 57 | uint16_t f_index = builder->AddFunction(); |
| 58 | WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
| 59 | uint16_t local_float32 = function->AddLocal(kAstF32); |
| 60 | uint16_t param_float32 = function->AddParam(kAstF32); |
| 61 | uint16_t local_int32 = function->AddLocal(kAstI32); |
| 62 | uint16_t local_float64 = function->AddLocal(kAstF64); |
| 63 | uint16_t local_int64 = function->AddLocal(kAstI64); |
| 64 | uint16_t param_int32 = function->AddParam(kAstI32); |
| 65 | uint16_t local_int32_2 = function->AddLocal(kAstI32); |
| 66 | |
| 67 | byte code[] = {kExprGetLocal, static_cast<uint8_t>(param_float32)}; |
| 68 | uint32_t local_indices[] = {1}; |
| 69 | function->EmitCode(code, sizeof(code), local_indices, 1); |
| 70 | code[1] = static_cast<uint8_t>(param_int32); |
| 71 | function->EmitCode(code, sizeof(code), local_indices, 1); |
| 72 | code[1] = static_cast<uint8_t>(local_int32); |
| 73 | function->EmitCode(code, sizeof(code), local_indices, 1); |
| 74 | code[1] = static_cast<uint8_t>(local_int32_2); |
| 75 | function->EmitCode(code, sizeof(code), local_indices, 1); |
| 76 | code[1] = static_cast<uint8_t>(local_int64); |
| 77 | function->EmitCode(code, sizeof(code), local_indices, 1); |
| 78 | code[1] = static_cast<uint8_t>(local_float32); |
| 79 | function->EmitCode(code, sizeof(code), local_indices, 1); |
| 80 | code[1] = static_cast<uint8_t>(local_float64); |
| 81 | function->EmitCode(code, sizeof(code), local_indices, 1); |
| 82 | |
| 83 | WasmFunctionEncoder* f = function->Build(&zone, builder); |
| 84 | ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); |
| 85 | byte* buffer = &buffer_vector[0]; |
| 86 | byte* header = buffer; |
| 87 | byte* body = buffer + f->HeaderSize(); |
| 88 | f->Serialize(buffer, &header, &body); |
| 89 | for (size_t i = 0; i < 7; i++) { |
| 90 | CHECK_EQ(i, static_cast<size_t>(*(buffer + 2 * i + f->HeaderSize() + 1))); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | TEST_F(EncoderTest, Function_Builder_Indexing_Variable_Width) { |
| 96 | Zone zone; |
| 97 | WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 98 | uint16_t f_index = builder->AddFunction(); |
| 99 | WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
| 100 | for (size_t i = 0; i < 128; i++) { |
| 101 | AddLocal(function, kAstF32); |
| 102 | } |
| 103 | AddLocal(function, kAstI32); |
| 104 | |
| 105 | WasmFunctionEncoder* f = function->Build(&zone, builder); |
| 106 | ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); |
| 107 | byte* buffer = &buffer_vector[0]; |
| 108 | byte* header = buffer; |
| 109 | byte* body = buffer + f->HeaderSize(); |
| 110 | f->Serialize(buffer, &header, &body); |
| 111 | body = buffer + f->HeaderSize(); |
| 112 | for (size_t i = 0; i < 127; i++) { |
| 113 | CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * i))); |
| 114 | CHECK_EQ(i + 1, static_cast<size_t>(*(body + 2 * i + 1))); |
| 115 | } |
| 116 | CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * 127))); |
| 117 | CHECK_EQ(0x80, static_cast<size_t>(*(body + 2 * 127 + 1))); |
| 118 | CHECK_EQ(0x01, static_cast<size_t>(*(body + 2 * 127 + 2))); |
| 119 | CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * 127 + 3))); |
| 120 | CHECK_EQ(0x00, static_cast<size_t>(*(body + 2 * 127 + 4))); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | TEST_F(EncoderTest, LEB_Functions) { |
| 125 | byte leb_value[5] = {0, 0, 0, 0, 0}; |
| 126 | CheckReadValue(leb_value, 0, 1, kNoError); |
| 127 | CheckWriteValue(0, 1, leb_value); |
| 128 | leb_value[0] = 23; |
| 129 | CheckReadValue(leb_value, 23, 1, kNoError); |
| 130 | CheckWriteValue(23, 1, leb_value); |
| 131 | leb_value[0] = 0x80; |
| 132 | leb_value[1] = 0x01; |
| 133 | CheckReadValue(leb_value, 128, 2, kNoError); |
| 134 | CheckWriteValue(128, 2, leb_value); |
| 135 | leb_value[0] = 0x80; |
| 136 | leb_value[1] = 0x80; |
| 137 | leb_value[2] = 0x80; |
| 138 | leb_value[3] = 0x80; |
| 139 | leb_value[4] = 0x01; |
| 140 | CheckReadValue(leb_value, 0x10000000, 5, kNoError); |
| 141 | CheckWriteValue(0x10000000, 5, leb_value); |
| 142 | leb_value[0] = 0x80; |
| 143 | leb_value[1] = 0x80; |
| 144 | leb_value[2] = 0x80; |
| 145 | leb_value[3] = 0x80; |
| 146 | leb_value[4] = 0x80; |
| 147 | CheckReadValue(leb_value, -1, -1, kInvalidLEB128); |
| 148 | } |
| 149 | } // namespace wasm |
| 150 | } // namespace internal |
| 151 | } // namespace v8 |