| 1 |
package wazevoapi |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/hex" |
| 6 |
"fmt" |
| 7 |
"math/rand" |
| 8 |
"os" |
| 9 |
"sync" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
// These consts are used various places in the wazevo implementations. |
| 14 |
// Instead of defining them in each file, we define them here so that we can quickly iterate on |
| 15 |
// debugging without spending "where do we have debug logging?" time. |
| 16 |
|
| 17 |
// ----- Debug logging ----- |
| 18 |
// These consts must be disabled by default. Enable them only when debugging. |
| 19 |
|
| 20 |
const ( |
| 21 |
FrontEndLoggingEnabled = false |
| 22 |
SSALoggingEnabled = false |
| 23 |
RegAllocLoggingEnabled = false |
| 24 |
) |
| 25 |
|
| 26 |
// ----- Output prints ----- |
| 27 |
// These consts must be disabled by default. Enable them only when debugging. |
| 28 |
|
| 29 |
const ( |
| 30 |
PrintSSA = false |
| 31 |
PrintOptimizedSSA = false |
| 32 |
PrintSSAToBackendIRLowering = false |
| 33 |
PrintRegisterAllocated = false |
| 34 |
PrintFinalizedMachineCode = false |
| 35 |
PrintMachineCodeHexPerFunction = printMachineCodeHexPerFunctionUnmodified || PrintMachineCodeHexPerFunctionDisassemblable //nolint |
| 36 |
printMachineCodeHexPerFunctionUnmodified = false |
| 37 |
// PrintMachineCodeHexPerFunctionDisassemblable prints the machine code while modifying the actual result |
| 38 |
// to make it disassemblable. This is useful when debugging the final machine code. See the places where this is used for detail. |
| 39 |
// When this is enabled, functions must not be called. |
| 40 |
PrintMachineCodeHexPerFunctionDisassemblable = false |
| 41 |
) |
| 42 |
|
| 43 |
// printTarget is the function index to print the machine code. This is used for debugging to print the machine code |
| 44 |
// of a specific function. |
| 45 |
const printTarget = -1 |
| 46 |
|
| 47 |
// PrintEnabledIndex returns true if the current function index is the print target. |
| 48 |
func PrintEnabledIndex(ctx context.Context) bool { |
| 49 |
if printTarget == -1 { |
| 50 |
return true |
| 51 |
} |
| 52 |
return GetCurrentFunctionIndex(ctx) == printTarget |
| 53 |
} |
| 54 |
|
| 55 |
// ----- Validations ----- |
| 56 |
const ( |
| 57 |
// SSAValidationEnabled enables the SSA validation. This is disabled by default since the operation is expensive. |
| 58 |
SSAValidationEnabled = false |
| 59 |
) |
| 60 |
|
| 61 |
// ----- Stack Guard Check ----- |
| 62 |
const ( |
| 63 |
// StackGuardCheckEnabled enables the stack guard check to ensure that our stack bounds check works correctly. |
| 64 |
StackGuardCheckEnabled = false |
| 65 |
StackGuardCheckGuardPageSize = 8096 |
| 66 |
) |
| 67 |
|
| 68 |
// CheckStackGuardPage checks the given stack guard page is not corrupted. |
| 69 |
func CheckStackGuardPage(s []byte) { |
| 70 |
for i := 0; i < StackGuardCheckGuardPageSize; i++ { |
| 71 |
if s[i] != 0 { |
| 72 |
panic( |
| 73 |
fmt.Sprintf("BUG: stack guard page is corrupted:\n\tguard_page=%s\n\tstack=%s", |
| 74 |
hex.EncodeToString(s[:StackGuardCheckGuardPageSize]), |
| 75 |
hex.EncodeToString(s[StackGuardCheckGuardPageSize:]), |
| 76 |
)) |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// ----- Deterministic compilation verifier ----- |
| 82 |
|
| 83 |
const ( |
| 84 |
// DeterministicCompilationVerifierEnabled enables the deterministic compilation verifier. This is disabled by default |
| 85 |
// since the operation is expensive. But when in doubt, enable this to make sure the compilation is deterministic. |
| 86 |
DeterministicCompilationVerifierEnabled = false |
| 87 |
DeterministicCompilationVerifyingIter = 5 |
| 88 |
) |
| 89 |
|
| 90 |
type ( |
| 91 |
verifierState struct { |
| 92 |
initialCompilationDone bool |
| 93 |
maybeRandomizedIndexes []int |
| 94 |
r *rand.Rand |
| 95 |
values sync.Map |
| 96 |
} |
| 97 |
verifierStateContextKey struct{} |
| 98 |
currentFunctionNameKey struct{} |
| 99 |
currentFunctionIndexKey struct{} |
| 100 |
) |
| 101 |
|
| 102 |
// NewDeterministicCompilationVerifierContext creates a new context with the deterministic compilation verifier used per wasm.Module. |
| 103 |
func NewDeterministicCompilationVerifierContext(ctx context.Context, localFunctions int) context.Context { |
| 104 |
maybeRandomizedIndexes := make([]int, localFunctions) |
| 105 |
for i := range maybeRandomizedIndexes { |
| 106 |
maybeRandomizedIndexes[i] = i |
| 107 |
} |
| 108 |
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 109 |
return context.WithValue(ctx, verifierStateContextKey{}, &verifierState{ |
| 110 |
r: r, maybeRandomizedIndexes: maybeRandomizedIndexes, values: sync.Map{}, |
| 111 |
}) |
| 112 |
} |
| 113 |
|
| 114 |
// DeterministicCompilationVerifierRandomizeIndexes randomizes the indexes for the deterministic compilation verifier. |
| 115 |
// Returns a slice that maps an index to the randomized index. |
| 116 |
func DeterministicCompilationVerifierRandomizeIndexes(ctx context.Context) []int { |
| 117 |
state := ctx.Value(verifierStateContextKey{}).(*verifierState) |
| 118 |
if !state.initialCompilationDone { |
| 119 |
// If this is the first attempt, we use the index as-is order. |
| 120 |
state.initialCompilationDone = true |
| 121 |
return state.maybeRandomizedIndexes |
| 122 |
} |
| 123 |
r := state.r |
| 124 |
r.Shuffle(len(state.maybeRandomizedIndexes), func(i, j int) { |
| 125 |
state.maybeRandomizedIndexes[i], state.maybeRandomizedIndexes[j] = state.maybeRandomizedIndexes[j], state.maybeRandomizedIndexes[i] |
| 126 |
}) |
| 127 |
return state.maybeRandomizedIndexes |
| 128 |
} |
| 129 |
|
| 130 |
// VerifyOrSetDeterministicCompilationContextValue verifies that the `newValue` is the same as the previous value for the given `scope` |
| 131 |
// and the current function name. If the previous value doesn't exist, it sets the value to the given `newValue`. |
| 132 |
// |
| 133 |
// If the verification fails, this prints the diff and exits the process. |
| 134 |
func VerifyOrSetDeterministicCompilationContextValue(ctx context.Context, scope string, newValue string) { |
| 135 |
fn := ctx.Value(currentFunctionNameKey{}).(string) |
| 136 |
key := fn + ": " + scope |
| 137 |
verifierCtx := ctx.Value(verifierStateContextKey{}).(*verifierState) |
| 138 |
oldValue, loaded := verifierCtx.values.LoadOrStore(key, newValue) |
| 139 |
if !loaded { |
| 140 |
return |
| 141 |
} |
| 142 |
if oldValue != newValue { |
| 143 |
fmt.Printf( |
| 144 |
`BUG: Deterministic compilation failed for function%s at scope="%s". |
| 145 |
|
| 146 |
This is mostly due to (but might not be limited to): |
| 147 |
* Resetting ssa.Builder, backend.Compiler or frontend.Compiler, etc doens't work as expected, and the compilation has been affected by the previous iterations. |
| 148 |
* Using a map with non-deterministic iteration order. |
| 149 |
|
| 150 |
---------- [old] ---------- |
| 151 |
%s |
| 152 |
|
| 153 |
---------- [new] ---------- |
| 154 |
%s |
| 155 |
`, |
| 156 |
fn, scope, oldValue, newValue, |
| 157 |
) |
| 158 |
os.Exit(1) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// nolint |
| 163 |
const NeedFunctionNameInContext = PrintSSA || |
| 164 |
PrintOptimizedSSA || |
| 165 |
PrintSSAToBackendIRLowering || |
| 166 |
PrintRegisterAllocated || |
| 167 |
PrintFinalizedMachineCode || |
| 168 |
PrintMachineCodeHexPerFunction || |
| 169 |
DeterministicCompilationVerifierEnabled || |
| 170 |
PerfMapEnabled |
| 171 |
|
| 172 |
// SetCurrentFunctionName sets the current function name to the given `functionName`. |
| 173 |
func SetCurrentFunctionName(ctx context.Context, index int, functionName string) context.Context { |
| 174 |
ctx = context.WithValue(ctx, currentFunctionNameKey{}, functionName) |
| 175 |
ctx = context.WithValue(ctx, currentFunctionIndexKey{}, index) |
| 176 |
return ctx |
| 177 |
} |
| 178 |
|
| 179 |
// GetCurrentFunctionName returns the current function name. |
| 180 |
func GetCurrentFunctionName(ctx context.Context) string { |
| 181 |
ret, _ := ctx.Value(currentFunctionNameKey{}).(string) |
| 182 |
return ret |
| 183 |
} |
| 184 |
|
| 185 |
// GetCurrentFunctionIndex returns the current function index. |
| 186 |
func GetCurrentFunctionIndex(ctx context.Context) int { |
| 187 |
ret, _ := ctx.Value(currentFunctionIndexKey{}).(int) |
| 188 |
return ret |
| 189 |
} |