⚠️ This is not the current iteration of the course! Head here for the current offering.

CS 300/1310 Spring 2023 Midterm Quiz

Welcome to our midterm quiz! We’re hoping this quiz provides you with an opportunity to review your knowledge and what you've learned in CS 300 so far. Our questions are designed to check your conceptual understanding, rather than asking you to memorize details.

Please make sure you read the rules below before you get started.

Rules

The quiz is open book, open note, open computer. You may access the book and your own notes. You may also use computers or electronic devices to access your own class materials, public class materials, and online resources. Specifically:

However:

Any violations of this policy are breaches of academic honesty and will be treated accordingly. Please appreciate our flexibility and behave honestly and honorably.

Additionally, students are taking the quiz at different times. Do not post publicly about the quiz until given permission to do so by course staff. This includes general comments about quiz difficulty.

Completing your quiz

You have 80 minutes to complete the quiz starting from the time you press "Start". Different students may have different quizs. Enter your answers directly in this form; a green checkmark appears when an answer is saved.

Students with extra time accommodations: If your extra time is not reflected next to the start button, contact course staff and wait for confirmation before pressing it.

You should not need to attach a drawing or other longer notes, but you may do so by adding them to a midterm subdirectory in your cs300-s23-projects repository. Make sure you push your changes to GitHub before time is up, and explain in this form where we should look at your notes.

Notes

Assume a Linux operating system running on the x86-64 architecture unless otherwise stated. If you get stuck, move on to the next question. If you’re confused, explain your thinking briefly for potential partial credit.

1. Signed Numbers (8 points)

QUESTION 1A. Malte has two code examples he wants to use for a lecture on signed numbers and integer overflow. However, when running these examples he unintentionally invokes undefined behavior, and his computer catches on fire!

Below is the program that he ran. For each of the function calls (labeled 1-4), determine:

int add(int a, int b) {
	return a + b;
}

unsigned int sub(unsigned int a, unsigned int b) {
	return a - b;
}

