Giter VIP home page Giter VIP logo

lvm's Introduction

LVM

Lua virtualsation toolset in Java

Parts

  • bytecode parsing libary (5.x)
  • instruction sets (DONE: 5.1)
  • emulation of execution (IN PROGRESS)
  • access of frames and interception

Goals

To be used to analyse and other things on compiled lua scripts

Usages

File parser

The file parser works for all luac compiled files from version 5.0 -> 5.3
Here is some simple code to read a compiled file:

...
byte[] data = ...;
LuaDataStream stream = new LuaDataStream(data);
LuaFileReader reader = new LuaFileReader(stream);

LuaFile file = reader.read();

Disassembly and intstruction operations

Disassembly gives direct insight into the programs behaviour and operations

...
LuaFunction main = file.getFunction();
Disassembler disasm = new Disassembler(main, System.out);
disasm.disassemble(); // print disassembly to main

// assuming that some function calls get set up in this sample
ClosureInstruction inst = main.getInstructions().get(0); 
// get the function referenced by this instruction
LuaFunction refFunction = inst.getProto();
...

Virtual machine & code exection

The Virtual machine allows for virtualizing and executing compiled bytecode allowing for full static analysis of lua binaries

// prepare a vm
VM vm = new VM();
vm.initialize();
// execute the main function
ExecutionContext result = vm.getHelper().invoke(main, vm.getGlobal());
Value val = results.get(0); // get R0

System.out.println(val.asString()); // print R0

Function overwriting

In the vm it is possible to overwrite functions to give control what these do, this includes library functions

// get the vm libraries
Libraries libs = vm.getLibraries();
Library base = libs.getTop(); // get the top library (the base library)
// choose a method (in this case 'print')
// then provide a new method to execute
base.set("print", (ctx) -> {
    System.out.println("Custom print call!");	
});

// or during/before execution access the global table and overwrite functions directly
Table global = vm.getGlobal();
// first define a new Closure
Closure customClosure = new Closure((ctx) -> {   
	System.out.println("Intercepted call!");
});
// wrap in ClosureValue
ClosureValue value = new ClosureValue(customClosure);        
// set new closure
global.set("customFunction", value);

Instruction overwriting

It is also possible to overwrite the behaviour of specific instructions entirely

vm.getInterpreter().install(Opcodes.MOVE, (Executor<LoadInstruction>) (insn, ctx) -> {
	System.out.println("Custom instructions");
});

lvm's People

Contributors

jumanji144 avatar divisiion avatar

Stargazers

ProfessionalSkid avatar W13KM avatar dingyi avatar 难忘的旋律 avatar  avatar NickAc avatar Sightem avatar Mikhail Khromov avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.