Can you hack this out? Hint #1

Here is another challenge, smaller, this time, which can lead you the right path.  And yes, it is a screwy one.

All you have to do is make the following assertion fail.

public class Program
{
private static void Main(string[] args)
{
CanThisHappen<MyFunnyType>();
}

public static void CanThisHappen<T>() where T : class, new()
{
var instnace = new T();
Debug.Assert(instnace != null,"How did we break the CLR?");
}
}

The rules are simple, you are not allowed to do anything outside the current process. That means that profiling API, post compilation assembly rewrite, etc are forbidden. Anything else is a fair game.

I will let you know that I can make the assertion fail in 4 lines of code :-)

Print | posted on Wednesday, November 25, 2009 12:00 PM

Feedback


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 12:33 PM hazzik

perhaps like this:

public class Program {
private static void Main() {
CanThisHappen();
}

public static void CanThisHappen() where T : class, new() {
var instnace = new T();
Debug.Assert(!instnace.Equals(null), "How did we break the CLR?");
}
}

internal class MyFunnyType {
public override bool Equals(object obj) {
return true;
}
}


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 12:39 PM Ayende Rahien

Hazzik,
That would work, yes, but that doesn't match the challenge that I set.


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 12:43 PM hazzik

Can we modify CanThisHappen method of program?


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 12:46 PM Ayende Rahien

Hazzik,
No :-)


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 12:46 PM Marc Gravell

class MyFunnyTypeAttribute : ProxyAttribute {
public override MarshalByRefObject CreateInstance(Type serverType) { return null; }
}
[MyFunnyType] class MyFunnyType : ContextBoundObject{}


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 12:53 PM Ayende Rahien

Marc,
:-)
Yeah.
Once the previous hack was solved, this is pretty easy.


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 1:00 PM Dennis

Do you mean to break
var instnace = new ;
object.ReferenceEquals(instnace, null) == false?


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 1:03 PM Ayende Rahien

Dennis,
Yes.


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 1:14 PM Dennis

Damn, thats a NASTY solution :S


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 1:58 PM liviu

I did not know that ProxyAttribute exists :)
It opens so many possibilities!!!


Gravatar

# re: Can you hack this out? 11/25/2009 2:06 PM Marc Gravell

Indeed - pure evil; and definitely worthy of an update to my similar corner-case on stackoverflow: http://stackoverflow.com/questions/194484#194671


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 3:37 PM Reshef Mann

Apparently this will do the trick:

public static bool operator !=(object left, MyFunnyType right)
{
return false;
}


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 3:41 PM Ayende Rahien

Reshef,
Try that, it won't work :-)


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 3:55 PM Reshef

Right. When creating the object with new T() somehow the type is unknown and it won't call the != operator. Generics oddity...


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 5:02 PM Marc Gravell

@Reshef - it isn't that the T is unknown; it is that the operator can't be resolved via static analysis (which it must) to a single implementation. Similar to how +-*/ don't work (unless you cheat, like in MiscUtil's Operator class). I suspect there is some scenarios (involving subclassing and a constraint like T : SomeType) where it /would/ pick up the operator, but that ("T : SomeType") isn't in the question.


Gravatar

# re: Can you hack this out? Hint #1 11/25/2009 5:48 PM Wil

Looks like this would work.

public class InterceptionProxy : RealProxy
{
public InterceptionProxy() : base(typeof(MyFunnyType)) { }

public override IMessage Invoke(IMessage msg)
{
// Does not get here
....
}

public override object GetTransparentProxy()
{
return null;
}
}


Gravatar

# re: Can you hack this out? Hint #1 11/28/2009 4:34 PM Chris Wright

I've used a programming language in which you can return a value from a constructor, even null. Insanity :)


Gravatar

# re: Can you hack this out? Hint #1 11/30/2009 5:17 PM Mark Rogers

Cool post, I hate to say but I think you misspelled 'instance' as 'instnace' in your code example.

Peace!


Gravatar

# re: Can you hack this out? Hint #1 11/30/2009 11:08 PM Avish

Ooh, nice solution. I was thinking about this:

using MyFunnyType = System.Nullable;

As far as I recall, Nullable structs are not applicable for the "struct" constraint (which is actually defined as "non-nullable value type"), so I guess they might be applicable for the "class" constraint. I'm probably wrong, but it'd be a nice solution.

Comments have been closed on this topic.