instruction.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package asm
  2. import (
  3. "crypto/sha1"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "math"
  10. "strings"
  11. "github.com/cilium/ebpf/internal/unix"
  12. )
  13. // InstructionSize is the size of a BPF instruction in bytes
  14. const InstructionSize = 8
  15. // RawInstructionOffset is an offset in units of raw BPF instructions.
  16. type RawInstructionOffset uint64
  17. // Bytes returns the offset of an instruction in bytes.
  18. func (rio RawInstructionOffset) Bytes() uint64 {
  19. return uint64(rio) * InstructionSize
  20. }
  21. // Instruction is a single eBPF instruction.
  22. type Instruction struct {
  23. OpCode OpCode
  24. Dst Register
  25. Src Register
  26. Offset int16
  27. Constant int64
  28. Reference string
  29. Symbol string
  30. }
  31. // Sym creates a symbol.
  32. func (ins Instruction) Sym(name string) Instruction {
  33. ins.Symbol = name
  34. return ins
  35. }
  36. // Unmarshal decodes a BPF instruction.
  37. func (ins *Instruction) Unmarshal(r io.Reader, bo binary.ByteOrder) (uint64, error) {
  38. var bi bpfInstruction
  39. err := binary.Read(r, bo, &bi)
  40. if err != nil {
  41. return 0, err
  42. }
  43. ins.OpCode = bi.OpCode
  44. ins.Offset = bi.Offset
  45. ins.Constant = int64(bi.Constant)
  46. ins.Dst, ins.Src, err = bi.Registers.Unmarshal(bo)
  47. if err != nil {
  48. return 0, fmt.Errorf("can't unmarshal registers: %s", err)
  49. }
  50. if !bi.OpCode.isDWordLoad() {
  51. return InstructionSize, nil
  52. }
  53. var bi2 bpfInstruction
  54. if err := binary.Read(r, bo, &bi2); err != nil {
  55. // No Wrap, to avoid io.EOF clash
  56. return 0, errors.New("64bit immediate is missing second half")
  57. }
  58. if bi2.OpCode != 0 || bi2.Offset != 0 || bi2.Registers != 0 {
  59. return 0, errors.New("64bit immediate has non-zero fields")
  60. }
  61. ins.Constant = int64(uint64(uint32(bi2.Constant))<<32 | uint64(uint32(bi.Constant)))
  62. return 2 * InstructionSize, nil
  63. }
  64. // Marshal encodes a BPF instruction.
  65. func (ins Instruction) Marshal(w io.Writer, bo binary.ByteOrder) (uint64, error) {
  66. if ins.OpCode == InvalidOpCode {
  67. return 0, errors.New("invalid opcode")
  68. }
  69. isDWordLoad := ins.OpCode.isDWordLoad()
  70. cons := int32(ins.Constant)
  71. if isDWordLoad {
  72. // Encode least significant 32bit first for 64bit operations.
  73. cons = int32(uint32(ins.Constant))
  74. }
  75. regs, err := newBPFRegisters(ins.Dst, ins.Src, bo)
  76. if err != nil {
  77. return 0, fmt.Errorf("can't marshal registers: %s", err)
  78. }
  79. bpfi := bpfInstruction{
  80. ins.OpCode,
  81. regs,
  82. ins.Offset,
  83. cons,
  84. }
  85. if err := binary.Write(w, bo, &bpfi); err != nil {
  86. return 0, err
  87. }
  88. if !isDWordLoad {
  89. return InstructionSize, nil
  90. }
  91. bpfi = bpfInstruction{
  92. Constant: int32(ins.Constant >> 32),
  93. }
  94. if err := binary.Write(w, bo, &bpfi); err != nil {
  95. return 0, err
  96. }
  97. return 2 * InstructionSize, nil
  98. }
  99. // RewriteMapPtr changes an instruction to use a new map fd.
  100. //
  101. // Returns an error if the instruction doesn't load a map.
  102. func (ins *Instruction) RewriteMapPtr(fd int) error {
  103. if !ins.OpCode.isDWordLoad() {
  104. return fmt.Errorf("%s is not a 64 bit load", ins.OpCode)
  105. }
  106. if ins.Src != PseudoMapFD && ins.Src != PseudoMapValue {
  107. return errors.New("not a load from a map")
  108. }
  109. // Preserve the offset value for direct map loads.
  110. offset := uint64(ins.Constant) & (math.MaxUint32 << 32)
  111. rawFd := uint64(uint32(fd))
  112. ins.Constant = int64(offset | rawFd)
  113. return nil
  114. }
  115. func (ins *Instruction) mapPtr() uint32 {
  116. return uint32(uint64(ins.Constant) & math.MaxUint32)
  117. }
  118. // RewriteMapOffset changes the offset of a direct load from a map.
  119. //
  120. // Returns an error if the instruction is not a direct load.
  121. func (ins *Instruction) RewriteMapOffset(offset uint32) error {
  122. if !ins.OpCode.isDWordLoad() {
  123. return fmt.Errorf("%s is not a 64 bit load", ins.OpCode)
  124. }
  125. if ins.Src != PseudoMapValue {
  126. return errors.New("not a direct load from a map")
  127. }
  128. fd := uint64(ins.Constant) & math.MaxUint32
  129. ins.Constant = int64(uint64(offset)<<32 | fd)
  130. return nil
  131. }
  132. func (ins *Instruction) mapOffset() uint32 {
  133. return uint32(uint64(ins.Constant) >> 32)
  134. }
  135. // isLoadFromMap returns true if the instruction loads from a map.
  136. //
  137. // This covers both loading the map pointer and direct map value loads.
  138. func (ins *Instruction) isLoadFromMap() bool {
  139. return ins.OpCode == LoadImmOp(DWord) && (ins.Src == PseudoMapFD || ins.Src == PseudoMapValue)
  140. }
  141. // IsFunctionCall returns true if the instruction calls another BPF function.
  142. //
  143. // This is not the same thing as a BPF helper call.
  144. func (ins *Instruction) IsFunctionCall() bool {
  145. return ins.OpCode.JumpOp() == Call && ins.Src == PseudoCall
  146. }
  147. // Format implements fmt.Formatter.
  148. func (ins Instruction) Format(f fmt.State, c rune) {
  149. if c != 'v' {
  150. fmt.Fprintf(f, "{UNRECOGNIZED: %c}", c)
  151. return
  152. }
  153. op := ins.OpCode
  154. if op == InvalidOpCode {
  155. fmt.Fprint(f, "INVALID")
  156. return
  157. }
  158. // Omit trailing space for Exit
  159. if op.JumpOp() == Exit {
  160. fmt.Fprint(f, op)
  161. return
  162. }
  163. if ins.isLoadFromMap() {
  164. fd := int32(ins.mapPtr())
  165. switch ins.Src {
  166. case PseudoMapFD:
  167. fmt.Fprintf(f, "LoadMapPtr dst: %s fd: %d", ins.Dst, fd)
  168. case PseudoMapValue:
  169. fmt.Fprintf(f, "LoadMapValue dst: %s, fd: %d off: %d", ins.Dst, fd, ins.mapOffset())
  170. }
  171. goto ref
  172. }
  173. fmt.Fprintf(f, "%v ", op)
  174. switch cls := op.Class(); cls {
  175. case LdClass, LdXClass, StClass, StXClass:
  176. switch op.Mode() {
  177. case ImmMode:
  178. fmt.Fprintf(f, "dst: %s imm: %d", ins.Dst, ins.Constant)
  179. case AbsMode:
  180. fmt.Fprintf(f, "imm: %d", ins.Constant)
  181. case IndMode:
  182. fmt.Fprintf(f, "dst: %s src: %s imm: %d", ins.Dst, ins.Src, ins.Constant)
  183. case MemMode:
  184. fmt.Fprintf(f, "dst: %s src: %s off: %d imm: %d", ins.Dst, ins.Src, ins.Offset, ins.Constant)
  185. case XAddMode:
  186. fmt.Fprintf(f, "dst: %s src: %s", ins.Dst, ins.Src)
  187. }
  188. case ALU64Class, ALUClass:
  189. fmt.Fprintf(f, "dst: %s ", ins.Dst)
  190. if op.ALUOp() == Swap || op.Source() == ImmSource {
  191. fmt.Fprintf(f, "imm: %d", ins.Constant)
  192. } else {
  193. fmt.Fprintf(f, "src: %s", ins.Src)
  194. }
  195. case JumpClass:
  196. switch jop := op.JumpOp(); jop {
  197. case Call:
  198. if ins.Src == PseudoCall {
  199. // bpf-to-bpf call
  200. fmt.Fprint(f, ins.Constant)
  201. } else {
  202. fmt.Fprint(f, BuiltinFunc(ins.Constant))
  203. }
  204. default:
  205. fmt.Fprintf(f, "dst: %s off: %d ", ins.Dst, ins.Offset)
  206. if op.Source() == ImmSource {
  207. fmt.Fprintf(f, "imm: %d", ins.Constant)
  208. } else {
  209. fmt.Fprintf(f, "src: %s", ins.Src)
  210. }
  211. }
  212. }
  213. ref:
  214. if ins.Reference != "" {
  215. fmt.Fprintf(f, " <%s>", ins.Reference)
  216. }
  217. }
  218. // Instructions is an eBPF program.
  219. type Instructions []Instruction
  220. func (insns Instructions) String() string {
  221. return fmt.Sprint(insns)
  222. }
  223. // RewriteMapPtr rewrites all loads of a specific map pointer to a new fd.
  224. //
  225. // Returns an error if the symbol isn't used, see IsUnreferencedSymbol.
  226. func (insns Instructions) RewriteMapPtr(symbol string, fd int) error {
  227. if symbol == "" {
  228. return errors.New("empty symbol")
  229. }
  230. found := false
  231. for i := range insns {
  232. ins := &insns[i]
  233. if ins.Reference != symbol {
  234. continue
  235. }
  236. if err := ins.RewriteMapPtr(fd); err != nil {
  237. return err
  238. }
  239. found = true
  240. }
  241. if !found {
  242. return &unreferencedSymbolError{symbol}
  243. }
  244. return nil
  245. }
  246. // SymbolOffsets returns the set of symbols and their offset in
  247. // the instructions.
  248. func (insns Instructions) SymbolOffsets() (map[string]int, error) {
  249. offsets := make(map[string]int)
  250. for i, ins := range insns {
  251. if ins.Symbol == "" {
  252. continue
  253. }
  254. if _, ok := offsets[ins.Symbol]; ok {
  255. return nil, fmt.Errorf("duplicate symbol %s", ins.Symbol)
  256. }
  257. offsets[ins.Symbol] = i
  258. }
  259. return offsets, nil
  260. }
  261. // ReferenceOffsets returns the set of references and their offset in
  262. // the instructions.
  263. func (insns Instructions) ReferenceOffsets() map[string][]int {
  264. offsets := make(map[string][]int)
  265. for i, ins := range insns {
  266. if ins.Reference == "" {
  267. continue
  268. }
  269. offsets[ins.Reference] = append(offsets[ins.Reference], i)
  270. }
  271. return offsets
  272. }
  273. // Format implements fmt.Formatter.
  274. //
  275. // You can control indentation of symbols by
  276. // specifying a width. Setting a precision controls the indentation of
  277. // instructions.
  278. // The default character is a tab, which can be overriden by specifying
  279. // the ' ' space flag.
  280. func (insns Instructions) Format(f fmt.State, c rune) {
  281. if c != 's' && c != 'v' {
  282. fmt.Fprintf(f, "{UNKNOWN FORMAT '%c'}", c)
  283. return
  284. }
  285. // Precision is better in this case, because it allows
  286. // specifying 0 padding easily.
  287. padding, ok := f.Precision()
  288. if !ok {
  289. padding = 1
  290. }
  291. indent := strings.Repeat("\t", padding)
  292. if f.Flag(' ') {
  293. indent = strings.Repeat(" ", padding)
  294. }
  295. symPadding, ok := f.Width()
  296. if !ok {
  297. symPadding = padding - 1
  298. }
  299. if symPadding < 0 {
  300. symPadding = 0
  301. }
  302. symIndent := strings.Repeat("\t", symPadding)
  303. if f.Flag(' ') {
  304. symIndent = strings.Repeat(" ", symPadding)
  305. }
  306. // Guess how many digits we need at most, by assuming that all instructions
  307. // are double wide.
  308. highestOffset := len(insns) * 2
  309. offsetWidth := int(math.Ceil(math.Log10(float64(highestOffset))))
  310. iter := insns.Iterate()
  311. for iter.Next() {
  312. if iter.Ins.Symbol != "" {
  313. fmt.Fprintf(f, "%s%s:\n", symIndent, iter.Ins.Symbol)
  314. }
  315. fmt.Fprintf(f, "%s%*d: %v\n", indent, offsetWidth, iter.Offset, iter.Ins)
  316. }
  317. return
  318. }
  319. // Marshal encodes a BPF program into the kernel format.
  320. func (insns Instructions) Marshal(w io.Writer, bo binary.ByteOrder) error {
  321. for i, ins := range insns {
  322. _, err := ins.Marshal(w, bo)
  323. if err != nil {
  324. return fmt.Errorf("instruction %d: %w", i, err)
  325. }
  326. }
  327. return nil
  328. }
  329. // Tag calculates the kernel tag for a series of instructions.
  330. //
  331. // It mirrors bpf_prog_calc_tag in the kernel and so can be compared
  332. // to ProgramInfo.Tag to figure out whether a loaded program matches
  333. // certain instructions.
  334. func (insns Instructions) Tag(bo binary.ByteOrder) (string, error) {
  335. h := sha1.New()
  336. for i, ins := range insns {
  337. if ins.isLoadFromMap() {
  338. ins.Constant = 0
  339. }
  340. _, err := ins.Marshal(h, bo)
  341. if err != nil {
  342. return "", fmt.Errorf("instruction %d: %w", i, err)
  343. }
  344. }
  345. return hex.EncodeToString(h.Sum(nil)[:unix.BPF_TAG_SIZE]), nil
  346. }
  347. // Iterate allows iterating a BPF program while keeping track of
  348. // various offsets.
  349. //
  350. // Modifying the instruction slice will lead to undefined behaviour.
  351. func (insns Instructions) Iterate() *InstructionIterator {
  352. return &InstructionIterator{insns: insns}
  353. }
  354. // InstructionIterator iterates over a BPF program.
  355. type InstructionIterator struct {
  356. insns Instructions
  357. // The instruction in question.
  358. Ins *Instruction
  359. // The index of the instruction in the original instruction slice.
  360. Index int
  361. // The offset of the instruction in raw BPF instructions. This accounts
  362. // for double-wide instructions.
  363. Offset RawInstructionOffset
  364. }
  365. // Next returns true as long as there are any instructions remaining.
  366. func (iter *InstructionIterator) Next() bool {
  367. if len(iter.insns) == 0 {
  368. return false
  369. }
  370. if iter.Ins != nil {
  371. iter.Index++
  372. iter.Offset += RawInstructionOffset(iter.Ins.OpCode.rawInstructions())
  373. }
  374. iter.Ins = &iter.insns[0]
  375. iter.insns = iter.insns[1:]
  376. return true
  377. }
  378. type bpfInstruction struct {
  379. OpCode OpCode
  380. Registers bpfRegisters
  381. Offset int16
  382. Constant int32
  383. }
  384. type bpfRegisters uint8
  385. func newBPFRegisters(dst, src Register, bo binary.ByteOrder) (bpfRegisters, error) {
  386. switch bo {
  387. case binary.LittleEndian:
  388. return bpfRegisters((src << 4) | (dst & 0xF)), nil
  389. case binary.BigEndian:
  390. return bpfRegisters((dst << 4) | (src & 0xF)), nil
  391. default:
  392. return 0, fmt.Errorf("unrecognized ByteOrder %T", bo)
  393. }
  394. }
  395. func (r bpfRegisters) Unmarshal(bo binary.ByteOrder) (dst, src Register, err error) {
  396. switch bo {
  397. case binary.LittleEndian:
  398. return Register(r & 0xF), Register(r >> 4), nil
  399. case binary.BigEndian:
  400. return Register(r >> 4), Register(r & 0xf), nil
  401. default:
  402. return 0, 0, fmt.Errorf("unrecognized ByteOrder %T", bo)
  403. }
  404. }
  405. type unreferencedSymbolError struct {
  406. symbol string
  407. }
  408. func (use *unreferencedSymbolError) Error() string {
  409. return fmt.Sprintf("unreferenced symbol %s", use.symbol)
  410. }
  411. // IsUnreferencedSymbol returns true if err was caused by
  412. // an unreferenced symbol.
  413. func IsUnreferencedSymbol(err error) bool {
  414. _, ok := err.(*unreferencedSymbolError)
  415. return ok
  416. }