CS520
Spring 2017
Programming Assignment 5
Due Wednesday April 19


The goal of this assignment is to support exception handling for C programs on the Intel 64 architecture.

The public interface to the exception handling mechanism is the following three functions:

catchException enables an exception handler for the current function. This function returns 0 on its initial return but will "re-awake" and return the number of the exception if an exception is thrown while the handler is active. Any exception handler active prior to the call to catchException will be temporarily disabled while the new handler is active and automatically re-enabled when the new handler is cancelled.

cancelCatchException cancels the currently active exception handler. If a handler was made inactive when the handler being cancelled was enabled, then the handler is now made active again.

In essence, handlers are stacked. A newly enabled handler is pushed on a stack of handlers and the topmost handler is always utilized when an exception is thrown. When a handler is cancelled it is popped from the stack,

If there is no active handler when cancelCatchException is called, then no action is taken. In essence the request to cancel is just ignored.

If an exception handler is still active after the function in which it was enabled returns, then the behavior is undefined. It is the user's responsibility to call cancelCatchException before the function returns. If this responsibility is not met, then strange things can happen.

throwException throws the given exception, which will be caught by the most recently enabled handler, if there is one. If there is no enabled handler, then the program is terminated with an appropriate message to stderr that gives the exception number. The standard C library function exit is used to terminate the function and the exception number is passed to exit as its argument.

It is a fatal error to pass 0 to throwException. An appropriate error message is printed to stderr and the program is terminated by passing -1 to exit.

A thrown exception causes the cancellation of the handler that catches it.

The values of the callee-saved registers (rbx, r12-r15, rsp and rbp) should be restored at the time of the re-awakening of catchException to what they were at the call to catchException.

You will need to implement pieces of this assignment in Intel 64 assembly language. You should aim to minimize the amount of assembly language you need to write. You are not allowed to use the GNU C inline assembly language mechanism. I want you to put your assembly language code in a separate file.

You are not allowed to use the standard C library functions setjmp/longjmp or setcontext/getcontext. This assignment, in part, explores how those functions are implemented, and I want you to in effect implement them yourself.

All C code for this assignment should be placed in the file eh.c and all assembly language code should be placed in the file asm.s. See ~cs520/public/prog5 for stubs for these files, as well as a main function for an initial test (in the file testEH.c) and a Makefile for building the initial test.

To avoid namespace pollution, preface the names of all helper functions that need to be visible to the linker by "EH_". Try to minimize the number of such functions and be sure to use the "static" keyword to hide all other functions and global variables.

This assignment is hard to decompose into pieces in order to award partial credit. Therefore, 50% of the points will be awarded based on a reading of your source code to identify whether an "honest effort" was made to try to complete this assignment. The other 50% of the points will be awarded on how well your exception handler works.

Remember, you may lose points if your program is not properly structured or adequately documented. Coding guidelines are given on the course overview webpage.

Your programs will be graded using agate.cs.unh.edu so be sure to test in that environment. Your programs will be compiled using these gcc flags: -g -Wall -std=c99.

Your programs should be submitted for grading from agate.cs.unh.edu. To turn in this assignment, type:
~cs520/bin/submit prog5 eh.c asm.s

Submissions can be checked by typing:
~cs520/bin/scheck prog5

This assignment is due Wednesday April 19. The standard late policy concerning late submissions will be in effect. See the course overview webpage.

Remember: as always you are expected to do your own work on this assignment. Copying code from another student or from sites on the internet is explicitly forbidden!


Last modified on March 24, 2017.

Comments and questions should be directed to pjh@cs.unh.edu