����� 15 ������������� ����������� ������ � �����������

� Windows ��� ��������� ������ � �������

� ���� ����� �� ������� ������ ����� � ����������� ������ ��������� ��� ������ (������������ � ������ ����� � ����) ��������������� �������������� � ������ 17 � 18

�������, ���������� � ����������� �������, ��������� �������� ��������� ���� ������ ��������� �����������, ���������� ��� ���������� ������ (�� ������� ��� �����) � ����������� ����� ���������� �������� ������

�������������� ������� � �������� ������������

��� ����� ������������� ������� VirtualAlloc

PVOID VirtualAlloc( PVOID pvAddress, SIZE_T dwSize, DWORD fdwAllocationType, DWORD fdwProtecf);

� ������ ����������, pvAddress, ���������� ����� ������, �����������, ��� ���� �� ������� ������ �� ������������� �������� ������������ ������ � ���� ��������� �������� NULL, ��� ����� ������� ������� VirtualAlloc, ��� �������, ������� ���� ��������� ��������, ������ ��������������� ������ ���, ���, �� �� ������, ����� ����� ������� ��� ������� ��������, ��� ������� ������ ������������� �������, ������� � ������ ������� ���, Hao�opo�, � ������� ������ � ������� ����� MEM_ TOP_DOWN (� ��� ���� �������) �� ������ ������� ���� ������ �����

��� ����������� ������������� ����������� ������ ����������� ������ ����� ��������� ������� � ����� ���������� ����� ���������, ��� ��� �������� ������ ������������ ������� ������ �������� ���������� �� ������� ���� ������, ���� ���� ���� ���� � ���������� ��� ����� �� ��������� ������ ������� ������� ��� �������� �������� �������������, � ��� ���������� ����������� ��������� ����� ������� ������� ����������� ������� ����� �������������� �������

��������, ����� �������� ������, ������� � �������� 50 �� � �������� ��� ��������� ��������. ����� �������� pvAdress ������ ���� ����� 52 428 800 (50 * 1024 * 1024). ���� �� ����� ������ ����� ���������� ������ ���������� �������, ����� �� ������������� ��� � ������ ��������������� �����. ���� �� �� ����� ������ ��� ������� ������������ ������������ ��� ������ ���, ������� �� ������������ ��� ���, � ������� VirtualAlloc ������ NULL �����, ������������ pvAdress, ������ �� ���������� � ������� ������� ����������������� ������ ������ ��������, ��� ��� ����� VirtualAlloc �������� ������� � ������ NULL.

��� � ��� ������� � ����� 13, ������� ������ ������������� � ������ �������� ����� ��������� ������ (64 �� ��� ������������ ���������� Windows). �������, ���� �� ����������� ��������������� ������ �� ������ 19668992 (300 x 65 536 + 8192), ������� �������� ���� ����� �� ���������� �������� �����, �������� 64 ��, � �� ����� ���� ������������� ������ �� ������ 19 660 800 (300 x 65 536).

���� VirtualAlloc � ��������� ������������� ������, ��� ���������� ������� �� ��� ������������������ �������. ���� �������� pvAddress �������� ���������� �� ���, ������� ���������� ���� �����, ����������� ��� ������������� �� ������� ��������, ������� �4 ��.

������ �������� ������� VirtualAlloc dwSize � ��������� ������ ���������� ���� ������� � ������. ��������� ������� ����������� ������� ������ ��������, �������� ������� ��������, ������������ ������ �����������, �� ������� ���� �����������, ������, 62 �� ���� ������ �������� 64 �� (���� ������ �������� �� �������� 4, 8 ��� l6 ��).

������ �������� ,fdwAllocationType, �������� �������, ��� ������ �� ������ ��� ����: ��������������� ������ ��� �������� ���������� ������. (����� ���������� ��� ����������, ��������� VirtualAlloc ��������� �� ������ ������������� �������, �� � ���������� �� ���������� ������.) �������, ����� ��������������� ������ ��������� ������������, � ���� ��������� ����� �������� ������������� MEM_RE SERVE.

���� �� ������ ��������������� ������ � �� ����������� ����������� ��� � ��� ������ �����, ���������� �������� ��� � ��������� ����� ������� � ��������� ��� �������� - �������. ����� ������ �� �������� ���-������ � �������� ��������� ��� ��������� ��������, ��� �������� �� ��������� ������ ��������� ������������ ����� ������������. ����� ��������������� ������ �� ����� ������� �������, ��� ���� �� ������� VirtualAlloc � ��������� pvAddress ��������� NULL, � � ���������� fdwAlloc- cationType � ���� MEM_RESERVE, ���������������� � ������ MEM_TOP_DOWN.

NOTE:
� Windows 98 ���� MEM_TOP_DOWN ������������.

��������� �������� fdwProtect, ��������� ������� ������, ������������� ���� ��� ��������, ��� ������� ������, ��������� � ��������, �� ������ �� ���������� ������, ������������ �� ���� ������ �� ���� ��� �� �������� ���������� ������, �� � ����� �� ������� ������ � ���� �� ��� � ����� ������� ��������� �� ���� �� �� ������� � ���� ��������� �������� � ��������� ������� ��� ������� ������.

���������� ������, ������������ ��� ��� ������� ������, ������� ����� ���� ����� �������������� � �������, ������������ �������. ������, ���� �� ����������� �������� ������� ���������� ������ � ��������� ������ PAGEREADWR!TE (���� ������� ����� ����������������), �� � ������������� ��� ������� � ��� �� ������ ���, ������� �������� �����������, ����� ������� ������ ������� ��������� � �� ������� ������ ������������ ������.

�� ������ ������������ ����� �� ��������� ��������� ������: PAGE_NOACCESS, PAGE_READWRITE, PAGE_READONLY, PAGE_EXECUTE, PAGE_EXECUTE_READ ��� PAGE_ EXECUTE_READWRITE. �� ��������� �������� PAGE_WRITECOPY ��� PAGE_EXECUTE_ WRITECOPY ������: ����� ������� VirtualAtloc �� ������������� ������ � ������ NULL ����� ����, ��� �������������� ������� ����� PAGE_GUARD, PAGE_WRITECOMBINE ��� PAGE_NOCACHE ��������� ���� ������ � ��� ������������� ������ �������� ���� ������.

NOTE:
Windows 98 ���������������� �������� ������ PAGE_NOACCESS, PAGE_READONLY � PAGE_READWRITE ������� �������������� ������� � ��������� PAGE_EXECUTE ��� PAGE_EXECUTE_READ ���� ������ � ��������� PAGE_READONLY. � ������ PAGE_EXECUTE_READWRITE, �� �������� ������ � ��������� PAGE_READWRITE.

�������� ������ ������������������ �������

�������������� ������, �� ������, ������ ��� ���������� �� ������������ � ��� �������, �������� ��� ���������� ������ ������� �������� ������� ���������� ������ �� ����������� ����� �� ������� ����� ��� ���� ���, ����������, ��������� ������������ ������� ���������� ������ ������� � �������� ������� �����������

��� �������� ���������� ������ �������� VirtualAlloc ��� ���, ������ � ������� pe fdwAllocationtype �� MEM_RESERVE, a MEM_COMMIT. ������ ��������� ��� �� �� ����� ������, ��� � ��� �������������� �������, ���� ����� ������ � ������

����� �������� ������� VirtuaMlloc, �� ������ ������ � ������� ���������� �� ���� ������� ��������. ��� ����� � �������� pvAddress �������� ����������� �����, � � �������� dwSize � ������ ���������� ������ � ������ ���������� ���������� ������ ����� ����� ������� �������������.

���������, ��� ��� �������� �� ��������. ��������, ��������� �������� �� ��� ������� x86 � ����������� ������ �������� 512 ��, ������� � ������ 5 242 880. �� ��� �� ��������� ���������� ������ ����� �������� 6 ��, ���������� �� ������ ������������������ ������� �� 2 ��. ����� �������� VirtualAlloc ������ MEM_COM MIT ���

VirtualAlloc((PVOID) (5242880 + (2 * 1024)), 6 * 1024, MEM_COMMIT, PAGE_READWRITE);

� ���� ������ ������� �������� 8 �� ���������� ������ � ��������� ������� �� 5 242 880 �� 5 251 071 (�. e. 5 242 880 + 8 �� - 1 ����), � ��� ���������� �������� ������� ������� ������ PAGE_READWRITE. �������� �������� ����������� ����� ��� ������, ������� ����� ��������� ����������� �������� ������. ����������� ��, � ������� ����� ���� �������� � ������� ���������� ������ (������, ���� - � ��������� PAGE_READWRITE, ������ � � ��������� PAGE_READONLY).

�������������� ������� � ������������� ��������� ���������� ������

������ ����� ������������ ��������������� ������ � �������� ��� ���������� ������. � ����� ������ VirtualAlloc ����� ������� ��������� �������

PVOID pvMem = VirtualAlloc(NULL, 99 * 1024, MEM_PESERVE | MFM_COMMIT, PAGE_READWRITE);1}

���� ����� �������� ������ �� ��������� ������� �������� 99 �� � �������� ��� 99 �� ���������� ������. ����������� ���� ������, ������� ������� ������������� �������� ������������ ������ ��������, ������� ����� ����������� ����������� �������� ������� �������� �� ����� 100 �� (�� ������� � 4-������������ ���� ������) ��� 104 �� (�� ������� � 8-������������� ����������).

������� ������������� �������� ������������ ������, ��� � pvAddress ������ NULL ���� �� �� �������� ���������� ����� ������, ������� ��������� �� ������ ��� � �������! �� �� ������� ������������� �� ��� �������� ������������ ��� ���� �� �������������, ������� VirtualAlloc ������� �� NULL.

���� ������� ������� ��������������� ���������� ������, ��� �������� ��� �� �������� ������. � ������, � ���������� ������ �������� ���� ������� ������ � � ������ ������ PAGE_READWRITE.

�������, ������� VirtuaLAlloc ���������� ����������� ����� ����� �������, �� ����� ����� ������������ � ���������� pvMem ���� �� ������� �� ������ � ����� ��� ������������ ���������� ������� ��� �� ������ �������� �� ���������� ������, VirtualAlloc ������ NULL

�������, ��� �������������� ������� � ������������� ��������� ��� ������ ����� ������� � �������� pvAddress ���������� ����� ��� ��������� ������� ���� ����� ��������� ����� � ������� ����� ��������� ������������ ��������. ��������� ��������� ���- � �������� pvAddress ������� NULL, a �������� ��������� fdwAlloca tionType ����������� � ������ MEM_TOP_DOWN.

� ����� ������ ������� �������� ���������� ������

��������, �� �������������� ��������� � ����������� �������, ������� �������� ������ 200 ����� ��� 256 �������� ��� ������ ������ ���������� ���� ��������� CELLDATA, ����������� �� (������) ����������. ���������� ������ ������ � ���� ������ �������� �����, �������� ��, � ����� � �������� � ��������� ����� ���� ������:

CELLDATA CellData[200][256];

�� ���� ������ ��������� CELLDATA ����� ���� �� 128 ������, ������� ������� �� 6 553 600 (200 * 256 * 128) ������ ���������� ������. �� ��������� ��? ��� ����� ��� ����������� ������������� ��������� ������� ����� ��������� ����� �������, ������� ����� ������ ������������

������� ����������� ������� ��������� �� ������ ������ ������� ���������� ����������� ������, ���������, ��������, ��������� ������ � ���� ������ ������� �� CELLDATA ��������� ������ ��� �����, ���������� �����-�� ������ � ��������� ������� ����� ����� � ������� �������� �����������������, �� ��������� ��������� ��� ������ ������ �� ��� ����������� ��������� ������ � ����������� �����. ��� ��, ��������, �������� ���������� ������ �� ����������� ������ 5 � ������� 10, �������� ������ �� ���� ������� ��������� ������� � ����� ����� ��������� ���� ��� �������� ���������, ��� �����, ���������� �� ���������� �������.

� �������, ����������� ������ ��������� ����� ���������� ����� �������� ����������� ���������� ������� � ����������� ��������� �������. ��� ����� ����� ���������� �������� � ������� �������� ������� � �������, ������������ "���������" �������, � ��������� ������������� ������, ���������� � ����� ��������� �������.

