Homework 2

From CS113

Answer the following questions based on the machine used for the course (x86/IA32, Linux 2.6).

Contents

Part 1: Exploring the Stack

Q1: Give an address of a variable on the stack (in hexadecimal representation).

Answer: 

Q2: Which direction does the stack grow on the test system? Write a program that verifies this and prints up or down respectively.

Answer: 
(Write stackdir.c that verifies this on the test system)

Q3: Write a function that has one local variable and calls a second function without any arguments. Compile with the argument -O0 to gcc (dash oh zero). What is the approximate size of the stack frame for the first function?

Answer:


Q4: What operations cause the size of the stack frame to increase? Additional variables, if/for statements, mathematical operations, additional arguments accepted by the first function, additional arguments to the second function, etc.

Answer:


Q5: If the code is optimized (compile with -O3), which of the above cases no longer effect the stack size?

Answer:


Being naughty with the stack

Q6: Write a function with one variable a initialized to 0. Call a second function without passing any arguments. Upon returning from the second function, the value of a must be 42.

(Write your solution in stackhack1.c)

Q7: Which of the following two programs (A or B) are safe or unsafe. Why?

// Q7 - A
#include <stdio.h>

void test(int *a) {
    printf("Hi\n");
    printf("%d\n", *a);
}

int main() {
    int a = 42;

    test(&a); 
}
// Q7 - B
#include <stdio.h>

int *test() {
    int a = 42;

    return &a;
}

int main() {
    int *ret = test();

    printf("Hi\n");
    printf("%d\n", *ret);
}
Answer:





Q8: What happens when the following code is run. Why?

#include <stdio.h>
#include <unistd.h>

int n = 0;

int test(int a) {
    *(&a - 1) = test;
    usleep(500000);
    printf("Hi %d. Press ctrl-c to quit.\n", n++);
}

int main() {
    test(0);
    printf("I am back.\n");
}
Answer:





Crashing the Stack

Q9: Write a program that runs out of stack space.

 (Write your solution in stackhack2.c)

Q10: Write a function that crashes the program when you attempt to return from it.

 (Write your solution in stackhack3.c)

Part 2: Exploring the Heap

Q11: Give an address of a variable on the heap.

Answer: 

Q12: Which of the following two programs (A or B) are safe or unsafe. Why?

// Q12 - A
#include <stdio.h>
#include <stdlib.h>

void test(int *a) {
    printf("Hi\n");
    printf("%d\n", *a);
}

int main() {
    int *a = (int *)malloc(sizeof(int));

    *a = 42;
    test(a); 
    free(a);
}
// Q12 - B
#include <stdio.h>
#include <stdlib.h>

int *test() {
    int *a = (int *)malloc(sizeof(int));
    
    *a = 42;
    return a;
}

int main() {
    int *ret = test();

    printf("Hi\n");
    printf("%d\n", *ret);
    free(ret);
}
Answer:






Being naughty with the heap

In the following questions, you must find the bug that violates good programming practice, causes non-deterministic behavior or causes the program to crash.

Q13: Explain the bug in the following program.

#include <stdlib.h>
#include <stdio.h>

int test(int *p) {
    free(p);   
    printf("%d\n", *p);
}

int main() {
    int *a = (int *)malloc(sizeof(int));

    *a = 42;
    test(a);
}
Answer:




Q14: Explain the bug in the following program.

#include <stdlib.h>
#include <stdio.h>

int test(int *p) {
    printf("%d\n", *p);
    free(p);   
}

int main() {
    int *a = (int *)malloc(sizeof(int));

    *a = 42;
    test(a);
    free(a);   
}
Answer:




Q15: Explain the bug in the following program.

#include <stdlib.h>
#include <stdio.h>

int test(int *p) {
    printf("%d\n", *p);
}

int main() {
    int *a = (int *)malloc(sizeof(int));

    *a = 42;
    test(a);
    return 0;
}
Answer:

Crashing the heap

Q16: Write a program that allocates memory in units of 1024 bytes until no more memory can be allocated. Run this program by calling shrinkheap_cs113 progname. How many kilobytes of memory was successfully allocated?

 Answer: 
WARNING: FOLLOW THESE INSTRUCTIONS FOR TESTING YOUR PROGRAM for Q15
Use shrinkheap_cs113 progname to test your code. This will reduce the heapsize available to your program to a small number so it can be filled easily. Not doing this would allow for a 4GB heap size which will take forever to overflow and will cause the test machine to swap heavily preventing you or anyone else from working on their problemset.
[netid@submit cs113]$ shrinkheap_cs113 ./heaphack1


Q17: Write a function that allocates memory, but crashes when that memory is freed for the first time.

 (Write your solution in heaphack1.c)

Submission Instructions

Print this page and write the answers in the space provided. Hand in part 1 and part 2 in class on the due dates announced.

Create the necessary files (stackdir.c, stackhack1.c, stackhack2.c, stackhack3.c, heaphack1.c) in your cs113 directory on the course machine.

Personal tools
Navigation