1// Copyright 2009 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// Load Splay tree and CodeMap implementations from <project root>/tools.
29// Files: tools/splaytree.js tools/codemap.js
30
31
32function newCodeEntry(size, name) {
33  return new CodeMap.CodeEntry(size, name);
34};
35
36
37function assertEntry(codeMap, expected_name, addr) {
38  var entry = codeMap.findEntry(addr);
39  assertNotNull(entry, 'no entry at ' + addr.toString(16));
40  assertEquals(expected_name, entry.name, 'at ' + addr.toString(16));
41};
42
43
44function assertNoEntry(codeMap, addr) {
45  assertNull(codeMap.findEntry(addr), 'at ' + addr.toString(16));
46};
47
48
49(function testLibrariesAndStaticCode() {
50  var codeMap = new CodeMap();
51  codeMap.addLibrary(0x1500, newCodeEntry(0x3000, 'lib1'));
52  codeMap.addLibrary(0x15500, newCodeEntry(0x5000, 'lib2'));
53  codeMap.addLibrary(0x155500, newCodeEntry(0x10000, 'lib3'));
54  assertNoEntry(codeMap, 0);
55  assertNoEntry(codeMap, 0x1500 - 1);
56  assertEntry(codeMap, 'lib1', 0x1500);
57  assertEntry(codeMap, 'lib1', 0x1500 + 0x100);
58  assertEntry(codeMap, 'lib1', 0x1500 + 0x1000);
59  assertEntry(codeMap, 'lib1', 0x1500 + 0x3000 - 1);
60  assertNoEntry(codeMap, 0x1500 + 0x3000);
61  assertNoEntry(codeMap, 0x15500 - 1);
62  assertEntry(codeMap, 'lib2', 0x15500);
63  assertEntry(codeMap, 'lib2', 0x15500 + 0x100);
64  assertEntry(codeMap, 'lib2', 0x15500 + 0x1000);
65  assertEntry(codeMap, 'lib2', 0x15500 + 0x5000 - 1);
66  assertNoEntry(codeMap, 0x15500 + 0x5000);
67  assertNoEntry(codeMap, 0x155500 - 1);
68  assertEntry(codeMap, 'lib3', 0x155500);
69  assertEntry(codeMap, 'lib3', 0x155500 + 0x100);
70  assertEntry(codeMap, 'lib3', 0x155500 + 0x1000);
71  assertEntry(codeMap, 'lib3', 0x155500 + 0x10000 - 1);
72  assertNoEntry(codeMap, 0x155500 + 0x10000);
73  assertNoEntry(codeMap, 0xFFFFFFFF);
74
75  codeMap.addStaticCode(0x1510, newCodeEntry(0x30, 'lib1-f1'));
76  codeMap.addStaticCode(0x1600, newCodeEntry(0x50, 'lib1-f2'));
77  codeMap.addStaticCode(0x15520, newCodeEntry(0x100, 'lib2-f1'));
78  assertEntry(codeMap, 'lib1', 0x1500);
79  assertEntry(codeMap, 'lib1', 0x1510 - 1);
80  assertEntry(codeMap, 'lib1-f1', 0x1510);
81  assertEntry(codeMap, 'lib1-f1', 0x1510 + 0x15);
82  assertEntry(codeMap, 'lib1-f1', 0x1510 + 0x30 - 1);
83  assertEntry(codeMap, 'lib1', 0x1510 + 0x30);
84  assertEntry(codeMap, 'lib1', 0x1600 - 1);
85  assertEntry(codeMap, 'lib1-f2', 0x1600);
86  assertEntry(codeMap, 'lib1-f2', 0x1600 + 0x30);
87  assertEntry(codeMap, 'lib1-f2', 0x1600 + 0x50 - 1);
88  assertEntry(codeMap, 'lib1', 0x1600 + 0x50);
89  assertEntry(codeMap, 'lib2', 0x15500);
90  assertEntry(codeMap, 'lib2', 0x15520 - 1);
91  assertEntry(codeMap, 'lib2-f1', 0x15520);
92  assertEntry(codeMap, 'lib2-f1', 0x15520 + 0x80);
93  assertEntry(codeMap, 'lib2-f1', 0x15520 + 0x100 - 1);
94  assertEntry(codeMap, 'lib2', 0x15520 + 0x100);
95
96})();
97
98
99(function testDynamicCode() {
100  var codeMap = new CodeMap();
101  codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
102  codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
103  codeMap.addCode(0x1900, newCodeEntry(0x50, 'code3'));
104  codeMap.addCode(0x1950, newCodeEntry(0x10, 'code4'));
105  assertNoEntry(codeMap, 0);
106  assertNoEntry(codeMap, 0x1500 - 1);
107  assertEntry(codeMap, 'code1', 0x1500);
108  assertEntry(codeMap, 'code1', 0x1500 + 0x100);
109  assertEntry(codeMap, 'code1', 0x1500 + 0x200 - 1);
110  assertEntry(codeMap, 'code2', 0x1700);
111  assertEntry(codeMap, 'code2', 0x1700 + 0x50);
112  assertEntry(codeMap, 'code2', 0x1700 + 0x100 - 1);
113  assertNoEntry(codeMap, 0x1700 + 0x100);
114  assertNoEntry(codeMap, 0x1900 - 1);
115  assertEntry(codeMap, 'code3', 0x1900);
116  assertEntry(codeMap, 'code3', 0x1900 + 0x28);
117  assertEntry(codeMap, 'code4', 0x1950);
118  assertEntry(codeMap, 'code4', 0x1950 + 0x7);
119  assertEntry(codeMap, 'code4', 0x1950 + 0x10 - 1);
120  assertNoEntry(codeMap, 0x1950 + 0x10);
121  assertNoEntry(codeMap, 0xFFFFFFFF);
122})();
123
124
125(function testCodeMovesAndDeletions() {
126  var codeMap = new CodeMap();
127  codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
128  codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
129  assertEntry(codeMap, 'code1', 0x1500);
130  assertEntry(codeMap, 'code2', 0x1700);
131  codeMap.moveCode(0x1500, 0x1800);
132  assertNoEntry(codeMap, 0x1500);
133  assertEntry(codeMap, 'code2', 0x1700);
134  assertEntry(codeMap, 'code1', 0x1800);
135  codeMap.deleteCode(0x1700);
136  assertNoEntry(codeMap, 0x1700);
137  assertEntry(codeMap, 'code1', 0x1800);
138})();
139
140
141(function testDynamicNamesDuplicates() {
142  var codeMap = new CodeMap();
143  // Code entries with same names but different addresses.
144  codeMap.addCode(0x1500, newCodeEntry(0x200, 'code'));
145  codeMap.addCode(0x1700, newCodeEntry(0x100, 'code'));
146  assertEntry(codeMap, 'code', 0x1500);
147  assertEntry(codeMap, 'code {1}', 0x1700);
148  // Test name stability.
149  assertEntry(codeMap, 'code', 0x1500);
150  assertEntry(codeMap, 'code {1}', 0x1700);
151})();
152
153
154(function testStaticEntriesExport() {
155  var codeMap = new CodeMap();
156  codeMap.addStaticCode(0x1500, newCodeEntry(0x3000, 'lib1'));
157  codeMap.addStaticCode(0x15500, newCodeEntry(0x5000, 'lib2'));
158  codeMap.addStaticCode(0x155500, newCodeEntry(0x10000, 'lib3'));
159  var allStatics = codeMap.getAllStaticEntries();
160  allStatics.sort();
161  assertEquals(['lib1: 3000', 'lib2: 5000', 'lib3: 10000'], allStatics);
162})();
163
164
165(function testDynamicEntriesExport() {
166  var codeMap = new CodeMap();
167  codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
168  codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
169  codeMap.addCode(0x1900, newCodeEntry(0x50, 'code3'));
170  var allDynamics = codeMap.getAllDynamicEntries();
171  allDynamics.sort();
172  assertEquals(['code1: 200', 'code2: 100', 'code3: 50'], allDynamics);
173  codeMap.deleteCode(0x1700);
174  var allDynamics2 = codeMap.getAllDynamicEntries();
175  allDynamics2.sort();
176  assertEquals(['code1: 200', 'code3: 50'], allDynamics2);
177  codeMap.deleteCode(0x1500);
178  var allDynamics3 = codeMap.getAllDynamicEntries();
179  assertEquals(['code3: 50'], allDynamics3);
180})();
181