Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic: x86_64: Runtime dynamic instrumentation #1698

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from

Conversation

clementguidi
Copy link
Contributor

Hello Namhyung and Honggyu,

This is a attempt to implement runtime dynamic instrumentation on x86_64 architecture. That is, performing full dynamic instrumentation on a live target.

This PR is long. If you want me to break it down, please advise me.

This feature, and technical details were already discussed in #1274 and #1369, so I'll be relying on this past material. Please ask me if you need me to elaborate on this PR.

It consists in 8 blocks of commits, as follows.

1. Refactor code for multiple runtime executions

The idea is that the agent will trigger the patching process at arbitrary moments, and an arbitrary amount of times. We refactor initialization and cleanup functions so they can be executed anytime at runtime, instead of when the target starts. We also make sure that they support multiple calls.

2. Add utils and wrappers

We introduce configuration options to use specific memory barriers when available. And also system call wrappers for older glibc versions. And last, we implement signal-related helpers.

3. Implement naive runtime dynamic patching

Core feature: we add safety mechanisms that allow to perform dynamic patching while code is executing. We make use of memory barriers or serializing instructions, and signals to ensure safe execution of cross-modifying code. This first implementation is based on the current patching process. We make heavy use of signals, and this has a strong negative impact on performance. We optimize the process later.

The patching process is seen as atomic regarding cores' execution. It follows these steps:

  1. Critical region locking: insert a trap a the start of the patching region so no new thread can enter it
  2. Serialization: synchronize execution across cores so each one is up to date on cross-modified instructions
  3. Thread eviction: clear the critical region of thread that might have entered it before the previous step
  4. Patching: move original code out of line at replace it with a relative call instruction (except the first byte, preserving the trap)
  5. Critical region unlocking: replace the trap with the first byte of the call instruction

4. Optimize signal use

We batch operations in a way to minimize the amount of signals that are emitted. After the patching regions are locked, we issue a single signal to clear them all from threads that could still execute them. Another signal is used for serialization when the required membarrier is unavailable. Then we patch the locked regions.

This approach uses only one (or two depending on the kernel version) signal, but increases the amount of time during which traps are installed and can be executed.

5. Implement optimized unpatching

We implement unpatching in a similar way that we did for unpatching, but instrumentation is replaced by original instructions.

6. Trigger (un)patching from the agent

We hook the relevant to the agent so it can perform runtime dynamic instrumentation when running.

7. Preserve original behavior

We make sure that our new process in only used when the target is execution, falling back to the original behavior of uftrace when the target is instrumented before being executed.

8. Display stats

We eventually reset and display dynamic statistics every time patching or unpatching occurs.

Based on: #1274
Related: #1369
See also: #870

clementguidi and others added 30 commits May 10, 2023 13:37
This is in preparation of runtime dynamic patching. This commit
guarantees that dynamic info is read only once for the target binary and
for each module.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
If 'mcount_dynamic_update' is called multiple times (e.g. at runtime),
it initializes the size filter only once.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Skip the initialization of the disassembly engine with it has already
been performed.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
The dynamic pattern list is not reused from a dynamic update to another,
keeping libmcount stateless in that regard.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
The 'mcount_dynamic_update' is now safe to call multiple times,
including at runtime. On each call, it will perform patching and
unpatching of the target (not implemented yet).

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Install a trampoline for each loaded module map, on initialization. Keep
the trampolines in memory to allow for dynamic patching at runtime.
Clear the trampolines on libmcount exit.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Skip the functions where uftrace already injected a call to a
trampoline.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Originally, when the user asks to only unpatch functions, uftrace would
patch all other functions by default. This is counter-intuitive,
especially when using the agent to unpatch at runtime. The user doesn't
expect functions to be patched when they only unpatch funtions.

This commit removes this behavior, and requires the user to explicitly
define functions to patch.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Libmcount would try to unpatch by default any function that is not
matched by the user patch or unpatch options.

