dart-sdk/runtime/vm/base64.cc
Alexander Markov 0e6b74543c Revert "[vm/kernel] Use GC-tracked ExternalTypedData/TypedDataView for kernel buffers"
This reverts commit ab6aeaa106.

Revert "[vm/compiler] Speed up the compiler part which deals with kernel reading up in DEBUG mode"

This reverts commit b316210d94.

Reason for revert: regression of snapshot sizes (DNO-599).

Original change's description:
> [vm/kernel] Use GC-tracked ExternalTypedData/TypedDataView for kernel buffers
>
> Until now we often leaked kernel buffers (e.g. hot reload buffers) because various
> objects were referencing ExternalTypedData objects pointing into the middle of
> c-allocated memory. This made it impossible for the GC to determine when the last
> reference is gone.
>
> This CL ensures that the actual buffers are *always* made available via
> ExternalTypedData and any inner pointers into it are created via TypedDataViews.
>
> The embedder guarantees to the free kernel buffers it has provided to:
>     - Dart_CreateIsolateFromKernel
>     - Dart_LoadScriptFromKernel
>     - Dart_LoadLibraryFromKernel
>     - Dart_SetDartLibrarySourcesKernel
> on isolate shutdown.
>
> All other kernel buffers will get a finalizer attached, which ensures the
> kernel buffers get freed by the GC once they are no longer referenced:
>     - Kernel blobs for expression evaluation
>     - Kernel blobs for Hot-Reload
>     - Kernel blobs for cc tests
>
> Fixes https://github.com/dart-lang/sdk/issues/33973
> Fixes https://github.com/dart-lang/sdk/issues/36857
> Issue https://github.com/dart-lang/sdk/issues/37030
>
> Change-Id: I1cc410c94c0f4b229413e793728a261afcb10aaf
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103130
> Reviewed-by: Ryan Macnak <rmacnak@google.com>
> Commit-Queue: Martin Kustermann <kustermann@google.com>

TBR=kustermann@google.com,rmacnak@google.com

# Not skipping CQ checks because original CL landed > 1 day ago.

Change-Id: I49715d2400f4a5c8806b7d6a2912b7258f671a0a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/104343
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
Auto-Submit: Alexander Markov <alexmarkov@google.com>
2019-05-31 22:15:51 +00:00

76 lines
2.7 KiB
C++

// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/base64.h"
#include "vm/os.h"
namespace dart {
// Taken from lib/_http/crypto.dart
// Lookup table used for finding Base 64 alphabet index of a given byte.
// -2 : Outside Base 64 alphabet.
// -1 : '\r' or '\n'
// 0 : = (Padding character).
// >0 : Base 64 alphabet index of given byte.
static const int8_t decode_table[] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -1, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, 62, -2, 63, //
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, 00, -2, -2, //
-2, 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, 10, 11, 12, 13, 14, //
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, 63, //
-2, 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, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, //
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2};
static const char PAD = '=';
uint8_t* DecodeBase64(Zone* zone, const char* str, intptr_t* out_decoded_len) {
intptr_t len = strlen(str);
if (len == 0 || (len % 4 != 0)) {
return nullptr;
}
int pad_length = 0;
for (intptr_t i = len - 1; i >= 0; i--) {
const uint8_t current_code_unit = str[i];
if (decode_table[current_code_unit] > 0) break;
if (current_code_unit == PAD) pad_length++;
}
intptr_t decoded_en = ((len * 6) >> 3) - pad_length;
uint8_t* bytes = zone->Alloc<uint8_t>(decoded_en);
for (int i = 0, o = 0; o < decoded_en;) {
// Accumulate 4 valid 6 bit Base 64 characters into an int.
int x = 0;
for (int j = 4; j > 0;) {
int c = decode_table[(uint8_t)str[i++]];
if (c >= 0) {
x = ((x << 6) & 0xFFFFFF) | c;
j--;
}
}
bytes[o++] = x >> 16;
if (o < decoded_en) {
bytes[o++] = (x >> 8) & 0xFF;
if (o < decoded_en) bytes[o++] = x & 0xFF;
}
}
if (out_decoded_len != nullptr) {
*out_decoded_len = decoded_en;
}
return bytes;
}
} // namespace dart