Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToJSON unconditionally serializes all conditionally-exposed interface members #25281

Closed
shnmorimoto opened this issue Dec 13, 2019 · 16 comments · Fixed by #26054
Closed

ToJSON unconditionally serializes all conditionally-exposed interface members #25281

shnmorimoto opened this issue Dec 13, 2019 · 16 comments · Fixed by #26054
Labels
A-content/bindings The DOM bindings A-content/dom Interacting with the DOM from web content C-assigned There is someone working on resolving the issue

Comments

@shnmorimoto
Copy link
Contributor

This test will crash after this PR.

▶ CRASH [expected OK] /hr-time/idlharness.any.worker.html




│ internal error: entered unreachable code: Are we trying to expose Performance.timing in workers? (thread WebWorker for http://web-platform.test:8000/hr-time/idlharness.any.worker.js, at components/script/dom/performan
ce.rs:394)
│ stack backtrace:
│ 0: backtrace::backtrace::trace_unsynchronized
│ 1: <servo::backtrace::Print as core::fmt::Debug>::fmt
│ 2: core::fmt::write
│ 3: <std::io::stdio::Stdout as std::io::Write>::write_fmt
│ 4: std::io::stdio::_print
│ 5: servo::backtrace::print
│ 6: servo::main::{{closure}}
│ 7: std::panicking::rust_panic_with_hook
│ 8: rust_begin_unwind
│ 9: std::panicking::begin_panic_fmt
│ 10: <script::dom::performance::Performance as script::dom::bindings::codegen::Bindings::PerformanceBinding::PerformanceBinding::PerformanceMethods>::Timing
│ 11: script::dom::bindings::codegen::Bindings::PerformanceBinding::PerformanceBinding::get_timing::{{closure}}
│ 12: core::ops::function::FnOnce::call_once
│ 13: <std::panic::AssertUnwindSafe as core::ops::function::FnOnce<()>>::call_once
│ 14: std::panicking::try::do_call
│ 15: __rust_maybe_catch_panic
│ 16: std::panicking::try
│ 17: std::panic::catch_unwind
│ 18: mozjs::panic::wrap_panic
│ 19: script::dom::bindings::codegen::Bindings::PerformanceBinding::PerformanceBinding::get_timing
│ 20: script::dom::bindings::codegen::Bindings::PerformanceBinding::PerformanceBinding::CollectJSONAttributes
│ 21: script::dom::bindings::codegen::Bindings::PerformanceBinding::PerformanceBinding::toJSON::{{closure}}
│ 22: core::ops::function::FnOnce::call_once
│ 23: <std::panic::AssertUnwindSafe as core::ops::function::FnOnce<()>>::call_once
│ 24: std::panicking::try::do_call
│ 25: __rust_maybe_catch_panic
│ 26: std::panicking::try
│ 27: std::panic::catch_unwind
│ 28: mozjs::panic::wrap_panic
│ 29: script::dom::bindings::codegen::Bindings::PerformanceBinding::PerformanceBinding::toJSON
│ 30: CallJitMethodOp
│ 31: script::dom::bindings::utils::generic_call
│ 32: script::dom::bindings::utils::generic_method
│ 33: _ZN2js23InternalCallOrConstructEP9JSContextRKN2JS8CallArgsENS_14MaybeConstructE
│ 34: _ZL9InterpretP9JSContextRN2js8RunStateE
│ 35: _ZN2js9RunScriptEP9JSContextRNS_8RunStateE
│ 36: _ZN2js23InternalCallOrConstructEP9JSContextRKN2JS8CallArgsENS_14MaybeConstructE
│ 37: _ZN2js4CallEP9JSContextN2JS6HandleINS2_5ValueEEES5_RKNS_13AnyInvokeArgsENS2_13MutableHandleIS4_EE
│ 38: _ZN2js3jit14InvokeFunctionEP9JSContextN2JS6HandleIP8JSObjectEEbbjPNS3_5ValueENS3_13MutableHandleIS8_EE
│ 39: _ZN2js3jit25InvokeFromInterpreterStubEP9JSContextPNS0_30InterpreterStubExitFrameLayoutE

│ [2019-12-13T14:29:19Z ERROR servo] internal error: entered unreachable code: Are we trying to expose Performance.timing in workers?
└ Pipeline failed in hard-fail mode. Crashing!

@jdm jdm added the A-content/dom Interacting with the DOM from web content label Dec 13, 2019
@jdm
Copy link
Member

jdm commented Dec 13, 2019

This looks like the generated CollectJSONAttributes for Performance does not take into account a) the set of globals that each attribute is exposed in and b) the global that is present when toJSON() is invoked.

@jdm jdm added the A-content/bindings The DOM bindings label Dec 13, 2019
@jdm
Copy link
Member

jdm commented Dec 13, 2019

Some relevant pointers:

