Get decompiled code source for an external symbol from DLL for analysis #77454
-
Dear Roslyn team, I would like to know if there is a way to get code sources for external symbols defined in DLLs. I am writing an inter-procedural analyzer that needs to step into called APIs. This works fine for calls to the code that is declared in the same solution. However, for the external symbols Roslyn can't obtain sources. At the same time, Visual Studio's "Go To Definition" command can go to the decompiled sources, if the decompilation is enabled in VS settings. The decompiled code is recognized by Roslyn, I can get AST and semantic model from the opened document with the decompiled code. Therefore, I wonder if I can somehow get the decompiled sources in my analyzer at least for some cases. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 8 replies
-
We get the decompiled sources by calling into ILSpy which has a library for doing so. So you could (theoretically) call into that if you'd like. That said...decompilation isn't always accurate and potentially quite slow which would not be a wise idea to put in an analyzer. What type of analyzer are you trying to build? |
Beta Was this translation helpful? Give feedback.
-
@jasonmalinowski you misunderstood me a little.
That's exactly how it works. Partners develop their plugins in VS as a regular solutions with projects. My analyzer works while they write code. The thing is, their plugins reference a large set of external DLLs, the customization platform. This platform is shipped as a set of DLLs, without the source code. The analyzer works on the plugin source code in VS on local machines of partner developers. It may see a call of some external API from a DLL that belongs to the customization platform. The analysis goal is to prevent the usage of some forbidden APIs from special methods in the code. The calls to external APIs also should be checked for the presence of calls to forbidden APIs. So, the analyzer should do the inter-procedural analysis and step into such calls. If they contain such calls, then the calls to these external APIs should be reported as errors. And that's where the issue appears. There is no problem to make inter-procedural analysis and step into API calls for APIs declared in the same solution. We have source code for such APIs inside the solution. However, there is a problem with stepping into external APIs from DLLs because there is no source code there. |
Beta Was this translation helpful? Give feedback.
-
@jasonmalinowski thanks again for your suggestions. I do not own the customization platform. I omitted some details previously for simplicity. The platform provides customization mechanisms of the functionality of original application to simplify the development of plugins. It is somewhat similar to IL weaving techniques and Harmony (https://github.com/pardeike/Harmony). This means that it is close to impossible to manually annotate or collect problem APIs. The application is too big for that, it's written by many developers. With source generators it may be possible but again there are problems:
|
Beta Was this translation helpful? Give feedback.
-
First, short summary of my answer. Here is the long explanation why I think that way.
Roslyn displays errors directly in the code editor while you write the code. This is a huge feature for developers. Reading results from the command line app is far less convenient.
Roslyn diagnostics work incrementally on a modified solution and and the analysis via the command line app is not incremental. It will have to analyze the whole DLL, not a particular file. Therefore, it will take much more time. This static analysis is also used by our application developers, they don't have the issue with external DLLs. I'm afraid that switching to a console app for analysis with all related downsides will make them very frustrated. I already have such application running during our testing in CI environment, so I have some experience with how long the analysis takes.
In addition to the downsides from switching analysis to the console app, the complete rewrite of the analysis with Reflection or Cecil also has its own downsides:
|
Beta Was this translation helpful? Give feedback.
-
@jasonmalinowski thanks again for the information and the ideas. I would say, that I now have an interesting idea for experiments with IlSPy. |
Beta Was this translation helpful? Give feedback.
You might be able to do something where you have ILSpy decompile a binary and feed it back to a Roslyn analyzer; I'm not aware of somebody having done that before but maybe somebody has.