Skip to main content
Malloc Lab

基于 Segregated Free Lists + First Fit + Immediate Coalescing + Smart Reallocation 实现.

Segregated Free Lists

使用最开始的 9 个 Word 存储 (0, 32], (32, 64], …, (2048, 4096], (4096, inf) 分块大小的 Free List Head, 同时维护这 9 个链表.

在代码中, 我们约定 PredSucc 表示链表中的拓扑顺序, PrevNext 表示 block 在 mem_heap 上的地址顺序.


Course WorkComputer Organization and ArchitectureMalloc LabAbout 3 minAbout 802 words
Cache Lab: Understanding Cache Memories

Part A: Writing a Cache Simulator

argparse 部分使用了更强大的 argp, 而没有使用 getopt (因为一开始没看到作业建议用 getopt)

忽略 Instruction Load, Data Load 和 Data Store 都可看作访问一次 cache, 而 Data Modify 由 Data Load 和 Data Store 组成可看作访问两次 cache.


Course WorkCache LabComputer Organization and ArchitectureAbout 5 minAbout 1385 words
Attack Lab: Phase 1
Run
objdump --disassemble ctarget > ctarget.asm
Read File ctarget.asm
...
0000000000401b28 <getbuf>:
  401b28:	48 83 ec 28          	sub    $0x28,%rsp
...
0000000000401b3e <touch1>:
...

Course WorkAttack LabComputer Organization and ArchitectureLess than 1 minuteAbout 130 words
Attack Lab: Phase 2
Run
objdump --disassemble ctarget > ctarget.asm
Read File cookie.txt
0x3a9a6c2f

Course WorkAttack LabComputer Organization and ArchitectureLess than 1 minuteAbout 233 words
Attack Lab: Phase 3
Run
$ gdb ctarget --tui
...
(gdb) break getbuf
Breakpoint 1 at 0x401b28: file buf.c, line 12.
(gdb) run -q
Starting program: /home/stu2020012872/3-lab-3-attacklab/target97/ctarget -q

Breakpoint 1, getbuf () at buf.c:12
(gdb) info registers rsp
rsp            0x55668cc0       0x55668cc0

Course WorkAttack LabComputer Organization and ArchitectureLess than 1 minuteAbout 277 words
Attack Lab: Phase 4
Run
objdump --disassemble rtarget > rtarget.asm
Read File rtarget.asm
...
0000000000401b6a <touch2>:
...
0000000000401d0f <start_farm>:
  401d0f:	b8 01 00 00 00       	mov    $0x1,%eax
  401d14:	c3                   	retq

0000000000401d15 <addval_420>:
  401d15:	8d 87 48 89 c7 94    	lea    -0x6b3876b8(%rdi),%eax
  401d1b:	c3                   	retq

0000000000401d1c <setval_146>:
  401d1c:	c7 07 58 92 90 90    	movl   $0x90909258,(%rdi)
  401d22:	c3                   	retq

0000000000401d23 <addval_451>:
  401d23:	8d 87 00 58 c3 1c    	lea    0x1cc35800(%rdi),%eax
  401d29:	c3                   	retq

0000000000401d2a <addval_158>:
  401d2a:	8d 87 48 89 c7 c3    	lea    -0x3c3876b8(%rdi),%eax
  401d30:	c3                   	retq

0000000000401d31 <setval_387>:
  401d31:	c7 07 58 c3 08 57    	movl   $0x5708c358,(%rdi)
  401d37:	c3                   	retq

0000000000401d38 <getval_439>:
  401d38:	b8 48 89 c7 c3       	mov    $0xc3c78948,%eax
  401d3d:	c3                   	retq

0000000000401d3e <getval_205>:
  401d3e:	b8 b7 d7 58 c2       	mov    $0xc258d7b7,%eax
  401d43:	c3                   	retq

0000000000401d44 <setval_398>:
  401d44:	c7 07 48 89 c7 c7    	movl   $0xc7c78948,(%rdi)
  401d4a:	c3                   	retq

0000000000401d4b <mid_farm>:
...

Course WorkAttack LabComputer Organization and ArchitectureAbout 1 minAbout 339 words
Attack Lab: Phase 5
Run
objdump --disassemble rtarget > rtarget.asm
Read File rtarget.asm
...
0000000000401c78 <touch3>:
...
0000000000401d0f <start_farm>:
  401d0f:	b8 01 00 00 00       	mov    $0x1,%eax
  401d14:	c3                   	retq

0000000000401d15 <addval_420>:
  401d15:	8d 87 48 89 c7 94    	lea    -0x6b3876b8(%rdi),%eax
  401d1b:	c3                   	retq

0000000000401d1c <setval_146>:
  401d1c:	c7 07 58 92 90 90    	movl   $0x90909258,(%rdi)
  401d22:	c3                   	retq

0000000000401d23 <addval_451>:
  401d23:	8d 87 00 58 c3 1c    	lea    0x1cc35800(%rdi),%eax
  401d29:	c3                   	retq

0000000000401d2a <addval_158>:
  401d2a:	8d 87 48 89 c7 c3    	lea    -0x3c3876b8(%rdi),%eax
  401d30:	c3                   	retq

0000000000401d31 <setval_387>:
  401d31:	c7 07 58 c3 08 57    	movl   $0x5708c358,(%rdi)
  401d37:	c3                   	retq

0000000000401d38 <getval_439>:
  401d38:	b8 48 89 c7 c3       	mov    $0xc3c78948,%eax
  401d3d:	c3                   	retq

0000000000401d3e <getval_205>:
  401d3e:	b8 b7 d7 58 c2       	mov    $0xc258d7b7,%eax
  401d43:	c3                   	retq

0000000000401d44 <setval_398>:
  401d44:	c7 07 48 89 c7 c7    	movl   $0xc7c78948,(%rdi)
  401d4a:	c3                   	retq

0000000000401d4b <mid_farm>:
  401d4b:	b8 01 00 00 00       	mov    $0x1,%eax
  401d50:	c3                   	retq

0000000000401d51 <add_xy>:
  401d51:	48 8d 04 37          	lea    (%rdi,%rsi,1),%rax
  401d55:	c3                   	retq

0000000000401d56 <getval_493>:
  401d56:	b8 89 c1 08 d2       	mov    $0xd208c189,%eax
  401d5b:	c3                   	retq

0000000000401d5c <getval_480>:
  401d5c:	b8 89 d6 08 db       	mov    $0xdb08d689,%eax
  401d61:	c3                   	retq

0000000000401d62 <getval_269>:
  401d62:	b8 89 ca 60 c9       	mov    $0xc960ca89,%eax
  401d67:	c3                   	retq

0000000000401d68 <getval_133>:
  401d68:	b8 5e 48 88 e0       	mov    $0xe088485e,%eax
  401d6d:	c3                   	retq

0000000000401d6e <addval_307>:
  401d6e:	8d 87 99 c1 20 c9    	lea    -0x36df3e67(%rdi),%eax
  401d74:	c3                   	retq

0000000000401d75 <addval_127>:
  401d75:	8d 87 81 d6 84 d2    	lea    -0x2d7b297f(%rdi),%eax
  401d7b:	c3                   	retq

0000000000401d7c <addval_116>:
  401d7c:	8d 87 89 c1 90 90    	lea    -0x6f6f3e77(%rdi),%eax
  401d82:	c3                   	retq

0000000000401d83 <addval_120>:
  401d83:	8d 87 2e 48 88 e0    	lea    -0x1f77b7d2(%rdi),%eax
  401d89:	c3                   	retq

0000000000401d8a <getval_260>:
  401d8a:	b8 a9 d6 20 c0       	mov    $0xc020d6a9,%eax
  401d8f:	c3                   	retq

0000000000401d90 <getval_400>:
  401d90:	b8 48 89 e0 c3       	mov    $0xc3e08948,%eax
  401d95:	c3                   	retq

0000000000401d96 <addval_196>:
  401d96:	8d 87 c9 c1 20 db    	lea    -0x24df3e37(%rdi),%eax
  401d9c:	c3                   	retq

0000000000401d9d <setval_327>:
  401d9d:	c7 07 89 c1 00 d2    	movl   $0xd200c189,(%rdi)
  401da3:	c3                   	retq

0000000000401da4 <getval_253>:
  401da4:	b8 89 d6 00 db       	mov    $0xdb00d689,%eax
  401da9:	c3                   	retq

0000000000401daa <setval_300>:
  401daa:	c7 07 81 c1 20 c0    	movl   $0xc020c181,(%rdi)
  401db0:	c3                   	retq

0000000000401db1 <getval_176>:
  401db1:	b8 48 89 e0 94       	mov    $0x94e08948,%eax
  401db6:	c3                   	retq

0000000000401db7 <setval_430>:
  401db7:	c7 07 4a 09 ca 90    	movl   $0x90ca094a,(%rdi)
  401dbd:	c3                   	retq

0000000000401dbe <getval_168>:
  401dbe:	b8 89 ca 20 db       	mov    $0xdb20ca89,%eax
  401dc3:	c3                   	retq

0000000000401dc4 <getval_337>:
  401dc4:	b8 89 ca 28 db       	mov    $0xdb28ca89,%eax
  401dc9:	c3                   	retq

0000000000401dca <setval_190>:
  401dca:	c7 07 c9 c1 84 db    	movl   $0xdb84c1c9,(%rdi)
  401dd0:	c3                   	retq

0000000000401dd1 <addval_479>:
  401dd1:	8d 87 89 d6 90 c3    	lea    -0x3c6f2977(%rdi),%eax
  401dd7:	c3                   	retq

