<commit_before>#include <trap.h>
#include <sys/ptrace.h>
#include <unistd.h>
#include <stdio.h>

static const void *ignored_ptr;
static const void *no_continue_signal = 0;

static trap_breakpoint_callback_t g_callback;
static unsigned char g_original_breakpoint_byte;

void trap_breakpoint_set_callback(trap_breakpoint_callback_t callback)
{
  g_callback = callback;
}

trap_breakpoint_t trap_inferior_set_breakpoint(trap_inferior_t inferior,
                                               char *location)
{
  const void *target_address = (void *)0x000000000040079d;
  const void *int3_opcode = (void *)0xCC;
  pid_t inferior_pid = inferior;

  ptrace(PTRACE_PEEKTEXT, inferior_pid, target_address,
	 &g_original_breakpoint_byte);
  ptrace(PTRACE_POKETEXT, inferior_pid, target_address, int3_opcode);

  return 0;
}

void breakpoint_trigger_callback(trap_inferior_t inferior,
				 trap_breakpoint_t handle)
{
  (*g_callback)(inferior, handle);
}

void breakpoint_resolve(trap_inferior_t inferior)
{
  return 0;
}

void breakpoint_remove(trap_inferior_t inferior, trap_breakpoint_t handle)
{
  const void *target_address = (void *)0x000000000040079d;
  const void *int3_opcode = (void *)0xCC;
  pid_t inferior_pid = inferior;

  ptrace(PTRACE_POKETEXT, inferior_pid, target_address,
	 g_original_breakpoint_byte);
}<commit_msg>Use Read / Modify / Write when inserting INT3<commit_after>#include <trap.h>
#include <sys/ptrace.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>

static trap_breakpoint_callback_t g_callback;
static uintptr_t g_original_breakpoint_word;

void trap_breakpoint_set_callback(trap_breakpoint_callback_t callback)
{
  g_callback = callback;
}

trap_breakpoint_t trap_inferior_set_breakpoint(trap_inferior_t inferior,
                                               char *location)
{
  const uintptr_t target_address = (void *)0x000000000040079d;
  const uintptr_t int3_opcode = 0xCC;
  pid_t inferior_pid = inferior;
  uintptr_t modified_word;

  uintptr_t aligned_address = target_address & ~(0x7UL);
  uintptr_t target_offset = target_address - aligned_address;


  g_original_breakpoint_word = ptrace(PTRACE_PEEKTEXT, inferior_pid,
				      aligned_address, 0);
  printf("Original word: 0x%016lx\n", g_original_breakpoint_word);
  modified_word = g_original_breakpoint_word;
  modified_word &= ~(0xFFUL << (target_offset * 8));
  modified_word |= int3_opcode << (target_offset * 8);
  printf("Original word: 0x%016lx\n", modified_word);
  ptrace(PTRACE_POKETEXT, inferior_pid, aligned_address, modified_word);

  return 0;
}

void breakpoint_trigger_callback(trap_inferior_t inferior,
				 trap_breakpoint_t handle)
{
  (*g_callback)(inferior, handle);
}

trap_breakpoint_t breakpoint_resolve(trap_inferior_t inferior)
{
  return 0;
}

void breakpoint_remove(trap_inferior_t inferior, trap_breakpoint_t handle)
{
  const void *target_address = (void *)0x000000000040079d;
  pid_t inferior_pid = inferior;

  ptrace(PTRACE_POKETEXT, inferior_pid, target_address,
	 g_original_breakpoint_word);
}
{'fraction_non_alphanumeric': 0.08473458621826574, 'fraction_numerical': 0.030333003626772173, 'mean_word_length': 5.414376321353066, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '182921', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1049, 'n_words': 203}