First part of TD4
This commit is contained in:
parent
3c2301f8b6
commit
8e3fb6fe6d
8 changed files with 201 additions and 0 deletions
|
@ -0,0 +1,7 @@
|
||||||
|
int foo (int x) {
|
||||||
|
int i = 10;
|
||||||
|
return i+2;
|
||||||
|
}
|
||||||
|
int main () {
|
||||||
|
return foo (1);
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
void foo (int a, int b, int c, int d, int e, int f) {
|
||||||
|
int i = 10 + a + b + c + d + e + f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
foo (1, 2, 3, 4, 5, 6);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
void foo (float a, int b, int c, int d, int e, int f, int g) {
|
||||||
|
float i = 10 + a + b + c + d + e + f + g;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
foo (1, 2, 3, 4, 5, 6, 7);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
void foo (int a, int b, int c, int d, int e, int f, int g) {
|
||||||
|
int i = 10 + a + b + c + d + e + f + g;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
foo (1, 2, 3, 4, 5, 6, 7);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
void foo (float a, int b, int c, int d, int e, int f, int g, int h) {
|
||||||
|
float i = 10 + a + b + c + d + e + f + g + h;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
foo (1, 2, 3, 4, 5, 6, 7, 8);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
void foo (int a, int b, int c, int d, int e, int f, int g, int k, int l) {
|
||||||
|
int i = 10 + a + b + c + d + e + f + g + k + l;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
foo (1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra -no-pie -lm -g -std=c99
|
||||||
|
SRC = $(wildcard *.c)
|
||||||
|
TGT = $(subst .c,,$(SRC))
|
||||||
|
BUILD_DIR = build
|
||||||
|
DUMP_DIR = dump
|
||||||
|
|
||||||
|
$(BUILD_DIR)/%_32: %.c
|
||||||
|
$(shell mkdir -p $(BUILD_DIR))
|
||||||
|
$(CC) $(CFLAGS) -m32 -o $@ $<
|
||||||
|
|
||||||
|
|
||||||
|
$(BUILD_DIR)/%_64: %.c
|
||||||
|
$(shell mkdir -p $(BUILD_DIR))
|
||||||
|
$(CC) $(CFLAGS) -m64 -o $@ $<
|
||||||
|
|
||||||
|
$(DUMP_DIR)/%_32.txt: $(BUILD_DIR)/%_32
|
||||||
|
$(shell mkdir -p $(DUMP_DIR))
|
||||||
|
objdump -Sd $< > $@
|
||||||
|
|
||||||
|
$(DUMP_DIR)/%_64.txt: $(BUILD_DIR)/%_64
|
||||||
|
$(shell mkdir -p $(DUMP_DIR))
|
||||||
|
objdump -Sd $< > $@
|
||||||
|
|
||||||
|
build: $(addprefix $(BUILD_DIR)/, $(addsuffix _32, $(TGT))) \
|
||||||
|
$(addprefix $(BUILD_DIR)/, $(addsuffix _64, $(TGT)))
|
||||||
|
|
||||||
|
|
||||||
|
dump: $(addprefix $(DUMP_DIR)/, $(addsuffix _32.txt, $(TGT))) \
|
||||||
|
$(addprefix $(DUMP_DIR)/, $(addsuffix _64.txt, $(TGT)))
|
||||||
|
|
||||||
|
|
||||||
|
PHONY: clean
|
||||||
|
clean:
|
||||||
|
@rm -rf $(BUILD_DIR)
|
||||||
|
@rm -rf $(DUMP_DIR)
|
118
content/secu_logicielle/td4-assembleur_x86_part2/index.md
Normal file
118
content/secu_logicielle/td4-assembleur_x86_part2/index.md
Normal file
|
@ -0,0 +1,118 @@
|
||||||
|
---
|
||||||
|
title: "Sécurité logicielle : TD 4 assembleur x86 partie 2"
|
||||||
|
date: 2023-02-10
|
||||||
|
tags: ["Assembleur", "x86"]
|
||||||
|
categories: ["Sécurité logicielle", "TD"]
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question 1
|
||||||
|
|
||||||
|
### partie 4
|
||||||
|
|
||||||
|
D'après les nos tests, le code assembleur généré utilise la pile pour y
|
||||||
|
positionner les paramètres de notre fonction `foo` à partir de 7 entiers. Le
|
||||||
|
code assembler généré pour 6 montre bien que les paramètres sont copiés des
|
||||||
|
regitres vers la pile:
|
||||||
|
|
||||||
|
```asm {linenos=inline}
|
||||||
|
# 0000000000401106 <foo>:
|
||||||
|
# void foo (int a, int b, int c, int d, int e, int f) {
|
||||||
|
push %rbp
|
||||||
|
mov %rsp,%rbp
|
||||||
|
mov %edi,-0x14(%rbp)
|
||||||
|
mov %esi,-0x18(%rbp)
|
||||||
|
mov %edx,-0x1c(%rbp)
|
||||||
|
mov %ecx,-0x20(%rbp)
|
||||||
|
mov %r8d,-0x24(%rbp)
|
||||||
|
mov %r9d,-0x28(%rbp)
|
||||||
|
# int i = 10 + a + b + c + d + e + f;
|
||||||
|
mov -0x14(%rbp),%eax
|
||||||
|
lea 0xa(%rax),%edx
|
||||||
|
mov -0x18(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov -0x1c(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov -0x20(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov -0x24(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov -0x28(%rbp),%eax
|
||||||
|
add %edx,%eax
|
||||||
|
mov %eax,-0x4(%rbp)
|
||||||
|
#}
|
||||||
|
nop
|
||||||
|
pop %rbp
|
||||||
|
ret
|
||||||
|
```
|
||||||
|
|
||||||
|
Voici le code pour 7 entiers, on peut voir que l'initialisation des paramètres
|
||||||
|
de notre fonction `foo` seul **6 paramètres** sont présent dans les
|
||||||
|
**registres** (ligne 10). On vois que le septième **est pris directement dans la
|
||||||
|
**pile** (ligne 25-25).
|
||||||
|
|
||||||
|
```asm {linenos=inline, hl_lines=[10, "24-25"]}
|
||||||
|
# 0000000000401106 <foo>:
|
||||||
|
# void foo (int a, int b, int c, int d, int e, int f, int g) {
|
||||||
|
push %rbp
|
||||||
|
mov %rsp,%rbp
|
||||||
|
mov %edi,-0x14(%rbp)
|
||||||
|
mov %esi,-0x18(%rbp)
|
||||||
|
mov %edx,-0x1c(%rbp)
|
||||||
|
mov %ecx,-0x20(%rbp)
|
||||||
|
mov %r8d,-0x24(%rbp)
|
||||||
|
mov %r9d,-0x28(%rbp)
|
||||||
|
# int i = 10 + a + b + c + d + e + f + g;
|
||||||
|
mov -0x14(%rbp),%eax
|
||||||
|
lea 0xa(%rax),%edx
|
||||||
|
mov -0x18(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov -0x1c(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov -0x20(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov -0x24(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov -0x28(%rbp),%eax
|
||||||
|
add %eax,%edx
|
||||||
|
mov 0x10(%rbp),%eax
|
||||||
|
add %edx,%eax
|
||||||
|
mov %eax,-0x4(%rbp)
|
||||||
|
#}
|
||||||
|
nop
|
||||||
|
pop %rbp
|
||||||
|
ret
|
||||||
|
```
|
||||||
|
|
||||||
|
Ce fonctionnement est d'ailleurs confirmé dans notre main. D'après le code
|
||||||
|
ci-dessous un `push` est réalisé **ligne 6** avec comme valeur `0x7`. Ce qui
|
||||||
|
correspond à **la valeur de notre dernier paramètre** :
|
||||||
|
|
||||||
|
``` asm {linenos=inline, hl_lines=[6]}
|
||||||
|
# 0000000000401148 <main>:
|
||||||
|
#int main () {
|
||||||
|
push %rbp
|
||||||
|
mov %rsp,%rbp
|
||||||
|
# foo (1, 2, 3, 4, 5, 6, 7);
|
||||||
|
push $0x7
|
||||||
|
mov $0x6,%r9d
|
||||||
|
mov $0x5,%r8d
|
||||||
|
mov $0x4,%ecx
|
||||||
|
mov $0x3,%edx
|
||||||
|
mov $0x2,%esi
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Test avec les flottants
|
||||||
|
|
||||||
|
Pour les flottants, le passage sur la pile se fait a partir de 8 paramètres
|
||||||
|
comme le montre le `main` obtenu (instruction `push` ligne 6):
|
||||||
|
|
||||||
|
```asm {linenos=inline, hl_lines=6}
|
||||||
|
# 0000000000401197 <main>:
|
||||||
|
# int main () {
|
||||||
|
push %rbp
|
||||||
|
mov %rsp,%rbp
|
||||||
|
# foo (1, 2, 3, 4, 5, 6, 7, 8);
|
||||||
|
push $0x8
|
||||||
|
mov $0x7,%r9d
|
||||||
|
mov $0x6,%r8d
|
||||||
|
```
|
Loading…
Add table
Add a link
Reference in a new issue