In each of the following C code snippets, there are issues that can prevent the compiler from applying certain optimizations. For each snippet: 12345678 Circle the line number that contains compiler optimization blocker. Select the best modification to improve optimization. 1. Which line prevents compiler optimization? Circle one: 23456 Suggested solution: } . Remove printf or move it outside the loop. Remove the loop. ⚫ Replace arr[i] with a constant value. int sum (int arr, int n) { int s = 0; for (int i=0; i

icon
Related questions
Question
In each of the following C code snippets, there are issues that can prevent the compiler
from applying certain optimizations. For each snippet:
12345678
Circle the line number that contains compiler optimization blocker.
Select the best modification to improve optimization.
1. Which line prevents compiler optimization? Circle one: 23456
Suggested solution:
}
. Remove printf or move it outside the loop.
Remove the loop.
⚫ Replace arr[i] with a constant value.
int sum (int arr, int n) {
int s = 0;
for (int i=0; i<n; i++) {
sarr[i];
}
printf("%d\n", s);
return 8;
2. Which line prevents compiler optimization? Circle one: 2 3 456
Suggested solution:
1
2
3
4
5
• Move or eliminate do_extra_work () if it's not necessary inside the loop.
Remove the loop (but what about scaling?).
Replace arr[i] factor; with arr[i] 0; (why would that help?).
void scale (int arr, int n, int factor) {
for (int i=0; i<n; i++) {
arr[i] factor;
do_extra_work ();
6 }
}
123451
A ROP (Return-Oriented Programming) attack can be used to execute arbitrary
instructions by chaining together small pieces of code called "gadgets," Your goal is
to create a stack layout for a ROP attack that calls a function located at 'Ox-4018bd3.
Below is the assembly code for the function 'getbuf, which allocates 8 bytes of stack
space for a 'char array. This array is then passed to the 'gets function. Additionally,
you are provided with five useful gadgets and their addresses. Use these gadgets to
construct the stack layout.
Assembly for getbuf
getbuf:
sub
mov
$8, %rsp
call
add
ret
%rsp. %rdi
gets
$8, %rsp
in
section)
Stack
cach
Layout
8-byte
119)
Address Value (8 bytes)
Ox7HHHidico
Ox7fffffdfbs
Ox7HHHidfbo
Ox7HHildfas
Ox7ffffffdfa0
Ox7HHHidros
Ox 7490
Ox7ffffffdfss
Original Return
Address
at
#Allocate 8 bytes for buffer.
#Load buffer address into %rdi
#Call gets with buffer
#Restore the stack pointer
#Return to caller
Gadgets
Address Gadget
Ox40067 pop Xrdi; ret
Ox-4006a9 pop rai: ret
Ox4006ab pop Xrax; ret
Ox-4006ad mov %rax, %rbx; ret
Ox-400660 pop Zrbp; ret
Ox7fffffffdf898
Task:
Fill in the stack layout on the left with the values (addresses or constants)
necessary to create a ROP chain that will execute a call to the function at
'Ox 4018bd3'.
Use the provided gadgets to control the values in registers as needed.
Transcribed Image Text:In each of the following C code snippets, there are issues that can prevent the compiler from applying certain optimizations. For each snippet: 12345678 Circle the line number that contains compiler optimization blocker. Select the best modification to improve optimization. 1. Which line prevents compiler optimization? Circle one: 23456 Suggested solution: } . Remove printf or move it outside the loop. Remove the loop. ⚫ Replace arr[i] with a constant value. int sum (int arr, int n) { int s = 0; for (int i=0; i<n; i++) { sarr[i]; } printf("%d\n", s); return 8; 2. Which line prevents compiler optimization? Circle one: 2 3 456 Suggested solution: 1 2 3 4 5 • Move or eliminate do_extra_work () if it's not necessary inside the loop. Remove the loop (but what about scaling?). Replace arr[i] factor; with arr[i] 0; (why would that help?). void scale (int arr, int n, int factor) { for (int i=0; i<n; i++) { arr[i] factor; do_extra_work (); 6 } } 123451 A ROP (Return-Oriented Programming) attack can be used to execute arbitrary instructions by chaining together small pieces of code called "gadgets," Your goal is to create a stack layout for a ROP attack that calls a function located at 'Ox-4018bd3. Below is the assembly code for the function 'getbuf, which allocates 8 bytes of stack space for a 'char array. This array is then passed to the 'gets function. Additionally, you are provided with five useful gadgets and their addresses. Use these gadgets to construct the stack layout. Assembly for getbuf getbuf: sub mov $8, %rsp call add ret %rsp. %rdi gets $8, %rsp in section) Stack cach Layout 8-byte 119) Address Value (8 bytes) Ox7HHHidico Ox7fffffdfbs Ox7HHHidfbo Ox7HHildfas Ox7ffffffdfa0 Ox7HHHidros Ox 7490 Ox7ffffffdfss Original Return Address at #Allocate 8 bytes for buffer. #Load buffer address into %rdi #Call gets with buffer #Restore the stack pointer #Return to caller Gadgets Address Gadget Ox40067 pop Xrdi; ret Ox-4006a9 pop rai: ret Ox4006ab pop Xrax; ret Ox-4006ad mov %rax, %rbx; ret Ox-400660 pop Zrbp; ret Ox7fffffffdf898 Task: Fill in the stack layout on the left with the values (addresses or constants) necessary to create a ROP chain that will execute a call to the function at 'Ox 4018bd3'. Use the provided gadgets to control the values in registers as needed.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer