SlideShare a Scribd company logo


!2
!3
!4
!5
!6
!7
!8
!9
!10
!11
!12
#include <stdio.h>
int main()
{
printf("hello world!n");
return 0;
}
!13
$ ls
helloworld helloworld.c
$ file helloworld.c
# helloworld.c: C source, ASCII text
$ file helloworld
# helloworld: ELF 64-bit LSB pie executable,
x86-64, version 1 (SYSV), dynamically linked,
interpreter /lib64/ld-linux-x86-64.so.2, for
GNU/Linux 3.2.0,
BuildID[sha1]=02ed1e530f48620e34b1abc02079fc1
17ebb2279, not stripped
!14
$ ls -alh
# -rwxr-xr-x 1 root root 17K Nov 4 23:21
helloworld
# -rw-r--r-- 1 root root 62 Nov 4 23:20
helloworld.c
!15
$ cat helloworld
{¥rtf1¥ansi
¥deff1{¥fonttbl{¥f1¥fmodern¥fprq1¥fcharset0
Courier New;}}{¥colortbl;
¥red224¥green234¥blue238;¥red00¥green00¥blue0
0;¥red191¥green03¥blue03;¥red176¥green126¥blu
e00;¥red131¥green129¥blue131;¥red131¥green129
¥blue131;¥red255¥green00¥blue255;¥red00¥green
130¥blue00;¥red12
……
!16
$ gdb helloworld
(gdb) disass main
# Dump of assembler code for function main:
# 0x0000000000001135 <+0>: push rbp
# 0x0000000000001136 <+1>: mov rbp,rsp
# 0x0000000000001139 <+4>: lea rdi,
[rip+0xec4] # 0x2004
# 0x0000000000001140 <+11>: mov eax,0x0
# 0x0000000000001145 <+16>: call 0x1030
<printf@plt>
# 0x000000000000114a <+21>: mov eax,0x0
# 0x000000000000114f <+26>: pop rbp
# 0x0000000000001150 <+27>: ret
# End of assembler dump.
!17
$ gdb helloworld
(gdb) disass main
# Dump of assembler code for function main:
# 0x0000000000001135 <+0>: push rbp
# 0x0000000000001136 <+1>: mov rbp,rsp
# 0x0000000000001139 <+4>: lea rdi,
[rip+0xec4] # 0x2004
# 0x0000000000001140 <+11>: mov eax,0x0
# 0x0000000000001145 <+16>: call 0x1030
<printf@plt>
# 0x000000000000114a <+21>: mov eax,0x0
# 0x000000000000114f <+26>: pop rbp
# 0x0000000000001150 <+27>: ret
# End of assembler dump.
!18
$ file helloworldmac
helloworldmac: Mach-O 64-bit x86_64
executable, flags:<NOUNDEFS|DYLDLINK|
TWOLEVEL|PIE>
!19
$ ./helloworldmac
zsh: exec format error: ./helloworldmac
!20
!21
!22
!23
!24
(gdb) disass main
Dump of assembler code for function main:
0x0000000000001135 <+0>: push rbp
0x0000000000001136 <+1>: mov rbp,rsp
0x0000000000001139 <+4>: lea rdi,
[rip+0xec4] # 0x2004
0x0000000000001140 <+11>:mov eax,0x0
0x0000000000001145 <+16>:call 0x1030
<printf@plt>
0x000000000000114a <+21>:mov eax,0x0
0x000000000000114f <+26>:pop rbp
0x0000000000001150 <+27>:ret
End of assembler dump.
!25
!26
!27
!28
!29
!30


!31
!32
void test_function(int a, int b, int c, int d)
{
int flag;
char buffer[10];
flag = 31337; //
buffer[0] = 'A';
}
int main()
{
test_function(1, 2, 3, 4); //
}
!33
gdb -q ./stack_example
Reading symbols from ./stack_example...done.
(gdb) list
1
2 void test_function(int a, int b, int c, int d)
3 {
4 int flag;
5 char buffer[10];
6
7 flag = 31337;
8 buffer[0] = 'A';
9 }
10
(gdb) list
11 int main()
12 {
13 test_function(1, 2, 3, 4);
14 test_function(5,6,7,8);
15 }
16
(gdb) break 13
Breakpoint 1 at 0x1147: file stack_example.c, line 13.
(gdb) break test_function
Breakpoint 2 at 0x1135: file stack_example.c, line 7.
(gdb) break 14
Breakpoint 3 at 0x1160: file stack_example.c, line 14.
!34
(gdb) run
Starting program: /root/rg/stack_example
Breakpoint 1, main () at stack_example.c:13
13 test_function(1, 2, 3, 4);
(gdb) i r rsp rbp
rsp 0x7fffffffe350 0x7fffffffe350
rbp 0x7fffffffe350 0x7fffffffe350
(gdb) cont
Continuing.
Breakpoint 2, test_function (a=1, b=2, c=3, d=4) at stack_example.c:7
7 flag = 31337;
(gdb) i r rsp rbp
rsp 0x7fffffffe340 0x7fffffffe340
rbp 0x7fffffffe340 0x7fffffffe340
(gdb) cont
Continuing.
Breakpoint 3, main () at stack_example.c:14
14 test_function(5,6,7,8);
(gdb) i r rsp rbp
rsp 0x7fffffffe350 0x7fffffffe350
rbp 0x7fffffffe350 0x7fffffffe350
!35
$ gcc -g -o memory_segments memory_seguments.c
!36
$ ./memory_segments
data segment:
global_init_var 0xaecf0040
static_init_var 0xaecf0044
bss segment
global_var 0xaecf0050
static_var 0xaecf004c
heap segment
heap_var_ptr 0xb098c260
stack segment
stack_var 0x9fefb384
function() stack_var 0x9fefb36c
!37
!38
!39
#include <stdio.h>
int main()
{
printf("hello world!n");
return 0;
}
!40


!41
!42
$ strace ./helloworld

execve("./helloworld", ["./helloworld"], 0x7ffc2b36e160 /*
29 vars */) = 0
brk(NULL) = 0x55f838726000
.
.
.
mprotect(0x7f313a6c9000, 4096, PROT_READ) = 0
munmap(0x7f313a67e000, 145183) = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136,
0), ...}) = 0
brk(NULL) = 0x55f838726000
brk(0x55f838747000) = 0x55f838747000
write(1, "hello world!", 12hello world!) = 12
exit_group(0) = ?
+++ exited with 0 +++
!43
!44
!45
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd1, fd2;
fd1 = open("helloworld.c", O_RDONLY);
close(fd1);
fd2 = open("helloworld.c", O_RDONLY);
close(fd2);
printf("fd1 = %d, fd2 = %dn", fd1, fd2);
fd1 = open("helloworld.c", O_RDONLY);
fd2 = open("helloworld.c", O_RDONLY);
close(fd1);
close(fd2);
printf("fd1 = %d, fd2 = %dn", fd1, fd2);
}
$ ./a.out
fd1 = 3, fd2 = 3
fd1 = 3, fd2 = 4
!46
cat tatsu-cat.c
#include <fcntl.h>
#include <unistd.h>
#define N 1024
int main(int argc, char *argv[])
{
int fd, buflen;
char buf[N];
if (argc < 2)
_exit(1);
for (int i = 1; i < argc; i++)
{
if ((fd = open(argv[i], O_RDONLY)) < 0)
{
close(fd);
continue;
}
while ((buflen = read(fd, buf, N)) > 0)
write(1, buf, buflen);
close(fd);
}
return 0;
}
!47
!48
$ gdb helloworld
(gdb) disass main
# Dump of assembler code for function main:
# 0x0000000000001135 <+0>: push rbp
# 0x0000000000001136 <+1>: mov rbp,rsp
# 0x0000000000001139 <+4>: lea rdi,
[rip+0xec4] # 0x2004
# 0x0000000000001140 <+11>: mov eax,0x0
# 0x0000000000001145 <+16>: call 0x1030
<printf@plt>
# 0x000000000000114a <+21>: mov eax,0x0
# 0x000000000000114f <+26>: pop rbp
# 0x0000000000001150 <+27>: ret
# End of assembler dump.
!49
$ gdb helloworld
(gdb) disass main
# Dump of assembler code for function main:
# 0x0000000000001135 <+0>: push rbp
#
!50
$ gdb helloworld
(gdb) disass main
# Dump of assembler code for function main:
# 0x0000000000001135 <+0>: push rbp
#
!51




!52
!53
!54
!55
!56
!57
!58
!59
!60
!61
!62
!63
!64
!65

More Related Content

What's hot

JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyYasuharu Nakano
 
NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練
NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練
NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練
Sheng-Hao Ma
 
Controlling Arduino With PHP
Controlling Arduino With PHPControlling Arduino With PHP
Controlling Arduino With PHPThomas Weinert
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
Ian Barber
 
How to write rust instead of c and get away with it
How to write rust instead of c and get away with itHow to write rust instead of c and get away with it
How to write rust instead of c and get away with it
Flavien Raynaud
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...akaptur
 
Jakarta Commons - Don't re-invent the wheel
Jakarta Commons - Don't re-invent the wheelJakarta Commons - Don't re-invent the wheel
Jakarta Commons - Don't re-invent the wheel
tcurdt
 
GDG DevFest Kyoto 2014 これからのGoの話をしよう
GDG DevFest Kyoto 2014 これからのGoの話をしようGDG DevFest Kyoto 2014 これからのGoの話をしよう
GDG DevFest Kyoto 2014 これからのGoの話をしよう
Satoshi Noda
 
Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)
hasan0812
 
No dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real worldNo dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real world
tcurdt
 
Codes
CodesCodes
我在豆瓣使用Emacs
我在豆瓣使用Emacs我在豆瓣使用Emacs
我在豆瓣使用Emacs
董 伟明
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
Ian Barber
 
