Skip to content

Commit 486024c

Browse files
authored
Align preambles, trampolines to 16 bytes. (#2432)
Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
1 parent 301b9c0 commit 486024c

File tree

2 files changed

+80
-49
lines changed

2 files changed

+80
-49
lines changed

internal/engine/wazevo/engine.go

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ func (exec *executables) compileEntryPreambles(m *wasm.Module, machine backend.M
187187
be.Init()
188188
buf := machine.CompileEntryPreamble(&sig)
189189
preambles = append(preambles, buf...)
190-
sizes[i] = len(buf)
190+
align := 15 & -len(preambles) // Align 16-bytes boundary.
191+
preambles = append(preambles, make([]byte, align)...)
192+
sizes[i] = len(buf) + align
191193
}
192194

193195
exec.entryPreambles = mmapExecutable(preambles)
@@ -725,88 +727,70 @@ func (e *engine) compileSharedFunctions() {
725727
var sizes [8]int
726728
var trampolines []byte
727729

730+
addTrampoline := func(i int, buf []byte) {
731+
trampolines = append(trampolines, buf...)
732+
align := 15 & -len(trampolines) // Align 16-bytes boundary.
733+
trampolines = append(trampolines, make([]byte, align)...)
734+
sizes[i] = len(buf) + align
735+
}
736+
728737
e.be.Init()
729-
{
730-
src := e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeGrowMemory, &ssa.Signature{
738+
addTrampoline(0,
739+
e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeGrowMemory, &ssa.Signature{
731740
Params: []ssa.Type{ssa.TypeI64 /* exec context */, ssa.TypeI32},
732741
Results: []ssa.Type{ssa.TypeI32},
733-
}, false)
734-
trampolines = append(trampolines, src...)
735-
sizes[0] = len(src)
736-
}
742+
}, false))
737743

738744
e.be.Init()
739-
{
740-
src := e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeTableGrow, &ssa.Signature{
745+
addTrampoline(1,
746+
e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeTableGrow, &ssa.Signature{
741747
Params: []ssa.Type{ssa.TypeI64 /* exec context */, ssa.TypeI32 /* table index */, ssa.TypeI32 /* num */, ssa.TypeI64 /* ref */},
742748
Results: []ssa.Type{ssa.TypeI32},
743-
}, false)
744-
trampolines = append(trampolines, src...)
745-
sizes[1] = len(src)
746-
}
749+
}, false))
747750

748751
e.be.Init()
749-
{
750-
src := e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeCheckModuleExitCode, &ssa.Signature{
752+
addTrampoline(2,
753+
e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeCheckModuleExitCode, &ssa.Signature{
751754
Params: []ssa.Type{ssa.TypeI32 /* exec context */},
752755
Results: []ssa.Type{ssa.TypeI32},
753-
}, false)
754-
trampolines = append(trampolines, src...)
755-
sizes[2] = len(src)
756-
}
756+
}, false))
757757

758758
e.be.Init()
759-
{
760-
src := e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeRefFunc, &ssa.Signature{
759+
addTrampoline(3,
760+
e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeRefFunc, &ssa.Signature{
761761
Params: []ssa.Type{ssa.TypeI64 /* exec context */, ssa.TypeI32 /* function index */},
762762
Results: []ssa.Type{ssa.TypeI64}, // returns the function reference.
763-
}, false)
764-
trampolines = append(trampolines, src...)
765-
sizes[3] = len(src)
766-
}
763+
}, false))
767764

768765
e.be.Init()
769-
{
770-
src := e.machine.CompileStackGrowCallSequence()
771-
trampolines = append(trampolines, src...)
772-
sizes[4] = len(src)
773-
}
766+
addTrampoline(4, e.machine.CompileStackGrowCallSequence())
774767

775768
e.be.Init()
776-
{
777-
src := e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeMemoryWait32, &ssa.Signature{
769+
addTrampoline(5,
770+
e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeMemoryWait32, &ssa.Signature{
778771
// exec context, timeout, expected, addr
779772
Params: []ssa.Type{ssa.TypeI64, ssa.TypeI64, ssa.TypeI32, ssa.TypeI64},
780773
// Returns the status.
781774
Results: []ssa.Type{ssa.TypeI32},
782-
}, false)
783-
trampolines = append(trampolines, src...)
784-
sizes[5] = len(src)
785-
}
775+
}, false))
786776

