Skip to content

Commit

Permalink
Fix bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
sisshiki1969 committed Jan 4, 2025
1 parent 8cd0114 commit e7ce3f8
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 90 deletions.
8 changes: 0 additions & 8 deletions monoruby/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,6 @@ impl<T: GCBox> Allocator<T> {
self.pages.len() + 1
}

pub fn set_enabled(enable: bool) -> bool {
ALLOC.with(|alloc| {
let old = alloc.borrow().gc_enabled;
alloc.borrow_mut().gc_enabled = enable;
old
})
}

///
/// Allocate object.
///
Expand Down
9 changes: 9 additions & 0 deletions monoruby/src/builtins/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(super) fn init(globals: &mut Globals) -> Module {
globals.define_builtin_module_func(kernel_class, "__dir__", dir_, 0);
globals.define_builtin_func(kernel_class, "__assert", assert, 2);
globals.define_builtin_func(kernel_class, "__dump", dump, 0);
globals.define_builtin_func(kernel_class, "__check_stack", check_stack, 0);
globals.define_builtin_func(kernel_class, "__instance_ty", instance_ty, 0);
globals.define_builtin_func(
kernel_class,
Expand Down Expand Up @@ -322,6 +323,14 @@ fn dump(vm: &mut Executor, globals: &mut Globals, _lfp: Lfp) -> Result<Value> {
Ok(Value::nil())
}

#[monoruby_builtin]
fn check_stack(vm: &mut Executor, globals: &mut Globals, _lfp: Lfp) -> Result<Value> {
if crate::runtime::_check_stack(vm, globals) {
crate::runtime::_dump_stacktrace(vm, globals);
}
Ok(Value::nil())
}

#[monoruby_builtin]
fn instance_ty(_vm: &mut Executor, globals: &mut Globals, lfp: Lfp) -> Result<Value> {
let class_id = lfp.self_val().class();
Expand Down
19 changes: 19 additions & 0 deletions monoruby/src/compiler/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,3 +864,22 @@ pub extern "C" fn _dump_stacktrace(vm: &mut Executor, globals: &mut Globals) {
}
eprintln!("-----end stacktrace");
}

pub extern "C" fn _check_stack(vm: &mut Executor, globals: &mut Globals) -> bool {
let mut invalid = false;
let mut cfp = vm.cfp();
unsafe {
for _ in 0..16 {
let prev_cfp = cfp.prev();
if globals.check_frame_info(cfp.lfp()) {
invalid = true;
};
if let Some(prev_cfp) = prev_cfp {
cfp = prev_cfp;
} else {
break;
}
}
}
invalid
}
14 changes: 7 additions & 7 deletions monoruby/src/compiler/vmgen/method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,19 @@ impl Codegen {
// version mismatch
slow_path2:
subq rsp, 1024;
//pushq rcx;
//subq rsp, 8;
//movq rdi, rcx;
//movq rax, (dump_rdi);
//call rax;
//addq rsp, 8;
//popq rcx;
movq rdi, rbx;
movq rsi, r12;
movl rdx, [r13 + (CALLSITE_ID)]; // CallSiteId
movq rax, (runtime::find_method);
call rax; // rax <- Option<FuncId>
movl rax, rax;
//pushq rax;
//subq rsp, 8;
//movq rdi, rax;
//movq rax, (dump_rdi);
//call rax;
//addq rsp, 8;
//popq rax;
addq rsp, 1024;
);
self.vm_handle_error();
Expand Down
22 changes: 19 additions & 3 deletions monoruby/src/globals/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ impl Globals {
eprint!(" ");
for r in 0..meta.reg_num() as usize {
eprint!(
"%{}{}:[{}] ",
"%{}:[{}] ",
r,
if r == 0 { "(self)" } else { "" },
if let Some(v) = lfp.register(r) {
v.debug(&self.store)
if let Some(s) = v.debug_check(&self.store) {
s
} else {
"INVALID".to_string()
}
} else {
"None".to_string()
}
Expand All @@ -42,6 +45,19 @@ impl Globals {
eprintln!();
}

pub(crate) unsafe fn check_frame_info(&self, lfp: Lfp) -> bool {
let meta = lfp.meta();
let mut invalid = false;
for r in 0..meta.reg_num() as usize {
if let Some(v) = lfp.register(r) {
if v.debug_check(&self.store).is_none() {
invalid = true;
}
}
}
invalid
}

#[cfg(feature = "emit-bc")]
pub fn dump_bc(&mut self) {
let dumped_bc = self.dumped_bc;
Expand Down
46 changes: 41 additions & 5 deletions monoruby/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,31 @@ impl Value {
NIL_VALUE => NIL_CLASS,
TRUE_VALUE => TRUE_CLASS,
FALSE_VALUE => FALSE_CLASS,
_ => unreachable!("Illegal packed value. {:x}", self.0),
_ => unreachable!("Illegal packed value. 0x{:016x}", self.0),
}
}
}

pub(crate) fn debug_class(&self) -> Option<ClassId> {
let class = if self.is_fixnum() {
INTEGER_CLASS
} else if self.is_flonum() {
FLOAT_CLASS
} else if let Some(rv) = self.try_rvalue() {
rv.debug_class()?
} else if self.is_symbol() {
SYMBOL_CLASS
} else {
match self.0.get() {
NIL_VALUE => NIL_CLASS,
TRUE_VALUE => TRUE_CLASS,
FALSE_VALUE => FALSE_CLASS,
_ => return None,
}
};
Some(class)
}

pub(crate) fn ty(&self) -> Option<ObjTy> {
self.try_rvalue().map(|rv| rv.ty())
}
Expand Down Expand Up @@ -447,7 +467,7 @@ impl Value {
} else if let Some(f) = self.try_flonum() {
RV::Float(f)
} else if self.id() == 0 {
RV::None
RV::Invalid
} else if let Some(rv) = self.try_rvalue() {
rv.unpack()
} else if self.is_symbol() {
Expand All @@ -471,7 +491,7 @@ impl Value {
///
pub fn debug(&self, store: &Store) -> String {
match self.unpack() {
RV::None => "UNDEFINED".to_string(),
RV::Invalid => "UNDEFINED".to_string(),
RV::Nil => "nil".to_string(),
RV::Bool(b) => format!("{:?}", b),
RV::Fixnum(n) => format!("{}", n),
Expand All @@ -484,6 +504,22 @@ impl Value {
}
}

pub fn debug_check(&self, store: &Store) -> Option<String> {
let s = match self.unpack() {
RV::Invalid => return None,
RV::Nil => "nil".to_string(),
RV::Bool(b) => format!("{:?}", b),
RV::Fixnum(n) => format!("{}", n),
RV::BigInt(n) => format!("{}", n),
RV::Float(f) => dtoa::Buffer::new().format(f).to_string(),
RV::Complex(_) => self.as_complex().debug(store),
RV::Symbol(id) => format!(":{id}"),
RV::String(s) => format!(r#""{}""#, s.inspect()),
RV::Object(rvalue) => rvalue.debug(store),
};
Some(s)
}

pub fn debug_tos(&self, store: &Store) -> String {
let s = match self.unpack() {
RV::Nil => "".to_string(),
Expand Down Expand Up @@ -1242,7 +1278,7 @@ impl Value {

#[derive(Clone, PartialEq)]
pub enum RV<'a> {
None,
Invalid,
Nil,
Bool(bool),
Fixnum(i64),
Expand All @@ -1257,7 +1293,7 @@ pub enum RV<'a> {
impl<'a> std::fmt::Debug for RV<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RV::None => write!(f, "Undef"),
RV::Invalid => write!(f, "Undef"),
RV::Nil => write!(f, "nil"),
RV::Bool(b) => write!(f, "{b:?}"),
RV::Fixnum(n) => write!(f, "{n}"),
Expand Down
Loading

0 comments on commit e7ce3f8

Please sign in to comment.