summaryrefslogtreecommitdiff
path: root/runtime/lambda/closure.cc
blob: f935e049fda0d8c371fd22f69717d4d15f75e0c1 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "lambda/closure.h"

#include "base/logging.h"
#include "lambda/art_lambda_method.h"
#include "runtime/mirror/object_reference.h"

namespace art {
namespace lambda {

template <typename T>
// TODO: can I return T __attribute__((__aligned__(1)))* here instead?
const uint8_t* Closure::GetUnsafeAtOffset(size_t offset) const {
  // Do not DCHECK here with existing helpers since most of them will call into this function.
  return reinterpret_cast<const uint8_t*>(captured_) + offset;
}

size_t Closure::GetCapturedVariableSize(ShortyFieldType variable_type, size_t offset) const {
  switch (variable_type) {
    case ShortyFieldType::kLambda:
    {
      return GetClosureSize(GetUnsafeAtOffset<Closure>(offset));
    }
    default:
      DCHECK(variable_type.IsStaticSize());
      return variable_type.GetStaticSize();
  }
}

// Templatize the flags to give the compiler a fighting chance to eliminate
// any unnecessary code through different uses of this function.
template <Closure::VariableInfo::Flags flags>
inline Closure::VariableInfo Closure::ParseTypeDescriptor(const char* type_descriptor,
                                                          size_t upto_index) const {
  DCHECK(type_descriptor != nullptr);

  VariableInfo result;

  ShortyFieldType last_type;
  size_t offset = (flags & VariableInfo::kOffset) ? GetStartingOffset() : 0;
  size_t prev_offset = 0;
  size_t count = 0;

  while ((type_descriptor =
      ShortyFieldType::ParseFromFieldTypeDescriptor(type_descriptor, &last_type)) != nullptr) {
    count++;

    if (flags & VariableInfo::kOffset) {
      // Accumulate the sizes of all preceding captured variables as the current offset only.
      offset += prev_offset;
      prev_offset = GetCapturedVariableSize(last_type, offset);
    }

    if ((count > upto_index)) {
      break;
    }
  }

  if (flags & VariableInfo::kVariableType) {
    result.variable_type_ = last_type;
  }

  if (flags & VariableInfo::kIndex) {
    result.index_ = count;
  }

  if (flags & VariableInfo::kCount) {
    result.count_ = count;
  }

  if (flags & VariableInfo::kOffset) {
    result.offset_ = offset;
  }

  // TODO: We should probably store the result of this in the ArtLambdaMethod,
  // to avoid re-computing the data every single time for static closures.
  return result;
}

size_t Closure::GetCapturedVariablesSize() const {
  const size_t captured_variable_offset = offsetof(Closure, captured_);
  DCHECK_GE(GetSize(), captured_variable_offset);  // Prevent underflows.
  return GetSize() - captured_variable_offset;
}

size_t Closure::GetSize() const {
  const size_t static_closure_size = lambda_info_->GetStaticClosureSize();
  if (LIKELY(lambda_info_->IsStaticSize())) {
    return static_closure_size;
  }

  DCHECK_GE(static_closure_size, sizeof(captured_[0].dynamic_.size_));
  const size_t dynamic_closure_size = captured_[0].dynamic_.size_;
  // The dynamic size better be at least as big as the static size.
  DCHECK_GE(dynamic_closure_size, static_closure_size);

  return dynamic_closure_size;
}

void Closure::CopyTo(void* target, size_t target_size) const {
  DCHECK_GE(target_size, GetSize());

  // TODO: using memcpy is unsafe with read barriers, fix this once we add reference support
  static_assert(kClosureSupportsReferences == false,
                "Do not use memcpy with readbarrier references");
  memcpy(target, this, GetSize());
}

ArtMethod* Closure::GetTargetMethod() const {
  return const_cast<ArtMethod*>(lambda_info_->GetArtMethod());
}

ArtLambdaMethod* Closure::GetLambdaInfo() const {
  return const_cast<ArtLambdaMethod*>(lambda_info_);
}

uint32_t Closure::GetHashCode() const {
  // Start with a non-zero constant, a prime number.
  uint32_t result = 17;

  // Include the hash with the ArtMethod.
  {
    uintptr_t method = reinterpret_cast<uintptr_t>(GetTargetMethod());
    result = 31 * result + Low32Bits(method);
    if (sizeof(method) == sizeof(uint64_t)) {
      result = 31 * result + High32Bits(method);
    }
  }

  // Include a hash for each captured variable.
  for (size_t i = 0; i < GetCapturedVariablesSize(); ++i) {
    // TODO: not safe for GC-able values since the address can move and the hash code would change.
    uint8_t captured_variable_raw_value;
    CopyUnsafeAtOffset<uint8_t>(i, /*out*/&captured_variable_raw_value);  // NOLINT: [whitespace/comma] [3]

    result = 31 * result + captured_variable_raw_value;
  }

  // TODO: Fix above loop to work for objects and lambdas.
  static_assert(kClosureSupportsGarbageCollection == false,
               "Need to update above loop to read the hash code from the "
                "objects and lambdas recursively");

  return result;
}

bool Closure::ReferenceEquals(const Closure* other) const {
  DCHECK(other != nullptr);

  // TODO: Need rework to use read barriers once closures have references inside of them that can
  // move. Until then, it's safe to just compare the data inside of it directly.
  static_assert(kClosureSupportsReferences == false,
                "Unsafe to use memcmp in read barrier collector");

  if (GetSize() != other->GetSize()) {
    return false;
  }

  return memcmp(this, other, GetSize());
}

size_t Closure::GetNumberOfCapturedVariables() const {
  // TODO: refactor into art_lambda_method.h. Parsing should only be required here as a DCHECK.
  VariableInfo variable_info =
      ParseTypeDescriptor<VariableInfo::kCount>(GetCapturedVariablesTypeDescriptor(),
                                                VariableInfo::kUpToIndexMax);
  size_t count = variable_info.count_;
  // Assuming each variable was 1 byte, the size should always be greater or equal than the count.
  DCHECK_LE(count, GetCapturedVariablesSize());
  return count;
}

const char* Closure::GetCapturedVariablesTypeDescriptor() const {
  return lambda_info_->GetCapturedVariablesTypeDescriptor();
}

ShortyFieldType Closure::GetCapturedShortyType(size_t index) const {
  DCHECK_LT(index, GetNumberOfCapturedVariables());

  VariableInfo variable_info =
      ParseTypeDescriptor<VariableInfo::kVariableType>(GetCapturedVariablesTypeDescriptor(),
                                                       index);

  return variable_info.variable_type_;
}

uint32_t Closure::GetCapturedPrimitiveNarrow(size_t index) const {
  DCHECK(GetCapturedShortyType(index).IsPrimitiveNarrow());

  ShortyFieldType variable_type;
  size_t offset;
  GetCapturedVariableTypeAndOffset(index, &variable_type, &offset);

  // TODO: Restructure to use template specialization, e.g. GetCapturedPrimitive<T>
  // so that we can avoid this nonsense regarding memcpy always overflowing.
  // Plus, this additional switching seems redundant since the interpreter
  // would've done it already, and knows the exact type.
  uint32_t result = 0;
  static_assert(ShortyFieldTypeTraits::IsPrimitiveNarrowType<decltype(result)>(),
                "result must be a primitive narrow type");
  switch (variable_type) {
    case ShortyFieldType::kBoolean:
      CopyUnsafeAtOffset<bool>(offset, &result);
      break;
    case ShortyFieldType::kByte:
      CopyUnsafeAtOffset<uint8_t>(offset, &result);
      break;
    case ShortyFieldType::kChar:
      CopyUnsafeAtOffset<uint16_t>(offset, &result);
      break;
    case ShortyFieldType::kShort:
      CopyUnsafeAtOffset<int16_t>(offset, &result);
      break;
    case ShortyFieldType::kInt:
      CopyUnsafeAtOffset<int32_t>(offset, &result);
      break;
    case ShortyFieldType::kFloat:
      // XX: Maybe there should just be a GetCapturedPrimitive<T> to avoid this shuffle?
      // The interpreter's invoke seems to only special case references and wides,
      // everything else is treated as a generic 32-bit pattern.
      CopyUnsafeAtOffset<float>(offset, &result);
      break;
    default:
      LOG(FATAL)
          << "expected a valid narrow primitive shorty type but got "
          << static_cast<char>(variable_type);
      UNREACHABLE();
  }

  return result;
}

uint64_t Closure::GetCapturedPrimitiveWide(size_t index) const {
  DCHECK(GetCapturedShortyType(index).IsPrimitiveWide());

  ShortyFieldType variable_type;
  size_t offset;
  GetCapturedVariableTypeAndOffset(index, &variable_type, &offset);

  // TODO: Restructure to use template specialization, e.g. GetCapturedPrimitive<T>
  // so that we can avoid this nonsense regarding memcpy always overflowing.
  // Plus, this additional switching seems redundant since the interpreter
  // would've done it already, and knows the exact type.
  uint64_t result = 0;
  static_assert(ShortyFieldTypeTraits::IsPrimitiveWideType<decltype(result)>(),
                "result must be a primitive wide type");
  switch (variable_type) {
    case ShortyFieldType::kLong:
      CopyUnsafeAtOffset<int64_t>(offset, &result);
      break;
    case ShortyFieldType::kDouble:
      CopyUnsafeAtOffset<double>(offset, &result);
      break;
    default:
      LOG(FATAL)
          << "expected a valid primitive wide shorty type but got "
          << static_cast<char>(variable_type);
      UNREACHABLE();
  }

  return result;
}

mirror::Object* Closure::GetCapturedObject(size_t index) const {
  DCHECK(GetCapturedShortyType(index).IsObject());

  ShortyFieldType variable_type;
  size_t offset;
  GetCapturedVariableTypeAndOffset(index, &variable_type, &offset);

  // TODO: Restructure to use template specialization, e.g. GetCapturedPrimitive<T>
  // so that we can avoid this nonsense regarding memcpy always overflowing.
  // Plus, this additional switching seems redundant since the interpreter
  // would've done it already, and knows the exact type.
  mirror::Object* result = nullptr;
  static_assert(ShortyFieldTypeTraits::IsObjectType<decltype(result)>(),
                "result must be an object type");
  switch (variable_type) {
    case ShortyFieldType::kObject:
      // TODO: This seems unsafe. This may need to use gcroots.
      static_assert(kClosureSupportsGarbageCollection == false,
                    "May need GcRoots and definitely need mutator locks");
      {
        mirror::CompressedReference<mirror::Object> compressed_result;
        CopyUnsafeAtOffset<uint32_t>(offset, &compressed_result);
        result = compressed_result.AsMirrorPtr();
      }
      break;
    default:
      CHECK(false)
          << "expected a valid shorty type but got " << static_cast<char>(variable_type);
      UNREACHABLE();
  }

  return result;
}

size_t Closure::GetCapturedClosureSize(size_t index) const {
  DCHECK(GetCapturedShortyType(index).IsLambda());
  size_t offset = GetCapturedVariableOffset(index);

  auto* captured_ptr = reinterpret_cast<const uint8_t*>(&captured_);
  size_t closure_size = GetClosureSize(captured_ptr + offset);

  return closure_size;
}

void Closure::CopyCapturedClosure(size_t index, void* destination, size_t destination_room) const {
  DCHECK(GetCapturedShortyType(index).IsLambda());
  size_t offset = GetCapturedVariableOffset(index);

  auto* captured_ptr = reinterpret_cast<const uint8_t*>(&captured_);
  size_t closure_size = GetClosureSize(captured_ptr + offset);

  static_assert(ShortyFieldTypeTraits::IsLambdaType<Closure*>(),
                "result must be a lambda type");

  CopyUnsafeAtOffset<Closure>(offset, destination, closure_size, destination_room);
}

size_t Closure::GetCapturedVariableOffset(size_t index) const {
  VariableInfo variable_info =
      ParseTypeDescriptor<VariableInfo::kOffset>(GetCapturedVariablesTypeDescriptor(),
                                                 index);

  size_t offset = variable_info.offset_;

  return offset;
}

void Closure::GetCapturedVariableTypeAndOffset(size_t index,
                                               ShortyFieldType* out_type,
                                               size_t* out_offset) const {
  DCHECK(out_type != nullptr);
  DCHECK(out_offset != nullptr);

  static constexpr const VariableInfo::Flags kVariableTypeAndOffset =
      static_cast<VariableInfo::Flags>(VariableInfo::kVariableType | VariableInfo::kOffset);
  VariableInfo variable_info =
      ParseTypeDescriptor<kVariableTypeAndOffset>(GetCapturedVariablesTypeDescriptor(),
                                                  index);

  ShortyFieldType variable_type = variable_info.variable_type_;
  size_t offset = variable_info.offset_;

  *out_type = variable_type;
  *out_offset = offset;
}

template <typename T>
void Closure::CopyUnsafeAtOffset(size_t offset,
                                 void* destination,
                                 size_t src_size,
                                 size_t destination_room) const {
  DCHECK_GE(destination_room, src_size);
  const uint8_t* data_ptr = GetUnsafeAtOffset<T>(offset);
  memcpy(destination, data_ptr, sizeof(T));
}

// TODO: This is kind of ugly. I would prefer an unaligned_ptr<Closure> here.
// Unfortunately C++ doesn't let you lower the alignment (i.e. alignas(1) Closure*) is not legal.
size_t Closure::GetClosureSize(const uint8_t* closure) {
  DCHECK(closure != nullptr);

  static_assert(!std::is_base_of<mirror::Object, Closure>::value,
                "It might be unsafe to call memcpy on a managed object");

  // Safe as long as it's not a mirror Object.
  // TODO: Should probably wrap this in like MemCpyNative or some such which statically asserts
  // we aren't trying to copy mirror::Object data around.
  ArtLambdaMethod* closure_info;
  memcpy(&closure_info, closure + offsetof(Closure, lambda_info_), sizeof(closure_info));

  if (LIKELY(closure_info->IsStaticSize())) {
    return closure_info->GetStaticClosureSize();
  }

  // The size is dynamic, so we need to read it from captured_variables_ portion.
  size_t dynamic_size;
  memcpy(&dynamic_size,
         closure + offsetof(Closure, captured_[0].dynamic_.size_),
         sizeof(dynamic_size));
  static_assert(sizeof(dynamic_size) == sizeof(captured_[0].dynamic_.size_),
                "Dynamic size type must match the structural type of the size");

  DCHECK_GE(dynamic_size, closure_info->GetStaticClosureSize());
  return dynamic_size;
}

size_t Closure::GetStartingOffset() const {
  static constexpr const size_t captured_offset = offsetof(Closure, captured_);
  if (LIKELY(lambda_info_->IsStaticSize())) {
    return offsetof(Closure, captured_[0].static_variables_) - captured_offset;
  } else {
    return offsetof(Closure, captured_[0].dynamic_.variables_) - captured_offset;
  }
}

}  // namespace lambda
}  // namespace art