Created
          January 27, 2019 20:18 
        
      - 
      
 - 
        
Save oleavr/f86f01aa2d7854ce22d2b1cf795cbe69 to your computer and use it in GitHub Desktop.  
Revisions
- 
        
oleavr created this gist
Jan 27, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ 'use strict'; const slowCallback = new NativeCallback(value => { console.log('slowCallback hit'); return 43; }, 'int', ['int']); const fastCallback = Memory.alloc(Process.pageSize); Memory.patchCode(fastCallback, 128, code => { const cw = new X86Writer(code, { pc: fastCallback }); cw.putCmpRegI32('edi', 10); cw.putJccShortLabel('je', 'match', 'unlikely'); cw.putLabel('nomatch'); cw.putMovRegU64('rax', 42); cw.putJmpShortLabel('done'); cw.putLabel('match'); cw.putSubRegImm('rsp', 8); cw.putCallAddressWithAlignedArguments(slowCallback, ['edi']); cw.putAddRegImm('rsp', 8); cw.putLabel('done'); cw.putRet(); cw.flush(); }); const cb = new NativeFunction(fastCallback, 'int', ['int']); for (let i = 0; i !== 100; i++) { const result = cb(i) console.log(`${i} => ${result}`); }