1. Background
Currently, an InstRawRule is defined with the following structure:
raw_helloworld:
target: main
func: Example
raw: |
go func(){
h := sha256.New()
h.Write([]byte("RawCode"))
fmt.Printf("RawCode: %x\n", h.Sum(nil))
}()
imports:
fmt: "fmt"
sha256: "crypto/sha256"
This configuration injects the code snippet from the raw field at the very beginning (the entry point) of the target function, such as main.Example(). The injection position is not customizable.
2. Proposal
I propose introducing a new optional field named pos. This field will allow users to specify a precise injection point within the function body using a regular expression.
When the pos field is present, the instrumenter will search for the first line in the function that matches the regex and inject the code snippet before that line. If pos is omitted, the behavior defaults to injecting at the function's entry point, preserving backward compatibility.
3. Example Usage
Consider the following target function:
func Example(){
a()
b()
c()
}
To inject code before the call to b(), the configuration would be:
my_rule:
target: main
func: Example
pos: "b\\(\\)" # Inject before the line matching b()
raw: |
// ... your code snippet ...
Similarly, pos: "a\\(\\)" would inject the code before a().
1. Background
Currently, an
InstRawRuleis defined with the following structure:This configuration injects the code snippet from the
rawfield at the very beginning (the entry point) of the target function, such asmain.Example(). The injection position is not customizable.2. Proposal
I propose introducing a new optional field named
pos. This field will allow users to specify a precise injection point within the function body using a regular expression.When the
posfield is present, the instrumenter will search for the first line in the function that matches the regex and inject the code snippet before that line. Ifposis omitted, the behavior defaults to injecting at the function's entry point, preserving backward compatibility.3. Example Usage
Consider the following target function:
To inject code before the call to
b(), the configuration would be:Similarly,
pos: "a\\(\\)"would inject the code beforea().