Saturday, May 29, 2010

Perfect crime

So, there's a tiny logic bug in someone's code that you need to fix but the code has changed quite significantly and you don't want to have to go back through subversion logs?

  1. Download the existing dll from the production server
  2. Disassmble the dll using ildasm (ildasm /out:code.msil code.dll)
  3. Use notepad to insert the fix you need in MSIL. Ildasm conveniently pads line numbers in the disassembled code so if you're lucky you can insert code without having to change any existing line number references.
  4. Reassemble the code and deploy (ilasm /DLL code.msil)
  5. Try not to act surprised that all those years of writing compiler and reflection.emit code came in handy yet again!
  6. Hope no-one notices.

// Original C#

ViewData["CODE"] = string.IsNullOrEmpty(id)
? GetCookieValue("CODE") : id.ToUpper();

// Original IL

IL_001b: ldarg.0
IL_001c: ldstr "InviteCode"
IL_0021: call instance string GetCookieValue(string)
IL_0022: dup
IL_0023: starg 1
IL_0026: callvirt instance void set_Item(string, object)

// Fixed C#

id = string.IsNullOrEmpty(id)
? GetCookieValue("CODE") : id.ToUpper();

ViewData["CODE"] = id;

// Fixed C# (more compact)

ViewData["CODE"] = string.IsNullOrEmpty(id)
? id = GetCookieValue("CODE") : id.ToUpper();

// Fixed IL

IL_001b: ldarg.0
IL_001c: ldstr "InviteCode"
IL_0021: call instance string GetCookieValue(string)
IL_0022: dup
IL_0023: starg 1
IL_0026: callvirt instance void set_Item(string, object)

Friday, April 16, 2010

mflow launches publically with new features and new website



Click on the image to get an account with £1 free credit.

If you already have an account, apply the promo code TN736X in your account settings.






Tuesday, April 13, 2010

About Thong Nguyen

Name
Thong Nguyen
Born
December 24th 1979
Cam Ranh, Khánh Hòa, Vietnam
Home Town
Christchurch, New Zealand
Current Location
London, United Kingdom
Current Job
CTO at mflow




Thursday, March 25, 2010

Visual Studio Find In Files Stops working

This happens randomly and is really annoying. It usually requires Visual Studio to fix. However, there is a better solution which I found on this this blog. You just need to press Ctrl+Scrollock. I guess this happens to me because I regularly press Ctrl+Break and must accidentally hit the ScrollLock key.

Saturday, March 6, 2010

Simple C++ SmartPtr

namespace Neon
{

template
<class T>

class
SmartPtr
{

private
:

T *ptr;

mutable volatile
long *count;

public
:

const
long refcount()
{


return
*this->count;
}


void
addref() const

{

if
(count == 0)
{

return
;
}


XInterlockedIncrement(count);
}


void
deref()
{

if
(count == 0)
{


return
;
}


if
(XInterlockedDecrement(count) == 0)
{


delete
count;
delete
ptr;
}
}


void
init(T *ptr, volatile long *count)
{


this
->ptr = ptr;
this
->count = count;
}



SmartPtr(T *ptr_p, volatile long *count)
{


init(ptr_p, count);
this
->addref();
}


SmartPtr()
{


long
*count_p;

count_p = new long;

*
count_p = 1;

init((T *)0, count_p);
}


SmartPtr(T *ptr_p)
{


long
*count_p;

count_p = new long;
*
count_p = 1;

init(ptr_p, count_p);
}



SmartPtr(const SmartPtr<T>& ptr)
{


this
->init(ptr.ptr, ptr.count);
this
->addref();
}


template
<typename U>
SmartPtr<U> reinterpret()
{

SmartPtr<U> retval;

retval.init((U *)this->ptr, this->count);

retval.addref();

return
retval;
}

~
SmartPtr()
{

deref();
}


inline operator
T *()
{

return
ptr;
}


inline
T *operator&()
{


return
ptr;
}


inline
T *operator->()
{


return
ptr;
}


inline
bool operator==(const SmartPtr<T> &ptr)
{


return
ptr.ptr == this->ptr;
}


inline
bool operator==(const T *ptr_p)
{


return
ptr_p == ptr;
}


inline
bool operator!=(const SmartPtr<T> &ptr)
{


return
ptr.ptr != this->ptr;
}


inline
bool operator!=(const T *ptr_p)
{


return
ptr_p != ptr;
}


inline
void operator=(const T *ptr_p)
{


long
*count_p;

deref();

count_p = new long;
*
count_p = 1;


init(ptr_p, count_p);
}


inline
void operator=(const SmartPtr<T> &ptr)
{


ptr.addref();
deref();
init(ptr.ptr, ptr.count);
}
};
}


Tuesday, December 8, 2009

What I've been working on with a fantastic team for 18 months!


















Thursday, April 16, 2009

MemCpy in C#

Here's an example of implementing a fast equivalent of MemCpy in C# using DynamicMethods.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;

namespace Test
{
public unsafe class Program
{
public delegate void MemCpyFunction(void *des, void *src, uint bytes);

private static readonly MemCpyFunction MemCpy;

static Program()
{
var dynamicMethod = new DynamicMethod
(
"MemCpy",
typeof(void),
new [] { typeof(void *), typeof(void *), typeof(uint) },
typeof(Program)
);

var ilGenerator = dynamicMethod.GetILGenerator();

ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Ldarg_2);

ilGenerator.Emit(OpCodes.Cpblk);
ilGenerator.Emit(OpCodes.Ret);

MemCpy = (MemCpyFunction)dynamicMethod
.CreateDelegate(typeof(MemCpyFunction));
}

static void Main(string[] args)
{
var point1 = new Point
{
X = 10,
Y = 20
};

var point2 = new Point();

MemCpy(&point2, &point1, (uint)sizeof(Point));
}
}
}