In this lesson, we discuss the protection mechanisms provided by the page memory systems for protecting and isolating multiple users. We also discuss the virtual memory mapping, which lay out the data and the program area within the virtual memory addressing systems. These concepts lay down the foundation technologies for understanding buffer overflow attacks, memory leak attacks, such as Heartbleed, and operating system support for security. Buffer, in computer term, is defined as continuous blocks of memory that can be used for, one, data, which can be used to store data for variable, either static, global, dynamic, or local variable, and array. Second, the code, we have used the program, we have shared library, we have kernel program, and it contain instructions for execution. To shield the user and kernel programs from each other, the virtual memory is used in modern operating system. Within a virtual memory address space, different operating system and CPU has different way to organize and allocate a buffer for different region of data. On Linux operating systems, the static and global variable are allocated at load time on the data segment. The dynamic and local variable are allocated at run time on the stack, typically in the higher address space, so they're located different region. In a modern operating system, every program is compiled and allocated data and code to the same virtual memory address space by the compiler. The code or instructions are generated with addresses located in the lower part of the memory. The data are generated with the address located in the upper part of the memory. The concept is shown in the left-hand side of the diagram with three users and their data page and code page. And code page are the pages containing instruction. They typically are fixed size memory, 4k for example. The system is time shared by all these program users. This common virtual memory addresses to be mapped to physical memory address to avoid these conflicts. No two instructions from the same virtual memory address from two different users should be mapped into the same physical memory address locations or physical memory page. The instruction from one user should not be allowed to access or steal the content of the data page from the other user. Actually, for the matter that they are the code page to find out what an instruction is doing. For example, in the right side of these programs, the instruction user n, which is the last user there, data, the instruction is put in physical memory page (3,1). And she will not be allowed that instruction there, she will not be allowed to access the data page in page(2,7), which is belong to the user 2, and page number 7. This is achieved and enforced by the page loading mechanism and the booking function of the operating system. And all this address will be mapped by Memory Management Unit, called MMU, which is a hardware address mapping device, to speed up the processing. During the time slot allocated by particular users, the CPU will only be addressing, retrieving the data instruction of the code page content over public users. And so they are not sharing with other users. But when the time style expires, the operating system will perform content switching, loading the corresponding physical page and making entry in the MMU. So that the particular user will addressing and retrieving the data and the instruction page of the particular user in the main memory, in the physical memory. Here we show the virtual memory map used by the compiler of a Linux system to allocate memory area or addresses to various data and instructions of a user program. Here we use 32-bit memory address as an example. 64-bit is just considered you have more space in the area. The top three table entries show that the top one quarter of the memory address space is used and reserved by the kernel memory. They are from c0000000 to of under address location. The program instructions are allocated with address starting from 08048000, this is individual user's program instruction, and upward, until you finish all the instructions compiled by the compiler. In assembly language, assembly programming language, we call this region of the program .text segment. And typically in assembly programming code, it will have that particular section labeled. After that, the initialized program data, like the constant, the strings, used in creating the program are allocated. We call that region the .data segment. After that, the compiler will put the region of the data with uninitialized program data, such as global wearable, allocated in the section called .bss segment. They're on top of the top .data segment. After that, the compiler will reserve and point out the area of memory that reserve for heap, H-E-A-P. And this area will be allocated dynamically when a program execute function like malloc, M-A-L-L-O-C. And when that happens, they will allocate a block of address for a particular declaration structure. And we'll start at that address right after the BSS and go upward, to one of the higher address spaces. The variables created in the heap can be passed along to other part of the program through the pointer. So they are typically longer lasting, and they can persist throughout the execution of the program, until they are released. So unlike the local variable in the function, they can persist throughout the program. The local variable, on the other hand, we allocate in stack. On the other hand, they disappear right after the function return. They're released, so you cannot keep them. You somehow need to pass them along through the parameter that may allocate to them until this local variable are released. And therefore you need to take care of passing it back to the calling function through the parameter mechanism. The shared library code, right in the middle there, is allocate starting at address 4000000. The stack used by the subroutine call, starting at address very high, right after co0. Typically the last address of the first frame is bfffof, seven f, and grow downward. They allocate the stack by stack towards the lower edge of space. And the reason they have heap going up, the stack going down is so that we can have enough space for these two different memory usage. For example, if the stack frame number one is the main program, and stack frame zero is for the function foo, F-O-O, called by the main program. Then the frame, and let's assume the frame size of stack frame number 1 is 512 bytes. The frame size of the stack frame number 0 is 256 bytes. Then the stack frame could be arranged at the very top from Bffffe0 to bffff, all right, go to 250 bytes at the very top. And then when the function is called, another block of the memory beneath it, in that address space, bfffe0 to bfffdffdf will be allocated. So the stack actually goes downward, okay? But the fullest byte of those tech frame is actually the old edges of the whole bracket of address. The more code that being executed in-depths, the current stack frame will contain the address in a much lower address. And it contains local variable. It contains return program counter, which we are going to discuss in a moment about their structure. Here is another view of the memory layout of a Linux process. The content of the ESP, the Extended Stack Pointer register, which is the current Stack Pointer Register, will point to the last byte of the current stack. And which is typically the memory address of the last local variable allocated in that reactive functions. The break function call typically will contain the end of the data segment. It is set to the end of the heap data segment. That allow the next mmap function request to be allocated with the memory that hasn't been used and towards the high address space. And every time they're called, they're adjusted. Know that 32-bit architecture ideally there are 16 gigabyte of memory space. They are to be shared between the user stack and the shared library. If we look carefully at our starting address of 4o0, the address after address space bfff, right? They are to be shared by user stack and shared library. The next block, although they belong to the run time heap, they can have about 4 gigabyte of memory space, minus the area used by the instructions, the global variables, the constant, the .txt rate segment, the .data segment, and the .bss segment. Depending on how big is instructions and data, the remaining is used by that beginning 4 address that is used by the heap. Most of the time, space is enough. If not enough, you can move your execution system from 32-bit architecture to 64-bit architecture. But at the same time you need to have more memory to have that data effects, to utilize those extra space. Know that the content of this area are chopped into fixed chunk, fix sized page, they're called page, to be loaded into the physical main memory. Some are code page and some are instruction page, as shown.