We remove this implicit behavior, so the user explicitly choses which
function to unpatch.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Return whether a symbol is positively or negatively matched against a
pattern list, or not matched at all.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Runtime dynamic patching requires cache synchronization. This is best
achieved by using the 'MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE'
memory barrier. However it was introduced in Linux 4.16. We set a flag
indicating whether this membarrier can be used or if libmcount has to
rely on other mechanisms (e.g. signals).

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Glibc < 2.30 doen't provide wrappers for 'gettid()' and 'tgkill()' so we
define them.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Functions to setup real-time signals and broadcast signals to all
threads in an application. Useful for runtime synchronization
mechanisms.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Trigger architecture specific dynamic initialization when initializing
the dynamic instrumentation mechanics.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Refactor for more clarity.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Refactor 'patch_code' so it can later be used at runtime.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
When patching a function at runtime, first insert an int3 trap so any
incoming thread is diverted from the patching region, to avoid executing
partially modified code.

The trap handler emulates a call to the trampoline, thus enabling the
instrumentation.

The trap is eventually removed in subsequent commits.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
When cross-modifying code, the software needs to ensure that all cores
will execute valid instructions at any time.

When modifications are not atomic, we issue a specific memory
barrier (or execute a serializing instruction on Linux < 4.16) to
serialize the execution across all cores. This flushes the different
caches, especially the processor pipelines that may have partially
fetched straddling instructions.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
When patching at runtime, no thread can enter the patching region due to
the trap that is inserted at the start of it.

But threads that entered the region before the trap is installed can
still be executing instructions in the region.

We broadcast a real-time signal instruction all threads to check their
instruction pointer, and execute out-of-line if they are in the patching
region.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Add comments for the last steps of the runtime patching sequence.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Synchronization requires sending signals to threads inside the process.
This is performed for every symbols, which means a lot of signal can be
sent. This has a major performance impact. This commit batches the
initial and final steps of the patching process so we only need to send
one signal per thread for every batch.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Check if instruction at a given address is ENDBR64.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
This commit add for dynamically unpatching functions for the x86_64
architecture. The process is executed concurrently by replacing using a
trap to resolve race conditions between thread (similar to how optimized
kprobes are done).

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Synchronization requires sending signals to threads inside the process.
This is performed for every symbols, which means a lot of signal can be
sent. This has a major performance impact. This commit batches the
initial and final steps of the patching process so we only need to send
one signal per thread for every batch.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Use a global definition of the filter settings so the agent can modify
them easily.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Set the pattern type in the agent so it can interpret subsequent
options (e.g. filters, patch strings).

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Add support for the '--patch' and '--unpatch' options in the client.
When used, the agent patches or unpatches symbols on the fly.

Co-authored-by: Gabriel-Andrew Pollo-Guilbert <gabrielpolloguilbert@gmail.com>
Signed-off-by: Clément Guidi <cguidi@ciena.com>
Use a global flag to indicate the state of the target. When the target
is not running, tasks such as dynamic patching can be performed with
less constraints.

If libmcount.so is dynamically injected (not implemented yet), the
'mcount_target_running' flag indicates that libmcount has to be
initialized in a running target.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
When patching a binary before its execution, we can skip the
serialization and critical zone exclusion steps. These steps are only
use full when cross-modification occurs, at runtime.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
Print dynamic stats for patching and unpatching.

Signed-off-by: Clément Guidi <cguidi@ciena.com>
@honggyukim
Copy link
Collaborator

@clementguidi Thanks for your work as always. I couldn't look into the details yet, but I'm just overwhelmed by the amount of patches. Could you please break them into much smaller pieces?

@clementguidi clementguidi changed the title dynamic: Runtime dynamic instrumentation on x86_64 dynamic: x86_64: Runtime dynamic instrumentation May 11, 2023
@clementguidi
Copy link
Contributor Author

Sure. I believe it's a good thing to have an overview of the full feature set, because the first patches don't make much sense otherwise. Should I leave this PR open (maybe mark it as draft)? So we can refer to it when discussing design choices in the first patches that are useful for later patches.

@honggyukim
Copy link
Collaborator

Sure. If you think that’s more sensible then just leave it.

@namhyung
Copy link
Owner

I think I can take a look after finishing the agent changes and make a release.

@clementguidi
Copy link
Contributor Author

clementguidi commented May 12, 2023

Cool! I also want to let you know that I'm working on i386 full dynamic and runtime dynamic instrumentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants