54
301AA - Advanced Programming Lecturer: Andrea Corradini [email protected] h;p://pages.di.unipi.it/corradini/ AP-04: Run&me Systems and intro to JVM

301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

301AA-AdvancedProgramming

Lecturer:AndreaCorradini [email protected]

h;p://pages.di.unipi.it/corradini/

AP-04: Run&meSystemsandintrotoJVM

Page 2: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Overview•  RunAmeSystems•  TheJavaRunAmeEnvironment•  TheJVMasanabstractmachine•  JVMDataTypes•  JVMRunAmeDataAreas•  MulAthreading•  Per-threadDataAreas•  DynamicLinking•  JITcompilaAon•  MethodArea

2

Page 3: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

RunAmesystem

•  Everyprogramminglanguagedefinesanexecu.onmodel

•  Arun.mesystemimplements(partof)suchexecuAonmodel,providingsupportduringtheexecuAonofcorrespondingprograms

•  Run.mesupportisneededbothbyinterpretedandbycompiledprograms,eveniftypicallylessbythela;er

3

Page 4: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

RunAmesystem(2)

•  TherunAmesystemcanbemadeof– CodeintheexecuAngprogramgeneratedbythecompiler

– Coderunninginotherthreads/processesduringprogramexecuAon

– Languagelibraries– OperaAngsystemsfuncAonaliAes– Theinterpreter/virtualmachineitself

4

Page 5: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

RunAmeSupportneededfor…

•  Memorymanagement–  Stackmanagement:Push/popofacAvaAonrecords– Heapmanagement:allocaAon,garbagecollecAonèChapter7of"DragonBook"

•  Input/Output–  Interfacetofilesystem/networksockets/I/Odevices

•  InteracAonwiththerun.meenvironment,–  statevaluesaccessibleduringexecuAon(eg.environmentvariables)

–  acAveenAAeslikediskdriversandpeopleviakeyboards.

5

Page 6: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

RunAmeSupportneededfor…(2)

•  ParallelexecuAonviathreads/tasks/processes•  Dynamictypecheckinganddynamicbinding•  Dynamicloadingandlinkingofmodules•  Debugging•  CodegeneraAon(forJITcompilaAon)andOpAmizaAon

•  VerificaAonandmonitoring

6

Page 7: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

JavaRunAmeEnviroment-JRE

•  IncludesallwhatisneededtoruncompiledJavaprograms–  JVM–JavaVirtualMachine–  JCL–JavaClassLibrary(JavaAPI)

•  WeshallfocusontheJVMasarealrunAmesystemcoveringmostofthefuncAonaliAesjustlisted

•  ReferencedocumentaAon:–  TheJavaTMVirtualMachineSpecificaAon,JavaSE8EdiAon

https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf!

–  TheJavaLanguageSpecificaAon,JavaSE8EdiAonhttps://docs.oracle.com/javase/specs/jls/se8/jls8.pdf

7

Page 8: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

New"short-term"releasesofJavaè Java9,releasedinSeptember2017

–  Addedmodulesystem–  h;ps://www.oracle.com/corporate/features/

understanding-java-9-modules.htmlè Java10,releasedinMarch2018

–  Typeinferenceoflocalvariables(usingvar)–  h;ps://developer.oracle.com/java/jdk-10-local-

variable-type-inference–  h;ps://medium.com/the-java-report/java-10-sneak-

peek-local-variable-type-inference-var-3022016e1a2b

8

Page 9: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

New"short-term"releasesofJava(2)

è Java11(LTS),September2018–  Localparametertypeinferenceforlambdas,using

var–  ZGC:AScalableLow-LatencyGarbageCollector

(Experimental)–  h;ps://openjdk.java.net/projects/jdk/11/

è Java12,March2019–  switchexpressions,preview–  h;ps://openjdk.java.net/projects/jdk/12/

è Java13,September2019–  h;ps://openjdk.java.net/projects/jdk/13/

9

Page 10: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

WhatistheJVM?•  TheJVMisanabstractmachineinthetruesenseoftheword.•  TheJVMspecificaAondoesnotgiveimplementaAondetails

likememorylayoutofrun-Amedataarea,garbage-collecAonalgorithm,internalopAmizaAon(canbedependentontargetOS/plaform,performancerequirements,etc.)

•  TheJVMspecificaAondefinesamachineindependent“classfileformat”thatallJVMimplementaAonsmustsupport

•  TheJVMimposesstrongsyntac.candstructuralconstraintsonthecodeinaclassfile.AnylanguagewithfuncAonalitythatcanbeexpressedintermsofavalidclassfilecanbehostedbytheJVM

10

Page 11: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

ExecuAonmodel•  JVMisamul)-threadedstackbasedmachine•  JVMinstrucAons

•  implicitlytakeargumentsfromthetopoftheoperandstackofthecurrentframe

•  puttheirresultonthetopoftheoperandstack•  Theoperandstackisusedto

•  passargumentstomethods•  returnaresultfromamethod•  storeintermediateresultswhileevaluaAngexpressions

•  storelocalvariables

11

Page 12: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

JavaAbstactMachineHierarchy

12

Page 13: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

ClassFilesandClassFileFormat

.class files

JVMload

Externalrepresenta.on(plaformindependent)

Internalrepresenta.on(implementaAondependent)

objects

classes

methods

arraysstrings

primiAvetypes

13

Page 14: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

JVMDataTypesPrimi.vetypes:•  numericintegral:byte,short,int,long,char!•  numericfloaAngpoint:float,double!•  boolean:boolean (supportonlyforarrays)•  internal,forexcepAonhandling:returnAddress!

Referencetypes:•  classtypes•  arraytypes•  interfacetypes

Note:•  NotypeinformaAononlocalvariablesatrunAme•  Typesofoperandsspecifiedbyopcodes(eg:iadd,fadd,….)

14

Page 15: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

ObjectRepresentaAon

•  LemtotheimplementaAon–  Includingconcretevalueofnull

•  ThisaddsextralevelofindirecAon– needpointerstoinstancedataandclassdata– makegarbagecollecAoneasier

•  ObjectrepresentaAonmustinclude– mutexlock– GCstate(flags)

15

Page 16: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

JVMRunAmeDataAreas

16

PerThreadArea

SharedamongThreads

16

Page 17: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Threads•  JVMallowsmulAplethreadsperapplicaAon,starAngwithmain

•  CreatedasinstancesofThreadinvokingstart() (whichinvokesrun())!

•  Severalbackground(daemon)systemthreadsfor– GarbagecollecAon,finalizaAon–  Signaldispatching–  CompilaAon,etc.

•  ThreadscanbesupportedbyAme-slicingand/ormulApleprocessors

17

Page 18: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Threads(2)

•  Threadshavesharedaccesstoheapandpersistentmemory

•  ComplexspecificaAonofconsistencymodel– volaAles– workingmemoryvs.generalstore– non-atomiclongsanddoubles

•  TheJavaprogramminglanguagememorymodelprescribesthebehaviourofmulAthreadedprograms(JLSCh.17)

18

Page 19: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

JavaThreadLifeCycle

19

ii

ii

ii

ii

1.1 Il ciclo di vita di un thread 3

waitingObject.notify()

Thread.interrupt()timeout

��

suspendedsleep finished

Thread.interrupt()

