Skip to content

Extend the set of exported type ids with defined resources - #1251

Closed
elliottt wants to merge 3 commits into
bytecodealliance:mainfrom
elliottt:trevor/validation-error
Closed

elliottt wants to merge 3 commits into
bytecodealliance:mainfrom
elliottt:trevor/validation-error

Conversation

@elliottt

@elliottt elliottt commented Oct 13, 2023 •

Copy link
Copy Markdown
Member

Currently, validation of the following component fails, as the resource r is not viewed as exported when validating the export of $types'. (More specifically, it's the use of r in the method r.foo that causes the error, as r is not viewed as exported at that point.)

(component
  (type $types (component
    (type $types' (instance
      (export $r "r" (type (sub resource)))
      (export "[method]r.foo" (func (param "self" (borrow $r))))
    ))
    (export (interface "local:demo/types") (type (eq $types')))
  ))
  (export "types" (type $types))
)

This PR addresses that by augmenting the set of named types when processing an ComponentInstance export, extending the set with the explicitly exported resources in that instance.

An alternate approach would be to extend the special handling of instances in validate_and_register_named_types to the ComponentEntityType::Type case as well, which would be a more targeted change than the one this PR currently proposes.

@elliottt
elliottt marked this pull request as ready for review October 13, 2023 18:54
@elliottt
elliottt force-pushed the trevor/validation-error branch from 1d09c08 to 94986d5 Compare October 13, 2023 19:17
Comment on lines +732 to +736
assert_eq!(path.len(), 1);
TypeId {
index: path[0] as u32,
unique_id: res.contextually_unique_id,
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost certainly incorrect, @alexcrichton what's the right way to go from a ResourceId to a TypeId?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh no worries! There's a few things that will need to change here. The first is that the usize index of path, IIRC, are indices into self.exports. That means that you'll look at self.exports[path[0]] for example and extract out the TypeId from there. You should be able to assert that it's a ComponentEntityType::Type and return the created field for the TypeId return value here.

Additionally though you won't be able to assert that the path length is 1. For example this module:

(component
  (type (export "x") (instance
    (export $x "x" (instance
      (export "x" (type (sub resource)))
    ))
  ))
)

trips the assertion. A more complete example that doesn't validate today but should then validate with your PR is:

(component
  (type (export "x") (instance
    (export $i "i" (instance
      (export "x" (type (sub resource)))
    ))

    (alias export $i "x" (type $x))
    (export "f" (func (param "x" (borrow $x))))
  ))
)

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I believe that this is the correct approach instead of validate_and_register_named_types. The distinguishing feature there is that when an instance type is exported it shouldn't mutate self and add more exported resources since the resources are only considered exported for that one singular type itself and nothing else.

@@ -0,0 +1,30 @@
;; RUN: validate % --features component-model

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh for these kinds of tests they can go into tests/local/* to get a few extra tests related to printing and such.

@@ -0,0 +1,30 @@
;; RUN: validate % --features component-model

(component

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it might be good to add a more minimal test case in addition to this one:

(component
  (type (export "x") (instance
    (export $x "x" (type (sub resource)))
    (export "f" (func (param "x" (borrow $x))))
  ))
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the tests into tests/local/component-model, and added this one.

Comment on lines +732 to +736
assert_eq!(path.len(), 1);
TypeId {
index: path[0] as u32,
unique_id: res.contextually_unique_id,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh no worries! There's a few things that will need to change here. The first is that the usize index of path, IIRC, are indices into self.exports. That means that you'll look at self.exports[path[0]] for example and extract out the TypeId from there. You should be able to assert that it's a ComponentEntityType::Type and return the created field for the TypeId return value here.

Additionally though you won't be able to assert that the path length is 1. For example this module:

(component
  (type (export "x") (instance
    (export $x "x" (instance
      (export "x" (type (sub resource)))
    ))
  ))
)

trips the assertion. A more complete example that doesn't validate today but should then validate with your PR is:

(component
  (type (export "x") (instance
    (export $i "i" (instance
      (export "x" (type (sub resource)))
    ))

    (alias export $i "x" (type $x))
    (export "f" (func (param "x" (borrow $x))))
  ))
)

Type::ComponentInstance(ty) => {
let mut with_resources = HashSet::new();

let set = if ty.explicit_resources.is_empty() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While a bit subtle, I think that this check may want to look at the defined_resources list rather than look at the explicit_resources map. The reason for that is that concrete instances will have an empty list there but the instance types, which this PR is fixing, may not have an empty defined_resources list. On the other hand though it's frequent for any kind of instance to have an explicit_resources array set, so this I don't think is the right branch in the implementation.

The reason for this is that the set of exported resources should be only judiciously added to and for normal instances everywhere the previous additions to the set should already be taken care of. In effect by adding all explicit_resources it's forcing this check to basically pass.

So, in other words, I think this check should be refactored to look more like:

                let set = if ty.defined_resources.is_empty() {
                    set
                } else {
                    with_resources = set.clone();
                    for resource in ty.defined_resources.iter() {
                        with_resources.insert(ty.type_id_of(resource));
                    }
                    &with_resources
                };

or something along those lines where type_id_of is the body of the loop in the explicit_resources method you added. Either that or rename the added explicit_resources to defined_resources and have the same loop you have today returning an iterator (actually after typing this out I prefer the latter style here which you already have)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't only looking at defined_resources mean that the test you mentioned above wouldn't work?

(component
  (type (export "x") (instance
    (export $i "i" (instance
      (export "x" (type (sub resource)))
    ))

    (alias export $i "x" (type $x))
    (export "f" (func (param "x" (borrow $x))))
  ))
)

@elliottt

Copy link
Copy Markdown
Member Author

After some discussion with @alexcrichton I'm going to close this. We can work around this bug with the upcoming wit encoding changes, and there are some dragons lurking in how we convert resource ids back to type ids.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants