aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/test-poison-disasm-arm.cc
blob: bde584c3facace1079ffc17c6f4dc2a2d4a2b1b8 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2018 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 "src/api/api-inl.h"
#include "src/diagnostics/disassembler.h"
#include "src/objects/objects-inl.h"
#include "test/cctest/cctest.h"

namespace v8 {
namespace internal {

std::string DisassembleFunction(const char* function) {
  v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
  Handle<JSFunction> f = Handle<JSFunction>::cast(
      v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
          CcTest::global()->Get(context, v8_str(function)).ToLocalChecked())));

  Address begin = f->code().raw_instruction_start();
  Address end = f->code().raw_instruction_end();
  Isolate* isolate = CcTest::i_isolate();
  std::ostringstream os;
  Disassembler::Decode(isolate, &os, reinterpret_cast<byte*>(begin),
                       reinterpret_cast<byte*>(end),
                       CodeReference(handle(f->code(), isolate)));
  return os.str();
}

struct Matchers {
  std::string start = "0x[0-9a-f]+ +[0-9a-f]+ +[0-9a-f]+ +";
  std::regex map_load_re =
      std::regex(start + "ldr r([0-9]+), \\[r([0-9]+), #-1\\]");
  std::regex load_const_re = std::regex(start + "ldr r([0-9]+), \\[pc, .*");
  std::regex cmp_re = std::regex(start + "cmp r([0-9]+), r([0-9]+)");
  std::regex bne_re = std::regex(start + "bne (.*)");
  std::regex beq_re = std::regex(start + "beq (.*)");
  std::regex b_re = std::regex(start + "b (.*)");
  std::regex eorne_re =
      std::regex(start + "eorne r([0-9]+), r([0-9]+), r([0-9]+)");
  std::regex eoreq_re =
      std::regex(start + "eoreq r([0-9]+), r([0-9]+), r([0-9]+)");
  std::regex csdb_re = std::regex(start + "csdb");
  std::regex load_field_re =
      std::regex(start + "ldr r([0-9]+), \\[r([0-9]+), #\\+[0-9]+\\]");
  std::regex mask_re =
      std::regex(start + "and r([0-9]+), r([0-9]+), r([0-9]+)");
  std::regex untag_re = std::regex(start + "mov r([0-9]+), r([0-9]+), asr #1");

  std::string poison_reg = "9";
};

TEST(DisasmPoisonMonomorphicLoad) {
#ifdef ENABLE_DISASSEMBLER
  if (i::FLAG_always_opt || !i::FLAG_opt) 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 });");

  Matchers m;

  std::smatch match;
  std::string line;
  std::istringstream reader(DisassembleFunction("mono"));
  bool poisoning_sequence_found = false;
  while (std::getline(reader, line)) {
    if (std::regex_match(line, match, m.map_load_re)) {
      std::string map_reg = match[1];
      std::string object_reg = match[2];
      // Matches that the property access sequence is instrumented with
      // poisoning. We match the following sequence:
      //
      // ldr r1, [r0, #-1]                ; load map
      // ldr r2, [pc, #+104]              ; load expected map constant
      // cmp r1, r2                       ; compare maps
      // bne ...                          ; deopt if different
      // eorne r9, r9, r9                 ; update the poison
      // csdb                             ; speculation barrier
      // ldr r0, [r0, #+11]               ; load the field
      // and r0, r0, r9                   ; apply the poison

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.load_const_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.cmp_re));
      CHECK_EQ(match[1], map_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.bne_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.eorne_re));
      CHECK_EQ(match[1], m.poison_reg);
      CHECK_EQ(match[2], m.poison_reg);
      CHECK_EQ(match[3], m.poison_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.csdb_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.load_field_re));
      CHECK_EQ(match[2], object_reg);
      std::string field_reg = match[1];

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.mask_re));
      CHECK_EQ(match[1], field_reg);
      CHECK_EQ(match[2], field_reg);
      CHECK_EQ(match[3], m.poison_reg);

      poisoning_sequence_found = true;
      break;
    }
  }

  CHECK(poisoning_sequence_found);
#endif  // ENABLE_DISASSEMBLER
}

