Project Application Development
Building an IJVM emulator Sebastian ¨ Osterlund
s.osterlund@vu.nl
Department of Computer Science Vrije Universiteit Amsterdam
June, 2020
Sebastian ¨ Osterlund Project Application Development June, 2020 1 / 108
Project Application Development Building an IJVM emulator Sebastian - - PowerPoint PPT Presentation
Project Application Development Building an IJVM emulator Sebastian Osterlund s.osterlund@vu.nl Department of Computer Science Vrije Universiteit Amsterdam June, 2020 Sebastian Osterlund Project Application Development June, 2020 1
Project Application Development
Building an IJVM emulator Sebastian ¨ Osterlund
s.osterlund@vu.nl
Department of Computer Science Vrije Universiteit Amsterdam
June, 2020
Sebastian ¨ Osterlund Project Application Development June, 2020 1 / 108Outline
1
Introduction
2
IJVM
3
The C Programming Language
4
Assignment
Sebastian ¨ Osterlund Project Application Development June, 2020 2 / 108Outline
1
Introduction
2
IJVM
3
The C Programming Language
4
Assignment
Sebastian ¨ Osterlund Project Application Development June, 2020 3 / 108Project Application Development
Topics: Memory safety, Fuzz Testing, Microarchitectural Attacks (RIDL/ MDS).
Sebastian ¨ Osterlund Project Application Development June, 2020 4 / 108Goal of this course
The main aim of this course is to give the student a more hands-on practical experience with programming. By implementing a larger project with a basis in material covered during the first year of the Computer Science bachelor, you will have the opportunity to gain more experience developing software.
Sebastian ¨ Osterlund Project Application Development June, 2020 5 / 108The PAD team
Sebastian ¨ Osterlund (me) Atze van der Ploeg (lecturer) Andrea di Dio (Q&A sessions) Floris Gorter Kristian Laursen Ruby Bar-Ner Aron Heijstek Eduard Haivas Daniel K¨
Anthony Wilkes Dorukhan Arslan Frank de Jong Eduardo Lira Peter Kementzey
Sebastian ¨ Osterlund Project Application Development June, 2020 6 / 108Support
1 First point of contact is our discussion board:https://discuss.ridl.eu Read the rules of the discussion board (do not post long code snippets, etc.)
2 Lab sessions → online group Q&A sessions 3 Your TA Sebastian ¨ Osterlund Project Application Development June, 2020 7 / 108Links
Material available at: http://vu-programming.gitlab.io/ pad/website/
Sebastian ¨ Osterlund Project Application Development June, 2020 8 / 108Setup of the course
Fewer than four weeks (!!) for one deliverable. The deliverable is split into five smaller sub-modules, each building
Four meetings to discuss your progress with your Teaching Assistant. The deliverable is to be implemented individually. Finally, one last meeting to evaluate your submitted program (Monday June 29th). This evaluation can be scheduled at an earlier date on an individual basis. Deadline: Friday June 26th 23:59 CEST.
Sebastian ¨ Osterlund Project Application Development June, 2020 9 / 108The assignment
tl;dr
Implement a subset of the Java Virtual Machine in C
Sebastian ¨ Osterlund Project Application Development June, 2020 10 / 108(I)JVM?
An Instruction Set Architecture (ISA) consisting of a subset of the JVM ISA, created for educational purposes. Only operations on integers are considered Your task is to create a program that can execute IJVM binaries. I.e., implement a subset of the Java Virtual Machine.
Sebastian ¨ Osterlund Project Application Development June, 2020 11 / 108The five modules
The course consists of 6 modules (5 mandatory), spread over 4 meetings
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO).Module 4: Local variables and constant pool.
4 Module 5: Methods.Module 6: (Additional features).
Sebastian ¨ Osterlund Project Application Development June, 2020 12 / 108Grading
How is my submission graded?
40% based on basic tests (must pass all) 30% based on advanced tests 10% based on style and general impression (this includes the impression during the final evaluation meeting). A higher grade can be achieved by implementing additional functionality (see chapter 6 of the course manual). Pick-and-choose as you like. Capped at 30%.
Sebastian ¨ Osterlund Project Application Development June, 2020 13 / 108!
WARNING: very high workload! (at least in the first week)
Sebastian ¨ Osterlund Project Application Development June, 2020 14 / 108Additional info
Be well prepared for your first meeting! You should already be well on your way implementing the first task. Try to get the first three modules done as quickly as possible. The toughest part is getting everything to work correctly. Enrollment for the meetings opens today at 18:00 CEST. See Canvas. The manual is available right now! Start reading it today.
Sebastian ¨ Osterlund Project Application Development June, 2020 15 / 108Additional info
There will be four Q&A sessions:
Today 2.06 at 13:30 Tuesday 9.06 at 13:30) Tuesday 16.06 at 13:30) Tuesday 23.06 at 13:30)
Sebastian ¨ Osterlund Project Application Development June, 2020 16 / 108Support
1 First point of contact is our discussion board:https://discuss.ridl.eu Read the rules of the discussion board (do not post long code snippets, etc.)
2 Lab sessions 3 Your TA Sebastian ¨ Osterlund Project Application Development June, 2020 17 / 108Outline
1
Introduction
2
IJVM
3
The C Programming Language
4
Assignment
Sebastian ¨ Osterlund Project Application Development June, 2020 18 / 108Virtual machines
Sebastian ¨ Osterlund Project Application Development June, 2020 19 / 108Virtual machines
Run an emulated/ virtual version of a computer system. ISA: Instruction Set Architecture
x86, x86-64, RISC-V, JVM, AARCH64
Sebastian ¨ Osterlund Project Application Development June, 2020 20 / 108Virtual machines
Sebastian ¨ Osterlund Project Application Development June, 2020 21 / 108Virtual machines
Sebastian ¨ Osterlund Project Application Development June, 2020 22 / 108IJVM
IJVM: Integer Java Virtual Machine Subset of the JVM. Has 24 instructions. Created for educational purposes by Andy Tanenbaum Stack machine
Sebastian ¨ Osterlund Project Application Development June, 2020 23 / 108Stack
LIFO datastructure Operations:
pop() top()/ peek() push() size()
Implemented using an array and a counter (stack pointer)
Sebastian ¨ Osterlund Project Application Development June, 2020 24 / 108Stack machine
A (simple) machine operating on a stack RPN (Reverse Polish Notation) calculator 12+, 111++, 112-1++ ISA: +, -, *, 0-9
Sebastian ¨ Osterlund Project Application Development June, 2020 25 / 108RPN example
Sebastian ¨ Osterlund Project Application Development June, 2020 26 / 108RPN example
Sebastian ¨ Osterlund Project Application Development June, 2020 27 / 108RPN example
Sebastian ¨ Osterlund Project Application Development June, 2020 28 / 108RPN example
Sebastian ¨ Osterlund Project Application Development June, 2020 29 / 108RPN example
Sebastian ¨ Osterlund Project Application Development June, 2020 30 / 108RPN example
Sebastian ¨ Osterlund Project Application Development June, 2020 31 / 108RPN example
Sebastian ¨ Osterlund Project Application Development June, 2020 32 / 108RPN example
Sebastian ¨ Osterlund Project Application Development June, 2020 33 / 108RPN example complete
Sebastian ¨ Osterlund Project Application Development June, 2020 34 / 108RPN example
12#define STACK_SIZE 1024
13int stack[STACK_SIZE];
14int sp;
15 16int pop()
17{
18assert(sp >= 0);
19return stack[sp--];
20}
21 22int top()
23{
24assert(sp >= 0);
25return stack[sp];
26}
27 28void push(int n)
29{
30assert(sp < STACK_SIZE - 1);
31stack[++sp] = n;
32}
stack array sp stack pointer
Sebastian ¨ Osterlund Project Application Development June, 2020 35 / 108RPN example
33 34 35 int main(int argc, char **argv) 36 { 37 sp = -1; 38 while (1) { 39 char c = getc(stdin); 40 int a,b; 41 42 switch (c) { 43 case ’+’: 44 a = pop(); 45 b = pop(); 46 push(a + b); 47 break; 48 case ’-’: 49 a = pop(); 50 b = pop(); 51 push(b - a); 52 break; 53 case ’*’: 54 a = pop(); 55 b = pop(); 56 push(a * b); 57 break; 58 case EOF: 59 // Terminate on CTRL-D 60 printf("= %d\n", top()); 61 exit(0); 62 break; 63 case ’\n’: 64 printf("= %d\n", top()); 65 break; 66 default: 67 push(c - ’0’); // Push ASCII character as integer 68 } 69 } 70 return 0; 71 } 1 Fetch [39] 2 Decode [42-] 3 Translate/ Execute Sebastian ¨ Osterlund Project Application Development June, 2020 36 / 108IJVM
Back to the IJVM
Sebastian ¨ Osterlund Project Application Development June, 2020 37 / 108IJVM
Stack operations: BIPUSH, POP, IADD, ISUB . . . Branching: GOTO, IFEQ, IFICMPEQ, IFLT Memory: ILOAD, ISTORE, IINC, LDC W I/O: IN, OUT Method invocation: INVOKEVIRTUAL, IRETURN HALT, ERR In total 24 instructions, complete list in the manual
Sebastian ¨ Osterlund Project Application Development June, 2020 38 / 108Decoding
1 No argument (IADD, ISUB) 2 Byte argument (BIPUSH 0x42,BIPUSH 0x1)
3 2 bytes argument/ short (GOTO0x0010, IINC 0x2 0x1)
Sebastian ¨ Osterlund Project Application Development June, 2020 39 / 108Decoding
1 No argument (IADD, ISUB) 2 Byte argument (BIPUSH 0x42,BIPUSH 0x1)
3 2 bytes argument/ short (GOTO0x0010, IINC 0x2 0x1) 0x10 0x1?
Sebastian ¨ Osterlund Project Application Development June, 2020 39 / 108Decoding
1 No argument (IADD, ISUB) 2 Byte argument (BIPUSH 0x42,BIPUSH 0x1)
3 2 bytes argument/ short (GOTO0x0010, IINC 0x2 0x1) 0x10 0x1? BIPUSH 0x1 0x84 0x1 0x5?
Sebastian ¨ Osterlund Project Application Development June, 2020 39 / 108Decoding
1 No argument (IADD, ISUB) 2 Byte argument (BIPUSH 0x42,BIPUSH 0x1)
3 2 bytes argument/ short (GOTO0x0010, IINC 0x2 0x1) 0x10 0x1? BIPUSH 0x1 0x84 0x1 0x5? IINC 0x1 0x5
Sebastian ¨ Osterlund Project Application Development June, 2020 39 / 108Decoding
Note: some arguments are signed while others are not
Sebastian ¨ Osterlund Project Application Development June, 2020 40 / 108Signed vs. unsigned
Sebastian ¨ Osterlund Project Application Development June, 2020 41 / 108IJVM memory model
pc program counter
incremented after each instruction modified by branching instructions
sp stack pointer memory consists of local frames
used for methods keeps a link to the previous frame
Sebastian ¨ Osterlund Project Application Development June, 2020 42 / 108IJVM memory model
pc program counter
incremented after each instruction modified by branching instructions
sp stack pointer memory consists of local frames
used for methods keeps a link to the previous frame
Sebastian ¨ Osterlund Project Application Development June, 2020 42 / 108IJVM memory model
pc program counter
incremented after each instruction modified by branching instructions
sp stack pointer memory consists of local frames
used for methods keeps a link to the previous frame
Sebastian ¨ Osterlund Project Application Development June, 2020 42 / 108IJVM memory model
pc program counter
incremented after each instruction modified by branching instructions
sp stack pointer memory consists of local frames
used for methods keeps a link to the previous frame
Sebastian ¨ Osterlund Project Application Development June, 2020 42 / 108IJVM memory model
pc program counter
incremented after each instruction modified by branching instructions
sp stack pointer memory consists of local frames
used for methods keeps a link to the previous frame
Sebastian ¨ Osterlund Project Application Development June, 2020 42 / 108IJVM memory model
pc program counter
incremented after each instruction modified by branching instructions
sp stack pointer memory consists of local frames
used for methods keeps a link to the previous frame
Sebastian ¨ Osterlund Project Application Development June, 2020 42 / 108IJVM memory model
pc program counter
incremented after each instruction modified by branching instructions
sp stack pointer memory consists of local frames
used for methods keeps a link to the previous frame
Sebastian ¨ Osterlund Project Application Development June, 2020 42 / 108IJVM programs
IJVM executes bytecode Not really human-readable. . . Java ASsembly (JAS) We have written our custom assembler for this course (goJASM)
Sebastian ¨ Osterlund Project Application Development June, 2020 43 / 108Assembler
Translates text into machine code Translates variable names into indices labels into offsets
Sebastian ¨ Osterlund Project Application Development June, 2020 44 / 108JAS program
.main L1: BIPUSH 0xA // push 10 to stack L2: BIPUSH 0x1 ISUB // Subtract 1 DUP IFEQ END // Jump to end if zero BIPUSH 0x31 OUT // Print 1 GOTO L2 // Repeat loop END: HALT .end-main
Sebastian ¨ Osterlund Project Application Development June, 2020 45 / 108IJVM program
./gojasm -o count.ijvm count.jas xxd count.ijvm
00000000: 1dea dfad 0001 0000 0000 0000 0000 0000 ................ 00000010: 0000 0010 100a 1001 6459 9900 0910 31fd ........dY....1. 00000020: a7ff f6ff ....
Sebastian ¨ Osterlund Project Application Development June, 2020 46 / 108IJVM program
00000000: 1dea dfad 0001 0000 0000 0000 0000 0000 ................ 00000010: 0000 0010 100a 1001 6459 9900 0910 31fd ........dY....1. 00000020: a7ff f6ff ....
Sebastian ¨ Osterlund Project Application Development June, 2020 47 / 108IJVM program
2 3L1:
4BIPUSH 0xA // push 10 to stack
5L2:
6BIPUSH 0x1
7ISUB // Subtract 1
8DUP
9IFEQ END // Jump to end if zero
10BIPUSH 0x31
11OUT // Print 1
12GOTO L2 // Repeat loop
13 14END:
15HALT 00000000: 1dea dfad 0001 0000 0000 0000 0000 0000 ................ 00000010: 0000 0010 100a 1001 6459 9900 0910 31fd ........dY....1. 00000020: a7ff f6ff ....
Sebastian ¨ Osterlund Project Application Development June, 2020 48 / 108Recap
So far we have seen:
stack (machines) instruction decoding assembly branching
Sebastian ¨ Osterlund Project Application Development June, 2020 49 / 108Methods
Last part: methods
Sebastian ¨ Osterlund Project Application Development June, 2020 50 / 108Methods
re-usable subroutines for example add(a, b) also supports recursion last thing you have to implement (but can be
.main BIPUSH 0x0 // OBJREF BIPUSH 0x2 BIPUSH 0x3 INVOKEVIRTUAL add BIPUSH 0x30 IADD OUT HALT .end-main .method add(a, b) ILOAD a ILOAD b IADD IRETURN .end-method
Sebastian ¨ Osterlund Project Application Development June, 2020 51 / 108Invocation
Sebastian ¨ Osterlund Project Application Development June, 2020 52 / 108Return
Sebastian ¨ Osterlund Project Application Development June, 2020 53 / 108IJVM
IJVM: done!
Sebastian ¨ Osterlund Project Application Development June, 2020 54 / 108Outline
1
Introduction
2
IJVM
3
The C Programming Language
4
Assignment
Sebastian ¨ Osterlund Project Application Development June, 2020 55 / 108Where are we?
1
Introduction
2
IJVM
3
The C Programming Language
4
Assignment
Sebastian ¨ Osterlund Project Application Development June, 2020 56 / 108History
Developed by Dennis Ritchie at Bell Labs 1960-1973 Used for developing UNIX No 2 on TIOBE programming language popularity index Still used for most systems programming nowadays!
Sebastian ¨ Osterlund Project Application Development June, 2020 57 / 108Famous programs written in C
Operating systems kernels: Windows, OSX, Linux , Unix Wolfenstein 3D, DOOM, Quake (1/2/3) Git Most linux tools (ls,awk, etc) Embedded systems (your microwave?) Much much more
Sebastian ¨ Osterlund Project Application Development June, 2020 58 / 108Hello World
Sebastian ¨ Osterlund Project Application Development June, 2020 59 / 108C vs. C++
No classes No generics/ templates No references (only pointers) Much smaller standard library No operator overloading No namespaces No streams (cin/ cout) No exception handling No RAII
Sebastian ¨ Osterlund Project Application Development June, 2020 60 / 108C vs. C++
Upside: more control and is (arguably) easier to master
Sebastian ¨ Osterlund Project Application Development June, 2020 61 / 108C vs. C++
Note: C++ is a superset of C
This means that most C code compiles as C++
Sebastian ¨ Osterlund Project Application Development June, 2020 62 / 108C is notorious for memory errors
Source of most (serious) errors and vulnerabilities!
(In fact Sebastian’s research is on memory safety)
Sebastian ¨ Osterlund Project Application Development June, 2020 63 / 108Basics
#include <stdio.h> // Include standard I/O header // main is called on start, with the number of command // line arguments stored in argc, and the pointers to the // arguments in argv. argv is an array of char pointers. int main(int argc, char **argv) { char *binary_name = argv[0]; if (argc < 2) { printf("expected 2 command line arguments, got %d\n", argc - 1); return 1; // return status code 1 for error } char *name = argv[1]; char *code = argv[2]; printf("Hello %s, you entered the code %s\n", name, code) return 0; // return status code 0 for normal execution }
Sebastian ¨ Osterlund Project Application Development June, 2020 64 / 108What we will cover
1 Arrays 2 Pointers 3 Memory management 4 Strings 5 Structs 6 I/O 7 Style Sebastian ¨ Osterlund Project Application Development June, 2020 65 / 108Warning
I will be very brief. Feel free to ask questions at any time!
Sebastian ¨ Osterlund Project Application Development June, 2020 66 / 108Constants in C
Define constant as #define MINUTES_PER_HOUR 60 This is essentially a text replace by the preprocessor Cannot use C++ construct (const int MINUTES_PER_HOUR = 60;)
Sebastian ¨ Osterlund Project Application Development June, 2020 67 / 108Type alias
define a type using: typedef existingtype newtype typedef int32_t word_t; Note: (u)intX t defined in stdint.h
Sebastian ¨ Osterlund Project Application Development June, 2020 68 / 108Pointers
A memory item that references an address Reference using &. char *myptr; char mychar = 0x42; myptr = &mychar; myptr points to the address of mychar char *myptr = &mychar;. Can be confusing! View char * as the type. Dereference using *: *myptr == 0x42;
Sebastian ¨ Osterlund Project Application Development June, 2020 69 / 108Arrays
A contiguous list of elements of a certain type Many ways to define:
int myarr[1024]; int myarr[] = {1,2,3}; int *myarr = malloc(1024);
Dereference an element:
First element: myarr[0]; n:th element: myarr[n];
Is essentially a pointer: myarr[42] is same as 42[myarr] is same as *(myarr + 42) No bounds checking!
Sebastian ¨ Osterlund Project Application Development June, 2020 70 / 108Types of Memory management
Static memory mangement (global variables) Automatic memory management (stack) Manual memory management, manual allocation/ deallocation on the heap
Sebastian ¨ Osterlund Project Application Development June, 2020 71 / 108Heap
Allocate using void *malloc(size_t n); (stdlib.h) Allocates n bytes of contiguous memory Returns a pointer Valid until void free(void *ptr) is called on that pointer
Sebastian ¨ Osterlund Project Application Development June, 2020 72 / 108Pointer/ array
int *myArray = malloc(sizeof(int) * 5); free(myArray);
Sebastian ¨ Osterlund Project Application Development June, 2020 73 / 108Strings
Unlike C++, C has no string type! A string is an array of characters, terminated by a NULL (’\0’) character. char mystr[] = {’h’, ’e’, ’l’, ’l’, ’o’, ’\0’}; char *mystr = "hello"; You can use it just like an array: mystr[2] == ’l’ Or point to a substring: char *mystr2 = &mystr[1]; // "ello" Built-in functions in C standard library (string.h): strdup(), strcat(), etc. Do not use ’==’ to compare strings! Use strcmp().
Sebastian ¨ Osterlund Project Application Development June, 2020 74 / 108Structs
A composite data type (record) Physically groups a list of variables under one name struct tag_name { type member1; type member2; /* declare as many members as desired, * but the entire structure size must * be known to the compiler. */ }; struct tag_name mystruct; Access fields: mystruct.member1 If you have a pointer: mystructptr->member1
Sebastian ¨ Osterlund Project Application Development June, 2020 75 / 108I/O
Writing:
printf("Hello\n "); fprintf(stderr, "OH NO!\n "); Read printf documentation! (man printf)
Reading:
char c = getc(stdin); fread(buf, sizeof(char), 1, stdin); scanf(...);
Sebastian ¨ Osterlund Project Application Development June, 2020 76 / 108I/O
Raw access using fopen(),fread(),fwrite() #include <stdio.h> int main(void) { FILE *f = fopen("myfile.txt", "r"); char buf[10]; fread(buf, sizeof(char), 10, f); }
Sebastian ¨ Osterlund Project Application Development June, 2020 77 / 108Using variables from other file
Access variable from other file (translation unit) using the extern keyword extern int mycounter; static variables cannot be accessed from other file (private)
Sebastian ¨ Osterlund Project Application Development June, 2020 78 / 108Switch statement
42switch (c) {
43case ’+’:
44a = pop();
45b = pop();
46push(a + b);
47break;
48case ’-’:
49a = pop();
50b = pop();
51push(b - a);
52break;
53case ’*’:
54a = pop();
55b = pop();
56push(a * b);
57break;
58case EOF:
59// Terminate on CTRL-D
60printf("= %d\n", top());
61exit(0);
62break;
63case ’\n’:
64printf("= %d\n", top());
65break;
66default:
67push(c - ’0’); // Push ASCII character as integer
68}
Sebastian ¨ Osterlund Project Application Development June, 2020 79 / 108Code style
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler
Sebastian ¨ Osterlund Project Application Development June, 2020 80 / 108Code style
Code should explain itself to reader Keep functions short (≈≤ 20 lines) Use meaningful names Do not repeat yourself (DRY) Avoid global variables if possible
Sebastian ¨ Osterlund Project Application Development June, 2020 81 / 108What is wrong here?
1void bad(int* a, int len_a, int* b, int len_b){
2int sum_a = 0;
3for(int i = 0 ; i < len_a; i++){
4sum_a+= a[i];
5}
6int average_a = sum_a / len_a;
7int sum_b = 0;
8for(int i = 0 ; i < len_b; i++){
9sum_b+= a[i];
10}
11int average_b = sum_b / len_b;
12 13...
14}
15 Sebastian ¨ Osterlund Project Application Development June, 2020 82 / 108Better
1double average(int* a, int len) {
2double sum = 0;
3for(int i = 0 ; i < len; i++){
4sum+= a[i];
5}
6return sum / len;
7}
8 9 10void better(int* a, int len_a, int* b, int len_b){
11double average_a = average(a,len_a);
12double average_b = average(b,len_b);
13...
14}
If you are copy and pasting code, you are doing something wrong. Do not repeat yourself (DRY) Easier to read
Sebastian ¨ Osterlund Project Application Development June, 2020 83 / 108What is wrong here?
1void iadd(int* pc, word_t* stack, int* sp, bool* halted) {
2...
3}
4 5void isub(int* pc, word_t* stack, int* sp, bool* halted) {
6...
7}
8 9void bipush(byte a, int* pc, word_t* stack, int* sp, bool* halted) {
10...
11}
Sebastian ¨ Osterlund Project Application Development June, 2020 84 / 108Better
1typedef struct {
2int pc;
3word_t* stack;
4int sp;
5bool halted
6} jvm_state;
7 8 9 10void iadd(jvm_state* state) {
11...
12}
13 14void isub(jvm_state* state) {
15...
16}
17 18void bipush(byte a, jvm_state* state) {
19...
20}
If you are copy and pasting code, you are doing something wrong. Do not repeat yourself (DRY) Easier to read
Sebastian ¨ Osterlund Project Application Development June, 2020 85 / 108What does this do?
1#include <stdio.h>
2 3 4void main(){
5int f, c;
6int l = 0;
7int u = 300;
8int s = 20;
9for(f = l ; f <= u ; f+=s ) {
10c = 5 * (f
printf("%d\t%d\n",f,c);
12}
13}
Sebastian ¨ Osterlund Project Application Development June, 2020 86 / 108Better
1#include <stdio.h>
2 3int fahr_to_celcius(int fahr) {
4return 5 * (fahr - 32) / 9;
5}
6 7void print_fahrenheit_to_celcius_table() {
8int fahr, celcius;
9int lower_fahr = 0;
10int upper_fahr = 300;
11int step_fahr = 20;
12for(fahr = lower_fahr; fahr <= upper_fahr; fahr+= step_fahr) {
13printf("%d\t%d\n",fahr,fahr_to_celcius(fahr));
14}
15}
16 17void main(){
18print_fahrenheit_to_celcius_table();
19}
Sebastian ¨ Osterlund Project Application Development June, 2020 87 / 108A word about style
Use more restrictive compiler flags -Wall -Werror -Wpedantic
pedantic flags (see Makefile for complete list) Avoid memory leaks (test with valgrind and LLVM sanitizers)
Sebastian ¨ Osterlund Project Application Development June, 2020 88 / 108Outline
1
Introduction
2
IJVM
3
The C Programming Language
4
Assignment
Sebastian ¨ Osterlund Project Application Development June, 2020 89 / 108Overview
Sebastian ¨ Osterlund Project Application Development June, 2020 90 / 108Skeleton
How to checkout skeleton
https://github.com/VU-Programming/pad-skeleton-c.git
How to build
make ./ijvm mybinary.ijvm
How to test
make test1 ./test1 make testbasic make testall
Sebastian ¨ Osterlund Project Application Development June, 2020 91 / 108Skeleton
. Makefile README.md include ijvm.h util.h src binary.c binary.h ijvm.c machine.c machine.h main.c tests test1.c test2.c test3.c test4.c test5.c testadvanced1.c testadvanced2.c testadvanced3.c testadvanced4.c testadvancedstack.c testutil.h tools Makefile 4 directories, 22 filesinclude: header files src: source files (work here) tests: automatic tests tools: other tools (goJASM)
Sebastian ¨ Osterlund Project Application Development June, 2020 92 / 108Grading
How is my submission graded?
40% based on basic tests (must pass all) 30% based on advanced tests 10% based on style and general impression (this includes the impression during the final evaluation meeting). A higher grade can be achieved by implementing additional functionality (see chapter 6 of the course manual). Pick-and-choose as you like. Capped at 30%.
Sebastian ¨ Osterlund Project Application Development June, 2020 93 / 108Tests
tests/testX.c: 5 basic tests (need to pass all) tests/testadvanced(X|stack).c: 20 advanced test cases tests/testbonusheap.c: test for heap additional functionality Example: pass all basic tests and all cases in testadvanced1,2,3,4,5 ⇒ 40% + 10
20 × 30% = 55%. If you then get 0% for style, you pass the
course with a 6
Sebastian ¨ Osterlund Project Application Development June, 2020 94 / 108Useful tools
gdb: debugger xxd/ hexdump: print binaries in hex git: version control Editor/ IDE: vim, emacs, Atom, Visual Studio, CLion
Sebastian ¨ Osterlund Project Application Development June, 2020 95 / 108Assignment recap
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO). 4 Module 4: Local variables and constant pool. 5 Module 5: Methods. 6 Module 6: Additional functionality.Networking, Heap, Debugger, Snapshoting, Hardening
Sebastian ¨ Osterlund Project Application Development June, 2020 96 / 108Assignment recap
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO). 4 Module 4: Local variables and constant pool. 5 Module 5: Methods. 6 Module 6: Additional functionality.Networking, Heap, Debugger, Snapshoting, Hardening
Sebastian ¨ Osterlund Project Application Development June, 2020 96 / 108Assignment recap
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO). 4 Module 4: Local variables and constant pool. 5 Module 5: Methods. 6 Module 6: Additional functionality.Networking, Heap, Debugger, Snapshoting, Hardening
Sebastian ¨ Osterlund Project Application Development June, 2020 96 / 108Assignment recap
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO). 4 Module 4: Local variables and constant pool. 5 Module 5: Methods. 6 Module 6: Additional functionality.Networking, Heap, Debugger, Snapshoting, Hardening
Sebastian ¨ Osterlund Project Application Development June, 2020 96 / 108Assignment recap
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO). 4 Module 4: Local variables and constant pool. 5 Module 5: Methods. 6 Module 6: Additional functionality.Networking, Heap, Debugger, Snapshoting, Hardening
Sebastian ¨ Osterlund Project Application Development June, 2020 96 / 108Assignment recap
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO). 4 Module 4: Local variables and constant pool. 5 Module 5: Methods. 6 Module 6: Additional functionality.Networking, Heap, Debugger, Snapshoting, Hardening
Sebastian ¨ Osterlund Project Application Development June, 2020 96 / 108Assignment recap
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO). 4 Module 4: Local variables and constant pool. 5 Module 5: Methods. 6 Module 6: Additional functionality.Networking, Heap, Debugger, Snapshoting, Hardening
Sebastian ¨ Osterlund Project Application Development June, 2020 96 / 108Assignment recap
1 Module 1: Loading a binary. 2 Module 2: Stack manipulations. 3 Module 3: Branching. (e.g. GOTO). 4 Module 4: Local variables and constant pool. 5 Module 5: Methods. 6 Module 6: Additional functionality.Networking, Heap, Debugger, Snapshoting, Hardening
Sebastian ¨ Osterlund Project Application Development June, 2020 96 / 108!
Get started today!
If you have trouble getting started, there is a Q&A session later today
Sebastian ¨ Osterlund Project Application Development June, 2020 97 / 108Plan of action
1 Download skeleton and get it to compile 2 Sign up for a meeting time-slot today 3 Start working on the assignment ASAP Sebastian ¨ Osterlund Project Application Development June, 2020 98 / 108How do I start?
1 Have a look at src/machine.c 2 Try to compile the tests make test1 (will fail at first!!) 3 Start by adding the missing functions for this module: step(),text size(), get text(), get program counter(). You can add these to src/machine.c
4 Start implementing init ijvm(). See the manual for some examplecode on how to read files
5 Read/ parse the blocks in the binary and save them in memory 6 Use the resources on the website (IJVM Binary Explorer) to get abetter understanding of the binary format
7 Run the test: make test1 followed by ./test1 Sebastian ¨ Osterlund Project Application Development June, 2020 99 / 108I do not pass the test? What now?
Sebastian ¨ Osterlund Project Application Development June, 2020 100 / 108I do not pass the test? What now?
You can debug the test using gdb ./test1 (Use lldb or see special instructions for macOS)
Sebastian ¨ Osterlund Project Application Development June, 2020 101 / 108I do not pass the test? What now?
Sebastian ¨ Osterlund Project Application Development June, 2020 102 / 108I do not pass the test? What now?
Sebastian ¨ Osterlund Project Application Development June, 2020 103 / 108!!
Be prepared for your first meeting
We expect you to be mostly done with module 1
Sebastian ¨ Osterlund Project Application Development June, 2020 104 / 108!!!
Let your TA know if you will be absent
If you don’t cancel, the TA is free to move your timeslot around
Sebastian ¨ Osterlund Project Application Development June, 2020 105 / 108!!!!
But most of all have fun!
enjoy debugging memory errors. . .
Sebastian ¨ Osterlund Project Application Development June, 2020 106 / 108Support
24/7 @
https://discuss.ridl.eu
Sebastian ¨ Osterlund Project Application Development June, 2020 107 / 108For info on (bachelor) projects, visit https://vusec.net
Sebastian ¨ Osterlund Project Application Development June, 2020 108 / 108