0xcafebabe (java byte codes)

30
David Evans http://www.cs.virginia.edu/ evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)

Upload: hoangdien

Post on 01-Jan-2017

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 0xCAFEBABE (Java Byte Codes)

David Evanshttp://www.cs.virginia.edu/evans

CS201j: Engineering SoftwareUniversity of VirginiaComputer Science

Lecture 18: 0xCAFEBABE (Java Byte Codes)

Page 2: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 2

Menu

• Running Programs– Crash Course in Architecture (CS333) – Crash Course in Compilers (CS571)

• Java Virtual Machine• Byte Codes

Page 3: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 3

Computer Architecture

Processor does computationMemory

stores bitsInput Devices (mouse, keyboard, accelerometer)

get input from user and environmentOutput Devices (display, speakers)

present output to user

Page 4: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 4

Central Processing Unit (CPU)

Page 5: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 5

Intel 4004

• First general purpose microprocessor, 1971

• 4-bit data• 46 instructions

– 8-bit instructions!

Page 6: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 6

PC Motherboard

From http://www.cyberiapc.com/hardwarebeg.htm

Memory

CPU

Page 7: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 7

Inside the CPU

• Registers• Loads and decodes instructions from

memory• ALU: Arithmetic Logic Unit

– Does arithmetic– Can only operate on values in registers– Must load values from memory into registers

before computing with them

Page 8: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 8

Compiler

• Translates a program in a high-level language into machine instructions

• Calling convention– How are parameters passed to functions– How is the stack managed to return

• Register allocation– Figure out how to use registers efficiently

Page 9: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 9

