~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/JavaScriptCore/runtime/FileBasedFuzzerAgent.cpp

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2019 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
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.
 
12
 *
 
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.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "FileBasedFuzzerAgent.h"
 
28
 
 
29
#include "CodeBlock.h"
 
30
#include "FuzzerPredictions.h"
 
31
#include <wtf/AnsiColors.h>
 
32
 
 
33
namespace JSC {
 
34
 
 
35
FileBasedFuzzerAgent::FileBasedFuzzerAgent(VM& vm)
 
36
    : FileBasedFuzzerAgentBase(vm)
 
37
{
 
38
}
 
39
 
 
40
SpeculatedType FileBasedFuzzerAgent::getPredictionInternal(CodeBlock* codeBlock, PredictionTarget& target, SpeculatedType original)
 
41
{
 
42
    FuzzerPredictions& fuzzerPredictions = ensureGlobalFuzzerPredictions();
 
43
    Optional<SpeculatedType> generated = fuzzerPredictions.predictionFor(target.lookupKey);
 
44
 
 
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);
 
48
 
 
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
 
53
        return original;
 
54
 
 
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
 
68
        if (!generated)
 
69
            return original;
 
70
        break;
 
71
 
 
72
    case op_call: // op_call appears implicitly in for-of loops, generators, spread/rest elements, destructuring assignment
 
73
        if (!generated) {
 
74
            if (sourceAfterDivot.containsIgnoringASCIICase("of "))
 
75
                return original;
 
76
            if (sourceAfterDivot.containsIgnoringASCIICase("..."))
 
77
                return original;
 
78
            if (sourceAfterDivot.containsIgnoringASCIICase("yield"))
 
79
                return original;
 
80
            if (sourceAfterDivot.startsWith('[') && sourceAfterDivot.endsWith("]"))
 
81
                return original;
 
82
            if (sourceUpToDivot.containsIgnoringASCIICase("yield"))
 
83
                return original;
 
84
            if (sourceUpToDivot == "...")
 
85
                return original;
 
86
            if (!target.startOffset && !target.endOffset)
 
87
                return original;
 
88
        }
 
89
        break;
 
90
 
 
91
    case op_get_by_val_with_this:
 
92
    case op_get_direct_pname:
 
93
    case op_construct:
 
94
    case op_construct_varargs:
 
95
    case op_call_varargs:
 
96
    case op_call_eval:
 
97
    case op_tail_call:
 
98
    case op_tail_call_varargs:
 
99
    case op_get_by_id_with_this:
 
100
        break;
 
101
 
 
102
    default:
 
103
        RELEASE_ASSERT_NOT_REACHED_WITH_MESSAGE("Unhandled opcode %s", opcodeNames[target.opcodeId]);
 
104
    }
 
105
    if (!generated) {
 
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)), "'");
 
108
 
 
109
        RELEASE_ASSERT_WITH_MESSAGE(!Options::requirePredictionForFileBasedFuzzerAgent(), "Missing expected prediction in FuzzerAgent");
 
110
        return original;
 
111
    }
 
112
    if (Options::dumpFuzzerAgentPredictions())
 
113
        dataLogLn(YELLOW(target.opcodeId), " ", CYAN(target.lookupKey), " original: ", CYAN(BOLD(SpeculationDump(original))), " generated: ", MAGENTA(BOLD(SpeculationDump(*generated))));
 
114
    return *generated;
 
115
}
 
116
 
 
117
} // namespace JSC