Re: Virtual Machine implementation problem, Please help me to spot the bug



weidongtom@xxxxxxxxx <weidongtom@xxxxxxxxx> wrote:
I tried to implement the Universal Machine as described in
http://www.boundvariable.org/task.shtml, and I managed to get one
implemented (After looking at what other's have done.) But when I use
to run a UM program, I kept on getting error messages.

That's not a problem description. What kind of error messages?
Do you get them during compilation or at runtime? And what kind
of error messages?

I just had the usual look at places where malloc() etc. is used
and, voila, there are already things going horribly wrong.
Looking at more of the code before you correct that and tell
what kind of error messages you got doesn't make any sense.

/* Create an array with size size+1,
* where array[-1] is used for holding the size of the array
* return a 32 bit arrayID (currently implemented as it's address)
*/
u32 create_array(u32 size){
u32 *tmp = (u32*)my_malloc(sizeof(u32)*size+1);

You don't allocate enough memory here. You only allocate
memory for 'size' array elements plus a single byte. You
would need

u32 *tmp = my_malloc( ( size + 1 ) * sizeof *tmp );

memset(tmp+1, 0, sizeof(u32)*size);
*tmp = size;
return (u32)(tmp+1);
}

And then your function returns a pointer to an u32 cast
to an u32. But pointers can't be used interchangeably
with integers. You my get away with that on some 32-bit
platforms but it's not valid C. And it's unnecessary and
forces you to clutter your code with ugly casts.

#define GET_ARRAY_SIZE(arrayID) (*((u32*)(arrayID-1)))
#define ARRAY(arrayID) ((u32*)(arrayID))
#define WHOLE_ARRAY(arrayID) ((u32*)(arrayID)-1)

/* Delete an array created with create_array()*/
void delete_array(u32 arrayID){
free(WHOLE_ARRAY(arrayID));
}

Again conversions between pointer and integer types. That
function probably needs to be defined as

void delete_array( u32 *array ) {
free( array - 1 );
}

/* Duplicate the content in from to that of to.
* original data in to is discarded.
*/

Please: either use correct comments or delete them.
There is no 'from' nor 'to'. Wrong comments are worse
than none.

void copy2array0(UM32 *um, u32 src){
int size = (u32)GET_ARRAY_SIZE(src);
if(!(um->program))
free(um->program);

Wouldn't this have to be

delete_array( um->program );

Otherwise you might be trying to free() something that
you didn't get from malloc().

um->program = (u32*)my_malloc(sizeof(u32)*size+1);

And here's the same problem with allocating not enough
memory.

memcpy(um->program, WHOLE_ARRAY(src), size+1);

And here again with using integer and pointer types as if
they could be used interchangeably.

um->program++;
}

void load_program(u32** membuffer, const char* filename){
int i, size_read, file_size, membuffer_size;
struct stat file_info;
FILE *infile;

//Find file info
if(stat(filename, &file_info)){
fprintf(stderr, "Failed to stat file: %s. %s\n", filename,
strerror(errno));
exit(1);
}
file_size = file_info.st_size;

//Open file
dprintf("Opening file: %s ...", filename);
infile = fopen(filename, "rb");
if(infile == NULL){
fprintf(stderr, "Failed to open file: %s. %s\n", filename,
strerror(errno));
exit(1);
}
dprintf("done\n");

//Read buffer
dprintf("Reading file content with size: %d ...", file_size);
membuffer_size = sizeof(u8)*file_size/sizeof(u32);
if(!(*membuffer))
free((*membuffer));
//(*membuffer) = (u32*)my_malloc(sizeof(u32)*membuffer_size);

This rather likely will not allocate enough memory. Let's assume
that the file lenght is 11 and sizeof(u32) is 4. Then you end
up with a 'membuffer_size' of 2, i.e. 8 bytes.

(*membuffer) = (u32*)create_array(membuffer_size);
size_read = fread((*membuffer), sizeof(u8), file_size,infile);

And here you read 11 bytes even though your buffer only has 8 bytes.

fclose(infile);
if(size_read != file_size && !feof(infile)){

You can't use feof() on a FILE* you already closed.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: Is There Any Reason to Even Use VC++ Anymore?
    ... If, for another reason, the calling function needs to allocate memory, ... It does this by taking a pointer to a ball object ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointer to the out of scope local variables
    ... of a pointer, and jumped to the conclusion that you allocated the memory ... In this particular case the funtion buildPoint would allocate ... If the caller ...
    (microsoft.public.vc.language)
  • Re: Memory management and allocation
    ... > As I'm writing a piece of code that basically acts as a server and ... > memory management is a topic that is quite crucial. ... Or can I just allocate the variable ... Nor is it usually necessary to set the pointer to ...
    (comp.lang.c)
  • Re: This is getting really weird.
    ... I thought 4 bytes for reference count and 4 for string length. ... > There should be no memory allocation for that line. ... > manager may allocate more space than requested for its own efficiency. ... > that New returned with a pointer to the string constant. ...
    (alt.comp.lang.borland-delphi)
  • Re: trying to make my "fillvalues" function work...
    ... I assume ptd contains the result of a malloccall. ... Usually, it is a good idea to put error messages to stderr, i.e. ... less memory but more time consuming method). ... allocate memory for a FILE structure. ...
    (comp.lang.c)