Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When PDB reading is enabled, MethodDef.Body cannot be read in other threads #24

Closed
yck1509 opened this issue Jan 7, 2015 · 15 comments
Closed

Comments

@yck1509
Copy link
Contributor

yck1509 commented Jan 7, 2015

dnlib uses COM interfaces to read PDB. When it is used in other threads, it will produce an error.

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 8, 2015

Is your application multi-threaded and you try to use the PDB feature in multiple threads at the same time or does it only occur if you use it in one thread and that thread is not the main thread? Do you have simple test code to trigger it (otherwise I'll write some code myself)?

@yck1509
Copy link
Contributor Author

yck1509 commented Jan 8, 2015

[STAThread]
static void Main() {
    var module = ModuleDefMD.Load(
        "ConfuserEx.exe",
        new ModuleCreationOptions() { TryToLoadPdbFromDisk = true }
    );
    var task = Task.Factory.StartNew(() => module.ResolveMethod(1).Body);
    task.Wait();
    Debug.Assert(task.Result != null);
}

Note that STAThread is needed, or it will just fail to read the state without throwing exception.

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 8, 2015

I tested this code. Without STAThread or with MTAThread, the code will work. Are you sure this code fails when STAThread isn't present? It worked for me.

//DISABLED: [STAThread]
static void Main() {

and

[MTAThread]
static void Main() {

When you use STAThread it seems like all COM calls must be done in the main thread. See this http://stackoverflow.com/questions/2175419/cannot-call-com-object-created-from-stathread-from-oher-sta-threads

@yck1509
Copy link
Contributor Author

yck1509 commented Jan 8, 2015

I suppose I should change that assert to task.Result.HasScope.

When I tried without STAThread or with MTAThread, reader.GetMethod returns E_FAIL so no exception is thrown, but the PDB symbols are not read for the method body.

When I tried with STAThread, reader.GetMethod throws the following exception:

Unable to cast COM object of type 'System.__ComObject' to interface type 'dnlib.DotNet.Pdb.Dss.ISymUnmanagedReader'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B4CE6286-2A6B-3712-A3B7-1EE1DAD467B5}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 8, 2015

Symbols are loaded in my test.

Could you try my code? It's almost identical to yours. Create a new project:

Update the path in the code to point to the exe:

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using dnlib.DotNet;

namespace dnpdbtest {
    class Program {
        //[STAThread]
        //[MTAThread]
        static void Main() {
            var module = ModuleDefMD.Load(
                @"C:\work\dnpdbtest\dnpdbtest\bin\Debug\dnpdbtest.exe",
                new ModuleCreationOptions() { TryToLoadPdbFromDisk = true }
            );
            var task = Task.Factory.StartNew(() => module.ResolveMethod(1).Body);
            task.Wait();
            Debug.Assert(task.Result.HasScope);
            var m = task.Result;
        }
    }
}

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 8, 2015

BTW, I used VS2010 to compile the code and Windows 7 SP1 with latest updates. The project was compiled as a .NET 4.0 app.

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 8, 2015

One reason your code might fail without STAThread is that the method with RID=1 does not have any debug info in the PDB file (eg. it's compiler generated).

@yck1509
Copy link
Contributor Author

yck1509 commented Jan 8, 2015

Your code works here. I suppose that is the reason. However, it still crashes when I put STAThread on the method. WinForms requires the main thread to be STAThread, so it would crash when I created the ModuleDefMD and use it in other threads.

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 8, 2015

I'll see if there's a solution. EDIT: see below

@0xd4d 0xd4d closed this as completed in c04b7c8 Jan 9, 2015
@0xd4d
Copy link
Collaborator

0xd4d commented Jan 9, 2015

I've added a new option to ModuleCreatorOptions. Set PreLoadAllPdbData to true and all PDB data get loaded in the ModuleDefMD constructor. Alternatively, you could call ModuleDefMD.PreLoadAllPdbData() yourself in the same thread that creates it.

@yck1509
Copy link
Contributor Author

yck1509 commented Jan 9, 2015

Thanks for the fix, but as a side effect, the method bodies are loaded with PDB, which mean whatever the CIL code references would be loaded as well. I found this project and it seems it has a suitable license along with format documentation. If you're OK with it, I could write a managed reader using the documentation there.

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 9, 2015

Which references are loaded when a CIL body is loaded? Nothing is resolved.

Sure if you want to write a managed reader, go for it. I assume you'll add it to dnlib, and if so, the COM code is in Pdb/Dss, so you could put your code in Pdb/Managed or whatever name you want.

If you want the code just to work with existing dnlib code, you'll need to implement various ifaces, eg. ISymbolMethod etc. dnlib only uses a few of the available methods/properties so most methods could throw a NotImplementedException.

Let me know if you need help with some of the PDB internals. I've written a PDB library myself so I know a few things about the file format. But this is a general PDB library so it's not suitable for dnlib. .NET PDB files only use a small subset of the PDB file format.

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 9, 2015

Another solution (similar to the code I already wrote) could be to just load all PDB data (and not the CIL body) and cache it in some dictionary. Whenever the CIL body is loaded, the cached data is used (if it's been cached).

@yck1509
Copy link
Contributor Author

yck1509 commented Jan 10, 2015

While I'm looking for information for internals of PDB, I found some parts of PdbReader in Rosyln. Would it be suitables to use some part of that, since it seems they re-licensed the code under Apache License?

@0xd4d
Copy link
Collaborator

0xd4d commented Jan 10, 2015

Apache 2.0 and MIT seem compatible according to some posts found by google.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant