Changeset 83748 in webkit


Ignore:
Timestamp:
Apr 13, 2011 9:45:42 AM (13 years ago)
Author:
podivilov@chromium.org
Message:

2011-04-12 Pavel Podivilov <podivilov@chromium.org>

Reviewed by Pavel Feldman.

Web Inspector: fix source mapping for deobfuscated scripts.
https://bugs.webkit.org/show_bug.cgi?id=58231

  • inspector/debugger/resources/obfuscated.js: Added.
  • inspector/debugger/script-formatter-expected.txt:
  • inspector/debugger/script-formatter.html:

2011-04-12 Pavel Podivilov <podivilov@chromium.org>

Reviewed by Pavel Feldman.

Web Inspector: fix source mapping for de-obfuscated scripts.
https://bugs.webkit.org/show_bug.cgi?id=58231

  • inspector/front-end/ScriptFormatterWorker.js: (buildMapping):
  • inspector/front-end/SourceFile.js: (WebInspector.SourceMapping): (WebInspector.SourceMapping.prototype.sourceLineToScriptLocation): (WebInspector.SourceMapping.prototype._sourceLocationToScriptLocation): (WebInspector): (WebInspector.FormattedSourceMapping): (WebInspector.FormattedSourceMapping.prototype.scriptLocationToSourceLine): (WebInspector.FormattedSourceMapping.prototype.sourceLineToScriptLocation):
Location:
trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r83744 r83748  
     12011-04-12  Pavel Podivilov  <podivilov@chromium.org>
     2
     3        Reviewed by Pavel Feldman.
     4
     5        Web Inspector: fix source mapping for deobfuscated scripts.
     6        https://bugs.webkit.org/show_bug.cgi?id=58231
     7
     8        * inspector/debugger/resources/obfuscated.js: Added.
     9        * inspector/debugger/script-formatter-expected.txt:
     10        * inspector/debugger/script-formatter.html:
     11
    1122011-04-13  Philippe Normand  <pnormand@igalia.com>
    213
  • trunk/LayoutTests/inspector/debugger/script-formatter-expected.txt

    r83284 r83748  
    11Tests the script formatting functionality.
    22
     3Debugger was enabled.
    34
    45Running: testScriptFormatterWorker
    56
     7Running: testSourceMapping
     8Debugger was disabled.
     9
  • trunk/LayoutTests/inspector/debugger/script-formatter.html

    r83284 r83748  
    33<script src="../../http/tests/inspector/inspector-test.js"></script>
    44<script src="../../http/tests/inspector/debugger-test.js"></script>
     5<script src="resources/obfuscated.js"></script>
    56
    67<script>
     
    89var test = function()
    910{
    10     InspectorTest.runTestSuite([
     11    var worker = new Worker("ScriptFormatterWorker.js");
     12
     13    InspectorTest.runDebuggerTestSuite([
    1114        function testScriptFormatterWorker(next)
    1215        {
    13             var worker = new Worker("ScriptFormatterWorker.js");
    14 
    1516            worker.onmessage = InspectorTest.safeWrap(function(event)
    1617            {
    1718                InspectorTest.assertEquals("var x = 0;", event.data.formattedSource);
    18                 InspectorTest.completeTest();
     19                next();
    1920            });
    2021
     
    2223            {
    2324                InspectorTest.addResult("Error in worker: " + event.data);
    24                 InspectorTest.completeTest();
     25                next();
    2526            };
    2627
    2728            worker.postMessage("var x=0");
     29        },
     30
     31        function testSourceMapping(next)
     32        {
     33            worker.onmessage = InspectorTest.safeWrap(function(event)
     34            {
     35                var source = WebInspector.panels.scripts.visibleView._content;
     36                var formattedSource = event.data.formattedSource;
     37                var mapping = event.data.mapping;
     38
     39                function testMapping(string)
     40                {
     41                    var position = source.indexOf(string);
     42                    var formattedPosition = mapping.formatted[mapping.original.upperBound(position - 1)];
     43                    InspectorTest.assertEquals(string, formattedSource.substr(formattedPosition, string.length));
     44                }
     45
     46                testMapping("onmessage");
     47                testMapping("indent_start");
     48                testMapping("function require");
     49                testMapping("var regexp");
     50                testMapping("importScripts");
     51
     52                next();
     53            });
     54
     55            worker.onerror = function(event)
     56            {
     57                InspectorTest.addResult("Error in worker: " + event.data);
     58                next();
     59            };
     60
     61            InspectorTest.showScriptSource("obfuscated.js", didShowScriptSource);
     62
     63            function didShowScriptSource(sourceFrame)
     64            {
     65                worker.postMessage(sourceFrame._content);
     66            }
    2867        }
    2968    ]);
  • trunk/Source/WebCore/ChangeLog

    r83747 r83748  
     12011-04-12  Pavel Podivilov  <podivilov@chromium.org>
     2
     3        Reviewed by Pavel Feldman.
     4
     5        Web Inspector: fix source mapping for de-obfuscated scripts.
     6        https://bugs.webkit.org/show_bug.cgi?id=58231
     7
     8        * inspector/front-end/ScriptFormatterWorker.js:
     9        (buildMapping):
     10        * inspector/front-end/SourceFile.js:
     11        (WebInspector.SourceMapping):
     12        (WebInspector.SourceMapping.prototype.sourceLineToScriptLocation):
     13        (WebInspector.SourceMapping.prototype._sourceLocationToScriptLocation):
     14        (WebInspector):
     15        (WebInspector.FormattedSourceMapping):
     16        (WebInspector.FormattedSourceMapping.prototype.scriptLocationToSourceLine):
     17        (WebInspector.FormattedSourceMapping.prototype.sourceLineToScriptLocation):
     18
    1192011-04-13  Mario Sanchez Prada  <msanchez@igalia.com>
    220
  • trunk/Source/WebCore/inspector/front-end/ScriptFormatterWorker.js

    r83284 r83748  
    5151{
    5252    var mapping = { original: [], formatted: [] };
    53     var lastFormattedPosition = 0;
    54     var regexp = /\b(?:function|var|const|try|catch|throw|new|switch|break|continue|if|for|while|do|return|with|null|undefined)\b/g;
     53    var lastPosition = 0;
     54    var regexp = /(^|[^\\])\b((?=\D)[\$\.\w]+)\b/g;
    5555    while (true) {
    56         var match = regexp.exec(source);
     56        var match = regexp.exec(formattedSource);
    5757        if (!match)
    5858            break;
    59         var formattedPosition = formattedSource.indexOf(match[0], lastFormattedPosition);
    60         mapping.original.push(match.index);
    61         mapping.formatted.push(formattedPosition);
    62         lastFormattedPosition = formattedPosition + match[0].length;
     59        var position = source.indexOf(match[2], lastPosition);
     60        if (position === -1)
     61            throw "No match found in original source for " + match[2];
     62        mapping.original.push(position);
     63        mapping.formatted.push(match.index + match[1].length);
     64        lastPosition = position + match[2].length;
    6365    }
    6466    return mapping;
  • trunk/Source/WebCore/inspector/front-end/SourceFile.js

    r83601 r83748  
    264264WebInspector.FormattedSourceFile.prototype.__proto__ = WebInspector.SourceFile.prototype;
    265265
    266 WebInspector.SourceMapping = function(sortedScripts)
     266WebInspector.SourceMapping = function(scripts)
    267267{
    268     this._sortedScripts = sortedScripts;
     268    this._sortedScripts = scripts.slice();
     269    this._sortedScripts.sort(function(x, y) { return x.lineOffset - y.lineOffset || x.columnOffset - y.columnOffset; });
    269270}
    270271
     
    277278    sourceLineToScriptLocation: function(lineNumber)
    278279    {
    279         var columnNumber = 0;
     280        return this._sourceLocationToScriptLocation(lineNumber, 0);
     281    },
     282
     283    _sourceLocationToScriptLocation: function(lineNumber, columnNumber)
     284    {
    280285        var closestScript = this._sortedScripts[0];
    281286        for (var i = 1; i < this._sortedScripts.length; ++i) {
     
    289294}
    290295
    291 WebInspector.FormattedSourceMapping = function(sortedScripts, originalText, formattedText, mapping)
     296WebInspector.FormattedSourceMapping = function(scripts, originalText, formattedText, mapping)
    292297{
    293     WebInspector.SourceMapping.call(this, sortedScripts);
     298    WebInspector.SourceMapping.call(this, scripts);
    294299    this._originalLineEndings = originalText.lineEndings();
    295300    this._formattedLineEndings = formattedText.lineEndings();
     
    301306    {
    302307        var originalPosition = WebInspector.ScriptFormatter.locationToPosition(this._originalLineEndings, location);
    303         var formattedPosition = this._convertPosition(this._mapping.original, this._mapping.formatted, originalPosition);
     308        var index = this._mapping.original.upperBound(originalPosition - 1);
     309        var formattedPosition = this._mapping.formatted[index];
    304310        return WebInspector.ScriptFormatter.positionToLocation(this._formattedLineEndings, formattedPosition).lineNumber;
    305311    },
     
    308314    {
    309315        var formattedPosition = WebInspector.ScriptFormatter.lineToPosition(this._formattedLineEndings, lineNumber);
    310         var originalPosition = this._convertPosition(this._mapping.formatted, this._mapping.original, formattedPosition);
     316        var index = this._mapping.formatted.upperBound(formattedPosition - 1);
     317        var originalPosition = this._mapping.original[index];
    311318        var originalLocation = WebInspector.ScriptFormatter.positionToLocation(this._originalLineEndings, originalPosition);
    312         return WebInspector.SourceMapping.prototype.sourceLineToScriptLocation.call(this, originalLocation.lineNumber);
    313     },
    314 
    315     _convertPosition: function(positions1, positions2, position)
    316     {
    317         var index = positions1.upperBound(position);
    318         var range1 = positions1[index] - positions1[index - 1];
    319         var range2 = positions2[index] - positions2[index - 1];
    320         var position2 = positions2[index - 1];
    321         if (range1)
    322             position2 += Math.round((position - positions1[index - 1]) * range2 / range1);
    323         return position2;
     319        return WebInspector.SourceMapping.prototype._sourceLocationToScriptLocation.call(this, originalLocation.lineNumber, originalLocation.columnNumber);
    324320    }
    325321}
  • trunk/Source/WebCore/inspector/front-end/SourceFrame.js

    r83725 r83748  
    4040
    4141    this._textViewer = new WebInspector.TextViewer(this._textModel, WebInspector.platform, this._url, this);
     42    this._textViewer.element.addStyleClass("script-view");
    4243    this._visible = false;
    4344
     
    306307
    307308        var element = this._textViewer.element;
    308         element.addStyleClass("script-view");
    309309        if (this._delegate.debuggingSupported()) {
    310310            element.addEventListener("contextmenu", this._contextMenu.bind(this), true);
Note: See TracChangeset for help on using the changeset viewer.