Repository navigation
Expand file tree
/
Copy pathfunction.go
More file actions
316 lines (276 loc) · 10.3 KB
/
Copy pathfunction.go
File metadata and controls
316 lines (276 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package sqlite
import (
"database/sql"
"database/sql/driver"
"fmt"
"sync"
"time"
"github.com/1set/starlet/dataconv"
"go.starlark.net/starlark"
"modernc.org/sqlite"
)
// ============================================================================
// Global Function Registry
// ============================================================================
// registeredFunction holds information about a registered custom function.
type registeredFunction struct {
name string
starlarkFunc starlark.Callable
numArgs int32 // -1 for variadic
deterministic bool
}
var (
// Global registry for custom functions
registeredFuncs = make(map[string]*registeredFunction)
funcMutex sync.RWMutex
)
// localDriverName is the database/sql driver name connect() opens through: a
// funcMutex-serialized wrapper over modernc's "sqlite" driver (see below).
const localDriverName = "sqlite-udf-serialized"
// noFunctionsDriverName uses an independent, immutable modernc driver with no
// registered custom functions, collations, or connection hooks.
const noFunctionsDriverName = "sqlite-no-custom-functions"
var registerLocalDriverOnce sync.Once
// ensureLocalDriver registers the funcMutex-serialized wrapper over modernc's
// "sqlite" driver, exactly once.
//
// modernc's Driver.Open reads its process-global user-defined-function map
// (d.udfs) with NO lock, while RegisterScalarFunction writes it.
// register_function serializes writes under funcMutex, but a connection's Open —
// called lazily by database/sql, possibly from another host machine running in
// parallel — reads d.udfs unlocked, so a concurrent register_function + connect
// is a data race (potentially a fatal concurrent-map access). Opening through
// this wrapper makes every Open hold funcMutex for reading, serialized against
// register_function's write.
//
// Scope: this serializes THIS module's register_function against THIS module's
// own connections (connect opens through localDriverName). It does not, and
// cannot, protect a different component in the same process that opens the raw
// "sqlite" driver directly, nor concurrent registration of modernc collations /
// connection hooks — this module registers neither, and those globals are
// modernc's own unlocked state. A modernc connection hook that reenters
// register_function while a wrapped Open holds the read lock would deadlock;
// this module registers no connection hooks, so that path is unreachable here.
func ensureLocalDriver() {
registerLocalDriverOnce.Do(func() {
// A lazily-opened handle purely to obtain the registered driver instance
// (Open is not called here, so the empty DSN is never used).
db, err := sql.Open("sqlite", "")
if err != nil {
return // "sqlite" is always registered by the modernc import
}
inner := db.Driver()
_ = db.Close()
sql.Register(localDriverName, &udfSerializedDriver{inner: inner})
sql.Register(noFunctionsDriverName, &sqlite.Driver{})
})
}
// udfSerializedDriver wraps modernc's driver so a connection Open (which reads
// the global UDF map) is serialized against register_function (which writes it).
type udfSerializedDriver struct{ inner driver.Driver }
// Open holds funcMutex for reading while the wrapped driver reads its global UDF
// map. register_function holds it for writing, so the two never run concurrently;
// concurrent Opens still proceed in parallel (a read lock).
func (d *udfSerializedDriver) Open(name string) (driver.Conn, error) {
funcMutex.RLock()
defer funcMutex.RUnlock()
return d.inner.Open(name)
}
// ============================================================================
// Function Registration
// ============================================================================
// registerFunction enforces the host policy before touching the global registry.
func (m *Module) registerFunction(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if m.disableCustomFunctions {
return nil, fmt.Errorf("custom functions are disabled by the host")
}
return registerFunction(thread, fn, args, kwargs)
}
// registerFunction implements the register_function Starlark builtin.
func registerFunction(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name string
var funcVal starlark.Callable
var numArgs starlark.Value = starlark.None
var deterministic bool = false
if err := starlark.UnpackArgs(fn.Name(), args, kwargs,
"name", &name,
"func", &funcVal,
"num_args?", &numArgs,
"deterministic?", &deterministic,
); err != nil {
return nil, err
}
// Validate function name
if name == "" {
return nil, fmt.Errorf("function name cannot be empty")
}
// Validate callable
if funcVal == nil {
return nil, fmt.Errorf("function cannot be nil")
}
// Parse num_args
var numArgsInt int32 = -1 // Default to variadic
if numArgs != starlark.None {
if intVal, ok := numArgs.(starlark.Int); ok {
val, _ := intVal.Int64()
if val < -1 {
return nil, fmt.Errorf("num_args must be >= -1 (got %d)", val)
}
numArgsInt = int32(val)
} else {
return nil, fmt.Errorf("num_args must be an integer")
}
}
// Register the function
if err := doRegisterFunction(name, funcVal, numArgsInt, deterministic); err != nil {
return nil, err
}
return starlark.None, nil
}
// doRegisterFunction performs the actual function registration.
func doRegisterFunction(name string, funcVal starlark.Callable, numArgs int32, deterministic bool) error {
funcMutex.Lock()
defer funcMutex.Unlock()
// Check if function already exists
if _, exists := registeredFuncs[name]; exists {
return fmt.Errorf("function '%s' is already registered", name)
}
// Store in global registry
regFunc := ®isteredFunction{
name: name,
starlarkFunc: funcVal,
numArgs: numArgs,
deterministic: deterministic,
}
registeredFuncs[name] = regFunc
// Create Go wrapper function
wrapper := createGoFunctionWrapper(regFunc)
// Register with SQLite driver
var err error
if deterministic {
err = sqlite.RegisterDeterministicScalarFunction(name, numArgs, wrapper)
} else {
err = sqlite.RegisterScalarFunction(name, numArgs, wrapper)
}
if err != nil {
// Remove from registry if SQLite registration failed
delete(registeredFuncs, name)
return fmt.Errorf("failed to register function with SQLite driver: %w", err)
}
return nil
}
// ============================================================================
// Go Function Wrapper Creation
// ============================================================================
// createGoFunctionWrapper creates a Go function that bridges Starlark to SQLite.
func createGoFunctionWrapper(regFunc *registeredFunction) func(ctx *sqlite.FunctionContext, args []driver.Value) (driver.Value, error) {
return func(ctx *sqlite.FunctionContext, args []driver.Value) (retVal driver.Value, retErr error) {
// A custom function runs arbitrary Starlark inside the SQLite driver's
// scalar-function callback. Recover any panic and surface it as an error
// so script input can never crash the host (hardening invariant).
defer func() {
if r := recover(); r != nil {
retVal = nil
retErr = fmt.Errorf("custom function %q panicked: %v", regFunc.name, r)
}
}()
// Create new thread for this function call
thread := &starlark.Thread{
Name: fmt.Sprintf("custom_function_%s", regFunc.name),
}
// Convert arguments from driver.Value to Starlark values
starlarkArgs := make([]starlark.Value, len(args))
for i, arg := range args {
val, err := driverValueToStarlark(arg)
if err != nil {
return nil, fmt.Errorf("failed to convert argument %d: %w", i, err)
}
starlarkArgs[i] = val
}
// Call the Starlark function
argTuple := starlark.Tuple(starlarkArgs)
result, err := starlark.Call(thread, regFunc.starlarkFunc, argTuple, nil)
if err != nil {
return nil, fmt.Errorf("Starlark function execution failed: %w", err)
}
// Convert result back to driver.Value
driverResult, err := starlarkToDriverValue(result)
if err != nil {
return nil, fmt.Errorf("failed to convert result: %w", err)
}
return driverResult, nil
}
}
// ============================================================================
// Type Conversion Functions
// ============================================================================
// driverValueToStarlark converts a SQLite driver.Value to a Starlark value.
func driverValueToStarlark(val driver.Value) (starlark.Value, error) {
if val == nil {
return starlark.None, nil
}
switch v := val.(type) {
case bool:
return starlark.Bool(v), nil
case int64:
return starlark.MakeInt64(v), nil
case float64:
return starlark.Float(v), nil
case string:
return starlark.String(v), nil
case []byte:
return starlark.Bytes(v), nil
case time.Time:
// Convert time.Time to string representation
return starlark.String(v.String()), nil
default:
// Try to handle other types by converting to string
return starlark.String(fmt.Sprintf("%v", v)), nil
}
}
// starlarkToDriverValue converts a Starlark value to a SQLite driver.Value.
func starlarkToDriverValue(val starlark.Value) (driver.Value, error) {
if val == nil || val == starlark.None {
return nil, nil
}
switch v := val.(type) {
case starlark.Bool:
return bool(v), nil
case starlark.Int:
// Mirror the bind-parameter path (starlarkToSQLiteValue): an int that
// does not fit int64 must surface a clear error, never silently
// truncate to a wrong driver value.
i64, ok := v.Int64()
if !ok {
return nil, fmt.Errorf("int value too large for SQLite: %v", v)
}
return i64, nil
case starlark.Float:
return float64(v), nil
case starlark.String:
return string(v), nil
case starlark.Bytes:
return []byte(v), nil
default:
// For complex types (dict, list, tuple), use the existing dataconv utility
// which converts them to JSON strings
if isComplexType(val) {
jsonStr, err := dataconv.MarshalStarlarkJSON(val, 0)
if err != nil {
return nil, fmt.Errorf("failed to convert complex type to JSON: %w", err)
}
return jsonStr, nil
}
// For other types, convert to string
return val.String(), nil
}
}
// isComplexType checks if a Starlark value is a complex type that should be JSON-encoded.
func isComplexType(val starlark.Value) bool {
switch val.(type) {
case *starlark.Dict, *starlark.List, starlark.Tuple:
return true
default:
return false
}
}