University of Virginia Computer Science
CS216: Program and Data Representation, Spring 2006
05 April 2010
Problem Set 6
Nibbling at Byte Code
Out: 31 March
Due: Monday (beginning of class), 10 April
Collaboration Policy - Read Carefully (similar to PS5)
For this assignment, you may work on your own or with any one other person of your choice except for anyone you worked with on PS5. If you work with a partner, you should turn in one assignment with both of your names on it. If you would prefer to be assigned a partner, send email to email@example.com (include any constraints or preferences you have on your assigned partner). If a suitable match requests a partner, you will receive a partner assignment. Partners will be assigned using a greedy algorithm based on when requests arrive, so you are more likely to receive a suitable partner assignment if you send in your request early.
You may consult any outside resources including books, papers, web sites and people you wish. You are also encouraged to discuss these problems with students in the class. You must acknowledge any people and outside resources you work with on your assignment. If you discuss the assignment with people other than your partner, you may not take any written materials out of your discussion. It is fine to bounce ideas off other people, but the answers you turn in must be your own.
You are strongly encouraged to take advantage of the staffed lab hours posted on the CS216 web site.
A comprehensive reference to JVML is The Java Virtual Machine Specification by Tim Lindholm and Frank Yellin. A specification for the Java programming language is The Java Language Specification (third edition) by James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. (If you remember Java programming from CS201 and CS101, you shouldn't need this.)
] D-Java -o jasmin Name.class > Name.jThis produces the output file Name.j.
To assemble a jasmin file into a class file run:
] java -jar jasmin.jar Name.j
2. Create a Java source code file that compiles to a class file containing two consecutive aload_0 instructions.
3. Create a Java source code file that compiles to a class file containing three consecutive invokevirtual instructions.
4. Create a Java source code file that compiles to a class file containing the wide iload instruction (javap will print it as iload_w).
By producing byte codes directly, however, we can violate type safety.
The next three questions assume that you were hired by LiveMeek, an unsuspecting vendor to produce a fancy animation that runs at the end of the election to impress the election officials who will decide which voting machine to purchase. Unbeknownst to the vendor (or the election officials), you are an associate of Mooch, a stray underdog candidate in the upcoming Dog Catcher election. To ensure the election of Mooch to the Dog Catcher position, and guarantee the safety of all Mooch's stray dog friends, your task is to violate type safety to help Mooch steal the election.
Before printing out the election results, the election code will call CompleteElection.displayAnimation(). Mooch has asked you to create an implemention of CompleteElection.displayAnimation that will set Sarge's vote total to 0 to ensure Mooch's victory (or at least a tie). (Of course, we know no UVa student would ever do anything so heinous as fix an election, but this is just an exercise.)
LiveMeek assumes it is safe to hire you to implement CompleteElection, since the sensitive ElectionResults object will not be visible inside the CompleteElection class. You know, however, that they will turn off the bytecode verifier when they run the election. The ps6.zip file contains the implementation of the LiveMeek voting machine. The files BallotDefinition.java, Election.java, ElectionResults.java, NoWinnerException.java, and Office.java are part of their implementation and you cannot change them. Your goal is to create a CompleteElection.class file that will enable Mooch to steal the election and ensure freedom for stray dogs everywhere.
Hint: You may want to start by modifying the Election.java to do:
int i = 3; System.out.println(i);before the call to CompleteElection.displayAnimation(). Then use D-Java to generate the corresponding Jasmin assembly code, and figure out how to modify it to find out the location of e.
java -noverify Election(on the original Election class) will produce:
The dog catcher is: Mooch
Your solution should not require making any changes to any class besides CompleteElection. To develop your solution, you will probably want to make changes to other classes, though, so you can use to Java compiler to generate byte codes similar to those you will need to use in your solution.
If you can change the election results only by changing CompleteElection.j without requiring the -noverify option (or doing any physical damage to the ITC lab machines!), that is worth a Double Gold Star bonus.
The ps6.zip file contains two Java classes: Mystery.class and Tester.class.
Your goal is to make Mystery.class smaller and faster. You can make any changes you want as long as your modified Mystery class still passes the test cases executed by java Tester.
The provided Tester class includes randomness so does not run exactly the same tests every execution. If you want to control the tester to make reproducible tests for your debugging, you can run it using java Tester [seed] [number] where seed is the seed used by the pseudorandom number generator (it doesn't matter what value you use for this, just pick a number) and [number] is the number of tests to run. If no parameters are given, Tester will run 1000 tests.
To modify the class file, you should use D-Java to disassemble it first, and then use jasmin to assemble a new class file. We recommend keeping a careful record (or backup copies) of the changes you make and re-running the tester regularly, so you notice right away if a change alters the expected behavior and can revert to a known good version.
CS216: Program and Data Representation
University of Virginia
Using these Materials