1// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/objects.h"
6#include "test/cctest/cctest.h"
7
8using namespace v8::internal;
9
10TEST(CodeLayoutWithoutUnwindingInfo) {
11  CcTest::InitializeVM();
12  HandleScope handle_scope(CcTest::i_isolate());
13
14  // "Hello, World!" in ASCII.
15  byte buffer_array[13] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20,
16                           0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21};
17
18  byte* buffer = &buffer_array[0];
19  int buffer_size = sizeof(buffer_array);
20
21  CodeDesc code_desc;
22  code_desc.buffer = buffer;
23  code_desc.buffer_size = buffer_size;
24  code_desc.constant_pool_size = 0;
25  code_desc.instr_size = buffer_size;
26  code_desc.reloc_size = 0;
27  code_desc.origin = nullptr;
28  code_desc.unwinding_info = nullptr;
29  code_desc.unwinding_info_size = 0;
30
31  Handle<Code> code = CcTest::i_isolate()->factory()->NewCode(
32      code_desc, 0, Handle<Object>::null());
33
34  CHECK(!code->has_unwinding_info());
35  CHECK_EQ(code->instruction_size(), buffer_size);
36  CHECK_EQ(memcmp(code->instruction_start(), buffer, buffer_size), 0);
37  CHECK_EQ(code->instruction_end() - reinterpret_cast<byte*>(*code),
38           Code::kHeaderSize + buffer_size - kHeapObjectTag);
39}
40
41TEST(CodeLayoutWithUnwindingInfo) {
42  CcTest::InitializeVM();
43  HandleScope handle_scope(CcTest::i_isolate());
44
45  // "Hello, World!" in ASCII.
46  byte buffer_array[13] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20,
47                           0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21};
48
49  // "JavaScript" in ASCII.
50  byte unwinding_info_array[10] = {0x4a, 0x61, 0x76, 0x61, 0x53,
51                                   0x63, 0x72, 0x69, 0x70, 0x74};
52
53  byte* buffer = &buffer_array[0];
54  int buffer_size = sizeof(buffer_array);
55  byte* unwinding_info = &unwinding_info_array[0];
56  int unwinding_info_size = sizeof(unwinding_info_array);
57
58  CodeDesc code_desc;
59  code_desc.buffer = buffer;
60  code_desc.buffer_size = buffer_size;
61  code_desc.constant_pool_size = 0;
62  code_desc.instr_size = buffer_size;
63  code_desc.reloc_size = 0;
64  code_desc.origin = nullptr;
65  code_desc.unwinding_info = unwinding_info;
66  code_desc.unwinding_info_size = unwinding_info_size;
67
68  Handle<Code> code = CcTest::i_isolate()->factory()->NewCode(
69      code_desc, 0, Handle<Object>::null());
70
71  CHECK(code->has_unwinding_info());
72  CHECK_EQ(code->instruction_size(), buffer_size);
73  CHECK_EQ(memcmp(code->instruction_start(), buffer, buffer_size), 0);
74  CHECK(IsAligned(code->GetUnwindingInfoSizeOffset(), 8));
75  CHECK_EQ(code->unwinding_info_size(), unwinding_info_size);
76  CHECK(
77      IsAligned(reinterpret_cast<uintptr_t>(code->unwinding_info_start()), 8));
78  CHECK_EQ(
79      memcmp(code->unwinding_info_start(), unwinding_info, unwinding_info_size),
80      0);
81  CHECK_EQ(code->unwinding_info_end() - reinterpret_cast<byte*>(*code),
82           Code::kHeaderSize + RoundUp(buffer_size, kInt64Size) + kInt64Size +
83               unwinding_info_size - kHeapObjectTag);
84}
85