for m in interface.members:
if m.isAttr() and not m.isStatic() and m.type.isJSONType():
name = m.identifier.name
ret += fill(
"""
rooted!(in(*cx) let mut temp = UndefinedValue());
if !get_${name}(cx, obj, this, JSJitGetterCallArgs { _base: temp.handle_mut().into() }) {
return false;
}
if !JS_DefineProperty(*cx, result.handle().into(),
${nameAsArray} as *const u8 as *const libc::c_char,
temp.handle(), JSPROP_ENUMERATE as u32) {
return false;
}
""",

def MemberCondition(pref, func, exposed):
"""
A string representing the condition for a member to actually be exposed.
Any of the arguments can be None. If not None, they should have the
following types:
pref: The name of the preference.
func: The name of the function.
exposed: One or more names of an exposed global.
"""
assert pref is None or isinstance(pref, str)
assert func is None or isinstance(func, str)
assert exposed is None or isinstance(exposed, set)
assert func is None or pref is None or exposed is None
if pref:
return 'Condition::Pref("%s")' % pref
if func:
return 'Condition::Func(%s)' % func
if exposed:
return ["Condition::Exposed(InterfaceObjectMap::Globals::%s)" % camel_to_upper_snake(i) for i in exposed]
return "Condition::Satisfied"

@jdm jdm changed the title Crash in /hr-time/idlharness.any.worker.html ToJSON unconditionally serializes all conditionally-exposed interface members Dec 13, 2019
@shnmorimoto
Copy link
Contributor Author

I would like to try and solve this one.
@highfive: assign me

@highfive highfive added the C-assigned There is someone working on resolving the issue label Dec 15, 2019
@highfive
Copy link

Hey @shnmorimoto! Thanks for your interest in working on this issue. It's now assigned to you!

@shnmorimoto
Copy link
Contributor Author

@jdm If there are a good document about codegen, I'd be happy if you could let me know.

@jdm
Copy link
Member

jdm commented Dec 16, 2019

Unfortunately it is not documented well :( #23332 is the issue tracking the work that added Exposed support for interface members, and #23500 is the PR for that work.

@shnmorimoto
Copy link
Contributor Author

Thank you for the helpful issue and PR.
I will refer to them and try to solve this issue.

@shnmorimoto
Copy link
Contributor Author

How do I view the code generated by CodegenRust.py?
I checked the options of mach but could not find it.

@pshaughn
Copy link
Member

It's still on the filesystem after the build ends; if you know a string you want to find, you can grep -r for it. There's probably an easier way but I don't happen to know it.

@shnmorimoto
Copy link
Contributor Author

@pshaughn Thanks! I'll give it a try!

@jdm
Copy link
Member

jdm commented Dec 24, 2019

You will find the generated files under target/debug/build/script-[hash]/out/Bindings/.

@shnmorimoto
Copy link
Contributor Author

I'm sorry to be away from here for a long time.

Could you confirm my understanding is correct?

  • Performance is only exposed to Window. This crash occurs when called from elsewhere.
  • CollectJSONAttributes should return false when called from where it is not exposed.
    (Actually I don't really understand what CollectJSONAttributes does..)

@shnmorimoto
Copy link
Contributor Author

shnmorimoto commented Mar 23, 2020

@jdm I added bellow code to CGCollectJSONAttributesMethod in CodegenRust.py

    def definition_body(self):
        ret = ''
        interface = self.descriptor.interface
        conditions = MemberCondition(None, None, interface.exposureSet)

        if isinstance(conditions, list):
            ret += "let condition = vec!["
            for condition in conditions:
                ret += condition
                ret += ","
            ret += "];\n"
            ret += """
                if !condition.iter().any(|c| c.is_satisfied(SafeJSContext::from_ptr(cx), Handle::from_raw(obj), Handle::from_raw(obj))) {
                  return false;
                } 
                """

and Regarding Performance, I expect

let condition = vec![Condition::Exposed(InterfaceObjectMap::Globals::WINDOW)]

since webidl is bellow.

[Exposed=Window]
partial interface Performance {
[SameObject]
readonly attribute PerformanceNavigationTiming timing;
[SameObject]
readonly attribute PerformanceNavigation navigation;
};

but got

let condition = vec![Condition::Exposed(InterfaceObjectMap::Globals::WINDOW),Condition::Exposed(InterfaceObjectMap::Globals::SERVICE_WORKER_GLOBAL_SCOPE),Condition::Exposed(InterfaceObjectMap::Globals::DEDICATED_WORKER_GLOBAL_SCOPE),];

How do I get expose definition from webidl?

@jdm
Copy link
Member

jdm commented Mar 25, 2020

@shnmorimoto The Performance interface is exposed to all of those via

[Exposed=(Window, Worker)]
. For the ToJSON, we'll need to look at the exposure set for each member.

@jdm
Copy link
Member

jdm commented Mar 25, 2020

More specifically, in this code we'll want to use m.exposureSet to create a list of conditions from MemberCondition, rather than using interface.exposure, and we'll need to call is_satisfied for each member and skip it if it's false.

@shnmorimoto
Copy link
Contributor Author

Thanks for the helpful info! I got it.
I'll do that.

bors-servo added a commit that referenced this issue Mar 28, 2020
…zes, r=<try>

Fix tojson unconditionally serializes

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #25281

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
bors-servo added a commit that referenced this issue Mar 29, 2020
…zes, r=<try>

Fix tojson unconditionally serializes

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #25281

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
bors-servo added a commit that referenced this issue Mar 31, 2020
…zes, r=jdm

Fix tojson unconditionally serializes

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #25281

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
bors-servo added a commit that referenced this issue Mar 31, 2020
…zes, r=jdm

Fix tojson unconditionally serializes

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #25281

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
bors-servo added a commit that referenced this issue Mar 31, 2020
…zes, r=jdm

Fix tojson unconditionally serializes

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #25281

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-content/bindings The DOM bindings A-content/dom Interacting with the DOM from web content C-assigned There is someone working on resolving the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants