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).    
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...
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...
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?
Instructions: Write a program to calculate students’ average test scores and their grades. You may assume...
Instructions: Write a program to calculate students’ average test scores and their grades. You may assume the following input data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63...
Assume there is a medical test to diagnose a disease. If a person has the disease,...
Assume there is a medical test to diagnose a disease. If a person has the disease, the probability of having positive test result is 98 percent. If a person does not have the disease, the probability of having negative test results is 99.6 percent. The probability that a person has a disease is 1 percent in the population. Answer the following questions: a) If a person has a positive test result, what is the probability that he/she has the disease?...
Write a machine language program to output your name on the output device. The name you...
Write a machine language program to output your name on the output device. The name you output must be longer than two characters. Write it in a format suitable for the loader and execute it on the Pep/9 simulator. my name is Kevin
Write a program to determine if the student has passed the test or not. Please use...
Write a program to determine if the student has passed the test or not. Please use two if statements to display the results. For example, if the user has entered ‘true’, display ‘the student has passed the test’, and if the user has entered ‘false’, display ‘the student has failed the test’. Use the scanner class to get the Boolean datatype answer from the user if the student has passed the test or not. Use two if statements to determine...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT