简体   繁体   English

程序的虚拟地址空间有多大?

[英]How large is the virtual address space of a program?

I was reading Operating Systems: Three Easy Pieces . 我正在阅读操作系统:三篇简单文章 To learn how the virtual address space for a program look like, I run the following code. 要了解程序的虚拟地址空间的外观,我运行以下代码。

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    printf("location of code : %p\n", (void *) main);
    printf("location of heap : %p\n", (void *) malloc(1));
    int x = 3;
    printf("location of stack : %p\n", (void *) &x);
    return x;
}

Its output is: 其输出为:

location of code : 0x564eac1266fa
location of heap : 0x564ead8e5670
location of stack : 0x7fffd0e77e54

Why the code segment's location is 0x564eac1266fa ? 为什么代码段的位置是0x564eac1266fa What does so large a (virtual) space before it use for? 在使用之前,这么大的(虚拟)空间是什么? Why doesn't it start from or near 0x0 ) 为什么不从0x0或附近开始)

And, why the program's virtual address is so large?(from the stack location, it's 48 bits wide) What's the point of it? 而且,为什么程序的虚拟地址这么大?(从堆栈位置开始,它是48位宽),这有什么意义?

The possible virtual address space organizations are defined by the hardware you are using, specifically the MMU it supports. 可能的虚拟地址空间组织由您使用的硬件(特别是它支持的MMU)定义。 The OS may then use any organization that the hardware can be coerced into using, but generally it just uses it directly (possibly with some subsetting), as that is most efficient. 然后,操作系统可以使用可以强制使用硬件的任何组织,但是通常,它仅直接使用它(可能带有某些子集),因为这是最有效的。

The x86_64 architecture defines a 48-bit virtual address space 1 , and most OSes reserve half of that for system use, so user programs see a 47 bit address space. x86_64体系结构定义了48位虚拟地址空间1 ,大多数OS保留一半的虚拟地址空间供系统使用,因此用户程序看到的是47位地址空间。 Within that address space, most OSes will randomize the addresses used for any given program, so as to make exploiting bugs in the programs harder. 在该地址空间内,大多数操作系统将随机化用于任何给定程序的地址,以使利用程序中的错误变得更加困难。


1 Strictly speaking, the architecture defines a 64-bit virtual address space, but then reserves all addresses that do not have the top 17 bits all 0 or all 1. 1 严格来说,该架构定义了一个64位虚拟地址空间,但是保留了所有地址,这些地址没有前17位全0或全1。

You are barking up the wrong tree with what you are trying to do here. 您正在用这里尝试做的事情树错了树。 A process has multiple stacks, may have multiple heaps, and main might not be the start of the code. 一个进程具有多个堆栈,可能具有多个堆,并且main可能不是代码的开始。 Viewing an address space as a code segment, stack segment, heap segment, ... as horrible operating systems books do is only going to get you confused. 将地址空间视为代码段,堆栈段,堆段等,就像可怕的操作系统书籍一样,只会使您感到困惑。

Because of logical addressing, the memory mapped into the address space does not have to be contiguous. 由于逻辑寻址,映射到地址空间的内存不必是连续的。

Why the code segment's location is 0x564eac1266fa? 为什么代码段的位置是0x564eac1266fa? What does so large a (virtual) space before it use for? 在使用之前,这么大的(虚拟)空间是什么? Why doesn't it start from or near 0x0) 为什么不从0x0或附近开始)

The start of code in your process would well be at 0x564eac1266f8. 您过程中的代码起始位置应为0x564eac1266f8。 The fact that you have a high address does not mean the lower addresses have been mapped into the process address space. 您拥有高地址的事实并不意味着低位地址已映射到进程地址空间中。

And, why the program's virtual address is so large?(from the stack location, it's 48 bits wide) What's the point of it? 而且,为什么程序的虚拟地址这么大?(从堆栈位置开始,它是48位宽),这有什么意义?

Stacks generally start high and grow low. 堆栈通常从高处开始,到低处开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM