Question

In: Computer Science

this is a test program foo that has a bug. Assume your machine has 4KB pages....

this is a test program foo that has a bug. Assume your machine has 4KB pages. Note that this code is runnable on a Linux machine.

#include <stdlib.h>

#include <stdio.h>

#include <sys/mman.h>

struct foo {

    int a; // 4-byte int

    int b; // 4-byte int

};

int main (int argc, char * argv[]) {

    int num = atoi(argv[1]); // convert program arg to integer

    struct foo * a = mmap(NULL,

                          sizeof(struct foo) * num,

                          PROT_READ | PROT_WRITE,

                          MAP_ANONYMOUS | MAP_PRIVATE,

                          -1,

                          0);

    a[num].a = 100;

    printf("Succeed\n");

}

  1. What is the bug?
  2. Explain (in detail) why when I run foo with different arguments as below I get different results:

[me@machine] ./foo 512

bash: “./test 512” terminated by signal SIGSEGV

[me@machine] ./foo 256

Succeed

Solutions

Expert Solution

Please find the correct program and added a comment for the reason of crash.

Cause for segment fault:

We are accessing the more than the allocated memory to assign the value of num.

More explanation:

Here the array index starts with 0-511. if we use 512 here it goes beyond the page size
256 is not issuing segmentation fault because we have page size upto 4k(4096 bytes)

Error code:

a.[num]=100; //incorrect

Correct code:

a.[num-1]=100; //correct

Correct Program:

#include <stdlib.h>

#include <stdio.h>

#include <sys/mman.h>

struct foo {

int a; // 4-byte int

int b; // 4-byte int

};

int main (int argc, char * argv[]) {


int num = atoi(argv[1]); // convert program arg to integer

struct foo * a = mmap(NULL,

sizeof(struct foo)*num,

PROT_READ | PROT_WRITE,

MAP_ANONYMOUS | MAP_PRIVATE,

-1,

0);

//index starts with 0-511. if we use 512 here it goes beyound the page size
//256 is not issuing segmentation fault because we have page size upto 4k(4096 bytes)

a[num-1].a = 100;

printf("Succeed\n");

}

Sample Output:
USER>./a.out 256
Succeed
USER>./a.out 512
Succeed


Related Solutions

The average comic book has 55 pages and a standard deviation of 7 pages. Assume it...
The average comic book has 55 pages and a standard deviation of 7 pages. Assume it has a normally distributed. a) If a single comic book is selected, find the probability that the number of pages will be greater than 73. b) If 25 comic books are selected, find the probability that the that the number of pages will be greater than 73.
Determine the output of the following program: var x; function bar() { writeln(x); } function foo()...
Determine the output of the following program: var x; function bar() { writeln(x); } function foo() { var x; x = 3; bar(); } function oof() { var x; x = 7; foo(); } x = 5; oof(); (a) if the static scoping rule is used? What is the reason?. (b) if the dynamic scoping rule is used? What is the reason?.
Assume that a benchmark program executes in 480 seconds on a reference machine A. The same
Assume that a benchmark program executes in 480 seconds on a reference machine A. The same program executes on systems B, C, and D in 360, 540, and 210 seconds, respectively. a. Show the speedup of each of the three systems under test relative to A. b. Now show the relative speedup of the three systems. Comment on the three ways of comparing machines (execution time, speedup, relative speedup).    
I'd like to test the efficiency of a new bug spray. I take 30 people and...
I'd like to test the efficiency of a new bug spray. I take 30 people and randomly assign one of two bug sprays to either their right or left arm. Bug spray 1 is laboratory tested and Bug spray 2 is just vinegar. Each individual is is then THROWN INTO AN INESCAPABLE CAGE WITH MANY MOSQUITOES. (Yes, I am an evil statistician). Once 30 minutes has passed for each individual, they are released and the number of bug bites on...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
A bug collector collects bugs every day for seven days. Design a program that keeps a...
A bug collector collects bugs every day for seven days. Design a program that keeps a running total of the number of bugs collected during the seven days. The loop should ask for the number of bugs collected for each day, and when the loop is finished, the program should display the total number of bugs collected. Finally, compute and display the average number of bugs collected. You should have 2 functions defined: A main – control program flow A...
A bug collector collects bugs every day for five days. Write a program that keeps a...
A bug collector collects bugs every day for five days. Write a program that keeps a running total of the number of bugs collected during the five days. The loop should ask for the number of bugs collected for each day, and when the loop is finished, the program should display the total number of bugs collected. need this in Python
Program Description Write and test a MASM program to perform the following tasks: Display your name...
Program Description Write and test a MASM program to perform the following tasks: Display your name and program title on the output screen. Display instructions for the user. Prompt the user to enter three numbers (A, B, C) in descending order. Calculate and display the sum and differences: (A+B, A-B, A+C, A-C, B+C, B-C, A+B+C). Display a closing message. Program Requirements The program must be fully documented and laid out according to the CS271 Style Guide. This includes a complete...
In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried...
In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried to reference Numbers[11][-13]. Which element of Numbers will actually be accessed?
Correct mistakes in the following program: /* BUG ZONE!!! Example: some common pointer errors */ #include...
Correct mistakes in the following program: /* BUG ZONE!!! Example: some common pointer errors */ #include <stdio.h> main() { int i = 57; float ztran4; int track[] = {1, 2, 3, 4, 5, 6}, stick[2][2]; int *nsave; /* Let's try using *nsave as an int variable, and set it to 38 */ *nsave = 38; /* BUG */ nsave = NULL; *nsave = 38; /* BUG */ nsave = 38; /* BUG */ &nsave = 38; /* BUG */ nsave...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT