codemap.js 7.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Copyright 2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Load Splay tree and CodeMap implementations from <project root>/tools.
// Files: tools/splaytree.js tools/codemap.js


function newCodeEntry(size, name) {
33
  return new CodeMap.CodeEntry(size, name);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
};


function assertEntry(codeMap, expected_name, addr) {
  var entry = codeMap.findEntry(addr);
  assertNotNull(entry, 'no entry at ' + addr.toString(16));
  assertEquals(expected_name, entry.name, 'at ' + addr.toString(16));
};


function assertNoEntry(codeMap, addr) {
  assertNull(codeMap.findEntry(addr), 'at ' + addr.toString(16));
};


49
(function testLibrariesAndStaticCode() {
50
  var codeMap = new CodeMap();
51 52 53
  codeMap.addLibrary(0x1500, newCodeEntry(0x3000, 'lib1'));
  codeMap.addLibrary(0x15500, newCodeEntry(0x5000, 'lib2'));
  codeMap.addLibrary(0x155500, newCodeEntry(0x10000, 'lib3'));
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  assertNoEntry(codeMap, 0);
  assertNoEntry(codeMap, 0x1500 - 1);
  assertEntry(codeMap, 'lib1', 0x1500);
  assertEntry(codeMap, 'lib1', 0x1500 + 0x100);
  assertEntry(codeMap, 'lib1', 0x1500 + 0x1000);
  assertEntry(codeMap, 'lib1', 0x1500 + 0x3000 - 1);
  assertNoEntry(codeMap, 0x1500 + 0x3000);
  assertNoEntry(codeMap, 0x15500 - 1);
  assertEntry(codeMap, 'lib2', 0x15500);
  assertEntry(codeMap, 'lib2', 0x15500 + 0x100);
  assertEntry(codeMap, 'lib2', 0x15500 + 0x1000);
  assertEntry(codeMap, 'lib2', 0x15500 + 0x5000 - 1);
  assertNoEntry(codeMap, 0x15500 + 0x5000);
  assertNoEntry(codeMap, 0x155500 - 1);
  assertEntry(codeMap, 'lib3', 0x155500);
  assertEntry(codeMap, 'lib3', 0x155500 + 0x100);
  assertEntry(codeMap, 'lib3', 0x155500 + 0x1000);
  assertEntry(codeMap, 'lib3', 0x155500 + 0x10000 - 1);
  assertNoEntry(codeMap, 0x155500 + 0x10000);
  assertNoEntry(codeMap, 0xFFFFFFFF);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

  codeMap.addStaticCode(0x1510, newCodeEntry(0x30, 'lib1-f1'));
  codeMap.addStaticCode(0x1600, newCodeEntry(0x50, 'lib1-f2'));
  codeMap.addStaticCode(0x15520, newCodeEntry(0x100, 'lib2-f1'));
  assertEntry(codeMap, 'lib1', 0x1500);
  assertEntry(codeMap, 'lib1', 0x1510 - 1);
  assertEntry(codeMap, 'lib1-f1', 0x1510);
  assertEntry(codeMap, 'lib1-f1', 0x1510 + 0x15);
  assertEntry(codeMap, 'lib1-f1', 0x1510 + 0x30 - 1);
  assertEntry(codeMap, 'lib1', 0x1510 + 0x30);
  assertEntry(codeMap, 'lib1', 0x1600 - 1);
  assertEntry(codeMap, 'lib1-f2', 0x1600);
  assertEntry(codeMap, 'lib1-f2', 0x1600 + 0x30);
  assertEntry(codeMap, 'lib1-f2', 0x1600 + 0x50 - 1);
  assertEntry(codeMap, 'lib1', 0x1600 + 0x50);
  assertEntry(codeMap, 'lib2', 0x15500);
  assertEntry(codeMap, 'lib2', 0x15520 - 1);
  assertEntry(codeMap, 'lib2-f1', 0x15520);
  assertEntry(codeMap, 'lib2-f1', 0x15520 + 0x80);
  assertEntry(codeMap, 'lib2-f1', 0x15520 + 0x100 - 1);
  assertEntry(codeMap, 'lib2', 0x15520 + 0x100);

96 97 98 99
})();


