-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
any-wrap.rkt
311 lines (294 loc) · 12.7 KB
/
any-wrap.rkt
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
#lang racket/base
(require racket/match racket/contract/combinator
racket/class racket/unit
racket/fixnum racket/flonum racket/extflonum
racket/set
racket/undefined
(only-in racket/async-channel async-channel?)
(only-in ffi/unsafe cpointer-predicate-procedure?)
(only-in racket/future future? fsemaphore?)
(only-in racket/pretty pretty-print-style-table?)
(only-in racket/udp udp?)
(only-in (combine-in racket/private/promise)
promise?
prop:force promise-forcer))
(define (base-val? e)
(or (number? e) (string? e) (char? e) (symbol? e)
(null? e) (eq? undefined e) (path? e) (eof-object? e)
(regexp? e) (pregexp? e) (byte-regexp? e) (byte-pregexp? e)
(keyword? e) (bytes? e) (boolean? e) (void? e)
(bytes-converter? e)
(impersonator-property? e)
(inspector? e)
(logger? e)
(module-path? e) (resolved-module-path? e)
(pretty-print-style-table? e)
(pseudo-random-generator? e)
(semaphore? e) (fsemaphore? e)
(thread-group? e)
(udp? e)
;; Base since they can only store strings
;; Bounded polymorphism would again make these not flat,
;; but would be unsound since the external OS can change them
(environment-variables? e)
;; Base values because you can only store flonums/fixnums in these
;; and not any higher-order values. This isn't sound if we ever
;; introduce bounded polymorphism for Flvector/Fxvector.
(flvector? e) (fxvector? e) (extflvector? e) (extflonum? e)))
(define (unsafe-val? e)
(or ;; TODO: async-channel and special-comment should be safe
(async-channel? e)
(special-comment? e)
;; --
(class? e)
(compiled-expression? e)
(compiled-module-expression? e)
(continuation-mark-key? e) ;; Stricter than necessary if key holds a base value
(continuation-mark-set? e)
(continuation-prompt-tag? e)
(custodian-box? e)
(custodian? e)
(ephemeron? e)
(future? e)
(internal-definition-context? e)
(mpair? e)
(namespace-anchor? e)
(namespace? e)
(parameterization? e)
(security-guard? e)
(struct-type-property? e)
(syntax? e)
(thread-cell? e)
(unit? e)
(variable-reference? e)
(weak-box? e)))
;; late-neg-projection :
;; (-> #:on-opaque (-> Val Blame Neg-Party (U Val Error))
;; (-> Blame
;; (-> Val Neg-Party Val)))
(define ((late-neg-projection #:on-opaque on-opaque) b)
(define (fail neg-party v)
(raise-blame-error
(blame-swap b) #:missing-party neg-party
v
"Attempted to use a higher-order value passed as `Any` in untyped code: ~v" v))
(define (wrap-struct neg-party s seen [inspector (current-inspector)])
(define blame+neg-party (cons b neg-party))
(define (extract-functions struct-type)
(define-values (sym init auto ref set! imms par skip?)
(parameterize ([current-inspector inspector])
(struct-type-info struct-type)))
(define-values (fun/chap-list _)
(for/fold ([res null]
[imms imms])
([n (in-range (+ init auto))])
(if (and (pair? imms) (= (car imms) n))
;; field is immutable
(values
(list* (make-struct-field-accessor ref n)
(lambda (s v) (with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse v neg-party seen)))
res)
(cdr imms))
;; field is mutable
(values
(list* (make-struct-field-accessor ref n)
(lambda (s v) (with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse v neg-party seen)))
(make-struct-field-mutator set! n)
(lambda (s v) (fail neg-party s))
res)
imms))))
(cond
[par (append fun/chap-list (extract-functions par))]
[else fun/chap-list]))
(define-values (type skipped?)
(parameterize ([current-inspector inspector])
(struct-info s)))
;; It's ok to just ignore skipped? -- see https://github.com/racket/typed-racket/issues/203
(apply chaperone-struct s (extract-functions type)))
(define (any-wrap/traverse v neg-party [seen (seteq)])
(define blame+neg-party (cons b neg-party))
(define seen/v (set-add seen v))
(match v
[(? (λ (v) (set-member? seen v)))
;; TODO handle cycles with a correct deep copy (github issue #823)
(raise-argument-error 'any-wrap/c "acyclic value" 0 v neg-party)]
[(? base-val?)
v]
[(? unsafe-val?)
(fail neg-party v)]
[(cons x y)
(define x* (any-wrap/traverse x neg-party seen/v))
(define y* (any-wrap/traverse y neg-party seen/v))
(cond
[(and (eq? x x*) (eq? y y*)) v]
[else (cons x* y*)])]
[(? vector? (? immutable?))
;; fixme -- should have an immutable for/vector
(define all-eq? #t)
(define v*
(for/vector #:length (vector-length v) ([i (in-vector v)])
(define i* (any-wrap/traverse i neg-party seen/v))
(unless (eq? i i*)
(set! all-eq? #f))
i*))
(if all-eq? v (vector->immutable-vector v*))]
[(? box? (? immutable?))
(define v* (any-wrap/traverse (unbox v) neg-party seen/v))
(cond
[(eq? v* (unbox v)) v]
[else (box-immutable v*)])]
[(? box?) (chaperone-box v
(lambda (v e)
(with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse e neg-party seen/v)))
(lambda (v e) (fail neg-party v)))]
;; fixme -- handling keys properly makes it not a chaperone
;; [(? hasheq? (? immutable?))
;; (for/hasheq ([(k v) (in-hash v)]) (values k v))]
;; [(? hasheqv? (? immutable?))
;; (for/hasheqv ([(k v) (in-hash v)]) (values k v))]
[(? (λ (e) (and (hash? e) (immutable? e))))
(define all-eq? #t)
(define v*
(hash-map/copy v
(lambda (k v)
(define k* (any-wrap/traverse k neg-party seen/v))
(define v* (any-wrap/traverse v neg-party seen/v))
(unless (and (eq? k k*) (eq? v v*))
(set! all-eq? #f))
(values k* v*))))
(if all-eq? v v*)]
[(? vector?) (chaperone-vector v
(lambda (v i e)
(with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse e neg-party seen/v)))
(lambda (v i e) (fail neg-party v)))]
[(? hash?) (chaperone-hash v
(lambda (h k)
(values k (lambda (h k v)
(with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse v neg-party seen/v))))) ;; ref
(lambda (h k n)
(if (immutable? v)
(values k n)
(fail neg-party v))) ;; set
(lambda (h v) v) ;; remove
(lambda (h k)
(with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse k neg-party seen/v))))] ;; key
[(? set?) (chaperone-hash-set
v
(λ (s e) e) ; inject
(λ (s e) (if (immutable? v)
e
(fail neg-party v))) ; add
(λ (s e) e) ; remove
(λ (s e) (with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse e neg-party seen/v))))] ; extract
;; could do something with generic sets here if they had
;; chaperones, or if i could tell if they were immutable.
[(? struct?)
(wrap-struct neg-party v seen/v)]
[(? procedure?)
(chaperone-procedure v (lambda args (fail neg-party v)))]
[(? promise?)
(chaperone-struct
v
promise-forcer
(λ (_ proc)
(chaperone-procedure
proc
(λ (promise)
(values (λ (val) (with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse val neg-party seen/v)))
promise)))))]
[(? channel?)
;;bg; Should be able to take `Any` from the channel, but can't put anything in
(chaperone-channel v
(lambda (e) (with-contract-continuation-mark
blame+neg-party
(values e (lambda (inner-val) (any-wrap/traverse inner-val neg-party seen/v)))))
(lambda (_e _new-val) (fail neg-party v)))]
[(? evt?)
;; must come after cases for write-able values that can be used as events
(chaperone-evt v (lambda (e) (values e (λ (v)
(with-contract-continuation-mark
blame+neg-party
(any-wrap/traverse v neg-party seen/v))))))]
[_
(on-opaque v b neg-party)]))
any-wrap/traverse)
;; on-opaque-error : Val Blame Neg-Party -> Error
;; To be passed as the #:on-opaque argument to make any-wrap/c raise
;; an error on opaque values.
(define (on-opaque-error v blame neg-party)
(raise-any-wrap/c-opaque-error v blame neg-party))
;; make-on-opaque-display-warning : (-> (Val Blame Neg-Party -> Val))
;; To be passed as the #:on-opaque argument to make any-wrap/c display
;; a warning, but keep going with possible unsoundness.
(define (make-on-opaque-display-warning)
(define display-warning (make-display-any-wrap/c-opaque-warning))
(lambda (v blame neg-party)
;; this can lead to unsoundness, see https://github.com/racket/typed-racket/issues/379.
;; an error here would make this sound, but it breaks the math library as of 2016-07-08,
;; see https://github.com/racket/typed-racket/pull/385#issuecomment-231354377.
(display-warning v blame neg-party)
(chaperone-struct v)))
;; make-any-wrap/c : (-> #:on-opaque (-> Val Blame Neg-Party (U Val Error)) Chaperone-Contract)
(define (make-any-wrap/c #:on-opaque on-opaque)
(make-chaperone-contract
#:name 'Any
#:first-order (lambda (x) #t)
#:late-neg-projection (late-neg-projection #:on-opaque on-opaque)))
(define any-wrap/c
(make-any-wrap/c #:on-opaque on-opaque-error))
(define (make-any-wrap-warning/c)
(make-any-wrap/c #:on-opaque (make-on-opaque-display-warning)))
;; struct?/inspector : (-> Inspector (-> Any Boolean))
(define ((struct?/inspector inspector) v)
(parameterize ([current-inspector inspector])
(struct? v)))
;; raise-any-wrap/c-opaque-error : Any Blame Neg-Party -> Error
(define (raise-any-wrap/c-opaque-error v blame neg-party)
(raise-blame-error
blame #:missing-party neg-party
v
(string-append
"any-wrap/c: Unable to protect opaque value passed as `Any`\n"
" value: ~e\n")
v))
;; make-display-any-wrap/c-opaque-warning : (-> (-> Any Blame Neg-Party Void))
(define (make-display-any-wrap/c-opaque-warning)
(define seen (make-hasheq))
(lambda (v blame neg-party)
(unless (hash-has-key? seen v)
(hash-set! seen v #true)
(displayln
((current-blame-format)
(blame-add-missing-party blame neg-party)
v
(format
(string-append
"any-wrap/c: Unable to protect opaque value passed as `Any`\n"
" value: ~e\n"
" This warning will become an error in a future release.\n")
v))
(current-error-port)))))
;; Contract for "safe" struct predicate procedures.
;; We can trust that these obey the type (-> Any Boolean).
(define (struct-predicate-procedure?/c x)
(and (or (struct-predicate-procedure? x)
(cpointer-predicate-procedure? x)
(struct-type-property-predicate-procedure? x))
(not (impersonator? x))))
(provide any-wrap/c make-any-wrap-warning/c struct-predicate-procedure?/c)