787777
e.be.Init()
788-
{
789-
src := e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeMemoryWait64, &ssa.Signature{
778+
addTrampoline(6,
779+
e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeMemoryWait64, &ssa.Signature{
790780
// exec context, timeout, expected, addr
791781
Params: []ssa.Type{ssa.TypeI64, ssa.TypeI64, ssa.TypeI64, ssa.TypeI64},
792782
// Returns the status.
793783
Results: []ssa.Type{ssa.TypeI32},
794-
}, false)
795-
trampolines = append(trampolines, src...)
796-
sizes[6] = len(src)
797-
}
784+
}, false))
798785

799786
e.be.Init()
800-
{
801-
src := e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeMemoryNotify, &ssa.Signature{
787+
addTrampoline(7,
788+
e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeMemoryNotify, &ssa.Signature{
802789
// exec context, count, addr
803790
Params: []ssa.Type{ssa.TypeI64, ssa.TypeI32, ssa.TypeI64},
804791
// Returns the number notified.
805792
Results: []ssa.Type{ssa.TypeI32},
806-
}, false)
807-
trampolines = append(trampolines, src...)
808-
sizes[7] = len(src)
809-
}
793+
}, false))
810794

811795
fns := &sharedFunctions{
812796
executable: mmapExecutable(trampolines),
@@ -916,7 +900,9 @@ func (e *engine) getListenerTrampolineForType(functionType *wasm.FunctionType) (
916900
buf := e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeCallListenerBefore, beforeSig, false)
917901
executable = append(executable, buf...)
918902

919-
offset := len(buf)
903+
align := 15 & -len(executable) // Align 16-bytes boundary.
904+
executable = append(executable, make([]byte, align)...)
905+
offset := len(executable)
920906

921907
e.be.Init()
922908
buf = e.machine.CompileGoFunctionTrampoline(wazevoapi.ExitCodeCallListenerAfter, afterSig, false)

internal/engine/wazevo/engine_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,51 @@ func TestEngine_CompileModule(t *testing.T) {
8080
}
8181
}
8282

83+
func TestEngine_CompileModule_alignment(t *testing.T) {
84+
ctx := context.Background()
85+
e := NewEngine(ctx, 0, nil).(*engine)
86+
87+
okModule := &wasm.Module{
88+
TypeSection: []wasm.FunctionType{{}},
89+
FunctionSection: []wasm.Index{0, 0, 0, 0},
90+
CodeSection: []wasm.Code{
91+
{Body: []byte{wasm.OpcodeEnd}},
92+
{Body: []byte{wasm.OpcodeEnd}},
93+
{Body: []byte{wasm.OpcodeEnd}},
94+
{Body: []byte{wasm.OpcodeEnd}},
95+
},
96+
ID: wasm.ModuleID{},
97+
}
98+
99+
err := e.CompileModule(ctx, okModule, nil, false)
100+
require.NoError(t, err)
101+
102+
cm, ok := e.getCompiledModuleFromMemory(okModule)
103+
require.True(t, ok)
104+
105+
for _, offset := range cm.functionOffsets {
106+
require.True(t, offset&15 == 0)
107+
}
108+
109+
for _, ptr := range cm.entryPreamblesPtrs {
110+
require.True(t, uintptr(unsafe.Pointer(ptr))&15 == 0)
111+
}
112+
113+
shared := cm.sharedFunctions
114+
require.True(t, uintptr(unsafe.Pointer(shared.memoryGrowAddress))&15 == 0)
115+
require.True(t, uintptr(unsafe.Pointer(shared.checkModuleExitCodeAddress))&15 == 0)
116+
require.True(t, uintptr(unsafe.Pointer(shared.stackGrowAddress))&15 == 0)
117+
require.True(t, uintptr(unsafe.Pointer(shared.tableGrowAddress))&15 == 0)
118+
require.True(t, uintptr(unsafe.Pointer(shared.refFuncAddress))&15 == 0)
119+
require.True(t, uintptr(unsafe.Pointer(shared.memoryWait32Address))&15 == 0)
120+
require.True(t, uintptr(unsafe.Pointer(shared.memoryWait64Address))&15 == 0)
121+
require.True(t, uintptr(unsafe.Pointer(shared.memoryNotifyAddress))&15 == 0)
122+
for _, trampoline := range shared.listenerTrampolines {
123+
require.True(t, uintptr(unsafe.Pointer(trampoline.before))&15 == 0)
124+
require.True(t, uintptr(unsafe.Pointer(trampoline.after))&15 == 0)
125+
}
126+
}
127+
83128
func TestEngine_sortedCompiledModules(t *testing.T) {
84129
requireEqualExisting := func(t *testing.T, e *engine, expected []uintptr) {
85130
actual := make([]uintptr, 0)

0 commit comments

Comments
 (0)