2017-09-16 05:22:38 +00:00
|
|
|
// Copyright 2017 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"
|
|
|
|
#include "testing/gmock/include/gmock/gmock.h"
|
|
|
|
|
2018-04-24 13:10:51 +00:00
|
|
|
#include "src/wasm/function-compiler.h"
|
2018-06-19 09:47:17 +00:00
|
|
|
#include "src/wasm/jump-table-assembler.h"
|
2017-12-05 01:47:59 +00:00
|
|
|
#include "src/wasm/wasm-code-manager.h"
|
2018-10-25 13:29:13 +00:00
|
|
|
#include "src/wasm/wasm-engine.h"
|
2017-09-16 05:22:38 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
2017-09-28 17:55:52 +00:00
|
|
|
namespace wasm_heap_unittest {
|
2017-09-16 05:22:38 +00:00
|
|
|
|
2017-09-28 17:55:52 +00:00
|
|
|
class DisjointAllocationPoolTest : public ::testing::Test {
|
2017-09-16 05:22:38 +00:00
|
|
|
public:
|
2018-09-24 13:25:40 +00:00
|
|
|
void CheckPool(const DisjointAllocationPool& mem,
|
2018-09-25 14:21:11 +00:00
|
|
|
std::initializer_list<base::AddressRegion> expected_regions);
|
|
|
|
void CheckRange(base::AddressRegion region1, base::AddressRegion region2);
|
|
|
|
DisjointAllocationPool Make(
|
|
|
|
std::initializer_list<base::AddressRegion> regions);
|
2017-09-16 05:22:38 +00:00
|
|
|
};
|
|
|
|
|
2018-09-24 13:25:40 +00:00
|
|
|
void DisjointAllocationPoolTest::CheckPool(
|
2017-09-16 05:22:38 +00:00
|
|
|
const DisjointAllocationPool& mem,
|
2018-09-25 14:21:11 +00:00
|
|
|
std::initializer_list<base::AddressRegion> expected_regions) {
|
|
|
|
const auto& regions = mem.regions();
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_EQ(regions.size(), expected_regions.size());
|
2018-09-25 14:21:11 +00:00
|
|
|
auto iter = expected_regions.begin();
|
|
|
|
for (auto it = regions.begin(), e = regions.end(); it != e; ++it, ++iter) {
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_EQ(*it, *iter);
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:21:11 +00:00
|
|
|
void DisjointAllocationPoolTest::CheckRange(base::AddressRegion region1,
|
|
|
|
base::AddressRegion region2) {
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_EQ(region1, region2);
|
2018-06-08 11:05:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-16 05:22:38 +00:00
|
|
|
DisjointAllocationPool DisjointAllocationPoolTest::Make(
|
2018-09-25 14:21:11 +00:00
|
|
|
std::initializer_list<base::AddressRegion> regions) {
|
2017-09-16 05:22:38 +00:00
|
|
|
DisjointAllocationPool ret;
|
2018-09-25 14:21:11 +00:00
|
|
|
for (auto& region : regions) {
|
|
|
|
ret.Merge(region);
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-08 11:05:58 +00:00
|
|
|
TEST_F(DisjointAllocationPoolTest, ConstructEmpty) {
|
2017-09-16 05:22:38 +00:00
|
|
|
DisjointAllocationPool a;
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(a.IsEmpty());
|
2018-09-24 13:25:40 +00:00
|
|
|
CheckPool(a, {});
|
2018-09-25 14:21:11 +00:00
|
|
|
a.Merge({1, 4});
|
|
|
|
CheckPool(a, {{1, 4}});
|
2018-06-08 11:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, ConstructWithRange) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a({1, 4});
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_FALSE(a.IsEmpty());
|
2018-09-25 14:21:11 +00:00
|
|
|
CheckPool(a, {{1, 4}});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, SimpleExtract) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{1, 4}});
|
|
|
|
base::AddressRegion b = a.Allocate(2);
|
|
|
|
CheckPool(a, {{3, 2}});
|
|
|
|
CheckRange(b, {1, 2});
|
2018-06-08 11:05:58 +00:00
|
|
|
a.Merge(b);
|
2018-09-25 14:21:11 +00:00
|
|
|
CheckPool(a, {{1, 4}});
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_EQ(a.regions().size(), uint32_t{1});
|
|
|
|
EXPECT_EQ(a.regions().begin()->begin(), uint32_t{1});
|
|
|
|
EXPECT_EQ(a.regions().begin()->end(), uint32_t{5});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, ExtractAll) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a({1, 4});
|
|
|
|
base::AddressRegion b = a.Allocate(4);
|
|
|
|
CheckRange(b, {1, 4});
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(a.IsEmpty());
|
2018-06-08 11:05:58 +00:00
|
|
|
a.Merge(b);
|
2018-09-25 14:21:11 +00:00
|
|
|
CheckPool(a, {{1, 4}});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, FailToExtract) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{1, 4}});
|
|
|
|
base::AddressRegion b = a.Allocate(5);
|
|
|
|
CheckPool(a, {{1, 4}});
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(b.is_empty());
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, FailToExtractExact) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{1, 4}, {10, 4}});
|
|
|
|
base::AddressRegion b = a.Allocate(5);
|
|
|
|
CheckPool(a, {{1, 4}, {10, 4}});
|
2021-02-01 06:12:34 +00:00
|
|
|
EXPECT_TRUE(b.is_empty());
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, ExtractExact) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{1, 4}, {10, 5}});
|
|
|
|
base::AddressRegion b = a.Allocate(5);
|
|
|
|
CheckPool(a, {{1, 4}});
|
|
|
|
CheckRange(b, {10, 5});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, Merging) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{10, 5}, {20, 5}});
|
|
|
|
a.Merge({15, 5});
|
|
|
|
CheckPool(a, {{10, 15}});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 09:14:24 +00:00
|
|
|
TEST_F(DisjointAllocationPoolTest, MergingFirst) {
|
|
|
|
DisjointAllocationPool a = Make({{10, 5}, {20, 5}});
|
|
|
|
a.Merge({5, 5});
|
|
|
|
CheckPool(a, {{5, 10}, {20, 5}});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, MergingAbove) {
|
|
|
|
DisjointAllocationPool a = Make({{10, 5}, {25, 5}});
|
|
|
|
a.Merge({20, 5});
|
|
|
|
CheckPool(a, {{10, 5}, {20, 10}});
|
|
|
|
}
|
|
|
|
|
2017-09-16 05:22:38 +00:00
|
|
|
TEST_F(DisjointAllocationPoolTest, MergingMore) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{10, 5}, {20, 5}, {30, 5}});
|
|
|
|
a.Merge({15, 5});
|
|
|
|
a.Merge({25, 5});
|
|
|
|
CheckPool(a, {{10, 25}});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, MergingSkip) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{10, 5}, {20, 5}, {30, 5}});
|
|
|
|
a.Merge({25, 5});
|
|
|
|
CheckPool(a, {{10, 5}, {20, 15}});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, MergingSkipLargerSrc) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{10, 5}, {20, 5}, {30, 5}});
|
|
|
|
a.Merge({25, 5});
|
|
|
|
a.Merge({35, 5});
|
|
|
|
CheckPool(a, {{10, 5}, {20, 20}});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DisjointAllocationPoolTest, MergingSkipLargerSrcWithGap) {
|
2018-09-25 14:21:11 +00:00
|
|
|
DisjointAllocationPool a = Make({{10, 5}, {20, 5}, {30, 5}});
|
|
|
|
a.Merge({25, 5});
|
|
|
|
a.Merge({36, 4});
|
|
|
|
CheckPool(a, {{10, 5}, {20, 15}, {36, 4}});
|
2017-09-16 05:22:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 17:55:52 +00:00
|
|
|
} // namespace wasm_heap_unittest
|
2017-09-16 05:22:38 +00:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|