From 296b3d6311186c7444d9d6763f8ee2d3c373dc14 Mon Sep 17 00:00:00 2001 From: Remco Bloemen Date: Thu, 20 Dec 2018 15:27:12 -0800 Subject: Throw error when source location is missing --- packages/sol-tracing-utils/src/source_maps.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'packages/sol-tracing-utils/src/source_maps.ts') diff --git a/packages/sol-tracing-utils/src/source_maps.ts b/packages/sol-tracing-utils/src/source_maps.ts index af0fb4035..ceb20d843 100644 --- a/packages/sol-tracing-utils/src/source_maps.ts +++ b/packages/sol-tracing-utils/src/source_maps.ts @@ -67,13 +67,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. -- cgit v1.2.3 From 2581bc93e5893d43642b240d290c141f0d9419bf Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 14 Jan 2019 12:02:32 +0100 Subject: Fix the bug with incorrect source maps parsing by changing contract data from an array to a mapping --- packages/sol-tracing-utils/src/source_maps.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'packages/sol-tracing-utils/src/source_maps.ts') diff --git a/packages/sol-tracing-utils/src/source_maps.ts b/packages/sol-tracing-utils/src/source_maps.ts index ceb20d843..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 } = {}; -- cgit v1.2.3 From 1c279f97ce9fc0025c554fd7bb58f3941a9fc77c Mon Sep 17 00:00:00 2001 From: Fabio B Date: Mon, 14 Jan 2019 13:51:01 +0100 Subject: Update packages/sol-tracing-utils/src/source_maps.ts Co-Authored-By: LogvinovLeon --- packages/sol-tracing-utils/src/source_maps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/sol-tracing-utils/src/source_maps.ts') diff --git a/packages/sol-tracing-utils/src/source_maps.ts b/packages/sol-tracing-utils/src/source_maps.ts index c674d32a3..9f7f6547f 100644 --- a/packages/sol-tracing-utils/src/source_maps.ts +++ b/packages/sol-tracing-utils/src/source_maps.ts @@ -36,7 +36,7 @@ export function getLocationByOffset(str: string): LocationByOffset { * @param sourceCodes sources contents by index * @param srcMap source map string * @param bytecodeHex contract bytecode - * @param sources sources file names by index + * @param indexToSource index to source file path */ export function parseSourceMap( sourceCodes: { [fileIndex: number]: string }, -- cgit v1.2.3 From 1f7179b1788d3e05163e8e5244a31470b01f8ca7 Mon Sep 17 00:00:00 2001 From: Fabio B Date: Mon, 14 Jan 2019 14:29:01 +0100 Subject: Update packages/sol-tracing-utils/src/source_maps.ts Co-Authored-By: LogvinovLeon --- packages/sol-tracing-utils/src/source_maps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/sol-tracing-utils/src/source_maps.ts') diff --git a/packages/sol-tracing-utils/src/source_maps.ts b/packages/sol-tracing-utils/src/source_maps.ts index 9f7f6547f..ae004c2ec 100644 --- a/packages/sol-tracing-utils/src/source_maps.ts +++ b/packages/sol-tracing-utils/src/source_maps.ts @@ -33,7 +33,7 @@ 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 by index + * @param indexToSourceCode index to source code * @param srcMap source map string * @param bytecodeHex contract bytecode * @param indexToSource index to source file path -- cgit v1.2.3 From 83b46cbf71abd897b434ff510035171297a51255 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 14 Jan 2019 14:48:59 +0100 Subject: Rename mappins to have a direct naming scheme instead of a reverse one --- packages/sol-tracing-utils/src/source_maps.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'packages/sol-tracing-utils/src/source_maps.ts') diff --git a/packages/sol-tracing-utils/src/source_maps.ts b/packages/sol-tracing-utils/src/source_maps.ts index ae004c2ec..0a2d3c88a 100644 --- a/packages/sol-tracing-utils/src/source_maps.ts +++ b/packages/sol-tracing-utils/src/source_maps.ts @@ -1,7 +1,7 @@ import * as _ from 'lodash'; import { getPcToInstructionIndexMapping } from './instructions'; -import { LocationByOffset, SourceRange } from './types'; +import { OffsetToLocation, SourceRange } from './types'; const RADIX = 10; @@ -15,19 +15,19 @@ export interface SourceLocation { * Receives a string with newlines and returns a map of byte offset to LineColumn * @param str A string to process */ -export function getLocationByOffset(str: string): LocationByOffset { - const locationByOffset: LocationByOffset = { 0: { line: 1, column: 0 } }; +export function getOffsetToLocation(str: string): OffsetToLocation { + const offsetToLocation: OffsetToLocation = { 0: { line: 1, column: 0 } }; let currentOffset = 0; for (const char of str.split('')) { - const location = locationByOffset[currentOffset]; + const location = offsetToLocation[currentOffset]; const isNewline = char === '\n'; - locationByOffset[currentOffset + 1] = { + offsetToLocation[currentOffset + 1] = { line: location.line + (isNewline ? 1 : 0), column: isNewline ? 0 : location.column + 1, }; currentOffset++; } - return locationByOffset; + return offsetToLocation; } /** @@ -46,9 +46,9 @@ export function parseSourceMap( ): { [programCounter: number]: SourceRange } { const bytecode = Uint8Array.from(Buffer.from(bytecodeHex, 'hex')); const pcToInstructionIndex: { [programCounter: number]: number } = getPcToInstructionIndexMapping(bytecode); - const locationByOffsetByFileIndex: { [fileIndex: number]: LocationByOffset } = {}; + const fileIndexToOffsetToLocation: { [fileIndex: number]: OffsetToLocation } = {}; _.map(sourceCodes, (sourceCode: string, fileIndex: number) => { - locationByOffsetByFileIndex[fileIndex] = _.isUndefined(sourceCode) ? {} : getLocationByOffset(sourceCode); + fileIndexToOffsetToLocation[fileIndex] = _.isUndefined(sourceCode) ? {} : getOffsetToLocation(sourceCode); }); const entries = srcMap.split(';'); let lastParsedEntry: SourceLocation = {} as any; @@ -69,12 +69,12 @@ export function parseSourceMap( length, fileIndex, }; - if (parsedEntry.fileIndex !== -1 && !_.isUndefined(locationByOffsetByFileIndex[parsedEntry.fileIndex])) { - const locationByOffset = locationByOffsetByFileIndex[parsedEntry.fileIndex]; + if (parsedEntry.fileIndex !== -1 && !_.isUndefined(fileIndexToOffsetToLocation[parsedEntry.fileIndex])) { + const offsetToLocation = fileIndexToOffsetToLocation[parsedEntry.fileIndex]; const sourceRange = { location: { - start: locationByOffset[parsedEntry.offset], - end: locationByOffset[parsedEntry.offset + parsedEntry.length], + start: offsetToLocation[parsedEntry.offset], + end: offsetToLocation[parsedEntry.offset + parsedEntry.length], }, fileName: sources[parsedEntry.fileIndex], }; -- cgit v1.2.3 From 63a63543be74d9e8822b7b111aa46350a5f524d8 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 15 Jan 2019 13:33:24 +0100 Subject: Make mapping namings direct --- packages/sol-tracing-utils/src/source_maps.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'packages/sol-tracing-utils/src/source_maps.ts') diff --git a/packages/sol-tracing-utils/src/source_maps.ts b/packages/sol-tracing-utils/src/source_maps.ts index 0a2d3c88a..8c17652d9 100644 --- a/packages/sol-tracing-utils/src/source_maps.ts +++ b/packages/sol-tracing-utils/src/source_maps.ts @@ -1,7 +1,7 @@ import * as _ from 'lodash'; import { getPcToInstructionIndexMapping } from './instructions'; -import { OffsetToLocation, SourceRange } from './types'; +import { OffsetToLocation, SourceCodes, SourceRange, Sources } from './types'; const RADIX = 10; @@ -39,10 +39,10 @@ export function getOffsetToLocation(str: string): OffsetToLocation { * @param indexToSource index to source file path */ export function parseSourceMap( - sourceCodes: { [fileIndex: number]: string }, + sourceCodes: SourceCodes, srcMap: string, bytecodeHex: string, - sources: { [fileIndex: number]: string }, + sources: Sources, ): { [programCounter: number]: SourceRange } { const bytecode = Uint8Array.from(Buffer.from(bytecodeHex, 'hex')); const pcToInstructionIndex: { [programCounter: number]: number } = getPcToInstructionIndexMapping(bytecode); -- cgit v1.2.3