Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.

Slides:



Advertisements
Similar presentations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Advertisements

Virtual Machines Matthew Dwyer 324E Nichols Hall
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CS 153: Concepts of Compiler Design November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Lightweight Modeling of Java Virtual Machine Security Constraints using Alloy Mark Reynolds BU CS511 Midterm Report March 26, 2008.
Lab 9 Java Bytecode & The Jasmin Assembler
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Consider With x = 10 we may proceed as (10-1) = 9 (10-7) = 3 (9*3) = 27 (10-11) = -1 27/(-1) = -27 Writing intermediates on paper.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Compilation 2007 The Java Virtual Machine Michael I. Schwartzbach BRICS, University of Aarhus.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)
Application Security Tom Chothia Computer Security, Lecture 14.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009.
Arpit Jain Mtech1. Outline Introduction Dalvik VM Java VM Examples Comparisons Experimental Evaluation.
CSCI 273: Processing An Introduction. Programming Languages –An abstract "human understandable" language for telling the computer what to do –The abstract.
The Java Virtual Machine 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Krakatoa: Decompilation in Java “Does Bytecode Reveal Source?” Todd A. Proebsting Scott A. Watterson The University of Arizona Presented by Karl von Randow.
Lecture 10 : Introduction to Java Virtual Machine
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
Introduction to the Java Virtual Machine 井民全. JVM (Java Virtual Machine) the environment in which the java programs execute The specification define an.
1 Introduction to JVM Based on material produced by Bill Venners.
Roopa.T PESIT, Bangalore. Source and Credits Dalvik VM, Dan Bornstein Google IO 2008 The Dalvik virtual machine Architecture by David Ehringer.
An Efficient Stack Machine Martin Schöberl. JOP Stack Architecture2 Overview JVM stack machine Parameter passing Stack access patterns Common stack caches.
A Java Compiler Overview. October 21, 2003Shane A. Brewer2 Who Am I? Shane A. Brewer Masters Graduate.
Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CSCI Processing CSCI Introduction to Algorithm Design An Introduction.
Programming Languages
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
Programming Languages and Paradigms Activation Records in Java.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 18: Code Safety and Virtual Machines
CS 153: Concepts of Compiler Design November 4 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Getting Started With Java September 22, Java Bytecode  Bytecode : is a highly optimized set of instructions designed to be executed by the Java.
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
CSCI 212 Object-Oriented Programming in Java. Prerequisite: CSCI 111 variable assignment statement while loop for loop post-increment (i++) strong typing.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1.
Review on Program Challenge CSc3210 Yuan Long.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
RealTimeSystems Lab Jong-Koo, Lim
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
CS 432: Compiler Construction Lecture 15
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
CS 153: Concepts of Compiler Design November 2 Class Meeting
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
CSc 453 Interpreters & Interpretation
Introduction to Algorithm Design
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Course Overview PART I: overview material PART II: inside a compiler
CS 153: Concepts of Compiler Design November 6 Class Meeting
CMPE 152: Compiler Design April 11 Class Meeting
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
CMPE 152: Compiler Design April 16 Class Meeting
CSc 453 Interpreters & Interpretation
Presentation transcript:

Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007

What is in a.class file? Create source file Compile source file into.class file Run the.class file So, what is in the.class file?

The way other languages work (C, ADA, etc…) Source code is written Source code is ported to every different platform machine code (or binaries) Source code is compiled into platform specific machine code (or binaries) Binaries execute on a single platform

Source Code Linux portWindows portMac port Linux Compile Windows Compile Mac Compile Linux binaryWin binaryMac binary Linux Machine Windows Machine Mac Machine

Java: Write once, compile, run anywhere! Java is compiled into machine independent bytecode class files Bytecode is interpreted by the Java Virtual Machine (JVM) JVM executes the step-by-step instructions given to it from the bytecode JVM is specific to each computer architecture (Linux JVM, Windows JVM, Mac JVM).

Source Code Java Compile Linux JVMWin JVMMac JVM Linux Machine Windows Machine Mac Machine How Java Does it

Advantages of Bytecode Bytecode is architecture independent (and writing a VM is easier than rewriting a compiler for every architecture) VMs can enforce different levels of security automatically (applets versus applications) Just In-Time (JIT) compiling helps achieve same or better speed than traditional compiled code

Other languages that have an intermediate representation C# Perl PHP Python Forth Tcl

Java Bytecode 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 = 0xCAFEBABE

