From dd6186d1f2854b530e3f06bc7df6e4a2ad05550f Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Thu, 5 Sep 2019 08:45:04 +0000 Subject: [PATCH] experiment with unaligned address in ffi --- moo/kernel/System.moo | 4 ++++ moo/kernel/test-bi.moo | 3 ++- moo/lib/moo.c | 2 +- moo/lib/pf-sys.c | 4 ++-- moo/mod/ffi.c | 33 ++++++++++++++++++++++++++------- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/moo/kernel/System.moo b/moo/kernel/System.moo index 868b313..b081acf 100644 --- a/moo/kernel/System.moo +++ b/moo/kernel/System.moo @@ -408,3 +408,7 @@ class SmallPointer(Object) method(#primitive) free. } + +class LargePointer(Object) +{ +} diff --git a/moo/kernel/test-bi.moo b/moo/kernel/test-bi.moo index 800acaf..6ef7a58 100644 --- a/moo/kernel/test-bi.moo +++ b/moo/kernel/test-bi.moo @@ -40,7 +40,8 @@ class MyObject(Object) | ffi now | - ffi := FFI new: 'libc.so.6'. + [ ffi := FFI new: 'libc.so.6'. ] on: Exception do: [:ex | ffi := FFI new: 'libc.so' ]. + now := ffi call: #time signature: 'l>i' arguments: #(0). ////ffi call: #srand signature: 'i>' arguments: ##(now). ffi call: #srandom signature: 'i>' arguments: ##(now). diff --git a/moo/lib/moo.c b/moo/lib/moo.c index 2a3bb95..bfc81ae 100644 --- a/moo/lib/moo.c +++ b/moo/lib/moo.c @@ -840,7 +840,7 @@ int moo_genpfmethod (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, moo_met goto oops; } - mnsym = (moo_oop_char_t)moo_makesymbol (moo, mthname, i); + mnsym = (moo_oop_char_t)moo_makesymbol(moo, mthname, i); if (!mnsym) goto oops; moo_pushvolat (moo, (moo_oop_t*)&mnsym); tmp_count++; diff --git a/moo/lib/pf-sys.c b/moo/lib/pf-sys.c index 9ab70de..a049d72 100644 --- a/moo/lib/pf-sys.c +++ b/moo/lib/pf-sys.c @@ -282,7 +282,7 @@ static MOO_INLINE moo_oop_t _fetch_raw_uint (moo_t* moo, moo_uint8_t* rawptr, mo return MOO_NULL; } - return moo_oowtoint (moo, v); + return moo_oowtoint(moo, v); } @@ -1093,7 +1093,7 @@ moo_pfrc_t moo_pf_smptr_as_string (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) ptr = MOO_OOP_TO_SMPTR(rcv); sprintptr (buf, (moo_oow_t)ptr, &len); - ss = moo_makestring (moo, buf, len); + ss = moo_makestring(moo, buf, len); if (!ss) return MOO_PF_FAILURE; MOO_STACK_SETRET (moo, nargs, ss); diff --git a/moo/mod/ffi.c b/moo/mod/ffi.c index 3d67cda..7b0075b 100644 --- a/moo/mod/ffi.c +++ b/moo/mod/ffi.c @@ -542,7 +542,8 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) sig = MOO_STACK_GETARG(moo, nargs, 1); args = MOO_STACK_GETARG(moo, nargs, 2); - if (!MOO_OOP_IS_SMPTR(fun)) goto inval; + if (MOO_OOP_IS_SMPTR(fun)) f = MOO_OOP_TO_SMPTR(fun); + else if (moo_inttooow(moo, fun, (moo_oow_t*)&f) <= -1) goto softfail; /* the signature must not be empty. at least the return type must be * specified */ @@ -553,9 +554,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) if (MOO_OBJ_GET_SIZE(sig) > 1 && MOO_CLASSOF(moo,args) != moo->_array) goto inval; #endif - f = MOO_OOP_TO_SMPTR(fun); arr = (moo_oop_oop_t)args; - /*MOO_DEBUG2 (moo, " %p in %p\n", f, ffi->handle);*/ #if defined(USE_DYNCALL) @@ -782,7 +781,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) if (!MOO_IN_SMPTR_RANGE(r)) { /* the returned pointer is not aligned */ - goto softfail; + goto inval; } MOO_STACK_SETRET (moo, nargs, MOO_SMPTR_TO_OOP(r)); @@ -827,7 +826,7 @@ hardfail: static moo_pfrc_t pf_getsym (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) { ffi_t* ffi; - moo_oop_t name; + moo_oop_t name, ret; void* sym; MOO_ASSERT (moo, nargs == 1); @@ -852,8 +851,28 @@ static moo_pfrc_t pf_getsym (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) MOO_DEBUG4 (moo, " %.*js => %p in %p\n", MOO_OBJ_GET_SIZE(name), MOO_OBJ_GET_CHAR_SLOT(name), sym, ffi->handle); - MOO_ASSERT (moo, MOO_IN_SMPTR_RANGE(sym)); - MOO_STACK_SETRET (moo, nargs, MOO_SMPTR_TO_OOP(sym)); +#if 0 + ret = moo_oowtoptr(moo, (moo_oow_t)sym); + if (!ret) goto softfail; + +#else + if (MOO_IN_SMPTR_RANGE(sym)) + { + ret = MOO_SMPTR_TO_OOP(sym); + } + else + { + ret = moo_oowtoint(moo, (moo_oow_t)sym); + if (!ret) goto softfail; + /* + MOO_DEBUG1 (moo, " unaligned symbol address - %p\n", sym); + moo_seterrnum (moo, MOO_EINVAL); + goto softfail; + */ + } +#endif + + MOO_STACK_SETRET (moo, nargs, ret); return MOO_PF_SUCCESS; softfail: