instruction.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package asm
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "io"
  6. "math"
  7. "strings"
  8. "github.com/pkg/errors"
  9. )
  10. // InstructionSize is the size of a BPF instruction in bytes
  11. const InstructionSize = 8
  12. // Instruction is a single eBPF instruction.
  13. type Instruction struct {
  14. OpCode OpCode
  15. Dst Register
  16. Src Register
  17. Offset int16
  18. Constant int64
  19. Reference string
  20. Symbol string
  21. }
  22. // Sym creates a symbol.
  23. func (ins Instruction) Sym(name string) Instruction {
  24. ins.Symbol = name
  25. return ins
  26. }
  27. // Unmarshal decodes a BPF instruction.
  28. func (ins *Instruction) Unmarshal(r io.Reader, bo binary.ByteOrder) (uint64, error) {
  29. var bi bpfInstruction
  30. err := binary.Read(r, bo, &bi)
  31. if err != nil {
  32. return 0, err
  33. }
  34. ins.OpCode = bi.OpCode
  35. ins.Dst = bi.Registers.Dst()
  36. ins.Src = bi.Registers.Src()
  37. ins.Offset = bi.Offset
  38. ins.Constant = int64(bi.Constant)
  39. if !bi.OpCode.isDWordLoad() {
  40. return InstructionSize, nil
  41. }
  42. var bi2 bpfInstruction
  43. if err := binary.Read(r, bo, &bi2); err != nil {
  44. // No Wrap, to avoid io.EOF clash
  45. return 0, errors.New("64bit immediate is missing second half")
  46. }
  47. if bi2.OpCode != 0 || bi2.Offset != 0 || bi2.Registers != 0 {
  48. return 0, errors.New("64bit immediate has non-zero fields")
  49. }
  50. ins.Constant = int64(uint64(uint32(bi2.Constant))<<32 | uint64(uint32(bi.Constant)))
  51. return 2 * InstructionSize, nil
  52. }
  53. // Marshal encodes a BPF instruction.
  54. func (ins Instruction) Marshal(w io.Writer, bo binary.ByteOrder) (uint64, error) {
  55. if ins.OpCode == InvalidOpCode {
  56. return 0, errors.New("invalid opcode")
  57. }
  58. isDWordLoad := ins.OpCode.isDWordLoad()
  59. cons := int32(ins.Constant)
  60. if isDWordLoad {
  61. // Encode least significant 32bit first for 64bit operations.
  62. cons = int32(uint32(ins.Constant))
  63. }
  64. bpfi := bpfInstruction{
  65. ins.OpCode,
  66. newBPFRegisters(ins.Dst, ins.Src),
  67. ins.Offset,
  68. cons,
  69. }
  70. if err := binary.Write(w, bo, &bpfi); err != nil {
  71. return 0, err
  72. }
  73. if !isDWordLoad {
  74. return InstructionSize, nil
  75. }
  76. bpfi = bpfInstruction{
  77. Constant: int32(ins.Constant >> 32),
  78. }
  79. if err := binary.Write(w, bo, &bpfi); err != nil {
  80. return 0, err
  81. }
  82. return 2 * InstructionSize, nil
  83. }
  84. // RewriteMapPtr changes an instruction to use a new map fd.
  85. //
  86. // Returns an error if the fd is invalid, or the instruction
  87. // is incorrect.
  88. func (ins *Instruction) RewriteMapPtr(fd int) error {
  89. if !ins.OpCode.isDWordLoad() {
  90. return errors.Errorf("%s is not a 64 bit load", ins.OpCode)
  91. }
  92. if fd < 0 {
  93. return errors.New("invalid fd")
  94. }
  95. ins.Src = R1
  96. ins.Constant = int64(fd)
  97. return nil
  98. }
  99. // Format implements fmt.Formatter.
  100. func (ins Instruction) Format(f fmt.State, c rune) {
  101. if c != 'v' {
  102. fmt.Fprintf(f, "{UNRECOGNIZED: %c}", c)
  103. return
  104. }
  105. op := ins.OpCode
  106. if op == InvalidOpCode {
  107. fmt.Fprint(f, "INVALID")
  108. return
  109. }
  110. // Omit trailing space for Exit
  111. if op.JumpOp() == Exit {
  112. fmt.Fprint(f, op)
  113. return
  114. }
  115. fmt.Fprintf(f, "%v ", op)
  116. switch cls := op.Class(); cls {
  117. case LdClass, LdXClass, StClass, StXClass:
  118. switch op.Mode() {
  119. case ImmMode:
  120. fmt.Fprintf(f, "dst: %s imm: %d", ins.Dst, ins.Constant)
  121. case AbsMode:
  122. fmt.Fprintf(f, "imm: %d", ins.Constant)
  123. case IndMode:
  124. fmt.Fprintf(f, "dst: %s src: %s imm: %d", ins.Dst, ins.Src, ins.Constant)
  125. case MemMode:
  126. fmt.Fprintf(f, "dst: %s src: %s off: %d imm: %d", ins.Dst, ins.Src, ins.Offset, ins.Constant)
  127. case XAddMode:
  128. fmt.Fprintf(f, "dst: %s src: %s", ins.Dst, ins.Src)
  129. }
  130. case ALU64Class, ALUClass:
  131. fmt.Fprintf(f, "dst: %s ", ins.Dst)
  132. if op.ALUOp() == Swap || op.Source() == ImmSource {
  133. fmt.Fprintf(f, "imm: %d", ins.Constant)
  134. } else {
  135. fmt.Fprintf(f, "src: %s", ins.Src)
  136. }
  137. case JumpClass:
  138. switch jop := op.JumpOp(); jop {
  139. case Call:
  140. if ins.Src == R1 {
  141. // bpf-to-bpf call
  142. fmt.Fprint(f, ins.Constant)
  143. } else {
  144. fmt.Fprint(f, BuiltinFunc(ins.Constant))
  145. }
  146. default:
  147. fmt.Fprintf(f, "dst: %s off: %d ", ins.Dst, ins.Offset)
  148. if op.Source() == ImmSource {
  149. fmt.Fprintf(f, "imm: %d", ins.Constant)
  150. } else {
  151. fmt.Fprintf(f, "src: %s", ins.Src)
  152. }
  153. }
  154. }
  155. if ins.Reference != "" {
  156. fmt.Fprintf(f, " <%s>", ins.Reference)
  157. }
  158. }
  159. // Instructions is an eBPF program.
  160. type Instructions []Instruction
  161. func (insns Instructions) String() string {
  162. return fmt.Sprint(insns)
  163. }
  164. // RewriteMapPtr rewrites all loads of a specific map pointer to a new fd.
  165. //
  166. // Returns an error if the symbol isn't used, see IsUnreferencedSymbol.
  167. func (insns Instructions) RewriteMapPtr(symbol string, fd int) error {
  168. if symbol == "" {
  169. return errors.New("empty symbol")
  170. }
  171. found := false
  172. for i := range insns {
  173. ins := &insns[i]
  174. if ins.Reference != symbol {
  175. continue
  176. }
  177. if err := ins.RewriteMapPtr(fd); err != nil {
  178. return err
  179. }
  180. found = true
  181. }
  182. if !found {
  183. return &unreferencedSymbolError{symbol}
  184. }
  185. return nil
  186. }
  187. // SymbolOffsets returns the set of symbols and their offset in
  188. // the instructions.
  189. func (insns Instructions) SymbolOffsets() (map[string]int, error) {
  190. offsets := make(map[string]int)
  191. for i, ins := range insns {
  192. if ins.Symbol == "" {
  193. continue
  194. }
  195. if _, ok := offsets[ins.Symbol]; ok {
  196. return nil, errors.Errorf("duplicate symbol %s", ins.Symbol)
  197. }
  198. offsets[ins.Symbol] = i
  199. }
  200. return offsets, nil
  201. }
  202. // ReferenceOffsets returns the set of references and their offset in
  203. // the instructions.
  204. func (insns Instructions) ReferenceOffsets() map[string][]int {
  205. offsets := make(map[string][]int)
  206. for i, ins := range insns {
  207. if ins.Reference == "" {
  208. continue
  209. }
  210. offsets[ins.Reference] = append(offsets[ins.Reference], i)
  211. }
  212. return offsets
  213. }
  214. func (insns Instructions) marshalledOffsets() (map[string]int, error) {
  215. symbols := make(map[string]int)
  216. marshalledPos := 0
  217. for _, ins := range insns {
  218. currentPos := marshalledPos
  219. marshalledPos += ins.OpCode.marshalledInstructions()
  220. if ins.Symbol == "" {
  221. continue
  222. }
  223. if _, ok := symbols[ins.Symbol]; ok {
  224. return nil, errors.Errorf("duplicate symbol %s", ins.Symbol)
  225. }
  226. symbols[ins.Symbol] = currentPos
  227. }
  228. return symbols, nil
  229. }
  230. // Format implements fmt.Formatter.
  231. //
  232. // You can control indentation of symbols by
  233. // specifying a width. Setting a precision controls the indentation of
  234. // instructions.
  235. // The default character is a tab, which can be overriden by specifying
  236. // the ' ' space flag.
  237. func (insns Instructions) Format(f fmt.State, c rune) {
  238. if c != 's' && c != 'v' {
  239. fmt.Fprintf(f, "{UNKNOWN FORMAT '%c'}", c)
  240. return
  241. }
  242. // Precision is better in this case, because it allows
  243. // specifying 0 padding easily.
  244. padding, ok := f.Precision()
  245. if !ok {
  246. padding = 1
  247. }
  248. indent := strings.Repeat("\t", padding)
  249. if f.Flag(' ') {
  250. indent = strings.Repeat(" ", padding)
  251. }
  252. symPadding, ok := f.Width()
  253. if !ok {
  254. symPadding = padding - 1
  255. }
  256. if symPadding < 0 {
  257. symPadding = 0
  258. }
  259. symIndent := strings.Repeat("\t", symPadding)
  260. if f.Flag(' ') {
  261. symIndent = strings.Repeat(" ", symPadding)
  262. }
  263. // Figure out how many digits we need to represent the highest
  264. // offset.
  265. highestOffset := 0
  266. for _, ins := range insns {
  267. highestOffset += ins.OpCode.marshalledInstructions()
  268. }
  269. offsetWidth := int(math.Ceil(math.Log10(float64(highestOffset))))
  270. offset := 0
  271. for _, ins := range insns {
  272. if ins.Symbol != "" {
  273. fmt.Fprintf(f, "%s%s:\n", symIndent, ins.Symbol)
  274. }
  275. fmt.Fprintf(f, "%s%*d: %v\n", indent, offsetWidth, offset, ins)
  276. offset += ins.OpCode.marshalledInstructions()
  277. }
  278. return
  279. }
  280. // Marshal encodes a BPF program into the kernel format.
  281. func (insns Instructions) Marshal(w io.Writer, bo binary.ByteOrder) error {
  282. absoluteOffsets, err := insns.marshalledOffsets()
  283. if err != nil {
  284. return err
  285. }
  286. num := 0
  287. for i, ins := range insns {
  288. switch {
  289. case ins.OpCode.JumpOp() == Call && ins.Constant == -1:
  290. // Rewrite bpf to bpf call
  291. offset, ok := absoluteOffsets[ins.Reference]
  292. if !ok {
  293. return errors.Errorf("instruction %d: reference to missing symbol %s", i, ins.Reference)
  294. }
  295. ins.Constant = int64(offset - num - 1)
  296. case ins.OpCode.Class() == JumpClass && ins.Offset == -1:
  297. // Rewrite jump to label
  298. offset, ok := absoluteOffsets[ins.Reference]
  299. if !ok {
  300. return errors.Errorf("instruction %d: reference to missing symbol %s", i, ins.Reference)
  301. }
  302. ins.Offset = int16(offset - num - 1)
  303. }
  304. n, err := ins.Marshal(w, bo)
  305. if err != nil {
  306. return errors.Wrapf(err, "instruction %d", i)
  307. }
  308. num += int(n / InstructionSize)
  309. }
  310. return nil
  311. }
  312. type bpfInstruction struct {
  313. OpCode OpCode
  314. Registers bpfRegisters
  315. Offset int16
  316. Constant int32
  317. }
  318. type bpfRegisters uint8
  319. func newBPFRegisters(dst, src Register) bpfRegisters {
  320. return bpfRegisters((src << 4) | (dst & 0xF))
  321. }
  322. func (r bpfRegisters) Dst() Register {
  323. return Register(r & 0xF)
  324. }
  325. func (r bpfRegisters) Src() Register {
  326. return Register(r >> 4)
  327. }
  328. type unreferencedSymbolError struct {
  329. symbol string
  330. }
  331. func (use *unreferencedSymbolError) Error() string {
  332. return fmt.Sprintf("unreferenced symbol %s", use.symbol)
  333. }
  334. // IsUnreferencedSymbol returns true if err was caused by
  335. // an unreferenced symbol.
  336. func IsUnreferencedSymbol(err error) bool {
  337. _, ok := err.(*unreferencedSymbolError)
  338. return ok
  339. }