views:

75

answers:

2

Hi.

I'd like to have access to the bytecode that is currently running or about to run in order to detect certain instructions and take specific actions (depending the instructions). In short, I'd like to monitor the bytecode in order to add safety control (see EDIT #1 for explanation).

Is this possible? I know there are some AOP frameworks that notify you of specific events, like an access to a field or the invocation of a method, but I'd like to skip that extra layer and just look at all the bytecode myself, throughout the entire execution of the application.

I've already looked at the following questions (...among many many others ;) ):
    Preprocessing C# - Detecting Methods
    What CLR/.NET bytecode tools exist?
as well as several AOP frameworks (although not in great detail, since they don't seem to do quite what I need) and I'm familiar with Mono.Cecil.

I appreciate alternative suggestions, but I don't want to introduce the overhead of an AOP framework when what I actually need is access to the bytecode, without all the stuff they add on top to make it more user-friendly (... admittedly very useful stuff when you don't want to go low-level).

Thanks :)


EDIT #1: more details on what I want to do.

Basically, I have a C# application and I need to monitor the instructions it wants to run in order to detect read or write operations to fields (operations Ldfld and Stfld) and insert some instructions before the read/write takes place: I may need to acquire locks, or if that fails abort the operation. Also, I may need to update a read log (in case of a read) or write log (in case of a write).

In fact, what I'd really like to do is to replace the read/write instruction with my own custom code, but it that fails I think I could manage just inserting some instructions before and after.


EDIT #2: PostSharp

Dave suggests I use PostSharp

The problem is at compile time I still don't know which classes I'm going to need to weave, so I'd like to delay this until they are loaded. As I understand, this isn't possible with PostSharp? Please correct me if I'm wrong.

I know PostSharp does load-time static weaving, but apparently "there is currently no "off-the-shelf" way to perform it" (notice however that this post is from PostSharp 1.5; maybe this has changed). I guess it would still be easier than just doing everything myself, but I can't find any info on how PostSharp would help me in this case. I guess I'll ask in the PostSharp forums.

+1  A: 

What kind of "safety control"? Have you looked into the Code Access Security provided with the .NET framework?

http://msdn.microsoft.com/en-us/library/930b76w0%28VS.80%29.aspx

http://www.codeproject.com/KB/security/UB_CAS_NET.aspx

Dave Swersky
I've just been looking at it and I don't think it suits my needs. I want to detect specific instructions and replace them, or insert my own code before them; from what I can gather you can't do that with Code Access Security? Correct me if I'm wrong.I've updated my answer with a more detailed explanation of what I need to do (EDIT #1). Thanks for your answer :)
Alix
I think you should reconsider AOP- it sounds like a good option based on your requirements. PostSharp actually does what you're looking for- it performs post-build operations, injecting the advice directly into the IL.
Dave Swersky
The problem is at compile time I still don't know which classes I'm going to need to weave, so I'd like to delay this until they are loaded. As I understand, this isn't possible with PostSharp? Please correct me if I'm wrong.
Alix
See EDIT #2. *[ignore this text here, I just need to reach 15 chars length]*
Alix
A: 

The answer is no. You can use the (extremely low-level and unmanaged-code-only) Profiling API to receive notifications of certain events, but not of access to fields, which is what I wanted. Plus, it's not supposed to be used in production:

(from the previous link)

Profiling in production environments with high-availability requirements. The profiling API was created to support development-time diagnostics. It has not undergone the rigorous testing required to support production environments.

Alix