Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JVM Hacking

JVM Hacking

Hacking on the JVM is no black magic: it's just code.

Arnout Engelen

October 13, 2015
Tweet

More Decks by Arnout Engelen

Other Decks in Technology

Transcript

  1. SO THIS SUCKS $ j a v a B i

    n d F a i l u r e E x c e p t i o n i n t h r e a d " m a i n " j a v a . n e t . B i n d E x c e p t i o n : A d d r e s s a l r e a d y i n u s e a t s u n . n i o . c h . N e t . b i n d 0 ( N a t i v e M e t h o d ) a t s u n . n i o . c h . N e t . b i n d ( N e t . j a v a : 4 3 3 ) a t s u n . n i o . c h . N e t . b i n d ( N e t . j a v a : 4 2 5 ) a t s u n . n i o . c h . S e r v e r S o c k e t C h a n n e l I m p l . b i n d ( S e r v e r S o c k e t C h a n n e l I m p l . j a v a : a t s u n . n i o . c h . S e r v e r S o c k e t A d a p t o r . b i n d ( S e r v e r S o c k e t A d a p t o r . j a v a : 7 4 ) a t s u n . n i o . c h . S e r v e r S o c k e t A d a p t o r . b i n d ( S e r v e r S o c k e t A d a p t o r . j a v a : 6 7 ) a t B i n d F a i l u r e . m a i n ( B i n d F a i l u r e . j a v a : 1 1 ) $ WHAT address is already in use?
  2. GET & BUILD $ h g c l o n

    e h t t p : / / h g . o p e n j d k . j a v a . n e t / j d k 9 / j d k 9 j d k 9 $ c d j d k 9 j d k 9 $ . / c o n f i g u r e . . . j d k 9 $ m a k e a l l . . . j d k 9 $ $ . . / j d k 9 / b u i l d / l i n u x ­ x 8 6 _ 6 4 ­ n o r m a l ­ s e r v e r ­ r e l e a s e / j d k / b i n / j a v a B i n d F a i l u r e E x c e p t i o n i n t h r e a d " m a i n " j a v a . n e t . B i n d E x c e p t i o n : A d d r e s s a l r e a d y i n u s e a t s u n . n i o . c h . N e t . b i n d 0 ( N a t i v e M e t h o d ) a t s u n . n i o . c h . N e t . b i n d ( N e t . j a v a : 4 3 3 ) a t s u n . n i o . c h . N e t . b i n d ( N e t . j a v a : 4 2 5 ) a t s u n . n i o . c h . S e r v e r S o c k e t C h a n n e l I m p l . b i n d ( S e r v e r S o c k e t C h a n n e l I m p l . j a v a :
  3. FIND j d k 9 $ f i n d

    . ­ n a m e N e t . j a v a . / j d k / s r c / j a v a . b a s e / s h a r e / c l a s s e s / s u n / n i o / c h / N e t . j a v a j d k 9 $ g r e p b i n d 0 j d k / s r c / j a v a . b a s e / s h a r e / c l a s s e s / s u n / n i o / c h / N e t . j a v a b i n d 0 ( f d , p r e f e r I P v 6 , e x c l u s i v e B i n d , a d d r , p o r t ) ; p r i v a t e s t a t i c n a t i v e v o i d b i n d 0 ( F i l e D e s c r i p t o r f d , b o o l e a n p r e f e r I P v 6 , j d k 9 $ g r e p ­ r N e t _ b i n d 0 j d k / s r c | g r e p \ \ . c j d k / s r c / j a v a . b a s e / w i n d o w s / n a t i v e / l i b n i o / c h / N e t . c : J a v a _ s u n _ n i o _ c h _ N e t _ b i n d 0 ( J N I E n j d k / s r c / j a v a . b a s e / u n i x / n a t i v e / l i b n i o / c h / N e t . c : J a v a _ s u n _ n i o _ c h _ N e t _ b i n d 0 ( J N I E n v *
  4. READ J N I E X P O R T

    v o i d J N I C A L L J a v a _ s u n _ n i o _ c h _ N e t _ b i n d 0 ( J N I E n v * e n v , j c l a s s c l a z z , j o b j e c t f d o , j b o o l e a n p r e f e j b o o l e a n u s e E x c l B i n d , j o b j e c t i a o , i n t p o r t ) { S O C K A D D R s a ; i n t s a _ l e n = S O C K A D D R _ L E N ; i n t r v = 0 ; . . . r v = N E T _ B i n d ( f d v a l ( e n v , f d o ) , ( s t r u c t s o c k a d d r * ) & s a , s a _ l e n ) ; i f ( r v ! = 0 ) { h a n d l e S o c k e t E r r o r ( e n v , e r r n o ) ; } }
  5. READ h a n d l e S o c

    k e t E r r o r ( J N I E n v * e n v , j i n t e r r o r V a l u e ) { c h a r * x n ; s w i t c h ( e r r o r V a l u e ) { . . . c a s e E A D D R I N U S E : / * F a l l t h r o u g h * / c a s e E A D D R N O T A V A I L : x n = J N U _ J A V A N E T P K G " B i n d E x c e p t i o n " ; b r e a k ; . . . e r r n o = e r r o r V a l u e ; J N U _ T h r o w B y N a m e W i t h L a s t E r r o r ( e n v , x n , " N i o S o c k e t E r r o r " ) ; r e t u r n I O S _ T H R O W N ; }
  6. READ J N I E X P O R T

    v o i d J N I C A L L J N U _ T h r o w B y N a m e W i t h L a s t E r r o r ( J N I E n v * e n v , c o n s t c h a r * n a m e , c o n s t c h a r * d e f a u l t D e t a i l ) { c h a r b u f [ 2 5 6 ] ; s i z e _ t n = g e t L a s t E r r o r S t r i n g ( b u f , s i z e o f ( b u f ) ) ; i f ( n > 0 ) { j s t r i n g s = J N U _ N e w S t r i n g P l a t f o r m ( e n v , b u f ) ; i f ( s ! = N U L L ) { j o b j e c t x = J N U _ N e w O b j e c t B y N a m e ( e n v , n a m e , " ( L j a v a / l a n g / S t r i n g ; ) V " , s ) ; i f ( x ! = N U L L ) { ( * e n v ) ­ > T h r o w ( e n v , x ) ; } } } i f ( ! ( * e n v ) ­ > E x c e p t i o n O c c u r r e d ( e n v ) ) {
  7. HACK d i f f ­ r 8 7 0

    1 7 a 1 9 8 d 4 e s r c / j a v a . b a s e / s h a r e / n a t i v e / l i b j a v a / j n i _ u t i l . c ­ ­ ­ a / s r c / j a v a . b a s e / s h a r e / n a t i v e / l i b j a v a / j n i _ u t i l . c T h u O c t 0 8 2 2 : 3 0 : 4 2 2 0 1 5 + + + b / s r c / j a v a . b a s e / s h a r e / n a t i v e / l i b j a v a / j n i _ u t i l . c S u n O c t 1 1 1 7 : 1 3 : 1 5 2 0 1 5 @ @ ­ 1 7 4 , 6 + 1 7 4 , 5 2 @ @ } } + / * T h r o w a n e x c e p t i o n b y n a m e , u s i n g t h e s t r i n g r e t u r n e d b y + * J V M _ G e t L a s t E r r o r S t r i n g a l o n g w i t h a g i v e n m e s s a g e . I f t h e + * m s g i s N U L L , o n l y t h e l a s t e r r o r i s a d d e d . I f t h e l a s t ­ e r r o r + * s t r i n g i s N U L L , u s e t h e g i v e n d e f a u l t d e t a i l s t r i n g . + * / + J N I E X P O R T v o i d J N I C A L L + J N U _ T h r o w B y N a m e W i t h L a s t E r r o r A n d M s g ( J N I E n v * e n v , c o n s t c h a r * n a m e , + c o n s t c h a r * m s g , + c o n s t c h a r * d e f a u l t E r r o r ) + { + c h a r b u f [ 1 0 2 4 ] ;
  8. PROFIT! $ . . / j d k 9 /

    b u i l d / l i n u x ­ x 8 6 _ 6 4 ­ n o r m a l ­ s e r v e r ­ r e l e a s e / j d k / b i n / j a v a B i n d F a i l u r e E x c e p t i o n i n t h r e a d " m a i n " j a v a . n e t . B i n d E x c e p t i o n : : : f f f f : 1 7 2 . 1 6 . 3 . 8 3 : 9 9 9 9 : A d d r a t s u n . n i o . c h . N e t . b i n d 0 ( N a t i v e M e t h o d ) a t s u n . n i o . c h . N e t . b i n d ( N e t . j a v a : 4 3 2 ) a t s u n . n i o . c h . N e t . b i n d ( N e t . j a v a : 4 2 4 ) a t s u n . n i o . c h . S e r v e r S o c k e t C h a n n e l I m p l . b i n d ( S e r v e r S o c k e t C h a n n e l I m p l . j a v a : a t s u n . n i o . c h . S e r v e r S o c k e t A d a p t o r . b i n d ( S e r v e r S o c k e t A d a p t o r . j a v a : 7 4 ) a t s u n . n i o . c h . S e r v e r S o c k e t A d a p t o r . b i n d ( S e r v e r S o c k e t A d a p t o r . j a v a : 6 7 ) a t B i n d F a i l u r e . m a i n ( B i n d F a i l u r e . j a v a : 8 )
  9. NOW WHAT? http://openjdk.java.net/contribute/ check the "JDK" Project of the bug

    database to see if your idea is already described there