unicorn-engine / unicorn

Unicorn CPU emulator framework (ARM, AArch64, M68K, Mips, Sparc, PowerPC, RiscV, S390x, TriCore, X86)
http://www.unicorn-engine.org
GNU General Public License v2.0
7.33k stars 1.31k forks source link

Not starting on x64 with low memory #1898

Open dchristl opened 8 months ago

dchristl commented 8 months ago

Hello,

thanks for this great project. I'm currently trying out pypush in the cloud. It uses Unicorn internally but cannot start because an error occurs:

Could not allocate dynamic translation buffer The server I'm trying this on is hosted in the Oracle Cloud (Free Tier with 1 x64 processor and 1 GB of RAM). If I interpret the Unicorn source correctly, it automatically reserves 1 GB of RAM upon startup for an x64 processor (DEFAULT_CODE_GEN_BUFFER_SIZE_1). However, since the server is very limited and cannot afford this, it results in this error. Furthermore, I came across this comment in the code:

We expect most system emulation to run one or two guests per host. Users running large scale system emulation may want to tweak their runtime setup via the tb-size control on the command line.

How can I set the 'tb-size' parameter using the Python bindings, or is there a better alternative to run this on a limited system?

Kind Regards Danny

wtdcode commented 8 months ago

This has been resolved in our dev branch while not released. You could build it by yourself with: https://github.com/unicorn-engine/unicorn/wiki/Compile#build-python-bindings

dchristl commented 8 months ago

Thank you, I will try it out.

dchristl commented 8 months ago

Sorry, there is exact the same issue with the dev-Version (2.0.2). I've reinstalled it many times and cleared the cache and left folders. Any advice?

wtdcode commented 7 months ago

That's weird. I have exactly the same Oracle instance and it works well.

dchristl commented 7 months ago

Could you point me to the commit, where this should be solved? There are not really changes any near to the code with the issue. I will debug the current release and the dev-version, but I don't think there will be any difference in the output/ memory consumption.

hInfern0 commented 7 months ago

I don't think its only on x64 when i am even trying with arm like with go , rust and python it takes like 1gb of ram with the smallest memory map i tried the sample from rust

use unicorn_engine::{Unicorn, RegisterARM};
use unicorn_engine::unicorn_const::{Arch, Mode, Permission, SECOND_SCALE};
use std::{thread, time};

fn main() {
    let arm_code32: Vec<u8> = vec![0x17, 0x00, 0x40, 0xe2]; // sub r0, #23

    let mut unicorn = Unicorn::new(Arch::ARM, Mode::LITTLE_ENDIAN).expect("failed to initialize Unicorn instance");
    let emu = &mut unicorn;
    emu.mem_map(0x1000, 0x4000, Permission::ALL).expect("failed to map code page");
    emu.mem_write(0x1000, &arm_code32).expect("failed to write instructions");

    emu.reg_write(RegisterARM::R0, 123).expect("failed write R0");
    emu.reg_write(RegisterARM::R5, 1337).expect("failed write R5");
    thread::sleep(time::Duration::from_millis(20000)); // 
    let _ = emu.emu_start(0x1000, (0x1000 + arm_code32.len()) as u64, 10 * SECOND_SCALE, 1000);
    assert_eq!(emu.reg_read(RegisterARM::R0), Ok(100));
    assert_eq!(emu.reg_read(RegisterARM::R5), Ok(1337));
}
// cargo.toml deps 
//[dependencies]
//unicorn-engine = "2.0.1"

image