Bug 469742

Summary: System.NullReferenceException when generics used
Product: [Mono] Mono: Runtime Reporter: Marek Safar <msafar>
Component: JITAssignee: Mark Probst <mprobst>
Status: RESOLVED FIXED QA Contact: Mono Bugs <mono-bugs>
Severity: Normal    
Priority: P5 - None CC: forgotten_vxPDddArjq
Version: unspecified   
Target Milestone: ---   
Hardware: Other   
OS: Other   
Whiteboard:
Found By: --- Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---
Bug Depends on:    
Bug Blocks: 324319    
Attachments: Test
Proposed patch.
Updated proposed patch.

Description Marek Safar 2009-01-27 12:47:00 UTC
mono gtest-116.exe

Calling Test.DoNothing<Program>() on an object reference...  
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at Slow.Program.Main (System.String[] args) [0x00000] 


.NET

gtest-116.exe
Calling Test.DoNothing<Program>() on an object reference...  Took 0 ms.
Calling Test.DoNothing<Program>() on an interface reference...  Took 0 ms.
Comment 1 Marek Safar 2009-01-27 12:47:37 UTC
Created attachment 267890 [details]
Test
Comment 2 Mark Probst 2009-01-27 15:16:05 UTC
Created attachment 267946 [details]
Proposed patch.

The problem is the sequence

        IL_0000:  ldnull 
        IL_0001:  unbox.any !!0
        IL_0006:  stloc.0 

The code which generates the code for unbox.any just emits a check and then leaves the variable on the stack that ldnull generated.  emit_stloc_ir() doesn't take into account that that variable might be in use somewhere else and just destroys it, so the generated code doesn't check NULL but uninitialized data.
Comment 3 Rodrigo Kumpera 2009-01-27 16:41:50 UTC
Your patch removes a small opt which is trivially avoidable.

Instead of removing, it should check if sp[0] is the last ins of the current basic block.

And besides that unbox.any should not emit a cast class if sp [0] is a ldnull.
Comment 4 Mark Probst 2009-01-27 17:02:21 UTC
Created attachment 268005 [details]
Updated proposed patch.

Updated.  Doesn't remove the optimization now.  Based on all the code I've run the optimization never makes a difference, though (admittedly, I only compared the generated native code size, not the generated code itself).

The generated castclass is superfluous, but that's a different issue.
Comment 5 Forgotten User vxPDddArjq 2009-01-27 17:24:50 UTC
This looks ok.
Comment 6 Mark Probst 2009-01-27 21:52:54 UTC
Fixed in SVN and backported to 2.2 and 2.4.