summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/test-poison-disasm-arm64.cc
blob: 32f4315e3a0b4855d7aff98b3ae1262eb5d6e525 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2019 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.

// The C++ style guide recommends using <re2> instead of <regex>. However, the
// former isn't available in V8.
#include <regex>  // NOLINT(build/c++11)
#include <vector>

#include "src/codegen/arm64/register-arm64.h"
#include "test/cctest/cctest.h"
#include "test/cctest/disasm-regex-helper.h"

namespace v8 {
namespace internal {

namespace {
// Poison register.
const int kPRegCode = kSpeculationPoisonRegister.code();
const std::string kPReg =  // NOLINT(runtime/string)
    "x" + std::to_string(kPRegCode);
}  // namespace

TEST(DisasmPoisonMonomorphicLoad) {
#ifdef ENABLE_DISASSEMBLER
  if (i::FLAG_always_opt || !i::FLAG_opt) return;
  // TODO(9684): Re-enable for TurboProp if necessary.
  if (i::FLAG_turboprop) return;

  i::FLAG_allow_natives_syntax = true;
  i::FLAG_untrusted_code_mitigations = true;

  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  CompileRun(
      "function mono(o) { return o.x; };"
      "%PrepareFunctionForOptimization(mono);"
      "mono({ x : 1 });"
      "mono({ x : 1 });"
      "%OptimizeFunctionOnNextCall(mono);"
      "mono({ x : 1 });");

  // Matches that the property access sequence is instrumented with
  // poisoning.
#if defined(V8_COMPRESS_POINTERS)
  std::vector<std::string> patterns_array = {
      "ldur <<Map:w[0-9]+>>, \\[<<Obj:x[0-9]+>>, #-1\\]",  // load map
      "ldr <<ExpMap:w[0-9]+>>, pc",                        // load expected map
      "cmp <<Map>>, <<ExpMap>>",                           // compare maps
      "b.ne",                                              // deopt if different
      "csel " + kPReg + ", xzr, " + kPReg + ", ne",        // update the poison
      "csdb",                                              // spec. barrier
      "ldur w<<Field:[0-9]+>>, \\[<<Obj>>, #[0-9]+\\]",    // load the field
      "and x<<Field>>, x<<Field>>, " + kPReg,              // apply the poison
  };
#else
  std::vector<std::string> patterns_array = {
      "ldur <<Map:x[0-9]+>>, \\[<<Obj:x[0-9]+>>, #-1\\]",  // load map
      "ldr <<ExpMap:x[0-9]+>>, pc",                        // load expected map
      "cmp <<Map>>, <<ExpMap>>",                           // compare maps
      "b.ne",                                              // deopt if different
      "csel " + kPReg + ", xzr, " + kPReg + ", ne",        // update the poison
      "csdb",                                              // spec. barrier
      "ldur <<Field:x[0-9]+>>, \\[<<Obj>>, #[0-9]+\\]",    // load the field
      "and <<Field>>, <<Field>>, " + kPReg,                // apply the poison
  };
#endif
  CHECK(CheckDisassemblyRegexPatterns("mono", patterns_array));
#endif  // ENABLE_DISASSEMBLER
}

TEST(DisasmPoisonPolymorphicLoad) {
#ifdef ENABLE_DISASSEMBLER
  if (i::FLAG_always_opt || !i::FLAG_opt) return;
  // TODO(9684): Re-enable for TurboProp if necessary.
  if (i::FLAG_turboprop) return;

  i::FLAG_allow_natives_syntax = true;
  i::FLAG_untrusted_code_mitigations = true;

  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  CompileRun(
      "function poly(o) { return o.x + 1; };"
      "let o1 = { x : 1 };"
      "let o2 = { y : 1 };"
      "o2.x = 2;"
      "%PrepareFunctionForOptimization(poly);"
      "poly(o2);"
      "poly(o1);"
      "poly(o2);"
      "%OptimizeFunctionOnNextCall(poly);"
      "poly(o1);");

  // Matches that the property access sequence is instrumented with
  // poisoning.
#if defined(V8_COMPRESS_POINTERS)
  std::vector<std::string> patterns_array = {
      "ldur <<Map0:w[0-9]+>>, \\[<<Obj:x[0-9]+>>, #-1\\]",  // load map
      "ldr <<ExpMap:w[0-9]+>>, pc",                         // load map const #1
      "cmp <<Map0>>, <<ExpMap>>",                           // compare maps
      "b.eq",                                               // ? go to the load
      "csel " + kPReg + ", xzr, " + kPReg + ", eq",         // update the poison
      "csdb",                                               // spec. barrier
      "ldur <<Map1:w[0-9]+>>, \\[<<Obj>>, #-1\\]",          // load map
      "ldr <<ExpMap1:w[0-9]+>>, pc",                        // load map const #2
      "cmp <<Map1>>, <<ExpMap1>>",                          // compare maps
      "b.ne",                                            // deopt if different
      "csel " + kPReg + ", xzr, " + kPReg + ", ne",      // update the poison
      "csdb",                                            // spec. barrier
      "ldur w<<Field:[0-9]+>>, \\[<<Obj>>, #[0-9]+\\]",  // load the field
      "and x<<Field>>, x<<Field>>, " + kPReg,            // apply the poison
      "asr w[0-9]+, w<<Field>>, #1",                     // untag
      "b",                                               // goto merge point
      // Lcase1:
      "csel " + kPReg + ", xzr, " + kPReg + ", ne",      // update the poison
      "csdb",                                            // spec. barrier
      "ldur w<<BSt:[0-9]+>>, \\[<<Obj>>, #[0-9]+\\]",    // load backing store
                                                         // branchful decompress
      "add x<<BSt>>, x26, x<<BSt>>",                     // Add root to ref
      "and x<<BSt>>, x<<BSt>>, " + kPReg,                // apply the poison
      "ldur w<<Prop:[0-9]+>>, \\[x<<BSt>>, #[0-9]+\\]",  // load the property
      "and x<<Prop>>, x<<Prop>>, " + kPReg,              // apply the poison
                                                         // Ldone:
  };
#else
  std::vector<std::string> patterns_array = {
      "ldur <<Map0:x[0-9]+>>, \\[<<Obj:x[0-9]+>>, #-1\\]",  // load map
      "ldr <<ExpMap0:x[0-9]+>>, pc",                        // load map const #1
      "cmp <<Map0>>, <<ExpMap0>>",                          // compare maps
      "b.eq",                                               // ? go to the load
      "csel " + kPReg + ", xzr, " + kPReg + ", eq",         // update the poison
      "csdb",                                               // spec. barrier
      "ldur <<Map1:x[0-9]+>>, \\[<<Obj>>, #-1\\]",          // load map
      "ldr <<ExpMap1:x[0-9]+>>, pc",                        // load map const #2
      "cmp <<Map1>>, <<ExpMap1>>",                          // compare maps
      "b.ne",                                            // deopt if different
      "csel " + kPReg + ", xzr, " + kPReg + ", ne",      // update the poison
      "csdb",                                            // spec. barrier
      "ldur x<<Field:[0-9]+>>, \\[<<Obj>>, #[0-9]+\\]",  // load the field
      "and x<<Field>>, x<<Field>>, " + kPReg,            // apply the poison
#ifdef V8_31BIT_SMIS_ON_64BIT_ARCH
      "asr w<<Field>>, w<<Field>>, #1",                  // untag
#else
      "asr x[0-9]+, x<<Field>>, #32",                    // untag
#endif
      "b",                                               // goto merge point
      // Lcase1:
      "csel " + kPReg + ", xzr, " + kPReg + ", ne",     // update the poison
      "csdb",                                           // spec. barrier
      "ldur <<BSt:x[0-9]+>>, \\[<<Obj>>, #[0-9]+\\]",   // load backing store
      "and <<BSt>>, <<BSt>>, " + kPReg,                 // apply the poison
      "ldur <<Prop:x[0-9]+>>, \\[<<BSt>>, #[0-9]+\\]",  // load the property
      "and <<Prop>>, <<Prop>>, " + kPReg,               // apply the poison
                                                        // Ldone:
  };
#endif
  CHECK(CheckDisassemblyRegexPatterns("poly", patterns_array));
#endif  // ENABLE_DISASSEMBLER
}

TEST(DisasmPoisonMonomorphicLoadFloat64) {
#ifdef ENABLE_DISASSEMBLER
  if (i::FLAG_always_opt || !i::FLAG_opt) return;
  // TODO(9684): Re-enable for TurboProp if necessary.
  if (i::FLAG_turboprop) return;

  i::FLAG_allow_natives_syntax = true;
  i::FLAG_untrusted_code_mitigations = true;

  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  CompileRun(
      "function mono(o) { return o.x; }"
      "%PrepareFunctionForOptimization(mono);"
      "mono({ x : 1.1 });"
      "mono({ x : 1.1 });"
      "%OptimizeFunctionOnNextCall(mono);"
      "mono({ x : 1.1 });");

  // Matches that the property access sequence is instrumented with
  // poisoning.
#if defined(V8_COMPRESS_POINTERS)
  std::vector<std::string> patterns_array = {
      "ldur <<Map:w[0-9]+>>, \\[<<Obj:x[0-9]+>>, #-1\\]",  // load map
      "ldr <<ExpMap:w[0-9]+>>, pc",                        // load expected map
      "cmp <<Map>>, <<ExpMap>>",                           // compare maps
      "b.ne",                                              // deopt if differ
      "csel " + kPReg + ", xzr, " + kPReg + ", ne",        // update the poison
      "csdb",                                              // spec. barrier
      "ldur w<<F1:[0-9]+>>, \\[<<Obj>>, #11\\]",           // load heap number
      "add x<<F1>>, x26, x<<F1>>",                         // Decompress ref
      "and x<<F1>>, x<<F1>>, " + kPReg,                    // apply the poison
      "add <<Addr:x[0-9]+>>, x<<F1>>, #0x[0-9a-f]+",       // addr. calculation
      "and <<Addr>>, <<Addr>>, " + kPReg,                  // apply the poison
      "ldr d[0-9]+, \\[<<Addr>>\\]",                       // load Float64
  };
#else
  std::vector<std::string> patterns_array = {
      "ldur <<Map:x[0-9]+>>, \\[<<Obj:x[0-9]+>>, #-1\\]",  // load map
      "ldr <<ExpMap:x[0-9]+>>, pc",                        // load expected map
      "cmp <<Map>>, <<ExpMap>>",                           // compare maps
      "b.ne",                                              // deopt if differ
      "csel " + kPReg + ", xzr, " + kPReg + ", ne",        // update the poison
      "csdb",                                              // spec. barrier
#if V8_DOUBLE_FIELDS_UNBOXING
      "add <<Addr:x[0-9]+>>, <<Obj>>, #0x[0-9a-f]+",       // addr. calculation
#else
      "ldur <<F1:x[0-9]+>>, \\[<<Obj>>, #23\\]",           // load heap number
      "and <<F1>>, <<F1>>, " + kPReg,                      // apply the poison
      "add <<Addr:x[0-9]+>>, <<F1>>, #0x7",                // addr. calculation
#endif
      "and <<Addr>>, <<Addr>>, " + kPReg,                  // apply the poison
      "ldr d[0-9]+, \\[<<Addr>>\\]",                       // load Float64
  };
#endif
  CHECK(CheckDisassemblyRegexPatterns("mono", patterns_array));
#endif  // ENABLE_DISASSEMBLER
}

}  // namespace internal
}  // namespace v8