2
* Copyright (C) 2019 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#include "FileBasedFuzzerAgent.h"
29
#include "CodeBlock.h"
30
#include "FuzzerPredictions.h"
31
#include <wtf/AnsiColors.h>
35
FileBasedFuzzerAgent::FileBasedFuzzerAgent(VM& vm)
36
: FileBasedFuzzerAgentBase(vm)
40
SpeculatedType FileBasedFuzzerAgent::getPredictionInternal(CodeBlock* codeBlock, PredictionTarget& target, SpeculatedType original)
42
FuzzerPredictions& fuzzerPredictions = ensureGlobalFuzzerPredictions();
43
Optional<SpeculatedType> generated = fuzzerPredictions.predictionFor(target.lookupKey);
45
SourceProvider* provider = codeBlock->source().provider();
46
auto sourceUpToDivot = provider->source().substring(target.divot - target.startOffset, target.startOffset);
47
auto sourceAfterDivot = provider->source().substring(target.divot, target.endOffset);
49
switch (target.opcodeId) {
50
// FIXME: these can not be targeted at all due to the bugs below
51
case op_to_this: // broken https://bugs.webkit.org/show_bug.cgi?id=203555
52
case op_get_argument: // broken https://bugs.webkit.org/show_bug.cgi?id=203554
55
// FIXME: the output of codeBlock->expressionRangeForBytecodeIndex() allows for some of
56
// these opcodes to have predictions, but not all instances can be reliably targeted.
57
case op_bitand: // partially broken https://bugs.webkit.org/show_bug.cgi?id=203604
58
case op_bitor: // partially broken https://bugs.webkit.org/show_bug.cgi?id=203604
59
case op_bitxor: // partially broken https://bugs.webkit.org/show_bug.cgi?id=203604
60
case op_bitnot: // partially broken
61
case op_get_from_scope: // partially broken https://bugs.webkit.org/show_bug.cgi?id=203603
62
case op_get_from_arguments: // partially broken https://bugs.webkit.org/show_bug.cgi?id=203608
63
case op_get_by_val: // partially broken https://bugs.webkit.org/show_bug.cgi?id=203665
64
case op_rshift: // partially broken https://bugs.webkit.org/show_bug.cgi?id=203664
65
case op_lshift: // partially broken https://bugs.webkit.org/show_bug.cgi?id=203664
66
case op_to_number: // partially broken
67
case op_get_by_id: // sometimes occurs implicitly for things related to Symbol.iterator
72
case op_call: // op_call appears implicitly in for-of loops, generators, spread/rest elements, destructuring assignment
74
if (sourceAfterDivot.containsIgnoringASCIICase("of "))
76
if (sourceAfterDivot.containsIgnoringASCIICase("..."))
78
if (sourceAfterDivot.containsIgnoringASCIICase("yield"))
80
if (sourceAfterDivot.startsWith('[') && sourceAfterDivot.endsWith("]"))
82
if (sourceUpToDivot.containsIgnoringASCIICase("yield"))
84
if (sourceUpToDivot == "...")
86
if (!target.startOffset && !target.endOffset)
91
case op_get_by_val_with_this:
92
case op_get_direct_pname:
94
case op_construct_varargs:
98
case op_tail_call_varargs:
99
case op_get_by_id_with_this:
103
RELEASE_ASSERT_NOT_REACHED_WITH_MESSAGE("Unhandled opcode %s", opcodeNames[target.opcodeId]);
106
if (Options::dumpFuzzerAgentPredictions())
107
dataLogLn(MAGENTA(BOLD(target.bytecodeIndex)), " ", BOLD(YELLOW(target.opcodeId)), " missing prediction for: ", RED(BOLD(target.lookupKey)), " ", GREEN(target.sourceFilename), ":", CYAN(target.line), ":", CYAN(target.column), " divot: ", target.divot, " -", target.startOffset, " +", target.endOffset, " name: '", YELLOW(codeBlock->inferredName()), "' source: '", BLUE(sourceUpToDivot), BLUE(BOLD(sourceAfterDivot)), "'");
109
RELEASE_ASSERT_WITH_MESSAGE(!Options::requirePredictionForFileBasedFuzzerAgent(), "Missing expected prediction in FuzzerAgent");
112
if (Options::dumpFuzzerAgentPredictions())
113
dataLogLn(YELLOW(target.opcodeId), " ", CYAN(target.lookupKey), " original: ", CYAN(BOLD(SpeculationDump(original))), " generated: ", MAGENTA(BOLD(SpeculationDump(*generated))));