aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-tracing-utils/src/source_maps.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/sol-tracing-utils/src/source_maps.ts')
-rw-r--r--packages/sol-tracing-utils/src/source_maps.ts21
1 files changed, 14 insertions, 7 deletions
diff --git a/packages/sol-tracing-utils/src/source_maps.ts b/packages/sol-tracing-utils/src/source_maps.ts
index af0fb4035..c674d32a3 100644
--- a/packages/sol-tracing-utils/src/source_maps.ts
+++ b/packages/sol-tracing-utils/src/source_maps.ts
@@ -33,20 +33,23 @@ export function getLocationByOffset(str: string): LocationByOffset {
/**
* Parses a sourcemap string.
* The solidity sourcemap format is documented here: https://github.com/ethereum/solidity/blob/develop/docs/miscellaneous.rst#source-mappings
- * @param sourceCodes sources contents
+ * @param sourceCodes sources contents by index
* @param srcMap source map string
* @param bytecodeHex contract bytecode
- * @param sources sources file names
+ * @param sources sources file names by index
*/
export function parseSourceMap(
- sourceCodes: string[],
+ sourceCodes: { [fileIndex: number]: string },
srcMap: string,
bytecodeHex: string,
- sources: string[],
+ sources: { [fileIndex: number]: string },
): { [programCounter: number]: SourceRange } {
const bytecode = Uint8Array.from(Buffer.from(bytecodeHex, 'hex'));
const pcToInstructionIndex: { [programCounter: number]: number } = getPcToInstructionIndexMapping(bytecode);
- const locationByOffsetByFileIndex = _.map(sourceCodes, s => (_.isUndefined(s) ? {} : getLocationByOffset(s)));
+ const locationByOffsetByFileIndex: { [fileIndex: number]: LocationByOffset } = {};
+ _.map(sourceCodes, (sourceCode: string, fileIndex: number) => {
+ locationByOffsetByFileIndex[fileIndex] = _.isUndefined(sourceCode) ? {} : getLocationByOffset(sourceCode);
+ });
const entries = srcMap.split(';');
let lastParsedEntry: SourceLocation = {} as any;
const instructionIndexToSourceRange: { [instructionIndex: number]: SourceRange } = {};
@@ -67,13 +70,17 @@ export function parseSourceMap(
fileIndex,
};
if (parsedEntry.fileIndex !== -1 && !_.isUndefined(locationByOffsetByFileIndex[parsedEntry.fileIndex])) {
+ const locationByOffset = locationByOffsetByFileIndex[parsedEntry.fileIndex];
const sourceRange = {
location: {
- start: locationByOffsetByFileIndex[parsedEntry.fileIndex][parsedEntry.offset],
- end: locationByOffsetByFileIndex[parsedEntry.fileIndex][parsedEntry.offset + parsedEntry.length],
+ start: locationByOffset[parsedEntry.offset],
+ end: locationByOffset[parsedEntry.offset + parsedEntry.length],
},
fileName: sources[parsedEntry.fileIndex],
};
+ if (sourceRange.location.start === undefined || sourceRange.location.end === undefined) {
+ throw new Error(`Error while processing sourcemap: location out of range in ${sourceRange.fileName}`);
+ }
instructionIndexToSourceRange[i] = sourceRange;
} else {
// Some assembly code generated by Solidity can't be mapped back to a line of source code.