dargueta / unicorn-lua Goto Github PK
View Code? Open in Web Editor NEWLua bindings for the Unicorn CPU emulation engine
License: GNU General Public License v2.0
Lua bindings for the Unicorn CPU emulation engine
License: GNU General Public License v2.0
Lua 5.1 and 5.2 exclusively use floating-point numbers, which means that anything over 53 bits can't be represented accurately and will get truncated.
When reading a model-specific register on an x86 machine, uc_reg_read()
expects the register ID to be present in the buffer. There is currently no way to pass the ID of the MSR you want to read, so this always returns the machine check exception address register (judging from this).
We need to modify this to allow specifying a model.
Maximum supported register size is 64 bits. This means we can't read 80-bit floating-point registers, nor the XMMX (128-bit), YMMX (256-bit) or ZMMX (512-bit) registers. Reading these registers will result in a buffer overflow (see #3) and garbage return values.
I've been trying to get this to work off and on for two years, but just getting Unicorn and Lua to build is proving much more difficult than I thought. I would really like to get this working if possible.
So far my major problem is building and installing Lua and Unicorn properly. I'm fairly sure the code itself is cross-platform, but we'll see.
The library is incapable of reading or writing the 80-bit floating-point ST(x) registers on x86 architectures. The encoding and decoding code is buggy so it currently throws an exception rather than give an incorrect answer.
Everything is read as an integer so you're going to get back whatever the binary representation of a floating-point integer is on your machine, which can differ between architectures.
This is fixable, but is going to be tedious and error-prone for architectures other than x86 and MIPS because I'm not as familiar with those.
The current code for reading a register looks like this:
lua_Unsigned value = 0;
error = uc_reg_read(engine, register_id, &value);
On Lua 5.3, integers are 64 bits, so this will result in a buffer overflow when reading the 128-bit XMMX/YMMX etc. registers on an Intel machine. Similarly, reading a 64-bit register on any 32-bit Lua installation will also result in a buffer overflow.
this looks like a nice work! do you want to send a pull req to add this binding to https://github.com/unicorn-engine/unicorn/blob/master/bindings/README?
keep it up, cheers!
Reading from/writing to registers on a big-endian host system won't work for registers that aren't the same size as a Lua integer. This is because the library currently has no concept of register sizes and thus doesn't know how to do typecasts.
Due to how byte order works this doesn't matter on a little-endian host, but on a big-endian host it'll result in things like a 16-bit register getting returned to Lua as 0x7fff000000000000 instead of 0x7fff.
Running luarocks build
fails to compile.
Update: It compiles fine if the -Wall
flag is removed. I really don't want to do this.
The command Make runs (reformatted, paths are slightly modified to remove personal info)
gcc -std=c++11 -DIS_LUAJIT=0 -Wall -Wextra -Werror -Wpedantic -pedantic-errors \
-I./include \
-I./.luaenv-5.4.6/include \
-I./.luaenv-5.4.6/include \
-I/usr/local/include \
-I/usr/local/include \
-O2 -fPIC -c \
-o src/control_functions.o src/control_functions.cpp
GCC barfs:
In file included from /usr/include/c++/12/memory:75,
from src/control_functions.cpp:15:
In member function ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = long unsigned int]’,
inlined from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = long unsigned int; _Dp = std::default_delete<long unsigned int>]’ at /usr/include/c++/12/bits/unique_ptr.h:396:17,
inlined from ‘int ul_ctl_get_exits(lua_State*)’ at src/control_functions.cpp:47:1:
/usr/include/c++/12/bits/unique_ptr.h:95:9: error: ‘void operator delete(void*)’ called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
95 | delete __ptr;
| ^~~~~~~~~~~~
src/control_functions.cpp: In function ‘int ul_ctl_get_exits(lua_State*)’:
src/control_functions.cpp:34:55: note: returned from ‘void* operator new [](std::size_t)’
34 | std::unique_ptr<uint64_t> array(new uint64_t[count]);
| ^
In member function ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = long unsigned int]’,
inlined from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = long unsigned int; _Dp = std::default_delete<long unsigned int>]’ at /usr/include/c++/12/bits/unique_ptr.h:396:17,
inlined from ‘int ul_ctl_get_exits(lua_State*)’ at src/control_functions.cpp:47:1:
/usr/include/c++/12/bits/unique_ptr.h:95:9: error: ‘void operator delete(void*)’ called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
95 | delete __ptr;
| ^~~~~~~~~~~~
src/control_functions.cpp: In function ‘int ul_ctl_get_exits(lua_State*)’:
src/control_functions.cpp:34:55: note: returned from ‘void* operator new [](std::size_t)’
34 | std::unique_ptr<uint64_t> array(new uint64_t[count]);
| ^
In member function ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = long unsigned int]’,
inlined from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = long unsigned int; _Dp = std::default_delete<long unsigned int>]’ at /usr/include/c++/12/bits/unique_ptr.h:396:17,
inlined from ‘int ul_ctl_set_exits(lua_State*)’ at src/control_functions.cpp:88:1:
/usr/include/c++/12/bits/unique_ptr.h:95:9: error: ‘void operator delete(void*)’ called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
95 | delete __ptr;
| ^~~~~~~~~~~~
src/control_functions.cpp: In function ‘int ul_ctl_set_exits(lua_State*)’:
src/control_functions.cpp:74:61: note: returned from ‘void* operator new [](std::size_t)’
74 | std::unique_ptr<uint64_t> entries(new uint64_t[n_entries]);
| ^
In member function ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = long unsigned int]’,
inlined from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = long unsigned int; _Dp = std::default_delete<long unsigned int>]’ at /usr/include/c++/12/bits/unique_ptr.h:396:17,
inlined from ‘int ul_ctl_set_exits(lua_State*)’ at src/control_functions.cpp:88:1:
/usr/include/c++/12/bits/unique_ptr.h:95:9: error: ‘void operator delete(void*)’ called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
95 | delete __ptr;
| ^~~~~~~~~~~~
src/control_functions.cpp: In function ‘int ul_ctl_set_exits(lua_State*)’:
src/control_functions.cpp:74:61: note: returned from ‘void* operator new [](std::size_t)’
74 | std::unique_ptr<uint64_t> entries(new uint64_t[n_entries]);
| ^
cc1plus: all warnings being treated as errors
make: *** [Makefile:194: src/control_functions.o] Error 1
Error: Build error: Failed building.
I didn't test every single one, but it looks like any test involving calls to Lua segfaults on OSX.
Need to add a rockspec file and make it easily installable. The major problem we have here is that we currently rely on Python scripts to process the Unicorn header files, meaning the user has to have Python installed. We need to
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.