windows 内存管理之 Section and View

http://msdn.microsoft.com/en-us/library/windows/hardware/ff563684(v=vs.85).aspx


Section Objects and Views

section object represents a section of memory that can be shared. A process can use a section object to share parts of its memory address space (memory sections) with other processes. Section objects also provide the mechanism by which a process can map a file into its memory address space.

Each memory section has one or more corresponding views. A view of a section is a part of the section that is actually visible to a process. The act of creating a view for a section is known as mapping a view of the section. Each process that is manipulating the contents of a section has its own view; a process can also have multiple views (to the same or different sections).

This section contains the following topics:

File-Backed and Page-File-Backed Sections

Managing Memory Sections

Security Issues for Section Objects and Views

File-Backed and Page-File-Backed Sections

All memory sections are supported ("backed") by disk files that can contain, either temporarily or permanently, the data to be shared. When you create a section, you can identify a specific data file to which the section will be backed. Such sections are called file-backed sections. If you do not identify a backing file, the section is backed by the system's paging file and the section is called a page-file-backed section. The data in file-backed sections can be permanently written to disk. Data in page-file-backed sections is never permanently written to disk.

file-backed section reflects the contents of an actual file on disk; in other words, it is a memory-mapped file. Any access to memory locations within a given file-backed section corresponds to accesses to locations in the associated file. If a process maps the view as read-only, any data that is read from the view is transparently read from the file. Similarly, if the process maps the view as read/write, any data that is read from the view or written to the view is transparently read from or written to the file. In either case, the view's virtual memory does not use any space in the page files. A file-backed section can also be mapped as copy-on-write. In that case, the view's data is read from the file, but any data written to the view is not written to the file; instead it is discarded after the final view is unmapped and the last handle to the section is closed.

A page-file-backed section is backed by the page files instead of by any explicit file on the disk. Any changes that are made to a page-file-backed section are automatically discarded after the section object is destroyed. Page-file-backed sections can be used as shared memory segments between two processes.

Any section, file-backed or not, can be shared between two processes. The same physical memory address range is mapped to a virtual memory address range within each process (though not necessarily to the same virtual address).

Managing Memory Sections

A driver can create a section object by calling ZwCreateSection, which returns a handle to the section object. Use theFileHandle parameter to specify the backing file, or NULL if the section is not file-backed. Additional handles to the section object can be opened by using ZwOpenSection.

To make the data that belongs to a section object accessible within the current process' address space, a view of the section must be mapped. Drivers can map a view of a section into the current process' address space by using theZwMapViewOfSection routine. The SectionOffset parameter specifies the byte offset where the view begins within the section, and the ViewSize specifies the number of bytes to be mapped.

The Protect parameter specifies the allowed operations on the view. Specify PAGE_READONLY for a read-only view, PAGE_READWRITE for a read/write view, and PAGE_WRITECOPY for a copy-on-write view.

No physical memory is allocated for a view until the virtual memory range is accessed. The first access of the memory range causes a page fault; the system then allocates a page to hold that memory location. If the section is file-backed, the system reads the contents of the file that corresponds to that page and copies it into memory. (Note that unused section objects and views do use some paged and nonpaged pool for bookkeeping purposes.)

After a driver is no longer using a view, it unmaps it by making a call to ZwUnmapViewOfSection. After the driver is no longer using the section object, it closes the section handle with ZwClose. Note that after the view is mapped and no other views are going to be mapped, it is safe to immediately call ZwClose on the section handle; the view (and section object) continue to exist until the view is unmapped. This is the recommended practice because it reduces the risk of the driver failing to close the handle.

Security Issues for Section Objects and Views

Drivers that create sections and views that are not to be shared with user mode must use the following protocol when they are working with sections and views:

  • The driver must use a kernel handle when it is opening a handle to the section object. Drivers can make sure that a handle is a kernel handle by either creating it in the system process, or specifying the OBJ_KERNEL_HANDLE attribute for the handle. For more information, see Object Handles.

  • The view must be mapped only from a system thread. (Otherwise, the view is accessible from the process whose context it is created in.) A driver can make sure that the view is mapped from the system process by using a system worker thread to perform the mapping operation. For more information, see System Worker Threads and Driver Thread Context.

Drivers that share a view with a user-mode process must use the following protocol when they are working with sections and views:

  • The driver, not the user-mode process, must create the section object and map the views.

  • As mentioned earlier, the driver must use a kernel handle when it is opening a handle to the section object. Drivers can make sure that a handle is a kernel handle by either creating it in the system process, or specifying the OBJ_KERNEL_HANDLE attribute for the handle. For more information, see Object Handles.

  • The view is mapped in the thread context of the process that shares the view. A highest-level driver can guarantee the view is mapped in the current process context by performing the mapping operation in a dispatch routine, suchDispatchDeviceControl. Dispatch routines of lower-level drivers run in an arbitrary thread context, and thus cannot safely map a view in a dispatch routine. For more information, see Driver Thread Context.

  • All memory accesses to the view within the driver must be protected by try-except blocks. A malicious user-mode application could unmap the view or change the protection state of the view. Either would cause a system crash unless protected by a try-except block. For more information, see Handling Exceptions.

The driver must also validate the contents of the view as necessary. The driver writer cannot assume that only a trusted user-mode component will have access to the view.

A driver that must share a section object with a user-mode application (that must be able to create its own views) must use the following protocol:

  • The driver, not the user-mode process, must create the section object. Drivers must never use a handle that was passed from user mode.

  • Before passing the handle to user mode, the driver must call ObReferenceObjectByHandle to obtain a reference to the section object. This prevents a malicious application from deleting the section object by closing the handle. The object reference should be stored in the driver's device extension.

  • After the driver is no longer using the section object, it must call ObDereferenceObject to release the object reference.

On systems that run Microsoft Windows Server 2003 with Service Pack 1 (SP1) and later versions, only kernel-mode drivers can open \Device\PhysicalMemory. However, drivers can decide to give a handle to a user application. To prevent security issues, only user applications that the driver trusts should be given access to \Device\PhysicalMemory.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C/C++中,你可以使用__attribute__关键字来把函数放到指定的内存段。 例如,你可以这样做: ``` __attribute__((section("MySection"))) void MyFunction() { // 函数内容 } ``` 这样,MyFunction函数就会被放到名为"MySection"的内存段中。 注意,__attribute__关键字是GCC编译器特有的,不是C/C++语言的一部分。因此,不同的编译器可能不支持这种用法。 另外,你还可以使用#pragma指令来控制函数的内存段,但是#pragma指令也是编译器特有的,并不是C/C++语言的一部分。 ### 回答2: 要将函数放到指定的内存段,我们可以使用特殊的编译器指令或者在代码中使用特殊的属性。 在某些编程语言(如C、C++)中,我们可以使用编译器指令来将函数放到指定的内存段。具体的指令可能因编译器而异,但通常类似于 "__attribute__((section("section_name")))". 通过在函数定义前使用这个指令,我们可以告诉编译器将该函数放置在名为"section_name"的内存段中。 示例代码如下: ```c void __attribute__((section("my_section"))) my_function() { // 函数的实现 } ``` 这样,编译器就会将函数"my_function"放置在名为"my_section"的内存段中。需要注意的是,具体的内存段名称可能与编译器和平台有关,需要查阅相关文档进行确认。 此外,某些编程语言还提供了直接在代码中使用特殊的属性来实现类似的功能。例如,使用Python中的装饰器可以实现将函数放置在指定内存段的效果。 ```python def my_decorator(func): # 将函数放置在特定内存段的装饰器实现 # ... return func @my_decorator def my_function(): # 函数的实现 pass ``` 上述代码中,"my_decorator"装饰器可以修改函数的属性,使其在运行时被放置在指定的内存段中。 总之,要将函数放到指定的内存段,我们可以使用特殊的编译器指令或者在代码中使用特殊的属性来实现。具体实现方法可能因编程语言、编译器和平台而异,需要查阅相关文档或了解特定环境下的具体用法。 ### 回答3: 将函数放到指定的内存段可以通过使用特定的属性(attribute)指定函数所在的段(section)。在C语言中,可以使用__attribute__((section("段名")))语法来实现。 首先,在定义函数之前,使用attribute将其放置到指定的内存段中。例如,我们可以定义一个放置在名为"mysection"段的函数如下: ```c __attribute__((section("mysection"))) void myFunction() { // 函数体 } ``` 在上述代码中,myFunction函数使用attribute属性将其放置到名为"mysection"的内存段中。 接下来,我们需要在链接器脚本文件(linker script)中将这个内存段定义为可执行代码段。例如,我们假设我们有一个名为"custom.ld"的链接器脚本文件,其中定义了不同的内存段及其属性: ``` SECTIONS { .mysection : { /* 指定mysection为可执行的代码段 */ *(.mysection) } /* 其他段的定义... */ } ``` 在以上链接器脚本文件中,我们使用*(.mysection)将mysection段定义为可执行的代码段。这样,当我们编译并链接程序时,myFunction函数将被放置在这个段中。 需要注意的是,具体的链接器脚本文件以及可用的段名称取决于所使用的编译器和目标硬件平台。因此,要将函数放置到指定的内存段,需要仔细查阅相关文档或资料,以确定正确的语法和设置。 总结起来,要将函数放到指定的内存段,我们可以使用attribute属性指定函数所在的段,然后在链接器脚本文件中将该段定义为可执行代码段。这样,编译和链接程序时,函数将被放置在这个指定的内存段中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值