329f694678
The members of HandleScopeImplementer are copied with memcpy when the isolate is transferred to another thread. List contained some primitives which allowed us to manually free the backing store, which was needed in order to ensure that threads would not hold on to old pointers and use them later. With std::vector, we can't do that. Here we change the HandleScopeImplementer to instead use a custom structure DetachableVector, which contains a std::vector but allows manual detaching and freeing of the backing store. This allows us to maintain the old behavior. Bug: v8:6333 Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng Change-Id: I6361d161cdb19878ba19ed51d6ba2fae99e8cdc0 Reviewed-on: https://chromium-review.googlesource.com/660125 Reviewed-by: Yang Guo <yangguo@chromium.org> Commit-Queue: Peter Marshall <petermarshall@chromium.org> Cr-Commit-Position: refs/heads/master@{#48197}
68 lines
1.5 KiB
C++
68 lines
1.5 KiB
C++
// 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 "src/detachable-vector.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
namespace v8 {
|
|
namespace internal {
|
|
|
|
TEST(DetachableVector, ConstructIsEmpty) {
|
|
DetachableVector<int> v;
|
|
|
|
size_t empty_size = 0;
|
|
EXPECT_EQ(empty_size, v.size());
|
|
EXPECT_TRUE(v.empty());
|
|
}
|
|
|
|
TEST(DetachableVector, PushAddsElement) {
|
|
DetachableVector<int> v;
|
|
|
|
v.push_back(1);
|
|
|
|
EXPECT_EQ(1, v.front());
|
|
EXPECT_EQ(1, v.back());
|
|
EXPECT_EQ(1, v.at(0));
|
|
size_t one_size = 1;
|
|
EXPECT_EQ(one_size, v.size());
|
|
EXPECT_FALSE(v.empty());
|
|
}
|
|
|
|
TEST(DetachableVector, AfterFreeIsEmpty) {
|
|
DetachableVector<int> v;
|
|
|
|
v.push_back(1);
|
|
v.free();
|
|
|
|
size_t empty_size = 0;
|
|
EXPECT_EQ(empty_size, v.size());
|
|
EXPECT_TRUE(v.empty());
|
|
}
|
|
|
|
// This test relies on ASAN to detect leaks and double-frees.
|
|
TEST(DetachableVector, DetachLeaksBackingStore) {
|
|
DetachableVector<int> v;
|
|
DetachableVector<int> v2;
|
|
|
|
size_t one_size = 1;
|
|
EXPECT_TRUE(v2.empty());
|
|
|
|
// Force allocation of the backing store.
|
|
v.push_back(1);
|
|
// Bit-copy the data structure.
|
|
memcpy(&v2, &v, sizeof(DetachableVector<int>));
|
|
// The backing store should be leaked here - free was not called.
|
|
v.detach();
|
|
|
|
// We have transferred the backing store to the second vector.
|
|
EXPECT_EQ(one_size, v2.size());
|
|
EXPECT_TRUE(v.empty());
|
|
|
|
// The destructor of v2 will release the backing store.
|
|
}
|
|
|
|
} // namespace internal
|
|
} // namespace v8
|