Monday, May 21, 2007
Boxing - value types
Question: Structures inherit ToString from System.Object. Why would someone override that method within a structure? (Choose as many correct answers as apply.)
A. To avoid boxing.
B. To return something other than the type name.
C. The compiler requires structures to override the ToString method.
D. To avoid run-time errors caused by invalid string conversions
Correct Answers: A and B
A. Correct: Value types are boxed when an abstract method inherited from System.Object is called. Overriding the method avoids boxing.
B. Correct: By default, the ToString method simply returns the type name, which is typically not useful for a consuming application.
C. Incorrect: The compiler does not require structures to override the ToString method.
D. Incorrect: ToString never causes a run-time error; it simply returns the type name unless overridden.
O'kay I shall provide feedback to author regarding correction.
I have written two simple programs to understand the behavior correctly:
Program 1
public struct MyStruct1
{
public int i;
public override string ToString()
{
return i.ToString();
}
}
public struct MyStruct2
{
public int i;
}
class Program
{
static void Main(string[] args)
{
MyStruct1 st1;
st1.i = 55;
Console.WriteLine(st1.ToString());
MyStruct2 st2;
st2.i = 99;
Console.WriteLine(st2.ToString());
}
}
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 58 (0x3a)
.maxstack 2
.locals init ([0] valuetype BoxUnBox.MyStruct1 st1,
[1] valuetype BoxUnBox.MyStruct2 st2)
IL_0000: nop
IL_0001: ldloca.s st1
IL_0003: ldc.i4.s 55
IL_0005: stfld int32 BoxUnBox.MyStruct1::i
IL_000a: ldloca.s st1
IL_000c: constrained. BoxUnBox.MyStruct1
IL_0012: callvirt instance string [mscorlib]System.Object::ToString()
IL_0017: call void [mscorlib]System.Console::WriteLine(string)
IL_001c: nop
IL_001d: ldloca.s st2
IL_001f: ldc.i4.s 99
IL_0021: stfld int32 BoxUnBox.MyStruct2::i
IL_0026: ldloca.s st2
IL_0028: constrained. BoxUnBox.MyStruct2
IL_002e: callvirt instance string [mscorlib]System.Object::ToString()
IL_0033: call void [mscorlib]System.Console::WriteLine(string)
IL_0038: nop
IL_0039: ret
} // end of method Program::Main
Program 2
... definition of both structs remains the same as above!
[-snip-]
static void Main(string[] args)
{
MyStruct1 st1;
st1.i = 55;
Console.WriteLine(st1);
MyStruct2 st2;
st2.i = 99;
Console.WriteLine(st2);
}
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 44 (0x2c)
.maxstack 2
.locals init ([0] valuetype BoxUnBox.MyStruct1 st1,
[1] valuetype BoxUnBox.MyStruct2 st2)
IL_0000: nop
IL_0001: ldloca.s st1
IL_0003: ldc.i4.s 55
IL_0005: stfld int32 BoxUnBox.MyStruct1::i
IL_000a: ldloc.0
IL_000b: box BoxUnBox.MyStruct1
IL_0010: call void [mscorlib]System.Console::WriteLine(object)
IL_0015: nop
IL_0016: ldloca.s st2
IL_0018: ldc.i4.s 99
IL_001a: stfld int32 BoxUnBox.MyStruct2::i
IL_001f: ldloc.1
IL_0020: box BoxUnBox.MyStruct2
IL_0025: call void [mscorlib]System.Console::WriteLine(object)
IL_002a: nop
IL_002b: ret
} // end of method Program::Main
The above experiment shows that - in both cases MyStruct1 and MyStruct2 are boxed irrespective of ToString() was overridden in MyStruct1.
One question: Why would there be boxing at all in first case (in Program 1 above):
static void Main(string[] args)
{
MyStruct1 st1;
st1.i = 55;
Console.WriteLine(st1.ToString()); <----- this will call overridden ToString() in MyStruct1...
....
public struct MyStruct1
{
public int i;
public override string ToString()
{
return i.ToString(); <----- this, anyway, will not be boxed, right? (because compiler knows i is an int)
Lets Jump Into The Special Pool
Many of you have heard about special pool. This is what it is and how it works.
What is Special Pool?
Special Pool is another memory pool type. Therefore, when you have Special Pool enabled, three types of memory pools exist: Paged, Non-paged and Special pool. Allocations will be made from Special Pool depending if it is enabled and configured.
How can I configure special pool?
When memory is allocated, drivers use what is called a tag to identify a memory block. Tags are used to record allocations to see how much memory is used. A tag is just a 4 byte text such as “ABCD”. Special pool can be enabled so whenever an allocation is made for a certain tag, the allocation comes from special pool. For this you need to know what tag you are looking for. Also Special Pool can be set up for a particular allocation size, such as 40.
What happens when a special pool allocation is made?
When a special pool allocation is made, one 4096 byte page is used for the allocation. The actual allocation goes at the end of a page or at the beginning depending on configuration options. Where the allocation goes depends on which option is chosen to catch underruns or overruns. If underruns are selected, the allocation goes at the top of the page. If overruns are selected, the allocation goes at the end of the page. Below is an example of overruns.
0: kd> dd 8ae24fc0-50 l 55
8ae24f70 5d5d5d5d 5d5d5d5d 5d5d5d5d 5d5d5d5d
8ae24f80 5d5d5d5d 5d5d5d5d 5d5d5d5d 5d5d5d5d
8ae24f90 5d5d5d5d 5d5d5d5d 5d5d5d5d 5d5d5d5d ß Fill pattern
8ae24fa0 5d5d5d5d 5d5d5d5d 5d5d5d5d 5d5d5d5d
8ae24fb0 5d5d5d5d 5d5d5d5d 5d5d5d5d 5d5d5d5d
8ae24fc0 00400709 00008003 0078006c e18853f8
8ae24fd0 0000003e 00000000 e1938f70 e1938f70
8ae24fe0 e1938f28 e1938eb0 e1938eb0 00000002
8ae24ff0 00000000 00000000 00000000 00000020
8ae25000 ???????? ???????? ???????? ???????? ß Next page set to an invalid address.
The idea for overruns is to catch a driver writing beyond its allocation. So the next page is set to be invalid. This will cause the system blue screen immediately when a driver attempts to write beyond its allocation. Otherwise the overwrite may not cause a problem until much later and there may be no way to find what did it. In the underrun case, the upper page is invalid.
What is the fill pattern?
The fill pattern is checked for validity after every release. If the fill pattern is written to, the system will blue screen on release.
Are there any other reasons to enable special pool besides pool corruption?
Special Pool has a feature where it saves the stack of the last thread to free the block. So if a block has been freed back to special pool, the !fpsearch command can be used to see the stack of the last free.
0: kd> !fpsearch be962c48Searching the free page list (38 entries) for VA be962c48
VA PFN Tag Size Pagable Thread Tickbe962c48 ec79 CM 3b8 Yes 81ed1020 ae2 CALL STACK AT TIME OF DEALLOCATION nt!ExFreePoolWithTag+0x22 nt!ExFreePool+0xb nt!CmpValidateAlternate+0x63 nt!CmpInitializeHiveList+0x2cd nt!CmpWorker+0x68 nt!NtInitializeRegistry+0x92 nt!_KiSystemService+0xc9 nt!NtClose+0x2d8 nt!_KiSystemService+0xc9
Beware of Red Herrings in the Pool
After enabling Special Pool, the system may blue screen on boot. This is because during boot up, the memory manager may round up some memory allocations thus giving a driver a few more bytes than it actually requested. Some of these drivers may occasionally overrun their requested allocation size, but since the memory manager allocated a few extra bytes, there’s no problem. There’s no problem until you enable Special Pool, that is… Once Special Pool is enabled, the actual allocation size will be strictly enforced and the system will bug check. This may not indicate the actual problem, however.
Conclusion
I hope this gives a better understanding of Special Pool.
Addressing Problems
Imagine you're working on a project and a large problem presents itself. The problem has far-reaching impact for you and your team. What's the best way to inform your manager of this situation? Although your first thought may be to barge into his or her office and "dump it", that may not be the best approach. An effective approach is to stop, think, and prepare for the discussion.
Use the following steps to organize your thoughts and prepare for the conversation with your manager:
- Describe the problem to your managerProvide a general overview of the problem, and show the specific impact that the problem is having on your work and on the organization's goals.
Identify your solution or approachRecommend a specific solution or approach, along with alternatives, to provide your manager with options.
- Explain the implications of the solution or approachConsider the impact that your solution or approach will have on yourself and others, including your manager and the organization as a whole.
- Discuss the benefits of your solution or approachFocus your discussion on the benefits to be gained from implementing your solution or approach.
- Accept responsibility for the outcomeLet your manager know that you are willing to take responsibility for the outcome of your solution or approach. This is an important part of your discussion and demonstrates your commitment to ensuring success.
Can u prove 2=3 ?
I like the KISS method using basic math fundamentals and a little fools’ logic. Suppose we have positive values x and y where y =
so xy=1
and x =
and x != y
and y>x,
so <
and 0 < x < 1 < y
we would all agree on those fundamentals, right? If x were and y were , then xy = 1
simple, right?
So if xy = 1
and 2 + 1 = 3,
then 2 + xy = 3
Let’s shift gears for a bit.
1/10 = .1
1/100 = .01
1/1000 = .001
So for j>=1 where we have j-1 zeroes at the end of decimal point and before the final 1
= .0001
And so on.
So if we had k = ,
1-k = .9 to j places
and .9 + k = 1
So = .1, so .9 with one place,
= .01 so .99 with two places and so on…
So for j = 1 to who knows what, we have
1-() = .9 to j places
So if we drive for infinite 9’s we have .
So where k = x = (1 -0.) and since 2 + xy = 3
On principal that 3 + 3 + 3 = 9,
. + . + . = .
but since = .
and + + = 1,
1 = .
and since x = 1 - .,
or x = 1 -1 = 0
x=0
and therefore xy = 0
and 2 + xy = 3
so 2 + 0 = 3
and 2 = 3
…of course I left out the part where now
y is probably somewhere close to ∞ (1 divided by an astronomically small number)
if it was, 0 * ∞ = 1 and/or 0
so and/or = ∞
and to reiterate 0=1 (and to think I wanted to add the astronomically small number to 2, y individual times to get 3)
And my favorite
∞ = 1 (I will now call Guinness and try to break the record for the most memorized digits of ∞)
Laugh all you want – this is my math and you reserve the right to follow my lead or be wrong. ;)
Thursday, May 17, 2007
Disable systray balloon
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\EnableBalloonTips to ZERO.
Set up JIT debugger
1. install Debugging Tools for Window x64.
2. run command prompt with administrator authority
3. change directory to the Debugging Tools for Windows installed folder and run the command "CDB.EXE -iae". As a result, Auto and Debugger settings are added shown below.
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug]
"Auto"="1"
"Debugger"="\"C:\\Program Files\\Debugging Tools for Windows\\cdb.exe\" -p %ld -e %ld -g"
4. Using regedit, add below settings.
WOW6432Node\Microsoft\Windows NT\Current Version\AeDebug
"Auto"="1"
"Debugger"="\"C:\\Program Files\\Debugging Tools for Windows\\cdb.exe\" -p %ld -e %ld -g"