touched up unsafe raw pointer handling routines

This commit is contained in:
hyunghwan.chung 2019-09-07 17:48:02 +00:00
parent be71f46b40
commit 4d6cd7840a
2 changed files with 64 additions and 117 deletions

View File

@ -409,6 +409,6 @@ class(#limited) SmallPointer(Object)
method(#primitive) free.
}
class(#limited,#immutable,#word(1)) LargePointer(Object)
class(#limited,#immutable,#word(1)) LargePointer(SmallPointer)
{
}

View File

@ -24,28 +24,8 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include "moo-prv.h"
/* TODO: experimental */
#include <signal.h>
#include <sys/ucontext.h>
static int segfault = 0;
void on_segfault (int sig, siginfo_t* si, void* ctx)
{
ucontext_t* context = (ucontext_t*)ctx;
printf ("SEG FAULT AT %p instruction at %p\n", si->si_addr, context->uc_mcontext.gregs[REG_RIP]);
/*printf ("SEG FAULT AT %p instruction at %p\n", si->si_addr, context->uc_mcontext.arm_pc); arm */
segfault = 1;
context->uc_mcontext.gregs[REG_RIP] += 1; /* very platform and instruction specific... */
}
moo_pfrc_t moo_pf_system_collect_garbage (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_gc (moo);
@ -302,13 +282,12 @@ static MOO_INLINE moo_oop_t _fetch_raw_uint (moo_t* moo, moo_uint8_t* rawptr, mo
return moo_oowtoint(moo, v);
}
static MOO_INLINE int _store_raw_int (moo_t* moo, moo_uint8_t* rawptr, moo_oow_t offset, int size, moo_oop_t voop)
{
int n;
moo_ooi_t w, max, min;
if (moo_inttoooi(moo, voop, &w) == 0)
if ((n = moo_inttoooi(moo, voop, &w)) == 0) /* not convertable */
{
moo_seterrbfmt (moo, moo_geterrnum(moo), "invalid value %O for raw signed memory store", voop);
return -1;
@ -324,35 +303,40 @@ static MOO_INLINE int _store_raw_int (moo_t* moo, moo_uint8_t* rawptr, moo_oow_t
return -1;
}
n = 0;
switch (size)
{
case 1:
((struct st_int8_t*)&rawptr[offset])->v = w;
return 0;
break;
case 2:
((struct st_int16_t*)&rawptr[offset])->v = w;
return 0;
break;
case 4:
((struct st_int32_t*)&rawptr[offset])->v = w;
return 0;
break;
#if defined(MOO_HAVE_INT64_T) && (MOO_SIZEOF_OOW_T >= MOO_SIZEOF_INT64_T)
case 8:
((struct st_int64_t*)&rawptr[offset])->v = w;
return 0;
break;
#endif
#if defined(MOO_HAVE_INT128_T) && (MOO_SIZEOF_OOW_T >= MOO_SIZEOF_INT128_T)
case 16:
((struct st_int128_t*)&rawptr[offset])->v = w;
return 0;
break;
#endif
default:
moo_seterrbfmt (moo, MOO_EINVAL, "unsupported size %d for raw signed memory store", size);
n = -1;
}
moo_seterrbfmt (moo, MOO_EINVAL, "unsupported size %d for raw signed memory store", size);
return -1;
return n;
}
static MOO_INLINE int _store_raw_uint (moo_t* moo, moo_uint8_t* rawptr, moo_oow_t offset, int size, moo_oop_t voop)
@ -380,21 +364,7 @@ static MOO_INLINE int _store_raw_uint (moo_t* moo, moo_uint8_t* rawptr, moo_oow_
return -1;
}
#if defined(_WIN32)
__try
{
#else
struct sigaction sa, oldsa1, oldsa2;
MOO_MEMSET (&sa, 0, MOO_SIZEOF(sa));
sigemptyset (&sa.sa_mask);
sa.sa_sigaction = on_segfault;
sa.sa_flags = SA_SIGINFO;
sigaction (SIGSEGV, &sa, &oldsa1);
sigaction (SIGBUS, &sa, &oldsa2);
segfault = 0;
#endif
n = 1; /* ok */
n = 0;
switch (size)
{
case 1:
@ -422,34 +392,11 @@ static MOO_INLINE int _store_raw_uint (moo_t* moo, moo_uint8_t* rawptr, moo_oow_
#endif
default:
n = 0; /* not ok */
break;
}
#if defined(_WIN32)
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
moo_seterrbfmt (moo, MOO_EACCES, "invalid memory access at %p", (moo_uint8_t*)rawptr + offset);
return -1;
}
#else
if (n)
{
sigaction (SIGSEGV, &oldsa1, MOO_NULL);
sigaction (SIGBUS, &oldsa2, MOO_NULL);
if (segfault)
{
moo_seterrbfmt (moo, MOO_EACCES, "invalid memory access at %p", (moo_uint8_t*)rawptr + offset);
return -1;
}
return 0;
}
#endif
moo_seterrbfmt (moo, MOO_EINVAL, "unsupported size %d for raw unsigned memory store", size);
return -1;
n = -1;
}
return n;
}
/* ------------------------------------------------------------------------------------- */