Ejemplo n.º 1
0
mem          *
count_find_and_extract (const char *fname, int line, void *ptr, int type)
{
	mem          *m = NULL;
	if( allocs_hash && ptr )
	{
		ASHashData hd ;
		service_mode++ ;
		if( remove_hash_item (allocs_hash, AS_HASHABLE(ptr), &hd.vptr, False) == ASH_Success )
		{
			m = hd.vptr ;
//			if( allocs_hash->items_num <= 0 )
//				destroy_ashash(&allocs_hash);
			if( (m->type & 0xff) != (type & 0xff) && (m->type & 0xff) != C_IMAGE )
			{
            	show_error( "while deallocating pointer %p discovered that it was allocated with different type\n   Called from %s:%d", ptr, fname, line );
#ifdef DEBUG_ALLOC_STRICT
{	char *segv = NULL ;	*segv = 0 ;  }
#endif
			}
			if( total_service < sizeof(ASHashItem) )
			{
            	show_error( "it seems that we have too little auditing memory (%lu) while deallocating pointer %p.\n   Called from %s:%d", total_service, ptr, fname, line );
#ifdef DEBUG_ALLOC_STRICT
{	char *segv = NULL ;	*segv = 0 ;  }
#endif
      		}else
		        total_service -= sizeof(ASHashItem);
        }
        service_mode-- ;
	}
	/* fprintf( stderr, "looking for ptr %p, produced result %p at %s:%d", ptr, m, fname, line ); */
	if( m )
	{
		if ((m->type & 0xff) == C_MEM)
			total_alloc -= m->length;
		else
			total_x_alloc -= m->length;
		deallocations++;
	}
	return m;
}
Ejemplo n.º 2
0
void         *
countrealloc (const char *fname, int line, void *ptr, size_t length)
{
	if (ptr != NULL && length == 0)
		countfree (fname, line, ptr);
	if (length == 0)
		return NULL;
	if (ptr != NULL)
	{
		mem          *m = NULL;
		ASHashResult  res ;

		if( allocs_hash != NULL )
		{
			ASHashData hd ;
			service_mode++ ;
			if( remove_hash_item (allocs_hash, AS_HASHABLE(ptr), &hd.vptr, False) == ASH_Success )
			{
				m = hd.vptr ;	  
				if( (m->type & 0xff) != C_MEM )
				{
					show_error( "while deallocating pointer 0x%lX discovered that it was allocated with different type", ptr );
					print_simple_backtrace();
#ifdef DEBUG_ALLOC_STRICT
{	char *segv = NULL ;	*segv = 0 ;  }
#endif
					m = NULL ;
				}
			}
			service_mode-- ;
		}
		if (m == NULL)
		{
			show_error ("countrealloc:attempt in %s:%d to realloc memory(%p) that was never allocated!\n",
					     fname, line, ptr);
			print_simple_backtrace();
#ifdef DEBUG_ALLOC_STRICT
{	char *segv = NULL ;	*segv = 0 ;  }
#endif
			return NULL;
		}
		if ((m->type & 0xff) == C_MEM)
		{
			total_alloc -= m->length;
			total_alloc += length;
			if (total_alloc > max_alloc)
				max_alloc = total_alloc;
		} else
		{
			total_x_alloc -= m->length;
			total_x_alloc += length;
			if (total_x_alloc > max_x_alloc)
				max_x_alloc = total_x_alloc;
		}
		m->fname = fname;
		m->line = line;
		m->length = length;
		m->type = C_MEM | C_REALLOC;
		m->ptr = saferealloc (ptr, length);
		m->freed = 0;
		ptr = m->ptr;
		if( (res = add_hash_item( allocs_hash, (ASHashableValue)ptr, m )) != ASH_Success )
		{
			show_error( "failed to log allocation for pointer 0x%lX - result = %d", ptr, res);
#ifdef DEBUG_ALLOC_STRICT
{	char *segv = NULL ;	*segv = 0 ;  }
#endif
		}
  		reallocations++;
	} else
		ptr = countmalloc (fname, line, length);

	return ptr;
}
Ejemplo n.º 3
0
int
filehash_get(const unsigned char *path, unsigned char *val)
{
  unsigned long p_hash;
  unsigned int idx, i, min_i;
  struct hash_entry *p, *q;
  FILE *f = 0;
  unsigned min_tick;

  ASSERT(path);
  p_hash = get_hash(path);

  idx = p_hash % HASH_SIZE;
  while (hash_table[idx] && hash_table[idx]->path_hash == p_hash
         && strcmp(hash_table[idx]->path, path) != 0) {
    idx = (idx + HASH_STEP) % HASH_SIZE;
  }
  if (hash_table[idx] && hash_table[idx]->path_hash == p_hash) {
    // hit!
    if (!file_stamp_is_updated(path, hash_table[idx]->stamp)) {
      info("entry <%s> is in hash table and is not changed", path);
      memcpy(val, hash_table[idx]->sha1_hash, SHA1_SIZE);
      hash_table[idx]->tick = cur_tick++;
      return 0;
    }
    // update the hash code, maybe removing an item
    info("entry <%s> is in hash table and is CHANGED!", path);
    hash_table[idx]->stamp = file_stamp_update(path, hash_table[idx]->stamp);
    if (!hash_table[idx]->stamp || !(f = fopen(path, "rb"))
        || sha_stream(f, hash_table[idx]->sha1_hash)) {
      // file no longer exists or I/O error
      if (f) fclose(f);
      p = remove_hash_item(idx);
      free_hash_item(p);
      hash_use--;
      return -1;
    }
    // recalculate the hash
    fclose(f);
    memcpy(val, hash_table[idx]->sha1_hash, SHA1_SIZE);
    hash_table[idx]->tick = cur_tick++;
    return 0;
  }

  // no entry in the hash table
  XCALLOC(p, 1);
  if (!(p->stamp = file_stamp_get(path))
      || !(f = fopen(path, "rb"))
      || sha_stream(f, p->sha1_hash)) {
    if (f) fclose(f);
    free_hash_item(p);
    return -1;
  }
  fclose(f);
  p->path_hash = p_hash;
  p->path = xstrdup(path);
  p->tick = cur_tick++;
  memcpy(val, p->sha1_hash, SHA1_SIZE);
  if (hash_use < HASH_CAP) {
    info("entry <%s> is not in the hash table - adding", path);
    add_hash_item(p);
    hash_use++;
    return 0;
  }

  // find the least recently used entry and remove it
  info("entry <%s> is not in the hash table - REPLACING", path);
  min_i = -1;
  min_tick = cur_tick;
  for (i = 0; i < HASH_SIZE; i++)
    if (hash_table[i] && hash_table[i]->tick < min_tick) {
      min_i = i;
      min_tick = hash_table[i]->tick;
    }
  ASSERT(min_i >= 0);
  q = remove_hash_item(min_i);
  free_hash_item(q);
  add_hash_item(p);
  return 0;
}