��� ��� ���� ������� � ����� ���������.

  1. ��������������� ���������� ������� ������, ����� ��� ������������� � ���� ��� ����������� ���� ������ �������� CELLDATA. ��� �������������� ������� ���������� ������ �� �����
  2. ����� ������������ ������ ������ � ������, ��������� ����� � ����������� ������ �������, �� �������������� ���� �������� ��������������� c�py� ���� CELLDATA. �����������, ���������� ������ �� ���� ������ ���� �� ��� ���������, � ������� ����� ��������� � ������ �� ������� ������ ������� ��������� �������.
  3. �������� �� ������, ����������� � �. 2, ���������� ������, ����������� ��� ���������� ����� ��������� CELLDATA. (��� ��� ������� ��������� �������� ������ ��������� ������ ������������������ �������, � ��� ����� �������� �� � ������������, � �� ������������ �� ���������� ������ �������.)
  4. ���������������� �������� ����� ��������� CELLDATA.

������, ������������ ���������� ������ �� ������ ������� ���������������� �� �������, ��������� ����� ���������� � ����, �� ������ ��� ���� ��������� ��� ���� ����� �������, �����, ���������� �� ������������� ����������� ������, �� ��� �����������, ��������� ��������� ���������� ���������� ������ ������ �� ���� ����� ������ � ������ ����������� �������. � ����� ����, ��� ������� ����� ����� � ����������� ������� ������ �����, �� � ������� ����� ������������������ ������� ���������� ������ �� ��������.

�� ��� ������������� ����������� ������ ��� �� ��������� ���� �������� ��� ������� ����������, ����� ������ ������������������ ������� ���� ���������� ���������� ������. ���� ������������ ����� ���� ����������� ������, ��� ������� ����� � ������, � �������� ���������� ������ ������������� ��� � ��� ���� ��� ���� � ������ ������� ���������� ������

������ �������� � � ����������� ������� ������. ������� �������� ���������� ������ ��� ������������ ��������� CELLDATA (��� � �. 2 ����������� ������) ��� ����� � �������� ������ �������� ������ �� � ����, ��� �� �������, ���� ���� ��� ���������: ������� ���������� ������ ��� ���� ��������� CELLDATA, �� ������� ����� �������� �� � ��������� ���������� CELLDATA. ����� ������������ ������ ��������� ��������� ������ (� ��� ������ � ������), ���, �����, � �� �������� �� �������� �������������� ���������� ������.

����������, ���� �� ���������� ���������� ������ ����� �������, ����� ������ �� ���������

������� ���������� ������ � ������������ �������

��� �������� ���������� ������, ������������ �� ������, ��� ������������ ����� ������� ��������� ������������ ������������ ������� VirtualFree.

BOOL VirtualFree( LPVOID pvAddress, SIZE_T dwSize, DWORD fdwFreeType);

���������� ���������� ������ ������ ���� ������� � ��� ������������ ���� �������������� �������. ����� �������� ������ �� ����� ���������� ������, ���� ������ �������, ����������������� ������ � ��� ��������� � ��� ���������� �� ���� ����� ���������� ������������ ������� VtrtualFree,

� ���� ������ � �������� pvAddress ���� ��������� ������� ����� �������, �. e. ��������, ������������ �������� VirtualAlloc ����� �������������� ������� ����� ��. ������� �������� ������ �������, �������������� �� ���������� ������, ����� �� � ��������� dwSize ����� �������� 0. ���������� �� ���� ������� ��� �������, ����� ����� VirtualFree �� ���� ����������. � ������� ��������� (fdwFreeType) ���� ����� ������������� MEM_RELEASE; ��� �������� � �������� ������� ���� ������� ��� ������, ������������ �� ������, � � ������������ ������ �������. ������� ��� ������, �� ������ ���������� � ����������������� ��� ���� �������� �������� ����. ������ �������� ������ ��������, ��������, 1 28 ��, � ����� ���������� ���� �� 64 ��: ���� ����������� ��� 128 ��.

���� ��� �����, �� ���������� ������, ������� � ������� ����� ���������� �� ����, ���������� �������, ��� ����� ���� ������� ������� VirtualFree. ��� ���� �� �������� pvAddress ������ ��������� �����, ����������� �� ������ ������������ �������� ����� ����, � ��������� dwSize ������� ���������� ������������� ������, � � ��������� fdwFreeType � ������������� MEM_DECOMMIT.

��� � ��������, ������� ������ �������������� � ������ ����������� �������. ����� ������, ������� ������, ������������ �� �������� ��������, �������� � ��� ����� ���� ��������. ����������, �� �� ����� ����������, ���� ��������� ��������

���������� pvAddress dwSize ������� �� �������� ��������. ��� ��� ������� ��� ��������� ��� ��������, ���������� � �������� �� pvAddress �� pvAddress + dwSize. ���� �� dwSize ����� 0, a pvAddress ��������� �� ������� ����� ����������� �� �����, VirtualFree ������ ������� ���� �������� ���������� �������. ����� �������� ���������� ������ ������������� �������� �������� ������������� ��������, � ������� ��������� � �������, ��� �� ��������� � ���������� �������, �������� � ��������� �������.

� ����� ������ ���������� ������ ���������� �������

�� �������� ������� ������, ���������� ��� ������� ������, ~ ����� ���������. �������� � ������� � ����������� ��������. ���� ��������� �������� �� ������ � ����������� x86, ������ ������ �������� ������ � 4 ��, � e. �� ����� �������� ��������� 32 (4096 / 128) ��������� CELLDATA. ���� ������������ ������� ������� ��� �������� CellData[0][l], �� ������ ������� �������� ������, �� ������ ��� �� �����, ��� ������ � ��������� �� CellData[0][0] �� CellData[0][31] ���� �� �������� ����. ��� �� ���� ������? �������� �������� ����������� ���������.

��� ���������� ��������� �� ������ ������������ �������� ������� ��� �� ������, ������� ������ ������������� ��� ���������. ��� ������ ������� �� (� ������������, � ���, ������� ����� ���� �������) ������� ������� ����������, �������� �� ��� ��� ������; ���� ��, �� ��������� �������� fInUse. ���� �� ����� 0, ��������� �� ������������; TRUE � ��������� ������. ����� ��� ��� ���������, ������������� � �������� �������� ��������, ������� ����� ������ �������� VirtualFree, ����� ���������� ������, � ����, ����� ��, �� ���� �������� ��� ������������ ��������.

������� ����� ������ ����� �������� ����� ����� ����, ��� ������������� � ����� �� �������� �������, �� ������ ��� �� �����, ��������� ������� ��� ��� ��� ������������� ��� ��������� � � ������������, � ��, ������� ����� ���� �������. ����������� ���� � ����������� ��� ������� ��� ����� � ����� ������ ������� ���������� ��� �������� �� �������� ����� � ������, ������������ �������� ��������� � ����� �������� ��������� ����� ��� �������� ��� �� ����� �������� �������� ������-�������, ��� ����� ������� � ������� ����� ������� ����� ������

����� � ����������� ������ ��� ������� ������, ���� ���� ��������� ������ ��� (������ ����� �������� ������), ������� ��������� ��������� �����

{375}

���������-������ VMAIloc

��� ���������, "15 VMAllocexe" (�� ������� �� ��� 15-1),������������� ������� ��� ��������� ����������� ������ ��� ���������� �������� �������� ����� ����� ���� ���� � �������� ���� ��������� ��������� � �������� 15-VMAlloc �� ������� �����, ����������� � ����� ����� ������� VMAlloc �� ������ �������� ���������� ����, �������� ����

rihter15-1.jpg

���������� ��� ������� �� ������������� �������� �������, � ��� �������� ��� ���������, ��������������� ��� ���, ��������, ��� � �������� �� ����� ������ ���� �������� ������ Reserve Region (50,2KB Structures), ��������� VMAlloc ������� Vtrtual Alloc ��� �������������� �������, ��� ����� ��������� �� ����� ������ ����� ����� ������ ��������� � ��������� ������ � ���������� ����

������ � ���� ����� ������ ������ � �������� ������ Use ��� ���� �� ������, ��� ������ ������������� ��������� ������� �������, ���������� ���������� ������ ����� ����� ������ ����� ���������������� � ��� �������� ��������� �������, �� ���������������� ��� ���� ������ ����� ��, �������������� ������, ����� ������ �� ������ Use, ����� �������� �������� 7 � 46 ��� �������, ���� (��� ���������� ��������� �� ���������� � �������� ������� �� 4 ��) ����� ��������� ���

rihter15-2.jpg

����� ������� �������, ���������� ��� �������, ����� ���������� ������� ������ Clear �� ��� �� �������� � �������� ���������� ������, ���������� ��� ������� ������� ���� � ���, ��� ������ �������� �������� ��������� �������� � ������������ ����� ��������� �� ������ �� ����� ������������ ������ ���� �� ������ ���� ����������, �� ������� �� � ������, ������������ � ��������� �����

����� � ��������� ����� ������ Clear ����� �� ����������� �� ���������� ������ �������, ����� ������ ����� ������������ �������� �� ��������.

������ ������������ ��������� �������� � ����, ��� �� ������� fInUse ������� �������� � FALSE ��� ����� ��� ����, ����� ������� ����� ������ ����� ������� �� ������������ ������ ���������� ������ ������ Garbage Collect, ���� �� ��� �� ����������, ���������� ��������� VMAlloc ��������� ������� ����� ������ ��� ��������� ��������� � �� ���� �������� ��� ������� � ��������� �����

����� ����������, ��� �������� ������� ����� ������, �������� ������� ����� �� � �������� 46. ��������, ��� ����� ������ ���� �� ���������� ������ �������� ������ Garbage Collect. ��������� ��������� ��������, ���������� 46-� �������, � ����� ������ ����� �� ���������, ��� �������� ���� ��������, ��� ������� Garbage Collect ����� ����� ������������ � ����� ������ ����������� � ���������� �� ���, ����� ��� �������� � ��������� �������� ������ ������ �������; ��� ���� ����� ���� �� ����������� ������ ��������� ��������� �� �������� ������. ��������� ��� ���������� ����������� � ���, ��� ������ ������� ��������� ������ ���� ��� ������ ���� BOOL, ������� ���������, ������������� �� ������ ���������

rihter15-3.jpg

�, �������, ���� ��� � �� ����� �� ������, �������� ���� �������� � �������� ���� ���������� ������ � ������������ ������������������ �������.

�� ���� � ���� ��������� ��� ���� �����������, � ������� � ���� �� �������� ��������� ���������� ���������� ��������� ������ � �������� ������������ �� ����� � ���� �������

��� ��� �������� VMAlloc ������������ ����� ������� VirtualQuery, ���������� ��� � ���������� �����.

VMAIIoc

 

��������� ��������� ������

���� ��� � �� �������, �� �������� ������, ����������� �������� ��� ��������� ���������� ���������� ������, ����� ��������. ��������, �� ����������� ��� ��� ���������� ��������� �������, ���� (�������) �������� �������� � ����������� ������ �������. ��� ������� ����� �������� �������, ������� ������������ �� ��������� ������ � �������� �� �������� ������ ���������� ������ ��� ������ �� PAGE_READWRITE, � ��� ���������� � ������� �� PAGE_NOACCESS.

������ ���, �� �������� ������ � ��������� ������ �� ��������� �������, ���� ��� � ��������� ��������, ���� ����� �� ���� ���� � ����� ��������� ��-�� ���� ��� ������������ ��������� ��������� � ������ � ��������� ������, ��������� ��������� �������. ������������ ������ ������ ����� ������� � �������� ����� ��������� ����� �������������� ������ � ����� ���������.

�������� ������ �������� ������ ����� �������� ������� VirtualProtect.

BOOL VirtualProtect( PVOID pvAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD pflOldProtect);

����� pvAddress ��������� �� ������� ����� ������ (������� ������ ���������� � ���������������� ������� ������ ��������), dwSize ���������� ����� ������, ��� ������� �� ��������� ������� ������, � flNewProtect �������� ���� �� ���������� ����� PAGE_*, ����� PAGE_WRITECOPY � PAGE_EXECUTE_WRITECOPY.

