Skip to content

Add pos field to InstRawRule forcustomizable injection position #324

@y1yang0

Description

@y1yang0

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    availableWelcome to claim it and implement iteffort:highHigh effort to accomplish it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions