Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created October 22, 2016 09:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rednaxelafx/c474cadaa9057f909d48e7593b9e1483 to your computer and use it in GitHub Desktop.
Save rednaxelafx/c474cadaa9057f909d48e7593b9e1483 to your computer and use it in GitHub Desktop.
Java implicit null check example. Run on Oracle JDK8u101
$ ~/sdk/jdk1.8.0_101/Contents/Home/bin/java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=compileonly,TrapDemo,demo -XX:TieredStopAtLevel=1 -Xcomp TrapDemo
Java HotSpot(TM) 64-Bit Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output
CompilerOracle: compileonly TrapDemo.demo
Loaded disassembler from /Users/krismo/sdk/jdk1.8.0_101/Contents/Home/jre/lib/server/hsdis-amd64.dylib
Decoding compiled method 0x00000001024a5990:
Code:
[Disassembling for mach='i386:x86-64']
[Entry Point]
[Verified Entry Point]
[Constants]
# {method} {0x000000011b510308} 'demo' '(LIntBox;)V' in 'TrapDemo'
# parm0: rsi:rsi = 'IntBox'
# [sp+0x40] (sp of caller)
0x00000001024a5ae0: mov %eax,-0x14000(%rsp)
0x00000001024a5ae7: push %rbp
0x00000001024a5ae8: sub $0x30,%rsp ;*aload_0
; - TrapDemo::demo@0 (line 3)
0x00000001024a5aec: mov 0xc(%rsi),%edi ;*getfield value
; - TrapDemo::demo@1 (line 3)
; implicit exception: dispatches to 0x00000001024a5b18
0x00000001024a5aef: cmp $0x0,%rsi
0x00000001024a5af3: jne 0x00000001024a5b05 ;*ifnonnull
; - TrapDemo::demo@6 (line 4)
0x00000001024a5af9: add $0x30,%rsp
0x00000001024a5afd: pop %rbp
0x00000001024a5afe: test %eax,-0x18dca04(%rip) # 0x0000000100bc9100
; {poll_return}
0x00000001024a5b04: retq ;*return
; - TrapDemo::demo@9 (line 4)
0x00000001024a5b05: movl $0x2a,0xc(%rsi) ;*putfield value
; - TrapDemo::demo@13 (line 5)
0x00000001024a5b0c: add $0x30,%rsp
0x00000001024a5b10: pop %rbp
0x00000001024a5b11: test %eax,-0x18dca17(%rip) # 0x0000000100bc9100
; {poll_return}
0x00000001024a5b17: retq
0x00000001024a5b18: callq 0x0000000102496a40 ; OopMap{rsi=Oop off=61}
;*getfield value
; - TrapDemo::demo@1 (line 3)
; {runtime_call}
0x00000001024a5b1d: nop
0x00000001024a5b1e: nop
0x00000001024a5b1f: mov 0x2a8(%r15),%rax
0x00000001024a5b26: movabs $0x0,%r10
0x00000001024a5b30: mov %r10,0x2a8(%r15)
0x00000001024a5b37: movabs $0x0,%r10
0x00000001024a5b41: mov %r10,0x2b0(%r15)
0x00000001024a5b48: add $0x30,%rsp
0x00000001024a5b4c: pop %rbp
0x00000001024a5b4d: jmpq 0x00000001024094a0 ; {runtime_call}
0x00000001024a5b52: hlt
0x00000001024a5b53: hlt
0x00000001024a5b54: hlt
0x00000001024a5b55: hlt
0x00000001024a5b56: hlt
0x00000001024a5b57: hlt
0x00000001024a5b58: hlt
0x00000001024a5b59: hlt
0x00000001024a5b5a: hlt
0x00000001024a5b5b: hlt
0x00000001024a5b5c: hlt
0x00000001024a5b5d: hlt
0x00000001024a5b5e: hlt
0x00000001024a5b5f: hlt
[Exception Handler]
[Stub Code]
0x00000001024a5b60: callq 0x00000001024988e0 ; {no_reloc}
0x00000001024a5b65: mov %rsp,-0x28(%rsp)
0x00000001024a5b6a: sub $0x80,%rsp
0x00000001024a5b71: mov %rax,0x78(%rsp)
0x00000001024a5b76: mov %rcx,0x70(%rsp)
0x00000001024a5b7b: mov %rdx,0x68(%rsp)
0x00000001024a5b80: mov %rbx,0x60(%rsp)
0x00000001024a5b85: mov %rbp,0x50(%rsp)
0x00000001024a5b8a: mov %rsi,0x48(%rsp)
0x00000001024a5b8f: mov %rdi,0x40(%rsp)
0x00000001024a5b94: mov %r8,0x38(%rsp)
0x00000001024a5b99: mov %r9,0x30(%rsp)
0x00000001024a5b9e: mov %r10,0x28(%rsp)
0x00000001024a5ba3: mov %r11,0x20(%rsp)
0x00000001024a5ba8: mov %r12,0x18(%rsp)
0x00000001024a5bad: mov %r13,0x10(%rsp)
0x00000001024a5bb2: mov %r14,0x8(%rsp)
0x00000001024a5bb7: mov %r15,(%rsp)
0x00000001024a5bbb: movabs $0x1019d5e93,%rdi ; {external_word}
0x00000001024a5bc5: movabs $0x1024a5b65,%rsi ; {internal_word}
0x00000001024a5bcf: mov %rsp,%rdx
0x00000001024a5bd2: and $0xfffffffffffffff0,%rsp
0x00000001024a5bd6: callq 0x000000010180249a ; {runtime_call}
0x00000001024a5bdb: hlt
[Deopt Handler Code]
0x00000001024a5bdc: movabs $0x1024a5bdc,%r10 ; {section_word}
0x00000001024a5be6: push %r10
0x00000001024a5be8: jmpq 0x00000001023e43c0 ; {runtime_call}
0x00000001024a5bed: hlt
0x00000001024a5bee: hlt
0x00000001024a5bef: hlt
$ ~/sdk/jdk1.8.0_101/Contents/Home/bin/java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=compileonly,TrapDemo,demo -XX:-TieredCompilation -Xcomp TrapDemo
Java HotSpot(TM) 64-Bit Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output
CompilerOracle: compileonly TrapDemo.demo
Loaded disassembler from /Users/krismo/sdk/jdk1.8.0_101/Contents/Home/jre/lib/server/hsdis-amd64.dylib
Decoding compiled method 0x000000011030fa10:
Code:
[Disassembling for mach='i386:x86-64']
[Entry Point]
[Verified Entry Point]
[Constants]
# {method} {0x000000011d0c3308} 'demo' '(LIntBox;)V' in 'TrapDemo'
# parm0: rsi:rsi = 'IntBox'
# [sp+0x20] (sp of caller)
0x000000011030fb60: mov %eax,-0x14000(%rsp)
0x000000011030fb67: push %rbp
0x000000011030fb68: sub $0x10,%rsp
0x000000011030fb6c: movl $0x2a,0xc(%rsi) ;*synchronization entry
; - TrapDemo::demo@-1 (line 3)
; implicit exception: dispatches to 0x000000011030fb7f
0x000000011030fb73: add $0x10,%rsp
0x000000011030fb77: pop %rbp
0x000000011030fb78: test %eax,-0x19bfb7e(%rip) # 0x000000010e950000
; {poll_return}
0x000000011030fb7e: retq
0x000000011030fb7f: mov $0xfffffff6,%esi
0x000000011030fb84: data32 xchg %ax,%ax
0x000000011030fb87: callq 0x00000001102a51a0 ; OopMap{off=44}
;*getfield value
; - TrapDemo::demo@1 (line 3)
; {runtime_call}
0x000000011030fb8c: callq 0x000000010f6849a0 ;*getfield value
; - TrapDemo::demo@1 (line 3)
; {runtime_call}
0x000000011030fb91: hlt
0x000000011030fb92: hlt
0x000000011030fb93: hlt
0x000000011030fb94: hlt
0x000000011030fb95: hlt
0x000000011030fb96: hlt
0x000000011030fb97: hlt
0x000000011030fb98: hlt
0x000000011030fb99: hlt
0x000000011030fb9a: hlt
0x000000011030fb9b: hlt
0x000000011030fb9c: hlt
0x000000011030fb9d: hlt
0x000000011030fb9e: hlt
0x000000011030fb9f: hlt
[Exception Handler]
[Stub Code]
0x000000011030fba0: jmpq 0x000000011030c760 ; {no_reloc}
[Deopt Handler Code]
0x000000011030fba5: callq 0x000000011030fbaa
0x000000011030fbaa: subq $0x5,(%rsp)
0x000000011030fbaf: jmpq 0x00000001102e73c0 ; {runtime_call}
0x000000011030fbb4: hlt
0x000000011030fbb5: hlt
0x000000011030fbb6: hlt
0x000000011030fbb7: hlt
$ ~/sdk/hs_comp/Contents/Home/bin/java -XX:+PrintOptoAssembly -XX:CompileCommand=compileonly,TrapDemo,demo -XX:-TieredCompilation -Xcomp TrapDemo
CompilerOracle: compileonly TrapDemo.demo
{method}
- this oop: 0x0000000111d83320
- method holder: 'TrapDemo'
- constants: 0x0000000111d83060 constant pool [37] {0x0000000111d83060} for 'TrapDemo' cache=0x0000000111d83448
- access: 0x81000009 public static
- name: 'demo'
- signature: '(LIntBox;)V'
- max stack: 3
- max locals: 2
- size of params: 1
- method size: 12
- vtable index: -2
- i2i entry: 0x0000000104eee6e0
- adapters: AHE@0x00007fd19c04fc70: 0xb0000000 i2c: 0x0000000104fd6ae0 c2i: 0x0000000104fd6bf4 c2iUV: 0x0000000104fd6bc7
- compiled entry 0x0000000104fd6bf4
- code size: 17
- code start: 0x0000000111d832f0
- code end (excl): 0x0000000111d83301
- method data: 0x0000000111d836b8
- checked ex length: 0
- linenumber start: 0x0000000111d83301
- localvar length: 2
- localvar start: 0x0000000111d83306
#
# void ( IntBox * )
#
#r018 rsi:rsi : parm 0: IntBox *
# -- Old rsp -- Framesize: 32 --
#r191 rsp+28: in_preserve
#r190 rsp+24: return address
#r189 rsp+20: in_preserve
#r188 rsp+16: saved fp register
#r187 rsp+12: pad2, stack alignment
#r186 rsp+ 8: pad2, stack alignment
#r185 rsp+ 4: Fixed slot 1
#r184 rsp+ 0: Fixed slot 0
#
abababab N1: # B1 <- B3 B2 Freq: 1
abababab
000 B1: # B3 B2 <- BLOCK HEAD IS JUNK Freq: 1
000 # stack bang
pushq rbp # Save rbp
subq rsp, #16 # Create frame
00c movl [RSI + #12 (8-bit)], #42 # int ! Field: IntBox.value
013 NullCheck RSI
013
013 B2: # N1 <- B1 Freq: 0.999999
013 addq rsp, 16 # Destroy frame
popq rbp
testl rax, [rip + #offset_to_poll_page] # Safepoint: poll for GC
01e ret
01e
01f B3: # N1 <- B1 Freq: 1.01328e-06
01f movl RSI, #-10 # int
nop # 3 bytes pad for loops and calls
027 call,static wrapper for: uncommon_trap(reason='null_check' action='maybe_recompile')
# TrapDemo::demo @ bci:1 L[0]=_ L[1]=_ STK[0]=#NULL
# OopMap{off=44}
02c int3 # ShouldNotReachHere
02c
public class TrapDemo {
public static void demo(IntBox p) {
int dead = p.value;
if (p == null) return;
p.value = 42;
}
public static void main(String[] args) throws Exception {
demo(null);
}
}
class IntBox {
public int value;
}
// Oracle JDK8u101 HotSpot C1, and C2 on first compilation,
// will compile TrapDemo.demo(IntBox) to the following form:
// public static void demo(IntBox p) {
// p.value = 42; // implicit null check
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment