test-stack-unwinding-win64.cc 3.97 KB
Newer Older
1 2 3 4 5
// Copyright 2019 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/base/win32-headers.h"
6
#include "src/init/v8.h"
7 8
#include "test/cctest/cctest.h"

9 10 11 12 13 14 15
#if defined(V8_OS_WIN_X64)
#define CONTEXT_PC(context) (context.Rip)
#elif defined(V8_OS_WIN_ARM64)
#define CONTEXT_PC(context) (context.Pc)
#endif

class UnwindingWin64Callbacks {
16
 public:
17
  UnwindingWin64Callbacks() = default;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

  static void Getter(v8::Local<v8::String> name,
                     const v8::PropertyCallbackInfo<v8::Value>& info) {
    // Expects to find at least 15 stack frames in the call stack.
    // The stack walking should fail on stack frames for builtin functions if
    // stack unwinding data has not been correctly registered.
    int stack_frames = CountCallStackFrames(15);
    CHECK_GE(stack_frames, 15);
  }
  static void Setter(v8::Local<v8::String> name, v8::Local<v8::Value> value,
                     const v8::PropertyCallbackInfo<void>& info) {}

 private:
  // Windows-specific code to walk the stack starting from the current
  // instruction pointer.
  static int CountCallStackFrames(int max_frames) {
    CONTEXT context_record;
    ::RtlCaptureContext(&context_record);

    int iframe = 0;
    while (++iframe < max_frames) {
      uint64_t image_base;
40 41
      PRUNTIME_FUNCTION function_entry = ::RtlLookupFunctionEntry(
          CONTEXT_PC(context_record), &image_base, nullptr);
42 43 44 45
      if (!function_entry) break;

      void* handler_data;
      uint64_t establisher_frame;
46 47 48 49
      ::RtlVirtualUnwind(UNW_FLAG_NHANDLER, image_base,
                         CONTEXT_PC(context_record), function_entry,
                         &context_record, &handler_data, &establisher_frame,
                         NULL);
50 51 52 53 54
    }
    return iframe;
  }
};

55 56
// Verifies that stack unwinding data has been correctly registered on Win64.
UNINITIALIZED_TEST(StackUnwindingWin64) {
57 58
#ifdef V8_WIN64_UNWINDING_INFO

59
  static const char* unwinding_win64_test_source =
60 61 62 63 64
      "function start(count) {\n"
      "  for (var i = 0; i < count; i++) {\n"
      "    var o = instance.foo;\n"
      "    instance.foo = o + 1;\n"
      "  }\n"
65 66
      "};\n"
      "%PrepareFunctionForOptimization(start);\n";
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

  // This test may fail on Windows 7
  if (!::IsWindows8OrGreater()) {
    return;
  }

  i::FLAG_allow_natives_syntax = true;
  i::FLAG_win64_unwinding_info = true;

  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
  isolate->Enter();
  {
    v8::HandleScope scope(isolate);
    LocalContext env(isolate);

    v8::Local<v8::FunctionTemplate> func_template =
        v8::FunctionTemplate::New(isolate);
    v8::Local<v8::ObjectTemplate> instance_template =
        func_template->InstanceTemplate();

89
    UnwindingWin64Callbacks accessors;
90 91
    v8::Local<v8::External> data = v8::External::New(isolate, &accessors);
    instance_template->SetAccessor(v8_str("foo"),
92 93
                                   &UnwindingWin64Callbacks::Getter,
                                   &UnwindingWin64Callbacks::Setter, data);
94 95 96 97 98 99
    v8::Local<v8::Function> func =
        func_template->GetFunction(env.local()).ToLocalChecked();
    v8::Local<v8::Object> instance =
        func->NewInstance(env.local()).ToLocalChecked();
    env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();

100
    CompileRun(unwinding_win64_test_source);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(
        env->Global()->Get(env.local(), v8_str("start")).ToLocalChecked());

    CompileRun("%OptimizeFunctionOnNextCall(start);");

    int32_t repeat_count = 100;
    v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
    function->Call(env.local(), env.local()->Global(), arraysize(args), args)
        .ToLocalChecked();
  }
  isolate->Exit();
  isolate->Dispose();

#endif  // V8_WIN64_UNWINDING_INFO
}
116 117

#undef CONTEXT_PC