| 1 |
package arm64 |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"encoding/hex" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/tetratelabs/wazero/internal/engine/wazevo/backend" |
| 9 |
"github.com/tetratelabs/wazero/internal/engine/wazevo/ssa" |
| 10 |
"github.com/tetratelabs/wazero/internal/testing/require" |
| 11 |
) |
| 12 |
|
| 13 |
func Test_maxNumFunctions(t *testing.T) { |
| 14 |
// Ensures that trampoline island is small enough to fit in the interval. |
| 15 |
require.True(t, maxNumFunctions*trampolineCallSize < trampolineIslandInterval/2) //nolint:gomnd |
| 16 |
// Ensures that each function is small enough to fit in the interval. |
| 17 |
require.True(t, maxFunctionExecutableSize < trampolineIslandInterval/2) //nolint:gomnd |
| 18 |
} |
| 19 |
|
| 20 |
func Test_encodeCallTrampolineIsland(t *testing.T) { |
| 21 |
executable := make([]byte, 16*1000) |
| 22 |
islandOffset := 160 |
| 23 |
refToBinaryOffset := []int{0: 0, 1: 16, 2: 1600, 3: 16000} |
| 24 |
|
| 25 |
encodeCallTrampolineIsland(refToBinaryOffset, 0, islandOffset, executable) |
| 26 |
for i := 0; i < len(refToBinaryOffset); i++ { |
| 27 |
offset := islandOffset + trampolineCallSize*i |
| 28 |
instrs := executable[offset : offset+trampolineCallSize-4] |
| 29 |
// Instructions are always the same except for the last immediate. |
| 30 |
require.Equal(t, "9b0000106b0380b97b030b8b60031fd6", hex.EncodeToString(instrs)) |
| 31 |
imm := binary.LittleEndian.Uint32(executable[offset+trampolineCallSize-4:]) |
| 32 |
require.Equal(t, uint32(refToBinaryOffset[ssa.FuncRef(i)]-(offset+16)), imm) |
| 33 |
} |
| 34 |
|
| 35 |
executable = executable[:] |
| 36 |
// We test again, assuming that the first offset is an imported function. |
| 37 |
const importedFns = 1 |
| 38 |
encodeCallTrampolineIsland(refToBinaryOffset, importedFns, islandOffset, executable) |
| 39 |
for i := 0; i < len(refToBinaryOffset[importedFns:]); i++ { |
| 40 |
offset := islandOffset + trampolineCallSize*i |
| 41 |
instrs := executable[offset : offset+trampolineCallSize-4] |
| 42 |
// Instructions are always the same except for the last immediate. |
| 43 |
require.Equal(t, "9b0000106b0380b97b030b8b60031fd6", hex.EncodeToString(instrs)) |
| 44 |
imm := binary.LittleEndian.Uint32(executable[offset+trampolineCallSize-4:]) |
| 45 |
require.Equal(t, uint32(refToBinaryOffset[ssa.FuncRef(i+importedFns)]-(offset+16)), imm) |
| 46 |
} |
| 47 |
// If the encoding is incorrect, then this offset should contain zeroes. |
| 48 |
finalOffset := islandOffset + trampolineCallSize*len(refToBinaryOffset) |
| 49 |
instrs := executable[finalOffset : finalOffset+trampolineCallSize] |
| 50 |
require.Equal(t, "0000000000000000000000000000000000000000", hex.EncodeToString(instrs)) |
| 51 |
} |
| 52 |
|
| 53 |
func Test_ResolveRelocations_importedFns(t *testing.T) { |
| 54 |
tests := []struct { |
| 55 |
name string |
| 56 |
refToOffset []int |
| 57 |
importedFns int |
| 58 |
relocations []backend.RelocationInfo |
| 59 |
islandOffsets []int |
| 60 |
}{ |
| 61 |
{ |
| 62 |
name: "Single imported function", |
| 63 |
refToOffset: []int{10, 20, 200 * 1024 * 1024}, |
| 64 |
importedFns: 1, |
| 65 |
relocations: []backend.RelocationInfo{ |
| 66 |
{Offset: 100, FuncRef: 2}, |
| 67 |
}, |
| 68 |
islandOffsets: []int{500}, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "Multiple imported functions", |
| 72 |
refToOffset: []int{10, 20, 30, 40, 200 * 1024 * 1024}, |
| 73 |
importedFns: 3, |
| 74 |
relocations: []backend.RelocationInfo{ |
| 75 |
{Offset: 100, FuncRef: 4}, |
| 76 |
}, |
| 77 |
islandOffsets: []int{500}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "Multiple relocations with imported functions", |
| 81 |
refToOffset: []int{10, 20, 30, 40, 200 * 1024 * 1024, 250 * 1024 * 1024}, |
| 82 |
importedFns: 2, |
| 83 |
relocations: []backend.RelocationInfo{ |
| 84 |
{Offset: 100, FuncRef: 4}, |
| 85 |
{Offset: 200, FuncRef: 5}, |
| 86 |
}, |
| 87 |
islandOffsets: []int{500}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "Many imported functions", |
| 91 |
refToOffset: []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200 * 1024 * 1024}, |
| 92 |
importedFns: 9, |
| 93 |
relocations: []backend.RelocationInfo{ |
| 94 |
{Offset: 120, FuncRef: 10}, |
| 95 |
}, |
| 96 |
islandOffsets: []int{600}, |
| 97 |
}, |
| 98 |
{ |
| 99 |
name: "Multiple islands", |
| 100 |
refToOffset: []int{10, 20, 30, 40, 200 * 1024 * 1024, 300 * 1024 * 1024}, |
| 101 |
importedFns: 3, |
| 102 |
relocations: []backend.RelocationInfo{ |
| 103 |
{Offset: 100, FuncRef: 4}, |
| 104 |
{Offset: 200*1024*1024 - 1000, FuncRef: 5}, |
| 105 |
}, |
| 106 |
islandOffsets: []int{500, 200*1024*1024 - 500}, |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: "Multiple islands, no imported functions", |
| 110 |
refToOffset: []int{10, 20, 30, 40, 200 * 1024 * 1024, 300 * 1024 * 1024}, |
| 111 |
importedFns: 0, |
| 112 |
relocations: []backend.RelocationInfo{ |
| 113 |
{Offset: 100, FuncRef: 4}, |
| 114 |
{Offset: 200*1024*1024 - 1000, FuncRef: 5}, |
| 115 |
}, |
| 116 |
islandOffsets: []int{500, 200*1024*1024 - 500}, |
| 117 |
}, |
| 118 |
} |
| 119 |
|
| 120 |
extractTargetInstruction := func(executable []byte, branchInstrOffset int64) uint32 { |
| 121 |
bytes := executable[branchInstrOffset : branchInstrOffset+4] |
| 122 |
return uint32(bytes[0]) | uint32(bytes[1])<<8 | uint32(bytes[2])<<16 | uint32(bytes[3])<<24 |
| 123 |
} |
| 124 |
|
| 125 |
for _, tt := range tests { |
| 126 |
t.Run(tt.name, func(t *testing.T) { |
| 127 |
// Create large executable buffer to handle far offsets |
| 128 |
executable := make([]byte, 310*1024*1024) |
| 129 |
|
| 130 |
m := &machine{} |
| 131 |
m.ResolveRelocations( |
| 132 |
tt.refToOffset, |
| 133 |
tt.importedFns, |
| 134 |
executable, |
| 135 |
tt.relocations, |
| 136 |
tt.islandOffsets, |
| 137 |
) |
| 138 |
|
| 139 |
for _, reloc := range tt.relocations { |
| 140 |
relocOffset := reloc.Offset |
| 141 |
|
| 142 |
// Extract the actual target instruction from the generated branch instruction |
| 143 |
actualInstruction := extractTargetInstruction(executable, relocOffset) |
| 144 |
|
| 145 |
// First find which island this relocation would use |
| 146 |
funcOffset := tt.refToOffset[reloc.FuncRef] |
| 147 |
diff := int64(funcOffset) - relocOffset |
| 148 |
|
| 149 |
// Only test relocations that need trampolines |
| 150 |
if diff < minUnconditionalBranchOffset || diff > maxUnconditionalBranchOffset { |
| 151 |
// Find the nearest trampoline island |
| 152 |
islandOffset := searchTrampolineIsland(tt.islandOffsets, int(relocOffset)) |
| 153 |
|
| 154 |
expectedIslandOffset := islandOffset + trampolineCallSize*(int(reloc.FuncRef)-tt.importedFns) |
| 155 |
expectedBranchOffset := int64(expectedIslandOffset) - relocOffset |
| 156 |
expectedInstruction := encodeUnconditionalBranch(true, expectedBranchOffset) |
| 157 |
|
| 158 |
require.Equal(t, expectedInstruction, actualInstruction) |
| 159 |
|
| 160 |
} else { |
| 161 |
t.Logf("Skipping relocation that doesn't need a trampoline: %v", reloc) |
| 162 |
} |
| 163 |
} |
| 164 |
}) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func Test_searchTrampolineIsland(t *testing.T) { |
| 169 |
offsets := []int{16, 32, 48} |
| 170 |
require.Equal(t, 32, searchTrampolineIsland(offsets, 30)) |
| 171 |
require.Equal(t, 32, searchTrampolineIsland(offsets, 17)) |
| 172 |
require.Equal(t, 16, searchTrampolineIsland(offsets, 16)) |
| 173 |
require.Equal(t, 48, searchTrampolineIsland(offsets, 48)) |
| 174 |
require.Equal(t, 48, searchTrampolineIsland(offsets, 56)) |
| 175 |
require.Equal(t, 16, searchTrampolineIsland(offsets, 1)) |
| 176 |
} |