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

Side by Side Diff: test/unittests/wasm/ast-decoder-unittest.cc

Issue 1856413002: [wasm] Refactor decoding of local declarations and make more robust. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 8 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 | « src/wasm/decoder.h ('k') | no next file » | 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 #include "test/unittests/test-utils.h" 5 #include "test/unittests/test-utils.h"
6 6
7 #include "src/v8.h" 7 #include "src/v8.h"
8 8
9 #include "test/cctest/wasm/test-signatures.h" 9 #include "test/cctest/wasm/test-signatures.h"
10 10
(...skipping 2179 matching lines...) Expand 10 before | Expand all | Expand 10 after
2190 EXPECT_ARITY(1, kExprF64SConvertI32); 2190 EXPECT_ARITY(1, kExprF64SConvertI32);
2191 EXPECT_ARITY(1, kExprF64UConvertI32); 2191 EXPECT_ARITY(1, kExprF64UConvertI32);
2192 EXPECT_ARITY(1, kExprF64SConvertI64); 2192 EXPECT_ARITY(1, kExprF64SConvertI64);
2193 EXPECT_ARITY(1, kExprF64UConvertI64); 2193 EXPECT_ARITY(1, kExprF64UConvertI64);
2194 EXPECT_ARITY(1, kExprF64ConvertF32); 2194 EXPECT_ARITY(1, kExprF64ConvertF32);
2195 EXPECT_ARITY(1, kExprF64ReinterpretI64); 2195 EXPECT_ARITY(1, kExprF64ReinterpretI64);
2196 EXPECT_ARITY(1, kExprI32ReinterpretF32); 2196 EXPECT_ARITY(1, kExprI32ReinterpretF32);
2197 EXPECT_ARITY(1, kExprI64ReinterpretF64); 2197 EXPECT_ARITY(1, kExprI64ReinterpretF64);
2198 } 2198 }
2199 2199
2200 typedef std::vector<LocalType>* LocalTypeMap; 2200 typedef ZoneVector<LocalType> LocalTypeMap;
2201 2201
2202 class LocalDeclDecoderTest : public TestWithZone { 2202 class LocalDeclDecoderTest : public TestWithZone {
2203 public: 2203 public:
2204 base::AccountingAllocator allocator;
2205
2204 size_t ExpectRun(LocalTypeMap map, size_t pos, LocalType expected, 2206 size_t ExpectRun(LocalTypeMap map, size_t pos, LocalType expected,
2205 size_t count) { 2207 size_t count) {
2206 for (size_t i = 0; i < count; i++) { 2208 for (size_t i = 0; i < count; i++) {
2207 EXPECT_EQ(expected, map->at(pos++)); 2209 EXPECT_EQ(expected, map[pos++]);
2208 } 2210 }
2209 return pos; 2211 return pos;
2210 } 2212 }
2213
2214 LocalTypeMap Expand(AstLocalDecls& decls) {
2215 ZoneVector<LocalType> map(zone());
2216 for (auto p : decls.local_types) {
2217 map.insert(map.end(), p.second, p.first);
2218 }
2219 return map;
2220 }
2211 }; 2221 };
2212 2222
2223 TEST_F(LocalDeclDecoderTest, EmptyLocals) {
2224 AstLocalDecls decls(zone());
2225 bool result = DecodeLocalDecls(decls, nullptr, nullptr);
2226 EXPECT_FALSE(result);
2227 }
2228
2213 TEST_F(LocalDeclDecoderTest, NoLocals) { 2229 TEST_F(LocalDeclDecoderTest, NoLocals) {
2214 static const byte data[] = {0}; 2230 static const byte data[] = {0};
2215 base::AccountingAllocator allocator; 2231 AstLocalDecls decls(zone());
2216 LocalTypeMap map = 2232 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2217 DecodeLocalDeclsForTesting(&allocator, data, data + sizeof(data)); 2233 EXPECT_TRUE(result);
2218 EXPECT_EQ(0, map->size()); 2234 EXPECT_EQ(0, decls.total_local_count);
2219 if (map) delete map;
2220 } 2235 }
2221 2236
2222 TEST_F(LocalDeclDecoderTest, OneLocal) { 2237 TEST_F(LocalDeclDecoderTest, OneLocal) {
2223 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 2238 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
2224 LocalType type = kLocalTypes[i]; 2239 LocalType type = kLocalTypes[i];
2225 const byte data[] = { 2240 const byte data[] = {
2226 1, 1, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))}; 2241 1, 1, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))};
2227 base::AccountingAllocator allocator; 2242 AstLocalDecls decls(zone());
2228 LocalTypeMap map = 2243 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2229 DecodeLocalDeclsForTesting(&allocator, data, data + sizeof(data)); 2244 EXPECT_TRUE(result);
2230 EXPECT_EQ(1, map->size()); 2245 EXPECT_EQ(1, decls.total_local_count);
2231 EXPECT_EQ(type, map->at(0)); 2246
2232 if (map) delete map; 2247 LocalTypeMap map = Expand(decls);
2248 EXPECT_EQ(1, map.size());
2249 EXPECT_EQ(type, map.at(0));
2233 } 2250 }
2234 } 2251 }
2235 2252
2236 TEST_F(LocalDeclDecoderTest, FiveLocals) { 2253 TEST_F(LocalDeclDecoderTest, FiveLocals) {
2237 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 2254 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
2238 LocalType type = kLocalTypes[i]; 2255 LocalType type = kLocalTypes[i];
2239 const byte data[] = { 2256 const byte data[] = {
2240 1, 5, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))}; 2257 1, 5, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))};
2241 base::AccountingAllocator allocator; 2258 AstLocalDecls decls(zone());
2242 LocalTypeMap map = 2259 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2243 DecodeLocalDeclsForTesting(&allocator, data, data + sizeof(data)); 2260 EXPECT_TRUE(result);
2244 EXPECT_EQ(5, map->size()); 2261 EXPECT_EQ(sizeof(data), decls.decls_encoded_size);
2262 EXPECT_EQ(5, decls.total_local_count);
2263
2264 LocalTypeMap map = Expand(decls);
2265 EXPECT_EQ(5, map.size());
2245 ExpectRun(map, 0, type, 5); 2266 ExpectRun(map, 0, type, 5);
2246 if (map) delete map;
2247 } 2267 }
2248 } 2268 }
2249 2269
2250 TEST_F(LocalDeclDecoderTest, MixedLocals) { 2270 TEST_F(LocalDeclDecoderTest, MixedLocals) {
2251 for (byte a = 0; a < 3; a++) { 2271 for (byte a = 0; a < 3; a++) {
2252 for (byte b = 0; b < 3; b++) { 2272 for (byte b = 0; b < 3; b++) {
2253 for (byte c = 0; c < 3; c++) { 2273 for (byte c = 0; c < 3; c++) {
2254 for (byte d = 0; d < 3; d++) { 2274 for (byte d = 0; d < 3; d++) {
2255 const byte data[] = {4, a, kLocalI32, b, kLocalI64, 2275 const byte data[] = {4, a, kLocalI32, b, kLocalI64,
2256 c, kLocalF32, d, kLocalF64}; 2276 c, kLocalF32, d, kLocalF64};
2257 base::AccountingAllocator allocator; 2277 AstLocalDecls decls(zone());
2258 LocalTypeMap map = 2278 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2259 DecodeLocalDeclsForTesting(&allocator, data, data + sizeof(data)); 2279 EXPECT_TRUE(result);
2260 EXPECT_EQ(a + b + c + d, map->size()); 2280 EXPECT_EQ(sizeof(data), decls.decls_encoded_size);
2281 EXPECT_EQ(a + b + c + d, decls.total_local_count);
2282
2283 LocalTypeMap map = Expand(decls);
2284 EXPECT_EQ(a + b + c + d, map.size());
2261 2285
2262 size_t pos = 0; 2286 size_t pos = 0;
2263 pos = ExpectRun(map, pos, kAstI32, a); 2287 pos = ExpectRun(map, pos, kAstI32, a);
2264 pos = ExpectRun(map, pos, kAstI64, b); 2288 pos = ExpectRun(map, pos, kAstI64, b);
2265 pos = ExpectRun(map, pos, kAstF32, c); 2289 pos = ExpectRun(map, pos, kAstF32, c);
2266 pos = ExpectRun(map, pos, kAstF64, d); 2290 pos = ExpectRun(map, pos, kAstF64, d);
2267
2268 if (map) delete map;
2269 } 2291 }
2270 } 2292 }
2271 } 2293 }
2272 } 2294 }
2273 } 2295 }
2274 2296
2275 TEST_F(LocalDeclDecoderTest, UseEncoder) { 2297 TEST_F(LocalDeclDecoderTest, UseEncoder) {
2276 const byte* data = nullptr; 2298 const byte* data = nullptr;
2277 const byte* end = nullptr; 2299 const byte* end = nullptr;
2278 LocalDeclEncoder local_decls; 2300 LocalDeclEncoder local_decls;
2279 2301
2280 local_decls.AddLocals(5, kAstF32); 2302 local_decls.AddLocals(5, kAstF32);
2281 local_decls.AddLocals(1337, kAstI32); 2303 local_decls.AddLocals(1337, kAstI32);
2282 local_decls.AddLocals(212, kAstI64); 2304 local_decls.AddLocals(212, kAstI64);
2283 local_decls.Prepend(&data, &end); 2305 local_decls.Prepend(&data, &end);
2284 2306
2285 base::AccountingAllocator allocator; 2307 AstLocalDecls decls(zone());
2286 LocalTypeMap map = DecodeLocalDeclsForTesting(&allocator, data, end); 2308 bool result = DecodeLocalDecls(decls, data, end);
2309 EXPECT_TRUE(result);
2310 EXPECT_EQ(5 + 1337 + 212, decls.total_local_count);
2311
2312 LocalTypeMap map = Expand(decls);
2287 size_t pos = 0; 2313 size_t pos = 0;
2288 pos = ExpectRun(map, pos, kAstF32, 5); 2314 pos = ExpectRun(map, pos, kAstF32, 5);
2289 pos = ExpectRun(map, pos, kAstI32, 1337); 2315 pos = ExpectRun(map, pos, kAstI32, 1337);
2290 pos = ExpectRun(map, pos, kAstI64, 212); 2316 pos = ExpectRun(map, pos, kAstI64, 212);
2291
2292 if (map) delete map;
2293 delete[] data; 2317 delete[] data;
2294 } 2318 }
2295 2319
2296 } // namespace wasm 2320 } // namespace wasm
2297 } // namespace internal 2321 } // namespace internal
2298 } // namespace v8 2322 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/decoder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698