CSAPP 六个重要实验 lab5

CSAPP  && lab5

实验指导书:

http://download.csdn.net/detail/u011368821/7951657

实验材料:

http://download.csdn.net/detail/u011368821/8019293

搞定这个实验还是要看一下以前的笔记,再复习一下block的组织方式,只看link里面第11节,动态内存分配的部分就可以了

http://blog.csdn.net/cinmyheart/article/details/38136375

然后看看对block的探测性学习

http://blog.csdn.net/cinmyheart/article/details/38174421

The only file you will modify and turn in is mm.c.

Your dynamic storage allocator will consist of the following three functions (and several helper functions), which are declared in mm.h and defined in mm.c:

int mm_init(void);
void* mm_malloc(size_t size);
void mm_free(void* ptr);

Your job is to complete this implementation by filling out mm_malloc() and mm_free().

任务就是利用以下现有的API去补全mm_malloc()和mm_free()

Provided Code

We define a BlockInfo struct designed to be used as a node in a doubly-linked explicit free list, and the following functions for manipulating free lists:

BlockInfo* searchFreeList(int reqSize): returns a block of at least the requested size if one exists (and NULL otherwise).
void insertFreeBlock(BlockInfo* blockInfo): inserts the given block in the free list in LIFO manner.
void removeFreeBlock(BlockInfo* blockInfo): removes the given block from the free list.

In addition, we implement mm_init and provide two helper functions implementing important parts of the allocator:

void requestMoreSpace(int incr): enlarges the heap by incr bytes (if enough memory is available on the machine to do so).
void coalesceFreeBlock(BlockInfo* oldBlock): coalesces any other free blocks adjacent in memory to oldBlock into a
single new large block and updates the free list accordingly.

这里free list的实现介绍: 这里需要说明一下的就是header处于低地址,footer处于高地址. 这里说明的原因是因为实现的时候会有指针的加减,注意一下就是了 :-)

sizeAndTags中记录的size信息是整个block的大小,包括了struct BlockInfo结构体的大小

论证: 观察struct BlockInfo结构体在内存中的分布

正是根据内存布局的特点,这里blockCursor只想当前block头的时候减去一个WORD_SIZE会得到什么?

前一个block的footer 即boundary tag,由此获知当前block的前一个block的大小

My answer:

/* Allocate a block of size size and return a pointer to it. */
void* mm_malloc (size_t size) {
  size_t reqSize;
  BlockInfo * ptrFreeBlock = NULL;
  BlockInfo * ptrNextBlock = NULL;
  size_t blockSize;
  size_t precedingBlockUseTag;
  size_t* ptrNextBlockFooter = NULL; 

  // Zero-size requests get NULL.
  if (size == 0) {
    return NULL;
  }

  // Add one word for the initial size header.
  // Note that we don't need to boundary tag when the block is used!
  size += WORD_SIZE;
  if (size <= MIN_BLOCK_SIZE) {
    // Make sure we allocate enough space for a blockInfo in case we
    // free this block (when we free this block, we'll need to use the
    // next pointer, the prev pointer, and the boundary tag).
    reqSize = MIN_BLOCK_SIZE;
  } else {
    // Round up for correct alignment
    reqSize = ALIGNMENT * ((size + ALIGNMENT - 1) / ALIGNMENT);
  }

  // Implement mm_malloc.  You can change or remove any of the above
  // code.  It is included as a suggestion of where to start.
  // You will want to replace this return statement...

  ptrFreeBlock =  searchFreeList(reqSize);

  if(!ptrFreeBlock)
  {
	requestMoreSpace(reqSize);
	ptrFreeBlock = searchFreeList(reqSize);
  }

  blockSize = SIZE(ptrFreeBlock->sizeAndTags);

  if(blockSize < reqSize + MIN_BLOCK_SIZE)
  {
	reqSize = blockSize;
  }

  precedingBlockUseTag = ptrFreeBlock->sizeAndTags & TAG_PRECEDING_USED;
  ptrFreeBlock->sizeAndTags = reqSize | precedingBlockUseTag | TAG_USED;

  ptrNextBlock = (BlockInfo*)UNSCALED_POINTER_ADD(ptrFreeBlock,reqSize);

  if(reqSize != blockSize)
  {
	ptrNextBlock->sizeAndTags = (blockSize - reqSize) | TAG_PRECEDING_USED;

	ptrNextBlockFooter = (size_t *)UNSCALED_POINTER_ADD(ptrFreeBlock,blockSize - WORD_SIZE);

	(*ptrNextBlockFooter) = ptrNextBlock->sizeAndTags;

	insertFreeBlock(ptrNextBlock);
  }
  else
  {
   	ptrNextBlock->sizeAndTags = ptrNextBlock->sizeAndTags | TAG_PRECEDING_USED;

	ptrNextBlockFooter = (size_t*)UNSCALED_POINTER_ADD(ptrNextBlock,SIZE(ptrNextBlock->sizeAndTags) - WORD_SIZE);

	(*ptrNextBlockFooter) = ptrNextBlock->sizeAndTags;
  }
  removeFreeBlock(ptrFreeBlock);

  return (void*)UNSCALED_POINTER_ADD(ptrFreeBlock,WORD_SIZE);
}