TEST(DisasmPoisonPolymorphicLoad) {
#ifdef ENABLE_DISASSEMBLER
  if (i::FLAG_always_opt || !i::FLAG_opt) 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(o1);"
      "poly(o2);"
      "poly(o1);"
      "poly(o2);"
      "%OptimizeFunctionOnNextCall(poly);"
      "poly(o1);");

  Matchers m;

  std::smatch match;
  std::string line;
  std::istringstream reader(DisassembleFunction("poly"));
  bool poisoning_sequence_found = false;
  while (std::getline(reader, line)) {
    if (std::regex_match(line, match, m.map_load_re)) {
      std::string map_reg = match[1];
      std::string object_reg = match[2];
      // Matches that the property access sequence is instrumented with
      // poisoning. We match the following sequence:
      //
      //   ldr r1, [r0, #-1]                ; load map
      //   ldr r2, [pc, #+104]              ; load map constant #1
      //   cmp r1, r2                       ; compare maps
      //   beq +Lcase1                      ; if match, got to the load
      //   eoreq r9, r9, r9                 ; update the poison
      //   csdb                             ; speculation barrier
      //   ldr r1, [r0, #-1]                ; load map
      //   ldr r2, [pc, #+304]              ; load map constant #2
      //   cmp r1, r2                       ; compare maps
      //   bne +Ldeopt                      ; deopt if different
      //   eorne r9, r9, r9                 ; update the poison
      //   csdb                             ; speculation barrier
      //   ldr r0, [r0, #+11]               ; load the field
      //   and r0, r0, r9                   ; apply the poison
      //   mov r0, r0, asr #1               ; untag
      //   b +Ldone                         ; goto merge point
      // Lcase1:
      //   eorne r9, r9, r9                 ; update the poison
      //   csdb                             ; speculation barrier
      //   ldr r0, [r0, #+3]                ; load property backing store
      //   and r0, r0, r9                   ; apply the poison
      //   ldr r0, [r0, #+3]                ; load the property
      //   and r0, r0, r9                   ; apply the poison
      // Ldone:

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.load_const_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.cmp_re));
      CHECK_EQ(match[1], map_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.beq_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.eoreq_re));
      CHECK_EQ(match[1], m.poison_reg);
      CHECK_EQ(match[2], m.poison_reg);
      CHECK_EQ(match[3], m.poison_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.csdb_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.map_load_re));
      map_reg = match[1];
      CHECK_EQ(match[2], object_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.load_const_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.cmp_re));
      CHECK_EQ(match[1], map_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.bne_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.eorne_re));
      CHECK_EQ(match[1], m.poison_reg);
      CHECK_EQ(match[2], m.poison_reg);
      CHECK_EQ(match[3], m.poison_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.csdb_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.load_field_re));
      CHECK_EQ(match[2], object_reg);
      std::string field_reg = match[1];

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.mask_re));
      CHECK_EQ(match[1], field_reg);
      CHECK_EQ(match[2], field_reg);
      CHECK_EQ(match[3], m.poison_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.untag_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.b_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.eorne_re));
      CHECK_EQ(match[1], m.poison_reg);
      CHECK_EQ(match[2], m.poison_reg);
      CHECK_EQ(match[3], m.poison_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.csdb_re));

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.load_field_re));
      CHECK_EQ(match[2], object_reg);
      std::string storage_reg = match[1];

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.mask_re));
      CHECK_EQ(match[1], storage_reg);
      CHECK_EQ(match[2], storage_reg);
      CHECK_EQ(match[3], m.poison_reg);

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.load_field_re));
      CHECK_EQ(match[2], storage_reg);
      field_reg = match[1];

      CHECK(std::getline(reader, line));
      CHECK(std::regex_match(line, match, m.mask_re));
      CHECK_EQ(match[1], field_reg);
      CHECK_EQ(match[2], field_reg);
      CHECK_EQ(match[3], m.poison_reg);

      poisoning_sequence_found = true;
      break;
    }
  }

  CHECK(poisoning_sequence_found);
#endif  // ENABLE_DISASSEMBLER
}

}  // namespace internal
}  // namespace v8