Пытаюсь вернуть строку из функции через параметр, в функции выделяю память с помощью MmAllocateNonCachedMemory, значение получаю, при попытке освободить память получаю BSOD. Что не так делаю? Код (Text): PWCHAR wcRecentPath = NULL; CreatePathToUserRecentFolder(UserName,&wcRecentPath); IFDEBUG(DbgPrint("wcRecentPath 0x%x\n",wcRecentPath)); if( MmIsAddressValid ( wcRecentPath ) ) { [b]MmFreeNonCachedMemory(wcRecentPath,MAX_PATH);[/b]//!!!!!!!!!!!!Тут падает } CreatePathToUserRecentFolder: Код (Text): VOID CreatePathToUserRecentFolder( IN PWCHAR UserName,OUT PWCHAR* Path) { PWCHAR usTempString; DWORD dwStrOffset = 0,dwTmp =0; usTempString =(PWCHAR) MmAllocateNonCachedMemory(MAX_PATH); /*формирование строки*/ *Path = usTempString; IFDEBUG(DbgPrint("usTempString 0x%x\n",usTempString)); } При сравнении значения wcRecentPath и UsTempString совпадают. Почему я не могу освободить память? Код (Text): UserName Admin UserName 0xf9fd2000 UserName 0xf9fd2000 *** Fatal System Error: 0x00000050 (0xF9FD2000,0x00000000,0x8050D5C7,0x00000000) Break instruction exception - code 80000003 (first chance) A fatal system error has occurred. Debugger entered on first try; Bugcheck callbacks have not been invoked. A fatal system error has occurred. Connected to Windows XP 2600 x86 compatible target at (Wed Nov 24 13:58:13.562 2010 (GMT+3)), ptr64 FALSE *** ERROR: Symbol file could not be found. Defaulted to export symbols for ntkrnlmp.exe - Loading Kernel Symbols ............................................................... ........................................................ Loading User Symbols Loading unloaded module list ........... ******************************************************************************* * * * Bugcheck Analysis * * * ******************************************************************************* Use !analyze -v to get detailed debugging information. BugCheck 50, {f9fd2000, 0, 8050d5c7, 0} ***** Kernel symbols are WRONG. Please fix symbols to do analysis. *** ERROR: Module load completed but symbols could not be loaded for mssmbios.sys ************************************************************************* *** *** *** *** *** Your debugger is not using the correct symbols *** *** *** *** In order for this command to work properly, your symbol path *** *** must point to .pdb files that have full type information. *** *** *** *** Certain .pdb files (such as the public OS symbols) do not *** *** contain the required information. Contact the group that *** *** provided you with these symbols if you need this command to *** *** work. *** *** *** *** Type referenced: nt!_KPRCB *** *** *** ************************************************************************* ************************************************************************* *** *** *** *** *** Your debugger is not using the correct symbols *** *** *** *** In order for this command to work properly, your symbol path *** *** must point to .pdb files that have full type information. *** *** *** *** Certain .pdb files (such as the public OS symbols) do not *** *** contain the required information. Contact the group that *** *** provided you with these symbols if you need this command to *** *** work. *** *** *** *** Type referenced: nt!_KPRCB *** *** *** ************************************************************************* Probably caused by : RecentMon.sys ( RecentMon!OpenRecentDirectory+cd ) Followup: MachineOwner --------- nt!DbgBreakPointWithStatus+0x4: 804e30d9 cc int 3 0: kd> !analyze -v ******************************************************************************* * * * Bugcheck Analysis * * * ******************************************************************************* PAGE_FAULT_IN_NONPAGED_AREA (50) Invalid system memory was referenced. This cannot be protected by try-except, it must be protected by a Probe. Typically the address is just plain bad or it is pointing at freed memory. Arguments: Arg1: f9fd2000, memory referenced. Arg2: 00000000, value 0 = read operation, 1 = write operation. Arg3: 8050d5c7, If non-zero, the instruction address which referenced the bad memory address. Arg4: 00000000, (reserved) Debugging Details: ------------------ ***** Kernel symbols are WRONG. Please fix symbols to do analysis. ************************************************************************* *** *** *** *** *** Your debugger is not using the correct symbols *** *** *** *** In order for this command to work properly, your symbol path *** *** must point to .pdb files that have full type information. *** *** *** *** Certain .pdb files (such as the public OS symbols) do not *** *** contain the required information. Contact the group that *** *** provided you with these symbols if you need this command to *** *** work. *** *** *** *** Type referenced: nt!_KPRCB *** *** *** ************************************************************************* ************************************************************************* *** *** *** *** *** Your debugger is not using the correct symbols *** *** *** *** In order for this command to work properly, your symbol path *** *** must point to .pdb files that have full type information. *** *** *** *** Certain .pdb files (such as the public OS symbols) do not *** *** contain the required information. Contact the group that *** *** provided you with these symbols if you need this command to *** *** work. *** *** *** *** Type referenced: nt!_KPRCB *** *** *** ************************************************************************* ADDITIONAL_DEBUG_TEXT: Use '!findthebuild' command to search for the target build information. If the build information is available, run '!findthebuild -s ; .reload' to set symbol path and load symbols. FAULTING_MODULE: 804d7000 nt DEBUG_FLR_IMAGE_TIMESTAMP: 4cecefb6 READ_ADDRESS: unable to get nt!MmSpecialPoolStart unable to get nt!MmSpecialPoolEnd unable to get nt!MmPoolCodeStart unable to get nt!MmPoolCodeEnd f9fd2000 FAULTING_IP: nt!KeInitializeDeviceQueue+356 8050d5c7 66833800 cmp word ptr [eax],0 MM_INTERNAL_CODE: 0 DEFAULT_BUCKET_ID: DRIVER_FAULT BUGCHECK_STR: 0x50 LAST_CONTROL_TRANSFER: from 8053721e to 804e30d9 STACK_TEXT: WARNING: Stack unwind information not available. Following frames may be wrong. f9eb2214 8053721e 00000003 80703a4c c03e7f48 nt!DbgBreakPointWithStatus+0x4 f9eb25f4 80537832 00000050 f9fd2000 00000000 nt!KeDeregisterBugCheckReasonCallback+0x6c7 f9eb2614 805296be 00000050 f9fd2000 00000000 nt!KeBugCheckEx+0x1b f9eb2664 804e0f07 00000000 f9fd2000 00000000 nt!KePulseEvent+0xd123 f9eb2688 8051ba6d 80507edd 80508797 f9eb2974 nt!Kei386EoiHelper+0x274d f9eb2958 80507fbe f9eb2974 f9cfc940 f9eb2c14 nt!wcscat+0x2f2 f9eb2994 80508702 f9eb29d0 00000200 f9cfc940 nt!vsnprintf+0x2f f9eb2bec 80508793 80508796 ffffffff 00000000 nt!vDbgPrintExWithPrefix+0x82 f9eb2c08 f9cfc61d f9cfc940 f9fd2000 f9fd0000 nt!DbgPrint+0x1a f9eb2c54 f9cfc68b 8151e528 f9eb2c84 f9cfc0f8 RecentMon!OpenRecentDirectory+0xcd [d:\projects\recentmon\recentmon\recentmonfunc.c @ 126] f9eb2c60 f9cfc0f8 002c002a f9cfc6f0 c0000035 RecentMon!StartRecentMonitor+0xb [d:\projects\recentmon\recentmon\recentmonfunc.c @ 142] f9eb2c84 805a3777 8163e2c0 814e6000 00000000 RecentMon!DriverEntry+0xe8 [d:\projects\recentmon\recentmon\recentmon.c @ 46] f9eb2d54 805ad447 0000072c 00000001 00000000 nt!RtlUpperChar+0x1872 f9eb2d7c 804e29d6 0000072c 00000000 817c7020 nt!MmResetDriverPaging+0x514 f9eb2dac 80576b24 f0713cf4 00000000 00000000 nt!KeRemoveQueue+0x221 f9eb2ddc 804eed86 804e2912 00000001 00000000 nt!PsCreateSystemThread+0x70 00000000 00000000 00000000 00000000 00000000 nt!KeInitializeTimerEx+0x1eb STACK_COMMAND: kb FOLLOWUP_IP: RecentMon!OpenRecentDirectory+cd [d:\projects\recentmon\recentmon\recentmonfunc.c @ 126] f9cfc61d 83c408 add esp,8 FAULTING_SOURCE_CODE: 122: if( MmIsAddressValid ( wcRecentPath ) ) 123: { 124: MmFreeNonCachedMemory(wcRecentPath,MAX_PATH); 125: } > 126: IFDEBUG(DbgPrint("Recent Folder path %S\n",usRecentFolderPath.Buffer)); 127: 128: InitializeObjectAttributes(&DirectoryAttributes,&usRecentFolderPath,OBJ_CASE_INSENSITIVE,NULL,NULL); 129: rc = ZwOpenFile(&hPathDirHandle,GENERIC_READ,&DirectoryAttributes,&iosb,FILE_SHARE_READ|FILE_SHARE_WRITE,FILE_DIRECTORY_FILE); 130: if (rc!=STATUS_SUCCESS) 131: { SYMBOL_STACK_INDEX: 9 SYMBOL_NAME: RecentMon!OpenRecentDirectory+cd FOLLOWUP_NAME: MachineOwner MODULE_NAME: RecentMon IMAGE_NAME: RecentMon.sys BUCKET_ID: WRONG_SYMBOLS Followup: MachineOwner ---------
А 'DbgPrint("%s")' где-нибудь в коде встречается? Такое ощущение, что вызывается 'DbgPrint("%s")' для строки без завершающего нуля.
странное ощущение.. можешь описать, почему возникают такие мысли?? дбгпринты есть, но причем тут они?
Падение, если верить стеку, произошло в DbgPrint: Код (Text): f9eb2c08 f9cfc61d f9cfc940 f9fd2000 f9fd0000 nt!DbgPrint+0x1a f9eb2c54 f9cfc68b 8151e528 f9eb2c84 f9cfc0f8 RecentMon!OpenRecentDirectory+0xcd [d:\projects\recentmon\recentmon\recentmonfunc.c @ 126] Инструкция, на которой произошло падение, похожа на инструкцию поиска завершающего нуля: Код (Text): FAULTING_IP: nt!KeInitializeDeviceQueue+356 8050d5c7 66833800 cmp word ptr [eax],0 Ты не показал, как заполняется usTempString: Код (Text): usTempString =(PWCHAR) MmAllocateNonCachedMemory(MAX_PATH); /*формирование строки*/ *Path = usTempString; Ну и немного психологии . Тебе наверняка было интересно, как заполнилась строка, вот ты и сделал что-то в духе 'DbgPrint(usTempString)' или 'DbgPrint("%s\n", usTempString)'. Если, конечно, предположения верны...
Во-первых, не понятно, как инициализируется строка usTempString, может быть там ошибка. Во-вторых, для Unicode-строк в DbgPrint() следует указывать %S, а не %s. Чтобы избежать таких ошибок в будущем, следует использовать структуру UNICODE_STRING и выводить её через %wZ в DbgPrint(). Ну и совет автору: научись, наконец, префиксы у переменных выставлять корректно: us для UNICODE_STRING, pus для PUNICODE_STRING, w для WCHAR, pw для PWSTR и т.д.
У меня точно %S, PWCHAR usTempString =(PWCHAR) MmAllocateNonCachedMemory(MAX_PATH); вот так она инициализируется
Код (Text): VOID CreatePathToUserRecentFolder( IN PWCHAR UserName,OUT PWCHAR* Path) { PWCHAR usTempString; DWORD dwStrOffset = 0,dwTmp = 0; //Create string in buffer by memory copping usTempString =(PWCHAR) MmAllocateNonCachedMemory(MAX_PATH); dwTmp = wcslen(SYSTEM_DISK); RtlCopyMemory(usTempString,SYSTEM_DISK,dwTmp*sizeof(WCHAR)); dwStrOffset += dwTmp; dwTmp = wcslen(L"Documents and Settings\\"); RtlCopyMemory(usTempString+dwStrOffset,L"Documents and Settings\\",dwTmp*sizeof(WCHAR)); dwStrOffset += dwTmp; dwTmp = wcslen(UserName); RtlCopyMemory(usTempString+dwStrOffset,UserName,dwTmp*sizeof(WCHAR)); dwStrOffset += dwTmp; dwTmp = wcslen(L"\\Recent"); RtlCopyMemory(usTempString+dwStrOffset,L"\\Recent",dwTmp*sizeof(WCHAR)); *Path = usTempString; IFDEBUG(DbgPrint("UserName 0x%x\n",usTempString)); } Вот так