background-compile-task-unittest.cc 8.99 KB
Newer Older
1
// Copyright 2018 the V8 project authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

7
#include "include/v8.h"
8
#include "src/api-inl.h"
9
#include "src/ast/ast.h"
10
#include "src/ast/scopes.h"
11
#include "src/base/platform/semaphore.h"
12
#include "src/base/template-utils.h"
13
#include "src/compiler.h"
14
#include "src/flags.h"
15
#include "src/isolate-inl.h"
16
#include "src/objects/smi.h"
17
#include "src/parsing/parse-info.h"
18
#include "src/parsing/parser.h"
19
#include "src/parsing/preparse-data.h"
20
#include "src/v8.h"
21
#include "src/zone/zone-list-inl.h"
22
#include "test/unittests/test-helpers.h"
23 24 25 26 27 28
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace v8 {
namespace internal {

29
class BackgroundCompileTaskTest : public TestWithNativeContext {
30
 public:
31 32
  BackgroundCompileTaskTest() : allocator_(isolate()->allocator()) {}
  ~BackgroundCompileTaskTest() override = default;
33

34
  AccountingAllocator* allocator() { return allocator_; }
35

36
  static void SetUpTestCase() {
37 38
    CHECK_NULL(save_flags_);
    save_flags_ = new SaveFlags();
39
    TestWithNativeContext::SetUpTestCase();
40 41 42
  }

  static void TearDownTestCase() {
43
    TestWithNativeContext::TearDownTestCase();
44 45 46
    CHECK_NOT_NULL(save_flags_);
    delete save_flags_;
    save_flags_ = nullptr;
47 48
  }

49
  BackgroundCompileTask* NewBackgroundCompileTask(
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      Isolate* isolate, Handle<SharedFunctionInfo> shared,
      size_t stack_size = FLAG_stack_size) {
    std::unique_ptr<ParseInfo> outer_parse_info =
        test::OuterParseInfoForShared(isolate, shared);
    AstValueFactory* ast_value_factory =
        outer_parse_info->GetOrCreateAstValueFactory();
    AstNodeFactory ast_node_factory(ast_value_factory,
                                    outer_parse_info->zone());

    const AstRawString* function_name =
        ast_value_factory->GetOneByteString("f");
    DeclarationScope* script_scope = new (outer_parse_info->zone())
        DeclarationScope(outer_parse_info->zone(), ast_value_factory);
    DeclarationScope* function_scope =
        new (outer_parse_info->zone()) DeclarationScope(
            outer_parse_info->zone(), script_scope, FUNCTION_SCOPE);
    function_scope->set_start_position(shared->StartPosition());
    function_scope->set_end_position(shared->EndPosition());
68 69
    std::vector<void*> buffer;
    ScopedPtrList<Statement> statements(&buffer);
70 71
    const FunctionLiteral* function_literal =
        ast_node_factory.NewFunctionLiteral(
72
            function_name, function_scope, statements, -1, -1, -1,
73 74 75 76 77
            FunctionLiteral::kNoDuplicateParameters,
            FunctionLiteral::kAnonymousExpression,
            FunctionLiteral::kShouldEagerCompile, shared->StartPosition(), true,
            shared->FunctionLiteralId(isolate), nullptr);

78 79
    return new BackgroundCompileTask(
        allocator(), outer_parse_info.get(), function_name, function_literal,
80
        isolate->counters()->worker_thread_runtime_call_stats(),
81
        isolate->counters()->compile_function_on_background(), FLAG_stack_size);
82 83
  }

84
 private:
85
  AccountingAllocator* allocator_;
86
  static SaveFlags* save_flags_;
87

88
  DISALLOW_COPY_AND_ASSIGN(BackgroundCompileTaskTest);
89 90
};

91
SaveFlags* BackgroundCompileTaskTest::save_flags_ = nullptr;
92

93
TEST_F(BackgroundCompileTaskTest, Construct) {
94 95 96
  Handle<SharedFunctionInfo> shared =
      test::CreateSharedFunctionInfo(isolate(), nullptr);
  ASSERT_FALSE(shared->is_compiled());
97 98
  std::unique_ptr<BackgroundCompileTask> task(
      NewBackgroundCompileTask(isolate(), shared));
jochen's avatar
jochen committed
99 100
}

101
TEST_F(BackgroundCompileTaskTest, SyntaxError) {
102
  test::ScriptResource* script = new test::ScriptResource("^^^", strlen("^^^"));
103 104
  Handle<SharedFunctionInfo> shared =
      test::CreateSharedFunctionInfo(isolate(), script);
105 106
  std::unique_ptr<BackgroundCompileTask> task(
      NewBackgroundCompileTask(isolate(), shared));
jochen's avatar
jochen committed
107

108 109 110
  task->Run();
  ASSERT_FALSE(Compiler::FinalizeBackgroundCompileTask(
      task.get(), shared, isolate(), Compiler::KEEP_EXCEPTION));
111
  ASSERT_TRUE(isolate()->has_pending_exception());
112

113
  isolate()->clear_pending_exception();
114 115
}

116
TEST_F(BackgroundCompileTaskTest, CompileAndRun) {
117
  const char raw_script[] =
118 119 120 121 122 123 124 125
      "function g() {\n"
      "  f = function(a) {\n"
      "        for (var i = 0; i < 3; i++) { a += 20; }\n"
      "        return a;\n"
      "      }\n"
      "  return f;\n"
      "}\n"
      "g();";
126 127
  test::ScriptResource* script =
      new test::ScriptResource(raw_script, strlen(raw_script));
128
  Handle<JSFunction> f = RunJS<JSFunction>(script);
129 130
  Handle<SharedFunctionInfo> shared = handle(f->shared(), isolate());
  ASSERT_FALSE(shared->is_compiled());
131 132
  std::unique_ptr<BackgroundCompileTask> task(
      NewBackgroundCompileTask(isolate(), shared));
133

134 135 136
  task->Run();
  ASSERT_TRUE(Compiler::FinalizeBackgroundCompileTask(
      task.get(), shared, isolate(), Compiler::KEEP_EXCEPTION));
137
  ASSERT_TRUE(shared->is_compiled());
138

139
  Smi value = Smi::cast(*RunJS("f(100);"));
140 141 142
  ASSERT_TRUE(value == Smi::FromInt(160));
}

143
TEST_F(BackgroundCompileTaskTest, CompileFailure) {
144
  std::string raw_script("() { var a = ");
145
  for (int i = 0; i < 10000; i++) {
146 147 148 149 150
    // TODO(leszeks): Figure out a more "unit-test-y" way of forcing an analysis
    // failure than a binop stack overflow.

    // Alternate + and - to avoid n-ary operation nodes.
    raw_script += "'x' + 'x' - ";
151 152
  }
  raw_script += " 'x'; }";
153 154
  test::ScriptResource* script =
      new test::ScriptResource(raw_script.c_str(), strlen(raw_script.c_str()));
155 156
  Handle<SharedFunctionInfo> shared =
      test::CreateSharedFunctionInfo(isolate(), script);
157 158
  std::unique_ptr<BackgroundCompileTask> task(
      NewBackgroundCompileTask(isolate(), shared, 100));
159

160 161 162
  task->Run();
  ASSERT_FALSE(Compiler::FinalizeBackgroundCompileTask(
      task.get(), shared, isolate(), Compiler::KEEP_EXCEPTION));
163
  ASSERT_TRUE(isolate()->has_pending_exception());
164

165
  isolate()->clear_pending_exception();
166 167
}

168 169
class CompileTask : public Task {
 public:
170 171
  CompileTask(BackgroundCompileTask* task, base::Semaphore* semaphore)
      : task_(task), semaphore_(semaphore) {}
172
  ~CompileTask() override = default;
173 174

  void Run() override {
175
    task_->Run();
176 177 178 179
    semaphore_->Signal();
  }

 private:
180
  BackgroundCompileTask* task_;
181 182 183 184
  base::Semaphore* semaphore_;
  DISALLOW_COPY_AND_ASSIGN(CompileTask);
};

185
TEST_F(BackgroundCompileTaskTest, CompileOnBackgroundThread) {
186 187 188 189 190 191 192
  const char* raw_script =
      "(a, b) {\n"
      "  var c = a + b;\n"
      "  function bar() { return b }\n"
      "  var d = { foo: 100, bar : bar() }\n"
      "  return bar;"
      "}";
193 194
  test::ScriptResource* script =
      new test::ScriptResource(raw_script, strlen(raw_script));
195 196
  Handle<SharedFunctionInfo> shared =
      test::CreateSharedFunctionInfo(isolate(), script);
197 198
  std::unique_ptr<BackgroundCompileTask> task(
      NewBackgroundCompileTask(isolate(), shared));
199 200

  base::Semaphore semaphore(0);
201
  auto background_task = base::make_unique<CompileTask>(task.get(), &semaphore);
202

203
  V8::GetCurrentPlatform()->CallOnWorkerThread(std::move(background_task));
204
  semaphore.Wait();
205 206 207
  ASSERT_TRUE(Compiler::FinalizeBackgroundCompileTask(
      task.get(), shared, isolate(), Compiler::KEEP_EXCEPTION));
  ASSERT_TRUE(shared->is_compiled());
208 209
}

210
TEST_F(BackgroundCompileTaskTest, EagerInnerFunctions) {
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  const char raw_script[] =
      "function g() {\n"
      "  f = function() {\n"
      "    // Simulate an eager IIFE with brackets.\n "
      "    var e = (function () { return 42; });\n"
      "    return e;\n"
      "  }\n"
      "  return f;\n"
      "}\n"
      "g();";
  test::ScriptResource* script =
      new test::ScriptResource(raw_script, strlen(raw_script));
  Handle<JSFunction> f = RunJS<JSFunction>(script);
  Handle<SharedFunctionInfo> shared = handle(f->shared(), isolate());
  ASSERT_FALSE(shared->is_compiled());
226 227 228 229 230 231
  std::unique_ptr<BackgroundCompileTask> task(
      NewBackgroundCompileTask(isolate(), shared));

  task->Run();
  ASSERT_TRUE(Compiler::FinalizeBackgroundCompileTask(
      task.get(), shared, isolate(), Compiler::KEEP_EXCEPTION));
232 233 234 235 236 237 238
  ASSERT_TRUE(shared->is_compiled());

  Handle<JSFunction> e = RunJS<JSFunction>("f();");

  ASSERT_TRUE(e->shared()->is_compiled());
}

239
TEST_F(BackgroundCompileTaskTest, LazyInnerFunctions) {
240 241 242
  const char raw_script[] =
      "function g() {\n"
      "  f = function() {\n"
243
      "    function e() { return 42; };\n"
244 245 246 247 248 249 250
      "    return e;\n"
      "  }\n"
      "  return f;\n"
      "}\n"
      "g();";
  test::ScriptResource* script =
      new test::ScriptResource(raw_script, strlen(raw_script));
251
  Handle<JSFunction> f = RunJS<JSFunction>(script);
252 253
  Handle<SharedFunctionInfo> shared = handle(f->shared(), isolate());
  ASSERT_FALSE(shared->is_compiled());
254 255 256 257 258 259
  std::unique_ptr<BackgroundCompileTask> task(
      NewBackgroundCompileTask(isolate(), shared));

  task->Run();
  ASSERT_TRUE(Compiler::FinalizeBackgroundCompileTask(
      task.get(), shared, isolate(), Compiler::KEEP_EXCEPTION));
260
  ASSERT_TRUE(shared->is_compiled());
261

262
  Handle<JSFunction> e = RunJS<JSFunction>("f();");
263

264
  ASSERT_FALSE(e->shared()->is_compiled());
265 266
}

267 268
}  // namespace internal
}  // namespace v8