2016-04-21 10:14:34 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2016-04-21 10:14:34 +00:00
|
|
|
#include "src/wasm/decoder.h"
|
|
|
|
#include "src/wasm/leb-helper.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class LEBHelperTest : public TestWithZone {};
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, sizeof_u32v) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_u32v(0));
|
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_u32v(1));
|
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_u32v(3));
|
2016-04-21 10:14:34 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 4; i < 128; i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
for (uint32_t i = (1u << 7); i < (1u << 9); i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(2u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
for (uint32_t i = (1u << 14); i < (1u << 16); i += 33) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(3u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
for (uint32_t i = (1u << 21); i < (1u << 24); i += 33999) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(4u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
for (uint32_t i = (1u << 28); i < (1u << 31); i += 33997779u) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(5u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(5u, LEBHelper::sizeof_u32v(0xFFFFFFFF));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, sizeof_i32v) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(0));
|
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(1));
|
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(3));
|
2016-04-21 10:14:34 +00:00
|
|
|
|
|
|
|
for (int32_t i = 0; i < (1 << 6); i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = (1 << 6); i < (1 << 8); i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(2u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = (1 << 13); i < (1 << 15); i += 31) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(3u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = (1 << 20); i < (1 << 22); i += 31991) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(4u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = (1 << 27); i < (1 << 29); i += 3199893) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(5u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 6); i <= 0; i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 13); i < -(1 << 6); i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(2u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 20); i < -(1 << 18); i += 11) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(3u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 27); i < -(1 << 25); i += 11999) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(4u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 30); i < -(1 << 28); i += 1199999) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(5u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 09:37:50 +00:00
|
|
|
#define DECLARE_ENCODE_DECODE_CHECKER(ctype, name) \
|
|
|
|
static void CheckEncodeDecode_##name(ctype val) { \
|
|
|
|
static const int kSize = 16; \
|
|
|
|
static byte buffer[kSize]; \
|
|
|
|
byte* ptr = buffer; \
|
|
|
|
LEBHelper::write_##name(&ptr, val); \
|
|
|
|
EXPECT_EQ(LEBHelper::sizeof_##name(val), \
|
|
|
|
static_cast<size_t>(ptr - buffer)); \
|
|
|
|
Decoder decoder(buffer, buffer + kSize); \
|
|
|
|
unsigned length = 0; \
|
|
|
|
ctype result = \
|
|
|
|
decoder.read_##name<Decoder::kNoValidation>(buffer, &length); \
|
|
|
|
EXPECT_EQ(val, result); \
|
|
|
|
EXPECT_EQ(LEBHelper::sizeof_##name(val), static_cast<size_t>(length)); \
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_ENCODE_DECODE_CHECKER(int32_t, i32v)
|
|
|
|
DECLARE_ENCODE_DECODE_CHECKER(uint32_t, u32v)
|
|
|
|
DECLARE_ENCODE_DECODE_CHECKER(int64_t, i64v)
|
|
|
|
DECLARE_ENCODE_DECODE_CHECKER(uint64_t, u64v)
|
|
|
|
|
2017-09-08 13:59:05 +00:00
|
|
|
#undef DECLARE_ENCODE_DECODE_CHECKER
|
|
|
|
|
2016-04-21 10:14:34 +00:00
|
|
|
TEST_F(LEBHelperTest, WriteAndDecode_u32v) {
|
|
|
|
CheckEncodeDecode_u32v(0);
|
|
|
|
CheckEncodeDecode_u32v(1);
|
|
|
|
CheckEncodeDecode_u32v(5);
|
|
|
|
CheckEncodeDecode_u32v(99);
|
|
|
|
CheckEncodeDecode_u32v(298);
|
|
|
|
CheckEncodeDecode_u32v(87348723);
|
|
|
|
CheckEncodeDecode_u32v(77777);
|
|
|
|
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uint32_t val = 0x3A; val != 0; val = val << 1) {
|
2016-04-21 10:14:34 +00:00
|
|
|
CheckEncodeDecode_u32v(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, WriteAndDecode_i32v) {
|
|
|
|
CheckEncodeDecode_i32v(0);
|
|
|
|
CheckEncodeDecode_i32v(1);
|
|
|
|
CheckEncodeDecode_i32v(5);
|
|
|
|
CheckEncodeDecode_i32v(99);
|
|
|
|
CheckEncodeDecode_i32v(298);
|
|
|
|
CheckEncodeDecode_i32v(87348723);
|
|
|
|
CheckEncodeDecode_i32v(77777);
|
|
|
|
|
|
|
|
CheckEncodeDecode_i32v(-2);
|
|
|
|
CheckEncodeDecode_i32v(-4);
|
|
|
|
CheckEncodeDecode_i32v(-59);
|
|
|
|
CheckEncodeDecode_i32v(-288);
|
|
|
|
CheckEncodeDecode_i32v(-12608);
|
|
|
|
CheckEncodeDecode_i32v(-87328723);
|
|
|
|
CheckEncodeDecode_i32v(-77377);
|
|
|
|
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uint32_t val = 0x3A; val != 0; val = val << 1) {
|
2022-05-05 13:37:00 +00:00
|
|
|
CheckEncodeDecode_i32v(base::bit_cast<int32_t>(val));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t val = 0xFFFFFF3B; val != 0; val = val << 1) {
|
2022-05-05 13:37:00 +00:00
|
|
|
CheckEncodeDecode_i32v(base::bit_cast<int32_t>(val));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, WriteAndDecode_u64v) {
|
|
|
|
CheckEncodeDecode_u64v(0);
|
|
|
|
CheckEncodeDecode_u64v(1);
|
|
|
|
CheckEncodeDecode_u64v(5);
|
|
|
|
CheckEncodeDecode_u64v(99);
|
|
|
|
CheckEncodeDecode_u64v(298);
|
|
|
|
CheckEncodeDecode_u64v(87348723);
|
|
|
|
CheckEncodeDecode_u64v(77777);
|
|
|
|
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uint64_t val = 0x3A; val != 0; val = val << 1) {
|
2016-04-21 10:14:34 +00:00
|
|
|
CheckEncodeDecode_u64v(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, WriteAndDecode_i64v) {
|
|
|
|
CheckEncodeDecode_i64v(0);
|
|
|
|
CheckEncodeDecode_i64v(1);
|
|
|
|
CheckEncodeDecode_i64v(5);
|
|
|
|
CheckEncodeDecode_i64v(99);
|
|
|
|
CheckEncodeDecode_i64v(298);
|
|
|
|
CheckEncodeDecode_i64v(87348723);
|
|
|
|
CheckEncodeDecode_i64v(77777);
|
|
|
|
|
|
|
|
CheckEncodeDecode_i64v(-2);
|
|
|
|
CheckEncodeDecode_i64v(-4);
|
|
|
|
CheckEncodeDecode_i64v(-59);
|
|
|
|
CheckEncodeDecode_i64v(-288);
|
|
|
|
CheckEncodeDecode_i64v(-87648723);
|
|
|
|
CheckEncodeDecode_i64v(-77377);
|
|
|
|
|
2017-12-02 00:30:37 +00:00
|
|
|
for (uint64_t val = 0x3A; val != 0; val = val << 1) {
|
2022-05-05 13:37:00 +00:00
|
|
|
CheckEncodeDecode_i64v(base::bit_cast<int64_t>(val));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint64_t val = 0xFFFFFFFFFFFFFF3B; val != 0; val = val << 1) {
|
2022-05-05 13:37:00 +00:00
|
|
|
CheckEncodeDecode_i64v(base::bit_cast<int64_t>(val));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|