Skip to content

Commit

Permalink
Try to create a minimal reproduction of #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ubolonton committed Apr 18, 2018
1 parent 2590b55 commit e38f82d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ macro_rules! raw_fn {
macro_rules! raw_call_no_exit {
($env:ident, $name:ident $(, $args:expr)*) => {
unsafe {
// println!("raw_call_no_exit {:?}", stringify!($name));
let $name = raw_fn!($env, $name);
$name($env.raw $(, $args)*)
}
Expand All @@ -16,6 +17,7 @@ macro_rules! raw_call_no_exit {
macro_rules! raw_call {
($env:expr, $name:ident $(, $args:expr)*) => {
{
// println!("raw_call {:?}", stringify!($name));
let env = $env;
let result = unsafe {
let $name = raw_fn!(env, $name);
Expand All @@ -29,6 +31,7 @@ macro_rules! raw_call {
macro_rules! raw_call_value {
($env:ident, $name:ident $(, $args:expr)*) => {
{
// println!("raw_call_value {:?}", stringify!($name));
let result: $crate::Result<$crate::raw::emacs_value> = raw_call!($env, $name $(, $args)*);
result.map(|raw| unsafe {
$crate::Value::new(raw, $env)
Expand All @@ -41,6 +44,7 @@ macro_rules! raw_call_value {
macro_rules! call_lisp {
($env:ident, $name:expr $(, $arg:expr)*) => {
{
// println!("call_lisp {:?}", $name);
let symbol: $crate::Value = $env.intern($name)?;
let args = &mut [$($arg.raw,)*];
raw_call_value!($env, funcall, symbol.raw, args.len() as ::libc::ptrdiff_t, args.as_mut_ptr())
Expand Down
6 changes: 6 additions & 0 deletions test-module/src/test.el
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
(require 't)

(ert-deftest lifetime ()
(let ((h (t/use-after-gc)))
(message "Here")
(garbage-collect)
(print h)))

(ert-deftest convert::inc ()
(should (= (t/inc 3) 4))
(should (equal (documentation 't/inc) "1+"))
Expand Down
119 changes: 119 additions & 0 deletions test-module/src/test_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,124 @@ fn to_lowercase_or_nil(env: &CallEnv) -> Result<Value> {
r.into_lisp(env)
}

fn _use_after_gc(env: &CallEnv) -> Result<Value> {
let n = 2;
let h = env.call("make-hash-table", &[
env.intern(":test")?,
env.intern("equal")?,
env.intern(":size")?,
n.into_lisp(env)?
])?;
let mut v = Vec::with_capacity(n as usize);
for _ in 0..n {
v.push("0123456789".into_lisp(env)?);
env.call("garbage-collect", &[])?;
}
env.call("puthash", &[
"0".into_lisp(env)?,
env.call("list", &v)?,
h,
])?;

let l = env.call("list", &[h])?;

// for _ in 0..5 {
// env.call("garbage-collect", &[])?;
// }

env.call("print", &[l])?;
Ok(l)
}

fn use_after_gc(env: &CallEnv) -> Result<Value> {
let n = 2;
let mut v = Vec::with_capacity(n as usize);

// // Crashes
// for _ in 0..n {
// v.push("0".into_lisp(env)?);
// env.call("garbage-collect", &[])?;
// }

// // Crashes
// let mut i = 0;
// while i < n {
// i = i + 1;
// v.push("0".into_lisp(env)?);
// env.call("garbage-collect", &[])?;
// }

// // Crashes
// (|| {
// v.push("0".into_lisp(env).unwrap());
// })();
// (|| {
// v.push("0".into_lisp(env).unwrap());
// })();
// env.call("garbage-collect", &[])?;

// // Doesn't crash
// (|| {
// v.push("0".into_lisp(env).unwrap());
// v.push("0".into_lisp(env).unwrap());
// })();
// env.call("garbage-collect", &[]);

// // Doesn't crashes
// v.push("0".into_lisp(env).unwrap());
// (|| {
// v.push("0".into_lisp(env).unwrap());
// })();
// env.call("garbage-collect", &[]);

// // Crashes
// v.push("0".into_lisp(env).unwrap());
// (|| {
// v.push("0".into_lisp(env).unwrap());
// })();
// v.push("0".into_lisp(env)?);
// env.call("garbage-collect", &[])?;

// Crashes
(|| {
v.push("0".into_lisp(env).unwrap());
})();
"0".into_lisp(env)?;
// env.message("0")?; // Also crashes
env.call("garbage-collect", &[])?;

// // Doesn't crash (? -> .unwrap())
// (|| {
// v.push("0".into_lisp(env).unwrap());
// })();
// "0".into_lisp(env).unwrap();
// env.call("garbage-collect", &[])?;

// // Doesn't crash
// {
// v.push("0".into_lisp(env)?);
// env.call("garbage-collect", &[])?;
// }
// {
// v.push("0".into_lisp(env)?);
// env.call("garbage-collect", &[])?;
// }

// // Doesn't crash
// let x = "0".into_lisp(env)?;
// for _ in 0..n {
// v.push(x);
// env.call("garbage-collect", &[])?;
// }

println!("v={:?}", v);
let l = env.call("list", &v)?;
println!("1");
env.call("print", &[l])?;
println!("2");
Ok(l)
}

pub fn init(env: &Env) -> Result<()> {
using_fset(env)?;
using_defuns(env)?;
Expand All @@ -112,6 +230,7 @@ pub fn init(env: &Env) -> Result<()> {
env, *MODULE_PREFIX, {
"sum" => (sum, 2..2),
"to-lowercase-or-nil" => (to_lowercase_or_nil, 1..1),
"use-after-gc" => (use_after_gc, 0..0),
}
}

Expand Down

0 comments on commit e38f82d

Please sign in to comment.