Giter VIP home page Giter VIP logo

Comments (6)

jon-chuang avatar jon-chuang commented on July 24, 2024

@Lichtso
Here is how to do dynamic memory mapping:

Maintain the following metadata in EbpfElf and JitProgram:

// fields should always be positive
struct SectionInfo {
    bp_host_addr: i64,
    len: i64,
}

struct EbpfElf {
  ..
  // sections as indexed from 1 to 4
  section_infos: [SectionInfo; 4],
}

// memory remapping ops
SUBTRACT_HOST_ADDR: u8 = 0;
ADD_HOST_ADDR: u8 = 1;
SUBTRACT_LEN: u8 = 2;
ADD_LEN: u8 = 3;

struct OffsetEntry {
  offset: isize,
  op: u8,
}

struct JitProgram {
  ..
  // sections as indexed from 1 to 4
  section_infos: [SectionInfo; 4],
  remap_offsets: [Vec<OffsetEntry>; 4],
}

impl JitProgram {
  fn remap_memory(&mut self, new_section_infos: [SectionInfo; 4]) {
    let text_ptr_i64_loc = jit_program._sections.text_section as *mut i64;
    for ((info, new_info), offsets) self.section_infos.iter().zip(&new_section_infos).zip(&self.remap_offsets) {
      for entry in &offsets {
        let modifier = match entry.op {
          0 => info.bp_host_addr - new_info.bp_host_addr,
          1 => new_info.bp_host_addr - info.bp_host_addr,
          2 => info.len - new_info.len,
          3 => new_info.len - info.len,
          _ => Err,
        };
        unsafe { *text_ptr_i64_loc.offset(entry.offset) += modifier; }
      }
    }
    self.section_infos = new_infos;
  }
}

impl JitCompiler {
..
  fn emit_memory_translation(..) {
    // subtract input section's host base ptr addr from x86 i64 immediate.
    jit.add_offset_entry(
      section_index::INPUT, 
      remap_ops::SUBTRACT_HOST_ADDR,
      (jit.offset_in_text_section + opcode_leading_size) as isize, 
    );
    ..
  }
}

from rbpf.

Lichtso avatar Lichtso commented on July 24, 2024

I think this part here won't work:

struct EbpfElf {
  ..
  // sections as indexed from 1 to 4
  section_infos: [SectionInfo; 4],
}

The MemoryMapping must be in the VM, not the Executable, because the same Executable can be instanced and run in different VMs across multiple threads simultaneously (each having a different set of host addresses). Also, this is only intended for the "input" memory region, right?

from rbpf.

jon-chuang avatar jon-chuang commented on July 24, 2024

@Lichtso

Hmm, I believe this approach requires making a local copy of the executable everytime a VM is created.

That's because each instance should have its own memory mapping. So each instance needs to have a unique remapped executable.

So maybe I ought to first do a local copy into the VM. i.e. the VM should have a local copy of the JitProgram.

from rbpf.

jon-chuang avatar jon-chuang commented on July 24, 2024

@Lichtso , maybe in order to save on copies, I should make the semantics copy-on-write if shared, else write in place.

Well, one executor could be about 1MB or so, so this can save on copies. 1MB copy will take about 50-120us. This is already somewhat long. It's not clear the remapping operation will be quick either, since it needs to write to many random memory locations.

from rbpf.

jon-chuang avatar jon-chuang commented on July 24, 2024

Here is a simplified strategy to bounds checking:

  1. First, add the offset to the register.
  2. Check via the 33rd bit which section it is pointing to. jmp to section's bounds logic.
  3. Do the upperbound check. If fail, jmp to OOB error. Add the required constant (host_addr_bp - section_addr_bp) to the vm_addr, and store it in the register. jmp to return.
  4. perform operation

from rbpf.

Lichtso avatar Lichtso commented on July 24, 2024

I will close this for now, as #196 was merged.
The next thing to approach is solana-labs/solana#14523 and redesigning the entrypoint / ABI.
We might come back to another redesign of the address translation, but for now I think we have a reasonably good solution while maintaining backwards compatibility.

from rbpf.

Related Issues (20)

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.