1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "Test.h"
#include <WebCore/SharedMemory.h>
#include <limits>
#if PLATFORM(COCOA)
#include <mach/mach.h>
#endif
namespace WebCore {
void PrintTo(SharedMemory::Protection protection, ::std::ostream* o)
{
if (protection == SharedMemory::Protection::ReadOnly)
*o << "ReadOnly";
else if (protection == SharedMemory::Protection::ReadWrite)
*o << "ReadWrite";
else
*o << "Unknown";
}
}
namespace TestWebKitAPI {
using namespace WebCore;
enum class MemorySource {
Malloc,
SharedMemory,
#if PLATFORM(COCOA)
ExplicitMapping
#endif
};
void PrintTo(MemorySource source, ::std::ostream* o)
{
if (source == MemorySource::Malloc)
*o << "Malloc";
else if (source == MemorySource::SharedMemory)
*o << "SharedMemory";
#if PLATFORM(COCOA)
else if (source == MemorySource::ExplicitMapping)
*o << "ExplicitMapping";
#endif
else
*o << "Unknown";
}
static void fillTestPattern(std::span<uint8_t> data, size_t seed)
{
for (size_t i = 0; i < 5 && i < data.size(); ++i)
data[i] = seed + i;
if (data.size() < 12)
return;
for (size_t i = 1; i < 6; ++i)
data[data.size() - i] = seed + i + 77u;
auto mid = data.size() / 2;
data[mid] = seed + 99;
}
static void expectTestPattern(std::span<uint8_t> data, size_t seed)
{
for (size_t i = 0; i < 5 && i < data.size(); ++i)
EXPECT_EQ(data[i], static_cast<uint8_t>(seed + i));
if (data.size() < 12)
return;
for (size_t i = 1; i < 6; ++i)
EXPECT_EQ(data[data.size() - i], static_cast<uint8_t>(seed + i + 77u));
auto mid = data.size() / 2;
EXPECT_EQ(data[mid], static_cast<uint8_t>(seed + 99));
}
#if PLATFORM(COCOA)
namespace {
class VMAllocSpan {
public:
VMAllocSpan() = default;
VMAllocSpan(std::span<uint8_t> data)
: m_data(data)
{
}
VMAllocSpan(VMAllocSpan&& other)
{
*this = WTFMove(other);
}
VMAllocSpan& operator=(VMAllocSpan&& other)
{
if (this != &other)
m_data = std::exchange(other.m_data, { });
return *this;
}
~VMAllocSpan()
{
if (m_data.empty())
return;
kern_return_t kr = vm_deallocate(mach_task_self(), reinterpret_cast<uintptr_t>(m_data.data()), m_data.size());
ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
}
private:
std::span<uint8_t> m_data;
};
}
#endif
class SharedMemoryFromMemoryTest : public testing::TestWithParam<std::tuple<uint64_t, size_t, MemorySource, SharedMemory::Protection>> {
public:
auto memorySize() const { return std::get<0>(GetParam()); }
auto offset() const { return std::get<1>(GetParam()); }
auto memorySource() const { return std::get<2>(GetParam()); }
auto protection() const { return std::get<3>(GetParam()); }
std::span<uint8_t> allocate();
protected:
static constexpr uint64_t sizeOkToSkip = sizeof(size_t) == 4 ? 2 * GB : 4 * GB;
#if PLATFORM(COCOA)
using Source = std::variant<std::unique_ptr<uint8_t[]>, RefPtr<SharedMemory>, VMAllocSpan>;
#else
using Source = std::variant<std::unique_ptr<uint8_t[]>, RefPtr<SharedMemory>>;
#endif
Source m_source;
};
std::span<uint8_t> SharedMemoryFromMemoryTest::allocate()
{
auto size = static_cast<size_t>(memorySize()) + offset();
std::span<uint8_t> data;
if (memorySource() == MemorySource::Malloc) {
if (auto source = std::unique_ptr<uint8_t[]> { new (std::nothrow) uint8_t[size] }) {
data = { source.get(), size };
m_source = WTFMove(source);
}
}
if (memorySource() == MemorySource::SharedMemory) {
if (auto shm = SharedMemory::allocate(size)) {
data = shm->mutableSpan();
m_source = WTFMove(shm);
}
}
#if PLATFORM(COCOA)
if (memorySource() == MemorySource::ExplicitMapping) {
// Try to create a more complex vm region. The intention would be to to get multiple kernel-side
// memory objects.
// 1. allocate a full region
// 2. allocate one page at the start of the full region as named memory.
vm_address_t dataAddress = 0;
vm_prot_t vmProtection = VM_PROT_READ | VM_PROT_WRITE;
size = std::max(size, static_cast<size_t>(vm_page_size));
kern_return_t kr = vm_map(mach_task_self(), &dataAddress, size, 0, VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE, 0, 0, false, vmProtection, vmProtection, VM_INHERIT_NONE);
ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
memory_object_size_t memoryObjectSize = vm_page_size;
mach_port_t port = MACH_PORT_NULL;
kr = mach_make_memory_entry_64(mach_task_self(), &memoryObjectSize, 0, vmProtection | MAP_MEM_NAMED_CREATE, &port, MACH_PORT_NULL);
ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
kr = vm_map(mach_task_self(), &dataAddress, vm_page_size, 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, port, 0, false, vmProtection, vmProtection, VM_INHERIT_NONE);
ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
kr = mach_port_deallocate(mach_task_self(), port);
ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
data = { static_cast<uint8_t*>(reinterpret_cast<void*>(static_cast<uintptr_t>(dataAddress))), size };
m_source = VMAllocSpan { data };
}
#endif
return data.subspan(offset(), static_cast<size_t>(memorySize()));
}
// Tests creating shared memory from a VM region.
// Tests that:
// * The changes made to the VM region are visible through the shared memory object.
// * The changes made through the shared memory object are visible to the original.
TEST_P(SharedMemoryFromMemoryTest, CreateHandleFromMemory)
{
if (memorySize() > std::numeric_limits<size_t>::max())
return;
auto data = allocate();
if (data.empty() && memorySize() >= sizeOkToSkip)
return;
ASSERT_FALSE(data.empty());
ASSERT_EQ(data.size(), memorySize());
fillTestPattern(data, 1);
expectTestPattern(data, 1);
auto handle = SharedMemory::Handle::createVMShare(data, protection());
#if !PLATFORM(COCOA)
// Remove when implemented.
if (!handle.has_value())
return;
ASSERT_FALSE(handle.has_value());
#endif
ASSERT_TRUE(handle.has_value());
auto shm2 = SharedMemory::map(WTFMove(*handle), protection());
ASSERT_NOT_NULL(shm2);
auto data2 = shm2->mutableSpan();
expectTestPattern(data2, 1);
EXPECT_NE(data.data(), data2.data());
// Modify the orginal VM region and observe that the modification is visible
// through the shared object.
fillTestPattern(data, 2);
expectTestPattern(data2, 2);
if (protection() == SharedMemory::Protection::ReadWrite) {
// Modify through the shared object and observe that the change is visible
// in the original VM region.
fillTestPattern(data2, 3);
expectTestPattern(data2, 3);
expectTestPattern(data, 3);
}
}
// Tests creating shared memory from a VM copy of a VM region.
// Tests that:
// * The changes made to the VM region are not visible through the shared memory object.
// * The changes made through the shared memory object are not visible to the original.
TEST_P(SharedMemoryFromMemoryTest, CreateHandleVMCopyFromMemory)
{
if (memorySize() > std::numeric_limits<size_t>::max())
return;
auto data = allocate();
if (data.empty() && memorySize() >= sizeOkToSkip)
return;
ASSERT_FALSE(data.empty());
ASSERT_EQ(data.size(), memorySize());
fillTestPattern(data, 1);
expectTestPattern(data, 1);
auto handle = SharedMemory::Handle::createVMCopy(data, protection());
#if !PLATFORM(COCOA)
// Remove when implemented.
if (!handle.has_value())
return;
ASSERT_FALSE(handle.has_value());
#endif
ASSERT_TRUE(handle.has_value());
auto shm2 = SharedMemory::map(WTFMove(*handle), protection());
ASSERT_NOT_NULL(shm2);
auto data2 = shm2->mutableSpan();
expectTestPattern(data2, 1);
fillTestPattern(data, 2);
expectTestPattern(data2, 1);
if (protection() == SharedMemory::Protection::ReadWrite) {
fillTestPattern(data2, 3);
expectTestPattern(data2, 3);
expectTestPattern(data, 2);
}
}
// Tests creating shared memory from a physical copy of a VM region.
// Tests that:
// * The changes made to the VM region are not visible through the shared memory object.
// * The changes made through the shared memory object are not visible to the original.
TEST_P(SharedMemoryFromMemoryTest, CreateHandleCopyFromMemory)
{
if (memorySize() > std::numeric_limits<size_t>::max())
return;
auto data = allocate();
if (data.empty() && memorySize() >= sizeOkToSkip)
return;
ASSERT_FALSE(data.empty());
ASSERT_EQ(data.size(), memorySize());
fillTestPattern(data, 1);
expectTestPattern(data, 1);
auto handle = SharedMemory::Handle::createCopy(data, protection());
ASSERT_TRUE(handle.has_value());
auto shm2 = SharedMemory::map(WTFMove(*handle), protection());
ASSERT_NOT_NULL(shm2);
auto data2 = shm2->mutableSpan();
expectTestPattern(data2, 1);
fillTestPattern(data, 2);
expectTestPattern(data2, 1);
if (protection() == SharedMemory::Protection::ReadWrite) {
fillTestPattern(data2, 3);
expectTestPattern(data2, 3);
expectTestPattern(data, 2);
}
}
#if PLATFORM(COCOA)
#define ANY_MEMORY_SOURCE testing::Values(MemorySource::Malloc, MemorySource::SharedMemory, MemorySource::ExplicitMapping)
#else
#define ANY_MEMORY_SOURCE testing::Values(MemorySource::Malloc, MemorySource::SharedMemory)
#endif
INSTANTIATE_TEST_SUITE_P(SharedMemoryTest,
SharedMemoryFromMemoryTest,
testing::Combine(
testing::Values(1, 2, KB, 100 * KB, 500 * MB, 4 * GB + 1, 20 * GB),
testing::Values(0, 1, 444, 4097),
ANY_MEMORY_SOURCE,
testing::Values(SharedMemory::Protection::ReadOnly, SharedMemory::Protection::ReadWrite)),
TestParametersToStringFormatter());
}
|