��������� ��������, pftOldPrntect, �������� ����� ���������� ���� DWORD, � ������� VirtualProtect ������� ������ �������� �������� ������ ��� ������ ������� ������. � ���� ��������� (���� ���� ��� �� ���������� ����� ����������) ����� �������� ���������� �����, ����� ������� �������� � ��������� �������

�����������, �������� ������ ������� � ������ ���������� ������ � �� ����� ������������� ��������� ������. �������, ���� �� ���������� � ���������������� ���� ���������� ������� VirtualProtect, ��������, ���:

VirtualProtect(pvRgnBase + (3 * 1024), 2 * 1024, PAGE_NOACCESS, &flOldProtect);

�� ������� ������ PAGE_NOACCESS ����� �������� ���� ��������� ������,

WINDOWS 98
Windows 98 ������������ ���� �������� ������ PAGE_NOACCESS, PAGE_READ ONLY � PAGE_READWRITE. ������� �������� ������� ������ �������� �� PAGEEXECUTE ��� PAGE_EXECUTE_READ �������� � ����, ��� ��� ������� ������ ������� ������� PAGE_KEADONLY. � ������ ������� PAGE_EXECUTE_ READWRITE. �� �������� �������� � ��������� PAGE_READWRITE.

������� VirtualProtect ������ ������������ ��� ��������� ��������� ������ ���� ���, �������� ������� ���������� ������ ����������������� �������. � ����� ��� ���� VirtualProtect ���� �������� ��� ������� ������� ��������.

����� ����������� ���������� ������

WINDOWS 98
Windows 98 �� ������������ ����� ���������� ������.

����� �� ������������� ���������� ������� ���������� ������, ������� ������ �� ��� ����� ������ ������� ��� ��������� � ����������� ������. ������, ����� ��� ����������, ������� ��������� �������� ������� �� �������� � ����������� ������ ������� �� ���-������, DLL �/��� ����������� �����. ����� ����� ������ ���������� ������� ������������� ����������� ������ � ��������� ������������� ��� �������� � ���������� ����.

Windows 2000 ��������� ���������� ��������� ���� ������������������ �� ���� ������ ���������� ������. �� ��������� �������, ��� ������ �� ����� ��� ������� ��� ��������� ������ �� ����������. ���� ������� � �������� ������ ��������� �������� � ����������� ������ �������� ���������� ��������, �� ������ ������� �������� �� � ���������� ���� ��� �������� ���������������� ����� ������� � ������������ ����������� �� ������������������. ������� � ����������� ������� ��� ����������, ����� ������� ��� ����� ������ ������� ���������������� ���� ���� � ���������� �����.

������ ��������� ��������� �������� ����� ������ �� ����� ����� �����, � ����� �� ��� �� ��������� �� ���������� ��� �������� �������������� ��������� ����� ��������� ������� �� ���������� ������������ �������� � ���������� ����. � �����, ���� ���� �� ���� ������� ����������� ��� ������ �����, ������� �� ��� ����� ��������� �� � ���������� �����, ��o, �����������, ������� �������� ������ ���������. ����� ����� �� �������� (��� �������) ������ ���������� ������� �� �������� ������ (resetting of physical storage) � ������������ ������� ������� VirtualAlloc � ��������� �� � ������� ��������� ����� MEM_RESET

���� ��������, �� ������� �� ���������� ��� ������ VirtualAlloc, ��������� � ���� ������ �����, ������� �� ������. ����� � ��������� ��� ��������� ��������� � ������, ��� ������� ����� ��������, ������������������ ������ ���� �� �� ���� ������� ��������, ����������� � ����������� ������, ������� �������� �� ��� �� ������������, � ��� �� ������������ � ���������� ����. ��, ���� �� ���������� �� ����������, ������ ����� �������� ������ ��� ������ ���� ������� �� ����������� ��� �������� ����������� ������, �� ���������� ��������� �������. � ���� ��� ��� ������� ����� ������� �� � ���� ������������, � ����� ��������� � ���� ���� ���� �������� � ����, ��� ������� ����������� ��������� ����� ��������, �������� ��� ������. � ��������� ���� ������� ��� �� ����������, ����� �������, ��� ����� ������ �������� �������� ������ �����

��� ������ ���������� ������ ���� ��������� � ��������� ������ ��������. �� ������, ����� �� ��������� VtrtualAlloc, ������� ����� ������ ����������� �� ��� ������� �������� ��������, �������� ������� �������, � ���������� ������ � �� ���������� �������� ��������, �������� ��� �� ��������. ����� �������� ����� ����� �������� ������ � ���������� ������ ��� �� ����� ������ ��� ������ ���� ������ ������; ������� VirtualAlloc ��� �������� �� ����� MEMRESET ��������� ��� �������� ����� ��������.��������, � ����� ��������� ���� ��������� �������� ���:

PINT pnData = (PINT) VirtualAlloc(NULL, 1024, MEM_FlESERVE | MEM_COMMIT, PAGE_READWRITE);

pn[0] = 100;
pn[1] = 200;

VirtualAlloc((PVOID) pnData, sizeof(int), MEM_RESFT, PAGE_READWRITE);

���� ��� �������� ���� �������� ������, � ����� ��������, ��� ������ ������ ����� (sizeof(int)) ������ �� ����� � �� ����� ��������. ������, ��� � ��� ����� ������ ��������� � �������, ��a �������� ����������� ������ ��� ������� ������, ������ ������� ������ ������� ������� � ������ ������ ����� ���������� ������ �� (VirtualAlloc ������ NULL) ������? ���� � ���, ��� ��� ������ VirtualAlloc �� ��� ���� ���� MEM_RESET � ������� �����, ���������� �������, ������ ����������� �� ���������� �������� ��������, �������� ������� �������, � ���������� ������ � �� ���������� �������� ��������, �������� ��� �� �������� ��� ��������, ����� ��������� ��������� ������ ������ ������ � ���������� ������� ���������� ���������� ������ �� ���������� �������� �������� ���� 0, � ��� �������� ���� �������.

������, � ��� ������� ������� ��� ������ ������, � ���� MEM_RESET ������ ������������� (���������� ��������� OR) �� � ������ ������� �������. ������ ��� ����� ������ ����� ������������� ��������:

