3D: add entrypoint wrappers that check the whole input was consumed - #314
Open
tahina-pro wants to merge 2 commits into
Open
3D: add entrypoint wrappers that check the whole input was consumed#314tahina-pro wants to merge 2 commits into
tahina-pro wants to merge 2 commits into
Conversation
Fixes project-everest#312. The existing `ModuleCheckTyp` wrappers only check that validation succeeded, so an input buffer with trailing bytes beyond the message is silently accepted. Next to each existing wrapper, 3d now generates a `Complete` variant with the same signature, which additionally checks that the validator consumed the whole input buffer: * `ModuleCheckTyp` -> `ModuleCheckCompleteTyp` * `ModuleProbeFnCheckTyp` -> `ModuleProbeFnCheckCompleteTyp` * user-provided `entrypoint(Foo)` -> `FooComplete` On trailing bytes, the wrapper calls `ModuleEverParseError` with the type name, an empty field name and "unexpected trailing bytes", then returns FALSE. Since the wrapper needs to know the length of the whole input, the `Complete` wrappers are only generated for the `buffer` input stream binding. 3d now also rejects colliding entry point names, which can now happen when a user-provided name is the `Complete` counterpart of another one. The doc/3d-snapshot snapshots are regenerated in a follow-up commit. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Mechanical output of `make 3d-doc-snapshot' after the previous commit: every entrypoint in the documentation examples now additionally gets a `Complete' wrapper, which checks that the validator consumed the whole input buffer. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Fixes #312.
Problem
For a type marked
entrypoint, the 3d frontend generates a wrapperModuleCheckTypinModuleWrapper.c/.h. That wrapper calls the verifiedvalidator and only checks whether it reported an error:
On success,
ep_statuscarries the position up to which the input wasconsumed, but the wrapper discards it. So a buffer that starts with a valid
message and then contains extra bytes is accepted, with no way for the caller
to notice from the wrapper's return value alone.
Solution
Next to every existing wrapper, 3d now generates a
Completevariant with thesame signature, which additionally checks that the validator consumed the whole
input buffer.
ModuleCheckTypModuleCheckCompleteTypModuleProbeFnCheckTypModuleProbeFnCheckCompleteTypentrypoint(Foo)FooCompleteFor example, for
TWrapper.hnow declares:and the body of the new wrapper is the old one plus:
Callers therefore choose explicitly:
ModuleCheckTypwhen the input buffer maylegitimately contain trailing data (e.g. a fixed-size receive buffer), and
ModuleCheckCompleteTypwhen the buffer is meant to hold exactly one message.Naming
Completeechoes LowParse'sparse_consumes_all/ "complete parser"vocabulary, keeps the
Module...Check...Typshape so both variants sorttogether in the header, and extends to user-provided entrypoint names as a
plain suffix.
CheckAllwas rejected becauseCheckAllis already used as acustom entrypoint name in the documentation.
Design notes
Buffer binding only. The
Completewrappers are generated only for thedefault
bufferinput stream binding. With the other bindings the wrapperreturns
parsedSizeover an unbounded stream, so there is neither a knowntotal length to compare against nor a return convention to signal "trailing
bytes". Extending this to streams would be a separate change.
Error reporting. The error frame is not filled in this case (the
validator itself succeeded), so the wrapper calls
ModuleEverParseErrordirectly with the type name, an empty field name, and
"unexpected trailing bytes".No new locals. The check reuses
ep_statusandlen, so the--hoist_locals,--init_localsand--goto_for_early_returnvariants ofthe generated code are unaffected structurally; under
--goto_for_early_returnthe check does
goto exitlike the other failure paths.Name collisions. Since
FooCompleteis now derived fromentrypoint(Foo),3d rejects a
.3dfile declaring two entrypoints whose public names collide,including when one is the
Completecounterpart of the other:Visibility. The new wrappers follow exactly the same rules as the
existing ones: public in the header iff a plain
entrypointis declared, andemitted
staticin the.cfile when onlyentrypoint probeattributes arepresent.
Completeprobe wrappers call theCompletemain wrapper.Changes
Commit 1 —
3d: generate wrappers that check the whole input was consumedsrc/3d/Target.fst,src/3d/Target.fsti: newwrapper_complete_name,probe_wrapper_complete_nameandcomplete_name_of_custom_name;wrapped_call_buffergained acheck_completeflag;print_validators_for_one_declemits both families; newentrypoint-name-collision check.
src/3d/tests/check_complete/: new test (see below), wired intosrc/3d/tests/Makefile.src/3d/tests/goto_return/snapshot/: regenerated.doc/3d-lang.rst,doc/3d.rst: documentation.Commit 2 —
doc: regenerate 3d-snapshot for the new 'Complete' wrappersdoc/3d-snapshot/*Wrapper.{c,h}: mechanical output ofmake 3d-doc-snapshot, separated out to keep the first commit reviewable.Testing
New test
src/3d/tests/check_complete/, run as part of the 3d test suite. Itsdriver checks that:
Completewrapper accept;Completewrapper rejects and reports exactly one error;Completecounterpart (CheckHeaderComplete) behavesthe same way.
make -j16 -k 3d-testpasses (exit code 0), including the regeneratedsnapshots and the existing hash-check tests.