SlideShare uma empresa Scribd logo
1 de 30
Baixar para ler offline
程式設計師的自我修養
      Chapter 10 記憶體

Shu-Yu Fu (shuyufu@gmail.com)
本章概述

本章討論程式執行時Stack和heap的配置!
● 藉由觀察stack的變化導出Calling convention,
  然後函式如何傳遞返回值!
● 除了比較heap在Linux和Windows上的實作外,
  我們還會討論heap管理演算法!
● PS︰記得參考這篇產生你的assembly code+C
  code
  How to get gcc to interleave assembly output
  with original source code
  Using GCC to produce readable assembly?
程式的記憶體配置
Flat memory model or linear memory model refers to a memory addressing
paradigm in low-level software design such that the CPU can directly (and sequentially/linearly)
address all of the available memory locations without having to resort to any sort of memory
segmentation or paging schemes.
int *p = (int *)0x12345678;
++*p;

● 記憶體空間"預設"的區域︰
      ○ 堆疊﹙stack﹚
      ○ 堆積﹙heap﹚
      ○ ELF / dynamic libraries / reserved
程式的記憶體配置
Flat memory model or linear memory model refers to a memory addressing
paradigm in low-level software design such that the CPU can directly (and sequentially/linearly)
address all of the available memory locations without having to resort to any sort of memory
segmentation or paging schemes.
int *p = (int *)0x12345678;
++*p;

● 記憶體空間"預設"的區域︰
      ○ 堆疊﹙stack﹚
      ○ 堆積﹙heap﹚
      ○ ELF / dynamic libraries / reserved
堆疊與呼叫慣例

● 在i386下,esp暫存器指向堆疊頂端
● 堆疊保存了一個函式呼叫所需要的維護資訊,
  常被稱為堆疊框架﹙stack frame﹚或活動記
 錄﹙activate record﹚
 ○ 函式的傳回位址和參數
 ○ 臨時變數
 ○ 保存的上下脈絡
● ESP暫存器始終指向堆疊頂部,EBP﹙又稱為
  框架指標Frame Pointer﹚指向函式活動記錄的
  一個固定位置。
堆疊與呼叫慣例

● 在i386下,esp暫存器指向堆疊頂端
● 堆疊保存了一個函式呼叫所需要的維護資訊,
  常被稱為堆疊框架﹙stack frame﹚或活動記
 錄﹙activate record﹚
 ○ 函式的傳回位址和參數
 ○ 臨時變數
 ○ 保存的上下脈絡
● ESP暫存器始終指向堆疊頂部,EBP﹙又稱為
  框架指標Frame Pointer﹚指向函式活動記錄的
  一個固定位置。
呼叫一個函式

1. 把所有或一部分參數推入堆疊中,如果有其他
   參數沒有入堆疊,那麼使用某些特定的暫存器
   傳遞
2. 把當前指令的下一條指令的位址推入堆疊中
3. 跳轉到函式體執行
呼叫一個函式

1. 把所有或一部分參數推入堆疊中,如果有其他
   參數沒有入堆疊,那麼使用某些特定的暫存器
   傳遞
2. 把當前指令的下一條指令的位址推入堆疊中
3. 跳轉到函式體執行
函式的開始與結束
HIGH           LOW




   EBP   ESP
函式的開始與結束
HIGH           LOW
         E
         B
         P




   EBP   ESP
函式的開始與結束
HIGH         LOW
       E
       B
       P




       ESP



       EBP
函式的開始與結束
HIGH               LOW
       E V V V
       B A A A
       P R R R




             ESP



       EBP
函式的開始與結束
HIGH                   LOW
       E V V V R R
       B A A A E E
       P R R R G G




                 ESP



       EBP
函式的開始與結束
HIGH               LOW
       E V V V
       B A A A
       P R R R




             ESP



       EBP
函式的開始與結束
HIGH             LOW
       E V V V
       B A A A
       P R R R




       ESP



       EBP
函式的開始與結束
HIGH                   LOW
               V V V
               A A A
               R R R




   EBP   ESP
小知識

● 未初始化的區域變數的初始化成0xcccccccc
● Hot Patch Prologue
  More: 3.13 Hotpatch Support
  在GCC對應的實作為ms_hook_prologue
呼叫慣例
int foo(int n, float m)
{
  int a = 0, b = 0;
  ...
}
● 先推入n後推入m?
● 呼叫方和被呼叫方對於呼叫的約定
  ○ 參數的傳遞順序﹙左先?右先?﹚和方式﹙堆疊?暫
    存器?﹚
  ○ 堆疊的維護方式,由呼叫方或被呼叫方維護
  ○ 名稱修飾﹙name-mangling﹚的策略,對呼叫慣例進
    行區分
列表
呼叫慣例        出堆疊方       參數傳遞             名稱修飾

cdecl      函式呼叫方   從右至左的順序推參數    底線+函式名
                   入堆疊

stdcall    函式本身    從右至左的順序推參數    底線+函式名+@+參數的位元
                   入堆疊           組數,如函式int func(int a,
                                 double b)的修飾名是
                                 _func@12
fastcall   函式本身    頭兩個DWORD(4位元組) @+函式名+@+參數的位元組
                   類型或者占更少位元組 數
                   的參數被放入暫存器,其
                   他剩下的參數按右到左
                   的順序推入堆疊



pascal     函式本身    從左至右的順序推參數    較為複雜,參見pascal文件
                   入堆疊
更多的呼叫慣例

● 用於特殊場合naked call
● C++更複雜的名稱修飾策略和thiscall
 ○ VC,this存放於ecx暫存器
 ○ GCC,this是函式的第一個參數
函式傳回值傳遞

● eax是傳遞傳回值的通道
● 大於4-byte的傳回值如何傳遞?
 ○ 5-byte ~ 8-byte採eax(low-byte)和edx(high-byte)聯合
   傳回
 ○ > 8-byte,用隱含參數+eax
● 可以不懂過程,但,要記得不要輕易傳回大尺
  寸的物件
● C++提出了傳回值最佳化﹙Return Value
  Optimization, RVO﹚,可以將某些場合下的
  物件複製減少1次
堆積與記憶體管理

● 為什麼需要堆積?
 ○ 動態產生
 ○ 堆疊上的資料在函式傳回時就會被釋放,那main()裡的
   區域變數呢?
● 把配置和釋放做在作業系統核心
 ○ 每次都要系統呼叫,效能太差
● 應用程式自行管理,多數的情況是執行階段程式庫管理
 ○ 配置
 ○ 管理,堆積的分配演算法
Linux行程堆積管理

● brk系統呼叫,設定行程資料區段的結束位址
 int brk(void * end_data_segment);
● mmap系統呼叫,申請一段虛擬位址空間
 void * mmap(void        * start,     // 起始位址
       size_t length, // 長度
       int    prot, // 權限﹙讀/寫/執行﹚
             int       flags,   // 類型﹙檔案/匿名﹚
             int       fd,      // fd...XD
             off_t     offset); // 偏移量
● malloc一次能夠申請的最大空間是多少?
 ○ Demo
Windows行程堆積管理

● VirtualAlloc()用來向系統申請空間,應用程式
  可以按照需要隨意使用。
● 堆積管理器﹙Heap Manager﹚實作了空間分
  配演算法。
 ○   NTDLL.DLL︰HeapCreate﹑HeapAlloc﹑HeapFree﹑HeapDestroy
● 預設1MB,可透過連結器的/HEAP參數指定
● 最大的一塊大約是1.5GB
堆積分配演算法

● 堆積只是程式向作業系統申請劃出來的一塊
  位址空間
● 如何管理一大塊連續的記憶體空間並按照需
  求分配﹑釋放空間。
 ○ 空間串列
 ○ 點陣圖
 ○ 物件集區
空間串列
把堆積中各個空閒的區塊按照串列的方式連接起來,當使用
者請求一塊空間時,可以遊走整個列表,直到找到合適大小的
區塊並且將它拆分;當使用者釋放空間時將它合併到空閒串
列中。
點陣圖

將整個堆積劃分為大量大小相同的區塊
﹙block﹚,當請求記憶體時,總是分配整數個的
空間。
物件集區

如果每一次分配的空間大小都一樣,依把整個堆
積空間劃分為大量的小塊,每次請求的時候只需
要找到一個小塊就可以了。
現實應用

● 堆積分配演算法往往採取多演算法複合而成
● 以glibc來講,
    ○ < 64-byte︰物件集區
    ○ < 512-byte︰物件集區和最佳適配演算法的最佳折衷
    ○ < 128-KB︰最佳適配演算法
    ○ > 128-KB︰mmap()
●   More: glibc中malloc的详细解释
參考實作

● jemalloc
  http://www.canonware.
  com/jemalloc/
● nedmalloc
  http://www.nedprod.
  com/programs/portable/nedmalloc
  /

Mais conteúdo relacionado

Mais procurados

Return to dlresolve
Return to dlresolveReturn to dlresolve
Return to dlresolveAngel Boy
 
ch13-pv1-system-calls
ch13-pv1-system-callsch13-pv1-system-calls
ch13-pv1-system-callsyushiang fu
 
系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統鍾誠 陳鍾誠
 
系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器鍾誠 陳鍾誠
 
系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 5 章 連結與載入系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 5 章 連結與載入鍾誠 陳鍾誠
 
