windows»·¾³ÏÂ32λ»ã±àÓïÑÔ³ÌÐòÉè¼Æ-µÚ59ÕÂ
°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
ÔÚÕâЩÇé¿öÏ£¬¿ÉÒÔÖ¸¶¨HEAP_NO_SERIALIZE¡¡¡¡¡¡¡¡±êÖ¾À´½¨Á¢Ë½Óжѣ¬ÕâÑù½¨Á¢µÄ¶Ñ²»»á½øÐжÀÕ¼ÐԵļì²â£¬·ÃÎÊËٶȿÉÒÔ¸ü¿ì¡£
²ÎÊýdwInitialSizeÖ¸¶¨´´½¨¶Ñʱ·ÖÅä¸ø¶ÑµÄÎïÀíÄڴ棬Ëæ×ŶÑÖÐÄÚ´æµÄ·ÖÅ䣬µ±ÕâЩÄڴ汻ʹÓÃÍêʱ£¬¶ÑµÄ³¤¶È¿ÉÒÔ×Ô¶¯À©Õ¹¡£dwMaximumSize²ÎÊýÖ¸¶¨ÁËÄܹ»À©Õ¹µ½µÄ×î´óÖµ£¬µ±À©Õ¹µ½×î´óֵʱÔÙ³¢ÊÔÔÚ¶ÑÖзÖÅäÄÚ´æµÄ»°¾Í»áʧ°Ü£¬Õâ¸öÖµ¾ö¶¨ÁËϵͳ¸ø¶Ñ±£ÁôµÄÁ¬ÐøµØÖ·¿Õ¼äµÄ´óС£¬º¯Êý»á×Ô¶¯½«ÕâÁ½¸ö²ÎÊýµÄÊýÖµµ÷ÕûΪҳÃæ´óСµÄÕûÊý±¶¡£Èç¹ûdwMaximumSize²ÎÊýµÄÖµÖ¸¶¨Îª0£¬ÄÇô¶ÑûÓÐ×î´óÖµÏÞÖÆ£¬À©Õ¹·¶Î§Ö»ÊÜÏÞÓÚ¿ÕÏеÄÄÚ´æ×ÜÁ¿¡£Èç¹ûdwMaximumSizeÖ¸¶¨Îª·Ç0Öµ£¬ÔÚ¶ÑÖÐÉêÇëµÄ×î´óµ¥¸öÄÚ´æ¿é²»ÄÜ´óÓÚ7FFF8h£¨Ï൱ÓÚ524¡¡KB£©£¬dwMaximumSizeÖ¸¶¨0µÄ»°¾ÍûÓÐÕâ¸öÏÞÖÆ¡£
Èç¹ûÒ»¸ö˽ÓжѲ»ÔÙÐèÒªÁË£¬¿ÉÒÔͨ¹ýµ÷ÓÃHeapDestroyº¯Êý½«ËüÊÍ·Å£º
¡¡¡¡¡¡¡¡invoke¡¡¡¡HeapDestroy£¬hHeap
ÊÍ·Å˽ÓжѿÉÒÔÊͷŶÑÖаüº¬µÄËùÓÐÄÚ´æ¿é£¬Ò²¿ÉÒÔ½«¶ÑÕ¼ÓõÄÎïÀíÄÚ´æºÍ±£ÁôµÄµØÖ·¿Õ¼äÈ«²¿·µ»¹¸øϵͳ¡£Èç¹ûº¯ÊýÔËÐгɹ¦£¬·µ»ØÖµÊÇTRUE¡£µ±ÔÚ½ø³ÌÖÕÖ¹µÄʱºòûÓе÷ÓÃHeapDestroyº¯Êý½«Ë½ÓжÑÊÍ·Åʱ£¬ÏµÍ³»á×Ô¶¯ÊÍ·Å¡£
ËäÈ»ÔÚĬÈ϶ÑÖеÄÄÚ´æÉêÇëÖ÷ҪʹÓñê×¼ÄÚ´æ¹ÜÀíº¯Êý£¬¶ø¶Ñ¹ÜÀíº¯ÊýµÄÖ÷Òª¹ÜÀí¶ÔÏóÊÇ˽Óжѣ¬µ«ÊÇÈç¹û±à³ÌÕßÔ¸ÒâµÄ»°£¬Ò²¿ÉÒÔÓöѹÜÀíº¯ÊýÔÚĬÈ϶ÑÖзÖÅäÄڴ棬±Ï¾¹Ä¬È϶ÑÒ²ÊÇÒ»¸ö¶Ñ£¬µ«ÕâÑùµÄ»°Ê×ÏÈÐèÒªÓÐÒ»¸ö¾ä±úÀ´´ú±íĬÈ϶ѣ¬Ä¬È϶ѵľä±ú²»ÄÜÓÃHeapCreateÀ´´´½¨£¬µ«¿ÉÒÔÓÃGetProcessHeapº¯ÊýÀ´»ñÈ¡£¬Õâ¸öº¯ÊýûÓÐÊäÈë²ÎÊý£¬Èç¹ûÖ´Ðгɹ¦Ôò·µ»ØĬÈ϶ѵľä±ú¡£×¢Ò⣺Õâ¸ö¾ä±úÊÇ¡°»ñÈ¡¡±µÄ¶ø²»ÊÇ¡°´´½¨¡±µÄ£¬ËùÒÔ²»Äܵ÷ÓÃHeapDestroyÀ´ÊÍ·ÅËü£¬Èç¹û¶ÔËüµ÷ÓÃHeapDestroyº¯Êý£¬ÏµÍ³»á½«ËüºöÂÔ¡£
2¡£¡¡ÔÚ¶ÑÖзÖÅäºÍÊÍ·ÅÄÚ´æ¿é
Èç¹ûÒªÔÚ¶ÑÖзÖÅäÄÚ´æ¿é£¬¿ÉÒÔʹÓÃHeapAllocº¯Êý£º
¡¡¡¡¡¡¡¡invoke¡¡¡¡HeapAlloc£¬hHeap£¬dwFlags£¬dwBytes
¡¡¡¡¡¡¡¡¡£if¡¡¡¡¡¡¡¡¡¡eax¡¡&&¡¡£¨eax¡¡¡¶¡¡0c0000000h£©
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mov¡¡¡¡¡¡¡¡¡¡lpMemory£¬eax
¡¡¡¡¡¡¡¡¡£endif
hHeap²ÎÊý¾ÍÊÇÇ°Ãæ´´½¨¶Ñʱ·µ»ØµÄ¶Ñ¾ä±ú£¨»òÕßʹÓÃGetProcessHeapº¯ÊýµÃµ½µÄĬÈ϶Ѿä±ú£©£¬ÓÃÀ´±íʾÔÚÄĸö¶ÑÖзÖÅäÄڴ棬dwBytesÊÇÐèÒª·ÖÅäµÄÄÚ´æ¿éµÄ×Ö½ÚÊý£¬dwFlagsÊDZêÖ¾£¬Ëü¿ÉÒÔÊÇÏÂÃæÖµµÄ×éºÏ£º
¡ñ¡¡¡¡¡¡HEAP_NO_SERIALIZE¡ª¡ªµ±Ê¹ÓÃHeapCreateʱָ¶¨ÁËHEAP_NO_SERIALIZE±êÖ¾£¬ÒÔºóÕâ¸ö¶ÑÖÐʹÓõÄËùÓÐHeapAllocº¯Êý¶¼²»½øÐжÀÕ¼¼ì²â¡£Èç¹ûʹÓÃHeapCreateʱûÓÐÖ¸¶¨HEAP_NO_SERIALIZE±êÖ¾£¬¿ÉÒÔÔÚÕâÀïʹÓÃHEAP_NO_SERIALIZE±êÖ¾µ¥¶ÀÖ¸¶¨¶Ô±¾´Î·ÖÅä²Ù×÷²»½øÐжÀÕ¼¼ì²â¡£
¡ñ¡¡¡¡¡¡HEAP_GENERATE¡¡_EXCEPTIONS¡ª¡ªÈç¹ûÉêÇëÄÚ´æʧ°Üº¯Êý·µ»Ø¾ßÌåµÄ³ö´íÔÒò£¬¶ø²»½ö·µ»ØÒ»¸öNULL¡£Í¬Ñù£¬µ±Ê¹ÓÃHeapCreateʱָ¶¨Á˴˱êÖ¾µÄÇé¿öÏ£¬ÔÚÕâÀï¾Í²»±ØÔÙÒ»´ÎÖ¸¶¨¡£
¡ñ¡¡¡¡¡¡HEAP_ZERO_MEMORY¡ª¡ª½«·ÖÅäµÄÄÚ´æÓÃ0³õʼ»¯¡£
µ±º¯Êý·ÖÅäÄÚ´æ³É¹¦µÄʱºò£¬·µ»ØÖµÊÇÖ¸ÏòÄÚ´æ¿éµÚÒ»¸ö×Ö½ÚµÄÖ¸Õ룬Èç¹û·ÖÅäÄÚ´æʧ°Ü£¬·µ»ØÖµÒªÊÓdwFlagsµÄÉèÖã¬Èç¹ûûÓÐÖ¸¶¨HEAP_GENERATE_EXCEPTIONS±êÖ¾£¬ÄÇô·µ»ØֵΪNULL£¬·ñÔò£¬·µ»ØÖµ¿ÉÄÜÊÇÏÂÃæµÄÊýÖµ£º
¡ñ¡¡¡¡¡¡STATUS_NO_MEMORY¡ª¡ªÈ¡ÖµÎª0C0000017h£¬±íʾÄÚ´æ²»¹»¡£
¡ñ¡¡¡¡¡¡STATUS_ACCESS_VIOLATION¡ª¡ªÈ¡ÖµÎª0C0000005h£¬±íʾ²ÎÊý²»ÕýÈ·»òÕ߶ѱ»ÆÆ»µ¡£
ÔÚ¶ÑÖзÖÅäµÄÄÚ´æ¿éÖ»ÄÜÊǹ̶¨µØÖ·µÄÄÚ´æ¿é£¬²»ÏñGlobalAllocº¯ÊýÒ»Ñù¿ÉÒÔ·ÖÅä¿ÉÒƶ¯µÄÄÚ´æ¿é¡£Èç¹ûÒªÊÍ·Å·ÖÅäµ½µÄÄÚ´æ¿é£¬¿ÉÒÔʹÓÃHeapFreeº¯Êý£º
¡¡¡¡¡¡¡¡invoke¡¡¡¡HeapFree£¬hHeap£¬dwFlags£¬lpMemory
hHeap²ÎÊýÊǶѾä±ú£¬lpMemoryÊÇHeapAllocº¯Êý·µ»ØµÄÄÚ´æ¿éÖ¸Õ룬dwFlags²ÎÊýÖÐÒ²¿ÉÒÔʹÓÃHEAP_NO_SERIALIZE±êÖ¾£¬º¬ÒåÓëʹÓÃHeapAllocʱÏàͬ¡£µ±º¯ÊýÖ´Ðгɹ¦µÄʱºò£¬·µ»ØֵΪ·Ç0Öµ£¬Ö´ÐÐʧ°ÜÔòº¯Êý·µ»Ø0¡£
¶ÔÓÚÓÃHeapAlloc·ÖÅäµÄÄÚ´æ¿é£¬Ò²¿ÉÒÔʹÓÃHeapReAllocÖØе÷Õû´óС£º
invoke¡¡¡¡HeapReAlloc£¬hHeap£¬dwFlags£¬lpMemory£¬dwBytes
¡¡¡¡¡¡¡¡¡£if¡¡¡¡¡¡¡¡¡¡eax¡¡&&¡¡£¨eax¡¡¡¶¡¡0c0000000h£©
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mov¡¡¡¡¡¡¡¡¡¡lpMemory£¬eax
¡¡¡¡¡¡¡¡¡£endif
¡¡
À´Ô´£ºµç×Ó¹¤Òµ³ö°æÉç¡¡×÷ÕߣºÂÞÔƱò¡¡ÉÏÒ»Ò³¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡»ØÊéÄ¿¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÏÂÒ»Ò³¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
ÉÏÒ»Ò³¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡»ØÊéÄ¿¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÏÂÒ»Ò³¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡
µÚ10Õ¡¡ÄÚ´æ¹ÜÀíºÍÎļþ²Ù×÷
10¡£1¡¡ÄÚ¡¡´æ¡¡¹Ü¡¡Àí£¨6£©
¡¡¡¡¡¡¡¡
ÆäÖÐdwBytesÖ¸¶¨ÁËеĴóС£¬dwFlagsΪ±êÖ¾£¬¿ÉÒÔ×éºÏÖ¸¶¨µÄ±êÖ¾ÓУº
¡ñ¡¡¡¡¡¡HEAP_GENERATE_EXCEPTIONS¡ª¡ª²Î¼ûHeapAllocº¯ÊýµÄ˵Ã÷¡£
¡ñ¡¡¡¡¡¡HEAP_NO_SERIALIZE¡ª¡ª²Î¼ûHeapAllocº¯ÊýµÄ˵Ã÷¡£
¡ñ¡¡¡¡¡¡HEAP_ZERO_MEMORY¡ª¡ªµ±À©´óÄÚ´æ¿éµÄʱºò£¬½«ÐÂÔöµÄ²¿·Ö³õʼ»¯Îª0£¬µ±ËõСÄÚ´æµÄʱºò£¬±¾²ÎÊýÎÞЧ¡£
¡ñ¡¡¡¡¡¡HEAP_REALLOC_IN_PLACE_ONLY¡ª¡ªÓëGlobalReAllocº¯ÊýÀàËÆ£¬µ±ÄÚ´æ¿éµÄ¸ß´¦ÒѾ±»ÆäËûÄÚ´æ¿éÕ¼¾ÝµÄʱºò£¬ÒªÀ©´óÄÚ´æ¿é±ØÐ뽫ËüÒƶ¯Î»Ö㬵±Ã»ÓÐÖ¸¶¨Õâ¸ö±êÖ¾µÄʱºò£¬º¯Êý»áÔÚÐèÒªµÄʱºò×Ô¶¯Òƶ¯ÄÚ´æ¿é£¬Èç¹ûÖ¸¶¨ÁËÕâ¸ö±êÖ¾£¬Ôò²»ÔÊÐíÄÚ´æ¿éÒƶ¯£¬Õâʱ£¬µ±ÄÚ´æ¿é¸ß´¦²»ÊÇ¿ÕÏеÄʱºò£¬º¯ÊýµÄÖ´Ðлáʧ°Ü¡£
Èç¹ûº¯ÊýÖ´Ðгɹ¦£¬·µ»ØÖµÊÇÖ¸ÏòÐÂÄÚ´æ¿éµÄÖ¸Õ룬ÏÔ¶øÒ×¼û£¬µ±ËõС»òÀ©´óÄÚ´æ¿éʱָ¶¨ÁËHEAP_REALLOC_IN_PLACE_ONLY±êÖ¾£¬ÔòÕâ¸öÖ¸Õë±Ø¶¨ºÍÔÀ´µÄÏàͬ£¬·ñÔòµÄ»°£¬Ëü¼ÈÓпÉÄܺÍÔÀ´µÄÖ¸ÕëÏàͬҲÓпÉÄܲ»Í¬¡£
3¡£¡¡ÆäËû¶Ñ¹ÜÀíº¯Êý
³ýÁËÉÏÃæµÄһЩº¯Êý£¬¶Ñ¹ÜÀíº¯ÊýÖл¹ÓÐHeapLock£¬HeapUnlock£¬GetProcessHeaps£¬Heappact£¬HeapSize£¬HeapValidateºÍHeapWalkµÈº¯Êý¡£
GetProcessHeapsº¯ÊýÓÃÀ´Áгö½ø³ÌÖÐËùÓеĶѣ¨×¢Ò⣺²»ÒªºÍÓÃÀ´»ñȡĬÈ϶Ѿä±úµÄGetProcessHeapº¯Êý¸ã»ì£©£¬HeapWalkÓÃÀ´ÁгöÒ»¸ö¶ÑÖÐËùÓеÄÄÚ´æ¿é£¬HeapValidateº¯ÊýÓÃÀ´¼ìÑéÒ»¸ö¶ÑÖÐËùÓÐÄÚ´æ¿éµÄÓÐЧÐÔ¡£Õâ3¸öº¯ÊýƽʱºÜÉÙʹÓã¬Ò»°ãÔÚµ÷ÊÔµÄʱºòʹÓá£
GetProcessHeapsº¯ÊýµÄÓ÷¨ÊÇ£º
¡¡¡¡¡¡¡¡invoke¡¡¡¡GetProcessHeaps£¬NumberOfHeaps£¬lpHeaps
ÆäÖÐlpHeapsÊÇÒ»¸öÖ¸Õ룬ָÏòÓÃÀ´½ÓÊնѾä±úµÄ»º³åÇø£¬NumberOfHeaps²ÎÊýÖ¸¶¨ÁËÕâ¸ö»º³åÇøÖпÉÒÔ´æ·Å¾ä±úµÄÊýÁ¿£¬ÏÔÈ»£¬»º³åÇøµÄ³¤¶ÈÓ¦¸ÃµÈÓÚNumberOfHeaps³ËÒÔ4×Ö½Ú¡£º¯ÊýÖ´Ðк󣬽ø³ÌÖÐËùÓжѵľä±úÈ«²¿·µ»Øµ½»º³åÇøÖУ¬ÆäÖÐÒ²°üÀ¨Ä¬È϶ѵľä±ú¡£
HeapWalkº¯ÊýµÄÓ÷¨ÊÇ£º
¡¡¡¡¡¡¡¡¡£repeat
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡invoke¡¡¡¡HeapWalk£¬hHeap£¬lpEntry
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡push¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡eax
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£»¼ì²â»º³åÇøÖеÄÄÚ´æ¿éÐÅÏ¢
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡pop¡¡eax
¡£until¡¡¡¡£¡eax
hHeapÊÇÐèÒª²Ù×÷µÄ¶Ñ¾ä±ú£¬lpEntryÖ¸ÏòÒ»¸ö°üº¬ÓÐPROCESS_HEAP_ENTRY½á¹¹µÄ»º³åÇø¡£µ÷ÓÃHeapWalkº¯Êýʱ£¬º¯Êýÿ´ÎÔÚPROCESS_HEAP_ENTRY½á¹¹Öзµ»ØÒ»¸öÄÚ´æ¿éµÄÐÅÏ¢£¬Èç¹û»¹ÓÐÆäËûÄÚ´æ¿é£¬º¯Êý·µ»ØTRUE£¬³ÌÐò¿ÉÒÔһֱѻ·µ÷ÓÃHeapWalkº¯ÊýÖ±µ½º¯Êý·µ»ØFALSEΪֹ¡£ÔÚ¶àÏ̵߳ijÌÐòÖÐʹÓÃHeapWalk£¬±ØÐëÊ×ÏÈʹÓÃHeapLockº¯Êý½«¶ÑËø¶¨£¬·ñÔòµ÷Óûáʧ°Ü¡£
HeapValidateÓÃÀ´ÑéÖ¤¶ÑµÄÍêÕûÐÔ»ò¶ÑÖÐij¸öÄÚ´æ¿éµÄÍêÕûÐÔ£º
¡¡¡¡¡¡¡¡invoke¡¡¡¡HeapValidate£¬hHeap£¬dwFlags£¬lpMemory
ÆäÖÐhHeapÖ¸¶¨ÒªÑéÖ¤µÄ¶Ñ¡£Èç¹ûlpMemoryΪNULL£¬ÄÇôº¯Êý˳ÐòÑéÖ¤¶ÑÖÐËùÓеÄÄÚ´æ¿é£»Èç¹ûlpMemoryÖ¸¶¨ÁËÒ»¸öÄÚ´æ¿é£¬ÔòÖ»ÑéÖ¤Õâ¸öÄÚ´æ¿é¡£dwFlagsÊDZêÖ¾£¬¿ÉÒÔÖ¸¶¨HEAP_NO_SERIALIZE¡¡±êÖ¾¡£Èç¹ûÑéÖ¤½á¹ûÊÇËùÓеÄÄÚ´æ¿é¶¼ÍêºÃÎÞË𣬺¯Êý·µ»Ø·Ç0Öµ£¬·ñÔòº¯Êý·µ»Ø0¡£
HeapLockº¯ÊýºÍHeapUnlockº¯ÊýÓÃÀ´Ëø¶¨¶ÑºÍ½âËø¶Ñ¡£ÕâÁ½¸öº¯ÊýÖ÷ÒªÓÃÓÚÏ̵߳Äͬ²½£¬µ±ÔÚÒ»¸öÏß³ÌÖе÷ÓÃHeapLockº¯Êýʱ£¬Õâ¸öÏß³ÌÔÝʱ³ÉΪÕâ¸ö¶ÑµÄËùÓÐÕߣ¬Ò²¾ÍÊÇ˵ֻÓÐÕâ¸öÏß³ÌÄܶԶѽøÐвÙ×÷£¨°üÀ¨·ÖÅäÄÚ´æ¡¢ÊÍ·Å¡¢µ÷ÓÃHeapWalkµÈº¯Êý£©£¬ÔÚ±ðµÄÏß³ÌÖжÔÕâ¸ö¶ÑµÄ²Ù×÷»áµÈ´ýÔÚÄÇÀֱµ½ËùÓÐÕßÏ̵߳÷ÓÃHeapUnlock½âËøΪֹ¡£ÕâÁ½¸öº¯ÊýµÄÓï·¨ÈçÏ£º
¡¡¡¡¡¡¡¡invoke¡¡¡¡HeapLock£¬hHeap
¡¡¡¡¡¡¡¡invoke¡¡¡¡HeapUnlock£¬hHeap
Èç¹ûº¯ÊýÖ´Ðгɹ¦£¬·µ»ØֵΪ·Ç0Öµ£¬·ñÔòº¯Êý·µ»Ø0¡£Ò»°ãÀ´Ëµ£¬ºÜÉÙÔÚ³ÌÐòÖÐʹÓÃÕâÁ½¸öº¯Êý£¬¶ø×ÜÊÇʹÓÃHEAP_NO_SERIALIZE±êÖ¾À´½øÐÐͬ²½¿ØÖÆ£¬Ö¸¶¨ÁËÕâ¸ö±êÖ¾µÄ»°£¬HeapAlloc£¬HeapReAlloc£¬HeapSizeºÍHeapFreeµÈº¯Êý»áÔÚÄÚ²¿×Ô¼ºµ÷ÓÃHeapLockºÍHeapUnlockº¯Êý¡£
Heappactº¯ÊýÓÃÓںϲ¢¶ÑÖеĿÕÏÐÄÚ´æ¿é²¢ÊͷŲ»ÔÚʹÓÃÖеÄÄÚ´æÒ³Ã棺
¡¡¡¡¡¡¡¡invoke¡¡¡¡Heappact£¬hHeap£¬dwFlags
HeapSizeº¯Êý·µ»Ø¶ÑÖÐij¸öÄÚ´æ¿éµÄ´óС£¬Õâ¸ö´óС¾ÍÊÇʹÓÃHeapAllocÒÔ¼°HeapReAllocʱָ¶¨µÄ´óС£º
¡¡¡¡¡¡¡¡invoke¡¡¡¡HeapSize£¬hHeap£¬dwFlags£¬lpMemory
lpMemoryÖ¸¶¨ÁËÐèÒª·µ»Ø´óСµÄÄÚ´æ¿é£¬º¯ÊýµÄ·µ»ØÖµÊÇÄÚ´æ¿éµÄ´óС£¬Èç¹ûÖ´ÐÐʧ°Ü£¬º¯Êý·µ»Ø£¿1¡£
10¡£1¡£5¡¡¡¡ÐéÄâÄÚ´æ¹ÜÀíº¯Êý
²»¹Üij¸ö½ø³Ìʵ¼Ê¿ÉÓõÄÎïÀíÄÚ´æÊǶàÉÙ£¬Ã¿¸ö½ø³Ì¿ÉÒÔʹÓõĵØÖ·¿Õ¼ä×ÜÊÇ2¡¡GB£¬Óû§³ÌÐò²»±Ø¿¼ÂÇÒ»¸öÏ̵߳ØÖ·¶ÔÓ¦µÄÎïÀíÄڴ澿¾¹°²ÅÅÔÚʲôµØ·½¡ª¡ªÊÇÔÚÕæÕýµÄÎïÀíÄÚ´æÖУ¿ÔÚ´ÅÅ̽»»»ÎļþÖУ¿»¹ÊǸù±¾Ã»ÓÐÎïÀíÄÚ´æÓëÖ®¶ÔÓ¦¡£
Ò»¸ö½ø³ÌµÄÕû¸öµØÖ·¿Õ¼äÊǿ͹۴æÔڵģ¬µ«ÊÇ·ñÓÐÄÚ´æÓë¸Ã¶ÎµØÖ·¿Õ¼äÖеĵØÖ·Ïà¹ØÁªÊÇÁíÍâµÄÎÊÌ⣬Windows¸ºÔðÔÚÊʵ±µÄʱ¼ä°ÑÏ̵߳ØÖ·Ó³Éäµ½ÎïÀíÄÚ´æ»ò´ÅÅÌÉϵĽ»»»ÎļþÉÏ£¬Õâ¾ÍÊÇÐéÄâÄÚ´æµÄ»ù±¾¸ÅÄî¡£
ÔÚ³ÌÐòÔËÐеÄʱºò£¬½ø³ÌÖÐÿ¸öµØÖ·¶¼¿ÉÒÔ´¦ÓÚÏÂÁÐ3ÖÖ״̬µÄ1ÖÖÖУº
¡ñ¡¡¡¡¡¡Õ¼ÓÃ״̬¡ª¡ªÏ̵߳ØÖ·ÒѾӳÉ䵽ʵ¼ÊµÄÎïÀíÄÚ´æÖС£Ò²³ÆΪÒÑÌύ״̬¡£
¡ñ¡¡¡¡¡¡×ÔÓÉ״̬¡ª¡ªÃ»ÓÐÓ³Éäµ½ÎïÀíÄÚ´æÖУ¬Ï̵߳ØÖ·µ±Ç°Ò²Ã»Óб»³ÌÐòʹÓá£
¡ñ¡¡¡¡¡¡±£Áô״̬¡ª¡ªËäÈ»Ï̵߳ØַûÓÐÓ³Éäµ½ÎïÀíÄÚ´æÖУ¬µ«Ëü²»»á±»Ê¹Óã¬Ö±µ½³ÌÐòÏ£ÍûʹÓÃËüΪֹ¡£
½ø³Ì¿ªÊ¼µÄʱºò£¬ËùÓеØÖ·¶¼ÊÇ´¦ÓÚ×ÔÓÉ״̬µÄ£¬ÕâÒâζ×ÅËüÃǶ¼ÊÇ×ÔÓɿռ䲢ÇÒ¿ÉÒÔ±»Ìá½»µ½ÎïÀíÄڴ棬»òÕßΪ½«À´Ê¹Óöø±£ÁôÆðÀ´¡£ÈκÎ×ÔÓÉ״̬µØÖ·ÔÚÄܹ»±»Ê¹ÓÃÇ°£¬±ØÐëÊ×Ïȱ»·ÖÅäΪ±£Áô״̬»òÒÑÌύ״̬¡£
µ±Ê¹Óñê×¼ÄÚ´æ¹ÜÀíº¯Êý·ÖÅäÄÚ´æµÄʱºò£¬Óû§ÎÞ·¨Ö¸¶¨ÄÚ´æ¿éλÓÚÄĸöÏ̵߳ØÖ·£¬»òÕß²»ÒªÎ»ÓÚÄĸöÏ̵߳ØÖ·£¬¶øʹÓÃÐéÄâÄÚ´æ¹ÜÀíº¯Êý¿ÉÒÔ×öµ½ÕâÒ»µã¡£µ«ÕâÑù×öµÄÀíÓÉÊÇʲôÄØ£¿¿¼ÂÇÕâÑùÒ»ÖÖÇé¿ö£º³ÌÐòÐèÒªÒ»¸öÄÚ´æ¿éÓÃ×ö»º³åÇø£¬Ëæ×ųÌÐòµÄÔËÐУ¬Õâ¸öÄÚ´æ¿é¿ÉÄÜËæʱÐèÒªÀ©Õ¹£¬×î´ó¿ÉÄÜÀ©Õ¹Îª100¡¡MB´óС£¬ËùÒÔÏ£ÍûϵͳÔÚ·ÖÅäÆäËûÄÚ´æ¿éµÄʱºò²»ÒªÊ¹ÓÃÕâ¸öÄÚ´æ¿éºóÃæ100¡¡MB´óС·¶Î§ÄڵĵØÖ·¿Õ¼ä£¬ÕâÑù£¬¾Í¿ÉÒÔËæʱ½«ÄÚ´æ¿éÀ©´ó¶ø²»±ØÒƶ¯ËüµÄλÖá£
³ýÁËÕâÑùÒ»¸öÖ÷ÒªµÄÓÃ;Í⣬ÐéÄâÄÚ´æ¹ÜÀíº¯Êý»¹Ìṩת»»ÐéÄâµØÖ·¿Õ¼äҳ״̬µÄÄÜÁ¦£¬Ò»¸öÓ¦ÓóÌÐò¿ÉÒÔ°ÑÄÚ´æµÄ״̬´ÓÒÑÌá½»¸Ä±äΪ±£Áô£¬»ò°Ñ±£»¤µÄģʽ´Ó¡¡PAGE¡¡_READWRITE¡¡£¨¿É¶Áд£©¸Ä±äΪ¡¡PAGE_READONLY£¨Ö»¶Á£©£¬´Ó¶ø·ÀÖ¹¶Ôij¶ÎµØÖ·¿Õ¼äµÄд·ÃÎÊ£»Ó¦ÓóÌÐòÒ²¿ÉÒÔËø¶¨Ò»Ò³Äڴ棬²»ÈÃËü±»½»»»µ½´ÅÅÌÖС£
ÐéÄâÄÚ´æ¹ÜÀíº¯ÊýÊÇÒ»×éÃû×ÖÒÔVirtual¿ªÍ·µÄº¯Êý£¬Ö÷Òª°üÀ¨ÏÂÃ漸ÖÖ£º
¡ñ¡¡¡¡¡¡VirtualAllocºÍVirtualFree¡ª¡ª½øÐеØÖ·¿Õ¼äµÄ·ÖÅäºÍÊͷŹ¤×÷¡£
¡ñ¡¡¡¡¡¡VirtualLockºÍVirtualUnlock¡ª¡ª¶ÔÄÚ´æÒ³½øÐÐËø¶¨ºÍ½âËø¡£
¡ñ¡¡¡¡¡¡VirtualQuery»òVirtualQueryEx¡ª¡ª²éѯÄÚ´æÒ³µÄ״̬¡£
¡ñ¡¡¡¡¡¡VirtualProtect»òVirtualProtectEx¡ª¡ª¸Ä±äÄÚ´æÒ³µÄ±£»¤ÊôÐÔ¡£
1¡£¡¡±£ÁôºÍÊͷŵØÖ·¿Õ¼ä
±£Áô»òÌá½»Ò»¶ÎµØÖ·¿Õ¼ä£¬Ê¹ÓÃVirtualAllocº¯Êý£¬ÊÍ·Å»ò½â³ýÌá½»µØÖ·¿Õ¼ä£¬ÔòʹÓÃVirtualFreeº¯Êý¡£ÏÈÀ´¿´Virtualallocº¯ÊýµÄʹÓ÷½·¨£º
invoke¡¡¡¡VirtualAlloc£¬lpAddress£¬dwSize£¬flAllocationType£¬flProtect¡¡
lpAddress²ÎÊýÖ¸¶¨ÐèÒª±£Áô»òÌá½»µÄµØÖ·¿Õ¼äµÄλÖ㬲ÎÊý¿ÉÒÔʹÓÃNULLÖµÒ²¿ÉÒÔÖ¸¶¨Ò»¸ö¾ßÌåµÄµØÖ·¡£NULLÖµ±íʾÓɺ¯Êý×ÔÐÐÔÚij¸ö×î·½±ãµÄλÖñ£ÁôµØÖ··¶Î§£¬·ÇNULLÖµÖ¸¶¨ÁËÒ»¸ö׼ȷµÄ³õʼµØÖ·¡£Èç¹ûº¯Êý·µ»ØNULL£¬±íʾִÐÐʧ°Ü£¬·ñÔò·µ»ØÒ»¸öÖ¸Õ룬ָÏò±»±£ÁôµØÖ··¶Î§µÄ¿ªÊ¼Î»Öá£
dwSize²ÎÊý±íʾº¯ÊýÓ¦¸Ã·ÖÅäµÄµØÖ··¶Î§´óС£¬Ëü¿ÉÒÔÊÇ0¡¡B¡«2¡¡GBµÄÈÎÒâÖµ£¬µ«ÏµÍ³»á×Ô¶¯°ÑËü½øλµ½Ò»¸öÒ³ÃæµÄÕûÊý±¶´óС¡£ÁíÍ⣬ËäÈ»²ÎÊýµÄ×î´óÖµ¿ÉÒÔÖ¸¶¨Îª2¡¡GB£¬µ«Êµ¼ÊÉÏÄܹ»±»±£ÁôµÄ×î´óÖµÊǸýø³ÌÖÐ×î´óµÄÁ¬Ðø×ÔÓɵØÖ·¿Õ¼ä¡£
flAllocationType²ÎÊýÓÃÀ´¾ö¶¨ÈçºÎ·ÖÅäµØÖ·£¬Ëü¿ÉÒÔÊÇÒÔÏÂÈ¡ÖµµÄ×éºÏ£º
¡ñ¡¡¡¡¡¡MEM_MIT¡ª¡ªÎªÖ¸¶¨µØÖ·¿Õ¼äÌá½»ÎïÀíÄÚ´æ¡£
¡ñ¡¡¡¡¡¡MEM_RESERVE¡ª¡ª±£ÁôÖ¸¶¨µØÖ·¿Õ¼ä£¬²»·ÖÅäÎïÀíÄÚ´æ¡£
¡ñ¡¡¡¡¡¡MEM_TOP_DOWN¡ª¡ª¾¡¿ÉÄÜʹÓø߶˵ĵØÖ·¿Õ¼ä¡£
flProtect²ÎÊýÓÃÀ´Ö¸¶¨±£»¤µÄÀàÐÍ£¬Ëü¿ÉÒÔÊÇÒÔÏÂÈ¡ÖµÖ®Ò»£º
¡ñ¡¡¡¡¡¡PAGE_READONLY¡ª¡ªÎªÒÑÌá½»ÎïÀíÄÚ´æµÄµØÖ·¿Õ¼äÉ趨ֻ¶ÁÊôÐÔ¡£
¡ñ¡¡¡¡¡¡PAGE_READWRITE¡ª¡ªÎªÒÑÌá½»ÎïÀíÄÚ´æµÄµØÖ·¿Õ¼äÉ趨¿É¶ÁдÊôÐÔ¡£
¡ñ¡¡¡¡¡¡PAGE_EXECUTE¡ª¡ªÎªÒÑÌá½»ÎïÀíÄÚ´æµÄµØÖ·¿Õ¼äÉ趨¿ÉÖ´ÐÐÊôÐÔ¡£
¡ñ¡¡¡¡¡¡PAGE_EXECUTE_READ¡ª¡ªÎªÒÑÌá½»ÎïÀíÄÚ´æµÄµØÖ·¿Õ¼äÉ趨¿É¶ÁºÍ¿ÉÖ´ÐÐÊôÐÔ¡£
¡ñ¡¡¡¡¡¡PAGE_EXECUTE_READWRITE¡ª¡ªÎªÒÑÌá½»ÎïÀíÄÚ´æµÄµØÖ·¿Õ¼äÉ趨¿É¶Á¡¢¿ÉдºÍ¿ÉÖ´ÐÐÊôÐÔ¡£
¡ñ¡¡¡¡¡¡PAGE_NOACCESS¡ª¡ª½«±£ÁôµÄµØÖ·¿Õ¼äÉ趨Ϊ²»¿É´æȡģʽ¡£
VirtualFreeº¯ÊýµÄʹÓÃÓï·¨ÊÇ£º
¡¡
À´Ô´£ºµç×Ó¹¤Òµ³ö°æÉç¡¡×÷ÕߣºÂÞÔƱò¡¡ÉÏÒ»Ò³¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡»ØÊéÄ¿¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÏÂÒ»Ò³¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
ÉÏÒ»Ò³¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡»ØÊéÄ¿¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÏÂÒ»Ò³¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡
µÚ10Õ¡¡ÄÚ´æ¹ÜÀíºÍÎļþ²Ù×÷
10¡£1¡¡ÄÚ¡¡´æ¡¡¹Ü¡¡Àí£¨7£©
¡¡¡¡¡¡¡¡
invoke¡¡¡¡VirtualFree£¬lpAddress£¬dwSize£¬dwFreeType
lpAddressºÍdwSize²ÎÊýÖ¸¶¨µØÖ·ºÍµØÖ·¿Õ¼äµÄ´óС£¬dwFreeTypeÖ¸¶¨ÊͷŵØÖ·¿Õ¼äµÄ·½Ê½£¬Ëü¿ÉÒÔÊÇÒÔϵÄÊýÖµ£º
¡ñ¡¡¡¡¡¡MEM_DEMIT¡ª¡ªÎªÒ»¸öÒѾÌá½»ÎïÀíÄÚ´æµÄµØÖ·¿Õ¼ä½â³ýÌá½»¡£
¡ñ¡¡¡¡¡¡MEM_RELEASE¡ª¡ªÊͷű£ÁôµÄµØÖ·¿Õ¼ä¡£
ÏÖÔÚÀ´¿´ÈçºÎʹÓÃËüÃÇÀ´±£ÁôµØÖ·¿Õ¼äºÍÊͷű£ÁôµÄµØÖ·¿Õ¼ä¡£Ê¹ÓÃVirtualAllocº¯Êý±£ÁôÒ»¸öµØÖ·¿Õ¼äµÄ·ÖÅ䷽ʽʹÓÃMEM_RESERVE£¬ÓÉÓÚ±»±£ÁôµÄµØÖ·¿Õ¼ä»¹Ã»ÓÐÌá½»¸øÎïÀíÄڴ棬ÊÇÎÞ·¨·ÃÎʵģ¬ËùÒÔ±£»¤ÊôÐÔ±ØÐëʹÓÃPAGE_NOACCESS±êÖ¾£¬¾ßÌåµÄÓï¾äÊÇ£º
¡¡¡¡¡¡¡¡invoke¡¡¡¡VirtualAlloc£¬NULL£¬10485760£¬MEM_RESERVE£¬PAGE_NOACCESS
¡¡¡¡¡¡¡¡¡£if¡¡¡¡¡¡¡¡¡¡eax
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mov¡¡lpAddress£¬eax
¡¡¡¡¡¡¡¡¡£endif
ÕâÒ»¶Î´úÂëµ¼ÖÂϵͳ±£ÁôÒ»¸ö10¡¡MB´óСµÄµØÖ·¿Õ¼ä¡£µ±ÔÚÒ»¸ö½ø³ÌÖб£ÁôµØַʱ£¬Ã»ÓÐÎïÀíÄÚ´æÒ³±»Ìá½»£¬Ò²Ã»ÓÐÔÚÒ³ÎļþÖÐΪËü±£Áô¿Õ¼ä£¬¶øÖ»ÊÇ×èÖ¹ÁËÆäËûÄÚ´æ·ÖÅ亯Êý¶Ô¸Ã¶ÎµØÖ·µÄÇëÇó¶øÒÑ£¬±£ÁôÒ»¸öµØÖ··¶Î§²¢²»±£Ö¤½«À´»áÓпÉÓõÄÎïÀíÄÚ´æÀ´Ìá½»¸øÕâЩµØÖ·¡£
±£ÁôµØÖ·µÄ²Ù×÷ÊǺܿìµÄ£¬±£ÁôÒ»¸öСµÄµØÖ··¶Î§ºÍ±£ÁôÒ»¸ö´ó·¶Î