instruction.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. package asm
  2. import (
  3. "crypto/sha1"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "math"
  10. "sort"
  11. "strings"
  12. "github.com/cilium/ebpf/internal/sys"
  13. "github.com/cilium/ebpf/internal/unix"
  14. )
  15. // InstructionSize is the size of a BPF instruction in bytes
  16. const InstructionSize = 8
  17. // RawInstructionOffset is an offset in units of raw BPF instructions.
  18. type RawInstructionOffset uint64
  19. var ErrUnreferencedSymbol = errors.New("unreferenced symbol")
  20. var ErrUnsatisfiedMapReference = errors.New("unsatisfied map reference")
  21. var ErrUnsatisfiedProgramReference = errors.New("unsatisfied program reference")
  22. // Bytes returns the offset of an instruction in bytes.
  23. func (rio RawInstructionOffset) Bytes() uint64 {
  24. return uint64(rio) * InstructionSize
  25. }
  26. // Instruction is a single eBPF instruction.
  27. type Instruction struct {
  28. OpCode OpCode
  29. Dst Register
  30. Src Register
  31. Offset int16
  32. Constant int64
  33. // Metadata contains optional metadata about this instruction.
  34. Metadata Metadata
  35. }
  36. // Unmarshal decodes a BPF instruction.
  37. func (ins *Instruction) Unmarshal(r io.Reader, bo binary.ByteOrder) (uint64, error) {
  38. data := make([]byte, InstructionSize)
  39. if _, err := io.ReadFull(r, data); err != nil {
  40. return 0, err
  41. }
  42. ins.OpCode = OpCode(data[0])
  43. regs := data[1]
  44. switch bo {
  45. case binary.LittleEndian:
  46. ins.Dst, ins.Src = Register(regs&0xF), Register(regs>>4)
  47. case binary.BigEndian:
  48. ins.Dst, ins.Src = Register(regs>>4), Register(regs&0xf)
  49. }
  50. ins.Offset = int16(bo.Uint16(data[2:4]))
  51. // Convert to int32 before widening to int64
  52. // to ensure the signed bit is carried over.
  53. ins.Constant = int64(int32(bo.Uint32(data[4:8])))
  54. if !ins.OpCode.IsDWordLoad() {
  55. return InstructionSize, nil
  56. }
  57. // Pull another instruction from the stream to retrieve the second
  58. // half of the 64-bit immediate value.
  59. if _, err := io.ReadFull(r, data); err != nil {
  60. // No Wrap, to avoid io.EOF clash
  61. return 0, errors.New("64bit immediate is missing second half")
  62. }
  63. // Require that all fields other than the value are zero.
  64. if bo.Uint32(data[0:4]) != 0 {
  65. return 0, errors.New("64bit immediate has non-zero fields")
  66. }
  67. cons1 := uint32(ins.Constant)
  68. cons2 := int32(bo.Uint32(data[4:8]))
  69. ins.Constant = int64(cons2)<<32 | int64(cons1)
  70. return 2 * InstructionSize, nil
  71. }
  72. // Marshal encodes a BPF instruction.
  73. func (ins Instruction) Marshal(w io.Writer, bo binary.ByteOrder) (uint64, error) {
  74. if ins.OpCode == InvalidOpCode {
  75. return 0, errors.New("invalid opcode")
  76. }
  77. isDWordLoad := ins.OpCode.IsDWordLoad()
  78. cons := int32(ins.Constant)
  79. if isDWordLoad {
  80. // Encode least significant 32bit first for 64bit operations.
  81. cons = int32(uint32(ins.Constant))
  82. }
  83. regs, err := newBPFRegisters(ins.Dst, ins.Src, bo)
  84. if err != nil {
  85. return 0, fmt.Errorf("can't marshal registers: %s", err)
  86. }
  87. data := make([]byte, InstructionSize)
  88. data[0] = byte(ins.OpCode)
  89. data[1] = byte(regs)
  90. bo.PutUint16(data[2:4], uint16(ins.Offset))
  91. bo.PutUint32(data[4:8], uint32(cons))
  92. if _, err := w.Write(data); err != nil {
  93. return 0, err
  94. }
  95. if !isDWordLoad {
  96. return InstructionSize, nil
  97. }
  98. // The first half of the second part of a double-wide instruction
  99. // must be zero. The second half carries the value.
  100. bo.PutUint32(data[0:4], 0)
  101. bo.PutUint32(data[4:8], uint32(ins.Constant>>32))
  102. if _, err := w.Write(data); err != nil {
  103. return 0, err
  104. }
  105. return 2 * InstructionSize, nil
  106. }
  107. // AssociateMap associates a Map with this Instruction.
  108. //
  109. // Implicitly clears the Instruction's Reference field.
  110. //
  111. // Returns an error if the Instruction is not a map load.
  112. func (ins *Instruction) AssociateMap(m FDer) error {
  113. if !ins.IsLoadFromMap() {
  114. return errors.New("not a load from a map")
  115. }
  116. ins.Metadata.Set(referenceMeta{}, nil)
  117. ins.Metadata.Set(mapMeta{}, m)
  118. return nil
  119. }
  120. // RewriteMapPtr changes an instruction to use a new map fd.
  121. //
  122. // Returns an error if the instruction doesn't load a map.
  123. //
  124. // Deprecated: use AssociateMap instead. If you cannot provide a Map,
  125. // wrap an fd in a type implementing FDer.
  126. func (ins *Instruction) RewriteMapPtr(fd int) error {
  127. if !ins.IsLoadFromMap() {
  128. return errors.New("not a load from a map")
  129. }
  130. ins.encodeMapFD(fd)
  131. return nil
  132. }
  133. func (ins *Instruction) encodeMapFD(fd int) {
  134. // Preserve the offset value for direct map loads.
  135. offset := uint64(ins.Constant) & (math.MaxUint32 << 32)
  136. rawFd := uint64(uint32(fd))
  137. ins.Constant = int64(offset | rawFd)
  138. }
  139. // MapPtr returns the map fd for this instruction.
  140. //
  141. // The result is undefined if the instruction is not a load from a map,
  142. // see IsLoadFromMap.
  143. //
  144. // Deprecated: use Map() instead.
  145. func (ins *Instruction) MapPtr() int {
  146. // If there is a map associated with the instruction, return its FD.
  147. if fd := ins.Metadata.Get(mapMeta{}); fd != nil {
  148. return fd.(FDer).FD()
  149. }
  150. // Fall back to the fd stored in the Constant field
  151. return ins.mapFd()
  152. }
  153. // mapFd returns the map file descriptor stored in the 32 least significant
  154. // bits of ins' Constant field.
  155. func (ins *Instruction) mapFd() int {
  156. return int(int32(ins.Constant))
  157. }
  158. // RewriteMapOffset changes the offset of a direct load from a map.
  159. //
  160. // Returns an error if the instruction is not a direct load.
  161. func (ins *Instruction) RewriteMapOffset(offset uint32) error {
  162. if !ins.OpCode.IsDWordLoad() {
  163. return fmt.Errorf("%s is not a 64 bit load", ins.OpCode)
  164. }
  165. if ins.Src != PseudoMapValue {
  166. return errors.New("not a direct load from a map")
  167. }
  168. fd := uint64(ins.Constant) & math.MaxUint32
  169. ins.Constant = int64(uint64(offset)<<32 | fd)
  170. return nil
  171. }
  172. func (ins *Instruction) mapOffset() uint32 {
  173. return uint32(uint64(ins.Constant) >> 32)
  174. }
  175. // IsLoadFromMap returns true if the instruction loads from a map.
  176. //
  177. // This covers both loading the map pointer and direct map value loads.
  178. func (ins *Instruction) IsLoadFromMap() bool {
  179. return ins.OpCode == LoadImmOp(DWord) && (ins.Src == PseudoMapFD || ins.Src == PseudoMapValue)
  180. }
  181. // IsFunctionCall returns true if the instruction calls another BPF function.
  182. //
  183. // This is not the same thing as a BPF helper call.
  184. func (ins *Instruction) IsFunctionCall() bool {
  185. return ins.OpCode.JumpOp() == Call && ins.Src == PseudoCall
  186. }
  187. // IsLoadOfFunctionPointer returns true if the instruction loads a function pointer.
  188. func (ins *Instruction) IsLoadOfFunctionPointer() bool {
  189. return ins.OpCode.IsDWordLoad() && ins.Src == PseudoFunc
  190. }
  191. // IsFunctionReference returns true if the instruction references another BPF
  192. // function, either by invoking a Call jump operation or by loading a function
  193. // pointer.
  194. func (ins *Instruction) IsFunctionReference() bool {
  195. return ins.IsFunctionCall() || ins.IsLoadOfFunctionPointer()
  196. }
  197. // IsBuiltinCall returns true if the instruction is a built-in call, i.e. BPF helper call.
  198. func (ins *Instruction) IsBuiltinCall() bool {
  199. return ins.OpCode.JumpOp() == Call && ins.Src == R0 && ins.Dst == R0
  200. }
  201. // IsConstantLoad returns true if the instruction loads a constant of the
  202. // given size.
  203. func (ins *Instruction) IsConstantLoad(size Size) bool {
  204. return ins.OpCode == LoadImmOp(size) && ins.Src == R0 && ins.Offset == 0
  205. }
  206. // Format implements fmt.Formatter.
  207. func (ins Instruction) Format(f fmt.State, c rune) {
  208. if c != 'v' {
  209. fmt.Fprintf(f, "{UNRECOGNIZED: %c}", c)
  210. return
  211. }
  212. op := ins.OpCode
  213. if op == InvalidOpCode {
  214. fmt.Fprint(f, "INVALID")
  215. return
  216. }
  217. // Omit trailing space for Exit
  218. if op.JumpOp() == Exit {
  219. fmt.Fprint(f, op)
  220. return
  221. }
  222. if ins.IsLoadFromMap() {
  223. fd := ins.mapFd()
  224. m := ins.Map()
  225. switch ins.Src {
  226. case PseudoMapFD:
  227. if m != nil {
  228. fmt.Fprintf(f, "LoadMapPtr dst: %s map: %s", ins.Dst, m)
  229. } else {
  230. fmt.Fprintf(f, "LoadMapPtr dst: %s fd: %d", ins.Dst, fd)
  231. }
  232. case PseudoMapValue:
  233. if m != nil {
  234. fmt.Fprintf(f, "LoadMapValue dst: %s, map: %s off: %d", ins.Dst, m, ins.mapOffset())
  235. } else {
  236. fmt.Fprintf(f, "LoadMapValue dst: %s, fd: %d off: %d", ins.Dst, fd, ins.mapOffset())
  237. }
  238. }
  239. goto ref
  240. }
  241. fmt.Fprintf(f, "%v ", op)
  242. switch cls := op.Class(); {
  243. case cls.isLoadOrStore():
  244. switch op.Mode() {
  245. case ImmMode:
  246. fmt.Fprintf(f, "dst: %s imm: %d", ins.Dst, ins.Constant)
  247. case AbsMode:
  248. fmt.Fprintf(f, "imm: %d", ins.Constant)
  249. case IndMode:
  250. fmt.Fprintf(f, "dst: %s src: %s imm: %d", ins.Dst, ins.Src, ins.Constant)
  251. case MemMode:
  252. fmt.Fprintf(f, "dst: %s src: %s off: %d imm: %d", ins.Dst, ins.Src, ins.Offset, ins.Constant)
  253. case XAddMode:
  254. fmt.Fprintf(f, "dst: %s src: %s", ins.Dst, ins.Src)
  255. }
  256. case cls.IsALU():
  257. fmt.Fprintf(f, "dst: %s ", ins.Dst)
  258. if op.ALUOp() == Swap || op.Source() == ImmSource {
  259. fmt.Fprintf(f, "imm: %d", ins.Constant)
  260. } else {
  261. fmt.Fprintf(f, "src: %s", ins.Src)
  262. }
  263. case cls.IsJump():
  264. switch jop := op.JumpOp(); jop {
  265. case Call:
  266. if ins.Src == PseudoCall {
  267. // bpf-to-bpf call
  268. fmt.Fprint(f, ins.Constant)
  269. } else {
  270. fmt.Fprint(f, BuiltinFunc(ins.Constant))
  271. }
  272. default:
  273. fmt.Fprintf(f, "dst: %s off: %d ", ins.Dst, ins.Offset)
  274. if op.Source() == ImmSource {
  275. fmt.Fprintf(f, "imm: %d", ins.Constant)
  276. } else {
  277. fmt.Fprintf(f, "src: %s", ins.Src)
  278. }
  279. }
  280. }
  281. ref:
  282. if ins.Reference() != "" {
  283. fmt.Fprintf(f, " <%s>", ins.Reference())
  284. }
  285. }
  286. func (ins Instruction) equal(other Instruction) bool {
  287. return ins.OpCode == other.OpCode &&
  288. ins.Dst == other.Dst &&
  289. ins.Src == other.Src &&
  290. ins.Offset == other.Offset &&
  291. ins.Constant == other.Constant
  292. }
  293. // Size returns the amount of bytes ins would occupy in binary form.
  294. func (ins Instruction) Size() uint64 {
  295. return uint64(InstructionSize * ins.OpCode.rawInstructions())
  296. }
  297. type symbolMeta struct{}
  298. // WithSymbol marks the Instruction as a Symbol, which other Instructions
  299. // can point to using corresponding calls to WithReference.
  300. func (ins Instruction) WithSymbol(name string) Instruction {
  301. ins.Metadata.Set(symbolMeta{}, name)
  302. return ins
  303. }
  304. // Sym creates a symbol.
  305. //
  306. // Deprecated: use WithSymbol instead.
  307. func (ins Instruction) Sym(name string) Instruction {
  308. return ins.WithSymbol(name)
  309. }
  310. // Symbol returns the value ins has been marked with using WithSymbol,
  311. // otherwise returns an empty string. A symbol is often an Instruction
  312. // at the start of a function body.
  313. func (ins Instruction) Symbol() string {
  314. sym, _ := ins.Metadata.Get(symbolMeta{}).(string)
  315. return sym
  316. }
  317. type referenceMeta struct{}
  318. // WithReference makes ins reference another Symbol or map by name.
  319. func (ins Instruction) WithReference(ref string) Instruction {
  320. ins.Metadata.Set(referenceMeta{}, ref)
  321. return ins
  322. }
  323. // Reference returns the Symbol or map name referenced by ins, if any.
  324. func (ins Instruction) Reference() string {
  325. ref, _ := ins.Metadata.Get(referenceMeta{}).(string)
  326. return ref
  327. }
  328. type mapMeta struct{}
  329. // Map returns the Map referenced by ins, if any.
  330. // An Instruction will contain a Map if e.g. it references an existing,
  331. // pinned map that was opened during ELF loading.
  332. func (ins Instruction) Map() FDer {
  333. fd, _ := ins.Metadata.Get(mapMeta{}).(FDer)
  334. return fd
  335. }
  336. type sourceMeta struct{}
  337. // WithSource adds source information about the Instruction.
  338. func (ins Instruction) WithSource(src fmt.Stringer) Instruction {
  339. ins.Metadata.Set(sourceMeta{}, src)
  340. return ins
  341. }
  342. // Source returns source information about the Instruction. The field is
  343. // present when the compiler emits BTF line info about the Instruction and
  344. // usually contains the line of source code responsible for it.
  345. func (ins Instruction) Source() fmt.Stringer {
  346. str, _ := ins.Metadata.Get(sourceMeta{}).(fmt.Stringer)
  347. return str
  348. }
  349. // A Comment can be passed to Instruction.WithSource to add a comment
  350. // to an instruction.
  351. type Comment string
  352. func (s Comment) String() string {
  353. return string(s)
  354. }
  355. // FDer represents a resource tied to an underlying file descriptor.
  356. // Used as a stand-in for e.g. ebpf.Map since that type cannot be
  357. // imported here and FD() is the only method we rely on.
  358. type FDer interface {
  359. FD() int
  360. }
  361. // Instructions is an eBPF program.
  362. type Instructions []Instruction
  363. // Unmarshal unmarshals an Instructions from a binary instruction stream.
  364. // All instructions in insns are replaced by instructions decoded from r.
  365. func (insns *Instructions) Unmarshal(r io.Reader, bo binary.ByteOrder) error {
  366. if len(*insns) > 0 {
  367. *insns = nil
  368. }
  369. var offset uint64
  370. for {
  371. var ins Instruction
  372. n, err := ins.Unmarshal(r, bo)
  373. if errors.Is(err, io.EOF) {
  374. break
  375. }
  376. if err != nil {
  377. return fmt.Errorf("offset %d: %w", offset, err)
  378. }
  379. *insns = append(*insns, ins)
  380. offset += n
  381. }
  382. return nil
  383. }
  384. // Name returns the name of the function insns belongs to, if any.
  385. func (insns Instructions) Name() string {
  386. if len(insns) == 0 {
  387. return ""
  388. }
  389. return insns[0].Symbol()
  390. }
  391. func (insns Instructions) String() string {
  392. return fmt.Sprint(insns)
  393. }
  394. // Size returns the amount of bytes insns would occupy in binary form.
  395. func (insns Instructions) Size() uint64 {
  396. var sum uint64
  397. for _, ins := range insns {
  398. sum += ins.Size()
  399. }
  400. return sum
  401. }
  402. // AssociateMap updates all Instructions that Reference the given symbol
  403. // to point to an existing Map m instead.
  404. //
  405. // Returns ErrUnreferencedSymbol error if no references to symbol are found
  406. // in insns. If symbol is anything else than the symbol name of map (e.g.
  407. // a bpf2bpf subprogram), an error is returned.
  408. func (insns Instructions) AssociateMap(symbol string, m FDer) error {
  409. if symbol == "" {
  410. return errors.New("empty symbol")
  411. }
  412. var found bool
  413. for i := range insns {
  414. ins := &insns[i]
  415. if ins.Reference() != symbol {
  416. continue
  417. }
  418. if err := ins.AssociateMap(m); err != nil {
  419. return err
  420. }
  421. found = true
  422. }
  423. if !found {
  424. return fmt.Errorf("symbol %s: %w", symbol, ErrUnreferencedSymbol)
  425. }
  426. return nil
  427. }
  428. // RewriteMapPtr rewrites all loads of a specific map pointer to a new fd.
  429. //
  430. // Returns ErrUnreferencedSymbol if the symbol isn't used.
  431. //
  432. // Deprecated: use AssociateMap instead.
  433. func (insns Instructions) RewriteMapPtr(symbol string, fd int) error {
  434. if symbol == "" {
  435. return errors.New("empty symbol")
  436. }
  437. var found bool
  438. for i := range insns {
  439. ins := &insns[i]
  440. if ins.Reference() != symbol {
  441. continue
  442. }
  443. if !ins.IsLoadFromMap() {
  444. return errors.New("not a load from a map")
  445. }
  446. ins.encodeMapFD(fd)
  447. found = true
  448. }
  449. if !found {
  450. return fmt.Errorf("symbol %s: %w", symbol, ErrUnreferencedSymbol)
  451. }
  452. return nil
  453. }
  454. // SymbolOffsets returns the set of symbols and their offset in
  455. // the instructions.
  456. func (insns Instructions) SymbolOffsets() (map[string]int, error) {
  457. offsets := make(map[string]int)
  458. for i, ins := range insns {
  459. if ins.Symbol() == "" {
  460. continue
  461. }
  462. if _, ok := offsets[ins.Symbol()]; ok {
  463. return nil, fmt.Errorf("duplicate symbol %s", ins.Symbol())
  464. }
  465. offsets[ins.Symbol()] = i
  466. }
  467. return offsets, nil
  468. }
  469. // FunctionReferences returns a set of symbol names these Instructions make
  470. // bpf-to-bpf calls to.
  471. func (insns Instructions) FunctionReferences() []string {
  472. calls := make(map[string]struct{})
  473. for _, ins := range insns {
  474. if ins.Constant != -1 {
  475. // BPF-to-BPF calls have -1 constants.
  476. continue
  477. }
  478. if ins.Reference() == "" {
  479. continue
  480. }
  481. if !ins.IsFunctionReference() {
  482. continue
  483. }
  484. calls[ins.Reference()] = struct{}{}
  485. }
  486. result := make([]string, 0, len(calls))
  487. for call := range calls {
  488. result = append(result, call)
  489. }
  490. sort.Strings(result)
  491. return result
  492. }
  493. // ReferenceOffsets returns the set of references and their offset in
  494. // the instructions.
  495. func (insns Instructions) ReferenceOffsets() map[string][]int {
  496. offsets := make(map[string][]int)
  497. for i, ins := range insns {
  498. if ins.Reference() == "" {
  499. continue
  500. }
  501. offsets[ins.Reference()] = append(offsets[ins.Reference()], i)
  502. }
  503. return offsets
  504. }
  505. // Format implements fmt.Formatter.
  506. //
  507. // You can control indentation of symbols by
  508. // specifying a width. Setting a precision controls the indentation of
  509. // instructions.
  510. // The default character is a tab, which can be overridden by specifying
  511. // the ' ' space flag.
  512. func (insns Instructions) Format(f fmt.State, c rune) {
  513. if c != 's' && c != 'v' {
  514. fmt.Fprintf(f, "{UNKNOWN FORMAT '%c'}", c)
  515. return
  516. }
  517. // Precision is better in this case, because it allows
  518. // specifying 0 padding easily.
  519. padding, ok := f.Precision()
  520. if !ok {
  521. padding = 1
  522. }
  523. indent := strings.Repeat("\t", padding)
  524. if f.Flag(' ') {
  525. indent = strings.Repeat(" ", padding)
  526. }
  527. symPadding, ok := f.Width()
  528. if !ok {
  529. symPadding = padding - 1
  530. }
  531. if symPadding < 0 {
  532. symPadding = 0
  533. }
  534. symIndent := strings.Repeat("\t", symPadding)
  535. if f.Flag(' ') {
  536. symIndent = strings.Repeat(" ", symPadding)
  537. }
  538. // Guess how many digits we need at most, by assuming that all instructions
  539. // are double wide.
  540. highestOffset := len(insns) * 2
  541. offsetWidth := int(math.Ceil(math.Log10(float64(highestOffset))))
  542. iter := insns.Iterate()
  543. for iter.Next() {
  544. if iter.Ins.Symbol() != "" {
  545. fmt.Fprintf(f, "%s%s:\n", symIndent, iter.Ins.Symbol())
  546. }
  547. if src := iter.Ins.Source(); src != nil {
  548. line := strings.TrimSpace(src.String())
  549. if line != "" {
  550. fmt.Fprintf(f, "%s%*s; %s\n", indent, offsetWidth, " ", line)
  551. }
  552. }
  553. fmt.Fprintf(f, "%s%*d: %v\n", indent, offsetWidth, iter.Offset, iter.Ins)
  554. }
  555. }
  556. // Marshal encodes a BPF program into the kernel format.
  557. //
  558. // insns may be modified if there are unresolved jumps or bpf2bpf calls.
  559. //
  560. // Returns ErrUnsatisfiedProgramReference if there is a Reference Instruction
  561. // without a matching Symbol Instruction within insns.
  562. func (insns Instructions) Marshal(w io.Writer, bo binary.ByteOrder) error {
  563. if err := insns.encodeFunctionReferences(); err != nil {
  564. return err
  565. }
  566. if err := insns.encodeMapPointers(); err != nil {
  567. return err
  568. }
  569. for i, ins := range insns {
  570. if _, err := ins.Marshal(w, bo); err != nil {
  571. return fmt.Errorf("instruction %d: %w", i, err)
  572. }
  573. }
  574. return nil
  575. }
  576. // Tag calculates the kernel tag for a series of instructions.
  577. //
  578. // It mirrors bpf_prog_calc_tag in the kernel and so can be compared
  579. // to ProgramInfo.Tag to figure out whether a loaded program matches
  580. // certain instructions.
  581. func (insns Instructions) Tag(bo binary.ByteOrder) (string, error) {
  582. h := sha1.New()
  583. for i, ins := range insns {
  584. if ins.IsLoadFromMap() {
  585. ins.Constant = 0
  586. }
  587. _, err := ins.Marshal(h, bo)
  588. if err != nil {
  589. return "", fmt.Errorf("instruction %d: %w", i, err)
  590. }
  591. }
  592. return hex.EncodeToString(h.Sum(nil)[:unix.BPF_TAG_SIZE]), nil
  593. }
  594. // encodeFunctionReferences populates the Offset (or Constant, depending on
  595. // the instruction type) field of instructions with a Reference field to point
  596. // to the offset of the corresponding instruction with a matching Symbol field.
  597. //
  598. // Only Reference Instructions that are either jumps or BPF function references
  599. // (calls or function pointer loads) are populated.
  600. //
  601. // Returns ErrUnsatisfiedProgramReference if there is a Reference Instruction
  602. // without at least one corresponding Symbol Instruction within insns.
  603. func (insns Instructions) encodeFunctionReferences() error {
  604. // Index the offsets of instructions tagged as a symbol.
  605. symbolOffsets := make(map[string]RawInstructionOffset)
  606. iter := insns.Iterate()
  607. for iter.Next() {
  608. ins := iter.Ins
  609. if ins.Symbol() == "" {
  610. continue
  611. }
  612. if _, ok := symbolOffsets[ins.Symbol()]; ok {
  613. return fmt.Errorf("duplicate symbol %s", ins.Symbol())
  614. }
  615. symbolOffsets[ins.Symbol()] = iter.Offset
  616. }
  617. // Find all instructions tagged as references to other symbols.
  618. // Depending on the instruction type, populate their constant or offset
  619. // fields to point to the symbol they refer to within the insn stream.
  620. iter = insns.Iterate()
  621. for iter.Next() {
  622. i := iter.Index
  623. offset := iter.Offset
  624. ins := iter.Ins
  625. if ins.Reference() == "" {
  626. continue
  627. }
  628. switch {
  629. case ins.IsFunctionReference() && ins.Constant == -1:
  630. symOffset, ok := symbolOffsets[ins.Reference()]
  631. if !ok {
  632. return fmt.Errorf("%s at insn %d: symbol %q: %w", ins.OpCode, i, ins.Reference(), ErrUnsatisfiedProgramReference)
  633. }
  634. ins.Constant = int64(symOffset - offset - 1)
  635. case ins.OpCode.Class().IsJump() && ins.Offset == -1:
  636. symOffset, ok := symbolOffsets[ins.Reference()]
  637. if !ok {
  638. return fmt.Errorf("%s at insn %d: symbol %q: %w", ins.OpCode, i, ins.Reference(), ErrUnsatisfiedProgramReference)
  639. }
  640. ins.Offset = int16(symOffset - offset - 1)
  641. }
  642. }
  643. return nil
  644. }
  645. // encodeMapPointers finds all Map Instructions and encodes their FDs
  646. // into their Constant fields.
  647. func (insns Instructions) encodeMapPointers() error {
  648. iter := insns.Iterate()
  649. for iter.Next() {
  650. ins := iter.Ins
  651. if !ins.IsLoadFromMap() {
  652. continue
  653. }
  654. m := ins.Map()
  655. if m == nil {
  656. continue
  657. }
  658. fd := m.FD()
  659. if fd < 0 {
  660. return fmt.Errorf("map %s: %w", m, sys.ErrClosedFd)
  661. }
  662. ins.encodeMapFD(m.FD())
  663. }
  664. return nil
  665. }
  666. // Iterate allows iterating a BPF program while keeping track of
  667. // various offsets.
  668. //
  669. // Modifying the instruction slice will lead to undefined behaviour.
  670. func (insns Instructions) Iterate() *InstructionIterator {
  671. return &InstructionIterator{insns: insns}
  672. }
  673. // InstructionIterator iterates over a BPF program.
  674. type InstructionIterator struct {
  675. insns Instructions
  676. // The instruction in question.
  677. Ins *Instruction
  678. // The index of the instruction in the original instruction slice.
  679. Index int
  680. // The offset of the instruction in raw BPF instructions. This accounts
  681. // for double-wide instructions.
  682. Offset RawInstructionOffset
  683. }
  684. // Next returns true as long as there are any instructions remaining.
  685. func (iter *InstructionIterator) Next() bool {
  686. if len(iter.insns) == 0 {
  687. return false
  688. }
  689. if iter.Ins != nil {
  690. iter.Index++
  691. iter.Offset += RawInstructionOffset(iter.Ins.OpCode.rawInstructions())
  692. }
  693. iter.Ins = &iter.insns[0]
  694. iter.insns = iter.insns[1:]
  695. return true
  696. }
  697. type bpfRegisters uint8
  698. func newBPFRegisters(dst, src Register, bo binary.ByteOrder) (bpfRegisters, error) {
  699. switch bo {
  700. case binary.LittleEndian:
  701. return bpfRegisters((src << 4) | (dst & 0xF)), nil
  702. case binary.BigEndian:
  703. return bpfRegisters((dst << 4) | (src & 0xF)), nil
  704. default:
  705. return 0, fmt.Errorf("unrecognized ByteOrder %T", bo)
  706. }
  707. }
  708. // IsUnreferencedSymbol returns true if err was caused by
  709. // an unreferenced symbol.
  710. //
  711. // Deprecated: use errors.Is(err, asm.ErrUnreferencedSymbol).
  712. func IsUnreferencedSymbol(err error) bool {
  713. return errors.Is(err, ErrUnreferencedSymbol)
  714. }