v8/test/unittests/wasm/trap-handler-win-unittest.cc
Dan Elphick ec06bb6ce5 Reland "[include] Split out v8.h"
This is a reland of d1b27019d3

Fixes include:
Adding missing file to bazel build
Forward-declaring classing before friend-classing them to fix win/gcc
Add missing v8-isolate.h include for vtune builds

Original change's description:
> [include] Split out v8.h
>
> This moves every single class/function out of include/v8.h into a
> separate header in include/, which v8.h then includes so that
> externally nothing appears to have changed.
>
> Every include of v8.h from inside v8 has been changed to a more
> fine-grained include.
>
> Previously inline functions defined at the bottom of v8.h would call
> private non-inline functions in the V8 class. Since that class is now
> in v8-initialization.h and is rarely included (as that would create
> dependency cycles), this is not possible and so those methods have been
> moved out of the V8 class into the namespace v8::api_internal.
>
> None of the previous files in include/ now #include v8.h, which means
> if embedders were relying on this transitive dependency then it will
> give compile failures.
>
> v8-inspector.h does depend on v8-scripts.h for the time being to ensure
> that Chrome continue to compile but that change will be reverted once
> those transitive #includes in chrome are changed to include it directly.
>
> Full design:
> https://docs.google.com/document/d/1rTD--I8hCAr-Rho1WTumZzFKaDpEp0IJ8ejZtk4nJdA/edit?usp=sharing
>
> Bug: v8:11965
> Change-Id: I53b84b29581632710edc80eb11f819c2097a2877
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3097448
> Reviewed-by: Yang Guo <yangguo@chromium.org>
> Reviewed-by: Camillo Bruni <cbruni@chromium.org>
> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
> Reviewed-by: Leszek Swirski <leszeks@chromium.org>
> Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
> Commit-Queue: Dan Elphick <delphick@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#76424}

Cq-Include-Trybots: luci.v8.try:v8_linux_vtunejit
Bug: v8:11965
Change-Id: I99f5d3a73bf8fe25b650adfaf9567dc4e44a09e6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3113629
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: Simon Zünd <szuend@chromium.org>
Commit-Queue: Dan Elphick <delphick@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76460}
2021-08-24 13:08:55 +00:00

95 lines
3.5 KiB
C++

// Copyright 2018 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 <windows.h>
#include "include/v8-initialization.h"
#include "include/v8-platform.h"
#include "src/base/page-allocator.h"
#include "src/trap-handler/trap-handler.h"
#include "src/utils/allocation.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
#if V8_TRAP_HANDLER_SUPPORTED
bool g_handler_got_executed = false;
// The start address of the virtual memory we use to cause an exception.
i::Address g_start_address;
// When using V8::EnableWebAssemblyTrapHandler, we save the old one to fall back
// on if V8 doesn't handle the exception. This allows tools like ASan to
// register a handler early on during the process startup and still generate
// stack traces on failures.
class ExceptionHandlerFallbackTest : public ::testing::Test {
protected:
void SetUp() override {
// Register this handler as the last handler.
registered_handler_ = AddVectoredExceptionHandler(/*first=*/0, TestHandler);
CHECK_NOT_NULL(registered_handler_);
v8::PageAllocator* page_allocator = i::GetPlatformPageAllocator();
// We only need a single page.
size_t size = page_allocator->AllocatePageSize();
void* hint = page_allocator->GetRandomMmapAddr();
i::VirtualMemory mem(page_allocator, size, hint, size);
g_start_address = mem.address();
// Set the permissions of the memory to no-access.
CHECK(mem.SetPermissions(g_start_address, size,
v8::PageAllocator::kNoAccess));
mem_ = std::move(mem);
}
void WriteToTestMemory(int value) {
*reinterpret_cast<volatile int*>(g_start_address) = value;
}
int ReadFromTestMemory() {
return *reinterpret_cast<volatile int*>(g_start_address);
}
void TearDown() override {
// be a good citizen and remove the exception handler.
ULONG result = RemoveVectoredExceptionHandler(registered_handler_);
EXPECT_TRUE(result);
}
private:
static LONG WINAPI TestHandler(EXCEPTION_POINTERS* exception) {
g_handler_got_executed = true;
v8::PageAllocator* page_allocator = i::GetPlatformPageAllocator();
// Make the allocated memory accessible so that from now on memory accesses
// do not cause an exception anymore.
EXPECT_TRUE(i::SetPermissions(page_allocator, g_start_address,
page_allocator->AllocatePageSize(),
v8::PageAllocator::kReadWrite));
// The memory access should work now, we can continue execution.
return EXCEPTION_CONTINUE_EXECUTION;
}
i::VirtualMemory mem_;
void* registered_handler_;
};
TEST_F(ExceptionHandlerFallbackTest, DoTest) {
constexpr bool kUseDefaultTrapHandler = true;
EXPECT_TRUE(v8::V8::EnableWebAssemblyTrapHandler(kUseDefaultTrapHandler));
// In the original test setup the test memory is protected against any kind of
// access. Therefore the access here causes an access violation exception,
// which should be caught by the exception handler we install above. In the
// exception handler we change the permission of the test memory to make it
// accessible, and then return from the exception handler to execute the
// memory access again. This time we expect the memory access to work.
constexpr int test_value = 42;
WriteToTestMemory(test_value);
EXPECT_EQ(test_value, ReadFromTestMemory());
EXPECT_TRUE(g_handler_got_executed);
v8::internal::trap_handler::RemoveTrapHandler();
}
#endif
} // namespace