Project Application Development Building an IJVM emulator Sebastian - - PowerPoint PPT Presentation

project application development
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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 / 108
slide-2
SLIDE 2

Outline

1

Introduction

2

IJVM

3

The C Programming Language

4

Assignment

Sebastian ¨ Osterlund Project Application Development June, 2020 2 / 108
slide-3
SLIDE 3

Outline

1

Introduction

2

IJVM

3

The C Programming Language

4

Assignment

Sebastian ¨ Osterlund Project Application Development June, 2020 3 / 108
slide-4
SLIDE 4

Project Application Development

VUSec VUSec

Topics: Memory safety, Fuzz Testing, Microarchitectural Attacks (RIDL/ MDS).

Sebastian ¨ Osterlund Project Application Development June, 2020 4 / 108
slide-5
SLIDE 5

Goal 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 / 108
slide-6
SLIDE 6

The 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¨

  • ves

Anthony Wilkes Dorukhan Arslan Frank de Jong Eduardo Lira Peter Kementzey

Sebastian ¨ Osterlund Project Application Development June, 2020 6 / 108
slide-7
SLIDE 7

Support

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 / 108
slide-8
SLIDE 8

Links

Material available at: http://vu-programming.gitlab.io/ pad/website/

Sebastian ¨ Osterlund Project Application Development June, 2020 8 / 108
slide-9
SLIDE 9

Setup of the course

Fewer than four weeks (!!) for one deliverable. The deliverable is split into five smaller sub-modules, each building

  • n the previous one.

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 / 108
slide-10
SLIDE 10

The assignment

tl;dr

Implement a subset of the Java Virtual Machine in C

Sebastian ¨ Osterlund Project Application Development June, 2020 10 / 108
slide-11
SLIDE 11

(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 / 108
slide-12
SLIDE 12

The 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 / 108
slide-13
SLIDE 13

Grading

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
slide-14
SLIDE 14

!

WARNING: very high workload! (at least in the first week)

Sebastian ¨ Osterlund Project Application Development June, 2020 14 / 108
slide-15
SLIDE 15

Additional 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 / 108
slide-16
SLIDE 16

Additional 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 / 108
slide-17
SLIDE 17

Support

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 / 108
slide-18
SLIDE 18

Outline

1

Introduction

2

IJVM

3

The C Programming Language

4

Assignment

Sebastian ¨ Osterlund Project Application Development June, 2020 18 / 108
slide-19
SLIDE 19

Virtual machines

Sebastian ¨ Osterlund Project Application Development June, 2020 19 / 108
slide-20
SLIDE 20

Virtual 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 / 108
slide-21
SLIDE 21

Virtual machines

Sebastian ¨ Osterlund Project Application Development June, 2020 21 / 108
slide-22
SLIDE 22

Virtual machines

Sebastian ¨ Osterlund Project Application Development June, 2020 22 / 108
slide-23
SLIDE 23

IJVM

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 / 108
slide-24
SLIDE 24

Stack

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 / 108
slide-25
SLIDE 25

Stack 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 / 108
slide-26
SLIDE 26

RPN example

Sebastian ¨ Osterlund Project Application Development June, 2020 26 / 108
slide-27
SLIDE 27

RPN example

Sebastian ¨ Osterlund Project Application Development June, 2020 27 / 108
slide-28
SLIDE 28

RPN example

Sebastian ¨ Osterlund Project Application Development June, 2020 28 / 108
slide-29
SLIDE 29

RPN example

Sebastian ¨ Osterlund Project Application Development June, 2020 29 / 108
slide-30
SLIDE 30

RPN example

Sebastian ¨ Osterlund Project Application Development June, 2020 30 / 108
slide-31
SLIDE 31

RPN example

Sebastian ¨ Osterlund Project Application Development June, 2020 31 / 108
slide-32
SLIDE 32

RPN example

Sebastian ¨ Osterlund Project Application Development June, 2020 32 / 108
slide-33
SLIDE 33

RPN example

Sebastian ¨ Osterlund Project Application Development June, 2020 33 / 108
slide-34
SLIDE 34

RPN example complete

Sebastian ¨ Osterlund Project Application Development June, 2020 34 / 108
slide-35
SLIDE 35

RPN example

12

#define STACK_SIZE 1024

13

int stack[STACK_SIZE];

14

int sp;

15 16

int pop()

17

{

18

assert(sp >= 0);

19

return stack[sp--];

20

}

21 22

int top()

23

{

24

assert(sp >= 0);

25

return stack[sp];

26

}

27 28

void push(int n)

29

{

30

assert(sp < STACK_SIZE - 1);

31

stack[++sp] = n;

32

}

stack array sp stack pointer

Sebastian ¨ Osterlund Project Application Development June, 2020 35 / 108
slide-36
SLIDE 36

RPN 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 / 108
slide-37
SLIDE 37

IJVM

Back to the IJVM

Sebastian ¨ Osterlund Project Application Development June, 2020 37 / 108
slide-38
SLIDE 38

IJVM

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 / 108
slide-39
SLIDE 39

Decoding

1 No argument (IADD, ISUB) 2 Byte argument (BIPUSH 0x42,

BIPUSH 0x1)

3 2 bytes argument/ short (GOTO

0x0010, IINC 0x2 0x1)

Sebastian ¨ Osterlund Project Application Development June, 2020 39 / 108
slide-40
SLIDE 40

Decoding

1 No argument (IADD, ISUB) 2 Byte argument (BIPUSH 0x42,

BIPUSH 0x1)

3 2 bytes argument/ short (GOTO

0x0010, IINC 0x2 0x1) 0x10 0x1?

Sebastian ¨ Osterlund Project Application Development June, 2020 39 / 108
slide-41
SLIDE 41

Decoding

1 No argument (IADD, ISUB) 2 Byte argument (BIPUSH 0x42,

BIPUSH 0x1)

3 2 bytes argument/ short (GOTO

0x0010, IINC 0x2 0x1) 0x10 0x1? BIPUSH 0x1 0x84 0x1 0x5?

Sebastian ¨ Osterlund Project Application Development June, 2020 39 / 108
slide-42
SLIDE 42

Decoding

1 No argument (IADD, ISUB) 2 Byte argument (BIPUSH 0x42,

BIPUSH 0x1)

3 2 bytes argument/ short (GOTO

0x0010, IINC 0x2 0x1) 0x10 0x1? BIPUSH 0x1 0x84 0x1 0x5? IINC 0x1 0x5

Sebastian ¨ Osterlund Project Application Development June, 2020 39 / 108
slide-43
SLIDE 43

Decoding

Note: some arguments are signed while others are not

Sebastian ¨ Osterlund Project Application Development June, 2020 40 / 108
slide-44
SLIDE 44

Signed vs. unsigned

Sebastian ¨ Osterlund Project Application Development June, 2020 41 / 108
slide-45
SLIDE 45

IJVM 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 / 108
slide-46
SLIDE 46

IJVM 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 / 108
slide-47
SLIDE 47

IJVM 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 / 108
slide-48
SLIDE 48

IJVM 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 / 108
slide-49
SLIDE 49

IJVM 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 / 108
slide-50
SLIDE 50

IJVM 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 / 108
slide-51
SLIDE 51

IJVM 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 / 108
slide-52
SLIDE 52

IJVM 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 / 108
slide-53
SLIDE 53

Assembler

Translates text into machine code Translates variable names into indices labels into offsets

Sebastian ¨ Osterlund Project Application Development June, 2020 44 / 108
slide-54
SLIDE 54

JAS 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 / 108
slide-55
SLIDE 55

IJVM 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 / 108
slide-56
SLIDE 56

IJVM 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 / 108
slide-57
SLIDE 57

IJVM program

2 3

L1:

4

BIPUSH 0xA // push 10 to stack

5

L2:

6

BIPUSH 0x1

7

ISUB // Subtract 1

8

DUP

9

IFEQ END // Jump to end if zero

10

BIPUSH 0x31

11

OUT // Print 1

12

GOTO L2 // Repeat loop

13 14

END:

15

HALT 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 / 108
slide-58
SLIDE 58

Recap

So far we have seen:

stack (machines) instruction decoding assembly branching

Sebastian ¨ Osterlund Project Application Development June, 2020 49 / 108
slide-59
SLIDE 59

Methods

Last part: methods

Sebastian ¨ Osterlund Project Application Development June, 2020 50 / 108
slide-60
SLIDE 60

Methods

re-usable subroutines for example add(a, b) also supports recursion last thing you have to implement (but can be

  • challenging. . . )

.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 / 108
slide-61
SLIDE 61

Invocation

Sebastian ¨ Osterlund Project Application Development June, 2020 52 / 108
slide-62
SLIDE 62

Return

Sebastian ¨ Osterlund Project Application Development June, 2020 53 / 108
slide-63
SLIDE 63

IJVM

IJVM: done!

Sebastian ¨ Osterlund Project Application Development June, 2020 54 / 108
slide-64
SLIDE 64

Outline

1

Introduction

2

IJVM

3

The C Programming Language

4

Assignment

Sebastian ¨ Osterlund Project Application Development June, 2020 55 / 108
slide-65
SLIDE 65

Where are we?

1

Introduction

2

IJVM

3

The C Programming Language

4

Assignment

Sebastian ¨ Osterlund Project Application Development June, 2020 56 / 108
slide-66
SLIDE 66

History

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 / 108
slide-67
SLIDE 67

Famous 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 / 108
slide-68
SLIDE 68

Hello World

Sebastian ¨ Osterlund Project Application Development June, 2020 59 / 108
slide-69
SLIDE 69

C 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 / 108
slide-70
SLIDE 70

C vs. C++

Upside: more control and is (arguably) easier to master

Sebastian ¨ Osterlund Project Application Development June, 2020 61 / 108
slide-71
SLIDE 71

C 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 / 108
slide-72
SLIDE 72

C 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 / 108
slide-73
SLIDE 73

Basics

#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 / 108
slide-74
SLIDE 74

What 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 / 108
slide-75
SLIDE 75

Warning

I will be very brief. Feel free to ask questions at any time!

Sebastian ¨ Osterlund Project Application Development June, 2020 66 / 108
slide-76
SLIDE 76

Constants 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 / 108
slide-77
SLIDE 77

Type 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 / 108
slide-78
SLIDE 78

Pointers

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 / 108
slide-79
SLIDE 79

Arrays

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 / 108
slide-80
SLIDE 80

Types 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 / 108
slide-81
SLIDE 81

Heap

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 / 108
slide-82
SLIDE 82

Pointer/ array

int *myArray = malloc(sizeof(int) * 5); free(myArray);

Sebastian ¨ Osterlund Project Application Development June, 2020 73 / 108
slide-83
SLIDE 83

Strings

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 / 108
slide-84
SLIDE 84

Structs

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 / 108
slide-85
SLIDE 85

I/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 / 108
slide-86
SLIDE 86

I/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 / 108
slide-87
SLIDE 87

Using 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 / 108
slide-88
SLIDE 88

Switch statement

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

}

Sebastian ¨ Osterlund Project Application Development June, 2020 79 / 108
slide-89
SLIDE 89

Code 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 / 108
slide-90
SLIDE 90

Code 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 / 108
slide-91
SLIDE 91

What is wrong here?

1

void bad(int* a, int len_a, int* b, int len_b){

2

int sum_a = 0;

3

for(int i = 0 ; i < len_a; i++){

4

sum_a+= a[i];

5

}

6

int average_a = sum_a / len_a;

7

int sum_b = 0;

8

for(int i = 0 ; i < len_b; i++){

9

sum_b+= a[i];

10

}

11

int average_b = sum_b / len_b;

12 13

...

14

}

15 Sebastian ¨ Osterlund Project Application Development June, 2020 82 / 108
slide-92
SLIDE 92

Better

1

double average(int* a, int len) {

2

double sum = 0;

3

for(int i = 0 ; i < len; i++){

4

sum+= a[i];

5

}

6

return sum / len;

7

}

8 9 10

void better(int* a, int len_a, int* b, int len_b){

11

double average_a = average(a,len_a);

12

double 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 / 108
slide-93
SLIDE 93

What is wrong here?

1

void iadd(int* pc, word_t* stack, int* sp, bool* halted) {

2

...

3

}

4 5

void isub(int* pc, word_t* stack, int* sp, bool* halted) {

6

...

7

}

8 9

void bipush(byte a, int* pc, word_t* stack, int* sp, bool* halted) {

10

...

11

}

Sebastian ¨ Osterlund Project Application Development June, 2020 84 / 108
slide-94
SLIDE 94

Better

1

typedef struct {

2

int pc;

3

word_t* stack;

4

int sp;

5

bool halted

6

} jvm_state;

7 8 9 10

void iadd(jvm_state* state) {

11

...

12

}

13 14

void isub(jvm_state* state) {

15

...

16

}

17 18

void 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 / 108
slide-95
SLIDE 95

What does this do?

1

#include <stdio.h>

2 3 4

void main(){

5

int f, c;

6

int l = 0;

7

int u = 300;

8

int s = 20;

9

for(f = l ; f <= u ; f+=s ) {

10

c = 5 * (f

  • 32) / 9;
11

printf("%d\t%d\n",f,c);

12

}

13

}

Sebastian ¨ Osterlund Project Application Development June, 2020 86 / 108
slide-96
SLIDE 96

Better

1

#include <stdio.h>

2 3

int fahr_to_celcius(int fahr) {

4

return 5 * (fahr - 32) / 9;

5

}

6 7

void print_fahrenheit_to_celcius_table() {

8

int fahr, celcius;

9

int lower_fahr = 0;

10

int upper_fahr = 300;

11

int step_fahr = 20;

12

for(fahr = lower_fahr; fahr <= upper_fahr; fahr+= step_fahr) {

13

printf("%d\t%d\n",fahr,fahr_to_celcius(fahr));

14

}

15

}

16 17

void main(){

18

print_fahrenheit_to_celcius_table();

19

}

Sebastian ¨ Osterlund Project Application Development June, 2020 87 / 108
slide-97
SLIDE 97

A word about style

Use more restrictive compiler flags -Wall -Werror -Wpedantic

  • Wextra. The compiler can prevent many common bugs by using

pedantic flags (see Makefile for complete list) Avoid memory leaks (test with valgrind and LLVM sanitizers)

Sebastian ¨ Osterlund Project Application Development June, 2020 88 / 108
slide-98
SLIDE 98

Outline

1

Introduction

2

IJVM

3

The C Programming Language

4

Assignment

Sebastian ¨ Osterlund Project Application Development June, 2020 89 / 108
slide-99
SLIDE 99

Overview

Sebastian ¨ Osterlund Project Application Development June, 2020 90 / 108
slide-100
SLIDE 100

Skeleton

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 / 108
slide-101
SLIDE 101

Skeleton

. 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 files

include: header files src: source files (work here) tests: automatic tests tools: other tools (goJASM)

Sebastian ¨ Osterlund Project Application Development June, 2020 92 / 108
slide-102
SLIDE 102

Grading

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 / 108
slide-103
SLIDE 103

Tests

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 / 108
slide-104
SLIDE 104

Useful 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 / 108
slide-105
SLIDE 105

Assignment 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
slide-106
SLIDE 106

Assignment 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
slide-107
SLIDE 107

Assignment 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
slide-108
SLIDE 108

Assignment 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
slide-109
SLIDE 109

Assignment 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
slide-110
SLIDE 110

Assignment 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
slide-111
SLIDE 111

Assignment 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
slide-112
SLIDE 112

Assignment 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
slide-113
SLIDE 113

!

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 / 108
slide-114
SLIDE 114

Plan 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 / 108
slide-115
SLIDE 115

How 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 example

code 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 a

better understanding of the binary format

7 Run the test: make test1 followed by ./test1 Sebastian ¨ Osterlund Project Application Development June, 2020 99 / 108
slide-116
SLIDE 116

I do not pass the test? What now?

Sebastian ¨ Osterlund Project Application Development June, 2020 100 / 108
slide-117
SLIDE 117

I 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 / 108
slide-118
SLIDE 118

I do not pass the test? What now?

Sebastian ¨ Osterlund Project Application Development June, 2020 102 / 108
slide-119
SLIDE 119

I do not pass the test? What now?

Sebastian ¨ Osterlund Project Application Development June, 2020 103 / 108
slide-120
SLIDE 120

!!

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
slide-121
SLIDE 121

!!!

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
slide-122
SLIDE 122

!!!!

But most of all have fun!

enjoy debugging memory errors. . .

Sebastian ¨ Osterlund Project Application Development June, 2020 106 / 108
slide-123
SLIDE 123

Support

24/7 @

https://discuss.ridl.eu

Sebastian ¨ Osterlund Project Application Development June, 2020 107 / 108
slide-124
SLIDE 124

VUSec VUSec

For info on (bachelor) projects, visit https://vusec.net

Sebastian ¨ Osterlund Project Application Development June, 2020 108 / 108