Understanding a JS binary file format

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Understanding a JS binary file format

Gábor Molnár-2
Hello all,

I'd like to understand a binary file format used to store JavaScript code,
and would appriciate any help. The application I'm looking at is using some
older version of SpiderMonkey as embedded JS engine, and stores it's
initialization JS code in a binary file. The file format itself looks
somewhat similar to the binary JS files found in StartupCache files of
Thunderbird. It's a serialization format for the SpiderMonkey JavaScript
AST, I guess.

Is this file format documented somewhere? If not, where should I look in
the mozilla source code to find out more about the
serialization/deserialization process? Do you think that SpiderMonkey could
somehow turn this AST-like representation back into JS code?

Thanks,
  Gábor
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Understanding a JS binary file format

Till Schneidereit-2
Hi Gábor,

I don't know which exact version you're using, so I can't direct you to
specific files, but I'd assume that your application is using XDR. In
current mozilla-central, the XDR-serialization is implemented in
js:XDRScript[1], so looking at that function in your version's source
should give you a good starting point.


cheers,
till


[1]: http://mxr.mozilla.org/mozilla-central/source/js/src/jsscript.cpp#443


On Sun, Feb 9, 2014 at 5:50 AM, Gábor Molnár <[hidden email]> wrote:

> Hello all,
>
> I'd like to understand a binary file format used to store JavaScript code,
> and would appriciate any help. The application I'm looking at is using some
> older version of SpiderMonkey as embedded JS engine, and stores it's
> initialization JS code in a binary file. The file format itself looks
> somewhat similar to the binary JS files found in StartupCache files of
> Thunderbird. It's a serialization format for the SpiderMonkey JavaScript
> AST, I guess.
>
> Is this file format documented somewhere? If not, where should I look in
> the mozilla source code to find out more about the
> serialization/deserialization process? Do you think that SpiderMonkey could
> somehow turn this AST-like representation back into JS code?
>
> Thanks,
>   Gábor
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Understanding a JS binary file format

Gábor Molnár
Thanks for the suggestion! Based on the source code and the magic number at
the beginning of the file (0xdead0007 in little endian encoding), I've
identified it as a SpiderMonkey 1.8 XDR file.

If my understanding is correct, an XDR file is not an AST storage format
but rather stores the state of a script at a given time. Assuming this, do
you think it is possible to reconstruct the whole JS file, or at least
extract the JS code of the contained functions?

 Gábor


2014-02-09 23:51 GMT+01:00 Till Schneidereit <[hidden email]>:

> Hi Gábor,
>
> I don't know which exact version you're using, so I can't direct you to
> specific files, but I'd assume that your application is using XDR. In
> current mozilla-central, the XDR-serialization is implemented in
> js:XDRScript[1], so looking at that function in your version's source
> should give you a good starting point.
>
>
> cheers,
> till
>
>
> [1]: http://mxr.mozilla.org/mozilla-central/source/js/src/jsscript.cpp#443
>
>
> On Sun, Feb 9, 2014 at 5:50 AM, Gábor Molnár <[hidden email]> wrote:
>
>> Hello all,
>>
>> I'd like to understand a binary file format used to store JavaScript code,
>> and would appriciate any help. The application I'm looking at is using
>> some
>> older version of SpiderMonkey as embedded JS engine, and stores it's
>> initialization JS code in a binary file. The file format itself looks
>> somewhat similar to the binary JS files found in StartupCache files of
>> Thunderbird. It's a serialization format for the SpiderMonkey JavaScript
>> AST, I guess.
>>
>> Is this file format documented somewhere? If not, where should I look in
>> the mozilla source code to find out more about the
>> serialization/deserialization process? Do you think that SpiderMonkey
>> could
>> somehow turn this AST-like representation back into JS code?
>>
>> Thanks,
>>   Gábor
>> _______________________________________________
>> dev-tech-js-engine mailing list
>> [hidden email]
>> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>>
>
>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Understanding a JS binary file format

Nicolas B. Pierron
In reply to this post by Gábor Molnár-2
Hi Gábor,

On 02/09/2014 05:50 AM, Gábor Molnár wrote:
> I'd like to understand a binary file format used to store JavaScript code,
> and would appriciate any help. The application I'm looking at is using some
> older version of SpiderMonkey as embedded JS engine, and stores it's
> initialization JS code in a binary file.

I am kind of surprized, by default the JS engine is not doing any
serialization on its own, unless you called JS_EncodeScript /
JS_DecodeScript your-self when you embed SpiderMonkey.

> The file format itself looks
> somewhat similar to the binary JS files found in StartupCache files of
> Thunderbird. It's a serialization format for the SpiderMonkey JavaScript
> AST, I guess.

I do not know about the StartupCache files of Thunderbird, but for XUL
scripts (JavaScript embedded in XUL), we do use JS_EncodeScript and
JS_EncodeFunction, to save a pre-parsed version.

We do not save the AST, but the bytecode including the meta-data of the
JSScript.

> Is this file format documented somewhere? If not, where should I look in
> the mozilla source code to find out more about the
> serialization/deserialization process? Do you think that SpiderMonkey could
> somehow turn this AST-like representation back into JS code?

There is no documentation of this process as it is not really exposed. All
these serializations are handled by XDR* functions.  These functions might
be a bit weird, as they contain both the code to serialize and deserialize,
based on the template parameter.

We used to have a decompiler, which was reconstructing the source based on
the source notes and the content of the bytecode.  But it got removed as
this was hard to maintain.  The decompiler was removed about 1.5y ago.

--
Nicolas B. Pierron

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Understanding a JS binary file format

Gábor Molnár
In reply to this post by Gábor Molnár
Okay, I think I've found what I need: using the js_DecompileScript()
function should do the trick. Thanks for the help again!

 Gabor


2014-02-10 11:06 GMT+01:00 Gábor Molnár <[hidden email]>:

> Thanks for the suggestion! Based on the source code and the magic number
> at the beginning of the file (0xdead0007 in little endian encoding), I've
> identified it as a SpiderMonkey 1.8 XDR file.
>
> If my understanding is correct, an XDR file is not an AST storage format
> but rather stores the state of a script at a given time. Assuming this, do
> you think it is possible to reconstruct the whole JS file, or at least
> extract the JS code of the contained functions?
>
>  Gábor
>
>
> 2014-02-09 23:51 GMT+01:00 Till Schneidereit <[hidden email]>:
>
> Hi Gábor,
>>
>> I don't know which exact version you're using, so I can't direct you to
>> specific files, but I'd assume that your application is using XDR. In
>> current mozilla-central, the XDR-serialization is implemented in
>> js:XDRScript[1], so looking at that function in your version's source
>> should give you a good starting point.
>>
>>
>> cheers,
>> till
>>
>>
>> [1]:
>> http://mxr.mozilla.org/mozilla-central/source/js/src/jsscript.cpp#443
>>
>>
>> On Sun, Feb 9, 2014 at 5:50 AM, Gábor Molnár <[hidden email]> wrote:
>>
>>> Hello all,
>>>
>>> I'd like to understand a binary file format used to store JavaScript
>>> code,
>>> and would appriciate any help. The application I'm looking at is using
>>> some
>>> older version of SpiderMonkey as embedded JS engine, and stores it's
>>> initialization JS code in a binary file. The file format itself looks
>>> somewhat similar to the binary JS files found in StartupCache files of
>>> Thunderbird. It's a serialization format for the SpiderMonkey JavaScript
>>> AST, I guess.
>>>
>>> Is this file format documented somewhere? If not, where should I look in
>>> the mozilla source code to find out more about the
>>> serialization/deserialization process? Do you think that SpiderMonkey
>>> could
>>> somehow turn this AST-like representation back into JS code?
>>>
>>> Thanks,
>>>   Gábor
>>> _______________________________________________
>>> dev-tech-js-engine mailing list
>>> [hidden email]
>>> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>>>
>>
>>
>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Understanding a JS binary file format

Till Schneidereit-2
On Mon, Feb 10, 2014 at 11:23 PM, Gábor Molnár <[hidden email]>wrote:

> Okay, I think I've found what I need: using the js_DecompileScript()
> function should do the trick. Thanks for the help again!
>

Great. Note that this won't work anymore if you ever update to current
versions of SpiderMonkey: most of the decompiler has been removed. I'm
pretty sure that that'd be the least of your worries during the updating
process, though. ;)
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine