From 4bfdb1c33a6317f4f64ce7ccaf480ca38edfc684 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Mon, 8 Mar 2004 00:27:52 +0000 Subject: [PATCH] Added support for stack allocation... --- .../RepairCompiler/MCC/Runtime/instrument.cc | 4 + .../RepairCompiler/MCC/Runtime/instrument.h | 2 +- Repair/RepairCompiler/MCC/Runtime/size.h | 21 ++--- Repair/RepairCompiler/MCC/Runtime/stack.c | 77 +++++++++++++++++++ Repair/RepairCompiler/MCC/Runtime/stack.h | 1 + Repair/RepairCompiler/MCC/Runtime/tmap.cc | 55 +++++++++---- Repair/RepairCompiler/MCC/Runtime/tmap.h | 3 + 7 files changed, 136 insertions(+), 27 deletions(-) create mode 100755 Repair/RepairCompiler/MCC/Runtime/stack.c create mode 100755 Repair/RepairCompiler/MCC/Runtime/stack.h diff --git a/Repair/RepairCompiler/MCC/Runtime/instrument.cc b/Repair/RepairCompiler/MCC/Runtime/instrument.cc index fdbdd50..134dd56 100755 --- a/Repair/RepairCompiler/MCC/Runtime/instrument.cc +++ b/Repair/RepairCompiler/MCC/Runtime/instrument.cc @@ -69,3 +69,7 @@ bool assertvalidtype(int ptr, int structure) { bool assertvalidmemory(int ptr, int structure) { return memmap->assertvalidmemory((void *)ptr, structure); } + +void initializestack(void *high) { + memmap->initializestack(high); +} diff --git a/Repair/RepairCompiler/MCC/Runtime/instrument.h b/Repair/RepairCompiler/MCC/Runtime/instrument.h index 88648bb..6037682 100755 --- a/Repair/RepairCompiler/MCC/Runtime/instrument.h +++ b/Repair/RepairCompiler/MCC/Runtime/instrument.h @@ -17,6 +17,6 @@ typeobject * gettypeobject(); void resettypemap(); bool assertvalidtype(int ptr, int structure); bool assertvalidmemory(int ptr, int structure); - +void initializestack(void *); extern typemap * memmap; #endif diff --git a/Repair/RepairCompiler/MCC/Runtime/size.h b/Repair/RepairCompiler/MCC/Runtime/size.h index 7c57bce..1b5021d 100755 --- a/Repair/RepairCompiler/MCC/Runtime/size.h +++ b/Repair/RepairCompiler/MCC/Runtime/size.h @@ -1,11 +1,14 @@ +#include "ex_aux.h" class typeobject { - public: - typeobject(); - int getfield(int type, int fieldindex); //returns type - int isArray(int type, int fieldindex); //returns if array - int numElements(int type, int fieldindex); //returns number of elements - int size(int type); - int getnumfields(int type); - bool issubtype(int subtype, int type); - void reset(); +public: +typeobject(); +int getfield(int type, int fieldindex); +int isArray(int type, int fieldindex); +int isPtr(int type, int fieldindex); +int numElements(int type, int fieldindex); +int size(int type); +int sizeBytes(int type); +int getnumfields(int type); +bool issubtype(int subtype, int type); +void computesizes(foo_state *); }; diff --git a/Repair/RepairCompiler/MCC/Runtime/stack.c b/Repair/RepairCompiler/MCC/Runtime/stack.c new file mode 100755 index 0000000..838293d --- /dev/null +++ b/Repair/RepairCompiler/MCC/Runtime/stack.c @@ -0,0 +1,77 @@ +/* +Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers +Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. +Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved. +Copyright (c) 1999-2001 by Hewlett-Packard. All rights reserved. + +THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED +OR IMPLIED. ANY USE IS AT YOUR OWN RISK. + +Permission is hereby granted to use or copy this program +for any purpose, provided the above notices are retained on all copies. +Permission to modify the code and to distribute modified code is granted, +provided the above notices are retained, and a notice that the code was +modified is included with the above copyright notice. + */ + + +#include +#include +#include +#include +#define ptr_t void * +#pragma weak __libc_stack_end +extern ptr_t __libc_stack_end; +#define word unsigned int +# define STAT_SKIP 27 /* Number of fields preceding startstack */ + /* field in /proc/self/stat */ +#define ABORT printf + + ptr_t GC_linux_stack_base(void) + { + /* We read the stack base value from /proc/self/stat. We do this */ + /* using direct I/O system calls in order to avoid calling malloc */ + /* in case REDIRECT_MALLOC is defined. */ +# define STAT_BUF_SIZE 4096 +# define STAT_READ read + /* Should probably call the real read, if read is wrapped. */ + char stat_buf[STAT_BUF_SIZE]; + int f; + char c; + word result = 0; + size_t i, buf_offset = 0; + + /* First try the easy way. This should work for glibc 2.2 */ + if (0 != &__libc_stack_end) { +# ifdef IA64 + /* Some versions of glibc set the address 16 bytes too */ + /* low while the initialization code is running. */ + if (((word)__libc_stack_end & 0xfff) + 0x10 < 0x1000) { + return __libc_stack_end + 0x10; + } /* Otherwise it's not safe to add 16 bytes and we fall */ + /* back to using /proc. */ +# else + return __libc_stack_end; +# endif + } + f = open("/proc/self/stat", O_RDONLY); + if (f < 0 || STAT_READ(f, stat_buf, STAT_BUF_SIZE) < 2 * STAT_SKIP) { + ABORT("Couldn't read /proc/self/stat"); + } + c = stat_buf[buf_offset++]; + /* Skip the required number of fields. This number is hopefully */ + /* constant across all Linux implementations. */ + for (i = 0; i < STAT_SKIP; ++i) { + while (isspace(c)) c = stat_buf[buf_offset++]; + while (!isspace(c)) c = stat_buf[buf_offset++]; + } + while (isspace(c)) c = stat_buf[buf_offset++]; + while (isdigit(c)) { + result *= 10; + result += c - '0'; + c = stat_buf[buf_offset++]; + } + close(f); + if (result < 0x10000000) ABORT("Absurd stack bottom value"); + return (ptr_t)result; + } diff --git a/Repair/RepairCompiler/MCC/Runtime/stack.h b/Repair/RepairCompiler/MCC/Runtime/stack.h new file mode 100755 index 0000000..293fe7f --- /dev/null +++ b/Repair/RepairCompiler/MCC/Runtime/stack.h @@ -0,0 +1 @@ +void * GC_linux_stack_base(void); diff --git a/Repair/RepairCompiler/MCC/Runtime/tmap.cc b/Repair/RepairCompiler/MCC/Runtime/tmap.cc index 558e529..4d2e489 100755 --- a/Repair/RepairCompiler/MCC/Runtime/tmap.cc +++ b/Repair/RepairCompiler/MCC/Runtime/tmap.cc @@ -3,6 +3,7 @@ #include "size.h" extern "C" { #include "redblack.h" +#include "stack.h" } #define CHECKTYPE @@ -12,6 +13,7 @@ typemap::typemap(typeobject * size) { alloctree=rbinit(); typetree=rbinit(); this->size=size; + this->low=GC_linux_stack_base(); } void freefunction(void *ptr) { @@ -28,6 +30,18 @@ typemap::~typemap() { void typemap::reset() { rbdestroy(typetree,freefunction); typetree=rbinit(); + if (lowhigh=high; + if (lowsize(s); - int inbytes=toadd>>3; - if (toadd%8) - inbytes++; - return asserttype(ptr,((char *) ptr)+inbytes,s); + int toadd=size->sizeBytes(s); + return asserttype(ptr,((char *) ptr)+toadd,s); } bool typemap::asserttype(void *ptr, void *high, int s) { @@ -60,11 +71,8 @@ bool typemap::asserttype(void *ptr, void *high, int s) { } bool typemap::assertvalidmemory(void* low, int s) { - int toadd=size->size(s); - int inbytes=toadd>>3; - if (toadd%8) - inbytes++; - return assertvalidmemory(low,((char *)low)+inbytes); + int toadd=size->sizeBytes(s); + return assertvalidmemory(low,((char *)low)+toadd); } bool typemap::assertvalidmemory(void* low, void* high) { @@ -94,6 +102,13 @@ void typemap::allocate(void *ptr, int size) { printf("Error\n"); } +inline int sizeinbytes(unsigned int bits) { + int bytes=bits>>3; + if (bits %8) + bytes++; + return bytes; +} + int typemap::findoffsetstructure(int s, int offset) { int count=0; for(int i=0;igetnumfields(s);i++) { @@ -103,14 +118,20 @@ int typemap::findoffsetstructure(int s, int offset) { mult=size->numElements(s,i); } int increment=size->size(ttype); - int delt=offset-count; - if (deltoffset) return -1; } - count+=mult*increment; + count+=sizeinbytes(mult*increment); } return -1; } @@ -133,7 +154,7 @@ bool typemap::checkmemory(void* low, void* high) { bool typemap::checktype(bool doaction,void *ptr, int structure) { - int ssize=size->size(structure); + int ssize=size->sizeBytes(structure); void *low=ptr; void *high=((char *)low)+ssize; struct pair allocp=rbfind(low,high,alloctree); diff --git a/Repair/RepairCompiler/MCC/Runtime/tmap.h b/Repair/RepairCompiler/MCC/Runtime/tmap.h index 27249ca..85f8619 100755 --- a/Repair/RepairCompiler/MCC/Runtime/tmap.h +++ b/Repair/RepairCompiler/MCC/Runtime/tmap.h @@ -15,7 +15,10 @@ class typemap { bool istype(void *ptr, void *high, int structure); void reset(); typeobject *size; + void initializestack(void *high); private: + void *low; + void *high; bool checkmemory(void* low, void* high); bool checktype(bool doaction,void *ptr, int structure); bool checktype(bool doaction, void *low, void *high,int structure, struct rbtree *ttree); -- 2.34.1