6: int max (int a, int b) {00401010 push ebp00401011 mov ebp,esp00401013 sub esp,40h00401016 push ebx00401017 push esi00401018 push edi00401019 lea edi,[ebp-40h]0040101C mov ecx,10h00401021 mov eax,0CCCCCCCCh00401026 rep stos dword ptr [edi]7: if (a > b) {00401028 mov eax,dword ptr [ebp+8]0040102B cmp eax,dword ptr [ebp+0Ch]0040102E jle max+25h (00401035)8: return b;00401030 mov eax,dword ptr [ebp+0Ch]00401033 jmp max+28h (00401038)9: } else {10: return a;00401035 mov eax,dword ptr [ebp+8]00401038 pop edi00401039 pop esi0040103A pop ebx0040103B mov esp,ebp0040103D pop ebp0040103E ret

int max (int a, int b) { if (a > b) { return b; } else { return a; }}

push instruction is 1 byte

In Visual C++, see assemblycode by running Debug, thenWindow | Disassembly

mov instruction is 2 bytes Dealing withfunction call:updating stack,moving arguments

Cleanup and return

Page 10: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 10

Java Virtual Machine

Page 11: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 11

Java Ring (1998)

Page 12: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 12

Java Card

Page 13: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 13

Java Virtual Machine

• Small and simple to implement• All VMs will run all programs the same

way• Secure

Page 14: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 14

Implementing the JavaVM

load class into memoryset the instruction pointer to point to the beginning of maindo { fetch the next instruction execute that instruction } while (there is more to do);

Some other issues we will talk about Thursday and next week:Verification – need to check byte codes satisfy security policyGarbage collection – need to reclaim unused storage

Page 15: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 15

Java Byte Codes

• Stack-based virtual machine• Small instruction set: 202 instructions (all

are 1 byte opcode + operands)– Intel x86: ~280 instructions (1 to 17 bytes

long!)• Memory is typed• Every Java class file begins with magic

number 3405691582 = 0xCAFEBABE in base 16

Page 16: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 16

Stack-Based Computation

• push – put something on the top of the stack

• pop – get and remove the top of the stack

Stackpush 2 2

push 3 3

addDoes 2 pops, pushes sum

5

Page 17: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 17

Some Java InstructionsOpcode Mnemonic Description

0 nop Does nothing

1 aconst_null Push null on the stack

3 iconst_0 Push int 0 on the stack

4 iconst_1 Push int 1 on the stack

Page 18: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 18

Some Java InstructionsOpcode Mnemonic Description

18 ldc <value> Push a one-word (4 bytes) constant onto the stack

ldc “Hello”ldc 201

Constant may be an int, float or String

The String is really a reference to an entry in the string constant table!

Page 19: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 19

ArithmeticOpcode Mnemonic Description

96 iadd Pops two integers from the stack and pushes their sum

iconst_2iconst_3iadd

Page 20: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 20

ArithmeticOpcode Mnemonic Description

96 iadd Pops two integers from the stack and pushes their sum

97 ladd Pops two long integers from the stack and pushes their sum

…106 fmul Pops two floats from the stack and

pushes their product

…119 dneg Pops a double from the stack, and

pushes its negation

Page 21: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 21

Java Byte Code Instructions

• 0: nop• 1-20: putting constants on the stack• 96-119: arithmetic on ints, longs, floats,

doubles

• What other kinds of instructions do we need?

Page 22: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 22

Other Instruction Classes• Control Flow (~20 instructions)

– if, goto, return• Method Calls (4 instructions)• Loading and Storing Variables (65

instructions)• Creating objects (1 instruction)• Using object fields (4 instructions)• Arrays (3 instructions)

Page 23: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 23

Control Flow

• ifeq <label>Pop an int off the stack. If it is zero, jump to the label. Otherwise, continue normally.

• if_icmple <label>Pop two ints off the stack. If the second one is <= the first one, jump to the label. Otherwise, continue normally.

Page 24: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 24

Method Calls• invokevirtual <method>

– Invokes the method <method> on the parameters and object on the top of the stack.

– Finds the appropriate method at run-time based on the actual type of the this object.

invokevirtual <Method void println(java.lang.String)>

Page 25: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 25

Method Calls• invokestatic <method>

– Invokes a static (class) method <method> on the parameters on the top of the stack.

– Finds the appropriate method at run-time based on the actual type of the this object.

Page 26: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 26

Example

public class Sample1 { static public void main (String args[]) { System.err.println ("Hello!"); System.exit (1); }}

Page 27: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 27

> javap -c Sample1Compiled from Sample1.javapublic class Sample1 extends java.lang.Object { public Sample1(); public static void main(java.lang.String[]);}

Method Sample1() 0 aload_0 1 invokespecial #1 <Method java.lang.Object()> 4 return

Method void main(java.lang.String[]) 0 getstatic #2 <Field java.io.PrintStream err> 3 ldc #3 <String "Hello!"> 5 invokevirtual #4 <Method void println(java.lang.String)> 8 iconst_1 9 invokestatic #5 <Method void exit(int)> 12 return

public class Sample1 { static public void main (String args[]) { System.err.println ("Hello!"); System.exit (1); } }

Page 28: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 28

Referencing Memory

• iload <varnum>– Pushes the int in local variable <varnum> (1

bytes) on the stack• istore <varnum>

– Pops the int on the top of the stack and stores it in local variable <varnum>

Page 29: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 29

Referencing ExampleMethod void main(java.lang.String[]) 0 iconst_2 1 istore_1 2 iconst_3 3 istore_2 4 iload_1 5 iload_2 6 iadd 7 istore_3 8 getstatic #2 <Field java.io.PrintStream err> 11 new #3 <Class java.lang.StringBuffer> 14 dup 15 invokespecial #4 <Method java.lang.StringBuffer()> 18 ldc #5 <String "c: "> 20 invokevirtual #6 <Method java.lang.StringBuffer append(java.lang.String)> 23 iload_3 24 invokevirtual #7 <Method java.lang.StringBuffer append(int)> 27 invokevirtual #8 <Method java.lang.String toString()> 30 invokevirtual #9 <Method void println(java.lang.String)> 33 return

public class Locals1 { static public void main (String args[]) { int a = 2; int b = 3; int c = a + b;

System.err.println ("c: " + c); } }

Page 30: 0xCAFEBABE (Java Byte Codes)

4 November 2003 CS 201J Fall 2003 30

Charge• PS6 will involve reading and writing Java

byte codes• Use javap –c <classname> to look at

what the javac compiler produces for your code

• Thursday: what would this program do?Method void main(java.lang.String[]) 0 iconst_2 1 iadd 2 return