Debian Codegraph @

js/src/jsapi-tests/testWasmLEB128.cpp

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