Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | /* |
| 17 | * Implementation of an expandable byte buffer. Designed for serializing |
| 18 | * primitive values, e.g. JDWP replies. |
| 19 | */ |
| 20 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 21 | #include "jdwp/jdwp_expand_buf.h" |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame^] | 26 | #include <android-base/logging.h> |
| 27 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 28 | #include "jdwp/jdwp.h" |
| 29 | #include "jdwp/jdwp_bits.h" |
| 30 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
| 33 | namespace JDWP { |
| 34 | |
| 35 | /* |
| 36 | * Data structure used to track buffer use. |
| 37 | */ |
| 38 | struct ExpandBuf { |
| 39 | uint8_t* storage; |
| 40 | int curLen; |
| 41 | int maxLen; |
| 42 | }; |
| 43 | |
| 44 | #define kInitialStorage 64 |
| 45 | |
| 46 | /* |
| 47 | * Allocate a JdwpBuf and some initial storage. |
| 48 | */ |
| 49 | ExpandBuf* expandBufAlloc() { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 50 | ExpandBuf* newBuf = new ExpandBuf; |
| 51 | newBuf->storage = reinterpret_cast<uint8_t*>(malloc(kInitialStorage)); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 52 | newBuf->curLen = 0; |
| 53 | newBuf->maxLen = kInitialStorage; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 54 | return newBuf; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Free a JdwpBuf and associated storage. |
| 59 | */ |
| 60 | void expandBufFree(ExpandBuf* pBuf) { |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 61 | if (pBuf == nullptr) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | free(pBuf->storage); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 66 | delete pBuf; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Get a pointer to the start of the buffer. |
| 71 | */ |
| 72 | uint8_t* expandBufGetBuffer(ExpandBuf* pBuf) { |
| 73 | return pBuf->storage; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Get the amount of data currently in the buffer. |
| 78 | */ |
| 79 | size_t expandBufGetLength(ExpandBuf* pBuf) { |
| 80 | return pBuf->curLen; |
| 81 | } |
| 82 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 83 | /* |
| 84 | * Ensure that the buffer has enough space to hold incoming data. If it |
| 85 | * doesn't, resize the buffer. |
| 86 | */ |
| 87 | static void ensureSpace(ExpandBuf* pBuf, int newCount) { |
| 88 | if (pBuf->curLen + newCount <= pBuf->maxLen) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | while (pBuf->curLen + newCount > pBuf->maxLen) { |
| 93 | pBuf->maxLen *= 2; |
| 94 | } |
| 95 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 96 | uint8_t* newPtr = reinterpret_cast<uint8_t*>(realloc(pBuf->storage, pBuf->maxLen)); |
Sebastien Hertz | 7d95565 | 2014-10-22 10:57:10 +0200 | [diff] [blame] | 97 | if (newPtr == nullptr) { |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 98 | LOG(FATAL) << "realloc(" << pBuf->maxLen << ") failed"; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | pBuf->storage = newPtr; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Allocate some space in the buffer. |
| 106 | */ |
| 107 | uint8_t* expandBufAddSpace(ExpandBuf* pBuf, int gapSize) { |
| 108 | uint8_t* gapStart; |
| 109 | |
| 110 | ensureSpace(pBuf, gapSize); |
| 111 | gapStart = pBuf->storage + pBuf->curLen; |
| 112 | /* do we want to garbage-fill the gap for debugging? */ |
| 113 | pBuf->curLen += gapSize; |
| 114 | |
| 115 | return gapStart; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Append a byte. |
| 120 | */ |
| 121 | void expandBufAdd1(ExpandBuf* pBuf, uint8_t val) { |
| 122 | ensureSpace(pBuf, sizeof(val)); |
| 123 | *(pBuf->storage + pBuf->curLen) = val; |
| 124 | pBuf->curLen++; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Append two big-endian bytes. |
| 129 | */ |
| 130 | void expandBufAdd2BE(ExpandBuf* pBuf, uint16_t val) { |
| 131 | ensureSpace(pBuf, sizeof(val)); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 132 | Set2BE(pBuf->storage + pBuf->curLen, val); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 133 | pBuf->curLen += sizeof(val); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Append four big-endian bytes. |
| 138 | */ |
| 139 | void expandBufAdd4BE(ExpandBuf* pBuf, uint32_t val) { |
| 140 | ensureSpace(pBuf, sizeof(val)); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 141 | Set4BE(pBuf->storage + pBuf->curLen, val); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 142 | pBuf->curLen += sizeof(val); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Append eight big-endian bytes. |
| 147 | */ |
| 148 | void expandBufAdd8BE(ExpandBuf* pBuf, uint64_t val) { |
| 149 | ensureSpace(pBuf, sizeof(val)); |
Elliott Hughes | f7c3b66 | 2011-10-27 12:04:56 -0700 | [diff] [blame] | 150 | Set8BE(pBuf->storage + pBuf->curLen, val); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 151 | pBuf->curLen += sizeof(val); |
| 152 | } |
| 153 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 154 | static void SetUtf8String(uint8_t* buf, const char* str, size_t strLen) { |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 155 | Set4BE(buf, strLen); |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 156 | if (str != nullptr) { |
| 157 | memcpy(buf + sizeof(uint32_t), str, strLen); |
| 158 | } |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 161 | /* |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 162 | * Add a UTF8 string as a 4-byte length followed by a non-nullptr-terminated |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 163 | * string. |
| 164 | * |
| 165 | * Because these strings are coming out of the VM, it's safe to assume that |
| 166 | * they can be null-terminated (either they don't have null bytes or they |
| 167 | * have stored null bytes in a multi-byte encoding). |
| 168 | */ |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 169 | void expandBufAddUtf8String(ExpandBuf* pBuf, const char* s) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 170 | int strLen = (s != nullptr ? strlen(s) : 0); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 171 | ensureSpace(pBuf, sizeof(uint32_t) + strLen); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 172 | SetUtf8String(pBuf->storage + pBuf->curLen, s, strLen); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 173 | pBuf->curLen += sizeof(uint32_t) + strLen; |
| 174 | } |
| 175 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 176 | void expandBufAddUtf8String(ExpandBuf* pBuf, const std::string& s) { |
| 177 | ensureSpace(pBuf, sizeof(uint32_t) + s.size()); |
| 178 | SetUtf8String(pBuf->storage + pBuf->curLen, s.data(), s.size()); |
| 179 | pBuf->curLen += sizeof(uint32_t) + s.size(); |
| 180 | } |
| 181 | |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 182 | void expandBufAddLocation(ExpandBuf* buf, const JdwpLocation& location) { |
| 183 | expandBufAdd1(buf, location.type_tag); |
| 184 | expandBufAddObjectId(buf, location.class_id); |
| 185 | expandBufAddMethodId(buf, location.method_id); |
| 186 | expandBufAdd8BE(buf, location.dex_pc); |
| 187 | } |
| 188 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 189 | } // namespace JDWP |
| 190 | |
| 191 | } // namespace art |