Referencing Memory iload Pushes (loads) the int in local variable (1 bytes) on the stack istore Pops the int on the top of the stack and stores it in local variable iload, fload, dload, aload (reference) istore, fstore, dstore, astore

Javap examples public class Test1 { public int add(int a, int b) { int c= a+b; return c; } … public int add(int, int); Code: 0: iload_1 1: iload_2 2: iadd 3: istore_3 4: iload_3 5: ireturn javap -c Test1javac -g Test1.java // add var 1 and 2 // store as local var 3 // store onto stack // return int // push onto stack Javap included with Java Development Kit (JDK)

JAD example public class Test1 { public int add(int a, int b) { int c= a+b; return c; } … public int add(int a, int b) { int c = a + b; // 0 0:iload_1 // 1 1:iload_2 // 2 2:iadd // 3 3:istore_3 return c; // 4 4:iload_3 // 5 5:ireturn } jad -a Test1javac -g Test1.java JAD is free, but not included with Java Development Kit (JDK)

Java Bytecode Explanation OpcodeMnemonicDescription 0nopDoes nothing 1aconst_null Push null on the stack 3iconst_0Push int 0 on the stack 4iconst_1Push int 1 on the stack …

Java Bytecode Explanation OpcodeMnemonicDescription 18ldc 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!

Java Bytecode Arithmetic OpcodeMnemonicDescription 96iaddPops two integers from the stack and pushes their sum iconst_2 iconst_3 iadd

Java Bytecode Arithmetic OpcodeMnemonicDescription 96iadd Pops two integers from the stack and pushes their sum 97ladd Pops two long integers from the stack and pushes their sum … 106fmul Pops two floats from the stack and pushes their product … 119dneg Pops a double from the stack, and pushes its negation

Other types of Instructions 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)

Method Calls invokevirtual Invokes the 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

Control Flow ifeq Pop an int off the stack. If it is zero, jump to the label. Otherwise, continue normally. if_icmple Pop two ints off the stack. If the second one is <= the first one, jump to the label. Otherwise, continue normally.

Static Method Calls invokestatic Invokes a static (class) 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.

JAD example 2 public void count() { for (int i=0; i<10; i++) { System.out.println( "i is "+i); } javac -g Test1.java VM Spec:

JAD example public void count() { for(int i = 0; i < 10; i++) //* 0 0:iconst_0 //* 1 1:istore_1 //* 2 2:iload_1 //* 3 3:bipush 10 //* 4 5:icmpge 39 System.out.println((new StringBuilder()).append("i is ").append(i).toString()); // 5 8:getstatic #2 // 6 11:new #3 // 7 14:dup // 8 15:invokespecial #4 // 9 18:ldc1 #5 // 10 20:invokevirtual #6 // 11 23:iload_1 // 12 24:invokevirtual #7 // 13 27:invokevirtual #8 // 14 30:invokevirtual #9 // 15 33:iinc 1 1 //* 16 36:goto 2 // 17 39:return }

JAD example 3: Test2.java // 0 0:iconst_5 // 1 1:istore_1 // 2 2:bipush 10 // 3 4:istore_2 // 4 5:iload_2 // 5 6:iload_1 // 6 7:isub // 7 8:istore_3 // 8 9:iload_3 // 9 10:ireturn What does this method do?

JAD example 3: Test2.java // 0 0:iconst_5 // 1 1:istore_1 // 2 2:bipush 10 // 3 4:istore_2 // 4 5:iload_2 // 5 6:iload_1 // 6 7:isub // 7 8:istore_3 // 8 9:iload_3 // 9 10:ireturn What does this method do? Push 5 onto stack Store into local var 1 Load var 2 onto stack Load var 3 onto stack Load var 1 onto stack Subtract stack vars Return (top val from stack) Push 10 onto stack

JAD example 3: Test2.java public int subtract() { int a = 5; // 0 0:iconst_5 // 1 1:istore_1 int b = 10; // 2 2:bipush 10 // 3 4:istore_2 int c = b - a; // 4 5:iload_2 // 5 6:iload_1 // 6 7:isub // 7 8:istore_3 return c; // 8 9:iload_3 // 9 10:ireturn }

References Summer-2005/3AFB411F-B5C BF733EEB3199/0/bytecode.pdf Summer-2005/3AFB411F-B5C BF733EEB3199/0/bytecode.pdf Bytecode, Lecture 18