Debug Labs


Memory Leak in Component

This lab will demonstrates memory leak in an ISAPI Filter. It uses the tool "LeakDiag" to find the culprit

 

1- Lab Setup
	

Note: This lab assumes that you have already installed the necessary lab files to run it.

1-Install the ISAPI Filter

 

- Right Click the Server Name

- Click "Properties"

- In the "WWW Service" Master Properties, click "Edit"

- In the "ISAPI Filters" tab, click "Add"

- In the "Filter Name" type in "BadFilter"

- In the "Executable", browse to C:\IISDebugLabs\www\App\badfil.dll

- Click "Apply" and click "Ok" all teh way out.

 

2- Restart the IIS Admin service

- Right Click the Server Name

- Click "Restart IIS" and Click "Ok"

 

3- Run Performace Monitor to monitor the process memory allocations.

a- Start Performance Monitor

b- Click the Add button

c- Under "Performance Object", Select "Process"

d- Under "Select Counters from List", select "Private Bytes" and "Virtual Bytes"

e- Under "Select Instances from List", select "inetinfo"

4- Attach LeakDiag tool to the inetinfo process

5- Add the symbol path in LeakDiag.

a) Using the GUI:

- Click "File"

- Click "Symbol File Path"

- Type in SRV*C:|LabSymbols\*\\symbols\symbols;c:\IISDebugLabs\Symbols\Pri and hit Enter

b) Choose the allocator to track

5- Choose the allocator to track 

Note: For more information on the LeakDiag tool, please visit http://leakdiag.

6- In LeakDiag GUI, click "Start"

 

 

2- Run Scenario

1- Depending on the allocator you chose to track in LeakDiag, Click the eaquivalent button to allocate each time 1024 B.
 For Example if you are tracking The C-Runtime allocator 
	a- Click "Allocate 1024 B (malloc)" many times. 	    
b- In "LeakDiag" GUI, generate a log file.
c- In "Performance Monitor", Compare the counters "Private Bytes" and "Virtual Bytes"

d- Repeat steps a and b four or five times
3- After taking the last log in LeakDiag, right click the process "dllhost" and choose "Genrate Dump" to generate a user dump

 

                                             

                                                    

 

 

1- Debug Annotation

 

1) What is memory leak?

 

Memory Leaks show up as an increasing Private Virtual size. At some point, the memory manager will stop the process from growing in physical size, though it can continue to

grow in its virtual size until it reaches around 1.5 GB.

2) Tracking "Windows Heap Allocator"

 

1- LeakDiag Logs without symbols

 

Using LDParser that you can download from http://leakdiag to read the logs, open one of the logs and make sure the "Stacks" table is sorted on "totalsize" column.

Select the first row in the "Stacks" table if you sorted on "totalsize" decreasing.

 

 

numallocs STACKID size totalsize

654 020C1468 1024 669696 

 

num dll function offset filename line addr

0 badfil.dll HttpFilterProc 0x84B 0x1621D4D

1 w3svc.dll STR__Copy 0x11A 0x65F0354F

2 w3svc.dll STR__Copy 0x87 0x65F034BC

3 w3svc.dll CLIENT_CONN__OnSessionStartup 0xA69 0x65F02CAD

4 w3svc.dll CLIENT_CONN__OnSessionStartup 0x87D 0x65F02AC1

 

 

This shows that the stack above did 654 allocations of 1024 B, a total of 669696 B and that "badFil.dll" is the one that made these allocations.

For precise reading of leakDiag logs, it is important that you compare the totalsize of allocations of the suspected stack to the increase you see in

performance monitor in "Private Bytes".

 

This data is enough to conclude that the culprit here is "badFil.dll"

 

2- LeakDiag Logs with symbols

 

num dll function offset filename line addr

0 badfil.dll doMemLeak1 0x72 0x1621D4D

1 w3svc.dll NotifyFilters 0x4c 0x65F0354F

2 w3svc.dll NotifyPreProcHeaderFilters 0x87 0x65F034BC

3 w3svc.dll Parse 0x380 0x65F02CAD

4 w3svc.dll OnCompleteRequest 0x36 0x65F02AC1

 

 

Back in Windbg, open the dump file generated by leakDiag to double check findings:

 

The addresses that LeakDiag logs show in the stack are where the functions are loaded in memory.

The "ln" debugger command shows what module!function is loaded in the specified address.

 

0:000> ln 0x1621D4D

(01621d42) badfil!oMemLeak1+0x72

 

You could use these addresses as well to walk through the disassembly for in depth analysis.

   

3) Tracking "C Runtime Allocator"

 

