forked from nervosnetwork/ckb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rewrite current_cycles * Rewrite process_id * Rewrite pipe * Rewrite wait * Rewrite read/write * Rewrite inherited_fd * Rewrite close
- Loading branch information
Showing
14 changed files
with
447 additions
and
264 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::syscalls::CLOSE; | ||
use crate::v2_types::{Message, PipeId, VmId}; | ||
use ckb_vm::{ | ||
registers::{A0, A7}, | ||
Error as VMError, Register, SupportMachine, Syscalls, | ||
}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Debug)] | ||
pub struct Close { | ||
id: VmId, | ||
message_box: Arc<Mutex<Vec<Message>>>, | ||
} | ||
|
||
impl Close { | ||
pub fn new(id: VmId, message_box: Arc<Mutex<Vec<Message>>>) -> Self { | ||
Self { id, message_box } | ||
} | ||
} | ||
|
||
impl<Mac: SupportMachine> Syscalls<Mac> for Close { | ||
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> { | ||
Ok(()) | ||
} | ||
|
||
fn ecall(&mut self, machine: &mut Mac) -> Result<bool, VMError> { | ||
if machine.registers()[A7].to_u64() != CLOSE { | ||
return Ok(false); | ||
} | ||
let pipe = PipeId(machine.registers()[A0].to_u64()); | ||
self.message_box | ||
.lock() | ||
.map_err(|e| VMError::Unexpected(e.to_string()))? | ||
.push(Message::Close(self.id, pipe)); | ||
Err(VMError::External("YIELD".to_string())) | ||
} | ||
} |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::syscalls::INHERITED_FD; | ||
use crate::v2_types::{Message, PipeId, PipeIoArgs, VmId}; | ||
use ckb_vm::{ | ||
registers::{A0, A1, A7}, | ||
Error as VMError, Register, SupportMachine, Syscalls, | ||
}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Debug)] | ||
pub struct InheritedFd { | ||
id: VmId, | ||
message_box: Arc<Mutex<Vec<Message>>>, | ||
} | ||
|
||
impl InheritedFd { | ||
pub fn new(id: VmId, message_box: Arc<Mutex<Vec<Message>>>) -> Self { | ||
Self { id, message_box } | ||
} | ||
} | ||
|
||
impl<Mac: SupportMachine> Syscalls<Mac> for InheritedFd { | ||
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> { | ||
Ok(()) | ||
} | ||
|
||
fn ecall(&mut self, machine: &mut Mac) -> Result<bool, VMError> { | ||
if machine.registers()[A7].to_u64() != INHERITED_FD { | ||
return Ok(false); | ||
} | ||
let buffer_addr = machine.registers()[A0].to_u64(); | ||
let length_addr = machine.registers()[A1].to_u64(); | ||
self.message_box | ||
.lock() | ||
.map_err(|e| VMError::Unexpected(e.to_string()))? | ||
.push(Message::InheritedFileDescriptor( | ||
self.id, | ||
PipeIoArgs { | ||
pipe: PipeId(0), | ||
length: 0, | ||
buffer_addr, | ||
length_addr, | ||
}, | ||
)); | ||
Err(VMError::External("YIELD".to_string())) | ||
} | ||
} |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::syscalls::PIPE; | ||
use crate::v2_types::{Message, PipeArgs, VmId}; | ||
use ckb_vm::{ | ||
registers::{A0, A7}, | ||
Error as VMError, Register, SupportMachine, Syscalls, | ||
}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Debug)] | ||
pub struct Pipe { | ||
id: VmId, | ||
message_box: Arc<Mutex<Vec<Message>>>, | ||
} | ||
|
||
impl Pipe { | ||
pub fn new(id: VmId, message_box: Arc<Mutex<Vec<Message>>>) -> Self { | ||
Self { id, message_box } | ||
} | ||
} | ||
|
||
impl<Mac: SupportMachine> Syscalls<Mac> for Pipe { | ||
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> { | ||
Ok(()) | ||
} | ||
|
||
fn ecall(&mut self, machine: &mut Mac) -> Result<bool, VMError> { | ||
if machine.registers()[A7].to_u64() != PIPE { | ||
return Ok(false); | ||
} | ||
let pipe1_addr = machine.registers()[A0].to_u64(); | ||
let pipe2_addr = pipe1_addr.wrapping_add(8); | ||
self.message_box | ||
.lock() | ||
.map_err(|e| VMError::Unexpected(e.to_string()))? | ||
.push(Message::Pipe( | ||
self.id, | ||
PipeArgs { | ||
pipe1_addr, | ||
pipe2_addr, | ||
}, | ||
)); | ||
Err(VMError::External("YIELD".to_string())) | ||
} | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::syscalls::PROCESS_ID; | ||
use ckb_vm::{ | ||
registers::{A0, A7}, | ||
Error as VMError, Register, SupportMachine, Syscalls, | ||
}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct ProcessID { | ||
id: u64, | ||
} | ||
|
||
impl ProcessID { | ||
pub fn new(id: u64) -> Self { | ||
Self { id } | ||
} | ||
} | ||
|
||
impl<Mac: SupportMachine> Syscalls<Mac> for ProcessID { | ||
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> { | ||
Ok(()) | ||
} | ||
|
||
fn ecall(&mut self, machine: &mut Mac) -> Result<bool, VMError> { | ||
if machine.registers()[A7].to_u64() != PROCESS_ID { | ||
return Ok(false); | ||
} | ||
machine.set_register(A0, Mac::REG::from_u64(self.id)); | ||
Ok(true) | ||
} | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::cost_model::transferred_byte_cycles; | ||
use crate::syscalls::READ; | ||
use crate::v2_syscalls::INVALID_PIPE; | ||
use crate::v2_types::{Message, PipeId, PipeIoArgs, VmId}; | ||
use ckb_vm::{ | ||
registers::{A0, A1, A2, A7}, | ||
Error as VMError, Memory, Register, SupportMachine, Syscalls, | ||
}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Debug)] | ||
pub struct Read { | ||
id: VmId, | ||
message_box: Arc<Mutex<Vec<Message>>>, | ||
} | ||
|
||
impl Read { | ||
pub fn new(id: VmId, message_box: Arc<Mutex<Vec<Message>>>) -> Self { | ||
Self { id, message_box } | ||
} | ||
} | ||
|
||
impl<Mac: SupportMachine> Syscalls<Mac> for Read { | ||
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> { | ||
Ok(()) | ||
} | ||
|
||
fn ecall(&mut self, machine: &mut Mac) -> Result<bool, VMError> { | ||
if machine.registers()[A7].to_u64() != READ { | ||
return Ok(false); | ||
} | ||
let pipe = PipeId(machine.registers()[A0].to_u64()); | ||
let buffer_addr = machine.registers()[A1].to_u64(); | ||
let length_addr = machine.registers()[A2].to_u64(); | ||
let length = machine | ||
.memory_mut() | ||
.load64(&Mac::REG::from_u64(length_addr))? | ||
.to_u64(); | ||
|
||
// We can only do basic checks here, when the message is actually processed, | ||
// more complete checks will be performed. | ||
// We will also leave to the actual write operation to test memory permissions. | ||
if !pipe.is_read() { | ||
machine.set_register(A0, Mac::REG::from_u8(INVALID_PIPE)); | ||
return Ok(true); | ||
} | ||
machine.add_cycles_no_checking(transferred_byte_cycles(length))?; | ||
self.message_box | ||
.lock() | ||
.map_err(|e| VMError::Unexpected(e.to_string()))? | ||
.push(Message::PipeRead( | ||
self.id, | ||
PipeIoArgs { | ||
pipe, | ||
length, | ||
buffer_addr, | ||
length_addr, | ||
}, | ||
)); | ||
Err(VMError::External("YIELD".to_string())) | ||
} | ||
} |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::syscalls::WAIT; | ||
use crate::v2_types::{Message, VmId, WaitArgs}; | ||
use ckb_vm::{ | ||
registers::{A0, A1, A7}, | ||
Error as VMError, Register, SupportMachine, Syscalls, | ||
}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Debug)] | ||
pub struct Wait { | ||
id: VmId, | ||
message_box: Arc<Mutex<Vec<Message>>>, | ||
} | ||
|
||
impl Wait { | ||
pub fn new(id: VmId, message_box: Arc<Mutex<Vec<Message>>>) -> Self { | ||
Self { id, message_box } | ||
} | ||
} | ||
|
||
impl<Mac: SupportMachine> Syscalls<Mac> for Wait { | ||
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> { | ||
Ok(()) | ||
} | ||
|
||
fn ecall(&mut self, machine: &mut Mac) -> Result<bool, VMError> { | ||
if machine.registers()[A7].to_u64() != WAIT { | ||
return Ok(false); | ||
} | ||
let target_id = machine.registers()[A0].to_u64(); | ||
let exit_code_addr = machine.registers()[A1].to_u64(); | ||
self.message_box | ||
.lock() | ||
.map_err(|e| VMError::Unexpected(e.to_string()))? | ||
.push(Message::Wait( | ||
self.id, | ||
WaitArgs { | ||
target_id, | ||
exit_code_addr, | ||
}, | ||
)); | ||
Err(VMError::External("YIELD".to_string())) | ||
} | ||
} |
Oops, something went wrong.