Git Product home page Git Product logo

inoculator's Introduction

Innoculator

An IL code Injector using Ilasm and Ildasm

Plan

* Needs Excessive Testing
* Automatically Add MSbuild PostBuild event handler

Usage

  • Reference Inoculator.Injecter
  • Add to Msbuild :
    <Target Name="InjectionStep" BeforeTargets="AfterBuild">
        <Exec Command="$(MSbuildProjectDirectory)\$(BaseOutputPath)$(Configuration)\$(TargetFramework)\Inoculator.Injector.exe   $(MSbuildProjectDirectory)\$(BaseOutputPath)$(Configuration)\$(TargetFramework)\$(AssemblyName).dll" />
    </Target>
    
  • Inherit InterceptorAttribute and override Function lifecycle nodes :
public class ElapsedTimeAttribute : InterceptorAttribute
{
    private Stopwatch watch = new();
    public override void OnEntry(MethodData method)
        => watch.Start();

    public override void OnExit(MethodData method)
        => Console.WriteLine($"Method {method.Name(false)} took {watch.Elapsed}");
}
public class RetryAttribute<TMarker> : RewriterAttribute
{
    public RetryAttribute(int retries) => Retries = retries;
    Engine<TMarker> engine = new();
    int Retries;
    public override MethodData OnCall(MethodData method)
    {
        bool functionIsDone = false;
        while(!method.Stop && Retries >= 0) {
            try {
                method = engine.Invoke(method);
                break;
            } catch {
                if(--Retries == 0) throw; 
                else {
                    Console.WriteLine($"Retrying {method.Name(false)} : {Retries} left")
                    engine.Restart();
                }
            }
        }
        return method;
    }
}
  • Flag function to be injected with code :
async static Task Main(string[] args)  {
    _ = SumIntervalIsEven(7, 23, out _);
    GuessNumberSeven(0, 10);
}

[ElapsedTime, LogEntrency, CallCount]
public static bool SumIntervalIsEven(int start, int end, out int r) {
    int result = 0;
    for (int j = start; j < end; j++) {
        result += j;
    }
    r = result;
    return r % 2 == 0;
}

[RetryAttribute<typeof(Main)>(5)]
public static void GuessNumberSeven(int start, int end) {
    var random = new Random();
    if(random.Next(start, end) != 7) {
        throw new Exception("Wrong guess");
    }
    Console.WriteLine("Congrats Done");
}
  • Output :
Before: {
  "MethodName": "SumIntervalIsEven",
  "TypeSignature": "(int32, int32, int32 & -> bool)",
  "TypeParameters": [],
  "Parameters": [
    7,
    23,
    0
  ]
}

Success: True

Method SumIntervalIsEven took 93ms

After: {
  "MethodName": "SumIntervalIsEven",
  "TypeSignature": "(int32, int32, int32 & -> bool)",
  "TypeParameters": [],
  "Parameters": [
    7,
    23,
    232
  ],
  "ReturnValue": true
}

Retrying GuessNumberSeven : 4 left
Retrying GuessNumberSeven : 3 left
Retrying GuessNumberSeven : 2 left
Congrats Done

inoculator's People

Contributors

demuirgos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

inoculator's Issues

Refactor Code

  • Separate Handlers [x]
  • Use Code Manipulation Api
  • Avoid strings
  • Use Compiler to help
  • Investigate Compiler Optimization (in release mode)

Async Methods Break the rebuilding process

Investigate this code :

static void Main(string[] args) {
    Test();
}

[ElapsedTime, LogEntrency]
public static void Test() {
    int i = 0;
    for (int j = 0; j < 100; j++) {
        i++;
    }
    Console.WriteLine(i);
}

public static async Task TestAsync() {
    int i = 0;
    for (int j = 0; j < 100; j++) {
        i++;
    }
    Console.WriteLine(i);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.