{{•

Thread.start() // readyselected

// in execution

I/O or monitorrequest

✏✏

Thread.sleep()

OO

expired time slotor Thread.yield()

oo

return from run()o Thread.stop()yy

Object.wait()

[[

terminated

✏✏

blocked

I/O data ready ormonitor free

ff

Figura 1.2: State Diagram of a thread

Page 20: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

PerThreadDataAreas•  pc:pointertonextinstrucAoninmethodarea

–  undefinedifcurrentmethodisna&ve•  Thejavastack:astackofframes(orac&va&onrecords).

–  AnewframeiscreatedeachAmeamethodisinvokedanditisdestroyedwhenthemethodcompletes.

–  TheJVMSdoesnotrequirethatframesareallocatedconAguously

•  Thena.vestack:isusedforinvocaAonofnaAvefuncAons,throughtheJNI(JavaNaAveInterface)–  WhenanaAvefuncAonisinvoked,eg.aCfuncAon,execuAon

conAnuesusingthenaAvestack–  NaAvefuncAonscancallbackJavamethods,whichusetheJavastack

20

Page 21: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Structureofframes

•  LocalVariableArray(32bits)containing– Referencetothis(ifinstancemethod)– Methodparameters– Localvariables

•  OperandStacktosupportevaluaAonofexpressionsandevalutaAonofthemethod– MostJVMbytecodesmanipulatethestack

•  ReferencetoConstantPoolofcurrentclass

21

Page 22: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

DynamicLinking(1)•  Thereferencetotheconstantpoolforthecurrentclass

helpstosupportdynamiclinking.•  InC/C++typicallymulApleobjectfilesarelinkedtogether

toproduceanexecutableordll.–  DuringthelinkingphasesymbolicreferencesarereplacedwithanactualmemoryaddressrelaAvetothefinalexecutable.

•  InJavathislinkingphaseisdonedynamicallyatrunAme.•  WhenaJavaclassiscompiled,allreferencestovariables

andmethodsarestoredintheclass'sconstantpoolassymbolicreferences.

22

Page 23: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

DynamicLinking(2)•  TheJVMimplementaAoncanchoosewhentoresolvesymbolic

references.–  Eagerorsta.cresolu.on:whentheclassfileisverifiedamerbeing

loaded–  Lazyorlateresolu.on:whenthesymbolicreferenceisusedforthe

firstAme•  TheJVMhastobehaveasiftheresoluAonoccurredwheneach

referenceisfirstusedandthrowanyresoluAonerrorsatthispoint.•  BindingistheprocessoftheenAty(field,methodorclass)

idenAfiedbythesymbolicreferencebeingreplacedbyadirectreference

•  Thisonlyhappensoncebecausethesymbolicreferenceiscompletelyreplacedintheconstantpool

•  Ifthesymbolicreferencereferstoaclassthathasnotyetbeenresolvedthenthisclasswillbeloaded.

23

Page 24: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

•  Memoryforobjectsandarrays;unlikeC/C++theyareneverallocatedtostack

•  ExplicitdeallocaAonnotsupported.OnlybygarbagecollecAon.

•  TheHotSpotJVMincludesfourGenera.onalGarbageCollec.onAlgorithms

•  SinceOracleJDK11:ZGarbageCollector

24

DataAreasSharedbyThreads:Heap

Page 25: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

•  Memoryforobjectswhichareneverdeallocated,neededfortheJVMexecuAon– Methodarea–  Internedstrings– CodecacheforJIT

25

DataAreasSharedbyThreads:Non-Heap

Page 26: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

JITcompilaAon

•  TheHotspotJVM(andotherJVMs)profilesthecodeduringinterpretaAon,lookingfor“hot”areasofbytecodethatareexecutedregularly

•  ThesepartsarecompiledtonaAvecode.•  Suchcodeisthenstoredinthecodecacheinnon-heapmemory.

26

Page 27: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

MethodareaThememorywhereclassfilesareloaded.Foreachclass:•  ClassloaderReference•  Fromtheclassfile:

–  RunTimeConstantPool–  Fielddata–  Methoddata–  Methodcode

Note:Methodareaissharedamongthread.Accesstoithastobethreadsafe.Changesofmethodareawhen:•  Anewclassisloaded•  Asymboliclinkisresolvedbydynamiclinking

27

Page 28: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

ClassfilestructureClassFile{

u4 magic; !u2 minor_version; u2 major_version;u2 constant_pool_count;cp_info contant_pool[constant_pool_count–1];u2 access_flags;u2 this_class;u2 super_class;u2 interfaces_count;u2 interfaces[interfaces_count];u2 fields_count;field_info fields[fields_count];u2 methods_count;method_info methods[methods_count];u2 a;ributes_count;a;ribute_info a;ributes[a;ributes_count];

}

28

ConstantPool

0xCAFEBABE !JavaLanguageVersion

Sta.candInstanceVariables

Methods

OtherInfoontheClass

ReferencestoClassandSuperclass

ReferencestoDirectInterfaces

accessmodifiersandotherinfo

Page 29: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

FielddataintheMethodArea

Perfield:•  Name•  Type•  Modifiers•  A;ributes

29

Page 30: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

FieldTypedescriptors

30

THE CLASS FILE FORMAT Descriptors 4.3

77

Table 4.3-A. Interpretation of field descriptors

FieldType term Type Interpretation

B byte signed byte

C char Unicode character code point in the BasicMultilingual Plane, encoded with UTF-16

D double double-precision floating-point value

F float single-precision floating-point value

I int integer

J long long integer

L ClassName ; reference an instance of class ClassName

S short signed short

Z boolean true or false

[ reference one array dimension

The field descriptor of an instance variable of type int is simply I.

The field descriptor of an instance variable of type Object is Ljava/lang/Object;. Notethat the internal form of the binary name for class Object is used.

The field descriptor of an instance variable of the multidimensional array type double[][][] is [[[D.

4.3.3 Method Descriptors

A method descriptor contains zero or more parameter descriptors, representing thetypes of parameters that the method takes, and a return descriptor, representing thetype of the value (if any) that the method returns.

MethodDescriptor:( {ParameterDescriptor} ) ReturnDescriptor

ParameterDescriptor:FieldType

ReturnDescriptor:FieldTypeVoidDescriptor

Page 31: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

MethoddataPermethod:•  Name•  ReturnType•  ParameterTypes(inorder)•  Modifiers•  A;ributes•  Methodcode…Amethoddescriptorcontains•  asequenceofzeroormoreparameterdescriptorsinbrackets•  areturndescriptororVforvoiddescriptorExample:Thedescriptorof

Object m(int i, double d, Thread t) {...}is:

(IDLjava/lang/Thread;)Ljava/lang/Object;

31

Page 32: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

MethodcodePermethod:•  Bytecodes•  Operandstacksize•  Localvariablesize•  Localvariabletable•  ExcepAontable•  LineNumberTable–whichlineofsourcecodecorrespondsto

whichbytecodeinstrucAon(fordebugger)PerexcepAonhandler(oneforeachtry/catch/finallyclause)•  Startpoint•  Endpoint•  PCoffsetforhandlercode•  ConstantpoolindexforexcepAonclassbeingcaught

32

Page 33: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

DisassemblingJavafiles:javac,javap,java

package org.jvminternals; public class SimpleClass { public void sayHello() { System.out.println("Hello"); } }

Compilerjavac SimpleClass.java

SimpleClass.java

Disassemblerjavap –c -v SimpleClass.class

JVMjava SimpleClass

SimpleClass.class

33

Page 34: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

34

{ public org.jvminternals.SimpleClass(); descriptor: ()V flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 2: 0 public void sayHello(); descriptor: ()V flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3 // String Hello 5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return LineNumberTable: line 4: 0 line 5: 8 } SourceFile: "SimpleClass.java”

SimpleClass.class:constructorandmethod

package org.jvminternals; public class SimpleClass { public void sayHello() { System.out.println("Hello"); }}

Methoddescriptors

FielddescriptorStringliteral

Indexintoconstantpool

Localvariable0=“this”

Page 35: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Theconstantpool•  Similartosymboltable,butwithmoreinfo•  Containsconstantsandsymbolicreferencesusedfordynamicbinding,suitablytagged–  numericliterals(Integer,Float,Long,Double)–  stringliterals(Uf8)–  classreferences(Class)–  fieldreferences(Fieldref)–  methodreferences(Mehodref,InterfaceMethodref,MethodHandle)

–  signatures(NameAndType)•  Operandsinbytecodesomenareindexesintheconstantpool

35

Page 36: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

36

Compiled from "SimpleClass.java" public class SimpleClass minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #6.#14 // java/lang/Object."<init>":()V #2 = Fieldref #15.#16 // java/lang/System.out:Ljava/io/PrintStream; #3 = String #17 // Hello #4 = Methodref #18.#19 // java/io/PrintStream.println:(Ljava/lang/String;)V #5 = Class #20 // SimpleClass #6 = Class #21 // java/lang/Object #7 = Utf8 <init> #8 = Utf8 ()V #9 = Utf8 Code #10 = Utf8 LineNumberTable #11 = Utf8 sayHello #12 = Utf8 SourceFile #13 = Utf8 SimpleClass.java #14 = NameAndType #7:#8 // "<init>":()V #15 = Class #22 // java/lang/System #16 = NameAndType #23:#24 // out:Ljava/io/PrintStream; #17 = Utf8 Hello #18 = Class #25 // java/io/PrintStream #19 = NameAndType #26:#27 // println:(Ljava/lang/String;)V #20 = Utf8 SimpleClass #21 = Utf8 java/lang/Object #22 = Utf8 java/lang/System #23 = Utf8 out #24 = Utf8 Ljava/io/PrintStream; #25 = Utf8 java/io/PrintStream #26 = Utf8 println #27 = Utf8 (Ljava/lang/String;)V

SimpleClass.class:theConstantpoolpublic class SimpleClass { public void sayHello() { System.out.println("Hello"); }}

public void sayHello(); descriptor: ()V Code: stack=2, locals=1, args_size=1 0: getstatic #2 3: ldc #3 5: invokevirtual #4 8: return

Page 37: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

37

public void sayHello(); descriptor: ()V Code: stack=2, locals=1, args_size=1 0: getstatic #2 3: ldc #3 5: invokevirtual #4 8: return

Page 38: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Loading,Linking,andIniAalizing

•  Loading:findingthebinaryrepresentaAonofaclassorinterfacetypewithagivennameandcreaAngaclassorinterfacefromit

•  Linking:takingaclassorinterfaceandcombiningitintotherun-AmestateoftheJavaVirtualMachinesothatitcanbeexecuted

•  Ini.aliza.on:execuAngtheclassorinterfaceiniAalizaAonmethod <clinit>!

38

Page 39: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

JVMStartup

•  TheJVMstartsupbyloadinganiniAalclassusingthebootstrapclassloader

•  TheclassislinkedandiniAalized•  public static void main(String[]) isinvoked.

•  Thiswilltriggerloading,linkingandiniAalizaAonofaddiAonalclassesandinterfaces…

39

Page 40: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Loading•  ClassorInterfaceCcreaAonistriggered

–  byotherclassorinterfacereferencingC–  bycertainmethods(eg.reflecAon)

•  ArrayclassesaregeneratedbytheJVM•  Checkwhetheralreadyloaded•  Ifnot,invoketheappropriateloader.loadClass•  Eachclassistaggedwiththeini&a&ngloader•  Loadingconstraintsarecheckedduringloading

–  toensurethatthesamenamedenotesthesametypeindifferentloaders

40

Page 41: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

ClassLoaderHierarchy•  BootstrapClassloaderloadsbasicJavaAPIs,includingforexample

rt.jar.ItmayskipmuchofthevalidaAonthatgetsdonefornormalclasses.

•  ExtensionClassloaderloadsclassesfromstandardJavaextensionAPIssuchassecurityextensionfuncAons.

•  SystemClassloaderisthedefaultapplicaAonclassloader,whichloadsapplicaAonclassesfromtheclasspath

•  UserDefinedClassloaderscanbeusedtoloadapplicaAonclasses:–  forrunAmereloadingofclasses–  forloadingfromdifferentsources,eg.fromnetwork,fromanencryptedfile,

oralsogeneratedonthefly–  forsupporAngseparaAonbetweendifferentgroupsofloadedclassesas

requiredbywebservers•  Classloaderhooks:findClass(buildsabytearray),defineClass

(turnsanarrayofbytesintoaclassobject),resolveClass (linksaclass)

41

Page 42: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

RunAmeConstantPool

•  Theconstant_pooltableinthe.classfileisusedtoconstructtherun-&meconstantpooluponclassorinterfacecreaAon.

•  Allreferencesintherun-AmeconstantpoolareiniAallysymbolic.

•  Symbolicreferencesarederivedfromthe.class fileintheexpectedway

•  ClassnamesarethosereturnedbyClass.getName()

•  Fieldandmethodreferencesaremadeofname,descriptorandclassname

42

Page 43: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Linking

•  Link=verificaAon,preparaAon,resoluAon•  Verifica.on:seebelow•  Prepara.on:allocaAonofstorage(methodtables)

•  Resolu.on(opAonal):resolvesymbolreferencesbyloadingreferredclasses/interfaces– OtherwisepostponedAllfirstusebyaninstrucAon

43

Page 44: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

VerificaAon•  When?

–  Mainlyduringtheloadandlinkprocess•  Why?

–  NoguaranteethattheclassfilewasgeneratedbyaJavacompiler–  EnhancerunAmeperformance

•  Examples–  Therearenooperandstackoverflowsorunderflows.–  Alllocalvariableusesandstoresarevalid.–  TheargumentstoalltheJVMinstrucAonsareofvalidtypes.

•  RelevantpartoftheJVMspecificaAon:describedin~170pagesoftheJVMS(total:~600pages)

44

Page 45: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

VerificaAonProcess•  Pass1–whentheclassfileisloaded

– Thefileisproperlyforma;ed,andallitsdataisrecognizedbytheJVM

•  Pass2–whentheclassfileislinked– AllchecksthatdonotinvolveinstrucAons

• finalclassesarenotsubclassed,finalmethodsarenotoverridden.

•  Everyclass(exceptObject)hasasuperclass.• Allfieldreferencesandmethodreferencesintheconstantpoolhavevalidnames,validclasses,andavalidtypedescriptor. 45

Page 46: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

VerificaAonProcess–cont.•  Pass3–sAllduringlinking

– Data-flowanalysisoneachmethod.– Ensurethatatanygivenpointintheprogram,noma;erwhatcodepathistakentoreachthatpoint:

•  Theoperandstackisalwaysthesamesizeandcontainsthesametypesofobjects.

•  Nolocalvariableisaccessedunlessitisknowntocontainavalueofanappropriatetype.

•  Methodsareinvokedwiththeappropriatearguments.•  Fieldsareassignedonlyusingvaluesofappropriatetypes.•  Allopcodeshaveappropriatetypeargumentsontheoperandstackandinthelocalvariables

•  AmethodmustnotthrowmoreexcepAonsthanitadmits•  AmethodmustendwithareturnvalueorthrowinstrucAon•  Methodmustnotuseonehalfofatwowordvalue

46

Page 47: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

VerificaAonProcess–cont.

•  Pass4-thefirstAmeamethodisactuallyinvoked– avirtualpasswhosecheckingisdonebyJVMinstrucAons

•  Thereferencedmethodorfieldexistsinthegivenclass.•  ThecurrentlyexecuAngmethodhasaccesstothereferencedmethodorfield.

47

Page 48: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

IniAalizaAon•  <clinit>iniAalizaAonmethodisinvokedonclassesandinterfacestoiniAalizeclassvariables

•  staAciniAalizersareexecuted•  directsuperclassneedtobeiniAalizedprior•  happensondirectuse:methodinvocaAon,construcAon,fieldaccess

•  synchronizediniAalizaAons:stateinClassobject•  <init>:iniAalizaAonmethodforinstances

–  invokespecialinstrucAon–  canbeinvokedonlyonuniniAalizedinstances

48

Page 49: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

IniAalizaAonexample(1)class Super {! static { System.out.print("Super ");}!}!class One {! static { System.out.print("One ");}!}!class Two extends Super {! static { System.out.print("Two ");}!}!class Test {! public static void main(String[] args) {!

!One o = null;!!Two t = new Two();!!System.out.println((Object)o == (Object)t);!

}!}!

Whatdoersjava Testprint?49SuperTwoFalse

Page 50: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

IniAalizaAonexample(2)class Super { static int taxi = 1729;}!}!class Sub extends Super {! static { System.out.print("Sub ");}!}!class Test {! public static void main(String[] args) {!

!System.out.println(Sub.taxi);!}}!

Whatdoesjava Testprint?

AreferencetoastaAcfield(§8.3.1.1)causesiniAalizaAonofonlytheclassorinterfacethatactuallydeclaresit,eventhoughitmightbereferredtothroughthenameofasubclass,asubinterface,oraclassthatimplementsaninterface.(page385of[JLS-8])

50

Onlyprints"1729"

Page 51: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

FinalizaAon•  InvokedjustbeforegarbagecollecAon•  JLSdoesnotspecifywhenitisinvoked•  Alsodoesnotspecifywhichthread•  NoautomaAcinvocaAonofsuper’sfinalizers•  Verytricky!

•  Eachobjectcanbe–  Reachable,finalizer-reachable,unreachable– Unfinalized,finalizable,finalized

void finalize() {! classVariable = this; // the object is reachable again!}!

51

Page 52: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

FinalizaAonStateDiagramhttps://notendur.hi.is/snorri/SDK-docs/lang/lang083.htm!

finalize()isnevercalledasecondAmeonthesameobject,butitcanbeinvokedasanyothermethod!

52

Page 53: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

JVMExit

•  classFinalizesimilartoobjectfinalizaAon•  Aclasscanbeunloadedwhen

–  noinstancesexist–  classobjectisunreachable

•  JVMexitswhen:–  allitsnon-daemonthreadsterminate– Runtime.exitorSystem.exitassumingitissecure

•  finalizerscanbeopAonallyinvokedonallobjectsjustbeforeexit

53

Page 54: 301AA - Advanced Programmingpages.di.unipi.it/corradini/Didattica/AP-19/SLIDES/AP-2019-04.pdf · • Includes all what is needed to run compiled Java programs – JVM – Java Virtual

Resources

•  JVMSChapter2-TheStructureoftheJavaVirtualMachine

•  JVMInternals,byJamesD.Bloomh;p://blog.jamesdbloom.com/JVMInternals.html

•  JLSChapter17–Memorymodel

54