PVOID pvMem = VirtualAlloc(NULL, 1024, MEM_RESERVE | MEM_COMMIT | MFM_RESET, PAGE_READWRITE);

�������, ������������� ���� MEM_RESET � ������� ������� ��� ����� ������������

�, �������, ���������. ����� VirtualAlloc � ������ MEM_RESET ������� �������� ����������� �������� ������ ��������, ���� �������� �� �� ��� �� �� ����� ������ �������� ������ ��������.

���������-������ MemReset

��� ���������, �15 MemReset.exe� (��. ������� �� ���. 15-2), �������������, ��� �� ������ ���� MEM_RESET. ����� ��������� ���� � �������� ���� ��������� ��������� � �������� 15-McmReset �� �������-�����, ����������� � �����

������, ��� ������ ��� ���� ���������, � ����������� ������ � �������� ��� ���������� ������. ��������� ������ �������, ���������� � VirtualAlloc, ����� 1024 ������, ������� ������������� ��������� ��� �������� �� ������� ��������. ����� ������� lstrcpy �������� � ���� ����� ������, � ���������� �������� ����������� ����������. ���� ������� ������������ ������, ��� �� ����� ��������, ���������� ���� ������, ��� ������� ��� �������� � ���������� ���� ����� ���� ��������� ���������� ������� ��� ������, ������� ������������� �������� �������� �� ���� ������� ����� � ����������� ������

����� ������ ������ � �������� ������ ���� ��������� ���������� � ������ ������, ����������� �� ��� ��� ������. ���� ������������ �������� �������������

����� (������� ������ No), ��������� �������� �������, ��� �������� �� ������ ����, ��� ���� �������� VirtualAlloc � ������ MEM_RESET

��� ������������ ���� �����, ��� ������ ������������� ��������, ����������� ������� �������� �� ����������� ������, ��� ����:

  1. ������� ����� ������ ����������� ������ �� ���������� ������� Global MemoryStatus
  2. ��������� ��� ������ ������� VirtualAlloc. ������ �������� ����������� ����� ������, ��������� ������� �� �������� ����������� ������ �� ��� ���, ���� ������� �� ������� �����-������ ��������.
  3. ������� ���������� ������ ��� ���������� ������� ����� ������� Zero Memory. ��� ������� ������� �������� �� ����������� ������, � ��������� �������� ����������� � ���������� ����.

���� ������������ ������� �������� ������, ����� �� ��������������, � ��� ��� ��� �� ������� ������� � ��� ��������������� �������� ����� ������������ � ��� �������� ������ �� � ���������� �����. ���� �� ������������ ��������� �� ���� ��� ���, �� ��������� ����� ������, ������� �� ���������� �� � ���������� ����, � ��� �������� ���������� ���������.

����� ������ ZeroMemory � ��������� ���������� �������� ������ �� �������, ������� ���� ���� ��������. ���� ������ �� ������������, ���������� ���������, � ���� ������������ � �� �� ���������, �� �� ���. � ���� ��������� ���������� ������� �� ��������� �������, ���������� ��������� ������ ��������� ��� ������ �� ����������� ������ � ���������� ����. �� ���� �� ������ ����������� ����� �� ��� ������ ������ ������ ����������� ������, �� �� ���������, ��� �������� ���������� ��� ����� �������� �� � ������. ��� ��� ������ ���������!

MemReset

�������� Address Windowing Extensions (������ Windows 2000)

����� ���� ������, � ���������� ������� ��� ������ � ������ ������ � �������� ���������. ��� ���� ����� ��������, ������������ � �������, ��� ������ ��� ��� ���������������. ��� ���������� �������������� ��������� ���������� ������ ������� ��� ����� ������ ����� ������ � ����������� ������ � ���������� �� �� ���� ��� ����� ����. ������ ������� ���������� (����� ������, ���������� ��� ������ � ���������� ��������, ��������������� �������� � ��.) ���� ����� �� ������������ �������� ������� ������. � ���� ���� ����������� ��� ����� � 32 ��������� �������� ������������.

��� ����� ���������� Windows 2000 ���������� ����� �������� � Address Win dowing Extensions (AWE). �������� AWE, Microsoft ���������� �����, ����� ������� ��� �����:

AWE ���� ����������� ���������� �������� ���� ���� � ����� ������ �������� ��� ������, ��������� � �������� ������������ ��������. ������ ���, ���������� ����������� ������ ��������� ������������ (� ������� VirtualAlloc), � �� �������� �� �������� ����� (address window). ����� ��������� �������� �������, ������� ��������� �������� ���� � ����� �� ���������� ������ ����������� ������. ��� �������� ����������� ����������� ������ (������ �� ���� �����������).

����� ���� �������� ���� ������������� �������� ���� ���� ���� ������. ���, �������, ��������� ����������������, ��� ��� ��� ��������� � ������� ����� ��� ������� ���� �������� �������, ������� ��� �� ����������� �������� ���� �� ��� ������ ����.

��� ������, ��������������� ������������� AWE:

// ������� ����������� ��� ��������� ���� ������ �������� 1 ��

ULONG_PTR ulRAMBytes = 1024 * 1024;

PVOlD pvWindow = VirtualAlloc(NULL, ulRAMBytes, MEM_RESERVE | MEMJ>HYSICAL, PAGE_REAOWRITE);

// �������� ������ ������� �� ������ ������������ ���������

SYSTEM_INFO sinf;

GetSystemInfo(&sint);

// ���������, ������� ������� ������ ����� ��� ������ ���������� ������
ULONG_PTR ulRAMPages = (ulRAMBytes + sinf.dwPageSize - 1) / sinf.dwPageSize;

// ������� ��������������� ������ ��� ������� ������� �������
ULONG_PTR aRAMPages[ulRAHPages];

// �������� �������� ����������� ������ (� ����������� ������������
// ������ ���� ��������� ���������� ������� � ������)

AllocateUserPhysicalPages(
GetCurrentProcess(), // �������� ������ ��� ������ ��������
&ulRAMPages, // �� �����: ���������� ����������� �������
RAM, // �� ������: ���������� ���������� ������� RAM
aRAMPages); // �� ������ ������������� ������,

// ���������������� ���������� ��������
// ��������� �������� ����������� ������ ������ ����
MapUserPhysicalPages(
pvWindow, // ����� ��������� ����
ulRAMPages, // ����� ��������� � �������
aRAHPages); // ������ ������� RAM

// ���������� � ���� ��������� ����� ����������� ����� pvWindow

...

// ����������� ���� ������� ����������� ������
FreeUserPhysicalPages(
GetCurrentProcess(), // ����������� RAM, ���������� ������ ��������
&ulRAMPages, // �� �����, ���������� �������
RAM, // �� ������: ���������� ������������� ������� RAM
aRAMPages); // �� �����- ������, ����1����������� �������������

// �������� RAM

// ���������� �������� ����
VirtualFree(pvWindow, 0, MEM_RELbASE);

��� ������, ������������ AWE ��������. � ������ ���� �������� ���� �������� �� ��������� ���������� ��������, ��������� � ���� ���������� ����.

����� VirtualAlloc ����������� �������� ���� �������� 1 ��. ������ �������� ���� ������� ������. �� ������ ������� ��� ������ � ������������ � ������� ��� ��� ����������� ������, ����������� ������ ����������. ��, �������, ������ ������ ���� ��������� �������� ������ �������� ���������� (� ������������) ����� � �������� ������������ ��������. ���� MEM_RESERVE ���������, ��� � ��� ��� ���������� �������� �������, � ���� MEM_PHYSICAL � ��� � �������� ����� ���� �������� ������� ����� ������ � ���������� (�����������) �������. �������� AWE �������, ����� ��� ������, ����������� � �������� �����, ���� �������� ��� ������ � ������; ������� � ������ ������ ������� VirtualAlloc ����� �������� ������ ���� ������� ������ � PAGE_READWRITE, ����� ����, ������ ������������ �������� VirtualProtect � �������� �������� ��� ������ ����� ����� ������.

��� ��������� ����� � ���������� ������ ���� ������� ������� AllocateUser PhysicalPages:

BOOL AllocateUserPhysicalPages( HANDLE hProcess, PULONG_PTR pulRAMPages, PULONG_PTR aRAMPages);

��� �������� ���������� ������� ����������� ������, �������� � ��������, �� ������� ��������� �������� pulRAMPages, � ���������� ��� �������� �� ���������, ������������ ���������� hProcess

������������ ������� ��������� ������ �������� ����������� ������ ����� ������ �������� (page frame number) �� ���� ���� ��� ������� �������� ������ �� ������, ���������� ����������, ��� ������ ��������������� ������ (����� ������ �������� ��� ������ �������� ����������� ������) � ������, �� ������� ��������� �������� dRAMPages. ���� �� ���� ��� ������ ��� ���������� �������� �� ����������; ��� �� ������� ������������� ���������� ����� ������� � ��� ��

��� ���-���� ������ � ���. �� �� �������, ����� �������� ����������� ������ ����� �������� ��� ����������� ����, �� ��� � �� �����. ����� ��� �������� ��������� �� � �������� �����, ��� ���������� � ���� ������������ ����� ������, � ��� ��� ������� ������ ��� �����, ��� �� ������ ������������

����� fyyHKiwnAllocateUserPbystcalPages ���������� ����������, �������� � pulRAM Pages ��������� ���������� ���������� ���������� �������. ������ ��� ��������� � ���, ��� �� ��������� �������, �� ����� ��������� � ��������.

�������� ����������� ������ ���������� ������ ��������, �� �������� ���� ������� ������ �������; AWE �� ��������� ������������ �� �� �������� �������� ���� ������� ��������. ������� ����� ����� ������ ������ ��������� ����� ������ ����.

NOTE:
�������, ����������� ������ � ������ �����������, � ���������� ����� �������� ���� �� ����������������� �����. �� ��������������� ���������� AWE: ���� ��� ������� �������� ������� ����� ����������� ������, ��� ����� �������� � ����������� ��������� ������� �� ���� � ������� ������� ������������������ ���� �������. ����� ����, ��� ��������� ����������� ������� � �������� ����� ���������, ������� � ������ �������� (������ ���� ������� ������������� ���������� ������ ����� ����������� ����� ������� GlobalMemoryStatusEx)

AllocateUserPhysicalPages ������� �����, ����� ���������� ���� ������� �� ���������� ������� � ������ (�. e. � ������������ ������ ���� ����� �Lock Pages in Memory�), a ����� ������� �������� �������. �� ��������� ����� ������ ������������ ��� �� ������ �� ����������. ��� ����������� ������� ������ Local System, ������� ������ ������������ ���������� ��������. ���� �� ������ ��������� ������������� ����������, ���������� AttocateUser PhysicalPages, ������������� ������ ������������ ��� ��������������� ��� �� ��� �� ����, ��� �� ����������������� � �������.

������, ������ �������� ���� � ������� ���� ������, � �������� ���� ���� � �� ��� ������� ������� MapUserPhysicalPages:

BOOL MapUserPhysicalPages( PVOID pvAddressWindow, ULONG_PTR ulRAMPages, PULONG_PTR aRAMPages);

�� ������ ��������, pvAddressWindow, ���������� ����������� ����� ��������� ����, � ��������� ��� ���������, ulRAMPages aRAMPages, ��������, ������� ���� ��� ����������� ������ ������ ���� ������ ����� �������� ���� � ��� ��� �� ���� ����. ���� ���� ������ ������������ ����� ������, ������� �������� �������.

NOTE:
������� MapUserPhysicalPages ��������� ������� ���� ����������� ������ �� ��������� ����, ���� ������ ��������� aRAMPages ���������� NULL. ��� ������:

// ��������� ������� ���� RAM �� ��������� ����
MapUserPhysicalPayes(pvWindow, ulRAMPapes, NULL);

WINDOWS 2000
������ ���� ����������� ������ � �������� �����, �� ������ ����� ���� ������ � ���� ������, ������ �������� �� ����������� ������ ������������ �������� ������ ��������� ���� (� ���� ������� ��� pvWindow)

����� ������������� � ����� ������ �������, ���������� ��� ������� ������� FreeUserPhysicalPages:

