Nested generic rules get the wrong argument when the outer rule is instantiated more than once

Open
#215 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
devtools

Research direction

Start by reproducing with the nested.cddl schema and value.cbor (82 81 82 00 01 81 82 00 61 61) using cuddle validate-cbor root value.cbor nested.cddl -f binary. The trace labels the nested instance %inner<x> instead of the substituted argument, so look at how generic rule arguments are substituted and how instances are named and shared during validation. Done when the reproducer prints Validation successful and the existing controls in the table still behave the same.

Written by the indexing model from the issue text.

Description

bug

Summary

When a generic rule passes its parameter on to another generic rule, and the
outer rule is instantiated more than once in the same file with different
arguments, validate-cbor checks the nested rule with the argument of a
different instantiation. Valid CBOR is then rejected.

The validation trace suggests why: the nested instance is labelled with the
outer rule's formal parameter name (%inner<x>) rather than with the
substituted argument (%inner<int>, %inner<tstr>), so all instantiations of
the outer rule seem to share one nested instance.

Version: cuddle 1.8.1.0.

Reproducer

nested.cddl:

root = [a, b]

a = outer<int>
b = outer<tstr>

outer<x> = [inner<x>]
inner<y> = [0, y]

value.cbor is [[[0, 1]], [[0, "a"]]], which matches root:

82 81 82 00 01 81 82 00 61 61
$ cuddle validate-cbor root value.cbor nested.cddl -f binary
array
└ failed to apply required rule:
  └ ref: a
    └ ref: %outer<int>
      └ array
        └ failed to apply required rule:
          └ ref: %inner<x>
            └ array
              └ 1 valid elements
                failed to apply required rule:
                └ failed to apply:
                    text

Validation failed

Inside a = outer<int>, the nested inner<x> is checked against text, the
argument of the other instantiation (b = outer<tstr>), so the integer 1 is
rejected.

Expected

Validation successful: a is [[0, int]] and b is [[0, tstr]].

Controls

All against [[[0, 1]], [[0, "a"]]] unless noted:

Variant Result
As above (outer instantiated twice, nested generic inner) fails
Only one instantiation (root = a, value [[0, 1]]) passes
outer inlined, no nested generic: outer<x> = [[0, x]] passes
Definitions of a and b in the opposite order fails the same way
root = [b, a] with value [[[0, "a"]], [[0, 1]]] fails the same way (%outer<int> → %inner<x> → text)
inner's parameter also named x (inner<x> = [0, x]) fails

So it takes two instantiations of the outer rule, and the order of the rules
or of the elements doesn't matter. cuddle validate nested.cddl accepts the
schema without complaint.

Where we hit it

ouroboros-consensus describes hard-fork-combinator state with

telescope8<byron, shelley, ..., dijkstra>
  = [pastEra, ..., currentEra<dijkstra>] / ...
currentEra<st> = [bound, st]

and uses telescope8 several times in the same file (for the ledger state and
for the chain-dependent state), which has the same shape as the reproducer:
the generic telescope8 passes its parameters on to the generic currentEra.
Valid snapshots were rejected until we worked around it by writing
currentEra<st> out inline as [bound, st] inside telescope8.

Dominant language
Haskell
Stars
8
Forks
5
Avg merge
3d 12h
Merged PRs (30d)
4

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from IntersectMBO/cuddle

All issues in IntersectMBO/cuddle

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.