<commit_before>#include "dr_api.h"
#include "dr_ir_opnd.h"
#include "drmgr.h"
#include "../includes/utils.h"
#include "../includes/block_utils.h"
#include "../includes/allocs.h"
#include "../includes/call.h"
#include "../includes/sym.h"

void	incr_orig(access_t *access, size_t size, void *pc, void *drcontext)
{
  orig_t	*tmp_orig = access->origs;
  orig_t	*orig = NULL;
  stack_t	*stack;

  while (tmp_orig)
    {
      if (tmp_orig->size == size && tmp_orig->addr == pc)
	{
	  orig = tmp_orig;
	  break;
	}
      tmp_orig  = tmp_orig->next;
    }

  // if a similar access with the actual is not found we create it
  if (!orig)
    {
      if (!(orig = dr_global_alloc(sizeof(*orig))))
	dr_printf("dr_malloc fail\n");
      else
	{
	  orig->size = size;
	  orig->nb_hit = 0;
	  orig->addr = pc;
	  orig->next = access->origs;
	  access->origs = orig;
	  // get the start value of the function doing the access
	  stack = drmgr_get_tls_field(drcontext, tls_stack_idx);
	  orig->start_func_addr = stack->addr;
	  orig->start_func_sym = hashtable_lookup(sym_hashtab, stack->addr);
	}
    }
  if (orig)
    orig->nb_hit++;
}

void	add_hit(void *pc, size_t size, void *target, int read, void *drcontext)
{
  malloc_t	*block = get_active_block_by_access(target);
  access_t	*access;

  // if the access is not on a malloc block we do nothing
  if (!block)
    return;

  dr_mutex_lock(lock);

  if (read)
    access = get_access(target - block->start, &(block->read));
  else
    access = get_access(target - block->start, &(block->write));

  access->total_hits++;
  incr_orig(access, size, pc, drcontext);

  dr_mutex_unlock(lock);  
}

void	memory_read(void *pc)
{
  void		*drcontext = dr_get_current_drcontext();
  instr_t	*instr = instr_create(drcontext);
  dr_mcontext_t mctx;
  opnd_t	src;

  pc = dr_app_pc_for_decoding(pc);

  mctx.flags = DR_MC_CONTROL|DR_MC_INTEGER;
  mctx.size = sizeof(mctx);
  dr_get_mcontext(drcontext, &mctx);

  instr_init(drcontext, instr);
  if (!decode(drcontext, pc, instr))
    {
      dr_printf("Decode of instruction at %p failed\n", pc);
      return;
    }

  for (int i = 0; i < instr_num_srcs(instr); i++)
    {
      src = instr_get_src(instr, i);
      if (opnd_is_memory_reference(src))
	// todo : take a look at other type of memory ref to be 
	// sure we don't miss any ref to the heap
	if (opnd_is_base_disp(src))
	  add_hit(pc, opnd_size_in_bytes(opnd_get_size(src)), 
		  opnd_get_disp(src) + (void *)reg_get_value(opnd_get_base(src),
							     &mctx),
		  1, drcontext);
    }
  
  instr_destroy(drcontext, instr);
}

void	memory_write(void *pc)
{
  void		*drcontext = dr_get_current_drcontext();
  instr_t	*instr = instr_create(drcontext);
  dr_mcontext_t mctx;
  opnd_t	dst;

  pc = dr_app_pc_for_decoding(pc);
  
  mctx.flags = DR_MC_CONTROL|DR_MC_INTEGER;
  mctx.size = sizeof(mctx);
  dr_get_mcontext(drcontext, &mctx);

  instr_init(drcontext, instr);
  if(!decode(drcontext, pc, instr))
    {
      dr_printf("Decode of instruction at %p failed\n", pc);
      return;
    }

  for (int i = 0; i < instr_num_dsts(instr); i++)
    {
      dst = instr_get_dst(instr, i);
      if (opnd_is_memory_reference(dst))
	// todo : take a look at other type of memory ref to be
	// sure we don't miss any ref to the heap
	if (opnd_is_base_disp(dst))
	  add_hit(pc, opnd_size_in_bytes(opnd_get_size(dst)), 
		  opnd_get_disp(dst) + (void *)reg_get_value(opnd_get_base(dst),
							     &mctx),
		  0, drcontext);

    }

  instr_destroy(drcontext, instr);
}<commit_msg>remove useless test<commit_after>#include "dr_api.h"
#include "dr_ir_opnd.h"
#include "drmgr.h"
#include "../includes/utils.h"
#include "../includes/block_utils.h"
#include "../includes/allocs.h"
#include "../includes/call.h"
#include "../includes/sym.h"

void	incr_orig(access_t *access, size_t size, void *pc, void *drcontext)
{
  orig_t	*tmp_orig = access->origs;
  orig_t	*orig = NULL;
  stack_t	*stack;

  while (tmp_orig)
    {
      if (tmp_orig->size == size && tmp_orig->addr == pc)
	{
	  orig = tmp_orig;
	  break;
	}
      tmp_orig  = tmp_orig->next;
    }

  // if a similar access with the actual is not found we create it
  if (!orig)
    {
      if (!(orig = dr_global_alloc(sizeof(*orig))))
	dr_printf("dr_malloc fail\n");
      else
	{
	  orig->size = size;
	  orig->nb_hit = 0;
	  orig->addr = pc;
	  orig->next = access->origs;
	  access->origs = orig;
	  // get the start value of the function doing the access
	  stack = drmgr_get_tls_field(drcontext, tls_stack_idx);
	  orig->start_func_addr = stack->addr;
	  orig->start_func_sym = hashtable_lookup(sym_hashtab, stack->addr);
	}
    }
  if (orig)
    orig->nb_hit++;
}

void	add_hit(void *pc, size_t size, void *target, int read, void *drcontext)
{
  malloc_t	*block = get_active_block_by_access(target);
  access_t	*access;

  // if the access is not on a malloc block we do nothing
  if (!block)
    return;

  dr_mutex_lock(lock);

  if (read)
    access = get_access(target - block->start, &(block->read));
  else
    access = get_access(target - block->start, &(block->write));

  access->total_hits++;
  incr_orig(access, size, pc, drcontext);

  dr_mutex_unlock(lock);  
}

void	memory_read(void *pc)
{
  void		*drcontext = dr_get_current_drcontext();
  instr_t	*instr = instr_create(drcontext);
  dr_mcontext_t mctx;
  opnd_t	src;

  pc = dr_app_pc_for_decoding(pc);

  mctx.flags = DR_MC_CONTROL|DR_MC_INTEGER;
  mctx.size = sizeof(mctx);
  dr_get_mcontext(drcontext, &mctx);

  instr_init(drcontext, instr);
  if (!decode(drcontext, pc, instr))
    {
      dr_printf("Decode of instruction at %p failed\n", pc);
      return;
    }

  for (int i = 0; i < instr_num_srcs(instr); i++)
    {
      src = instr_get_src(instr, i);
      if (opnd_is_memory_reference(src))
	add_hit(pc, opnd_size_in_bytes(opnd_get_size(src)), 
		opnd_get_disp(src) + (void *)reg_get_value(opnd_get_base(src),
							   &mctx),
		1, drcontext);
    }
  
  instr_destroy(drcontext, instr);
}

void	memory_write(void *pc)
{
  void		*drcontext = dr_get_current_drcontext();
  instr_t	*instr = instr_create(drcontext);
  dr_mcontext_t mctx;
  opnd_t	dst;

  pc = dr_app_pc_for_decoding(pc);
  
  mctx.flags = DR_MC_CONTROL|DR_MC_INTEGER;
  mctx.size = sizeof(mctx);
  dr_get_mcontext(drcontext, &mctx);

  instr_init(drcontext, instr);
  if(!decode(drcontext, pc, instr))
    {
      dr_printf("Decode of instruction at %p failed\n", pc);
      return;
    }

  for (int i = 0; i < instr_num_dsts(instr); i++)
    {
      dst = instr_get_dst(instr, i);
      if (opnd_is_memory_reference(dst))
	add_hit(pc, opnd_size_in_bytes(opnd_get_size(dst)), 
		opnd_get_disp(dst) + (void *)reg_get_value(opnd_get_base(dst),
							   &mctx),
		0, drcontext);
      
    }

  instr_destroy(drcontext, instr);
}
{'fraction_non_alphanumeric': 0.1376865120401832, 'fraction_numerical': 0.0014773230905599055, 'mean_word_length': 3.5013297872340425, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2902379', 'n_tokens_mistral': 2729, 'n_tokens_neox': 2617, 'n_words': 566}