|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package amd |
| 5 | + |
| 6 | +import ( |
| 7 | + "io" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "go.opentelemetry.io/ebpf-profiler/asm/expression" |
| 13 | +) |
| 14 | + |
| 15 | +func BenchmarkPythonInterpreter(b *testing.B) { |
| 16 | + for i := 0; i < b.N; i++ { |
| 17 | + testPythonInterpreter(b) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestPythonInterpreter(t *testing.T) { |
| 22 | + testPythonInterpreter(t) |
| 23 | +} |
| 24 | + |
| 25 | +func testPythonInterpreter(t testing.TB) { |
| 26 | + // 00010000 4D 89 F2 mov r10, r14 |
| 27 | + // 00010003 45 0F B6 36 movzx r14d, byte ptr [r14] |
| 28 | + // 00010007 48 8D 05 2D B3 35 00 lea rax, [rip + 0x35b32d] |
| 29 | + // 0001000E 4C 8B 6C 24 08 mov r13, qword ptr [rsp + 8] |
| 30 | + // 00010013 48 89 C1 mov rcx, rax |
| 31 | + // 00010016 48 89 44 24 10 mov qword ptr [rsp + 0x10], rax |
| 32 | + // 0001001B 45 0F B6 5A 01 movzx r11d, byte ptr [r10 + 1] |
| 33 | + // 00010020 41 0F B6 C6 movzx eax, r14b |
| 34 | + // 00010024 48 8B 04 C1 mov rax, qword ptr [rcx + rax*8] |
| 35 | + // 00010028 FF E0 jmp rax |
| 36 | + code := []byte{ |
| 37 | + 0x4d, 0x89, 0xf2, 0x45, 0x0f, 0xb6, 0x36, 0x48, 0x8d, 0x05, 0x2d, 0xb3, 0x35, |
| 38 | + 0x00, 0x4c, 0x8b, 0x6c, 0x24, 0x08, 0x48, 0x89, 0xc1, 0x48, 0x89, 0x44, 0x24, |
| 39 | + 0x10, 0x45, 0x0f, 0xb6, 0x5a, 0x01, 0x41, 0x0f, 0xb6, 0xc6, 0x48, 0x8b, 0x04, |
| 40 | + 0xc1, 0xff, 0xe0, |
| 41 | + } |
| 42 | + it := NewInterpreterWithCode(code) |
| 43 | + it.CodeAddress = expression.Imm(0x8AF05) |
| 44 | + r14 := it.Regs.Get(R14) |
| 45 | + _, err := it.Loop() |
| 46 | + if err == nil || err != io.EOF { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + actual := it.Regs.Get(RAX) |
| 50 | + expected := expression.Mem( |
| 51 | + expression.Add( |
| 52 | + expression.Multiply( |
| 53 | + expression.ZeroExtend8(expression.Mem1(r14)), |
| 54 | + expression.Imm(8), |
| 55 | + ), |
| 56 | + expression.NewImmediateCapture("switch table"), |
| 57 | + ), |
| 58 | + 8, |
| 59 | + ) |
| 60 | + if !actual.Match(expected) { |
| 61 | + t.Fatal() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestRecoverSwitchCase(t *testing.T) { |
| 66 | + blocks := []CodeBlock{ |
| 67 | + { |
| 68 | + Address: expression.Imm(0x3310E3), |
| 69 | + // 003310E3 48 8B 44 24 20 mov rax, qword ptr [rsp + 0x20] |
| 70 | + // 003310E8 48 89 18 mov qword ptr [rax], rbx |
| 71 | + // 003310EB 49 83 C2 02 add r10, 2 |
| 72 | + // 003310EF 44 89 E0 mov eax, r12d |
| 73 | + // 003310F2 83 E0 03 and eax, 3 |
| 74 | + // 003310F5 31 DB xor ebx, ebx |
| 75 | + // 003310F7 41 F6 C4 04 test r12b, 4 |
| 76 | + // 003310FB 4C 89 74 24 10 mov qword ptr [rsp + 0x10], r14 |
| 77 | + // 00331100 74 08 je 0x33110a |
| 78 | + Code: []byte{0x48, 0x8b, 0x44, 0x24, 0x20, 0x48, 0x89, 0x18, 0x49, |
| 79 | + 0x83, 0xc2, 0x02, 0x44, 0x89, 0xe0, 0x83, 0xe0, 0x03, 0x31, 0xdb, |
| 80 | + 0x41, 0xf6, 0xc4, 0x04, 0x4c, 0x89, 0x74, 0x24, 0x10, 0x74, 0x08}, |
| 81 | + }, |
| 82 | + { |
| 83 | + Address: expression.Imm(0x33110a), |
| 84 | + // 0033110A 4D 89 DC mov r12, r11 |
| 85 | + // 0033110D 4D 8D 47 F8 lea r8, [r15 - 8] |
| 86 | + // 00331111 4C 89 7C 24 60 mov qword ptr [rsp + 0x60], r15 |
| 87 | + // 00331116 4D 8B 7F F8 mov r15, qword ptr [r15 - 8] |
| 88 | + // 0033111A 48 8B 0D 87 06 17 01 mov rcx, qword ptr [rip + 0x1170687] |
| 89 | + // 00331121 89 C0 mov eax, eax |
| 90 | + // 00331123 48 8D 15 02 E7 C0 00 lea rdx, [rip + 0xc0e702] |
| 91 | + // 0033112A 48 63 04 82 movsxd rax, dword ptr [rdx + rax*4] |
| 92 | + // 0033112E 48 01 D0 add rax, rdx |
| 93 | + // 00331131 4C 89 D5 mov rbp, r10 |
| 94 | + // 00331134 4D 89 C5 mov r13, r8 |
| 95 | + // 00331137 FF E0 jmp rax |
| 96 | + Code: []byte{ |
| 97 | + 0x4d, 0x89, 0xdc, 0x4d, 0x8d, 0x47, 0xf8, 0x4c, 0x89, 0x7c, 0x24, |
| 98 | + 0x60, 0x4d, 0x8b, 0x7f, 0xf8, 0x48, 0x8b, 0x0d, 0x87, 0x06, 0x17, |
| 99 | + 0x01, 0x89, 0xc0, 0x48, 0x8d, 0x15, 0x02, 0xe7, 0xc0, 0x00, 0x48, |
| 100 | + 0x63, 0x04, 0x82, 0x48, 0x01, 0xd0, 0x4c, 0x89, 0xd5, 0x4d, 0x89, |
| 101 | + 0xc5, 0xff, 0xe0, |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + it := NewInterpreter() |
| 106 | + initR12 := it.Regs.Get(R12) |
| 107 | + it.ResetCode(blocks[0].Code, blocks[0].Address) |
| 108 | + _, err := it.Loop() |
| 109 | + require.ErrorIs(t, err, io.EOF) |
| 110 | + |
| 111 | + expected := expression.ZeroExtend(initR12, 2) |
| 112 | + assertEval(t, it.Regs.Get(RAX), expected) |
| 113 | + it.ResetCode(blocks[1].Code, blocks[1].Address) |
| 114 | + _, err = it.Loop() |
| 115 | + require.ErrorIs(t, err, io.EOF) |
| 116 | + table := expression.NewImmediateCapture("table") |
| 117 | + base := expression.NewImmediateCapture("base") |
| 118 | + expected = expression.Add( |
| 119 | + expression.SignExtend( |
| 120 | + expression.Mem( |
| 121 | + expression.Add( |
| 122 | + expression.Multiply( |
| 123 | + expression.ZeroExtend(initR12, 2), |
| 124 | + expression.Imm(4), |
| 125 | + ), |
| 126 | + table, |
| 127 | + ), |
| 128 | + 4, |
| 129 | + ), |
| 130 | + 64, |
| 131 | + ), |
| 132 | + base, |
| 133 | + ) |
| 134 | + assertEval(t, it.Regs.Get(RAX), expected) |
| 135 | + assert.EqualValues(t, 0xf3f82c, table.CapturedValue()) |
| 136 | + assert.EqualValues(t, 0xf3f82c, base.CapturedValue()) |
| 137 | +} |
| 138 | + |
| 139 | +func assertEval(t *testing.T, left, right expression.Expression) { |
| 140 | + if !left.Match(right) { |
| 141 | + assert.Failf(t, "failed to eval %s to %s", left.DebugString(), right.DebugString()) |
| 142 | + t.Logf("left %s", left.DebugString()) |
| 143 | + t.Logf("right %s", right.DebugString()) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func FuzzInterpreter(f *testing.F) { |
| 148 | + f.Fuzz(func(_ *testing.T, code []byte) { |
| 149 | + i := NewInterpreterWithCode(code) |
| 150 | + _, _ = i.Loop() |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +func TestMoveSignExtend(t *testing.T) { |
| 155 | + i := NewInterpreterWithCode([]byte{ |
| 156 | + // 00000000 B8 01 00 00 00 mov eax, 1 |
| 157 | + // 00000005 8B 40 04 mov eax, dword ptr [rax + 4] |
| 158 | + // 00000008 B8 02 00 00 00 mov eax, 2 |
| 159 | + // 0000000D 48 0F B6 40 04 movzx rax, byte ptr [rax + 4] |
| 160 | + // 00000012 B8 03 00 00 00 mov eax, 3 |
| 161 | + // 00000017 48 0F BF 40 04 movsx rax, word ptr [rax + 4] |
| 162 | + 0xB8, 0x01, 0x00, 0x00, 0x00, 0x8B, 0x40, 0x04, |
| 163 | + 0xB8, 0x02, 0x00, 0x00, 0x00, 0x48, 0x0F, 0xB6, |
| 164 | + 0x40, 0x04, 0xB8, 0x03, 0x00, 0x00, 0x00, 0x48, |
| 165 | + 0x0F, 0xBF, 0x40, 0x04, |
| 166 | + }) |
| 167 | + _, err := i.Loop() |
| 168 | + require.ErrorIs(t, err, io.EOF) |
| 169 | + pattern := expression.SignExtend(expression.Mem(expression.Imm(7), 2), 64) |
| 170 | + require.True(t, i.Regs.Get(RAX).Match(pattern)) |
| 171 | +} |
0 commit comments