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