1// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
29
30#include "cctest.h"
31#include "platform.h"
32
33using namespace ::v8::internal;
34
35TEST(NumberOfCores) {
36  CHECK_GT(OS::NumberOfCores(), 0);
37}
38
39
40#ifdef __GNUC__
41#define ASM __asm__ __volatile__
42
43#if defined(_M_X64) || defined(__x86_64__)
44#define GET_STACK_POINTER() \
45  static int sp_addr = 0; \
46  do { \
47    ASM("mov %%rsp, %0" : "=g" (sp_addr)); \
48  } while (0)
49#elif defined(_M_IX86) || defined(__i386__)
50#define GET_STACK_POINTER() \
51  static int sp_addr = 0; \
52  do { \
53    ASM("mov %%esp, %0" : "=g" (sp_addr)); \
54  } while (0)
55#elif defined(__ARMEL__)
56#define GET_STACK_POINTER() \
57  static int sp_addr = 0; \
58  do { \
59    ASM("str %%sp, %0" : "=g" (sp_addr)); \
60  } while (0)
61#elif defined(__MIPSEL__)
62#define GET_STACK_POINTER() \
63  static int sp_addr = 0; \
64  do { \
65    ASM("sw $sp, %0" : "=g" (sp_addr)); \
66  } while (0)
67#else
68#error Host architecture was not detected as supported by v8
69#endif
70
71void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
72  GET_STACK_POINTER();
73  args.GetReturnValue().Set(v8_num(sp_addr));
74}
75
76
77TEST(StackAlignment) {
78  v8::Isolate* isolate = v8::Isolate::GetCurrent();
79  v8::HandleScope handle_scope(isolate);
80  v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
81  global_template->Set(v8_str("get_stack_pointer"),
82                       v8::FunctionTemplate::New(GetStackPointer));
83
84  LocalContext env(NULL, global_template);
85  CompileRun(
86      "function foo() {"
87      "  return get_stack_pointer();"
88      "}");
89
90  v8::Local<v8::Object> global_object = env->Global();
91  v8::Local<v8::Function> foo =
92      v8::Local<v8::Function>::Cast(global_object->Get(v8_str("foo")));
93
94  v8::Local<v8::Value> result = foo->Call(global_object, 0, NULL);
95  CHECK_EQ(0, result->Int32Value() % OS::ActivationFrameAlignment());
96}
97
98#undef GET_STACK_POINTERS
99#undef ASM
100#endif  // __GNUC__
101