summaryrefslogtreecommitdiff
path: root/deps/v8/tools/clusterfuzz/js_fuzzer/test/test_mutate_function_calls.js
blob: 292c1c0c7e83f6e65b090c9868c6d083790dc296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Tests for mutating funciton calls.
 */

'use strict';

const sinon = require('sinon');

const helpers = require('./helpers.js');
const random = require('../random.js');
const scriptMutator = require('../script_mutator.js');
const sourceHelpers = require('../source_helpers.js');
const functionCallMutator = require('../mutators/function_call_mutator.js');

const sandbox = sinon.createSandbox();

function loadAndMutate(input_file) {
  const source = helpers.loadTestData(input_file);

  const settings = scriptMutator.defaultSettings();
  settings['engine'] = 'V8';
  settings['MUTATE_FUNCTION_CALLS'] = 1.0;

  const mutator = new functionCallMutator.FunctionCallMutator(settings);
  mutator.mutate(source);
  return source;
}

describe('Mutate functions', () => {
  afterEach(() => {
    sandbox.restore();
  });

  it('is robust without available functions', () => {
    sandbox.stub(random, 'random').callsFake(() => { return 0.3; });

    // We just ensure here that mutating this file doesn't throw.
    loadAndMutate('mutate_function_call.js');
  });

  it('optimizes functions in V8', () => {
    sandbox.stub(random, 'random').callsFake(() => { return 0.5; });

    const source = loadAndMutate('mutate_function_call.js');
    const mutated = sourceHelpers.generateCode(source);
    helpers.assertExpectedResult(
        'mutate_function_call_expected.js', mutated);
  });

  it('compiles functions in V8 to baseline', () => {
    sandbox.stub(random, 'random').callsFake(() => { return 0.7; });

    const source = loadAndMutate('mutate_function_call.js');
    const mutated = sourceHelpers.generateCode(source);
    helpers.assertExpectedResult(
        'mutate_function_call_baseline_expected.js', mutated);
  });

  it('deoptimizes functions in V8', () => {
    sandbox.stub(random, 'random').callsFake(() => { return 0.8; });

    const source = loadAndMutate('mutate_function_call.js');
    const mutated = sourceHelpers.generateCode(source);
    helpers.assertExpectedResult(
        'mutate_function_call_deopt_expected.js', mutated);
  });
});