CS360 Midterm Exam. March 13, 2012. James S. Plank

Put your answers on the answer sheets provided. Do not answer on this exam.

Question 0

Write a procedure atos() which takes a NULL-terminated array of strings as its parameter and returns a string. What it should do is allocate, construct and return a single string composed of each string in the array separated by a space. The procedure should run in O(n) time, where n is the total number of characters in the string that you return.

Question 1

In your jtar program, you called lstat(), and it filled in a data structure of type struct stat. List for me all of the ways in which that data structure was used by your jtar program. There may be parts of the data structure that were used for multiple purposes -- list each of these separately.

Question 2

Suppose rv, fd and sz are integers and buf is a pointer; and suppose I have the following line in my program:

rv = read(fd, buf, sz);

Below are 25 potential outcomes of the read() call. For each outcome, label it either "P" for "Possible" or "I" for "Impossible." In other words, if it is possible for the outcome to occur, label it "P". If there is no way for the outcome to occur, label it "I". I don't want explanation. I just want P's and I's.

AFewer than sz bytes are read from a file to buf, and rv is set to the number of bytes that were read.
Bsbrk(0)-buf is less than sz, and as a result, the read generates a segmentation violation
Cfd is not an open file, and the read call generates a segmentation violation.
Dfd is a file opened for writing only, and the read call returns -1 as a result.
Ebuf is pointing to a chunk of memory that is fewer than sz bytes, and the read call generates a segmentation violation.
Fbuf is pointing to the stack segment and sz bytes are read successfully.
GFewer than sz bytes are read from a file to buf, and rv is set to -1.
Hbuf is pointing into the void and the read call returns -1.
Ibuf is pointing to a chunk of memory that is fewer than sz bytes, and the read call corrupts memory in the process.
Jbuf is pointing to a region of sz bytes in the globals segment,and the read call returns -1 because of where buf is pointing
KA bus error occurs because buf is not a multiple of four.
Lbuf is pointing to sz bytes in the code segment, and the read call generates a segmentation violation because of where buf is pointing.
MZero bytes are read from any file, and rv is set to 0.
Nbuf is pointing to the code segment and sz bytes are read successfully.
Osbrk(0)-buf is less than sz, and as a result, the read call returns -1
Psz bytes are read from a file to buf, and rv is set to sz.
Qfd is a file opened for writing only, and the read call generates a segmentation violation.
Rbuf is pointing to the stack segment and a segmentation violation occurs because of where buf is pointing
SA buffer overflow attack occurs as a result of the read statement.
Tbuf is pointing to a region of more than sz bytes in the globals segment and sz bytes are read successfully.
Ubuf is pointing to a region of more than sz bytes in the globals segment and a segmentation violation occurs because of where buf is pointing.
Vfd is not an open file, and the read call returns -1 as a result.
Wbuf is pointing to the stack segment and the read call returns -1 because of where buf is pointing
Xbuf is pointing to sz bytes in the code segment, and the read call returns -1 because of where buf is pointing.
Ybuf is pointing into the void and the read call generates a segmentation violation.

Question 3

When the procedure messy_proc(), is called, the state of memory from addresses 0xbfffdb30 to 0xbfffdb87 is pictured below. In the picture, I show the value of every four bytes in three ways -- I show the value as an integer, in hexadecimal, and as four characters. If the character is not a printable character or the NULL character, I show that with "--".

For example, the four bytes starting at address 0xbfffdb30 are equal to -1073751220 when represented as an integer. They are equal to 0xbfffdb4c when represented as hexadecimal. The byte at 0xbfffdb30 is equal to the 'L' character. The bytes at 0xbfffdb31, 0xbfffdb32 and 0xbfffdb33 are all non-printable characters.

Here is messy_proc():

void messy_proc(int **a, int *b, char **c)
{
  int i, j;
  char *s, *t;

  printf("a: 0x%x\n", (unsigned int) a);
  printf("b: 0x%x\n", (unsigned int) b);
  printf("c: 0x%x\n", (unsigned int) c);
  printf("\n");

  for (i = 0; i < 5; i++) printf("%12d ", b[i]);
  printf("\n");
  printf("\n");

  for (i = 0; i < 5; i++) printf("%s\n", c[i]);
  printf("\n");

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      printf("%12d ", a[i][j]);
    }
    printf("\n");
  }
  printf("\n");
  for (b = a[0]; b < (int *) a[0][0]; b += 2) {
    printf("%12d\n", *b);
  }
  printf("\n");
  
  /* Make this the last thing you do on the test.
     Don't burn time on it if you don't have the 
     time to burn. */

  s = c[0];
  t = s+1;
  for (i = 0; i < 6; i++) {
    s[i] = *t;
    t += 7;
  }
  b = (int *) s;
  printf("%s 0x%x\n", s, *b);
}






The first three lines printed by messy_proc() are "a: 0xbfffdb30", "b: 0xbfffdb48" and "c: 0xbfffdb3c". Tell me what the rest of the output is. There are no segmentation violations or bus errors in this program (I have compiled and run it).

Question 4

Suppose your heap is composed of 384 bytes starting at address 0x1c230, pictured on the right. You are given the following assumptions:

  • Memory is allocated as described in class, where the size of an allocated block is stored eight bytes before the pointer.

  • The free list starts at 0x1c280.

  • Free list nodes contain size, flink and blink.

  • Pointers are four bytes.
Part A: Tell me all of the nodes on the free list, in order. For each node, tell me the address of the node and its size.

Part B: Tell me all of the allocated chunks of memory. For each chunk, tell me the value that was returned from malloc(), and the total size of the chunk.

Part C: What would sbrk(0) return?

Part D: Suppose I have an integer pointer j whose value is 0x1c3c4. If I execute "*j = 55", will the operation complete successfully, cause a segmentation violation or cause a bus error? Explain why.







Some useful prototypes

int strlen(char *s);  - Returns the length of a string

char *strcpy(char *dest, char *src);  - Copies the string in src to memory pointed to by dest.
                                      - Returns its first argument.

char *strdup(char *s);  - Allocates room for a copy of s, copies it and returns it.

char *strcat(char *dest, char *src);  - Assumes that dest is a string, and appends src to it.

char *strchr(char *s, char c)  - Returns a pointer to the first occurrence of c in s, or NULL.

char *strrchr(char *s, char c)  - Returns a pointer to the last occurrence of c in s, or NULL.

char *strstr(char *s, char *st)  - Returns a pointer to the first occurrence of st in s, or NULL.

int read(int fd, char *buf, int size);