2020-09-28 13:50:56 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
import { SplayTree } from "./splaytree.mjs";
|
|
|
|
|
2022-03-02 09:56:03 +00:00
|
|
|
/**
|
|
|
|
* The number of alignment bits in a page address.
|
|
|
|
*/
|
|
|
|
const kPageAlignment = 12;
|
|
|
|
/**
|
|
|
|
* Page size in bytes.
|
|
|
|
*/
|
|
|
|
const kPageSize = 1 << kPageAlignment;
|
|
|
|
|
2020-09-28 13:50:56 +00:00
|
|
|
/**
|
|
|
|
* Constructs a mapper that maps addresses into code entries.
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
export class CodeMap {
|
2020-09-28 13:50:56 +00:00
|
|
|
/**
|
|
|
|
* Dynamic code entries. Used for JIT compiled code.
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
dynamics_ = new SplayTree();
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name generator for entries having duplicate names.
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
dynamicsNameGen_ = new NameGenerator();
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Static code entries. Used for statically compiled code.
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
statics_ = new SplayTree();
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Libraries entries. Used for the whole static code libraries.
|
|
|
|
*/
|
2020-11-03 09:17:12 +00:00
|
|
|
libraries_ = new SplayTree();
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of memory pages occupied with static code.
|
|
|
|
*/
|
2022-03-02 09:56:03 +00:00
|
|
|
pages_ = new Set();
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
|
2022-04-26 17:18:04 +00:00
|
|
|
/**
|
|
|
|
* Adds a code entry that might overlap with static code (e.g. for builtins).
|
|
|
|
*
|
|
|
|
* @param {number} start The starting address.
|
|
|
|
* @param {CodeEntry} codeEntry Code entry object.
|
|
|
|
*/
|
|
|
|
addAnyCode(start, codeEntry) {
|
|
|
|
const pageAddr = (start / kPageSize) | 0;
|
|
|
|
if (!this.pages_.has(pageAddr)) return this.addCode(start, codeEntry);
|
|
|
|
// We might have loaded static code (builtins, bytecode handlers)
|
|
|
|
// and we get more information later in v8.log with code-creation events.
|
|
|
|
// Overwrite the existing entries in this case.
|
|
|
|
let result = this.findInTree_(this.statics_, start);
|
|
|
|
if (result === null) return this.addCode(start, codeEntry);
|
|
|
|
|
|
|
|
const removedNode = this.statics_.remove(start);
|
|
|
|
this.deleteAllCoveredNodes_(
|
|
|
|
this.statics_, start, start + removedNode.value.size);
|
|
|
|
this.statics_.insert(start, codeEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Adds a dynamic (i.e. moveable and discardable) code entry.
|
|
|
|
*
|
|
|
|
* @param {number} start The starting address.
|
2022-04-26 17:18:04 +00:00
|
|
|
* @param {CodeEntry} codeEntry Code entry object.
|
2020-11-03 09:17:12 +00:00
|
|
|
*/
|
|
|
|
addCode(start, codeEntry) {
|
|
|
|
this.deleteAllCoveredNodes_(this.dynamics_, start, start + codeEntry.size);
|
|
|
|
this.dynamics_.insert(start, codeEntry);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Moves a dynamic code entry. Throws an exception if there is no dynamic
|
|
|
|
* code entry with the specified starting address.
|
|
|
|
*
|
|
|
|
* @param {number} from The starting address of the entry being moved.
|
|
|
|
* @param {number} to The destination address.
|
|
|
|
*/
|
|
|
|
moveCode(from, to) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const removedNode = this.dynamics_.remove(from);
|
2020-11-03 09:17:12 +00:00
|
|
|
this.deleteAllCoveredNodes_(this.dynamics_, to, to + removedNode.value.size);
|
|
|
|
this.dynamics_.insert(to, removedNode.value);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Discards a dynamic code entry. Throws an exception if there is no dynamic
|
|
|
|
* code entry with the specified starting address.
|
|
|
|
*
|
|
|
|
* @param {number} start The starting address of the entry being deleted.
|
|
|
|
*/
|
|
|
|
deleteCode(start) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const removedNode = this.dynamics_.remove(start);
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Adds a library entry.
|
|
|
|
*
|
|
|
|
* @param {number} start The starting address.
|
2022-04-26 17:18:04 +00:00
|
|
|
* @param {CodeEntry} codeEntry Code entry object.
|
2020-11-03 09:17:12 +00:00
|
|
|
*/
|
|
|
|
addLibrary(start, codeEntry) {
|
|
|
|
this.markPages_(start, start + codeEntry.size);
|
|
|
|
this.libraries_.insert(start, codeEntry);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Adds a static code entry.
|
|
|
|
*
|
|
|
|
* @param {number} start The starting address.
|
2022-04-26 17:18:04 +00:00
|
|
|
* @param {CodeEntry} codeEntry Code entry object.
|
2020-11-03 09:17:12 +00:00
|
|
|
*/
|
|
|
|
addStaticCode(start, codeEntry) {
|
|
|
|
this.statics_.insert(start, codeEntry);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
markPages_(start, end) {
|
2022-03-02 09:56:03 +00:00
|
|
|
for (let addr = start; addr <= end; addr += kPageSize) {
|
|
|
|
this.pages_.add((addr / kPageSize) | 0);
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
deleteAllCoveredNodes_(tree, start, end) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const to_delete = [];
|
|
|
|
let addr = end - 1;
|
2020-11-03 09:17:12 +00:00
|
|
|
while (addr >= start) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const node = tree.findGreatestLessThan(addr);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (node === null) break;
|
2020-11-10 11:47:40 +00:00
|
|
|
const start2 = node.key, end2 = start2 + node.value.size;
|
2020-11-03 09:17:12 +00:00
|
|
|
if (start2 < end && start < end2) to_delete.push(start2);
|
|
|
|
addr = start2 - 1;
|
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
for (let i = 0, l = to_delete.length; i < l; ++i) tree.remove(to_delete[i]);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
isAddressBelongsTo_(addr, node) {
|
|
|
|
return addr >= node.key && addr < (node.key + node.value.size);
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
findInTree_(tree, addr) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const node = tree.findGreatestLessThan(addr);
|
2022-03-02 09:56:03 +00:00
|
|
|
return node !== null && this.isAddressBelongsTo_(addr, node) ? node : null;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Finds a code entry that contains the specified address. Both static and
|
|
|
|
* dynamic code entries are considered. Returns the code entry and the offset
|
|
|
|
* within the entry.
|
|
|
|
*
|
|
|
|
* @param {number} addr Address.
|
|
|
|
*/
|
|
|
|
findAddress(addr) {
|
2022-03-02 09:56:03 +00:00
|
|
|
const pageAddr = (addr / kPageSize) | 0;
|
|
|
|
if (this.pages_.has(pageAddr)) {
|
2020-11-03 09:17:12 +00:00
|
|
|
// Static code entries can contain "holes" of unnamed code.
|
|
|
|
// In this case, the whole library is assigned to this address.
|
2020-11-10 11:47:40 +00:00
|
|
|
let result = this.findInTree_(this.statics_, addr);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (result === null) {
|
2020-11-03 09:17:12 +00:00
|
|
|
result = this.findInTree_(this.libraries_, addr);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (result === null) return null;
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2021-05-31 12:18:32 +00:00
|
|
|
return {entry: result.value, offset: addr - result.key};
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
const max = this.dynamics_.findMax();
|
2022-03-02 09:56:03 +00:00
|
|
|
if (max === null) return null;
|
|
|
|
const min = this.dynamics_.findMin();
|
|
|
|
if (addr >= min.key && addr < (max.key + max.value.size)) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const dynaEntry = this.findInTree_(this.dynamics_, addr);
|
2022-03-02 09:56:03 +00:00
|
|
|
if (dynaEntry === null) return null;
|
2020-11-03 09:17:12 +00:00
|
|
|
// Dedupe entry name.
|
2020-11-10 11:47:40 +00:00
|
|
|
const entry = dynaEntry.value;
|
2020-11-03 09:17:12 +00:00
|
|
|
if (!entry.nameUpdated_) {
|
|
|
|
entry.name = this.dynamicsNameGen_.getName(entry.name);
|
|
|
|
entry.nameUpdated_ = true;
|
|
|
|
}
|
2021-05-31 12:18:32 +00:00
|
|
|
return {entry, offset: addr - dynaEntry.key};
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
return null;
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Finds a code entry that contains the specified address. Both static and
|
|
|
|
* dynamic code entries are considered.
|
|
|
|
*
|
|
|
|
* @param {number} addr Address.
|
|
|
|
*/
|
|
|
|
findEntry(addr) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const result = this.findAddress(addr);
|
2022-03-02 09:56:03 +00:00
|
|
|
return result !== null ? result.entry : null;
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns a dynamic code entry using its starting address.
|
|
|
|
*
|
|
|
|
* @param {number} addr Address.
|
|
|
|
*/
|
|
|
|
findDynamicEntryByStartAddress(addr) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const node = this.dynamics_.find(addr);
|
2022-03-02 09:56:03 +00:00
|
|
|
return node !== null ? node.value : null;
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of all dynamic code entries.
|
|
|
|
*/
|
|
|
|
getAllDynamicEntries() {
|
|
|
|
return this.dynamics_.exportValues();
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of pairs of all dynamic code entries and their addresses.
|
|
|
|
*/
|
|
|
|
getAllDynamicEntriesWithAddresses() {
|
|
|
|
return this.dynamics_.exportKeysAndValues();
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of all static code entries.
|
|
|
|
*/
|
|
|
|
getAllStaticEntries() {
|
|
|
|
return this.statics_.exportValues();
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of pairs of all static code entries and their addresses.
|
|
|
|
*/
|
|
|
|
getAllStaticEntriesWithAddresses() {
|
|
|
|
return this.statics_.exportKeysAndValues();
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
/**
|
2021-04-20 16:57:36 +00:00
|
|
|
* Returns an array of all library entries.
|
2020-11-03 09:17:12 +00:00
|
|
|
*/
|
2021-04-20 16:57:36 +00:00
|
|
|
getAllLibraryEntries() {
|
2020-11-03 09:17:12 +00:00
|
|
|
return this.libraries_.exportValues();
|
|
|
|
}
|
2021-04-20 16:57:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of pairs of all library entries and their addresses.
|
|
|
|
*/
|
|
|
|
getAllLibraryEntriesWithAddresses() {
|
|
|
|
return this.libraries_.exportKeysAndValues();
|
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
export class CodeEntry {
|
|
|
|
constructor(size, opt_name, opt_type) {
|
2022-04-26 17:18:04 +00:00
|
|
|
/** @type {number} */
|
2020-11-03 09:17:12 +00:00
|
|
|
this.size = size;
|
2022-04-26 17:18:04 +00:00
|
|
|
/** @type {string} */
|
2020-11-03 09:17:12 +00:00
|
|
|
this.name = opt_name || '';
|
2022-04-26 17:18:04 +00:00
|
|
|
/** @type {string} */
|
2020-11-03 09:17:12 +00:00
|
|
|
this.type = opt_type || '';
|
|
|
|
this.nameUpdated_ = false;
|
2022-04-26 17:18:04 +00:00
|
|
|
/** @type {?string} */
|
2020-11-30 10:53:12 +00:00
|
|
|
this.source = undefined;
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
getName() {
|
|
|
|
return this.name;
|
|
|
|
}
|
2020-09-28 13:50:56 +00:00
|
|
|
|
2020-11-03 09:17:12 +00:00
|
|
|
toString() {
|
|
|
|
return this.name + ': ' + this.size.toString(16);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
2020-12-09 08:01:26 +00:00
|
|
|
|
|
|
|
getSourceCode() {
|
|
|
|
return '';
|
|
|
|
}
|
2022-04-26 17:18:04 +00:00
|
|
|
|
|
|
|
get sourcePosition() {
|
|
|
|
return this.logEntry.sourcePosition;
|
|
|
|
}
|
2020-11-03 09:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class NameGenerator {
|
|
|
|
knownNames_ = { __proto__:null }
|
|
|
|
getName(name) {
|
|
|
|
if (!(name in this.knownNames_)) {
|
|
|
|
this.knownNames_[name] = 0;
|
|
|
|
return name;
|
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
const count = ++this.knownNames_[name];
|
2020-11-03 09:17:12 +00:00
|
|
|
return name + ' {' + count + '}';
|
|
|
|
};
|
|
|
|
}
|