/* Free the block referenced by ptr. */
void mm_free (void *ptr) {
  size_t payloadSize;
  size_t blockSize;
  BlockInfo * blockInfo;
  BlockInfo * followingBlock;

  // Implement mm_free.  You can change or remove the declaraions
  // above.  They are included as minor hints.

  BlockInfo* prev_block = NULL;
  BlockInfo* next_block = NULL;

  blockInfo = (BlockInfo*)UNSCALED_POINTER_SUB(ptr,WORD_SIZE);
  followingBlock = (BlockInfo *)UNSCALED_POINTER_ADD(blockInfo,blockInfo->sizeAndTags);

  blockSize = SIZE(blockInfo->sizeAndTags);
  payloadSize = blockSize - WORD_SIZE;
  blockInfo->sizeAndTags = blockInfo->sizeAndTags & ~TAG_USED;
  *(size_t *)UNSCALED_POINTER_ADD(blockInfo,payloadSize) = blockInfo->sizeAndTags;
  followingBlock->sizeAndTags = followingBlock->sizeAndTags & ~TAG_PRECEDING_USED;

  if(followingBlock->sizeAndTags & TAG_USED == 0)
  {
      *(size_t *)UNSCALED_POINTER_ADD(followingBlock,SIZE(followingBlock->sizeAndTags) - WORD_SIZE) = followingBlock->sizeAndTags;
  }

  insertFreeBlock(blockInfo);
  coalesceFreeBlock(blockInfo);

}

最后运行./mdriver

会有一下输出

题外话,追究这个malloc实现的话....其实它就是基于已经有的stdlib.h已经有的malloc而实现的,介绍的是实现原理,利用双向链表

后面的额外练习表示没有耐心了,对于realloc...

到此,CSAPP的lab暂时告一段落,以后还会峰峰补补的去更新 :-)

如果对这六个lab感兴趣,欢迎邮箱联系交流讨论

[email protected]

时间: 2024-11-03 07:15:42

CSAPP 六个重要实验 lab5的相关文章

CSAPP 六个重要实验 lab4

CSAPP && lab4 实验材料: http://download.csdn.net/detail/u011368821/7926305 实验指导书: http://download.csdn.net/detail/u011368821/7926323 实验环境: Linux 3.13.11 Ubuntu 14.0 Part I: An Experiment in C and Java Q&A Answer these questions: 1.  What are the s

CSAPP 六个重要实验 lab2

CSAPP  &&  lab2 哈哈~ 不愧是"美国进口的六级炸弹"!爽歪歪的"升级打怪" 我把实验材料都上传到下面这个link了,0分下载(良心啊~) http://download.csdn.net/detail/u011368821/7892649 再一个实验指导说明供大家下载: http://download.csdn.net/detail/u011368821/7892677 对于Phase_1的分析: 0000000000400ef0 &

CSAPP 六个重要实验 lab1

CSAPP && lab1 --------------------------------------------------------------------实验要求-------------------------------------------------------------------- The Bit Puzzles This section describes the puzzles that you will be solving in bits.c. More

CSAPP 六个重要实验 lab3

CSAPP && lab3 level2 & leve3未完,待更新! : ) In this lab, you will gain firsthand experience with one of the methods commonly used to exploit security weaknesses in operating systems and network servers. Our purpose is to help you learn about the r

CSAPP 六个重要实验 lab0(预热乱暖场 \-0-/ )

CS : APP  && Lab 0 之前在网上找了一会关于这几个实验的资料,发现都没有.其实washington university的<CSE351: The Hardware/Software Interface> 的课程实验. 伟大而又乐于分享的高校.WU 我陆续更新把这五个实验(这个预热的lab0不算,太简单,C入门的级别,这里指lab1~lab5),贴出来分析学习.希望更多的人能够收益. 开源,分享. --------------------------------

CSAPP缓冲区溢出攻击实验(上)

CSAPP缓冲区溢出攻击实验(上) 下载实验工具.最新的讲义在这. 网上能找到的实验材料有些旧了,有的地方跟最新的handout对不上.只是没有关系,大体上仅仅是程序名(sendstring)或者參数名(bufbomb -t)的差异,不影响我们的实验. 1.实验工具 1.1 makecookie 后面实验中,五次"攻击"中有四次都是使你的cookie出如今它原本不存在的位置,所以我们首先要为自己产生一个cookie. 实验工具中的makecookie就是生成cookie用的.參数是你的

CSAPP缓冲区溢出攻击实验(下)

CSAPP缓冲区溢出攻击实验(下) 3.3 Level 2: 爆竹 实验要求 这一个Level的难度陡然提升,我们要让getbuf()返回到bang()而非test(),并且在执行bang()之前将global_value的值修改为cookie.因为全局变量与代码不在一个段中,所以我们不能让缓冲区一直溢出到.bss段(因为global_value初始化为0,所以它会被放在.bss而非.data段以节省空间)覆盖global_value的值.若修改了.bss和.text之间某些只读的段会引起操作系

CSAPP 六个重要的实验 lab5

CSAPP  && lab5 实验指导书: http://download.csdn.net/detail/u011368821/7951657 实验材料: http://download.csdn.net/detail/u011368821/8019293 搞定这个实验还是要看一下曾经的笔记,再复习一下block的组织方式.仅仅看link里面第11节,动态内存分配的部分就能够了 http://blog.csdn.net/cinmyheart/article/details/3813637

第六周总结 &amp; 实验报告(四)

第六周小结 一.instanceof关键字         在Java中使用instanceof关键字判断一个对象到底是哪个类的实例,返回boolean类型 1.instanceof关键字的作用 例class A{ public void fun1(){ System.out.println("A-->public void fun1(){}"); } public void fun2(){ this.fun1(); } } classB extends A{ public vo