| 1 | # Bin_prot_shape |
| 2 | |
| 3 | What is `Bin_prot_shape`? |
| 4 | ========================= |
| 5 | |
| 6 | An extension to `bin_prot` to check safe use of deserialization. |
| 7 | |
| 8 | The `bin_prot` library supports reading and writing OCaml-values via a |
| 9 | binary protocol. Serialization & deserialization is performed by code |
| 10 | generated from the `[@@deriving bin_io]` syntax extension. However |
| 11 | the type safety only pertains if the serialization & deserialization |
| 12 | are using the same OCaml type definitions. |
| 13 | |
| 14 | Since the readers & writer may well be running in different processes, |
| 15 | in different executables, compiled and installed at different times, |
| 16 | from different OCaml code bases - it is very difficult to be sure that |
| 17 | the OCaml types are the same. |
| 18 | |
| 19 | Values deserialized at the wrong type are essentially garbage, and |
| 20 | their use is unsafe. |
| 21 | |
| 22 | The idea of `bin_prot_shape` is to generate a `Bin_prot.Shape.t` |
| 23 | description for every `Binable` type, so that shape equivalence |
| 24 | guarantees safety of `bin_prot`. |
| 25 | |
| 26 | Readers & writers can exchange a `Shape.Digest.t` to ensure both sides |
| 27 | have the same opinion of the types which will be communicated. |
| 28 | |
| 29 | ## Two uses of shapes |
| 30 | |
| 31 | * We can write unit tests for the expected shapes of types transmitted |
| 32 | over a given protocol. These would fail if the shape changes, |
| 33 | perhaps unexpectedly, giving a clear indication that the protocol |
| 34 | version must be incremented. |
| 35 | |
| 36 | * We extend `async_rpc` to check dynamically that the protocol |
| 37 | expected by client and server are shape-compatible. Shape |
| 38 | incompatibility prevents communication, and fails in a well-defined |
| 39 | manner. |
| 40 | |
| 41 | Motivation for shape incompatibility |
| 42 | ==================================== |
| 43 | |
| 44 | ## Reordering Record field |
| 45 | |
| 46 | Record type definitions that differ only in the field order are |
| 47 | serialized differently by `bin_io`. The shapes for these types will |
| 48 | not be equivalent. |
| 49 | |
| 50 | ```ocaml |
| 51 | module R1 = struct |
| 52 | type t = { foo : int; bar : string; } [@@deriving bin_io] |
| 53 | end |
| 54 | module R2 = struct |
| 55 | type t = { bar : string; foo : int; } [@@deriving bin_io] |
| 56 | end |
| 57 | ``` |
| 58 | |
| 59 | ## Reordering Variants |
| 60 | |
| 61 | In a similar way, reordering the constructors in a variant type |
| 62 | changes the bin_prot serialization. Again, the shapes for these types |
| 63 | will not be equivalent. |
| 64 | |
| 65 | ```ocaml |
| 66 | type variant1 = Foo | Bar [@@deriving bin_io] |
| 67 | type variant2 = Bar | Foo [@@deriving bin_io] |
| 68 | ``` |
| 69 | |
| 70 | The above examples strongly motivate shape checking. In each case the |
| 71 | two alternative type versions are equivalent at the OCaml type level; |
| 72 | changing between versions will elicit no warning from the type |
| 73 | checker. However the generated `bin_prot` serializations are |
| 74 | incompatible and the existing runtime checking done by `bin_prot` is |
| 75 | insufficient to detect this and so prevent communication. |
| 76 | |
| 77 | For the case of reordering variants the broken behaviour is clear: |
| 78 | values of `Foo` and `Bar` will be swapped when communicated. A |
| 79 | catastrophic bug. |
| 80 | |
| 81 | For the case of reordering record fields, the values communicated |
| 82 | will be garbage. For example: |
| 83 | |
| 84 | ```ocaml |
| 85 | utop: Binable.of_string (module R2) (Binable.to_string (module R1) { foo = 3; bar = "abc"; });; |
| 86 | - : R2.t = {R2.bar = "\003ab"; foo = 99} |
| 87 | ``` |
| 88 | |
| 89 | Syntax extension |
| 90 | ==================================== |
| 91 | |
| 92 | Normal use of `bin_prot_shape` is via the existing |
| 93 | `[@@deriving bin_io]` syntax extension. This is extended to generate |
| 94 | a shape description alongside the existing generation of the |
| 95 | `bin_prot` reader & writers. For example, from: |
| 96 | |
| 97 | ```ocaml |
| 98 | type t = ... [@@deriving bin_io] |
| 99 | ``` |
| 100 | |
| 101 | we generate: `val bin_shape_t : Shape.t`. |
| 102 | |
| 103 | For polymorphic types: |
| 104 | |
| 105 | ```ocaml |
| 106 | type 'a t1 = ... [@@deriving bin_io] |
| 107 | type ('a,'b) t2 = ... [@@deriving bin_io] |
| 108 | ``` |
| 109 | |
| 110 | We generate shape combinators or the corresponding arity: |
| 111 | |
| 112 | ```ocaml |
| 113 | val bin_shape_t1 : Shape.t -> Shape.t |
| 114 | val bin_shape_t2 : Shape.t -> Shape.t -> Shape.t |
| 115 | ``` |
| 116 | |
| 117 | We also support: `[@@deriving bin_shape]` to generate only the |
| 118 | `[bin_shape_..]` value. and `[%bin_shape: TYPE]` to generate an |
| 119 | expression of type `Shape.t`. |
| 120 | |
| 121 | It is not allowed for `TYPE` to contain free type |
| 122 | variables. i.e. `[%bin_shape: 'a list]` |
| 123 | |
| 124 | The shape generated when deriving the `bin_shape_` for a given type, |
| 125 | makes use of the shapes of the composed types. For example, given: |
| 126 | |
| 127 | ```ocaml |
| 128 | type t1 = ... [@@deriving bin_io] |
| 129 | type t2 = ...t1... [@@deriving bin_io] |
| 130 | ``` |
| 131 | |
| 132 | The generated definition of `bin_shape_t2` makes use of `bin_shape_t1` |
| 133 | and so a binding must exist for that name. |
| 134 | |
| 135 | In the case of a user defined `bin_prot` (i.e. no use of |
| 136 | `[@@deriving bin_io]`), then the user is responsible for declaring a |
| 137 | suitable `bin_shape_t` value. Commonly this will be a new `basetype` |
| 138 | shape, distinct from all other shapes. See below. |
| 139 | |
| 140 | We also have syntax for new base shapes. |
| 141 | |
| 142 | ```ocaml |
| 143 | [@@deriving bin_shape ~basetype:NAME] |
| 144 | ``` |
| 145 | |
| 146 | And annotated shapes |
| 147 | |
| 148 | ```ocaml |
| 149 | [@@deriving bin_shape ~annotate:NAME] |
| 150 | ``` |
| 151 | |
| 152 | See below for details. |
| 153 | |
| 154 | Runtime support |
| 155 | ==================================== |
| 156 | |
| 157 | The `Shape.t` values generated and composed by `[@@deriving bin_io]` |
| 158 | encode a description from which shape equivalence can be determined. |
| 159 | |
| 160 | The [Bin_prot] runtime library distinguishes distinct types for: |
| 161 | |
| 162 | ```ocaml |
| 163 | Shape.t [@@deriving sexp_of] |
| 164 | Shape.Canonical.t [@@deriving compare, sexp_of] |
| 165 | Shape.Digest.t [@@deriving compare, sexp] |
| 166 | ``` |
| 167 | |
| 168 | ## `Shape.t` |
| 169 | |
| 170 | The base `Shape.t` corresponds directly to the OCaml type definition |
| 171 | or type expression. Base shapes compose nicely, but are not directly |
| 172 | comparable since they contain irrelevant details such as names of type |
| 173 | definitions. For example, given: |
| 174 | |
| 175 | ```ocaml |
| 176 | type 'a pair = 'a * 'a |
| 177 | ``` |
| 178 | |
| 179 | base shapes distinguish `int * int` from `int pair`. |
| 180 | |
| 181 | ## `Shape.Canonical.t` |
| 182 | |
| 183 | A shape can be evaluated to a canonical shape: As the name implies, |
| 184 | the representation of canonical shapes is canonical. Equivalence is |
| 185 | structural equality. |
| 186 | |
| 187 | ```ocaml |
| 188 | val eval : Shape.t -> Shape.Canonical.t |
| 189 | ``` |
| 190 | |
| 191 | A canonical shape provides a human-level description of the (shape of |
| 192 | a) type, which is important if we wish to explain to a human why two |
| 193 | types are considered incompatible |
| 194 | |
| 195 | ```ocaml |
| 196 | val to_string_hum : Shape.Canonical.t -> string |
| 197 | ``` |
| 198 | |
| 199 | ## `Shape.Digest.t` |
| 200 | |
| 201 | A canonical shape can be digested to a cryptographic hash, and except |
| 202 | for hash collisions, equality of the digests implies equality of |
| 203 | canonical shapes and hence equivalence at the `Shape.t` level. |
| 204 | |
| 205 | ```ocaml |
| 206 | val to_digest : Shape.Canonical.t -> Shape.Digest.t |
| 207 | ``` |
| 208 | |
| 209 | The intention is that a shape digest can be passed between |
| 210 | server/client of an RPC protocol to check that both sides have the |
| 211 | same opinion of the types being passed. |
| 212 | |
| 213 | We can convert directly from a base shape to its digest, avoiding |
| 214 | construction of intermediate `Shape.Canonical.t`, which can be much |
| 215 | more expensive. |
| 216 | |
| 217 | ```ocaml |
| 218 | val eval_to_digest : Shape.t -> Shape.Digest.t |
| 219 | ``` |
| 220 | |
| 221 | In the following when we talk about compatible or equivalent types, we |
| 222 | mean that the following definition of `=` would return true: |
| 223 | |
| 224 | ```ocaml |
| 225 | let (=) x y = Shape.(eval_to_digest x = eval_to_digest y) |
| 226 | ``` |
| 227 | |
| 228 | Definition of shape equivalence |
| 229 | ==================================== |
| 230 | |
| 231 | We define the notion of shape equivalence w.r.t what aspects of a type |
| 232 | are considered significant for distinguishing one type from another, |
| 233 | and hence causing non-equivalence of the corresponding shapes. Shapes |
| 234 | with no significant differences should be equivalent. |
| 235 | |
| 236 | ## Shapes corresponding to the following type construction are mutually distinct: |
| 237 | |
| 238 | * built-in types: int, string.. |
| 239 | * built-in type constructors: 'a list, 'a array,.. |
| 240 | * user base types |
| 241 | * tuples |
| 242 | * records |
| 243 | * (normal) variants |
| 244 | * polymorphic variants |
| 245 | * annotated shapes |
| 246 | |
| 247 | ## For structured types: |
| 248 | |
| 249 | * The name of built-in/base types is significant. |
| 250 | * The name and sub-shape of built-in/base type-constructors is |
| 251 | significant. |
| 252 | * The type and order of tuple components is significant. |
| 253 | * The name, type and order of record fields is significant. |
| 254 | * The name, type and order of variant constructors is significant. |
| 255 | * The name and type (but not the order) of polymorphic variant |
| 256 | constructors is significant. |
| 257 | * The annotation and sub-shape of an annotated shape is significant. |
| 258 | |
| 259 | ## Types for which shape generation is not supported: |
| 260 | |
| 261 | * Polymorphic-variant inheritance from a recursive or annotated |
| 262 | polymorphic-variant type. |
| 263 | * Function types are not supported, since functions are not |
| 264 | serializable by `bin_io`. |
| 265 | * Universal types within records are not supported. |
| 266 | * GADTs, object types, class types, first-class module types are not |
| 267 | supported. |
| 268 | |
| 269 | ## Equivalence of type aliases |
| 270 | |
| 271 | Names chosen for types and type-vars are NOT significant. i.e |
| 272 | |
| 273 | ```ocaml |
| 274 | type myint = int [@@deriving bin_io] |
| 275 | |
| 276 | type 'a t1 = ... [@@deriving bin_io] |
| 277 | type 'b t2 = 'b t1 [@@deriving bin_io] |
| 278 | ``` |
| 279 | |
| 280 | Then: `myint` and `int` are compatible, and `t1` and `t2` are compatible. |
| 281 | |
| 282 | In a similar way, the order of type definitions within a mutual block |
| 283 | of type definitions is NOT significant. Given: |
| 284 | |
| 285 | ```ocaml |
| 286 | type t1 = TT of t1 | TU of u1 | TB |
| 287 | and u1 = UT of t1 | UU of u1 | UB |
| 288 | |
| 289 | type u2 = UT of t2 | UU of u2 | UB |
| 290 | and t2 = TT of t2 | TU of u2 | TB |
| 291 | ``` |
| 292 | |
| 293 | Then: `t1` and `t2` are compatible, and `u1` and `u2` are compatible. |
| 294 | |
| 295 | User defined `bin_prot` |
| 296 | ==================================== |
| 297 | |
| 298 | For some types, the bin-io readers and writers are constructed by |
| 299 | hand, and there is no relationship between the representation of the |
| 300 | type in memory, and the way the type is serialized over-the-wire. In |
| 301 | this case we should like a new shape distinct from any other. This is |
| 302 | obtained from: `Bin_prot.Shape.basetype`. |
| 303 | |
| 304 | ```ocaml |
| 305 | val basetype : string -> Shape.t list -> Shape.t |
| 306 | ``` |
| 307 | |
| 308 | The `string` argument identifies the name of the base type. The `Shape.t list` argument |
| 309 | allows for base types to be polymorphic. For example: |
| 310 | |
| 311 | ```ocaml |
| 312 | type 'a t = ... |
| 313 | let bin_writer_t = ... |
| 314 | let bin_reader_t = ... |
| 315 | ... |
| 316 | let bin_shape_t bin_shape_a = Bin_prot.Shape.basetype "My.Special.t" [bin_shape_a] |
| 317 | ``` |
| 318 | |
| 319 | Alternatively, we can use syntax: |
| 320 | |
| 321 | ```ocaml |
| 322 | [@@deriving bin_shape ~basetype:NAME] |
| 323 | ``` |
| 324 | |
| 325 | The above example is rewritten: |
| 326 | |
| 327 | ```ocaml |
| 328 | type 'a t = ... [@@deriving bin_shape ~basetype:"My.Special.t"] |
| 329 | ``` |
| 330 | |
| 331 | Since compatibility for basetypes is determined from their names, and |
| 332 | is not generative, the names are best chosen to involve something |
| 333 | globally unique, for example an identifier produced by `uuid` tool: |
| 334 | |
| 335 | ```ocaml |
| 336 | type t = int [@@deriving bin_shape ~basetype:"f53adba2-4aa1-11e6-983f-479189aad583"] |
| 337 | ``` |
| 338 | |
| 339 | Given the above declaration, `t` is not compatible with `int`. |
| 340 | |
| 341 | Shape annotations |
| 342 | ==================================== |
| 343 | |
| 344 | Sometimes we have types that are structurally identical, but |
| 345 | semantically incompatible. Without user intervention this will result |
| 346 | in equivalent shapes. Shape annotation allows otherwise compatible |
| 347 | types to be distinguished at the shape level. Annotated shapes are |
| 348 | created by using: `Bin_prot.Shape.annotate`: |
| 349 | |
| 350 | ```ocaml |
| 351 | val annotate : string -> Shape.t -> Shape.t |
| 352 | ``` |
| 353 | |
| 354 | Or using syntax: |
| 355 | |
| 356 | ```ocaml |
| 357 | [@@deriving bin_shape ~annotate:NAME] |
| 358 | ``` |
| 359 | |
| 360 | For example: |
| 361 | |
| 362 | ```ocaml |
| 363 | type dollars = float [@@deriving bin_io, bin_shape ~annotate:"dollars"] |
| 364 | ``` |
| 365 | |
| 366 | Other semantic invariants might be captured in a similar way: |
| 367 | |
| 368 | ```ocaml |
| 369 | type t = int list [@@deriving bin_io, bin_shape ~annotate:"sorted"] |
| 370 | ``` |
| 371 | |
| 372 | Annotations have a similar shape-level benefit as could be achieved |
| 373 | via use of a record type, but without the associated cost of an extra |
| 374 | level of boxing. |
| 375 | |
| 376 | ```ocaml |
| 377 | type t2 = { |
| 378 | sorted : int list; |
| 379 | } [@@deriving bin_io] |
| 380 | ``` |
| 381 | |
| 382 | Note: the shape for `t` and `t2` are not compatible. |
| 383 | |
| 384 | Basetype vs Shape annotations |
| 385 | ==================================== |
| 386 | |
| 387 | It is worth emphasizing the difference between `basetype` and |
| 388 | `annotate`. For example, given: |
| 389 | |
| 390 | ```ocaml |
| 391 | type dollars1 = float [@@deriving bin_io, bin_shape ~basetype:"dollars"] |
| 392 | type dollars2 = float [@@deriving bin_io, bin_shape ~annotate:"dollars"] |
| 393 | ``` |
| 394 | |
| 395 | The shape for `dollars1` and `dollars2` are not compatible, and |
| 396 | neither is compatible with `float`. |
| 397 | |
| 398 | In the first example we are defining a new basetype `dollars1` which |
| 399 | just happens to be serialized over the wire in the same way as a |
| 400 | float, but the fact of serialization like a float is not captured by |
| 401 | the shape for `dollars1`. |
| 402 | |
| 403 | In the second example we are defining an annotated type `dollars2` |
| 404 | which is serialized over the wire as a float, and this fact is |
| 405 | captured by the shape for `dollars2. |
| 406 | |
| 407 | We can regard the shape resulting from use of `annotate` as making a |
| 408 | stronger claim that that which results from `basetype`. |
| 409 | |
| 410 | Given additionally: |
| 411 | ```ocaml |
| 412 | type dollars3 = Bignum.t [@@deriving bin_io, bin_shape ~basetype:"dollars"] |
| 413 | type dollars4 = Bignum.t [@@deriving bin_io, bin_shape ~annotate:"dollars"] |
| 414 | ``` |
| 415 | |
| 416 | The shape for `dollars3` is compatible with `dollars1`, whereas |
| 417 | `dollars4` is incompatible with `dollars2`. |
| 418 | |
| 419 | Overall in this example: we can say that it is use of annotation which |
| 420 | is appropriate, and not a new basetype. Furthermore, the definitions |
| 421 | of `dollars1` and `dollars3` are something of an anti-pattern: When |
| 422 | `[@@deriving bin_shape ~basetype ...]` is used, we wouldn't normally |
| 423 | have `[@@deriving bin_io]` but instead have hand-written readers and |
| 424 | writers. |
| 425 | |
| 426 | Async_rpc |
| 427 | ==================================== |
| 428 | |
| 429 | We extend `Async_rpc` to support dynamically checking that protocols |
| 430 | as expected by client and server are shape-compatible. Shape |
| 431 | incompatibility prevents communication, and fails in a well-defined |
| 432 | manner. |
| 433 | |
| 434 | Shape checking is achieved via a new protocol in the `async_rpc` |
| 435 | framework. To avoid confusion with the existing use of the term |
| 436 | "version" in `async_rpc`, which refers to the version of a specific |
| 437 | rpc, we refer to the version of the entire `async_rpc` framework as |
| 438 | the "edition" of the `async_rpc` protocol. |
| 439 | |
| 440 | * `Edition.V1` - original shapeless protocol. |
| 441 | * `Edition.V2` - new shape-checking protocol. |
| 442 | |
| 443 | When a connection is created, the set of allowed editions is |
| 444 | specified. The edition is negotiated during the handshake phase of |
| 445 | establishing a connection. For a successful negotiation, there must |
| 446 | be an edition common to both parties. |
| 447 | |
| 448 | ```ocaml |
| 449 | Connection.create : ... |
| 450 | -> ?protocol_editions : Edition.t list |
| 451 | ... |
| 452 | ``` |
| 453 | |
| 454 | The expected migration path for client/server apps is to move through |
| 455 | the following stages of allowed connections: |
| 456 | |
| 457 | * `[V1]` -- don't check shapes; even if the other side is shape-aware |
| 458 | * `[V1;V2]` -- check shapes as long as the other side is shape-aware |
| 459 | * `[ V2]` -- insist on checking shapes; refuse to communicate to |
| 460 | parties not shape-aware |