Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: third_party/WebKit/Source/platform/TextCodecFuzzer.cpp

Issue 2762763002: Reland "Fuzzer for TextCodecs" (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "wtf/text/TextCodec.h"
6
7 #include "platform/testing/BlinkFuzzerTestSupport.h"
8 #include "platform/testing/FuzzedDataProvider.h"
9 #include "wtf/text/CString.h"
10 #include "wtf/text/TextEncoding.h"
11 #include "wtf/text/TextEncodingRegistry.h"
12
13 using namespace blink;
14
15 // TODO(jsbell): This fuzzes code in wtf/ but has dependencies on platform/,
16 // so it must live in the latter directory. Once wtf/ moves into platform/wtf
17 // this should move there as well.
18
19 WTF::FlushBehavior kFlushBehavior[] = {WTF::DoNotFlush, WTF::FetchEOF,
20 WTF::DataEOF};
21
22 WTF::UnencodableHandling kUnencodableHandlingOptions[] = {
23 WTF::QuestionMarksForUnencodables, WTF::EntitiesForUnencodables,
24 WTF::URLEncodedEntitiesForUnencodables,
25 WTF::CSSEncodedEntitiesForUnencodables};
26
27 class TextCodecFuzzHarness {};
28 extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
29 InitializeBlinkFuzzTest(argc, argv);
30 return 0;
31 }
32
33 // Fuzzer for WTF::TextCodec.
34 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
35 // The fuzzer picks 3 bytes off the end of the data to initialize metadata, so
36 // abort if the input is smaller than that.
37 if (size < 3)
38 return 0;
39
40 // TODO(csharrison): When crbug.com/701825 is resolved, add the rest of the
41 // text codecs.
42
43 // Initializes the codec map.
44 static const WTF::TextEncoding encoding = WTF::TextEncoding(
45 #if defined(UTF_8)
46 "UTF-8"
47 #elif defined(WINDOWS_1252)
48 "windows-1252"
49 #endif
50 "");
51
52 FuzzedDataProvider fuzzedData(data, size);
53
54 // Initialize metadata using the fuzzed data.
55 bool stopOnError = fuzzedData.ConsumeBool();
56 WTF::UnencodableHandling unencodableHandling =
57 fuzzedData.PickValueInArray(kUnencodableHandlingOptions);
58 WTF::FlushBehavior flushBehavior =
59 fuzzedData.PickValueInArray(kFlushBehavior);
60
61 // Now, use the rest of the fuzzy data to stress test decoding and encoding.
62 const CString byteString = fuzzedData.ConsumeRemainingBytes();
63 std::unique_ptr<TextCodec> codec = newTextCodec(encoding);
64
65 // Treat as bytes-off-the-wire.
66 bool sawError;
67 const String decoded = codec->decode(byteString.data(), byteString.length(),
68 flushBehavior, stopOnError, sawError);
69
70 // Treat as blink 8-bit string (latin1).
71 if (size % sizeof(LChar) == 0) {
72 std::unique_ptr<TextCodec> codec = newTextCodec(encoding);
73 codec->encode(reinterpret_cast<const LChar*>(byteString.data()),
74 byteString.length() / sizeof(LChar), unencodableHandling);
75 }
76
77 // Treat as blink 16-bit string (utf-16) if there are an even number of bytes.
78 if (size % sizeof(UChar) == 0) {
79 std::unique_ptr<TextCodec> codec = newTextCodec(encoding);
80 codec->encode(reinterpret_cast<const UChar*>(byteString.data()),
81 byteString.length() / sizeof(UChar), unencodableHandling);
82 }
83
84 if (decoded.isNull())
85 return 0;
86
87 // Round trip the bytes (aka encode the decoded bytes).
88 if (decoded.is8Bit()) {
89 codec->encode(decoded.characters8(), decoded.length(), unencodableHandling);
90 } else {
91 codec->encode(decoded.characters16(), decoded.length(),
92 unencodableHandling);
93 }
94 return 0;
95 }
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/BUILD.gn ('k') | third_party/WebKit/Source/platform/testing/FuzzedDataProvider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698