Using the Power to Prove
Using the Power to ProveUsing the Power to Prove
Using the Power to Prove
Kazuho Oku
 
2² C# 4.0 and .NET 4 Selected Features
2² C# 4.0 and .NET 4 Selected Features2² C# 4.0 and .NET 4 Selected Features
2² C# 4.0 and .NET 4 Selected Features
Mustafa Isik
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
Ian Barber
 

What's hot (20)

C99[2]
C99[2]C99[2]
C99[2]
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練
NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練
NTUSTxTDOH 資訊安全基礎工作坊 基礎逆向教育訓練
 
Controlling Arduino With PHP
Controlling Arduino With PHPControlling Arduino With PHP
Controlling Arduino With PHP
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
 
Vcs28
Vcs28Vcs28
Vcs28
 
How to write rust instead of c and get away with it
How to write rust instead of c and get away with itHow to write rust instead of c and get away with it
How to write rust instead of c and get away with it
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
 
Jakarta Commons - Don't re-invent the wheel
Jakarta Commons - Don't re-invent the wheelJakarta Commons - Don't re-invent the wheel
Jakarta Commons - Don't re-invent the wheel
 
GDG DevFest Kyoto 2014 これからのGoの話をしよう
GDG DevFest Kyoto 2014 これからのGoの話をしようGDG DevFest Kyoto 2014 これからのGoの話をしよう
GDG DevFest Kyoto 2014 これからのGoの話をしよう
 
Chat code
Chat codeChat code
Chat code
 
Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)
 
No dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real worldNo dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real world
 
Codes
CodesCodes
Codes
 
我在豆瓣使用Emacs
我在豆瓣使用Emacs我在豆瓣使用Emacs
我在豆瓣使用Emacs
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Using the Power to Prove
Using the Power to ProveUsing the Power to Prove
Using the Power to Prove
 
2² C# 4.0 and .NET 4 Selected Features
2² C# 4.0 and .NET 4 Selected Features2² C# 4.0 and .NET 4 Selected Features
2² C# 4.0 and .NET 4 Selected Features
 
Arp
ArpArp
Arp
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 

Similar to プログラム実行の話と
OSとメモリの挙動の話

Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
Ji Hun Kim
 
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
Masashi Shibata
 
Load-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOAD
Dharmalingam Ganesan
 
Software to the slaughter
Software to the slaughterSoftware to the slaughter
Software to the slaughter
Quinn Wilton
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
Functional Thursday
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
Michiel Borkent
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
corehard_by
 
Naughty And Nice Bash Features
Naughty And Nice Bash FeaturesNaughty And Nice Bash Features
Naughty And Nice Bash Features
Nati Cohen
 
SFO15-500: VIXL
SFO15-500: VIXLSFO15-500: VIXL
SFO15-500: VIXL
Linaro
 
Modern technologies in data science
Modern technologies in data science Modern technologies in data science
Modern technologies in data science
Chucheng Hsieh
 
Common Intermediate Language (.NET) by Example
Common Intermediate Language (.NET) by ExampleCommon Intermediate Language (.NET) by Example
Common Intermediate Language (.NET) by Example
Ganesh Samarthyam
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
Appier
 
Exploring the x64
Exploring the x64Exploring the x64
Exploring the x64FFRI, Inc.
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
akaptur
 

Similar to プログラム実行の話と
OSとメモリの挙動の話 (20)

Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
 
Marat-Slides
Marat-SlidesMarat-Slides
Marat-Slides
 
3
33
3
 
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
システムコールトレーサーの動作原理と実装 (Writing system call tracer for Linux/x86)
 
Load-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOAD
 
Software to the slaughter
Software to the slaughterSoftware to the slaughter
Software to the slaughter
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
 
Vcs23
Vcs23Vcs23
Vcs23
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
 
Naughty And Nice Bash Features
Naughty And Nice Bash FeaturesNaughty And Nice Bash Features
Naughty And Nice Bash Features
 
SFO15-500: VIXL
SFO15-500: VIXLSFO15-500: VIXL
SFO15-500: VIXL
 
Modern technologies in data science
Modern technologies in data science Modern technologies in data science
Modern technologies in data science
 
Vcs16
Vcs16Vcs16
Vcs16
 
Common Intermediate Language (.NET) by Example
Common Intermediate Language (.NET) by ExampleCommon Intermediate Language (.NET) by Example
Common Intermediate Language (.NET) by Example
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
 
20141106 asfws unicode_hacks
20141106 asfws unicode_hacks20141106 asfws unicode_hacks
20141106 asfws unicode_hacks
 
Exploring the x64
Exploring the x64Exploring the x64
Exploring the x64
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
Stop Monkeys Fall
Stop Monkeys FallStop Monkeys Fall
Stop Monkeys Fall
 

Recently uploaded

How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
KrzysztofKkol1
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 

Recently uploaded (20)

How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 

プログラム実行の話と
OSとメモリの挙動の話

  • 1.
  • 2. !2
  • 3. !3
  • 4. !4
  • 5. !5
  • 6. !6
  • 7. !7
  • 8. !8
  • 9. !9
  • 10. !10
  • 11. !11
  • 12. !12
  • 13. #include <stdio.h> int main() { printf("hello world!n"); return 0; } !13
  • 14. $ ls helloworld helloworld.c $ file helloworld.c # helloworld.c: C source, ASCII text $ file helloworld # helloworld: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=02ed1e530f48620e34b1abc02079fc1 17ebb2279, not stripped !14
  • 15. $ ls -alh # -rwxr-xr-x 1 root root 17K Nov 4 23:21 helloworld # -rw-r--r-- 1 root root 62 Nov 4 23:20 helloworld.c !15
  • 16. $ cat helloworld {¥rtf1¥ansi ¥deff1{¥fonttbl{¥f1¥fmodern¥fprq1¥fcharset0 Courier New;}}{¥colortbl; ¥red224¥green234¥blue238;¥red00¥green00¥blue0 0;¥red191¥green03¥blue03;¥red176¥green126¥blu e00;¥red131¥green129¥blue131;¥red131¥green129 ¥blue131;¥red255¥green00¥blue255;¥red00¥green 130¥blue00;¥red12 …… !16
  • 17. $ gdb helloworld (gdb) disass main # Dump of assembler code for function main: # 0x0000000000001135 <+0>: push rbp # 0x0000000000001136 <+1>: mov rbp,rsp # 0x0000000000001139 <+4>: lea rdi, [rip+0xec4] # 0x2004 # 0x0000000000001140 <+11>: mov eax,0x0 # 0x0000000000001145 <+16>: call 0x1030 <printf@plt> # 0x000000000000114a <+21>: mov eax,0x0 # 0x000000000000114f <+26>: pop rbp # 0x0000000000001150 <+27>: ret # End of assembler dump. !17
  • 18. $ gdb helloworld (gdb) disass main # Dump of assembler code for function main: # 0x0000000000001135 <+0>: push rbp # 0x0000000000001136 <+1>: mov rbp,rsp # 0x0000000000001139 <+4>: lea rdi, [rip+0xec4] # 0x2004 # 0x0000000000001140 <+11>: mov eax,0x0 # 0x0000000000001145 <+16>: call 0x1030 <printf@plt> # 0x000000000000114a <+21>: mov eax,0x0 # 0x000000000000114f <+26>: pop rbp # 0x0000000000001150 <+27>: ret # End of assembler dump. !18
  • 19. $ file helloworldmac helloworldmac: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS|DYLDLINK| TWOLEVEL|PIE> !19
  • 20. $ ./helloworldmac zsh: exec format error: ./helloworldmac !20
  • 21. !21
  • 22. !22
  • 23. !23
  • 24. !24
  • 25. (gdb) disass main Dump of assembler code for function main: 0x0000000000001135 <+0>: push rbp 0x0000000000001136 <+1>: mov rbp,rsp 0x0000000000001139 <+4>: lea rdi, [rip+0xec4] # 0x2004 0x0000000000001140 <+11>:mov eax,0x0 0x0000000000001145 <+16>:call 0x1030 <printf@plt> 0x000000000000114a <+21>:mov eax,0x0 0x000000000000114f <+26>:pop rbp 0x0000000000001150 <+27>:ret End of assembler dump. !25
  • 26. !26
  • 27. !27
  • 28. !28
  • 29. !29
  • 30. !30
  • 32. !32
  • 33. void test_function(int a, int b, int c, int d) { int flag; char buffer[10]; flag = 31337; // buffer[0] = 'A'; } int main() { test_function(1, 2, 3, 4); // } !33
  • 34. gdb -q ./stack_example Reading symbols from ./stack_example...done. (gdb) list 1 2 void test_function(int a, int b, int c, int d) 3 { 4 int flag; 5 char buffer[10]; 6 7 flag = 31337; 8 buffer[0] = 'A'; 9 } 10 (gdb) list 11 int main() 12 { 13 test_function(1, 2, 3, 4); 14 test_function(5,6,7,8); 15 } 16 (gdb) break 13 Breakpoint 1 at 0x1147: file stack_example.c, line 13. (gdb) break test_function Breakpoint 2 at 0x1135: file stack_example.c, line 7. (gdb) break 14 Breakpoint 3 at 0x1160: file stack_example.c, line 14. !34
  • 35. (gdb) run Starting program: /root/rg/stack_example Breakpoint 1, main () at stack_example.c:13 13 test_function(1, 2, 3, 4); (gdb) i r rsp rbp rsp 0x7fffffffe350 0x7fffffffe350 rbp 0x7fffffffe350 0x7fffffffe350 (gdb) cont Continuing. Breakpoint 2, test_function (a=1, b=2, c=3, d=4) at stack_example.c:7 7 flag = 31337; (gdb) i r rsp rbp rsp 0x7fffffffe340 0x7fffffffe340 rbp 0x7fffffffe340 0x7fffffffe340 (gdb) cont Continuing. Breakpoint 3, main () at stack_example.c:14 14 test_function(5,6,7,8); (gdb) i r rsp rbp rsp 0x7fffffffe350 0x7fffffffe350 rbp 0x7fffffffe350 0x7fffffffe350 !35
  • 36. $ gcc -g -o memory_segments memory_seguments.c !36
  • 37. $ ./memory_segments data segment: global_init_var 0xaecf0040 static_init_var 0xaecf0044 bss segment global_var 0xaecf0050 static_var 0xaecf004c heap segment heap_var_ptr 0xb098c260 stack segment stack_var 0x9fefb384 function() stack_var 0x9fefb36c !37
  • 38. !38
  • 39. !39
  • 40. #include <stdio.h> int main() { printf("hello world!n"); return 0; } !40
  • 42. !42
  • 43. $ strace ./helloworld
 execve("./helloworld", ["./helloworld"], 0x7ffc2b36e160 /* 29 vars */) = 0 brk(NULL) = 0x55f838726000 . . . mprotect(0x7f313a6c9000, 4096, PROT_READ) = 0 munmap(0x7f313a67e000, 145183) = 0 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0 brk(NULL) = 0x55f838726000 brk(0x55f838747000) = 0x55f838747000 write(1, "hello world!", 12hello world!) = 12 exit_group(0) = ? +++ exited with 0 +++ !43
  • 44. !44
  • 45. !45
  • 46. #include <stdio.h> #include <fcntl.h> #include <unistd.h> int main() { int fd1, fd2; fd1 = open("helloworld.c", O_RDONLY); close(fd1); fd2 = open("helloworld.c", O_RDONLY); close(fd2); printf("fd1 = %d, fd2 = %dn", fd1, fd2); fd1 = open("helloworld.c", O_RDONLY); fd2 = open("helloworld.c", O_RDONLY); close(fd1); close(fd2); printf("fd1 = %d, fd2 = %dn", fd1, fd2); } $ ./a.out fd1 = 3, fd2 = 3 fd1 = 3, fd2 = 4 !46
  • 47. cat tatsu-cat.c #include <fcntl.h> #include <unistd.h> #define N 1024 int main(int argc, char *argv[]) { int fd, buflen; char buf[N]; if (argc < 2) _exit(1); for (int i = 1; i < argc; i++) { if ((fd = open(argv[i], O_RDONLY)) < 0) { close(fd); continue; } while ((buflen = read(fd, buf, N)) > 0) write(1, buf, buflen); close(fd); } return 0; } !47
  • 48. !48
  • 49. $ gdb helloworld (gdb) disass main # Dump of assembler code for function main: # 0x0000000000001135 <+0>: push rbp # 0x0000000000001136 <+1>: mov rbp,rsp # 0x0000000000001139 <+4>: lea rdi, [rip+0xec4] # 0x2004 # 0x0000000000001140 <+11>: mov eax,0x0 # 0x0000000000001145 <+16>: call 0x1030 <printf@plt> # 0x000000000000114a <+21>: mov eax,0x0 # 0x000000000000114f <+26>: pop rbp # 0x0000000000001150 <+27>: ret # End of assembler dump. !49
  • 50. $ gdb helloworld (gdb) disass main # Dump of assembler code for function main: # 0x0000000000001135 <+0>: push rbp # !50
  • 51. $ gdb helloworld (gdb) disass main # Dump of assembler code for function main: # 0x0000000000001135 <+0>: push rbp # !51
  • 53. !53
  • 54. !54
  • 55. !55
  • 56. !56
  • 57. !57
  • 58. !58
  • 59. !59
  • 60. !60
  • 61. !61
  • 62. !62
  • 63. !63
  • 64. !64
  • 65. !65