test-platform.cc 2.06 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include <stdint.h>
6
#include "src/base/build_config.h"
7
#include "src/base/platform/platform.h"
8
#include "test/cctest/cctest.h"
9

10
#ifdef V8_CC_GNU
11

12
static uintptr_t sp_addr = 0;
13

14 15 16 17 18 19 20 21 22 23 24
void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
#if V8_HOST_ARCH_X64
  __asm__ __volatile__("mov %%rsp, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_IA32
  __asm__ __volatile__("mov %%esp, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_ARM
  __asm__ __volatile__("str %%sp, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_ARM64
  __asm__ __volatile__("mov x16, sp; str x16, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_MIPS
  __asm__ __volatile__("sw $sp, %0" : "=g"(sp_addr));
25 26
#elif V8_HOST_ARCH_MIPS64
  __asm__ __volatile__("sd $sp, %0" : "=g"(sp_addr));
27 28 29 30
#elif defined(__PPC64__) || defined(_ARCH_PPC64)
  __asm__ __volatile__("std 1, %0" : "=g"(sp_addr));
#elif defined(__PPC__) || defined(_ARCH_PPC)
  __asm__ __volatile__("stw 1, %0" : "=g"(sp_addr));
31 32 33 34
#else
#error Host architecture was not detected as supported by v8
#endif

35 36
  args.GetReturnValue().Set(v8::Integer::NewFromUnsigned(
      args.GetIsolate(), static_cast<uint32_t>(sp_addr)));
37 38
}

39

40
TEST(StackAlignment) {
41
  v8::Isolate* isolate = CcTest::isolate();
42
  v8::HandleScope handle_scope(isolate);
43 44
  v8::Handle<v8::ObjectTemplate> global_template =
      v8::ObjectTemplate::New(isolate);
45
  global_template->Set(v8_str("get_stack_pointer"),
46
                       v8::FunctionTemplate::New(isolate, GetStackPointer));
47 48 49 50 51 52 53 54 55 56 57 58

  LocalContext env(NULL, global_template);
  CompileRun(
      "function foo() {"
      "  return get_stack_pointer();"
      "}");

  v8::Local<v8::Object> global_object = env->Global();
  v8::Local<v8::Function> foo =
      v8::Local<v8::Function>::Cast(global_object->Get(v8_str("foo")));

  v8::Local<v8::Value> result = foo->Call(global_object, 0, NULL);
59 60
  CHECK_EQ(0u,
           result->Uint32Value() % v8::base::OS::ActivationFrameAlignment());
61 62
}

63
#endif  // V8_CC_GNU