Skip to content

Commit d29aacb

Browse files
authored
Compile SQLite with nostdlib. (#351)
1 parent 30649fa commit d29aacb

58 files changed

Lines changed: 10438 additions & 3215 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/libc.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/workflows/repro.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@ sqlite3/tools.sh
77
embed/build.sh
88
embed/bcw2/build.sh
99

10-
# Download and build sqlite-createtable-parser
11-
util/sql3util/wasm/download.sh
12-
util/sql3util/wasm/build.sh
13-
1410
# Check diffs
1511
git diff --exit-code

.github/workflows/repro.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ jobs:
2828
with:
2929
subject-path: |
3030
embed/sqlite3.wasm
31-
embed/bcw2/bcw2.wasm
32-
util/sql3util/wasm/sql3parse_table.wasm
31+
embed/bcw2/bcw2.wasm

embed/bcw2/bcw2.wasm

9.92 KB
Binary file not shown.

embed/bcw2/build.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ cp "$ROOT"/sqlite3/*.[ch] build/
1515
cp "$ROOT"/sqlite3/*.patch build/
1616
cd sqlite/
1717

18-
# https://sqlite.org/src/info/d577b2a2b2b04ff7
19-
curl -#L https://github.com/sqlite/sqlite/archive/e34ccf8.tar.gz | tar xz --strip-components=1
20-
# curl -#L https://sqlite.org/src/tarball/sqlite.tar.gz?r=d577b2a2b2 | tar xz --strip-components=1
18+
# https://sqlite.org/src/info/0fd87fa08db1caa6
19+
curl -#L https://github.com/sqlite/sqlite/archive/2f7c0da.tar.gz | tar xz --strip-components=1
20+
# curl -#L https://sqlite.org/src/tarball/sqlite.tar.gz?r=0fd87fa08d | tar xz --strip-components=1
2121

2222
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
2323
MSYS_NO_PATHCONV=1 nmake /f makefile.msc sqlite3.c "OPTS=-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES"
@@ -44,7 +44,7 @@ cd build
4444
cat *.patch | patch -p0 --no-backup-if-mismatch
4545
cd ~-
4646

47-
"$WASI_SDK/clang" --target=wasm32-wasi -std=c23 -g0 -O2 \
47+
"$WASI_SDK/clang" --target=wasm32 -nostdlib -std=c23 -g0 -O2 \
4848
-Wall -Wextra -Wno-unused-parameter -Wno-unused-function \
4949
-o bcw2.wasm build/main.c \
5050
-I"$ROOT/sqlite3/libc" -I"build" \

embed/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ WASI_SDK="$ROOT/tools/wasi-sdk/bin"
99

1010
trap 'rm -f sqlite3.tmp' EXIT
1111

12-
"$WASI_SDK/clang" --target=wasm32-wasi -std=c23 -g0 -O2 \
12+
"$WASI_SDK/clang" --target=wasm32 -nostdlib -std=c23 -g0 -O2 \
1313
-Wall -Wextra -Wno-unused-parameter -Wno-unused-function \
1414
-o sqlite3.wasm "$ROOT/sqlite3/main.c" \
1515
-I"$ROOT/sqlite3/libc" -I"$ROOT/sqlite3" \

embed/init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Test_init(t *testing.T) {
1919
if err != nil {
2020
t.Fatal(err)
2121
}
22-
if version != "3.51.2" {
22+
if version != "3.52.0" {
2323
t.Error(version)
2424
}
2525
}

embed/sqlite3.wasm

20.5 KB
Binary file not shown.

internal/util/func.go

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@ package util
22

33
import (
44
"context"
5+
"math"
56

67
"github.com/tetratelabs/wazero"
78
"github.com/tetratelabs/wazero/api"
89
)
910

11+
const (
12+
i32s = "\x7f\x7f\x7f\x7f\x7f\x7f"
13+
i64s = "\x7e"
14+
f64s = "\x7c\x7c"
15+
)
16+
1017
type funcVI[T0 i32] func(context.Context, api.Module, T0)
1118

1219
func (fn funcVI[T0]) Call(ctx context.Context, mod api.Module, stack []uint64) {
1320
fn(ctx, mod, T0(stack[0]))
1421
}
1522

16-
func ExportFuncVI[T0 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0)) {
23+
func ExportFuncVI[T0 i32](mod wazero.HostModuleBuilder, name string, fn funcVI[T0]) {
1724
mod.NewFunctionBuilder().
18-
WithGoModuleFunction(funcVI[T0](fn),
19-
[]api.ValueType{api.ValueTypeI32}, nil).
25+
WithGoModuleFunction(fn, []api.ValueType(i32s[:1]), nil).
2026
Export(name)
2127
}
2228

@@ -27,10 +33,9 @@ func (fn funcVIII[T0, T1, T2]) Call(ctx context.Context, mod api.Module, stack [
2733
fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]))
2834
}
2935

30-
func ExportFuncVIII[T0, T1, T2 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2)) {
36+
func ExportFuncVIII[T0, T1, T2 i32](mod wazero.HostModuleBuilder, name string, fn funcVIII[T0, T1, T2]) {
3137
mod.NewFunctionBuilder().
32-
WithGoModuleFunction(funcVIII[T0, T1, T2](fn),
33-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}, nil).
38+
WithGoModuleFunction(fn, []api.ValueType(i32s[:3]), nil).
3439
Export(name)
3540
}
3641

@@ -41,10 +46,9 @@ func (fn funcVIIII[T0, T1, T2, T3]) Call(ctx context.Context, mod api.Module, st
4146
fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]))
4247
}
4348

44-
func ExportFuncVIIII[T0, T1, T2, T3 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2, T3)) {
49+
func ExportFuncVIIII[T0, T1, T2, T3 i32](mod wazero.HostModuleBuilder, name string, fn funcVIIII[T0, T1, T2, T3]) {
4550
mod.NewFunctionBuilder().
46-
WithGoModuleFunction(funcVIIII[T0, T1, T2, T3](fn),
47-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}, nil).
51+
WithGoModuleFunction(fn, []api.ValueType(i32s[:4]), nil).
4852
Export(name)
4953
}
5054

@@ -55,10 +59,9 @@ func (fn funcVIIIII[T0, T1, T2, T3, T4]) Call(ctx context.Context, mod api.Modul
5559
fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]), T4(stack[4]))
5660
}
5761

58-
func ExportFuncVIIIII[T0, T1, T2, T3, T4 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2, T3, T4)) {
62+
func ExportFuncVIIIII[T0, T1, T2, T3, T4 i32](mod wazero.HostModuleBuilder, name string, fn funcVIIIII[T0, T1, T2, T3, T4]) {
5963
mod.NewFunctionBuilder().
60-
WithGoModuleFunction(funcVIIIII[T0, T1, T2, T3, T4](fn),
61-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}, nil).
64+
WithGoModuleFunction(fn, []api.ValueType(i32s[:5]), nil).
6265
Export(name)
6366
}
6467

@@ -69,120 +72,141 @@ func (fn funcVIIIIJ[T0, T1, T2, T3, T4]) Call(ctx context.Context, mod api.Modul
6972
fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]), T4(stack[4]))
7073
}
7174

72-
func ExportFuncVIIIIJ[T0, T1, T2, T3 i32, T4 i64](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2, T3, T4)) {
75+
func ExportFuncVIIIIJ[T0, T1, T2, T3 i32, T4 i64](mod wazero.HostModuleBuilder, name string, fn funcVIIIIJ[T0, T1, T2, T3, T4]) {
7376
mod.NewFunctionBuilder().
74-
WithGoModuleFunction(funcVIIIIJ[T0, T1, T2, T3, T4](fn),
75-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI64}, nil).
77+
WithGoModuleFunction(fn, []api.ValueType(i32s[:4]+i64s[:1]), nil).
7678
Export(name)
7779
}
7880

7981
type funcII[TR, T0 i32] func(context.Context, api.Module, T0) TR
8082

8183
func (fn funcII[TR, T0]) Call(ctx context.Context, mod api.Module, stack []uint64) {
82-
stack[0] = uint64(fn(ctx, mod, T0(stack[0])))
84+
stack[0] = uint64(uint32(fn(ctx, mod, T0(stack[0]))))
8385
}
8486

85-
func ExportFuncII[TR, T0 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0) TR) {
87+
func ExportFuncII[TR, T0 i32](mod wazero.HostModuleBuilder, name string, fn funcII[TR, T0]) {
8688
mod.NewFunctionBuilder().
87-
WithGoModuleFunction(funcII[TR, T0](fn),
88-
[]api.ValueType{api.ValueTypeI32}, []api.ValueType{api.ValueTypeI32}).
89+
WithGoModuleFunction(fn, []api.ValueType(i32s[:1]), []api.ValueType(i32s[:1])).
8990
Export(name)
9091
}
9192

9293
type funcIII[TR, T0, T1 i32] func(context.Context, api.Module, T0, T1) TR
9394

9495
func (fn funcIII[TR, T0, T1]) Call(ctx context.Context, mod api.Module, stack []uint64) {
9596
_ = stack[1] // prevent bounds check on every slice access
96-
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1])))
97+
stack[0] = uint64(uint32(fn(ctx, mod, T0(stack[0]), T1(stack[1]))))
9798
}
9899

99-
func ExportFuncIII[TR, T0, T1 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1) TR) {
100+
func ExportFuncIII[TR, T0, T1 i32](mod wazero.HostModuleBuilder, name string, fn funcIII[TR, T0, T1]) {
100101
mod.NewFunctionBuilder().
101-
WithGoModuleFunction(funcIII[TR, T0, T1](fn),
102-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{api.ValueTypeI32}).
102+
WithGoModuleFunction(fn, []api.ValueType(i32s[:2]), []api.ValueType(i32s[:1])).
103103
Export(name)
104104
}
105105

106106
type funcIIII[TR, T0, T1, T2 i32] func(context.Context, api.Module, T0, T1, T2) TR
107107

108108
func (fn funcIIII[TR, T0, T1, T2]) Call(ctx context.Context, mod api.Module, stack []uint64) {
109109
_ = stack[2] // prevent bounds check on every slice access
110-
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2])))
110+
stack[0] = uint64(uint32(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]))))
111111
}
112112

113-
func ExportFuncIIII[TR, T0, T1, T2 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2) TR) {
113+
func ExportFuncIIII[TR, T0, T1, T2 i32](mod wazero.HostModuleBuilder, name string, fn funcIIII[TR, T0, T1, T2]) {
114114
mod.NewFunctionBuilder().
115-
WithGoModuleFunction(funcIIII[TR, T0, T1, T2](fn),
116-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{api.ValueTypeI32}).
115+
WithGoModuleFunction(fn, []api.ValueType(i32s[:3]), []api.ValueType(i32s[:1])).
117116
Export(name)
118117
}
119118

120119
type funcIIIII[TR, T0, T1, T2, T3 i32] func(context.Context, api.Module, T0, T1, T2, T3) TR
121120

122121
func (fn funcIIIII[TR, T0, T1, T2, T3]) Call(ctx context.Context, mod api.Module, stack []uint64) {
123122
_ = stack[3] // prevent bounds check on every slice access
124-
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3])))
123+
stack[0] = uint64(uint32(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]))))
125124
}
126125

127-
func ExportFuncIIIII[TR, T0, T1, T2, T3 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2, T3) TR) {
126+
func ExportFuncIIIII[TR, T0, T1, T2, T3 i32](mod wazero.HostModuleBuilder, name string, fn funcIIIII[TR, T0, T1, T2, T3]) {
128127
mod.NewFunctionBuilder().
129-
WithGoModuleFunction(funcIIIII[TR, T0, T1, T2, T3](fn),
130-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{api.ValueTypeI32}).
128+
WithGoModuleFunction(fn, []api.ValueType(i32s[:4]), []api.ValueType(i32s[:1])).
131129
Export(name)
132130
}
133131

134132
type funcIIIIII[TR, T0, T1, T2, T3, T4 i32] func(context.Context, api.Module, T0, T1, T2, T3, T4) TR
135133

136134
func (fn funcIIIIII[TR, T0, T1, T2, T3, T4]) Call(ctx context.Context, mod api.Module, stack []uint64) {
137135
_ = stack[4] // prevent bounds check on every slice access
138-
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]), T4(stack[4])))
136+
stack[0] = uint64(uint32(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]), T4(stack[4]))))
139137
}
140138

141-
func ExportFuncIIIIII[TR, T0, T1, T2, T3, T4 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2, T3, T4) TR) {
139+
func ExportFuncIIIIII[TR, T0, T1, T2, T3, T4 i32](mod wazero.HostModuleBuilder, name string, fn funcIIIIII[TR, T0, T1, T2, T3, T4]) {
142140
mod.NewFunctionBuilder().
143-
WithGoModuleFunction(funcIIIIII[TR, T0, T1, T2, T3, T4](fn),
144-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{api.ValueTypeI32}).
141+
WithGoModuleFunction(fn, []api.ValueType(i32s[:5]), []api.ValueType(i32s[:1])).
145142
Export(name)
146143
}
147144

148145
type funcIIIIIII[TR, T0, T1, T2, T3, T4, T5 i32] func(context.Context, api.Module, T0, T1, T2, T3, T4, T5) TR
149146

150147
func (fn funcIIIIIII[TR, T0, T1, T2, T3, T4, T5]) Call(ctx context.Context, mod api.Module, stack []uint64) {
151148
_ = stack[5] // prevent bounds check on every slice access
152-
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]), T4(stack[4]), T5(stack[5])))
149+
stack[0] = uint64(uint32(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]), T4(stack[4]), T5(stack[5]))))
153150
}
154151

155-
func ExportFuncIIIIIII[TR, T0, T1, T2, T3, T4, T5 i32](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2, T3, T4, T5) TR) {
152+
func ExportFuncIIIIIII[TR, T0, T1, T2, T3, T4, T5 i32](mod wazero.HostModuleBuilder, name string, fn funcIIIIIII[TR, T0, T1, T2, T3, T4, T5]) {
156153
mod.NewFunctionBuilder().
157-
WithGoModuleFunction(funcIIIIIII[TR, T0, T1, T2, T3, T4, T5](fn),
158-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}, []api.ValueType{api.ValueTypeI32}).
154+
WithGoModuleFunction(fn, []api.ValueType(i32s[:6]), []api.ValueType(i32s[:1])).
159155
Export(name)
160156
}
161157

162158
type funcIIIIJ[TR, T0, T1, T2 i32, T3 i64] func(context.Context, api.Module, T0, T1, T2, T3) TR
163159

164160
func (fn funcIIIIJ[TR, T0, T1, T2, T3]) Call(ctx context.Context, mod api.Module, stack []uint64) {
165161
_ = stack[3] // prevent bounds check on every slice access
166-
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3])))
162+
stack[0] = uint64(uint32(fn(ctx, mod, T0(stack[0]), T1(stack[1]), T2(stack[2]), T3(stack[3]))))
167163
}
168164

169-
func ExportFuncIIIIJ[TR, T0, T1, T2 i32, T3 i64](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1, T2, T3) TR) {
165+
func ExportFuncIIIIJ[TR, T0, T1, T2 i32, T3 i64](mod wazero.HostModuleBuilder, name string, fn funcIIIIJ[TR, T0, T1, T2, T3]) {
170166
mod.NewFunctionBuilder().
171-
WithGoModuleFunction(funcIIIIJ[TR, T0, T1, T2, T3](fn),
172-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI64}, []api.ValueType{api.ValueTypeI32}).
167+
WithGoModuleFunction(fn, []api.ValueType(i32s[:3]+i64s[:1]), []api.ValueType(i32s[:1])).
173168
Export(name)
174169
}
175170

176171
type funcIIJ[TR, T0 i32, T1 i64] func(context.Context, api.Module, T0, T1) TR
177172

178173
func (fn funcIIJ[TR, T0, T1]) Call(ctx context.Context, mod api.Module, stack []uint64) {
179174
_ = stack[1] // prevent bounds check on every slice access
180-
stack[0] = uint64(fn(ctx, mod, T0(stack[0]), T1(stack[1])))
175+
stack[0] = uint64(uint32(fn(ctx, mod, T0(stack[0]), T1(stack[1]))))
176+
}
177+
178+
func ExportFuncIIJ[TR, T0 i32, T1 i64](mod wazero.HostModuleBuilder, name string, fn funcIIJ[TR, T0, T1]) {
179+
mod.NewFunctionBuilder().
180+
WithGoModuleFunction(fn, []api.ValueType(i32s[:1]+i64s[:1]), []api.ValueType(i32s[:1])).
181+
Export(name)
182+
}
183+
184+
type flt = interface{ ~float32 | ~float64 }
185+
186+
type funcDD[TR, T0 flt] func(T0) TR
187+
188+
func (fn funcDD[TR, T0]) Call(ctx context.Context, mod api.Module, stack []uint64) {
189+
stack[0] = math.Float64bits(float64(fn(
190+
T0(math.Float64frombits(stack[0])))))
191+
}
192+
193+
func ExportFuncDD[TR, T0 flt](mod wazero.HostModuleBuilder, name string, fn funcDD[TR, T0]) {
194+
mod.NewFunctionBuilder().
195+
WithGoModuleFunction(fn, []api.ValueType(f64s[:1]), []api.ValueType(f64s[:1])).
196+
Export(name)
197+
}
198+
199+
type funcDDD[TR, T0, T1 flt] func(T0, T1) TR
200+
201+
func (fn funcDDD[TR, T0, T1]) Call(ctx context.Context, mod api.Module, stack []uint64) {
202+
_ = stack[1] // prevent bounds check on every slice access
203+
stack[0] = math.Float64bits(float64(fn(
204+
T0(math.Float64frombits(stack[0])),
205+
T1(math.Float64frombits(stack[1])))))
181206
}
182207

183-
func ExportFuncIIJ[TR, T0 i32, T1 i64](mod wazero.HostModuleBuilder, name string, fn func(context.Context, api.Module, T0, T1) TR) {
208+
func ExportFuncDDD[TR, T0, T1 flt](mod wazero.HostModuleBuilder, name string, fn funcDDD[TR, T0, T1]) {
184209
mod.NewFunctionBuilder().
185-
WithGoModuleFunction(funcIIJ[TR, T0, T1](fn),
186-
[]api.ValueType{api.ValueTypeI32, api.ValueTypeI64}, []api.ValueType{api.ValueTypeI32}).
210+
WithGoModuleFunction(fn, []api.ValueType(f64s[:2]), []api.ValueType(f64s[:1])).
187211
Export(name)
188212
}

internal/util/mem.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const (
1313
)
1414

1515
type (
16-
i8 interface{ ~int8 | ~uint8 }
17-
i32 interface{ ~int32 | ~uint32 }
18-
i64 interface{ ~int64 | ~uint64 }
16+
i8 = interface{ ~int8 | ~uint8 }
17+
i32 = interface{ ~int32 | ~uint32 }
18+
i64 = interface{ ~int64 | ~uint64 }
1919

2020
Stk_t = uint64
2121
Ptr_t uint32

0 commit comments

Comments
 (0)