int main() {
  add(1, 0xFFFF'FFFA);     // 1
  add(0x7FFF'FFAC, 0x64);    // 2

  sub(300, 330);             // 3
  sub(0x8000'000A, 16);      // 4
}

2. Alignment (8 points)

QUESTION 2A. HTAs Liz and Ed are working on a new data struct called the linkEd_Lizt. The linkEd_Lizt is used to store information about their favorite jams, as seen below:

struct linkEd_Lizt {
  char* description;
  struct linkEd_Lizt* next_jam;
  struct linkEd_Lizt* previous_jam;
  char name[15]; // 14 characters + NUL terminators
  short jam_short;
  int ranking;
}

If HTA Richard were to malloc heap memory for this struct, how much memory should he request? How many of these bytes will be padding bytes? (Give your answer as a number of bytes, i.e., not as sizeof(struct linkEd_Lizt).)

QUESTION 2B. HTA Will thinks that the size of the structs should match the sizes of their jams -- humongous! How can Will rearrange the struct to ensure the linkEd_Lizt struct has a size of exactly 56 bytes? In your answer, write the struct declaration with comments to indicate where padding bytes (if any) occur.

3. Assembly (8 points)

QUESTION 3A. Armed with your Section 3 knowledge on ethical hacking, you hack the CS300 grading server and find a suspicious looking assembly file that you suspect lowers student grades based on their names—yikes! The function returns 1 if a student’s grade gets unfairly lowered, and 0 otherwise:

unfair:
        mov %rdi, %rcx
.L1:
        movzbl (%rcx), %eax
        cmpb $0, %al
        je .L2
        cmpb $76, %al
        je .L3
        addq $1, %rcx
        jmp .L1
.L2:
        movl    $0, %eax
        ret
.L3:
        movl    $1, %eax
        ret

What are the criteria that Malte is using to unfairly deduct points? In other words, in what cases will the function return 1?

4. Stack (10 points)

QUESTION 4A. Below is a partial implementation of Snake for a 1 by 300 board where the snake is permanently moving to the right and collisions with walls are not yet implemented. You may assume that the program includes everything necessary to compile.

int game_over = 0;
int board_length = 300;

void update(char* cells, int* pos_p) {
    int old_pos = *pos_p;
    int new_pos = *pos_p + 1;
    cells[new_pos] = FLAG_SNAKE;
    cells[old_pos] = FLAG_PLAIN;
    *pos_p = new_pos;
    // DRAW THE DIAGRAM AT THIS POINT
}

int main() {
    int pos = 0;
    char* cells = malloc(board_length);
    memset(cells, FLAG_PLAIN, board_length);
    cells[pos] = FLAG_SNAKE;
    while (!game_over) {
        update(cells, &pos);
        render(cells);
    }
    free(cells);
}

Draw a diagram of the layout of the stack segment at the point when the update function returns. Assume an x86-64 calling convention with compiler optimizations disabled, that the local variable pos is stored at address 0x7fff'ffff'e97c in main()'s stack frame, that the compiler is using the base pointer register %rbp, and that stack canaries are disabled.

Please format your answer like:

5. Caching (8 points)

The CS 300 staff have decided to add an exciting new extra credit opportunity to the Caching I/O project: a multi-slot I/O cache.

The idea is to take inspiration from the CPU cache, and store fixed-size blocks that each contain a contiguous range of bytes in the file on disk (e.g., 4096 bytes). Since data on disk doesn’t have addresses, cache entries are identified by byte offsets into the file, e.g., [0, 4096) or [8192, 12,288).

Slot ID Byte offset into the file Block
0 0 <4096 bytes of data>
... ... ...
N 8192 <4096 bytes of data>

When the program calls io300_read or io300_write, the caching I/O library checks the bytes at the current file position are contained in any of the blocks currently in the cache's N slots; if so, a cache hit happens, otherwise a cache miss happens and the data gets retrieved from disk.

QUESTION 5A. How should the caching I/O library handle the situation when all slots in the cache are occupied?

QUESTION 5B. For what workload would such a multi-slot cache provide speedups over the single-slot cache you implemented in the project, and why? (Give one example, there may be multiple.)

6. Undefined Behavior (10pts)

QUESTION 6A. Consider the following C program. For every labeled example, indicate whether it contains undefined behavior, and if it does, explain why. Be sure to read the entire code snippet before writing your answers.

long* dragons(int* ptr) {
   int* arr = malloc(sizeof(int) * (*ptr));
   for (int i=1; i < *ptr; i++) {
       arr[i - 1] = i;
   }
   // Example 1
   printf("My least favorite number is %d\n", arr[0]);
   // Example 2
   printf("My favorite number is %d\n", arr[4]);


   // Example 3
   char drags[] = "dragons";
   drags[7] = '!';
   // End of Example 3

   // Example 4
   printf("Here be %s?\n", drags);
   // End of Example 4

   free(ptr);
   free(arr);
   long l = 2024;
   int* l_ptr = &l;
   return l_ptr;
}


int main(){
   int* ptr = malloc(sizeof(int));
   *ptr = 5;
   long* returned_ptr = dragons(ptr);
   // Example 5
   printf("The current year is: %ld\n", *returned_ptr);
   // Example 6
   printf("High %d!\n", *ptr);
}

7. Project 2: DMalloc (4 points)

QUESTION 7A. Like your DMalloc implementation, the standard C malloc implementation also stores metadata with each allocation. Below, we show you a slightly simplified version of that metadata, which stores the size of each allocation and whether that allocation has been freed in a header:

Malloc Memory Diagram

Describe two memory errors that can occur using malloc’s metadata implementation, but not using DMalloc's. Explain how your DMalloc implementation fixes these problems.

8. CS 300/1310 Feedback (6 points)

QUESTION 8A. If you could make one change to CS 300/1310 to improve it (other than firing the instructor and dropping the quizzes), what would you change? Any answer but no answer will receive full credit.

QUESTION 8B. What topics do you wish we had spent more time on? Any answer but no answer will receive full credit.