1- LeakDiag Logs without symbols

 

Open a leakDiag log, make sure that the table "STACKS" is sorted, select the row that corresponds to the highest "totalsize"

 

numallocs STACKID size totalsize

33 1 01E14D28 1024 338944

 

The correspoding frames would look like:

 

num dll function offset filename line addr

0 badfil.dll HttpFilterProc 0x834 0x1961D36

1 w3svc.dll STR__Copy 0x11A 0x65F0354F

2 w3svc.dll STR__Copy 0x87 0x65F034BC

3 w3svc.dll CLIENT_CONN__OnSessionStartup 0xA69 0x65F02CAD

4 w3svc.dll CLIENT_CONN__OnSessionStartup 0x87D 0x65F02AC1

 

 

This shows that the stack above did 33 allocations of 1024 each, a total of 338944B allocated. This is the highest total allocations size in this log which leaves no

doubts that the culprit is "badFil.dll".

 

2- LeakDiag Logs with symbols

 

 

numallocs STACKID size totalsize

70 02344468 1024 71680

 

The correspoding frames would look like:

 

num dll function offset filename line addr

0 badfil.dll doMemLeak1 0x834 0x1961D36

1 w3svc.dll NotifyFilters 0x4c 0x65F0354F

2 w3svc.dll NotifyPreProcHeaderFilters 0x87 0x65F034BC

3 w3svc.dll Parse 0x380 0x65F02CAD

4 w3svc.dll OnCompleteRequest 0x36 0x65F02AC1

 

 

This shows that the function "doMemLeak1" in the module "badFil.dll" is responsible for these allocations. This is enough

to tell the customer to review the code of this function for resolution.

Sometimes, you may want to provide additional data from the dump to the customer to show what data is leaked especially if you took 2 or 3 dumps (not logs)

during a leakDiag session. These dumps will allow you to identify what heap segment(s) are leaking.

 

 

4) Tracking "Virtual Memory Allocator"

 

Open a leakDiag log generated while tracking "Virtual Memory Allocator", make sure that the table "STACKS" is sorted, select the row that corresponds to the highest "totalsize"

 

numallocs STACKID size totalsize

566 01E04628 4096 2318336

 

The correspoding frames would look like:

 

num dll function offset filename line addr

0 KERNEL32.dll VirtualAllocEx 0x5D 0x77E87FC7

1 KERNEL32.dll VirtualAlloc 0x16 0x77E87F66

2 badfil.dll HttpFilterProc 0x827 0x1621D29

3 w3svc.dll STR__Copy 0x11A 0x65F0354F

4 w3svc.dll STR__Copy 0x87 0x65F034BC

 

 

This shows that the stack above did 566 allocations of 4096 each, a total of 2318336B allocated. This is the highest total allocations size in this log which leaves no

doubts that the culprit is "badFil.dll".

 

2- LeakDiag Logs with symbols

 

numallocs STACKID size totalsize

566 01E04628 4096 2318336

 

 

The correspoding frames would look like:

 

num dll function offset filename line addr

0 KERNEL32.DLL VirtualAllocEx 0x62 0x77E87FC7

1 KERNEL32.DLL VirtualAlloc 0x16 0x77E87F66

2 badfil.dll HttpFilterProc 0x827 0x1621D29

3 w3svc.dll NotifyFilters 0x4c 0x65F0354F

4 w3svc.dll NotifyPreProcHeaderFilters 0x87 0x65F034BC

 

With symbols, we can see that the function "doMemLeak1()" is the one calling the win32 API "virtualAlloc()" to allocate virtual memory.

Again, this is enough to confirm that the culprit is "badext.dll".

 

Here is how the function "doMemLeak" is implemented.

 

DWORD doMemLeak1(int Variation)

{

char *szVar = new char[10];

char *szVal = new char[10];

 

getQSVars (lpEcb, szVar, szVal);

int iType;

iType = atoi( szVal );

 

if (1 == Variation)

{

void* lp = NULL;

switch ( iType )

{

case 1: lp = HeapAlloc( GetProcessHeap(), 0, 1024 ); //tracked by "WIndows Heap Allocator"

break;

case 2: lp = malloc( 1024 ); //Tracked by "C Runtime Allocator"

break;

case 3: lp = VirtualAlloc( NULL, 1024, MEM_COMMIT, PAGE_READWRITE ); //Tracked by "Virtual Memory Allocator"

break;

default: lp = HeapAlloc( GetProcessHeap(), 0, 1024 ); break;

}

}

return HSE_STATUS_SUCCESS;

}

 

 


- Microsoft Confidential -