Golangintro
GolangintroGolangintro
Golangintro理 傅
 
07 陣列與字串
07 陣列與字串07 陣列與字串
07 陣列與字串shademoon
 
Device Driver - Chapter 3字元驅動程式
Device Driver - Chapter 3字元驅動程式Device Driver - Chapter 3字元驅動程式
Device Driver - Chapter 3字元驅動程式ZongYing Lyu
 
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Justin Lin
 
第六章 函數與巨集
第六章 函數與巨集第六章 函數與巨集
第六章 函數與巨集shademoon
 
系統程式 -- 第 3 章 組合語言
系統程式 -- 第 3 章 組合語言系統程式 -- 第 3 章 組合語言
系統程式 -- 第 3 章 組合語言鍾誠 陳鍾誠
 

Mais procurados (20)

Return to dlresolve
Return to dlresolveReturn to dlresolve
Return to dlresolve
 
Execution
ExecutionExecution
Execution
 
系統程式 - 附錄
系統程式 - 附錄系統程式 - 附錄
系統程式 - 附錄
 
ch13-pv1-system-calls
ch13-pv1-system-callsch13-pv1-system-calls
ch13-pv1-system-calls
 
系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統
 
系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器
 
系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 5 章 連結與載入系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 5 章 連結與載入
 
ch7-pv1-modules
ch7-pv1-modulesch7-pv1-modules
ch7-pv1-modules
 
系統程式 -- 附錄
系統程式 -- 附錄系統程式 -- 附錄
系統程式 -- 附錄
 
linux mm
linux mmlinux mm
linux mm
 
系統程式 -- 第 4 章
系統程式 -- 第 4 章系統程式 -- 第 4 章
系統程式 -- 第 4 章
 
系統程式 -- 第 5 章
系統程式 -- 第 5 章系統程式 -- 第 5 章
系統程式 -- 第 5 章
 
Golangintro
GolangintroGolangintro
Golangintro
 
07 陣列與字串
07 陣列與字串07 陣列與字串
07 陣列與字串
 
Device Driver - Chapter 3字元驅動程式
Device Driver - Chapter 3字元驅動程式Device Driver - Chapter 3字元驅動程式
Device Driver - Chapter 3字元驅動程式
 
系統程式 -- 第 7 章
系統程式 -- 第 7 章系統程式 -- 第 7 章
系統程式 -- 第 7 章
 
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
 
系統程式 -- 第 2 章
系統程式 -- 第 2 章系統程式 -- 第 2 章
系統程式 -- 第 2 章
 
第六章 函數與巨集
第六章 函數與巨集第六章 函數與巨集
第六章 函數與巨集
 
系統程式 -- 第 3 章 組合語言
系統程式 -- 第 3 章 組合語言系統程式 -- 第 3 章 組合語言
系統程式 -- 第 3 章 組合語言
 

Destaque

程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1Shu-Yu Fu
 
TLPI - Chapter 44 Pipe and Fifos
TLPI - Chapter 44 Pipe and FifosTLPI - Chapter 44 Pipe and Fifos
TLPI - Chapter 44 Pipe and FifosShu-Yu Fu
 
程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4Shu-Yu Fu
 
程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5Shu-Yu Fu
 
BT5攻防手法
BT5攻防手法BT5攻防手法
BT5攻防手法openblue
 
Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1Lin Yo-An
 
Tlpi chapter 38 writing secure privileged programs
Tlpi   chapter 38 writing secure privileged programsTlpi   chapter 38 writing secure privileged programs
Tlpi chapter 38 writing secure privileged programsShu-Yu Fu
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 ProcessShu-Yu Fu
 
TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsShu-Yu Fu
 
大家來學GObject
大家來學GObject大家來學GObject
大家來學GObjectShu-Yu Fu
 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationShu-Yu Fu
 

Destaque (12)

程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1
 
TLPI - Chapter 44 Pipe and Fifos
TLPI - Chapter 44 Pipe and FifosTLPI - Chapter 44 Pipe and Fifos
TLPI - Chapter 44 Pipe and Fifos
 
程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4
 
程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5
 
BT5攻防手法
BT5攻防手法BT5攻防手法
BT5攻防手法
 
Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1
 
Chapter2
Chapter2  Chapter2
Chapter2
 
Tlpi chapter 38 writing secure privileged programs
Tlpi   chapter 38 writing secure privileged programsTlpi   chapter 38 writing secure privileged programs
Tlpi chapter 38 writing secure privileged programs
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 Process
 
TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File Systems
 
大家來學GObject
大家來學GObject大家來學GObject
大家來學GObject
 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory Allocation
 

Semelhante a 程式設計師的自我修養 Chapter 10 記憶體