BOOL FreeUserPhysicalPages( HANDLE hProcess, PULONG_PTR pulRAMPages, PULONG_PTR aRAMPages);

� Windows 2000 ����� �Lock Pages in Memory" �������������� ���:

  1. ��������� ������� Computer Management MMC. ��� ����� �������� ������ Start, �������� ������� Run, ������� "compmgmt.msc /�" � �������� ������ ��.
  2. ���� � ����� ������ ��� �������� Local Computer Policy, �������� �� ���� Console ������� Add/Remove Snap-in. �� ������� Standalone � ������ Snap-ins Added �� ������� ������ Computer Management (Local). ������ �������� ���� �� Add, ����� ������� ���������� ���� Add Standalone Snap-in, B ������ Avai lable Standalone Snap-ins ������� Select Group Policy � �������� ������ Add. � ���������� ���� Select Group Policy Objcct ������ �������� ������ Finish. �������, � ���������� ���� Add Standalone Snap-in �������� ������ Close, a � ���������� ���� Add/Remove Snap-in � ������ OK ����� ����� � ����� ��� ��� ������� Computer Management ������ ��������� ������� Local Computer Policy.
  3. � ����� ������ ������� ��������������� ��������� ��������� ��������: Local Computer Policy, Computer Configuration, Windows Settings, Security Settings � Local Policies. �������� User Rights Assignment.
  4. � ������ ������ �������� ������� Lock Pages in Memory.
  5. �������� �� ���� Action ������� Select Security, ����� ������� ���������� ���� Lock Pages in Memory. �������� ������ Add. � ���������� ���� Sclect Users or Groups �������� ������������� �/��� ������, ������� �� ������ ��� ������ ���������� ������� � ������. ����� �������� ��� ���������� ����, ������ � ������ �� ��� ������ ��.

����� ����� ������� � ���� ��� ��������� ����������� � �������. ���� �� ������ ��� ���� ���� ������������ ����� �Lock Pages in Memory�, ������� �� ������� � ����� ����������������� � ���.

�� ������ ��������, bProcess, �������������� �������, ��������� ������� ���������� ������, � ��������� ��� ��������� ��������, ������� ������� ����� ������ ������ ������� ���������� � ��� ��� �� ��������. ���� ������������� ���� � ������ ������ ������ � �������� �����, �� ������� ����������� �� ����� ����

�, �������, �������� �������, � ���������� �������� ����. ��� ����� � ������� VirtualFree � ������� �� ������� ����������� ����� ����, ���� ������ ������� ���� ��� � ���� MEM_RELEASE.

� ���� ������� ������� ��������� ���� �������� ���� � ������������ ���� ������. ��� ��������� ���� ��������� ���������� � ����������� ������, ������� ������� �� ����� ������������ �� ����. ������ ���������� ����� ������� ������� �� �������� ���� � �������� ��������� ������ ������. ��� ����� ����������� ��� ������ � ����� �������� �����, �� ������������ ������� �� �������� ������� ���� ���� ����� � ����� ������.

64-��������� Windows 2000 ��������� ������������ AWE, ��� ��� ������� 32 ��������� ����������, ������������ ���� ��������, �� �������� ������� �������. ������ AWE �� ����� ������� ��� 64-��������� ����������, ��������� ������� �� �������� ����������� ������� ������ �� ��� ����� �� ���� ����������� ������� ��� �������� ���������� ������, ������� ������� �� ������������ �� ����.

���������-������ AWE

��� ���������,�15 AWE ��� (�� ������� �� ��� 15-3), �������������, ��� ��������� ��������� �������� ���� � ��������� � ���� ������ ����� ������. ����� ������� �� ���� � �������� ���� ��������� ��������� � �������� 15-AWE �� �������-�����, ����������� � �����. ����� ����� ������� ��������� AWE ��������� ��� �������� ���� � ���������� ��� ����� ������

���������� ������ ���� �������� ������ �Text in Storage 0", ������ � ������ �Text in Storage 1". ����� ������ ���� ����������� � ������ �������� �����, � ������ � �� ������ ����� ��� ���� ���� ��������� �������� ���, ��� �������� ����.

rihter15-4.jpg

��� ��������� ������� �������������������� ��-������, ��� ����� ����� �� ������� ������ �������� �����, ��������� ��������������� ���� �� ��������. � ���, ������, ������������ � ������� No Storage, ��� ������ �������� ������ ����� ������ �� ��������� ����. ��-������, ����� ��������� ������ ���������� �������� �� �� ����� ������, ��������� � ������� �������� �����

���� �� ����������� ������� ���� � ��� �� ���� ������ � ����� ��������� ���� �� ������������, ��, ��������� �������� AWE ��� �� ���������, �� ������ �������� ��������� ���������

rihter15-5.jpg

�������� ��� ���� ���������-������� ��������� ����. ����� ��������� ���� �� � ���������� AWE, � ������ ��� �++-������, ������� ���������� � ����� AddrWin dows.h. ������ �����, CSystemInfo, � ����� ������� �������� ������� GetSystemInfo �� ������ ��� ���������� ������� ��������� ��� ������

������ �++-�����, CAddrWindow, ������������� �������� ���� ��� ����� Create ����������� �������� ����, ����� Destroy ���������� ��� ����, ����� UnmapStorage ��������� �� ���� ��������� � ��� ���� ������, � ����� ��������� ���������� PVOID ������ ���������� ����������� ����� ��������� ����.

������ C++-�����, CAddrWindowStorage, ������������� ���� ������, ������� ����� ��������� ������� ������ CAddrWindow ����� Attocate ��������� ����������� �������� � ������, �������� ���� ������, � ����� �������� ����� �� ����������, ����� Free ����������� ���� ������ ����� HowManyPagetAllocated ���������� �� �������� ���������� ���������� �������. �������, ����� MapStorage ��������� ���� ������ � �������� ������ CAddrWmdow, a UnmapStorage ��������� ���� �� ����� �������.

���������� C++ ������� ����������� ��������� ���������� ��������� AWE ��� ������� �� ��� ������� ������� CAddrWindow � CAddrWindowStorage ��������� ��� ������ �������� ������ ������ � ������ �����

AWE