Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / dom / base / fuzztest / FuzzStructuredClone.cpp
blob5473df2c8e84ed5ec4c8bf26d62899b7aa1b2774
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "FuzzingInterface.h"
9 #include "jsapi.h"
10 #include "js/StructuredClone.h"
11 #include "mozilla/dom/ipc/StructuredCloneData.h"
12 #include "mozilla/dom/ScriptSettings.h"
13 #include "mozilla/dom/StructuredCloneHolder.h"
14 #include "mozilla/dom/SimpleGlobalObject.h"
15 #include "mozilla/ErrorResult.h"
16 #include "mozilla/ScopeExit.h"
17 #include "mozilla/UniquePtr.h"
19 #include "nsCycleCollector.h"
21 using namespace mozilla;
22 using namespace mozilla::dom;
23 using namespace mozilla::dom::ipc;
25 JS::PersistentRooted<JSObject*> global;
27 static int FuzzingInitDomSC(int* argc, char*** argv) {
28 JSObject* simpleGlobal =
29 SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
30 global.init(mozilla::dom::RootingCx());
31 global.set(simpleGlobal);
32 return 0;
35 static int FuzzingRunDomSC(const uint8_t* data, size_t size) {
36 if (size < 8) {
37 return 0;
40 AutoJSAPI jsapi;
41 MOZ_RELEASE_ASSERT(jsapi.Init(global));
43 JSContext* cx = jsapi.cx();
44 auto gcGuard = mozilla::MakeScopeExit([&] {
45 JS::PrepareForFullGC(cx);
46 JS::NonIncrementalGC(cx, JS::GCOptions::Normal, JS::GCReason::API);
47 nsCycleCollector_collect(CCReason::API, nullptr);
48 });
50 // The internals of SCInput have a release assert about the padding
51 // of the data, so we fix it here to avoid performance problems
52 // during fuzzing.
53 size -= size % 8;
55 StructuredCloneData scdata;
56 if (!scdata.CopyExternalData(reinterpret_cast<const char*>(data), size)) {
57 return 0;
60 JS::Rooted<JS::Value> result(cx);
61 ErrorResult rv;
62 scdata.Read(cx, &result, rv);
64 rv.SuppressException();
66 return 0;
69 MOZ_FUZZING_INTERFACE_RAW(FuzzingInitDomSC, FuzzingRunDomSC,
70 StructuredCloneReaderDOM);