Test 4
1. Controversial process tests (13 points)
For problem set 4a,
everyone posted shell tests to Ed. Recall that a good test is reliable: it
passes with bash and with every correct sh61. The tests below were all
controversial; we’re asking you why.
Some reminders about sh61_tester:
struct sh61_tester {
// Runs the shell on `script`. Returns once the shell has exited and all
// `after` functions have been called. Can be called only once.
void run(std::string_view script);
// Calls `f()` exactly `delay` seconds after the shell starts
void after(double delay, std::function<void()> f);
// Returns the processes on the shell’s terminal, including the shell
std::vector<sh61_process_info> processes(); // members: pid, name, state
std::string stdout() const; // shell’s standard output
double elapsed() const; // number of seconds the shell ran
bool is_running(); // true iff the shell is still running
};
QUESTION 1A. This test fails on bash and every compliant sh61 shell.
t.run("printf %s\n one");
assert(t.stdout() == "one\n");
Briefly explain the problem and change the command line so that the assertion passes.
QUESTION 1B. This test passes on bash, but fails on many compliant
sh61 shells. The comment actually references the reason for the failure!
// Test: Foreground pipelines with two or more commands must wait at
// least for the last command to complete
t.run("true | sleep 2 | echo first ; echo second");
assert(t.elapsed() >= 2);
assert(t.stdout() == "first\nsecond\n");
Briefly explain the problem and change the command line so that the assertion passes. (Your test must contain a nontrivial foreground pipeline.)
QUESTION 1C. This test fails on bash and every compliant sh61 shell.
// Test: skipping a pipeline must not reset status
t.run("true || false && echo yes");
assert(t.stdout() == "");
Briefly explain the problem, and give the correct expected output.
QUESTION 1D. This test passes on bash, but fails on many compliant sh61
shells.
t.after(5, [&] {
auto ps = t.processes();
int x = 0;
for (size_t i = 0; i < ps.size(); i++) {
if (ps[i].name == "sleep") {
++x;
}
// check that there is at least 1 sleep process at time 5
assert(x >= 1);
}
});
t.run("sleep 10 &");
For instance, on a compliant shell whose run_line looks like this, the
assert(x >= 1); will often fail:
void run_line(command_line_parser clp) {
for (auto condp = clp.conditional_begin(); condp; ++condp) {
if (condp.next_op() == TYPE_BACKGROUND) {
pid_t p = fork();
assert(p >= 0);
if (p == 0) {
run_conditional(condp); // runs the conditional to completion
_exit(0);
}
} else {
run_conditional(condp);
}
}
}
Briefly explain the problem and describe how to fix the test.
2. Waitsy (11 points)
Robert Glück is trying to add waitpid functionality to his WeensyOS. So
far, he’s added one system call, pid_t sys_waitpid(pid_t pid, int* status, int wflags). His intention is for sys_waitpid to block until process
pid exits, store pid’s exit status in *status (if status is not null),
and return pid. sys_waitpid’s error conditions and wflags functionality
should follow that of Unix waitpid.
QUESTION 2A. Robert’s first implementation of sys_waitpid is very
simple. (syscall_waitpid is called from syscall in the natural way.)
pid_t syscall_waitpid_v1(pid_t pid, int* status, int wflags) {
if (pid < 1 || pid >= MAXNPROC) {
return E_CHILD;
} else if (ptable[pid].state != P_FREE) {
return 0; // try again
}
return pid;
}
List the true statements.
- Unlike Unix
waitpid,syscall_waitpid_v1always blocks. - Unlike Unix
waitpid,syscall_waitpid_v1never blocks. - Unlike Unix
waitpid,syscall_waitpid_v1lets a process wait for a process that is not its child. - Unlike Unix
waitpid,syscall_waitpid_v1lets a process wait for a process that never existed. - Like Unix
waitpid,syscall_waitpid_v1will return successfully for the same child at most once. - Unlike Unix
waitpid,syscall_waitpid_v1never reports the child process’s exit status throughstatus. - None of the above.
QUESTION 2B. The Unix process model has many features that WeensyOS lacks.
Three of them—zombie processes, parent process IDs, and exit status
storage—are relevant for waitpid implementation. Robert adds these features
to his WeensyOS. His kernel.hh changes look like this (new lines marked with
***):
// Process state constants: P_FREE, P_RUNNABLE, P_BLOCKED, P_FAULTED, and...
#define P_ZOMBIE 4 // *** zombie process
// Process descriptor type
struct proc {
x86_64_pagetable* pagetable; // process's page table
pid_t pid; // process ID
int state; // process state (see above)
regstate regs; // process's current registers
pid_t ppid; // *** parent process ID
int exit_status; // *** exit status (if process is P_ZOMBIE)
};
His sys_exit implementation now sets the exited process’s state to
P_ZOMBIE, not P_FREE, and stores the exit status in current->exit_status.
Here’s his revised syscall_waitpid:
pid_t syscall_waitpid_v2(pid_t pid, int* status, int wflags) {
if (pid < 1 || pid >= MAXNPROC
|| ptable[pid].state == P_FREE || ptable[pid].ppid != current->pid) {
return E_CHILD;
} else if (ptable[pid].state != P_ZOMBIE) {
return 0; // try again
}
if (status) {
*status = ptable[pid].exit_status;
}
return pid;
}
Which line of code represents a security hole that a user process could exploit to attack the kernel? Explain briefly.
QUESTION 2C. syscall_waitpid_v2 does not allow the kernel to reuse
process IDs, so at most 15 processes can ever be created. Add one line of code
to fix this problem (and say where it goes), or explain the problem briefly.
QUESTION 2D. Robert is now trying to add support for blocking. He changes
lines 5-7 of syscall_waitpid_v2 as follows:
} else if (ptable[pid].state != P_ZOMBIE) {
if ((wflags & WNOHANG) != 0) {
return 0; // try again
}
// Otherwise, we should block
current->regs.reg_rip -= 2; // `syscall` instruction is bytes `0f 05`; back up so process reexecutes it
schedule();
}
Give one reason why this function is not a correct implementation of
blocking waitpid. Explain briefly.
QUESTION 2E. Give one reason the function from 2D is a correct
implementation of blocking waitpid. Explain briefly.
3. In general (11 points)
QUESTION 3A. This program starts with log.txt empty.
int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
pid_t p = fork();
if (p == 0) {
write(fd, "AAA", 3);
_exit(0);
}
waitpid(p, nullptr, 0);
write(fd, "BBB", 3);
What does log.txt contain after both processes exit (assuming no system
calls fail or return short counts)? Explain briefly.
QUESTION 3B. If you swapped lines 7 and 8 of Question 3A’s code (so that
write(fd, "BBB", 3) preceded waitpid), what might log.txt contain
after both processes exit? Explain briefly.
QUESTION 3C. In lecture, the w-syncbyte, w-osbyte, and w-stdiobyte
programs each wrote a file one byte at a time. Order them from slowest to
fastest, and for each speedup, name the cache that is responsible.
QUESTION 3D. This program is run as ./prog < test.txt, where test.txt
contains CS 61 rocks\n (12 bytes).
int c = fgetc(stdin);
char buf[100];
ssize_t n = read(STDIN_FILENO, buf, 100);
What will be the value of n? Explain briefly.
QUESTION 3E. This code runs the pipeline yes | head -n 5.
int pfd[2];
pipe(pfd);
pid_t p1 = fork();
if (p1 == 0) {
dup2(pfd[1], STDOUT_FILENO);
close(pfd[1]);
execlp("yes", "yes", nullptr);
}
pid_t p2 = fork();
if (p2 == 0) {
dup2(pfd[0], STDIN_FILENO);
close(pfd[0]);
close(pfd[1]);
execlp("head", "head", "-n", "5", nullptr);
}
close(pfd[0]);
close(pfd[1]);
int status;
waitpid(p2, &status, 0);
waitpid(p1, &status, 0);
When run (with no system call failures), it prints just five ys, but the
yes process never exits (and the parent stays blocked on line 20, at
waitpid(p1)). Describe the problem briefly, and say how to fix it with one
line of code.
4. Bypass (10 points)
Leonora Carrington’s problem set 5
uses the single-slot cache from section (tag, end_tag, pos_tag, and a
4096-byte cbuf). Her io61_readc reads through the cache. Her io61_read
has an optimization for big reads:
ssize_t io61_read(io61_file* f, unsigned char* buf, size_t sz) {
if (sz >= f->bufsize) {
// Big read: skip the cache and read directly into `buf`
ssize_t n = read(f->fd, buf, sz);
if (n > 0) {
f->tag = f->pos_tag = f->end_tag = f->end_tag + n;
}
return n;
}
// Small read: copy data from the cache, refilling it as needed
// ... (correct code, as in section) ...
}
QUESTION 4A. Why might skipping the cache make big reads faster?
QUESTION 4B. A 20,000-byte file contains the characters abcdefghij, over
and over again: the bytes at file offsets 0, 10, 20, etc. are all a, the
bytes at offsets 1, 11, 21, etc. are all b, and so forth. A program opens
this file with Leonora’s io61_open_check and runs:
int c = io61_readc(f);
unsigned char buf[5000];
ssize_t n = io61_read(f, buf, 5000);
printf("c = %c, buf[0] = %c\n", c, buf[0]);
But this doesn’t print c = a, buf[0] = b like it should. What does it print?
Alternately, what file offsets are read for c and buf[0]?
QUESTION 4C. Explain the bug, and fix it by changing one line.
QUESTION 4D. Leonora argues that t/varblock-001.cc should have caught this
problem:
// Test: Copying a file in blocks of random sizes
#include "iotest.hh"
int main(int argc, char* argv[]) {
io61_tester t(argc, argv);
t.text_file("in.txt", 32 << 10);
// Each block is between 1 and 256 bytes long
t.run("varblockcat61 -b 256 -o out.txt in.txt");
assert(t.file_contents("out.txt") == t.file_contents("in.txt"));
}
She reasons that varblockcat61 reads with varying block sizes, and can
alternate io61_readc and io61_read calls, which is the core of the bug.
But the test as handed out will never catch Leonora’s bug. Change one line so
that it often will.