jbevain / cecil

Cecil is a library to inspect, modify and create .NET programs and libraries.
MIT License
2.77k stars 630 forks source link

Subsequent write operations create different outputs #859

Open tom-englert opened 2 years ago

tom-englert commented 2 years ago

Calling ModuleDefinition.Write multiple times generates different outputs. The first output is binary different from all subsequent outputs.

This is not a blocking issue, since both outputs seem to work fine, but might be a hint to some inconsistency in the write operation.

Zastai commented 2 years ago

I suppose I'm surprised it becomes stable after the first. I could understand each Write generating a new MVID (in fact, arguably it should, just like recompiling does even though everything else remains the same).

ltrzesniewski commented 2 years ago

I could understand each Write generating a new MVID (in fact, arguably it should, just like recompiling does even though everything else remains the same).

The MVID can be made deterministic. See #810.

vitek-karas commented 2 years ago

As per @ltrzesniewski comment - you need to ask for deterministic behavior. But if you still get different outputs after that I would be very interested (as it would break lot of things in our usage potentially).

jbevain commented 2 years ago

Interesting. Thanks for the test @tom-englert. Did you have a chance to look at what's happening?

tom-englert commented 2 years ago

I once tried to dig through the code, but gave up.

I remember for one experiment that there were some blocks shifted in the file, and an empty block with only 00 had appeared after the second save

However in the test that I've added in #860 the difference is more subtle: image

Looks like it's not the MVID that changes.

Zastai commented 2 years ago

Given that second file has a high(ish) value at the start makes me wonder if there's a static or instance field somewhere that does not get reset.