1// Copyright 2019 the V8 project 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 "src/execution/protectors.h"
6
7#include "src/execution/isolate-inl.h"
8#include "src/execution/protectors-inl.h"
9#include "src/handles/handles-inl.h"
10#include "src/objects/contexts.h"
11#include "src/objects/property-cell.h"
12#include "src/objects/smi.h"
13#include "src/tracing/trace-event.h"
14#include "src/utils/utils.h"
15
16namespace v8 {
17namespace internal {
18
19namespace {
20
21void TraceProtectorInvalidation(const char* protector_name) {
22  DCHECK(FLAG_trace_protector_invalidation);
23  static constexpr char kInvalidateProtectorTracingCategory[] =
24      "V8.InvalidateProtector";
25  static constexpr char kInvalidateProtectorTracingArg[] = "protector-name";
26
27  DCHECK(FLAG_trace_protector_invalidation);
28
29  // TODO(jgruber): Remove the PrintF once tracing can output to stdout.
30  i::PrintF("Invalidating protector cell %s\n", protector_name);
31  TRACE_EVENT_INSTANT1("v8", kInvalidateProtectorTracingCategory,
32                       TRACE_EVENT_SCOPE_THREAD, kInvalidateProtectorTracingArg,
33                       protector_name);
34}
35
36// Static asserts to ensure we have a use counter for every protector. If this
37// fails, add the use counter in V8 and chromium. Note: IsDefined is not
38// strictly needed but clarifies the intent of the static assert.
39constexpr bool IsDefined(v8::Isolate::UseCounterFeature) { return true; }
40#define V(Name, ...) \
41  STATIC_ASSERT(IsDefined(v8::Isolate::kInvalidated##Name##Protector));
42
43DECLARED_PROTECTORS_ON_ISOLATE(V)
44#undef V
45
46}  // namespace
47
48#define INVALIDATE_PROTECTOR_ON_ISOLATE_DEFINITION(name, unused_index, cell) \
49  void Protectors::Invalidate##name(Isolate* isolate) {                      \
50    DCHECK(isolate->factory()->cell()->value().IsSmi());                     \
51    DCHECK(Is##name##Intact(isolate));                                       \
52    if (FLAG_trace_protector_invalidation) {                                 \
53      TraceProtectorInvalidation(#name);                                     \
54    }                                                                        \
55    isolate->CountUsage(v8::Isolate::kInvalidated##name##Protector);         \
56    isolate->factory()->cell()->InvalidateProtector();                       \
57    DCHECK(!Is##name##Intact(isolate));                                      \
58  }
59DECLARED_PROTECTORS_ON_ISOLATE(INVALIDATE_PROTECTOR_ON_ISOLATE_DEFINITION)
60#undef INVALIDATE_PROTECTOR_ON_ISOLATE_DEFINITION
61
62}  // namespace internal
63}  // namespace v8
64