testWasmLEB128.cpp (3718B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "jsapi-tests/tests.h" 6 7 #include "wasm/WasmValidate.h" 8 9 static bool WriteValidBytes(js::wasm::Encoder& encoder, bool* passed) { 10 *passed = false; 11 if (!encoder.empty()) { 12 return true; 13 } 14 15 // These remain the same under LEB128 unsigned encoding 16 if (!encoder.writeVarU32(0x0) || !encoder.writeVarU32(0x1) || 17 !encoder.writeVarU32(0x42)) { 18 return false; 19 } 20 21 // 0x01 0x80 22 if (!encoder.writeVarU32(0x80)) { 23 return false; 24 } 25 26 // 0x03 0x80 27 if (!encoder.writeVarU32(0x180)) { 28 return false; 29 } 30 31 if (encoder.empty()) { 32 return true; 33 } 34 if (encoder.currentOffset() != 7) { 35 return true; 36 } 37 *passed = true; 38 return true; 39 } 40 41 BEGIN_TEST(testWasmLEB128_encoding) { 42 using namespace js; 43 using namespace wasm; 44 45 Bytes bytes; 46 Encoder encoder(bytes); 47 48 bool passed; 49 if (!WriteValidBytes(encoder, &passed)) { 50 return false; 51 } 52 CHECK(passed); 53 54 size_t i = 0; 55 CHECK(bytes[i++] == 0x0); 56 CHECK(bytes[i++] == 0x1); 57 CHECK(bytes[i++] == 0x42); 58 59 CHECK(bytes[i++] == 0x80); 60 CHECK(bytes[i++] == 0x01); 61 62 CHECK(bytes[i++] == 0x80); 63 CHECK(bytes[i++] == 0x03); 64 65 if (i + 1 < bytes.length()) { 66 CHECK(bytes[i++] == 0x00); 67 } 68 return true; 69 } 70 END_TEST(testWasmLEB128_encoding) 71 72 BEGIN_TEST(testWasmLEB128_valid_decoding) { 73 using namespace js; 74 using namespace wasm; 75 76 Bytes bytes; 77 if (!bytes.append(0x0) || !bytes.append(0x1) || !bytes.append(0x42)) { 78 return false; 79 } 80 81 if (!bytes.append(0x80) || !bytes.append(0x01)) { 82 return false; 83 } 84 85 if (!bytes.append(0x80) || !bytes.append(0x03)) { 86 return false; 87 } 88 89 { 90 // Fallible decoding 91 Decoder decoder(bytes); 92 uint32_t value; 93 94 CHECK(decoder.readVarU32(&value) && value == 0x0); 95 CHECK(decoder.readVarU32(&value) && value == 0x1); 96 CHECK(decoder.readVarU32(&value) && value == 0x42); 97 CHECK(decoder.readVarU32(&value) && value == 0x80); 98 CHECK(decoder.readVarU32(&value) && value == 0x180); 99 100 CHECK(decoder.done()); 101 } 102 103 { 104 // Infallible decoding 105 Decoder decoder(bytes); 106 uint32_t value; 107 108 value = decoder.uncheckedReadVarU32(); 109 CHECK(value == 0x0); 110 value = decoder.uncheckedReadVarU32(); 111 CHECK(value == 0x1); 112 value = decoder.uncheckedReadVarU32(); 113 CHECK(value == 0x42); 114 value = decoder.uncheckedReadVarU32(); 115 CHECK(value == 0x80); 116 value = decoder.uncheckedReadVarU32(); 117 CHECK(value == 0x180); 118 119 CHECK(decoder.done()); 120 } 121 return true; 122 } 123 END_TEST(testWasmLEB128_valid_decoding) 124 125 BEGIN_TEST(testWasmLEB128_invalid_decoding) { 126 using namespace js; 127 using namespace wasm; 128 129 Bytes bytes; 130 // Fill bits as per 28 encoded bits 131 if (!bytes.append(0x80) || !bytes.append(0x80) || !bytes.append(0x80) || 132 !bytes.append(0x80)) { 133 return false; 134 } 135 136 // Test last valid values 137 if (!bytes.append(0x00)) { 138 return false; 139 } 140 141 for (uint8_t i = 0; i < 0x0F; i++) { 142 bytes[4] = i; 143 144 { 145 Decoder decoder(bytes); 146 uint32_t value; 147 CHECK(decoder.readVarU32(&value)); 148 CHECK(value == uint32_t(i << 28)); 149 CHECK(decoder.done()); 150 } 151 152 { 153 Decoder decoder(bytes); 154 uint32_t value = decoder.uncheckedReadVarU32(); 155 CHECK(value == uint32_t(i << 28)); 156 CHECK(decoder.done()); 157 } 158 } 159 160 // Test all invalid values of the same size 161 for (uint8_t i = 0x10; i < 0xF0; i++) { 162 bytes[4] = i; 163 164 Decoder decoder(bytes); 165 uint32_t value; 166 CHECK(!decoder.readVarU32(&value)); 167 } 168 169 return true; 170 } 171 END_TEST(testWasmLEB128_invalid_decoding)