[若渴計畫]64-bit Linux Return-Oriented Programming
[若渴計畫]64-bit Linux Return-Oriented Programming[若渴計畫]64-bit Linux Return-Oriented Programming
[若渴計畫]64-bit Linux Return-Oriented ProgrammingAj MaChInE
 
Php extension开发
Php extension开发Php extension开发
Php extension开发thinkinlamp
 
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)jeffz
 
6, awk
6, awk6, awk
6, awkted-xu
 
Linux Tracing System 浅析 & eBPF框架开发经验分享
Linux Tracing System 浅析 & eBPF框架开发经验分享Linux Tracing System 浅析 & eBPF框架开发经验分享
Linux Tracing System 浅析 & eBPF框架开发经验分享happyagan
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
 
Erlang开发及应用
Erlang开发及应用Erlang开发及应用
Erlang开发及应用litaocheng
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented ProgrammingAngel Boy
 
1 C入門教學
1  C入門教學1  C入門教學
1 C入門教學Sita Liu
 
Sicmutils 介紹:Scmutils 的 Clojure 版函式庫
Sicmutils 介紹:Scmutils 的 Clojure 版函式庫Sicmutils 介紹:Scmutils 的 Clojure 版函式庫
Sicmutils 介紹:Scmutils 的 Clojure 版函式庫睿麒 王
 
Lab2在幹嘛
Lab2在幹嘛Lab2在幹嘛
Lab2在幹嘛F74011297
 
Coding guideline
Coding guidelineCoding guideline
Coding guideline斯理 衛
 
Method call in Ruby
Method call in RubyMethod call in Ruby
Method call in RubyHung Wu Lo
 
Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)FLASH开发者交流会
 
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)Shanda innovation institute
 
Ecmascript
EcmascriptEcmascript
Ecmascriptjay li
 
程式人雜誌 -- 2015 年5月號
程式人雜誌 -- 2015 年5月號程式人雜誌 -- 2015 年5月號
程式人雜誌 -- 2015 年5月號鍾誠 陳鍾誠
 
程式人雜誌 2015年五月
程式人雜誌 2015年五月程式人雜誌 2015年五月
程式人雜誌 2015年五月鍾誠 陳鍾誠
 

Semelhante a 程式設計師的自我修養 Chapter 10 記憶體 (20)

[若渴計畫]64-bit Linux Return-Oriented Programming
[若渴計畫]64-bit Linux Return-Oriented Programming[若渴計畫]64-bit Linux Return-Oriented Programming
[若渴計畫]64-bit Linux Return-Oriented Programming
 
Php extension开发
Php extension开发Php extension开发
Php extension开发
 
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)
 
6, awk
6, awk6, awk
6, awk
 
Reverse Engineering - Assembly & Introduction
Reverse Engineering - Assembly & IntroductionReverse Engineering - Assembly & Introduction
Reverse Engineering - Assembly & Introduction
 
Linux Tracing System 浅析 & eBPF框架开发经验分享
Linux Tracing System 浅析 & eBPF框架开发经验分享Linux Tracing System 浅析 & eBPF框架开发经验分享
Linux Tracing System 浅析 & eBPF框架开发经验分享
 
Hi Haskell
Hi HaskellHi Haskell
Hi Haskell
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
Erlang开发及应用
Erlang开发及应用Erlang开发及应用
Erlang开发及应用
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented Programming
 
1 C入門教學
1  C入門教學1  C入門教學
1 C入門教學
 
Sicmutils 介紹:Scmutils 的 Clojure 版函式庫
Sicmutils 介紹:Scmutils 的 Clojure 版函式庫Sicmutils 介紹:Scmutils 的 Clojure 版函式庫
Sicmutils 介紹:Scmutils 的 Clojure 版函式庫
 
Lab2在幹嘛
Lab2在幹嘛Lab2在幹嘛
Lab2在幹嘛
 
Coding guideline
Coding guidelineCoding guideline
Coding guideline
 
Method call in Ruby
Method call in RubyMethod call in Ruby
Method call in Ruby
 
Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)Avm2虚拟机浅析与as3性能优化(陈士凯)
Avm2虚拟机浅析与as3性能优化(陈士凯)
 
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
[Flash开发者交流][2010.05.30]avm2虚拟机浅析与as3性能优化(陈士凯)
 
Ecmascript
EcmascriptEcmascript
Ecmascript
 
程式人雜誌 -- 2015 年5月號
程式人雜誌 -- 2015 年5月號程式人雜誌 -- 2015 年5月號
程式人雜誌 -- 2015 年5月號
 
程式人雜誌 2015年五月
程式人雜誌 2015年五月程式人雜誌 2015年五月
程式人雜誌 2015年五月
 

程式設計師的自我修養 Chapter 10 記憶體