Repository navigation
Conversation
We want to publish the uapi16 crate on crates.io. Currently, it reads every record with the pull parser of our own json crate, so that crate would have to be published as well, and the name json is taken there already. So let's read records with serde_json instead. serde_json has no pull parser. It hands every value it reads to a visitor, so decode.rs is turned inside out into DeserializeSeed and Visitor code. A record is still read once, member by member. A field of the wrong type is still noted and the record read on, since the media type that decides what the record is can come last. Every array and object of a record is read by a part of its own: the record itself, contents and its objects, features, extendedAttributes and httpHeaders. Every other value is read by a visitor that notes what type it has. serde_json on its own is not strict enough for a manifest. Of two equal keys it takes the last one, and it takes noncharacters and NUL in strings. What it offers to pass over a value, IgnoredAny and RawValue, checks even less and takes lone surrogates too. So the rules stay ours and move from the json crate to src/uapi16/strict.rs: - the duplicate keys. Every kind of object has a table of its fields, and a key that names one is told apart from the keys before it by the bit of its place in the table. Any other key goes into a set of keys of its object, as every key did before: an unknown member, the name of an extended attribute or of an HTTP header field, a key in a value the reader passes over. The set is kept for the next object at the same depth within the record and shrunk when it grew, as before; - the string rules, checked on every string and every key, also in the values a reader passes over; - Skip, which reads through a value it doesn't keep and holds it to both rules. serde_json reports a broken rule of ours like any other error, at a line and a column. Strict notes which rule was broken and turns serde_json's error back into one of ours, Syntax, DuplicateKey or StringRule, at a byte of the record. A key twice is still found before its colon and its value are read, also in a value the reader passes over, and it is the same DuplicateKey error whether the key names a field or not. Its offset and those of the string rules stay what they were: right behind the key or the string. Note that serde_json may have read on over whitespace and the brace that ends the object behind a key before it says where the error is. So Strict takes the byte behind the last quote it read. What changes is this: - Every syntax error takes serde_json's words, so four vectors in refused.jsonl change their message. Most syntax errors stay at their byte. A lone surrogate moves by a byte, an invalid escape by a byte, a \u escape with too few digits by a few bytes, also at the end of a record, a misspelled literal such as tru from its start to the byte behind it, a number out of range by a byte, and one with a long exponent to where the exponent overflows. None of this changes whether a record is taken. - serde_json converts every number it reads and limits nesting for every value, also for one we pass over: 127 arrays and objects inside each other, the object of the record included. Our parser passed over any depth. So a record that holds a number too large for a double, or nests deeper, is not JSON now, wherever that value is. That is in a field we don't know, but also in the value of a field we know when it has the wrong type, which our parser passed over as well, and in a record that isn't an object. The record is read before it is checked, so that also comes before every check after the read, such as a file object after a signature. -0 is a float for serde_json as it was for our parser, so an unsigned field still refuses it. uapi16 turns on float_roundtrip of serde_json. Without it serde_json refuses some spellings of the largest doubles as out of range, such as 1.7976931348623158e308, which a field we don't know may hold. The features of serde_json are the same for every crate of a build, so uapi16 asks for it itself. A test pins that, and without the feature it fails when uapi16 is tested on its own. arbitrary_precision would hand every number that isn't a 64-bit integer to our visitors as an object. We never turn it on, but uapi16 can't stop another crate of a build from doing so. The crate docs say what changes then. serde_json brings serde_core, itoa, memchr and zmij. serde_json and serde_core also name serde and serde_derive for cfg(any()), which no target has, to keep their versions in step. So those two and the crates of serde_derive are in Cargo.lock but nothing builds them. The dependency check in CI lists all of them. Hashing every key into a set took a fifth of the instructions of the reader. With a bit for every field, a record with known fields only takes 9% fewer instructions than with our parser and is read about as fast or up to about 4% faster. A record with fields we don't know, here an object and an escaped string, takes 19% more instructions and about 24% to 34% longer. Both stay linear, in the size of a record and in the number of records. A manifest modelled on tests/fixtures/special takes 11% fewer instructions and is read about 5% to 15% faster. The ranges come from timing both parsers in one binary and as separate release binaries, on two machines. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The writer still puts its records together with the ObjectWriter of our json crate. Let's write them with serde_json as well, so that uapi16 doesn't need the json crate at all anymore. serde_json writes a value through its Serialize implementation, member by member. So every object of a manifest gets a write_fields() that hands its fields to a FieldWriter on top of a SerializeMap, in the order of their names, as before. What a field holds of its own, contents, extendedAttributes, features and httpHeaders, is a small Serialize type. Every member write can fail now, so they all return a Result. serde_json writes into an io::Write, which a String is not. So the Encoder puts each record together in a Vec<u8> of its own and keeps its room the way the Writer kept the room of its text, up to 64 KiB. The Writer in blocking.rs writes that buffer out as it is. It has the Encoder put a record together and empty it again through methods that are visible to the crate only. Encoder's public methods append the record to the caller's string and keep their signatures. The output is the same byte for byte. serde_json escapes exactly what our writer escaped, and a manifest has no floats, which is the one thing the two write differently. The fixtures and the round trips in the tests agree, and so do a million records written by both writers. Writing them takes 11% fewer instructions than before, and about 2% to 7% less time, depending on the machine. The tests of uapi16 read records back into a serde_json::Value and look at the order of the keys, so they turn on preserve_order. That brings indexmap, hashbrown, equivalent and foldhash into Cargo.lock. Of two equal keys serde_json's Value keeps the last one, where json::parse refused the record. So the tests hand every record to the strict reader of strict.rs before they look at it. fat-trace, the library built again for the crash harness, has json as a dependency. Only the unit tests of the library use it, which clippy builds with --all-targets. So far uapi16 brought json into every build, and that hid it. Now cargo 1.100 warns that fat-trace doesn't use json. So let's make it a dev-dependency of fat-trace. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The varlink crate reads every message into a json::Value, builds its replies as json::Object, and hands both to the service through its API. Let's move it to serde_json as well. varlink::Value is serde_json's Value now, and varlink::Object an alias for serde_json's Map of String to Value. object! stays and builds such a Map. Its users, the service, the CLI, bench and tests/varlink.rs, follow in the same commit: they pass their objects to varlink, so they can't keep json's types once varlink takes serde_json's. Of two equal keys in an object serde_json's Value keeps the last one. Our parser refused such a message, and varlink still does. A small seed of its own, UniqueKeys, builds the Value and refuses a key the object had before, at any depth, before it reads the value of that key. The Map that holds the members doubles as the set of keys, so this stays linear. UniqueKeys is public, so that bench reads --params with it and refuses a key twice there, as it did with json::parse. The tests read their JSON with it too. varlink turns on preserve_order and float_roundtrip. With preserve_order a Map keeps its members in the order they were put in, as json::Object did. So every message keeps the order it has today: "ufat capabilities" prints the same bytes, and the replies varlinkctl shows are the same, down to the examples in docs/varlink.md. Without float_roundtrip serde_json reads about a third of the doubles written with 17 digits as another double than std's parser, which our parser used. A service gets the float the caller sent. Inside ufat both features reach uapi16 too. uapi16 asks for float_roundtrip itself already, and preserve_order changes nothing there, uapi16 builds no Map. arbitrary_precision would hand UniqueKeys every number that isn't a 64-bit integer as an object, so the note in Cargo.toml that says to never turn it on names varlink now as well. -0 is a float for serde_json as it was for our parser. So an int parameter still refuses it, and so does every number ufat takes. The tests say so now. What changes: - A number is an integer if it fits into 64 bits, signed or unsigned. Our parser kept integers of up to 128 bits. So an int parameter wider than 64 bits fails the interface check now. ufat's replies to such calls stay the same, its parameters are 64 bits wide at most. - A message nests at most 127 arrays and objects inside each other, its own object included. Our parser took 128. A message that nests deeper is not JSON now. - A float goes out the way serde_json writes it, 1e+21 where our writer wrote 1e21. ufat sends no floats. - A serde_json Value takes 72 bytes where a json::Value took 32. So a hostile message of the largest size the service reads, 16 MiB, can need about 604 MB of heap instead of 268 MB. Only root and the user the service runs as can connect to it. - Reading a large array takes longer: about 1.4 times as long for an array of small objects and about 1.6 to 1.9 times for an array of numbers. The numbers take no more instructions than before, and neither do the members of an object. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
The tests of the manifest writer of a volume read the records it wrote with json::parse, and look at their keys and at the order of them. They are the last users of our json crate. So let's read the records into a serde_json::Value instead. preserve_order keeps the keys in the order the writer wrote them, which the tests check. fat-trace, the library built again for the crash harness, builds these tests as well when clippy looks at all targets, so its dev-dependency on json becomes one on serde_json. With that nothing uses the json crate anymore, and its entry in the workspace dependencies goes. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Nothing uses src/json anymore. The manifests of uapi16 and the messages of varlink go through serde_json, and so do the tests. So let's drop the crate from the tree, from the workspace members and from HACKING.md. The crate root of the Kani proofs said that it keeps Kani away from the json crate. What it keeps away from the proofs now is serde_json. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fat and the Varlink interface call the reason why an entry must not be used now a ValidityProblem, and the reason why a name is refused a NameProblem. uapi16 calls the same things Invalid and InvalidName, which don't say much on their own. Let's use the same names in uapi16 before the crate is published. After that, renaming them would break its users. fat has a ValidityProblem and a NameProblem of its own. So where it uses uapi16's, it names them by their path, and the two can't be mistaken for each other. Problem::InvalidName keeps its name: it is a kind of problem of a manifest, like Problem::InvalidString next to it. No functional changes. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Problem, SourceProblem, ContentProblem, UnsupportedContent, ErrorKind, Field, NameProblem, Data, Object and Order are #[non_exhaustive]. ValidityProblem, Validity, OpenError, Location and InodeType are not. So a variant added later would break every user who matches on one of them, and Validity already says that the specification may add values. Let's mark the five as well, before the crate is published. Only Step stays exhaustive. A caller that doesn't know a step can't keep driving the decoder. fat matches on ValidityProblem. A reason it doesn't know of becomes EKEYEXPIRED without a detail, since the entry is not to be used now either way. Its match on InodeType already refuses every type that FAT can't store, and a new type with them. The matches of the tests on InodeType have an arm for every other type too. fat's Directory still builds an OpenError as before, and nothing outside uapi16 matches on Location. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We want to publish uapi16 on crates.io. Currently, it reads and writes
manifests with our own json crate, so that crate would have to be
published as well, and the name json is already taken there.
This drops the json crate. uapi16 and varlink read and write JSON
through serde_json now. serde_json hands every value it reads to a
visitor, so uapi16's decoder is turned inside out into visitors of our
own.
serde_json on its own isn't strict enough for a manifest though. Of two
equal keys it takes the last one, and it takes noncharacters and NUL in
strings. What it offers to pass over a value checks even less. So the
rules stay ours and move from the json crate into src/uapi16/strict.rs:
key, before its value is read.
or StringRule, at a byte of the record.
varlink keeps refusing a message with a key twice. A small seed of its
own, UniqueKeys, builds the serde_json Value and refuses a key its
object had before, at any depth.
What changes:
Syntax errors take serde_json's words, so four vectors in
refused.jsonl change their message. Most syntax errors stay at their
byte. A few kinds move: a lone surrogate, an invalid escape, a \u
escape with too few digits, a misspelled literal, a number out of
range and a number with a long exponent. None of this changes whether
a record is taken.
serde_json converts every number it reads and limits nesting to 127
arrays and objects inside each other, the object of the record
included. It does that for a value we pass over too, where our parser
took any depth. So a record that nests deeper or holds a number too
large for a double is not JSON now, wherever that value is.
A Varlink message nests at most 127 levels deep, where our parser
took 128. A number is an integer only if it fits into 64 bits, signed
or unsigned, where our parser kept integers of up to 128 bits. So an
int parameter wider than 64 bits fails the interface check now.
ufat's own parameters are 64 bits wide at most, so its replies stay
the same.
A serde_json Value takes 72 bytes where a json::Value took 32. So a
hostile Varlink message of the largest size the service reads,
16 MiB, can need about 604 MB of heap instead of 268 MB. Only root
and the user the service runs as can connect to it.
A record with known fields only takes 9% fewer instructions than
before and is read about as fast or up to about 4% faster. A record
with fields we don't know takes 19% more instructions and about 24%
to 34% longer. Both stay linear. Writing a record takes 11% fewer
instructions and about 2% to 7% less time. In a Varlink message a
large array of small objects takes about 1.4 times as long to read,
and one of numbers about 1.6 to 1.9 times.
While at it, change two things in uapi16's API before the crate is
published, since changing them later would break its users:
the names fat and the Varlink interface already use.
#[non_exhaustive] like the other public enums of uapi16. Only Step
stays exhaustive, since a caller that doesn't know a step can't keep
driving the decoder.
After this series uapi16 only depends on crates from crates.io. So it
can move into a repository of its own and be published from there.
That's the next step and not part of this series.
This was compared against main on refused.jsonl, the fixtures, about
9.7k edge cases, 60k mutated manifests and about 203k Varlink texts.
Nothing in there that main refuses is taken now, and the writer's output
is the same byte for byte. The checks ran on every commit, with uapi16
also built and tested on its own.