| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "bin/vmservice_dartium.h" | |
| 6 | |
| 7 #include "bin/builtin.h" | |
| 8 #include "bin/dartutils.h" | |
| 9 #include "bin/eventhandler.h" | |
| 10 #include "bin/platform.h" | |
| 11 #include "bin/thread.h" | |
| 12 #include "bin/utils.h" | |
| 13 #include "bin/vmservice_impl.h" | |
| 14 #include "zlib/zlib.h" | |
| 15 | |
| 16 namespace dart { | |
| 17 namespace bin { | |
| 18 | |
| 19 #define CHECK_RESULT(result) \ | |
| 20 if (Dart_IsError(result)) { \ | |
| 21 fprintf(stderr, "CHECK_RESULT failed: %s", Dart_GetError(result)); \ | |
| 22 Dart_ExitScope(); \ | |
| 23 Dart_ShutdownIsolate(); \ | |
| 24 return 0; \ | |
| 25 } | |
| 26 | |
| 27 static const char* DART_IPV6_ONLY_FLAG = "DART_IPV6_ONLY"; | |
| 28 static const char* DEFAULT_VM_SERVICE_SERVER_IP_V6 = "::1"; | |
| 29 static const char* DEFAULT_VM_SERVICE_SERVER_IP_V4 = "127.0.0.1"; | |
| 30 static const int DEFAULT_VM_SERVICE_SERVER_PORT = 0; | |
| 31 | |
| 32 static bool IsIpv6Only() { | |
| 33 char* v = getenv(DART_IPV6_ONLY_FLAG); | |
| 34 if (!v) return 0; | |
| 35 return v[0] == '1'; | |
| 36 } | |
| 37 | |
| 38 void VmServiceServer::Bootstrap() { | |
| 39 if (!Platform::Initialize()) { | |
| 40 fprintf(stderr, "Platform::Initialize() failed\n"); | |
| 41 } | |
| 42 DartUtils::SetOriginalWorkingDirectory(); | |
| 43 Thread::InitOnce(); | |
| 44 TimerUtils::InitOnce(); | |
| 45 EventHandler::Start(); | |
| 46 } | |
| 47 | |
| 48 Dart_Isolate VmServiceServer::CreateIsolate(const uint8_t* snapshot_buffer) { | |
| 49 ASSERT(snapshot_buffer != NULL); | |
| 50 // Create the isolate. | |
| 51 IsolateData* isolate_data = | |
| 52 new IsolateData(DART_VM_SERVICE_ISOLATE_NAME, NULL, NULL, NULL); | |
| 53 char* error = 0; | |
| 54 Dart_Isolate isolate = | |
| 55 Dart_CreateIsolate(DART_VM_SERVICE_ISOLATE_NAME, "main", snapshot_buffer, | |
| 56 NULL, NULL, isolate_data, &error); | |
| 57 if (!isolate) { | |
| 58 fprintf(stderr, "Dart_CreateIsolate failed: %s\n", error); | |
| 59 return 0; | |
| 60 } | |
| 61 | |
| 62 Dart_EnterScope(); | |
| 63 Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); | |
| 64 Builtin::SetNativeResolver(Builtin::kIOLibrary); | |
| 65 | |
| 66 ASSERT(Dart_IsServiceIsolate(isolate)); | |
| 67 if (!VmService::Setup( | |
| 68 IsIpv6Only() ? DEFAULT_VM_SERVICE_SERVER_IP_V6 | |
| 69 : DEFAULT_VM_SERVICE_SERVER_IP_V4, | |
| 70 DEFAULT_VM_SERVICE_SERVER_PORT, false /* running_precompiled */, | |
| 71 false /* disable origin checks */, false /* trace_loading */)) { | |
| 72 fprintf(stderr, "Vmservice::Setup failed: %s\n", | |
| 73 VmService::GetErrorMessage()); | |
| 74 isolate = NULL; | |
| 75 } | |
| 76 Dart_ExitScope(); | |
| 77 Dart_ExitIsolate(); | |
| 78 return isolate; | |
| 79 } | |
| 80 | |
| 81 const char* VmServiceServer::GetServerAddress() { | |
| 82 return VmService::GetServerAddress(); | |
| 83 } | |
| 84 | |
| 85 void VmServiceServer::DecompressAssets(const uint8_t* input, | |
| 86 unsigned int input_len, | |
| 87 uint8_t** output, | |
| 88 unsigned int* output_length) { | |
| 89 ASSERT(input != NULL); | |
| 90 ASSERT(input_len > 0); | |
| 91 ASSERT(output != NULL); | |
| 92 ASSERT(output_length != NULL); | |
| 93 | |
| 94 // Initialize output. | |
| 95 *output = NULL; | |
| 96 *output_length = 0; | |
| 97 | |
| 98 const unsigned int kChunkSize = 256 * 1024; | |
| 99 uint8_t chunk_out[kChunkSize]; | |
| 100 z_stream strm; | |
| 101 strm.zalloc = Z_NULL; | |
| 102 strm.zfree = Z_NULL; | |
| 103 strm.opaque = Z_NULL; | |
| 104 strm.avail_in = 0; | |
| 105 strm.next_in = 0; | |
| 106 int ret = inflateInit2(&strm, 32 + MAX_WBITS); | |
| 107 ASSERT(ret == Z_OK); | |
| 108 | |
| 109 unsigned int input_cursor = 0; | |
| 110 unsigned int output_cursor = 0; | |
| 111 do { | |
| 112 // Setup input. | |
| 113 unsigned int size_in = input_len - input_cursor; | |
| 114 if (size_in > kChunkSize) { | |
| 115 size_in = kChunkSize; | |
| 116 } | |
| 117 strm.avail_in = size_in; | |
| 118 strm.next_in = const_cast<uint8_t*>(&input[input_cursor]); | |
| 119 | |
| 120 // Inflate until we've exhausted the current input chunk. | |
| 121 do { | |
| 122 // Setup output. | |
| 123 strm.avail_out = kChunkSize; | |
| 124 strm.next_out = &chunk_out[0]; | |
| 125 // Inflate. | |
| 126 ret = inflate(&strm, Z_SYNC_FLUSH); | |
| 127 // We either hit the end of the stream or made forward progress. | |
| 128 ASSERT((ret == Z_STREAM_END) || (ret == Z_OK)); | |
| 129 // Grow output buffer size. | |
| 130 unsigned int size_out = kChunkSize - strm.avail_out; | |
| 131 *output_length += size_out; | |
| 132 *output = reinterpret_cast<uint8_t*>(realloc(*output, *output_length)); | |
| 133 // Copy output. | |
| 134 memmove(&((*output)[output_cursor]), &chunk_out[0], size_out); | |
| 135 output_cursor += size_out; | |
| 136 } while (strm.avail_out == 0); | |
| 137 | |
| 138 // We've processed size_in bytes. | |
| 139 input_cursor += size_in; | |
| 140 | |
| 141 // We're finished decompressing when zlib tells us. | |
| 142 } while (ret != Z_STREAM_END); | |
| 143 | |
| 144 inflateEnd(&strm); | |
| 145 } | |
| 146 | |
| 147 /* DISALLOW_ALLOCATION */ | |
| 148 void VmServiceServer::operator delete(void* pointer) { | |
| 149 fprintf(stderr, "unreachable code\n"); | |
| 150 abort(); | |
| 151 } | |
| 152 | |
| 153 } // namespace bin | |
| 154 } // namespace dart | |
| OLD | NEW |