Verlässliche Echtzeitsysteme - Übungen zur Vorlesung Phillip Raffeck, Florian Schmaus, Simon Schuster - FAU
←
→
Transkription von Seiteninhalten
Wenn Ihr Browser die Seite nicht korrekt rendert, bitte, lesen Sie den Inhalt der Seite unten
Verlässliche Echtzeitsysteme
Übungen zur Vorlesung
Phillip Raffeck, Florian Schmaus, Simon Schuster
Friedrich-Alexander-Universität Erlangen-Nürnberg
Lehrstuhl Informatik 4 (Verteilte Systeme und Betriebssysteme)
https://www4.cs.fau.de
Sommersemester 2019
Raffeck, Schmaus, Schuster VEZS (SS19) 1 – 32Keynote: Challenges in Safety-Critical Software Chris Hobbs Senior Developer bei QNX Ten Truths about Building Safe Embedded Software Systems IEC 61508, IEC 62304/ISO 14971, ISO 26262, EN 5012x Standards sind laufend in Entwicklung Bitflips vs. Rowhammering Safety vs. Security + Need for qualified developers Raffeck, Schmaus, Schuster VEZS (SS19) 2 – 32
Überblick 1 Wiederholung: Grundlagen Fehlerbäume 2 Wiederholung: Triple Modular Redundancy 3 Ausblick: Rechnerarchitektur, Replikation und Redundanz 4 Replikation von Code Raffeck, Schmaus, Schuster VEZS (SS19) 3 – 32
Überblick 1 Wiederholung: Grundlagen Fehlerbäume 2 Wiederholung: Triple Modular Redundancy 3 Ausblick: Rechnerarchitektur, Replikation und Redundanz 4 Replikation von Code Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 4 – 32
Fehlerbäume – Wiederholung
Ausfall: Kühl-
system
Kühlsystem leckt Pumpe
Dichtung Rohrleitung Druckbehälter
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 5 – 32Fehlerbäume – Wiederholung
Ausfall: Kühl-
1
system
Kühlsystem leckt Pumpe
Dichtung Rohrleitung Druckbehälter
1. Schadensereignis
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 5 – 32Fehlerbäume – Wiederholung
Ausfall: Kühl-
1
system
Kühlsystem leckt Pumpe 2
Dichtung Rohrleitung Druckbehälter
1. Schadensereignis
2. Ereignisse auf Ebene 2
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 5 – 32Fehlerbäume – Wiederholung
Ausfall: Kühl-
ODER-Verknüpfung 1
system
UND-Verknüpfung 3
XOR-Verknüpfung
atomares Ereignis
Kühlsystem leckt Pumpe 2
Eingang
Dichtung Rohrleitung Druckbehälter
1. Schadensereignis
2. Ereignisse auf Ebene 2
3. Logische Verknüpfung
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 5 – 32Fehlerbäume – Wiederholung
Ausfall: Kühl-
ODER-Verknüpfung 1
system
UND-Verknüpfung 3
XOR-Verknüpfung
atomares Ereignis
Kühlsystem leckt Pumpe 2
Eingang
Dichtung Rohrleitung Druckbehälter 4
1. Schadensereignis
2. Ereignisse auf Ebene 2
3. Logische Verknüpfung
4. Ereignisse auf Ebene 3
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 5 – 32Fehlerbäume – Wiederholung
Ausfall: Kühl-
ODER-Verknüpfung 1
system
UND-Verknüpfung 3
XOR-Verknüpfung
atomares Ereignis
Kühlsystem leckt Pumpe 2
Eingang 5
Dichtung Rohrleitung Druckbehälter 4
1. Schadensereignis 5. Logische Verknüpfung
2. Ereignisse auf Ebene 2
3. Logische Verknüpfung
4. Ereignisse auf Ebene 3
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 5 – 32Fehlerbäume – Wiederholung
Ausfall: Kühl-
ODER-Verknüpfung 1
system
UND-Verknüpfung 3
XOR-Verknüpfung
atomares Ereignis
Kühlsystem leckt Pumpe 2
Eingang 5
Dichtung Rohrleitung Druckbehälter 4
6 6 6
1. Schadensereignis 5. Logische Verknüpfung
2. Ereignisse auf Ebene 2 6. Atomare Ereignisse
3. Logische Verknüpfung
4. Ereignisse auf Ebene 3
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 5 – 32Fehlerbäume – Wiederholung
Ausfall: Kühl-
ODER-Verknüpfung 1
system
UND-Verknüpfung 3
XOR-Verknüpfung
atomares Ereignis
Kühlsystem leckt Pumpe 2
Eingang 5 7
Dichtung Rohrleitung Druckbehälter 4
6 6 6
1. Schadensereignis 5. Logische Verknüpfung
2. Ereignisse auf Ebene 2 6. Atomare Ereignisse
3. Logische Verknüpfung 7. Eingänge zerlegen den Fehlerbaum 7→ Neuer
4. Ereignisse auf Ebene 3 Teilbaum
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Grundlagen Fehlerbäume 5 – 32Überblick 1 Wiederholung: Grundlagen Fehlerbäume 2 Wiederholung: Triple Modular Redundancy 3 Ausblick: Rechnerarchitektur, Replikation und Redundanz 4 Replikation von Code Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 6 – 32
Fehlerhypothese
Replikat 1 1 0 1 1 0 1 1 1 0 0 1 0 1 0
Replikat 2 1 0 1 1 0 1 1 0 0 0 1 0 1 0 E
Replikat 3 1 0 1 1 0 1 1 1 0 0 1 0 1 0
Wie viele Replikate benötigt man?
Arten des Fehlverhaltens (von n Replikaten sind f fehlerhaft)
1. fail-silent 7→ Anzahl Replikate: n = f + 1
2. fail-consistent 7→ Anzahl Replikate: n = 2f + 1
3. malicious 7→ Anzahl Replikate: n = 3f + 1
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 7 – 32Triple Modular Redundancy
Sensorik Verarbeitung Aktorik
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 8 – 32Triple Modular Redundancy
Sensorik Interface Verarbeitung Aktorik
Schnittstelle sammelt Eingangsdaten (Replikdeterminismus)
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 8 – 32Triple Modular Redundancy
Replica
1
Replica
Sensorik Interface Aktorik
2
Replica
3
Schnittstelle sammelt Eingangsdaten (Replikdeterminismus)
Verteilt Daten und aktiviert Replikate
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 8 – 32Triple Modular Redundancy
Replica
1
Replica
Sensorik Interface Voter Aktorik
2
Replica
3
Schnittstelle sammelt Eingangsdaten (Replikdeterminismus)
Verteilt Daten und aktiviert Replikate
Mehrheitsentscheider (Voter) wählt Ergebnis
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 8 – 32Triple Modular Redundancy
Replica
1
Replica
Sensorik Interface Voter Aktorik
2
Replica
3
Schnittstelle sammelt Eingangsdaten (Replikdeterminismus)
Verteilt Daten und aktiviert Replikate
Mehrheitsentscheider (Voter) wählt Ergebnis
Ergebnis wird an Aktuator versendet
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 8 – 32Triple Modular Redundancy
Replica
1
Replica
Sensorik Interface Voter Aktorik
2
Replica
3
Redundanzbereich
Ausschließlich Replikatausführung
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 8 – 32Triple Modular Redundancy
Replica
1
Replica
Sensorik Interface Voter Aktorik
2
Replica
3
Redundanzbereich
Ausschließlich Replikatausführung
Erweiterung der Ausgangsseite mit Informationsredundanz
Mehrheitsentscheid über Berechnungsergebnisse
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 8 – 32Replikdeterminismus
Replikat 1 Replikat 2 Replikat 3
void repl_1 ( void * p ){ void repl_2 ( void * p ){ void repl_3 ( void * p ){
ticks_t time = ticks_t time = ticks_t time =
ezs_get_time (); ezs_get_time (); ezs_get_time ();
... ... ...
} } }
Sicherstellung Replikdeterminismus
Globale diskrete Zeitbasis
Einigung über Eingabewerte
Statische Kontrollstruktur der Replikate
Deterministische Algorithmen
+ Sicherstellung, dass Replikat innerhalb Zeitspanne Ergebnis liefert
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 9 – 32Process-Level Redundancy Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 10 – 32
Process-Level Redundancy
Prozess 1
Prozess 2
Prozess 3
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 10 – 32Process-Level Redundancy
Prozess 1
Replikation Abstimmung
Eingabe Prozess 2 Ausgabe
der Eingaben der Ausgaben
Prozess 3
Fehlertoleranzimplementierung
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 10 – 32Process-Level Redundancy
Prozess 1
Replikation Abstimmung
Eingabe Prozess 2 Ausgabe
der Eingaben der Ausgaben
Prozess 3
Fehlertoleranzimplementierung
Betriebssystem
Mehrkernprozessor
Raffeck, Schmaus, Schuster VEZS (SS19) Wiederholung: Triple Modular Redundancy 10 – 32Überblick 1 Wiederholung: Grundlagen Fehlerbäume 2 Wiederholung: Triple Modular Redundancy 3 Ausblick: Rechnerarchitektur, Replikation und Redundanz 4 Replikation von Code Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 11 – 32
Ebenen
5 problemorientierte Programmiersprachenebene
Übersetzer
4 Assembliersprachenebene
Assemblierer
3 Machinenprogrammebene
Betriebssystem
2 Befehlssatzebene
Hardwareentwurf
1 Mikroarchitekturebene
Hardwaresynthese
0 digitale Logikebene
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 12 – 32Digitale Logikebene
ROM DRAM
SEL
Q
RS - FlipFlop
VCC VCC
Q1 Q2
E1 E2
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 13 – 32MIPS: Single-Cycle 4.6 Pipelined Datapath and Control 287
IF: Instruction fetch ID: Instruction decode/ EX: Execute/ MEM: Memory access WB: Write back
register file read address calculation
Add
4 Add
ADD
result
Shift
left 2
0
Read Read
M register 1
Address data 1
u PC Zero
x Read ALU ALU
1 register 2 Address
Instruction result Read
Registers 0 1
M data
Write Read Data M
Instruction register data 2 u u
memory
memory x x
Write 1
0
data Write
data
16 32
Sign-
extend
Source: D. A. Patterson und J. L. Hennessy, Computer organization and
design: the hardware/software interface, 4th ed., 2012
FIGURE 4.33 The single-cycle datapath from Section 4.4 (similar to Figure 4.17). Each step of the instruction can be mapped
Raffeck,
onto Schmaus,
the datapath Schuster
from left to right. VEZS (SS19)
The only exceptionsAusblick: Rechnerarchitektur,
are the update Replikation
of the PC and the write-backund
step,Redundanz 14 – 32
shown in color, which sends eitherMIPS: Pipelining
4.6 Pipelined Datapath and Control 299
Time (in clock cycles)
CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9
Program
execution
order
(in instructions)
Instruction Instruction Data
lw $10, 20($1) fetch decode
Execution
access
Write-back
Instruction Instruction Data
sub $11, $2, $3 fetch decode
Execution
access
Write-back
Instruction Instruction Data
add $12, $3, $4 fetch decode
Execution
access
Write-back
Instruction Instruction Data
lw $13, 24($1) fetch decode
Execution
access
Write-back
Instruction Instruction Data
add $14, $5, $6 fetch decode
Execution
access
Write-back
FIGURE 4.44 Traditional multiple-clock-cycle pipeline diagram of five instructions in Figure 4.43.
add $14, $5, $6 lw $13, 24 ($1) add $12, $3, $4 sub $11, $2, $3 lw $10, 20($1)
Instruction fetch Instruction decode Execution Memory Write-back
Source: D. A. Patterson und J. L. Hennessy, Computer organization and
IF/ID ID/EX EX/MEM MEM/WB
design: the hardware/software interface, 4th ed., 2012
Raffeck, Schmaus, Schuster
Add
VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 15 – 32Instruction Instruction Data
add $14, $5, $6 fetch decode
Execution
access
Write-back
MIPS: Pipelining
FIGURE 4.44 Traditional multiple-clock-cycle pipeline diagram of five instructions in Figure 4.43.
add $14, $5, $6 lw $13, 24 ($1) add $12, $3, $4 sub $11, $2, $3 lw $10, 20($1)
Instruction fetch Instruction decode Execution Memory Write-back
IF/ID ID/EX EX/MEM MEM/WB
Add
Add
4 Add
result
Shift
left 2
0
M
u PC Address Read
Read
Instruction
x register 1
1 data 1
Read Zero
Instruction register 2 ALU
Registers Read ALU Read
memory Address 1
Write 0 result data
data 2 M
register M
Data u
u
Write memory x
x 0
data 1
Write
data
16 Sign 32
extend
FIGURE 4.45 The single-clock-cycle diagram corresponding to clock cycle 5 of the pipeline in Figures 4.43 and 4.44.
As you can see, a single-clock-cycle figure is a vertical slice through a multiple-clock-cycle diagram.
1. Allowing jumps, branches, and ALU instructions Source: D. A.fewer
to take Patterson und J.
stages L. Hennessy, Computer organization and
than
design: the hardware/software interface, 4th ed., 2012
the five required by the load instruction will increase pipeline performance
Raffeck,under all Schuster
Schmaus, circumstances.
VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 16 – 32MIPS: Pipelining
Source: D. A. Patterson und J. L. Hennessy, Computer organization and
design: the hardware/software interface, 4th ed., 2012
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 17 – 32MIPS: Pipelining 4.9 Exceptions 329
EX.Flush
IF.Flush
ID.Flush
Hazard
detection
unit
M
ID/EX u
x
WB 0 EX/MEM
M
Control M M WB
u u MEM/WB
x Cause x
IF/ID ⫹ 0 M WB
EX EPC 0
⫹ Shift
4 left 2 M
u
x
Registers ⫽
M
ALU u
M Instruction
80000180 u PC x
x memory M Data
u
x memory
Sign-
extend
M
u
x
Forwarding
unit
FIGURE 4.66 The datapath with controls to handle exceptions. The key additions include a new input with the value 8000 0180hex
Source:
in the multiplexor that supplies the new PC value; a Cause register to record theD.cause
A. Patterson und J. L.and
of the exception; Hennessy, Computer
an Exception organization
PC register to save and
the address of the instruction that caused the exception. The 8000 0180hex input to the design: the hardware/software
multiplexor is the initial addressinterface,
to begin 4th ed., 2012
fetching
instructions in the event of an exception. Although not shown, the ALU overflow signal is an input to the control unit.
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 18 – 32Eigenschaften von CPU-Architekturen Mikroprogrammierbar vs. Fixed-Function Out-of-Order-Prozessoren Sprungvorhersage Transaktionaler Speicher Superskalarität Mehrkernarchitekturen Hyperthreading ... Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 19 – 32
Eigenschaften von CPU-Architekturen Mikroprogrammierbar vs. Fixed-Function Out-of-Order-Prozessoren Sprungvorhersage Transaktionaler Speicher Superskalarität Mehrkernarchitekturen Hyperthreading ... + All diese zusätzlichen Fehlerpunkte müssen im Fehlermodell berücksichtigt werden + Ein Ein-Bit-Fehler in einer dieser Komponenten kann zu komplexen Mehrbitfehlern auf ISA-Ebene führen Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 19 – 32
Ebenen
5 problemorientierte Programmiersprachenebene
Übersetzer
4 Assembliersprachenebene
Assemblierer
3 Machinenprogrammebene
Betriebssystem
2 Befehlssatzebene
Hardwareentwurf
1 Mikroarchitekturebene
Hardwaresynthese
0 digitale Logikebene
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 20 – 32Speicherorganisation auf einem Mikrocontroller
Speicherallokation
int a; // a: global, uninitialized
int b = 1; // b: global, initialized
const int c = 2; // c: global, const
void main() {
static int s = 3; // s: local, static, initialized
int x, y; // x: local, auto; y: local, auto
char* p = malloc( 100 ); // p: local, auto; *p: heap (100 byte)
}
Statische Allokation
compile / link – Reservierung beim Übersetzen / Linken
Quellprogramm
Betrifft globale und modullokale Variablen, sowie den Code
Allokation durch Platzierung in einer Sektion
.codeTable
Symbol den
– enthält Programmcode m
.bss
.data – enthält
s=3alle uninitalisierten / mit 0 initialisierten Variablen
b=1alle initalisierten Variablen
.data – enthält
.rodata
.rodata – enthält
c=2alle initalisierten unveränderlichen Variablen
Dynamische Allokation – Reservierung zur Laufzeit
.text main
Betrifft lokale Variablen und explizit angeforderten Speicher
Stack – enthält
... alle aktuell gültigen lokalen Variablen
HeapELF
– enthält
Header explizit mit malloc() angeforderte Speicherbereiche
c dl
� GSPiC (SS 11) 16 Speicherverwaltung | 16.1 Einführung
ELF-Binary
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 21 – 32Speicherorganisation auf einem Mikrocontroller
Speicherallokation
int a; // a: global, uninitialized
int b = 1; // b: global, initialized
const int c = 2; // c: global, const
void main() {
static int s = 3; // s: local, static, initialized
int x, y; // x: local, auto; y: local, auto
char* p = malloc( 100 ); // p: local, auto; *p: heap (100 byte)
}
Statische Allokation
compile / link – Reservierung beim Übersetzen / Linken
Quellprogramm
Betrifft globale und modullokale Variablen, sowie den Code
Allokation durch Platzierung in einer Sektion
.codeTable
Symbol den
– enthält Programmcode m
s=3 .bss – enthält
s=3alle uninitalisierten / mit 0 initialisierten Variablen
Flash / ROM
.data .data
b=1 b=1alle initalisierten Variablen
.data – enthält
.rodata c=2 .rodata
.rodata – enthält
c=2alle initalisierten unveränderlichen Variablen
Dynamische Allokation – Reservierung zur Laufzeit
.text main .text main
Betrifft lokale Variablen und explizit angeforderten Speicher
Stack – enthält
... alle aktuell gültigen lokalen Variablen
flash HeapELF
– enthält
Header explizit mit malloc() angeforderte Speicherbereiche
c dl
� GSPiC (SS 11) 16 Speicherverwaltung | 16.1 Einführung
μ-Controller ELF-Binary
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 21 – 32Speicherorganisation auf einem Mikrocontroller
Speicherallokation
...
Stack
x=? int a; // a: global, uninitialized
y=? int b = 1; // b: global, initialized
const int c = 2; // c: global, const
p=
void main() {
static int s = 3; // s: local, static, initialized
RAM
int x, y; // x: local, auto; y: local, auto
char* p = malloc( 100 ); // p: local, auto; *p: heap (100 byte)
}
Heap *p
Statische Allokation
compile / link – Reservierung beim Übersetzen / Linken
init .bss a=0 Quellprogramm
Betrifft globale und modullokale Variablen, sowie den Code
.data s=3 Allokation durch Platzierung in einer Sektion
b=1 den
.codeTable
Symbol – enthält Programmcode m
copy
s=3 .bss – enthält
s=3alle uninitalisierten / mit 0 initialisierten Variablen
Flash / ROM
.data .data
b=1 b=1alle initalisierten Variablen
.data – enthält
.rodata c=2 .rodata
.rodata – enthält
c=2alle initalisierten unveränderlichen Variablen
Dynamische Allokation – Reservierung zur Laufzeit
.text main .text main
Betrifft lokale Variablen und explizit angeforderten Speicher
Stack – enthält
... alle aktuell gültigen lokalen Variablen
flash HeapELF
– enthält
Header explizit mit malloc() angeforderte Speicherbereiche
c dl
� GSPiC (SS 11) 16 Speicherverwaltung | 16.1 Einführung
μ-Controller ELF-Binary
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 21 – 32Betriebssystem
Benutzermodus
Betriebssystem
Datei- Datei-
··· ··· Mutex ···
Handle 1 Handle X
1 2 3
Systemmodus
Hintergrund-
CPU Speicher ··· ···
Speicher
Hardware
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 22 – 32Process-Level Redundancy
Prozess 1
Replikation Abstimmung
Eingabe Prozess 2 Ausgabe
der Eingaben der Ausgaben
Prozess 3
Fehlertoleranzimplementierung
Betriebssystem
Mehrkernprozessor
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 23 – 32C-Code vs. Assembler-Code
C-Code Assembler-Code
int a ; 4004 f0 :
int b = 1; 4004 f0 : push % rbp
const int c = 2; 4004 f1 : mov % rsp ,% rbp
void main () { 4004 f4 : sub $0x10 ,% rsp
static int s = 3; 4004 f8 : movabs $0x64 ,% rdi
int x , y ; 400502: callq 4003 e0 < malloc@plt >
char * p = 400507: mov % rax , -0 x10 (% rbp )
malloc (100); 40050 b: add $0x10 ,% rsp
} 40050 f: pop % rbp
400510: retq
Wo können Datenfehler auftreten?
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 24 – 32C-Code vs. Assembler-Code
C-Code Assembler-Code
int a ; 4004 f0 :
int b = 1; 4004 f0 : push % rbp
const int c = 2; 4004 f1 : mov % rsp ,% rbp
void main () { 4004 f4 : sub $0x10 ,% rsp
static int s = 3; 4004 f8 : movabs $0x64 ,% rdi
int x , y ; 400502: callq 4003 e0 < malloc@plt >
char * p = 400507: mov % rax , -0 x10 (% rbp )
malloc (100); 40050 b: add $0x10 ,% rsp
} 40050 f: pop % rbp
400510: retq
Wo können Datenfehler auftreten?
1. RAM: -0 x10 (% rbp )
2. Allgemeine CPU-Register: % rsp
3. Sonstige CPU-Register: % rip , % rflags
Raffeck, Schmaus, Schuster VEZS (SS19) Ausblick: Rechnerarchitektur, Replikation und Redundanz 24 – 32Überblick 1 Wiederholung: Grundlagen Fehlerbäume 2 Wiederholung: Triple Modular Redundancy 3 Ausblick: Rechnerarchitektur, Replikation und Redundanz 4 Replikation von Code Raffeck, Schmaus, Schuster VEZS (SS19) Replikation von Code 25 – 32
Code-Replikation: Stringification
Stringification von CPP
1 # define CMP_FUNC ( pre , repl , type , op ) \
2 type pre ## repl ( type a , type b ) { \
3 return a op b ? a : b ; \
4 }
5
6 CMP_FUNC ( max , 1 , int , >); // Funktion ?
7 CMP_FUNC ( max , 2 , int , >); // Funktion ?
8 ...
9 CMP_FUNC ( min , 1 , int ,Code-Replikation: Stringification
Stringification von CPP
1 # define CMP_FUNC ( pre , repl , type , op ) \
2 type pre ## repl ( type a , type b ) { \
3 return a op b ? a : b ; \
4 }
5
6 CMP_FUNC ( max , 1 , int , >); // Funktion max1
7 CMP_FUNC ( max , 2 , int , >); // Funktion max2
8 ...
9 CMP_FUNC ( min , 1 , int ,C++ Templates
C++ Template
1 template < typename T >
2 T max ( T x , T y ) {
3 T value ;
4 if ( x < y )
5 value = y ;
6 else
7 value = x ;
8 return value ;
9 }
10 ...
11 double md = max < double >(2.3 , 4.2);
12 auto mi = max < int >(23 U , 42);
Templates ermöglichen generische Programmierung
Wiederverwendung durch Parametrisierung
Unterscheidung von Funktions- & Klassen-Templates
Expansion zur Compilezeit ; Quelltext muss verfügbar sein (im Header)
„Code Bloat” beim Compilieren → nutzbar für Replikation von Code
Raffeck, Schmaus, Schuster VEZS (SS19) Replikation von Code 27 – 32C++ Templates
Explizite Typen als Templateparameter möglich
Nutzbar zum „Zählen” von Templates
Indizierte Template-Spezialisierung
1 template < typename T , unsigned INDEX >
2 T max ( T x , T y ) {
3 T value ;
4 if ( x < y )
5 value = y ;
6 else
7 value = x ;
8 return value ;
9 }
10 ...
11 auto m0 = max < int , 0 >(23 , 42);
12 auto m1 = max < int , 1 >(23 , 42);
Raffeck, Schmaus, Schuster VEZS (SS19) Replikation von Code 28 – 32Umgang mit Assembly-Code I
Symboltabellen in ELF-Dateien
1 0000000000201028 B __bss_start
2 0000000000201028 b completed .6983
3 ...
4 000000000000061 a T main
5 0000000000000580 t register_tm_clones
6 0000000000000510 T _start
7 ...
8 000000000000066 d t int max < int ,0 u >( int , int )
9 000000000000067 c t int max < int ,1 u >( int , int )
nm: Ausgabe der Symboltabelle
Nützliche Optionen
-C: Dekodieren der C++-Namensmangelung:
_Z3maxIiLj0EET_S0_S0_ ⇒ int max < int , 0 u >( int , int )
_Z3maxIiLj1EET_S0_S0_ ⇒ int max < int , 1 u >( int , int )
Demangling auf der Kommandozeile: c++filt
Raffeck, Schmaus, Schuster VEZS (SS19) Replikation von Code – Umgang mit Assembly-Code 29 – 32Umgang mit Assembly-Code II
Assembly-Code gemischt mit Source-Code
1 int main (){
2 4007 cd : 55 push % rbp
3 4007 ce : 48 89 e5 mov % rsp ,% rbp
4 4007 d1 : 48 83 ec 10 sub $0x10 ,% rsp
5 int a = max < int >(23 U , 42);
6 4007 d5 : be 2 a 00 00 00 mov $0x2a ,% esi
7 4007 da : bf 17 00 00 00 mov $0x17 ,% edi
8 4007 df : e8 04 01 00 00 callq
9 4008 e8 < _Z3maxIiET_S0_S0_ >
10 4007 e4 : 89 45 fc mov % eax , -0 x4 (% rbp )
11 std :: coutC-Code in C++ Einbinden
C Linkage
1 // C ++ code
2 extern " C " void f ( int ); // one way
3 extern " C " { // another way
4 int g ( double );
5 double h ();
6 };
7 void code ( int i , double d )
8 {
9 f (i );
10 int ii = g ( d );
11 double dd = h ();
12 // ...
13 }
Name mangling von C++ verhindern
⇒ C++-Code aus C-Code aufrufen
Raffeck, Schmaus, Schuster VEZS (SS19) Replikation von Code – Umgang mit Assembly-Code 31 – 32Fragen. . . Raffeck, Schmaus, Schuster VEZS (SS19) Fragen? 32 – 32
Sie können auch lesen