(function testDynamicCode() {
100
  var codeMap = new CodeMap();
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
  codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
  codeMap.addCode(0x1900, newCodeEntry(0x50, 'code3'));
  codeMap.addCode(0x1950, newCodeEntry(0x10, 'code4'));
  assertNoEntry(codeMap, 0);
  assertNoEntry(codeMap, 0x1500 - 1);
  assertEntry(codeMap, 'code1', 0x1500);
  assertEntry(codeMap, 'code1', 0x1500 + 0x100);
  assertEntry(codeMap, 'code1', 0x1500 + 0x200 - 1);
  assertEntry(codeMap, 'code2', 0x1700);
  assertEntry(codeMap, 'code2', 0x1700 + 0x50);
  assertEntry(codeMap, 'code2', 0x1700 + 0x100 - 1);
  assertNoEntry(codeMap, 0x1700 + 0x100);
  assertNoEntry(codeMap, 0x1900 - 1);
  assertEntry(codeMap, 'code3', 0x1900);
  assertEntry(codeMap, 'code3', 0x1900 + 0x28);
  assertEntry(codeMap, 'code4', 0x1950);
  assertEntry(codeMap, 'code4', 0x1950 + 0x7);
  assertEntry(codeMap, 'code4', 0x1950 + 0x10 - 1);
  assertNoEntry(codeMap, 0x1950 + 0x10);
  assertNoEntry(codeMap, 0xFFFFFFFF);
})();


(function testCodeMovesAndDeletions() {
126
  var codeMap = new CodeMap();
127 128 129 130 131 132 133 134 135 136 137 138
  codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
  codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
  assertEntry(codeMap, 'code1', 0x1500);
  assertEntry(codeMap, 'code2', 0x1700);
  codeMap.moveCode(0x1500, 0x1800);
  assertNoEntry(codeMap, 0x1500);
  assertEntry(codeMap, 'code2', 0x1700);
  assertEntry(codeMap, 'code1', 0x1800);
  codeMap.deleteCode(0x1700);
  assertNoEntry(codeMap, 0x1700);
  assertEntry(codeMap, 'code1', 0x1800);
})();
139 140 141


(function testDynamicNamesDuplicates() {
142
  var codeMap = new CodeMap();
143 144 145 146 147
  // Code entries with same names but different addresses.
  codeMap.addCode(0x1500, newCodeEntry(0x200, 'code'));
  codeMap.addCode(0x1700, newCodeEntry(0x100, 'code'));
  assertEntry(codeMap, 'code', 0x1500);
  assertEntry(codeMap, 'code {1}', 0x1700);
148 149 150 151 152 153 154
  // Test name stability.
  assertEntry(codeMap, 'code', 0x1500);
  assertEntry(codeMap, 'code {1}', 0x1700);
})();


(function testStaticEntriesExport() {
155
  var codeMap = new CodeMap();
156 157 158 159
  codeMap.addStaticCode(0x1500, newCodeEntry(0x3000, 'lib1'));
  codeMap.addStaticCode(0x15500, newCodeEntry(0x5000, 'lib2'));
  codeMap.addStaticCode(0x155500, newCodeEntry(0x10000, 'lib3'));
  var allStatics = codeMap.getAllStaticEntries();
160
  allStatics = allStatics.map(String);
161 162 163 164 165 166
  allStatics.sort();
  assertEquals(['lib1: 3000', 'lib2: 5000', 'lib3: 10000'], allStatics);
})();


(function testDynamicEntriesExport() {
167
  var codeMap = new CodeMap();
168 169 170 171
  codeMap.addCode(0x1500, newCodeEntry(0x200, 'code1'));
  codeMap.addCode(0x1700, newCodeEntry(0x100, 'code2'));
  codeMap.addCode(0x1900, newCodeEntry(0x50, 'code3'));
  var allDynamics = codeMap.getAllDynamicEntries();
172
  allDynamics = allDynamics.map(String);
173 174 175 176
  allDynamics.sort();
  assertEquals(['code1: 200', 'code2: 100', 'code3: 50'], allDynamics);
  codeMap.deleteCode(0x1700);
  var allDynamics2 = codeMap.getAllDynamicEntries();
177
  allDynamics2 = allDynamics2.map(String);
178 179 180 181
  allDynamics2.sort();
  assertEquals(['code1: 200', 'code3: 50'], allDynamics2);
  codeMap.deleteCode(0x1500);
  var allDynamics3 = codeMap.getAllDynamicEntries();
182
  assertEquals(['code3: 50'], allDynamics3.map(String));
183
})();