0000000000401dd8 <getval_448>:
  401dd8:	b8 89 ca c3 99       	mov    $0x99c3ca89,%eax
  401ddd:	c3                   	retq

0000000000401dde <addval_105>:
  401dde:	8d 87 48 89 e0 92    	lea    -0x6d1f76b8(%rdi),%eax
  401de4:	c3                   	retq

0000000000401de5 <getval_267>:
  401de5:	b8 b2 48 89 e0       	mov    $0xe08948b2,%eax
  401dea:	c3                   	retq

0000000000401deb <getval_311>:
  401deb:	b8 09 d6 90 c3       	mov    $0xc390d609,%eax
  401df0:	c3                   	retq

0000000000401df1 <addval_404>:
  401df1:	8d 87 48 8b e0 c3    	lea    -0x3c1f74b8(%rdi),%eax
  401df7:	c3                   	retq

0000000000401df8 <setval_115>:
  401df8:	c7 07 a9 d6 08 c0    	movl   $0xc008d6a9,(%rdi)
  401dfe:	c3                   	retq

0000000000401dff <addval_306>:
  401dff:	8d 87 99 ca 20 d2    	lea    -0x2ddf3567(%rdi),%eax
  401e05:	c3                   	retq

0000000000401e06 <addval_170>:
  401e06:	8d 87 48 89 e0 94    	lea    -0x6b1f76b8(%rdi),%eax
  401e0c:	c3                   	retq

0000000000401e0d <addval_257>:
  401e0d:	8d 87 89 c1 18 db    	lea    -0x24e73e77(%rdi),%eax
  401e13:	c3                   	retq

0000000000401e14 <addval_325>:
  401e14:	8d 87 8d ca 08 c9    	lea    -0x36f73573(%rdi),%eax
  401e1a:	c3                   	retq

0000000000401e1b <getval_109>:
  401e1b:	b8 99 d6 84 c9       	mov    $0xc984d699,%eax
  401e20:	c3                   	retq

0000000000401e21 <setval_488>:
  401e21:	c7 07 a1 a9 ca 90    	movl   $0x90caa9a1,(%rdi)
  401e27:	c3                   	retq

0000000000401e28 <end_farm>:
...

Course WorkAttack LabComputer Organization and ArchitectureAbout 4 minAbout 1113 words
Bomb Lab: Phase 1

Assembly

0000000000401440 <phase_1>:
b0:
  401440:	48 83 ec 08          	sub    $0x8,%rsp
  401444:	48 8d 35 05 1d 00 00 	lea    0x1d05(%rip),%rsi        # 403150 <_IO_stdin_used+0x150>
  40144b:	e8 43 04 00 00       	callq  401893 <strings_not_equal>
  401450:	85 c0                	test   %eax,%eax
  401452:	74 05                	je     401459 <phase_1+0x19>
  401454:	e8 03 07 00 00       	callq  401b5c <explode_bomb>
b1:
  401459:	48 83 c4 08          	add    $0x8,%rsp
  40145d:	c3                   	retq

Course WorkBomb LabComputer Organization and ArchitectureLess than 1 minuteAbout 163 words
Bomb Lab: Phase 2

Assembly

000000000040145e <phase_2>:
b0:
  40145e:	55                   	push   %rbp
  40145f:	53                   	push   %rbx
  401460:	48 83 ec 28          	sub    $0x28,%rsp
  401464:	48 89 e6             	mov    %rsp,%rsi
  401467:	e8 2c 07 00 00       	callq  401b98 <read_six_numbers>
  40146c:	83 3c 24 00          	cmpl   $0x0,(%rsp)
  401470:	75 07                	jne    401479 <phase_2+0x1b>
  401472:	83 7c 24 04 01       	cmpl   $0x1,0x4(%rsp)
  401477:	74 23                	je     40149c <phase_2+0x3e>
b1:
  401479:	e8 de 06 00 00       	callq  401b5c <explode_bomb>
  40147e:	66 90                	xchg   %ax,%ax
  401480:	eb 1a                	jmp    40149c <phase_2+0x3e>
b2:
  401482:	8b 43 f8             	mov    -0x8(%rbx),%eax
  401485:	03 43 fc             	add    -0x4(%rbx),%eax
  401488:	39 03                	cmp    %eax,(%rbx)
  40148a:	74 05                	je     401491 <phase_2+0x33>
  40148c:	e8 cb 06 00 00       	callq  401b5c <explode_bomb>
b3:
  401491:	48 83 c3 04          	add    $0x4,%rbx
  401495:	48 39 eb             	cmp    %rbp,%rbx
  401498:	75 e8                	jne    401482 <phase_2+0x24>
  40149a:	eb 0e                	jmp    4014aa <phase_2+0x4c>
b4:
  40149c:	48 89 e5             	mov    %rsp,%rbp
  40149f:	48 8d 5c 24 08       	lea    0x8(%rsp),%rbx
  4014a4:	48 83 c5 18          	add    $0x18,%rbp
  4014a8:	eb d8                	jmp    401482 <phase_2+0x24>
b5:
  4014aa:	48 83 c4 28          	add    $0x28,%rsp
  4014ae:	5b                   	pop    %rbx
  4014af:	5d                   	pop    %rbp
  4014b0:	c3                   	retq

Course WorkBomb LabComputer Organization and ArchitectureAbout 1 minAbout 376 words
Bomb Lab: Phase 3

Assembly

00000000004014b1 <phase_3>:
b0:
  4014b1:	48 83 ec 18          	sub    $0x18,%rsp
  4014b5:	48 8d 4c 24 0c       	lea    0xc(%rsp),%rcx
  4014ba:	48 8d 54 24 08       	lea    0x8(%rsp),%rdx
  4014bf:	48 8d 35 6f 1f 00 00 	lea    0x1f6f(%rip),%rsi        # 403435 <array.3354+0x255>
  4014c6:	b8 00 00 00 00       	mov    $0x0,%eax
  4014cb:	e8 60 fc ff ff       	callq  401130 <__isoc99_sscanf@plt>
  4014d0:	83 f8 01             	cmp    $0x1,%eax
  4014d3:	7f 05                	jg     4014da <phase_3+0x29>
  4014d5:	e8 82 06 00 00       	callq  401b5c <explode_bomb>
b1:
  4014da:	83 7c 24 08 07       	cmpl   $0x7,0x8(%rsp)
  4014df:	77 6f                	ja     401550 <phase_3+0x9f>
  4014e1:	8b 54 24 08          	mov    0x8(%rsp),%edx
  4014e5:	48 8d 05 d4 1c 00 00 	lea    0x1cd4(%rip),%rax        # 4031c0 <_IO_stdin_used+0x1c0>
  4014ec:	48 63 14 90          	movslq (%rax,%rdx,4),%rdx
  4014f0:	48 01 d0             	add    %rdx,%rax
  4014f3:	ff e0                	jmpq   *%rax
  4014f5:	b8 00 00 00 00       	mov    $0x0,%eax
  4014fa:	eb 05                	jmp    401501 <phase_3+0x50>
  4014fc:	b8 cb 02 00 00       	mov    $0x2cb,%eax
b2:
  401501:	2d 4c 02 00 00       	sub    $0x24c,%eax
  401506:	eb 05                	jmp    40150d <phase_3+0x5c>
  401508:	b8 00 00 00 00       	mov    $0x0,%eax
b3:
  40150d:	05 ba 03 00 00       	add    $0x3ba,%eax
  401512:	eb 05                	jmp    401519 <phase_3+0x68>
  401514:	b8 00 00 00 00       	mov    $0x0,%eax
b4:
  401519:	2d 79 03 00 00       	sub    $0x379,%eax
  40151e:	eb 05                	jmp    401525 <phase_3+0x74>
  401520:	b8 00 00 00 00       	mov    $0x0,%eax
b5:
  401525:	05 79 03 00 00       	add    $0x379,%eax
  40152a:	eb 05                	jmp    401531 <phase_3+0x80>
  40152c:	b8 00 00 00 00       	mov    $0x0,%eax
b6:
  401531:	2d 79 03 00 00       	sub    $0x379,%eax
  401536:	eb 05                	jmp    40153d <phase_3+0x8c>
  401538:	b8 00 00 00 00       	mov    $0x0,%eax
b7:
  40153d:	05 79 03 00 00       	add    $0x379,%eax
  401542:	eb 05                	jmp    401549 <phase_3+0x98>
  401544:	b8 00 00 00 00       	mov    $0x0,%eax
b8:
  401549:	2d 79 03 00 00       	sub    $0x379,%eax
  40154e:	eb 0a                	jmp    40155a <phase_3+0xa9>
b9:
  401550:	e8 07 06 00 00       	callq  401b5c <explode_bomb>
  401555:	b8 00 00 00 00       	mov    $0x0,%eax
b10:
  40155a:	83 7c 24 08 05       	cmpl   $0x5,0x8(%rsp)
  40155f:	7f 06                	jg     401567 <phase_3+0xb6>
  401561:	3b 44 24 0c          	cmp    0xc(%rsp),%eax
  401565:	74 05                	je     40156c <phase_3+0xbb>
b11:
  401567:	e8 f0 05 00 00       	callq  401b5c <explode_bomb>
b12:
  40156c:	48 83 c4 18          	add    $0x18,%rsp
  401570:	c3                   	retq

Course WorkBomb LabComputer Organization and ArchitectureAbout 6 minAbout 1853 words