Closed Bug 1419094 Opened 7 years ago Closed 6 years ago

Result-ify XDR transcoding

Categories

(Core :: JavaScript Engine, enhancement, P3)

enhancement

Tracking

()

RESOLVED FIXED
mozilla61
Tracking Status
firefox61 --- fixed

People

(Reporter: tcampbell, Assigned: tcampbell)

References

Details

Attachments

(2 files, 4 obsolete files)

While looking at Bug 1390856 and other XDR crashes, I threaded a Result type into the XDR internals to better handle errors. This is focused on internal methods and anything that may XDRState::fail.

We can decide if this is a good approach or not, but for now it identifies potential problems with XDR. I'd like to defer landing this for a bit anyways until we are happier about XDR crash numbers so we uplifts are easier.
Make use of ScopeExit instead of manual handling on errors
Equivalent transform with XdrResult being a simple struct around a bool. We should look at using a standard mozilla::Result instead, but that should be a small follow-up patch.
Attachment #8930139 - Flags: feedback?(nicolas.b.pierron)
Right now XDRState directly has low level int/string serialization APIS and top-level codeScript/codeFunction APIs. Remove codeConstValue since it is odd one out and we are a little inconsistent about handling.
Assignee: nobody → tcampbell
Priority: -- → P3
There are currently 35 locations where we return XdrResult::fail() (previously false), without calling XDRState::fail(). Almost all are OOM. These get caught by postProcessContextErrors (See Bug 1390856)
Remove junk file from patch.
Attachment #8930139 - Attachment is obsolete: true
Attachment #8930139 - Flags: feedback?(nicolas.b.pierron)
Attachment #8930142 - Flags: feedback?(nicolas.b.pierron)
Comment on attachment 8930142 [details] [diff] [review]
0002-Bug-1419094-Result-ify-XDR-internal-code.patch

Review of attachment 8930142 [details] [diff] [review]:
-----------------------------------------------------------------

::: js/src/vm/Xdr.h
@@ +28,5 @@
> +// Result type for XDR internal functions. Functions that may call
> +// XDRState::fail(...) should return this type.
> +struct XdrResult
> +{
> +    bool result;

this would be even better if this would be the enumerated type instead of a boolean.
Such that failures would be:

  return Err(JS::TranscodeResult_Throw);

instead of:

  return xdr->fail(JS::TranscodeResult_Throw)

@@ +39,5 @@
> +    static MOZ_ALWAYS_INLINE XdrResult fail() { return { false }; };
> +};
> +
> +// A try/result macro for XDR. See MOZ_TRY for more information.
> +#define XDR_TRY(expr) \

use MOZ_TRY and mozilla::Result or JS::Result.

@@ +510,5 @@
>      void endSubTree() override;
>  
>      // Append the content collected during the incremental encoding into the
>      // buffer given as argument.
> +    MOZ_MUST_USE js::XdrResult linearize(JS::TranscodeBuffer& buffer);

with mozilla::Result there would no longer be any need for MOZ_MUST_USE as this is part of the Result class annotation.
Attachment #8930142 - Flags: feedback?(nicolas.b.pierron)
Delta:
 - Add XDRResult as an alias to Result<Ok, JS::TranscodeResult>
 - Move resultCode_ into XDRCoderBase, to be used in AutoXDRTree destructor.
 - Make resultCode_ debug-only, and rely exclusively on the returned value.
 - Make XDRState<mode>::fail(...) checks that only if an exception is reported
   then we have the JS::TranscodeResult_Throw code.
 - Within XDR functions:
   . return true; --> return Ok();
   . return false --> return xdr->fail(JS::TranscodeResult_Throw);
   . if (!XDR(...)) return false; --> MOZ_TRY(XDR(...));
   . if (!XDR(...)) { ...; return false; }
       -->
     auto guard = mozilla::MakeScopeExit([&] { ...; });
     MOZ_TRY(XDR(...));
     guard.release();
 - Move the isAligned resultCode() condition to AutoXDRTree::~AutoXDRTree
   MOZ_ASSERT.

This patch fixes the issue found in Bug 1401939, which caused the patch to be
backed out, by properly setting the resultCode_ to JS::TranscodeResult_Throw
when an allocation failure happen.

This patch is based on Bug 1401939 changes, and should be landed with Bug 1401939 patch.
(Sorry this is a big review)
Attachment #8958507 - Flags: review?(tcampbell)
Comment on attachment 8958507 [details] [diff] [review]
Result-ify XDR functions.

Review of attachment 8958507 [details] [diff] [review]:
-----------------------------------------------------------------

Yay! Thanks for getting this done.

::: js/src/jsapi.cpp
@@ +7631,5 @@
>  {
>      XDREncoder encoder(cx, buffer, buffer.length());
>      RootedFunction funobj(cx, &funobjArg->as<JSFunction>());
> +    XDRResult res = encoder.codeFunction(&funobj);
> +    if (res.isErr()) {

Much nicer :)

::: js/src/vm/JSScript.cpp
@@ +2150,4 @@
>  
>          size_t byteLen = compressedLength ? compressedLength : (len * sizeof(char16_t));
>          if (mode == XDR_DECODE) {
>              uint8_t* p = xdr->cx()->template pod_malloc<uint8_t>(Max<size_t>(byteLen, 1));

mozilla::UniquePtr<uint8_t[], JS::FreePolicy> p(
    xdr->cx()->template pod_malloc<uint8_t>(Max<size_t>(byteLen, 1)));

::: js/src/vm/Xdr.h
@@ +184,5 @@
>  
>  class XDRCoderBase
>  {
> +#ifdef DEBUG
> +  private:

Nit: put private outside the macro
Attachment #8958507 - Flags: review?(tcampbell) → review+
Comment on attachment 8930142 [details] [diff] [review]
0002-Bug-1419094-Result-ify-XDR-internal-code.patch

We are taking :nbp's version of this patch instead.
Attachment #8930142 - Attachment is obsolete: true
Leave-open. Once :nbp's patch goes in, I'll rebase the other cleanups on top.
Keywords: leave-open
These are some small cleanups I wanted while we are touching this stuff.
Attachment #8930136 - Attachment is obsolete: true
Attachment #8930140 - Attachment is obsolete: true
Attachment #8958563 - Flags: review?(nicolas.b.pierron)
Comment on attachment 8958563 [details] [diff] [review]
Part-2-Cleanup-some-XDR-apis.patch

Review of attachment 8958563 [details] [diff] [review]:
-----------------------------------------------------------------

Way better to have all these XDR types in the same header!
Attachment #8958563 - Flags: review?(nicolas.b.pierron) → review+
Depends on: 1445595
Status: NEW → RESOLVED
Closed: 6 years ago
Keywords: leave-open
Resolution: --- → FIXED
Target Milestone: --- → mozilla61
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: