1

I'm looking for a way to have an event get triggered and trapped when accessing a specific read-only section of a program's memory. Specifically, if I have a section of program memory that contains a number of constant values (read-only) and I want to trap an event on access to any of those values, where the event handler can perform some processing (some sort of augmentation to the data) and return the augmented value instead. so, I have a few questions:

  1. Is this possible?
  2. If so, how would one go about doing it?
  3. What kinds of privileges would be required (there is no need to access another processes memory)?
  4. Is there any documentation on techniques that can be used?

Update 1:

Here is a bit more information, but I'm still trying to figure out exactly the specifics.

What I'm trying to do is either programmatically or during compile/link time, mark a area of memory (probably a page or more) where, when accessed, an event will be triggered which will cause an event handler I wrote to be called and allow me to "fix" the issue that caused the event and return the value that was trying to be accessed. The goal is to be able to have code that is executing and when it tries to access the memory in question, the event handler will be called and can resolve the access and return back to the original code with the access corrected/fixed, so that the original code does not have to be modified. Say for a printf format string, where I want to be able to have the event called and the format string adjusted or replaced and the printf code continues on its merry way.

Does that make sense?

  • Debuggers such as gdb can access hardware registers to get control when regions of memory are accessed. Typically these regions are small, 4 or 8 bytes. sourceware.org/gdb/onlinedocs/gdb/Set-Watchpoints.html – icarus Jul 25 '19 at 12:55
  • 2
    You can use mprotect to mark a page or pages as unreadable. Accessing them will cause a segmentation violation signal, which you can catch and handle. – icarus Jul 25 '19 at 12:59
  • Yes debuggers do it. However I wonder if this is what will serve you best. Can you show a little more about what you are trying to do? It seems to me that you may be mid way into inventing polymorthism (Liskov substitution). – ctrl-alt-delor Jul 25 '19 at 14:15
1
  1. Yes

  2. Put the variables into a separate page of memory, probably by using a linker script. Use mprotect to mark the page as no access. Use sigaction to set up a handler for SIGSEGV, specify SA_SIGINFO so you get passed the info about what address is involved.

  3. No special privileges are needed.

  4. This has been done many times. The original Bourne shell caught SIGSEGV to do its memory handling, but that was before the days of sigaction. In your handler you can do whatever is needed to fix things up and return. Typically this requires a reasonable amount of knowledge of the access patterns and the generated machine code.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.