/src/trafficserver/src/proxy/hdrs/HdrHeap.cc
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | A brief file description |
4 | | |
5 | | @par License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | */ |
23 | | |
24 | | /**************************************************************************** |
25 | | |
26 | | HdrBuf.cc |
27 | | |
28 | | Description: |
29 | | |
30 | | |
31 | | ****************************************************************************/ |
32 | | |
33 | | #include "tscore/ink_platform.h" |
34 | | #include "tscore/ink_config.h" |
35 | | #include "tscore/Diags.h" |
36 | | #include "proxy/hdrs/HdrHeap.h" |
37 | | #include "proxy/hdrs/URL.h" |
38 | | #include "proxy/hdrs/MIME.h" |
39 | | #include "proxy/hdrs/HTTP.h" |
40 | | #include "iocore/eventsystem/EThread.h" |
41 | | #include "iocore/eventsystem/Thread.h" |
42 | | |
43 | | #include <cstdlib> |
44 | | |
45 | | static constexpr size_t MAX_LOST_STR_SPACE = 1024; |
46 | | static constexpr uint32_t MAX_HDR_HEAP_OBJ_LENGTH = (1 << 20) - 1; ///< m_length is 20 bit |
47 | | |
48 | | Allocator hdrHeapAllocator("hdrHeap", HdrHeap::DEFAULT_SIZE); |
49 | | Allocator strHeapAllocator("hdrStrHeap", HdrStrHeap::DEFAULT_SIZE); |
50 | | |
51 | | namespace |
52 | | { |
53 | | DbgCtl dbg_ctl_http{"http"}; |
54 | | |
55 | | #if TS_HAS_TESTS |
56 | | // Test hook: how far to rotate the well-known string indexes written into a marshalled heap. |
57 | | // Zero, the default, leaves marshalling alone. |
58 | 10 | int const test_wks_idx_shift = []() -> int { |
59 | 10 | char const *const value = std::getenv("ATS_TEST_WKS_IDX_SHIFT"); |
60 | | |
61 | 10 | return value != nullptr ? atoi(value) : 0; |
62 | 10 | }(); |
63 | | |
64 | | int16_t |
65 | | test_shift_wks_idx(int16_t wks_idx) |
66 | 0 | { |
67 | 0 | if (wks_idx < 0) { |
68 | 0 | return wks_idx; |
69 | 0 | } |
70 | | // Fold the configured shift into [0, hdrtoken_num_wks) here rather than where it is read: |
71 | | // hdrtoken_num_wks is initialized in another translation unit, so it is not dependable during |
72 | | // this one's static initialization. Folding also keeps a negative or oversized environment value |
73 | | // from producing an index that is not in the table. |
74 | 0 | int const shift = ((test_wks_idx_shift % hdrtoken_num_wks) + hdrtoken_num_wks) % hdrtoken_num_wks; |
75 | |
|
76 | 0 | return static_cast<int16_t>((wks_idx + shift) % hdrtoken_num_wks); |
77 | 0 | } |
78 | | |
79 | | /** Make a marshalled heap look like one written by a build with a different well-known string |
80 | | * table: rotate every stored index, and drop the presence bits and slot accelerators that a build |
81 | | * lacking some of this build's strings would never have set. |
82 | | * |
83 | | * There is no way to run two well-known string tables in one process now that the table is built at |
84 | | * compile time, so this stands in for the case the reader has to survive. Reading such a heap back |
85 | | * has to reproduce the header the writer had, because HTTPHdrImpl::recompute_wks_indices() rebuilds |
86 | | * all of it from the header strings the heap also carries. See the ATS_TEST_WKS_IDX_SHIFT autest. |
87 | | */ |
88 | | void |
89 | | test_shift_marshalled_wks_indices(HdrHeap *marshal_hdr) |
90 | 0 | { |
91 | 0 | if (test_wks_idx_shift == 0) { |
92 | 0 | return; |
93 | 0 | } |
94 | | |
95 | 0 | char *obj_data = reinterpret_cast<char *>(marshal_hdr) + HDR_HEAP_HDR_SIZE; |
96 | 0 | char *heap_end = reinterpret_cast<char *>(marshal_hdr) + marshal_hdr->m_size; |
97 | |
|
98 | 0 | while (obj_data < heap_end) { |
99 | 0 | HdrHeapObjImpl *obj = reinterpret_cast<HdrHeapObjImpl *>(obj_data); |
100 | |
|
101 | 0 | switch (static_cast<HdrHeapObjType>(obj->m_type)) { |
102 | 0 | case HdrHeapObjType::URL: { |
103 | 0 | URLImpl *url = reinterpret_cast<URLImpl *>(obj); |
104 | 0 | url->m_scheme_wks_idx = test_shift_wks_idx(url->m_scheme_wks_idx); |
105 | 0 | break; |
106 | 0 | } |
107 | 0 | case HdrHeapObjType::HTTP_HEADER: { |
108 | 0 | HTTPHdrImpl *hh = reinterpret_cast<HTTPHdrImpl *>(obj); |
109 | 0 | if (hh->m_polarity == HTTPType::REQUEST) { |
110 | 0 | hh->u.req.m_method_wks_idx = test_shift_wks_idx(hh->u.req.m_method_wks_idx); |
111 | 0 | } |
112 | 0 | break; |
113 | 0 | } |
114 | 0 | case HdrHeapObjType::FIELD_BLOCK: { |
115 | 0 | MIMEFieldBlockImpl *fblock = reinterpret_cast<MIMEFieldBlockImpl *>(obj); |
116 | 0 | for (uint32_t i = 0; i < fblock->m_freetop; ++i) { |
117 | 0 | MIMEField &field = fblock->m_field_slots[i]; |
118 | 0 | if (field.is_live()) { |
119 | 0 | field.m_wks_idx = test_shift_wks_idx(field.m_wks_idx); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | break; |
123 | 0 | } |
124 | 0 | case HdrHeapObjType::MIME_HEADER: { |
125 | 0 | MIMEHdrImpl *mh = reinterpret_cast<MIMEHdrImpl *>(obj); |
126 | 0 | mh->m_presence_bits = MIME_PRESENCE_NONE; |
127 | 0 | for (uint32_t &accelerator : mh->m_slot_accelerators) { |
128 | 0 | accelerator = 0xFFFFFFFF; |
129 | 0 | } |
130 | 0 | for (uint32_t i = 0; i < mh->m_first_fblock.m_freetop; ++i) { |
131 | 0 | MIMEField &field = mh->m_first_fblock.m_field_slots[i]; |
132 | 0 | if (field.is_live()) { |
133 | 0 | field.m_wks_idx = test_shift_wks_idx(field.m_wks_idx); |
134 | 0 | } |
135 | 0 | } |
136 | 0 | break; |
137 | 0 | } |
138 | 0 | default: |
139 | 0 | break; |
140 | 0 | } |
141 | | |
142 | 0 | if (obj->m_length <= 0) { |
143 | 0 | return; |
144 | 0 | } |
145 | 0 | obj_data += obj->m_length; |
146 | 0 | } |
147 | 0 | } |
148 | | #endif |
149 | | |
150 | | } // end anonymous namespace |
151 | | |
152 | | /*------------------------------------------------------------------------- |
153 | | -------------------------------------------------------------------------*/ |
154 | | |
155 | | void |
156 | | obj_describe(HdrHeapObjImpl *obj, bool recurse) |
157 | 0 | { |
158 | 0 | static constexpr const char *obj_names[] = {"EMPTY", "RAW", "URL", "HTTP_HEADER", "MIME_HEADER", "FIELD_BLOCK"}; |
159 | |
|
160 | 0 | Dbg(dbg_ctl_http, "%s %p: [T: %d, L: %4d, OBJFLAGS: %X] ", obj_names[obj->m_type], obj, obj->m_type, obj->m_length, |
161 | 0 | obj->m_obj_flags); |
162 | |
|
163 | 0 | switch (static_cast<HdrHeapObjType>(obj->m_type)) { |
164 | 0 | case HdrHeapObjType::EMPTY: |
165 | 0 | break; |
166 | 0 | case HdrHeapObjType::RAW: |
167 | 0 | break; |
168 | 0 | case HdrHeapObjType::MIME_HEADER: |
169 | 0 | mime_hdr_describe(obj, recurse); |
170 | 0 | break; |
171 | 0 | case HdrHeapObjType::FIELD_BLOCK: |
172 | 0 | mime_field_block_describe(obj, recurse); |
173 | 0 | break; |
174 | 0 | case HdrHeapObjType::HTTP_HEADER: |
175 | 0 | http_hdr_describe(obj, recurse); |
176 | 0 | break; |
177 | 0 | case HdrHeapObjType::URL: |
178 | 0 | url_describe(obj, recurse); |
179 | 0 | break; |
180 | 0 | default: |
181 | 0 | break; |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | /*------------------------------------------------------------------------- |
186 | | -------------------------------------------------------------------------*/ |
187 | | |
188 | | inline void |
189 | | HdrHeap::init() |
190 | 38.2k | { |
191 | 38.2k | m_data_start = m_free_start = (reinterpret_cast<char *>(this)) + HDR_HEAP_HDR_SIZE; |
192 | 38.2k | m_magic = HdrBufMagic::ALIVE; |
193 | 38.2k | m_writeable = true; |
194 | | |
195 | 38.2k | m_next = nullptr; |
196 | 38.2k | m_free_size = m_size - HDR_HEAP_HDR_SIZE; |
197 | | |
198 | | // We need to clear m_ptr directly since it's garbage and |
199 | | // using the operator functions will to free() what ever |
200 | | // garbage it is pointing to |
201 | 38.2k | m_read_write_heap.detach(); |
202 | | |
203 | 114k | for (auto &i : m_ronly_heap) { |
204 | 114k | i.m_heap_start = nullptr; |
205 | 114k | i.m_ref_count_ptr.detach(); |
206 | 114k | i.m_locked = false; |
207 | 114k | i.m_heap_len = 0; |
208 | 114k | } |
209 | 38.2k | m_lost_string_space = 0; |
210 | | |
211 | 38.2k | ink_assert(m_free_size > 0); |
212 | 38.2k | } |
213 | | |
214 | | HdrHeap * |
215 | | new_HdrHeap(int size) |
216 | 38.2k | { |
217 | 38.2k | HdrHeap *h; |
218 | 38.2k | if (size <= HdrHeap::DEFAULT_SIZE) { |
219 | 36.7k | size = HdrHeap::DEFAULT_SIZE; |
220 | 36.7k | h = static_cast<HdrHeap *>(THREAD_ALLOC(hdrHeapAllocator, this_ethread())); |
221 | 36.7k | } else { |
222 | 1.48k | h = static_cast<HdrHeap *>(ats_malloc(size)); |
223 | 1.48k | } |
224 | | |
225 | 38.2k | h->m_size = size; |
226 | 38.2k | h->init(); |
227 | | |
228 | 38.2k | return h; |
229 | 38.2k | } |
230 | | |
231 | | HdrStrHeap * |
232 | | HdrStrHeap::alloc(int heap_size) |
233 | 32.6k | { |
234 | | // The callee is asking for a string heap to be created |
235 | | // that can allocate at least size bytes. As such we, |
236 | | // need to include the size of the string heap header in |
237 | | // our calculations |
238 | | |
239 | 32.6k | int alloc_size = heap_size + sizeof(HdrStrHeap); |
240 | | |
241 | 32.6k | HdrStrHeap *sh; |
242 | 32.6k | if (alloc_size <= HdrStrHeap::DEFAULT_SIZE) { |
243 | 29.9k | alloc_size = HdrStrHeap::DEFAULT_SIZE; |
244 | 29.9k | sh = static_cast<HdrStrHeap *>(THREAD_ALLOC(strHeapAllocator, this_ethread())); |
245 | 29.9k | } else { |
246 | 2.65k | alloc_size = swoc::round_up<HdrStrHeap::DEFAULT_SIZE * 2>(alloc_size); |
247 | 2.65k | sh = static_cast<HdrStrHeap *>(ats_malloc(alloc_size)); |
248 | 2.65k | } |
249 | | |
250 | | // Placement new the HdrStrHeap. |
251 | 32.6k | sh = new (sh) HdrStrHeap(alloc_size); |
252 | | |
253 | 32.6k | sh->_avail_size = alloc_size - sizeof(HdrStrHeap); |
254 | | |
255 | 32.6k | ink_assert(sh->refcount() == 0); |
256 | | |
257 | 32.6k | ink_assert(int(sh->total_size()) == alloc_size); |
258 | | |
259 | 32.6k | ink_assert(sh->_avail_size > 0); |
260 | | |
261 | 32.6k | return sh; |
262 | 32.6k | } |
263 | | |
264 | | void |
265 | | HdrHeap::destroy() |
266 | 38.2k | { |
267 | 38.2k | if (m_next) { |
268 | 1.48k | m_next->destroy(); |
269 | 1.48k | } |
270 | | |
271 | 38.2k | m_read_write_heap = nullptr; |
272 | 114k | for (auto &i : m_ronly_heap) { |
273 | 114k | i.m_ref_count_ptr = nullptr; |
274 | 114k | } |
275 | | |
276 | 38.2k | if (m_size == HdrHeap::DEFAULT_SIZE) { |
277 | 36.7k | THREAD_FREE(this, hdrHeapAllocator, this_thread()); |
278 | 36.7k | } else { |
279 | 1.48k | ats_free(this); |
280 | 1.48k | } |
281 | 38.2k | } |
282 | | |
283 | | HdrHeapObjImpl * |
284 | | HdrHeap::allocate_obj(int nbytes, HdrHeapObjType type) |
285 | 125k | { |
286 | 125k | char *new_space; |
287 | 125k | HdrHeapObjImpl *obj; |
288 | | |
289 | 125k | ink_assert(m_writeable); |
290 | | |
291 | 125k | nbytes = HdrHeapMarshalBlocks{swoc::round_up(nbytes)}; |
292 | | |
293 | 125k | if (nbytes > static_cast<int>(HDR_MAX_ALLOC_SIZE)) { |
294 | 0 | ink_assert(!"alloc too big"); |
295 | 0 | return nullptr; |
296 | 0 | } |
297 | | |
298 | 125k | HdrHeap *h = this; |
299 | | |
300 | 239k | while (true) { |
301 | 239k | if (static_cast<unsigned>(nbytes) <= (h->m_free_size)) { |
302 | 125k | new_space = h->m_free_start; |
303 | 125k | h->m_free_start += nbytes; |
304 | 125k | h->m_free_size -= nbytes; |
305 | | |
306 | 125k | obj = reinterpret_cast<HdrHeapObjImpl *>(new_space); |
307 | 125k | obj_init_header(obj, type, nbytes, 0); |
308 | 125k | ink_assert(obj_is_aligned(obj)); |
309 | | |
310 | 125k | return obj; |
311 | 125k | } |
312 | | |
313 | 114k | if (h->m_next == nullptr) { |
314 | | // Allocate our next pointer heap |
315 | | // twice as large as this one so |
316 | | // number of pointer heaps is O(log n) |
317 | | // with regard to number of bytes allocated |
318 | 1.48k | h->m_next = new_HdrHeap(h->m_size * 2); |
319 | 1.48k | } |
320 | | |
321 | 114k | h = h->m_next; |
322 | 114k | } |
323 | 125k | } |
324 | | |
325 | | void |
326 | | HdrHeap::deallocate_obj(HdrHeapObjImpl *obj) |
327 | 0 | { |
328 | 0 | ink_assert(m_writeable); |
329 | 0 | obj->m_type = static_cast<uint32_t>(HdrHeapObjType::EMPTY); |
330 | 0 | } |
331 | | |
332 | | char * |
333 | | HdrHeap::allocate_str(int nbytes) |
334 | 397k | { |
335 | 397k | int last_size = 0; |
336 | 397k | int next_size = 0; |
337 | 397k | ink_assert(m_writeable); |
338 | | |
339 | | // INKqa08287 - We could get infinite build up |
340 | | // of dead strings on header merge. To prevent |
341 | | // this we keep track of the dead string space |
342 | | // and force a heap coalesce if it is too large. |
343 | | // Ideally this should be done on free_string() |
344 | | // but I already no that this code path is |
345 | | // safe for forcing a str coalesce so I'm doing |
346 | | // it here for sanity's sake |
347 | 397k | int coalesce = m_lost_string_space > static_cast<int>(MAX_LOST_STR_SPACE) ? 1 : 0; |
348 | | |
349 | 397k | for (;;) { |
350 | 397k | if (coalesce) { |
351 | 770 | switch (coalesce) { |
352 | 0 | case 2: |
353 | 0 | Warning("HdrHeap=%p coalescing twice", this); |
354 | 0 | break; |
355 | 0 | case 3: |
356 | 0 | Warning("HdrHeap=%p coalescing three or more times", this); |
357 | 0 | break; |
358 | 770 | default: |
359 | 770 | break; |
360 | 770 | } |
361 | | |
362 | 770 | coalesce_str_heaps(); |
363 | 770 | } |
364 | 399k | do { |
365 | | // First check to see if we have a read/write |
366 | | // string heap |
367 | 399k | if (!m_read_write_heap) { |
368 | 31.7k | if (next_size) { |
369 | 0 | Warning("HdrHeap=%p new read/write string heap twice last_size=%d", this, last_size); |
370 | 0 | } |
371 | 31.7k | next_size = (last_size * 2) - int(sizeof(HdrStrHeap)); |
372 | 31.7k | next_size = next_size > nbytes ? next_size : nbytes; |
373 | 31.7k | m_read_write_heap = HdrStrHeap::alloc(next_size); |
374 | 31.7k | } |
375 | | // Try to allocate of our read/write string heap |
376 | 399k | if (char *new_space = m_read_write_heap->allocate(nbytes); new_space) { |
377 | 397k | return new_space; |
378 | 397k | } |
379 | | |
380 | 2.00k | last_size = m_read_write_heap->total_size(); |
381 | | |
382 | | // Our existing rw str heap doesn't have sufficient |
383 | | // capacity. We need to move the current rw heap |
384 | | // out of the way and create a new one |
385 | 2.00k | } while (demote_rw_str_heap() == 0); |
386 | | |
387 | | // We failed to demote. We'll have to coalesce the heaps. |
388 | 54 | ++coalesce; |
389 | 54 | next_size = 0; |
390 | 54 | } |
391 | 397k | } |
392 | | |
393 | | // char* HdrHeap::expand_str(const char* old_str, int old_len, int new_len) |
394 | | // |
395 | | // Attempt to grow an already allocated string. For this to work, |
396 | | // the string has to be the last one in the read-write string |
397 | | // heap and there has to be enough space that string heap |
398 | | // If expansion succeeds, we return old_str. If it fails, we |
399 | | // return NULL |
400 | | // |
401 | | char * |
402 | | HdrHeap::expand_str(const char *old_str, int old_len, int new_len) |
403 | 6.47k | { |
404 | 6.47k | if (m_read_write_heap && m_read_write_heap->contains(old_str)) { |
405 | 6.24k | return m_read_write_heap->expand(const_cast<char *>(old_str), old_len, new_len); |
406 | 6.24k | } |
407 | | |
408 | 224 | return nullptr; |
409 | 6.47k | } |
410 | | |
411 | | // char* HdrHeap::duplicate_str(char* str, int nbytes) |
412 | | // |
413 | | // Allocates a new string and copies the old data. |
414 | | // Returns the new string pointer. |
415 | | // |
416 | | char * |
417 | | HdrHeap::duplicate_str(const char *str, int nbytes) |
418 | 397k | { |
419 | 397k | HeapGuard guard(this, str); // Don't let the source get de-allocated. |
420 | 397k | char *new_str = allocate_str(nbytes); |
421 | | |
422 | 397k | memcpy(new_str, str, nbytes); |
423 | 397k | return (new_str); |
424 | 397k | } |
425 | | |
426 | | // int HdrHeap::demote_rw_str_heap() |
427 | | // |
428 | | // Returns 0 on success and non-zero failure |
429 | | // Failure means all the read only heap slots |
430 | | // were full |
431 | | // |
432 | | int |
433 | | HdrHeap::demote_rw_str_heap() |
434 | 2.00k | { |
435 | | // First, see if we have any open slots for read |
436 | | // only heaps |
437 | 2.82k | for (auto &i : m_ronly_heap) { |
438 | 2.82k | if (i.m_heap_start == nullptr) { |
439 | | // We've found a slot |
440 | 1.94k | i.m_ref_count_ptr = m_read_write_heap.object(); |
441 | 1.94k | i.m_heap_start = reinterpret_cast<char *>(m_read_write_heap.get()); |
442 | 1.94k | i.m_heap_len = m_read_write_heap->total_size() - m_read_write_heap->space_avail(); |
443 | | |
444 | | // Dbg(dbg_ctl_hdrs, "Demoted rw heap of %d size", m_read_write_heap->total_size()); |
445 | 1.94k | m_read_write_heap = nullptr; |
446 | 1.94k | return 0; |
447 | 1.94k | } |
448 | 2.82k | } |
449 | | |
450 | | // No open slots |
451 | 54 | return 1; |
452 | 2.00k | } |
453 | | |
454 | | // void HdrHeap::coalesce_heaps() |
455 | | // |
456 | | // Take existing stringheaps and combine them to free up |
457 | | // slots in the heap array |
458 | | // |
459 | | // FIX ME: Should we combine a subset of the heaps |
460 | | // or all of them? Current plan is combine all heaps |
461 | | // since saves doing bounds checks every string. At |
462 | | // expense of doing far more copying |
463 | | // |
464 | | void |
465 | | HdrHeap::coalesce_str_heaps(int incoming_size) |
466 | 867 | { |
467 | 867 | int new_heap_size = incoming_size; |
468 | 867 | ink_assert(incoming_size >= 0); |
469 | 867 | ink_assert(m_writeable); |
470 | | |
471 | 867 | new_heap_size += required_space_for_evacuation(); |
472 | | |
473 | 867 | HdrStrHeap *new_heap = HdrStrHeap::alloc(new_heap_size); |
474 | 867 | evacuate_from_str_heaps(new_heap); |
475 | 867 | m_lost_string_space = 0; |
476 | | |
477 | | // At this point none of the currently used string |
478 | | // heaps are needed since everything is in the |
479 | | // new string heap. So deallocate all the old heaps |
480 | 867 | m_read_write_heap = new_heap; |
481 | | |
482 | 867 | int heaps_removed = 0; |
483 | 2.60k | for (auto &j : m_ronly_heap) { |
484 | 2.60k | if (j.m_heap_start != nullptr && j.m_locked == false) { |
485 | 1.02k | j.m_ref_count_ptr = nullptr; |
486 | 1.02k | j.m_heap_start = nullptr; |
487 | 1.02k | j.m_heap_len = 0; |
488 | 1.02k | heaps_removed++; |
489 | 1.02k | } |
490 | 2.60k | } |
491 | | |
492 | | // This function is presumed to free up read only |
493 | | // string heap slots or be for incoming heaps |
494 | | // If we don't have any free heaps, we are screwed |
495 | 867 | ink_assert(heaps_removed > 0 || incoming_size > 0 || m_ronly_heap[0].m_heap_start == nullptr); |
496 | 867 | } |
497 | | |
498 | | void |
499 | | HdrHeap::evacuate_from_str_heaps(HdrStrHeap *new_heap) |
500 | 867 | { |
501 | | // printf("Str Evac\n"); |
502 | | // Loop over the objects in heap and call the evacuation |
503 | | // function in each one |
504 | 867 | HdrHeap *h = this; |
505 | 867 | ink_assert(m_writeable); |
506 | | |
507 | 2.53k | while (h) { |
508 | 1.66k | char *data = h->m_data_start; |
509 | | |
510 | 22.0k | while (data < h->m_free_start) { |
511 | 20.4k | HdrHeapObjImpl *obj = reinterpret_cast<HdrHeapObjImpl *>(data); |
512 | | |
513 | | // Object length cannot be 0 by design, otherwise something is wrong + infinite loop here! |
514 | 20.4k | ink_release_assert(0 != obj->m_length); |
515 | | |
516 | 20.4k | switch (static_cast<HdrHeapObjType>(obj->m_type)) { |
517 | 824 | case HdrHeapObjType::URL: |
518 | 824 | ((URLImpl *)obj)->move_strings(new_heap); |
519 | 824 | break; |
520 | 867 | case HdrHeapObjType::HTTP_HEADER: |
521 | 867 | ((HTTPHdrImpl *)obj)->move_strings(new_heap); |
522 | 867 | break; |
523 | 867 | case HdrHeapObjType::MIME_HEADER: |
524 | 867 | ((MIMEHdrImpl *)obj)->move_strings(new_heap); |
525 | 867 | break; |
526 | 17.8k | case HdrHeapObjType::FIELD_BLOCK: |
527 | 17.8k | ((MIMEFieldBlockImpl *)obj)->move_strings(new_heap); |
528 | 17.8k | break; |
529 | 0 | case HdrHeapObjType::EMPTY: |
530 | 0 | case HdrHeapObjType::RAW: |
531 | | // Nothing to do |
532 | 0 | break; |
533 | 0 | default: |
534 | 0 | ink_release_assert(0); |
535 | 20.4k | } |
536 | | |
537 | 20.4k | data = data + obj->m_length; |
538 | 20.4k | } |
539 | | |
540 | 1.66k | h = h->m_next; |
541 | 1.66k | } |
542 | 867 | } |
543 | | |
544 | | size_t |
545 | | HdrHeap::required_space_for_evacuation() |
546 | 867 | { |
547 | 867 | size_t ret = 0; |
548 | 867 | HdrHeap *h = this; |
549 | 2.53k | while (h) { |
550 | 1.66k | char *data = h->m_data_start; |
551 | 1.66k | HdrHeapObjImpl *prev_obj = nullptr; |
552 | | |
553 | 22.0k | while (data < h->m_free_start) { |
554 | 20.4k | HdrHeapObjImpl *obj = reinterpret_cast<HdrHeapObjImpl *>(data); |
555 | | |
556 | | // Object length cannot be 0 by design, otherwise something is wrong + infinite loop here! |
557 | 20.4k | ink_release_assert(0 != obj->m_length); |
558 | | |
559 | 20.4k | switch (static_cast<HdrHeapObjType>(obj->m_type)) { |
560 | 824 | case HdrHeapObjType::URL: |
561 | 824 | ret += ((URLImpl *)obj)->strings_length(); |
562 | 824 | break; |
563 | 867 | case HdrHeapObjType::HTTP_HEADER: |
564 | 867 | ret += ((HTTPHdrImpl *)obj)->strings_length(); |
565 | 867 | break; |
566 | 867 | case HdrHeapObjType::MIME_HEADER: |
567 | 867 | ret += ((MIMEHdrImpl *)obj)->strings_length(); |
568 | 867 | break; |
569 | 17.8k | case HdrHeapObjType::FIELD_BLOCK: |
570 | 17.8k | ret += ((MIMEFieldBlockImpl *)obj)->strings_length(); |
571 | 17.8k | break; |
572 | 0 | case HdrHeapObjType::EMPTY: |
573 | 0 | case HdrHeapObjType::RAW: |
574 | | // Nothing to do |
575 | 0 | break; |
576 | 0 | default: |
577 | 0 | ink_release_assert(0); |
578 | 20.4k | } |
579 | | |
580 | | // coalesce empty objects next to each other |
581 | 20.4k | if (static_cast<HdrHeapObjType>(obj->m_type) == HdrHeapObjType::EMPTY) { |
582 | 0 | if (prev_obj != nullptr && prev_obj->m_length < (MAX_HDR_HEAP_OBJ_LENGTH - obj->m_length)) { |
583 | 0 | prev_obj->m_length += obj->m_length; |
584 | 0 | ink_release_assert(prev_obj->m_length > 0); |
585 | 0 | } else { |
586 | 0 | prev_obj = obj; |
587 | 0 | } |
588 | 20.4k | } else { |
589 | 20.4k | prev_obj = nullptr; |
590 | 20.4k | } |
591 | | |
592 | 20.4k | data = data + obj->m_length; |
593 | 20.4k | } |
594 | 1.66k | h = h->m_next; |
595 | 1.66k | } |
596 | 867 | return ret; |
597 | 867 | } |
598 | | |
599 | | void |
600 | | HdrHeap::sanity_check_strs() |
601 | 0 | { |
602 | 0 | int num_heaps = 0; |
603 | 0 | struct HeapCheck heaps[HDR_BUF_RONLY_HEAPS + 1]; |
604 | | |
605 | | // Build up a string check table |
606 | 0 | if (m_read_write_heap) { |
607 | 0 | heaps[num_heaps].start = (reinterpret_cast<char *>(m_read_write_heap.get())) + sizeof(HdrStrHeap); |
608 | |
|
609 | 0 | int heap_size = m_read_write_heap->total_size() - (sizeof(HdrStrHeap) + m_read_write_heap->space_avail()); |
610 | |
|
611 | 0 | heaps[num_heaps].end = heaps[num_heaps].start + heap_size; |
612 | 0 | num_heaps++; |
613 | 0 | } |
614 | |
|
615 | 0 | for (auto &i : m_ronly_heap) { |
616 | 0 | if (i.m_heap_start != nullptr) { |
617 | 0 | heaps[num_heaps].start = i.m_heap_start; |
618 | 0 | heaps[num_heaps].end = i.m_heap_start + i.m_heap_len; |
619 | 0 | num_heaps++; |
620 | 0 | } |
621 | 0 | } |
622 | | |
623 | | // Loop over the objects in heap call the check |
624 | | // function on each one |
625 | 0 | HdrHeap *h = this; |
626 | |
|
627 | 0 | while (h) { |
628 | 0 | char *data = h->m_data_start; |
629 | |
|
630 | 0 | while (data < h->m_free_start) { |
631 | 0 | HdrHeapObjImpl *obj = reinterpret_cast<HdrHeapObjImpl *>(data); |
632 | | |
633 | | // Object length cannot be 0 by design, otherwise something is wrong + infinite loop here! |
634 | 0 | ink_release_assert(0 != obj->m_length); |
635 | | |
636 | 0 | switch (static_cast<HdrHeapObjType>(obj->m_type)) { |
637 | 0 | case HdrHeapObjType::URL: |
638 | 0 | ((URLImpl *)obj)->check_strings(heaps, num_heaps); |
639 | 0 | break; |
640 | 0 | case HdrHeapObjType::HTTP_HEADER: |
641 | 0 | ((HTTPHdrImpl *)obj)->check_strings(heaps, num_heaps); |
642 | 0 | break; |
643 | 0 | case HdrHeapObjType::MIME_HEADER: |
644 | 0 | ((MIMEHdrImpl *)obj)->check_strings(heaps, num_heaps); |
645 | 0 | break; |
646 | 0 | case HdrHeapObjType::FIELD_BLOCK: |
647 | 0 | ((MIMEFieldBlockImpl *)obj)->check_strings(heaps, num_heaps); |
648 | 0 | break; |
649 | 0 | case HdrHeapObjType::EMPTY: |
650 | 0 | case HdrHeapObjType::RAW: |
651 | | // Nothing to do |
652 | 0 | break; |
653 | 0 | default: |
654 | 0 | ink_release_assert(0); |
655 | 0 | } |
656 | | |
657 | 0 | data = data + obj->m_length; |
658 | 0 | } |
659 | | |
660 | 0 | h = h->m_next; |
661 | 0 | } |
662 | 0 | } |
663 | | |
664 | | // int HdrHeap::marshal_length() |
665 | | // |
666 | | // Determines what the length of a buffer needs to |
667 | | // be to marshal this header |
668 | | // |
669 | | int |
670 | | HdrHeap::marshal_length() |
671 | 0 | { |
672 | 0 | int len; |
673 | | |
674 | | // If there is more than one HdrHeap block, we'll |
675 | | // coalesce the HdrHeap blocks together so we |
676 | | // only need one block header |
677 | 0 | len = HDR_HEAP_HDR_SIZE; |
678 | 0 | HdrHeap *h = this; |
679 | |
|
680 | 0 | while (h) { |
681 | 0 | len += static_cast<int>(h->m_free_start - h->m_data_start); |
682 | 0 | h = h->m_next; |
683 | 0 | } |
684 | | |
685 | | // Since when we unmarshal, we won't have a writable string |
686 | | // heap, we can drop the header on the read/write |
687 | | // string heap |
688 | 0 | if (m_read_write_heap) { |
689 | 0 | len += m_read_write_heap->total_size() - (sizeof(HdrStrHeap) + m_read_write_heap->space_avail()); |
690 | 0 | } |
691 | |
|
692 | 0 | for (auto &j : m_ronly_heap) { |
693 | 0 | if (j.m_heap_start != nullptr) { |
694 | 0 | len += j.m_heap_len; |
695 | 0 | } |
696 | 0 | } |
697 | |
|
698 | 0 | len = HdrHeapMarshalBlocks(swoc::round_up(len)); |
699 | 0 | return len; |
700 | 0 | } |
701 | | |
702 | | #ifdef HDR_HEAP_CHECKSUMS |
703 | | static uint32_t |
704 | | compute_checksum(void *buf, int len) |
705 | | { |
706 | | uint32_t cksum = 0; |
707 | | |
708 | | while (len > 4) { |
709 | | cksum += *((uint32_t *)buf); |
710 | | buf = ((char *)buf) + 4; |
711 | | len -= 4; |
712 | | } |
713 | | |
714 | | if (len > 0) { |
715 | | uint32_t tmp = 0; |
716 | | memcpy((char *)&tmp, buf, len); |
717 | | cksum += tmp; |
718 | | } |
719 | | |
720 | | return cksum; |
721 | | } |
722 | | #endif |
723 | | |
724 | | // int HdrHeap::marshal(char* buf, int len) |
725 | | // |
726 | | // Creates a marshalled representation of the contents |
727 | | // of HdrHeap. The marshalled representation is ususable |
728 | | // as a read-only HdrHeap after an unmarshal operation which |
729 | | // only swizzles offsets to pointer. Special care needs to be |
730 | | // taken not to mess up the alignment of objects in |
731 | | // the heap to make this representation usable in the read-only |
732 | | // form |
733 | | // |
734 | | int |
735 | | HdrHeap::marshal(char *buf, int len) |
736 | 0 | { |
737 | 0 | ink_assert((((uintptr_t)buf) & HDR_PTR_ALIGNMENT_MASK) == 0); |
738 | |
|
739 | 0 | HdrHeap *marshal_hdr = reinterpret_cast<HdrHeap *>(buf); |
740 | 0 | char *b = buf + HDR_HEAP_HDR_SIZE; |
741 | | |
742 | | // Variables for the ptr translation table |
743 | 0 | int ptr_xl_size = 2; |
744 | 0 | MarshalXlate static_table[2]; |
745 | 0 | MarshalXlate *ptr_xlation = static_table; |
746 | | // need to initialize it here because of those gotos |
747 | 0 | MarshalXlate str_xlation[HDR_BUF_RONLY_HEAPS + 1]; |
748 | | |
749 | | // Let's start by skipping over the header block |
750 | | // and copying the pointer blocks to marshalled |
751 | | // buffer |
752 | 0 | int ptr_heap_size = 0; |
753 | 0 | int str_size = 0; |
754 | 0 | int ptr_heaps = 0; |
755 | 0 | int str_heaps = 0; |
756 | | |
757 | | // Variables used later on. Sunpro doesn't like |
758 | | // bypassing initializations with gotos |
759 | 0 | int used; |
760 | |
|
761 | 0 | HdrHeap *unmarshal_hdr = this; |
762 | |
|
763 | 0 | do { |
764 | 0 | int copy_size = static_cast<int>(unmarshal_hdr->m_free_start - unmarshal_hdr->m_data_start); |
765 | |
|
766 | 0 | if (copy_size > len) { |
767 | 0 | goto Failed; |
768 | 0 | } |
769 | 0 | memcpy(b, unmarshal_hdr->m_data_start, copy_size); |
770 | | |
771 | | // Expand ptr xlation table if necessary - shameless hackery |
772 | 0 | if (ptr_heaps >= ptr_xl_size) { |
773 | 0 | MarshalXlate *tmp_xl = static_cast<MarshalXlate *>(alloca(sizeof(MarshalXlate) * ptr_xl_size * 2)); |
774 | 0 | memcpy(tmp_xl, ptr_xlation, sizeof(MarshalXlate) * ptr_xl_size); |
775 | 0 | ptr_xlation = tmp_xl; |
776 | 0 | ptr_xl_size *= 2; |
777 | 0 | } |
778 | | // Add translation table entry for pointer heaps |
779 | | // FIX ME - possible offset overflow issues? |
780 | 0 | ptr_xlation[ptr_heaps].start = unmarshal_hdr->m_data_start; |
781 | 0 | ptr_xlation[ptr_heaps].end = unmarshal_hdr->m_free_start; |
782 | 0 | ptr_xlation[ptr_heaps].offset = unmarshal_hdr->m_data_start - (b - buf); |
783 | |
|
784 | 0 | ptr_heap_size += copy_size; |
785 | 0 | b += copy_size; |
786 | 0 | len -= copy_size; |
787 | 0 | ptr_heaps++; |
788 | |
|
789 | 0 | unmarshal_hdr = unmarshal_hdr->m_next; |
790 | 0 | } while (unmarshal_hdr); |
791 | | |
792 | | // Now that we've got the pointer blocks marshaled |
793 | | // we can fill in the header on marshalled block |
794 | 0 | marshal_hdr->m_free_start = nullptr; |
795 | 0 | marshal_hdr->m_data_start = reinterpret_cast<char *>(HDR_HEAP_HDR_SIZE.value()); // offset |
796 | 0 | marshal_hdr->m_magic = HdrBufMagic::MARSHALED; |
797 | 0 | marshal_hdr->m_writeable = false; |
798 | 0 | marshal_hdr->m_size = ptr_heap_size + HDR_HEAP_HDR_SIZE; |
799 | 0 | marshal_hdr->m_next = nullptr; |
800 | 0 | marshal_hdr->m_free_size = 0; |
801 | 0 | marshal_hdr->m_read_write_heap.detach(); |
802 | 0 | marshal_hdr->m_lost_string_space = this->m_lost_string_space; |
803 | | |
804 | | // We'have one read-only string heap after marshalling |
805 | 0 | marshal_hdr->m_ronly_heap[0].m_heap_start = (char *)static_cast<intptr_t>(marshal_hdr->m_size); // offset |
806 | 0 | marshal_hdr->m_ronly_heap[0].m_ref_count_ptr.detach(); |
807 | |
|
808 | 0 | for (unsigned i = 1; i < HDR_BUF_RONLY_HEAPS; ++i) { |
809 | 0 | marshal_hdr->m_ronly_heap[i].m_heap_start = nullptr; |
810 | 0 | } |
811 | | |
812 | | // Next order of business is to copy over string heaps |
813 | | // As we are copying over the string heaps, build |
814 | | // translation table for string marshaling in the heap |
815 | | // objects |
816 | | // |
817 | | // FIX ME - really ought to check to see if lost_string_space |
818 | | // is too big and only copy over live strings if it is. May |
819 | | // not be too much of a problem since I've prevented too much |
820 | | // lost string space both in string alloc and inherit |
821 | |
|
822 | 0 | if (m_read_write_heap) { |
823 | 0 | char *copy_start = (reinterpret_cast<char *>(m_read_write_heap.get())) + sizeof(HdrStrHeap); |
824 | 0 | int nto_copy = m_read_write_heap->total_size() - (sizeof(HdrStrHeap) + m_read_write_heap->space_avail()); |
825 | |
|
826 | 0 | if (nto_copy > len) { |
827 | 0 | goto Failed; |
828 | 0 | } |
829 | | |
830 | 0 | memcpy(b, copy_start, nto_copy); |
831 | | |
832 | | // FIX ME - possible offset overflow issues? |
833 | 0 | str_xlation[str_heaps].start = copy_start; |
834 | 0 | str_xlation[str_heaps].end = copy_start + nto_copy; |
835 | 0 | str_xlation[str_heaps].offset = copy_start - (b - buf); |
836 | |
|
837 | 0 | b += nto_copy; |
838 | 0 | len -= nto_copy; |
839 | 0 | str_size += nto_copy; |
840 | 0 | str_heaps++; |
841 | 0 | } |
842 | | |
843 | 0 | for (auto &i : m_ronly_heap) { |
844 | 0 | if (i.m_heap_start != nullptr) { |
845 | 0 | if (i.m_heap_len > len) { |
846 | 0 | goto Failed; |
847 | 0 | } |
848 | | |
849 | 0 | memcpy(b, i.m_heap_start, i.m_heap_len); |
850 | | |
851 | | // Add translation table entry for string heaps |
852 | | // FIX ME - possible offset overflow issues? |
853 | 0 | str_xlation[str_heaps].start = i.m_heap_start; |
854 | 0 | str_xlation[str_heaps].end = i.m_heap_start + i.m_heap_len; |
855 | 0 | str_xlation[str_heaps].offset = str_xlation[str_heaps].start - (b - buf); |
856 | 0 | ink_assert(str_xlation[str_heaps].start <= str_xlation[str_heaps].end); |
857 | |
|
858 | 0 | str_heaps++; |
859 | 0 | b += i.m_heap_len; |
860 | 0 | len -= i.m_heap_len; |
861 | 0 | str_size += i.m_heap_len; |
862 | 0 | } |
863 | 0 | } |
864 | | |
865 | | // Patch the str heap len |
866 | 0 | marshal_hdr->m_ronly_heap[0].m_heap_len = str_size; |
867 | | |
868 | | // Take our translation tables and loop over the objects |
869 | | // and call the object marshal function to patch live |
870 | | // strings pointers & live object pointers to offsets |
871 | 0 | { |
872 | 0 | char *obj_data = (reinterpret_cast<char *>(marshal_hdr)) + HDR_HEAP_HDR_SIZE; |
873 | 0 | char *mheap_end = (reinterpret_cast<char *>(marshal_hdr)) + marshal_hdr->m_size; |
874 | |
|
875 | 0 | while (obj_data < mheap_end) { |
876 | 0 | HdrHeapObjImpl *obj = reinterpret_cast<HdrHeapObjImpl *>(obj_data); |
877 | 0 | ink_assert(obj_is_aligned(obj)); |
878 | |
|
879 | 0 | switch (static_cast<HdrHeapObjType>(obj->m_type)) { |
880 | 0 | case HdrHeapObjType::URL: |
881 | 0 | if (((URLImpl *)obj)->marshal(str_xlation, str_heaps) < 0) { |
882 | 0 | goto Failed; |
883 | 0 | } |
884 | 0 | break; |
885 | 0 | case HdrHeapObjType::HTTP_HEADER: |
886 | 0 | if (((HTTPHdrImpl *)obj)->marshal(ptr_xlation, ptr_heaps, str_xlation, str_heaps) < 0) { |
887 | 0 | goto Failed; |
888 | 0 | } |
889 | 0 | break; |
890 | 0 | case HdrHeapObjType::FIELD_BLOCK: |
891 | 0 | if (((MIMEFieldBlockImpl *)obj)->marshal(ptr_xlation, ptr_heaps, str_xlation, str_heaps) < 0) { |
892 | 0 | goto Failed; |
893 | 0 | } |
894 | 0 | break; |
895 | 0 | case HdrHeapObjType::MIME_HEADER: |
896 | 0 | if (((MIMEHdrImpl *)obj)->marshal(ptr_xlation, ptr_heaps, str_xlation, str_heaps)) { |
897 | 0 | goto Failed; |
898 | 0 | } |
899 | 0 | break; |
900 | 0 | case HdrHeapObjType::EMPTY: |
901 | 0 | case HdrHeapObjType::RAW: |
902 | | // Check to make sure we aren't stuck |
903 | | // in an infinite loop |
904 | 0 | if (obj->m_length <= 0) { |
905 | 0 | ink_assert(0); |
906 | 0 | goto Failed; |
907 | 0 | } |
908 | | // Nothing to do |
909 | 0 | break; |
910 | 0 | default: |
911 | 0 | ink_release_assert(0); |
912 | 0 | } |
913 | | |
914 | 0 | obj_data = obj_data + obj->m_length; |
915 | 0 | } |
916 | 0 | } |
917 | | |
918 | 0 | #if TS_HAS_TESTS |
919 | 0 | test_shift_marshalled_wks_indices(marshal_hdr); |
920 | 0 | #endif |
921 | | |
922 | | // Add up the total bytes used |
923 | 0 | used = ptr_heap_size + str_size + HDR_HEAP_HDR_SIZE; |
924 | 0 | used = HdrHeapMarshalBlocks(swoc::round_up(used)); |
925 | |
|
926 | | #ifdef HDR_HEAP_CHECKSUMS |
927 | | { |
928 | | uint32_t chksum = compute_checksum(buf, used); |
929 | | marshal_hdr->m_free_start = (char *)chksum; |
930 | | } |
931 | | #endif |
932 | |
|
933 | 0 | return used; |
934 | | |
935 | 0 | Failed: |
936 | 0 | marshal_hdr->m_magic = HdrBufMagic::CORRUPT; |
937 | 0 | return -1; |
938 | 0 | } |
939 | | |
940 | | // bool HdrHeap::check_marshalled(char* buf, int buf_length) |
941 | | // |
942 | | // Takes in marshalled buffer and verifies whether stuff appears |
943 | | // to be sane. Returns true is sane. Returns false if corrupt |
944 | | // |
945 | | bool |
946 | | HdrHeap::check_marshalled(uint32_t buf_length) |
947 | 0 | { |
948 | 0 | if (this->m_magic != HdrBufMagic::MARSHALED) { |
949 | 0 | return false; |
950 | 0 | } |
951 | | |
952 | 0 | if (this->m_size < (uint32_t)HDR_HEAP_HDR_SIZE) { |
953 | 0 | return false; |
954 | 0 | } |
955 | | |
956 | 0 | if (this->m_size != (uintptr_t)this->m_ronly_heap[0].m_heap_start) { |
957 | 0 | return false; |
958 | 0 | } |
959 | | |
960 | 0 | if (m_ronly_heap[0].m_heap_len < 0) { |
961 | 0 | return false; |
962 | 0 | } |
963 | | |
964 | 0 | if (static_cast<uint64_t>(this->m_size) + static_cast<uint64_t>(m_ronly_heap[0].m_heap_len) > buf_length) { |
965 | 0 | return false; |
966 | 0 | } |
967 | | |
968 | 0 | if (this->m_writeable != false) { |
969 | 0 | return false; |
970 | 0 | } |
971 | | |
972 | 0 | if (this->m_free_size != 0) { |
973 | 0 | return false; |
974 | 0 | } |
975 | | |
976 | 0 | if (this->m_ronly_heap[0].m_heap_start == nullptr) { |
977 | 0 | return false; |
978 | 0 | } |
979 | | |
980 | 0 | return true; |
981 | 0 | } |
982 | | |
983 | | // int HdrHeap::unmarshal(int buf_length, int obj_type, |
984 | | // HdrHeapObjImpl** found_obj, |
985 | | // RefCountObj* block_ref) |
986 | | // |
987 | | // Takes a marshalled representation and swizzles offsets |
988 | | // so they become live pointers and make the heap usable. |
989 | | // Sets *found_obj to first occurrence of object of |
990 | | // type obj_type in the heap |
991 | | // |
992 | | // Return value is the number of bytes unmarshalled or -1 |
993 | | // if error. Caller is responsible for memory |
994 | | // management policy |
995 | | // |
996 | | int |
997 | | HdrHeap::unmarshal(int buf_length, int obj_type, HdrHeapObjImpl **found_obj, RefCountObj *block_ref) |
998 | 0 | { |
999 | 0 | *found_obj = nullptr; |
1000 | | |
1001 | | // Check out this heap and make sure it is OK |
1002 | 0 | if (m_magic != HdrBufMagic::MARSHALED) { |
1003 | 0 | ink_assert(!"HdrHeap::unmarshal bad magic"); |
1004 | 0 | return -1; |
1005 | 0 | } |
1006 | | |
1007 | 0 | if (m_size < static_cast<uint32_t>(HDR_HEAP_HDR_SIZE) || // heap too small for header |
1008 | 0 | m_size != (uintptr_t)m_ronly_heap[0].m_heap_start || // string heap offset inconsistent |
1009 | 0 | m_ronly_heap[0].m_heap_len < 0 || // invalid string heap length |
1010 | 0 | buf_length < 0 || // invalid buf_length |
1011 | 0 | static_cast<uint64_t>(m_size) + static_cast<uint64_t>(m_ronly_heap[0].m_heap_len) > static_cast<uint64_t>(buf_length)) { |
1012 | 0 | ink_assert(!"HdrHeap::unmarshal truncated header"); |
1013 | 0 | return -1; |
1014 | 0 | } |
1015 | 0 | int unmarshal_size = this->unmarshal_size(); |
1016 | | #ifdef HDR_HEAP_CHECKSUMS |
1017 | | if (m_free_start != NULL) { |
1018 | | uint32_t stored_sum = (uint32_t)m_free_start; |
1019 | | m_free_start = NULL; |
1020 | | int sum_len = ROUND(unmarshal_size, HDR_PTR_SIZE); |
1021 | | uint32_t new_sum = compute_checksum((void *)this, sum_len); |
1022 | | |
1023 | | if (stored_sum != new_sum) { |
1024 | | fprintf(stderr, "WARNING: Unmarshal checksum comparison failed\n"); |
1025 | | dump_heap(unmarshal_size); |
1026 | | ink_assert(!"HdrHeap::unmarshal checksum failure"); |
1027 | | return -1; |
1028 | | } |
1029 | | } |
1030 | | #else |
1031 | | // Because checksums could have been enabled in the past |
1032 | | // and then be turned off without clearing the cache, |
1033 | | // always reset our variable we use for checksumming |
1034 | 0 | m_free_start = nullptr; |
1035 | 0 | #endif |
1036 | |
|
1037 | 0 | ink_release_assert(m_writeable == false); |
1038 | 0 | ink_release_assert(m_free_size == 0); |
1039 | 0 | ink_release_assert(m_ronly_heap[0].m_heap_start != nullptr); |
1040 | | |
1041 | 0 | ink_assert(m_free_start == nullptr); |
1042 | | |
1043 | | // Convert Heap offsets to pointers |
1044 | 0 | m_data_start = (reinterpret_cast<char *>(this)) + (intptr_t)m_data_start; |
1045 | 0 | m_free_start = (reinterpret_cast<char *>(this)) + m_size; |
1046 | 0 | m_ronly_heap[0].m_heap_start = (reinterpret_cast<char *>(this)) + (intptr_t)m_ronly_heap[0].m_heap_start; |
1047 | | |
1048 | | // Crazy Invariant - If we are sitting in a ref counted block, |
1049 | | // the HdrHeap lifetime is externally determined. Whoever |
1050 | | // unmarshalls us should keep the block around as long as |
1051 | | // they want to use the header. However, the strings can |
1052 | | // live beyond the heap life time because they are copied |
1053 | | // by reference into other header heap therefore we need |
1054 | | // to the set the refcount ptr for the strings. We don't |
1055 | | // actually increase the refcount here since for the header |
1056 | | // the lifetime is explicit but copies will increase |
1057 | | // the refcount |
1058 | 0 | if (block_ref) { |
1059 | 0 | m_ronly_heap[0].m_ref_count_ptr.swizzle(block_ref); |
1060 | 0 | } |
1061 | | |
1062 | | // Loop over objects and swizzle there pointer to |
1063 | | // live offsets |
1064 | 0 | char *obj_data = m_data_start; |
1065 | 0 | intptr_t offset = (intptr_t)this; |
1066 | |
|
1067 | 0 | while (obj_data < m_free_start) { |
1068 | 0 | HdrHeapObjImpl *obj = reinterpret_cast<HdrHeapObjImpl *>(obj_data); |
1069 | 0 | ink_assert(obj_is_aligned(obj)); |
1070 | | |
1071 | | // Object length cannot be 0 by design, otherwise something is wrong + infinite loop here! |
1072 | 0 | ink_release_assert(0 != obj->m_length); |
1073 | | |
1074 | 0 | if (obj->m_type == static_cast<unsigned>(obj_type) && *found_obj == nullptr) { |
1075 | 0 | *found_obj = obj; |
1076 | 0 | } |
1077 | |
|
1078 | 0 | switch (static_cast<HdrHeapObjType>(obj->m_type)) { |
1079 | 0 | case HdrHeapObjType::HTTP_HEADER: |
1080 | 0 | ((HTTPHdrImpl *)obj)->unmarshal(offset); |
1081 | 0 | break; |
1082 | 0 | case HdrHeapObjType::URL: |
1083 | 0 | ((URLImpl *)obj)->unmarshal(offset); |
1084 | 0 | break; |
1085 | 0 | case HdrHeapObjType::FIELD_BLOCK: |
1086 | 0 | ((MIMEFieldBlockImpl *)obj)->unmarshal(offset); |
1087 | 0 | break; |
1088 | 0 | case HdrHeapObjType::MIME_HEADER: |
1089 | 0 | ((MIMEHdrImpl *)obj)->unmarshal(offset); |
1090 | 0 | break; |
1091 | 0 | case HdrHeapObjType::EMPTY: |
1092 | | // Nothing to do |
1093 | 0 | break; |
1094 | 0 | default: |
1095 | 0 | fprintf(stderr, "WARNING: Unmarshal failed due to unknown obj type %d after %d bytes", static_cast<int>(obj->m_type), |
1096 | 0 | static_cast<int>(obj_data - reinterpret_cast<char *>(this))); |
1097 | 0 | dump_heap(unmarshal_size); |
1098 | 0 | return -1; |
1099 | 0 | } |
1100 | | |
1101 | 0 | obj_data = obj_data + obj->m_length; |
1102 | 0 | } |
1103 | | |
1104 | 0 | m_magic = HdrBufMagic::ALIVE; |
1105 | |
|
1106 | 0 | unmarshal_size = HdrHeapMarshalBlocks(swoc::round_up(unmarshal_size)); |
1107 | 0 | return unmarshal_size; |
1108 | 0 | } |
1109 | | |
1110 | | inline bool |
1111 | | HdrHeap::attach_str_heap(char const *h_start, int h_len, RefCountObj *h_ref_obj, int *index) |
1112 | 2.47k | { |
1113 | 2.47k | if (*index >= static_cast<int>(HDR_BUF_RONLY_HEAPS)) { |
1114 | 0 | return false; |
1115 | 0 | } |
1116 | | |
1117 | | // Loop over existing entries to see if this one is already present |
1118 | 3.03k | for (int z = 0; z < *index; z++) { |
1119 | 563 | if (m_ronly_heap[z].m_heap_start == h_start) { |
1120 | 0 | ink_assert(m_ronly_heap[z].m_ref_count_ptr.object() == h_ref_obj); |
1121 | | |
1122 | | // The lengths could be different because our copy could be |
1123 | | // read-only and the copy we are attaching from could be |
1124 | | // read-write and have expanded since the last time |
1125 | | // to was attached |
1126 | 0 | if (h_len > m_ronly_heap[z].m_heap_len) { |
1127 | 0 | m_ronly_heap[z].m_heap_len = h_len; |
1128 | 0 | } |
1129 | 0 | return true; |
1130 | 0 | } |
1131 | 563 | } |
1132 | | |
1133 | 2.47k | m_ronly_heap[*index].m_ref_count_ptr = h_ref_obj; |
1134 | 2.47k | m_ronly_heap[*index].m_heap_start = h_start; |
1135 | 2.47k | m_ronly_heap[*index].m_heap_len = h_len; |
1136 | 2.47k | m_ronly_heap[*index].m_locked = false; |
1137 | 2.47k | *index = *index + 1; |
1138 | | |
1139 | 2.47k | return true; |
1140 | 2.47k | } |
1141 | | |
1142 | | // void HdrHeap::inherit_string_heaps(const HdrHeap* inherit_from) |
1143 | | // |
1144 | | // Inherits all of inherit_from's string heaps as read-only |
1145 | | // string heaps |
1146 | | // |
1147 | | void |
1148 | | HdrHeap::inherit_string_heaps(const HdrHeap *inherit_from) |
1149 | 2.12k | { |
1150 | | // if heaps are the same, this is a no-op |
1151 | 2.12k | if (inherit_from == (const HdrHeap *)this) { |
1152 | 0 | return; |
1153 | 0 | } |
1154 | | |
1155 | 2.12k | int first_free = HDR_BUF_RONLY_HEAPS; // default is out of array bounds |
1156 | 2.12k | int free_slots = 0; |
1157 | 2.12k | int inherit_str_size = 0; |
1158 | 2.12k | ink_assert(m_writeable); |
1159 | | |
1160 | | // Find the number of free heap slots & the first open index |
1161 | 8.50k | for (unsigned index = 0; index < HDR_BUF_RONLY_HEAPS; ++index) { |
1162 | 6.37k | if (m_ronly_heap[index].m_heap_start == nullptr) { |
1163 | 6.37k | if (first_free == HDR_BUF_RONLY_HEAPS) { |
1164 | 2.12k | first_free = index; |
1165 | 2.12k | } |
1166 | 6.37k | free_slots++; |
1167 | 6.37k | } |
1168 | 6.37k | } |
1169 | | |
1170 | | // Find out if we have enough slots |
1171 | 2.12k | if (inherit_from->m_read_write_heap) { |
1172 | 2.12k | free_slots--; |
1173 | 2.12k | inherit_str_size = inherit_from->m_read_write_heap->total_size(); |
1174 | 2.12k | } |
1175 | 2.70k | for (const auto &index : inherit_from->m_ronly_heap) { |
1176 | 2.70k | if (index.m_heap_start != nullptr) { |
1177 | 632 | free_slots--; |
1178 | 632 | inherit_str_size += index.m_heap_len; |
1179 | 2.07k | } else { |
1180 | | // Heaps are allocated from the front of the array, so if |
1181 | | // we hit a NULL, we know we can stop |
1182 | 2.07k | break; |
1183 | 2.07k | } |
1184 | 2.70k | } |
1185 | | |
1186 | | // Find out if we are building up too much lost space |
1187 | 2.12k | int new_lost_space = m_lost_string_space + inherit_from->m_lost_string_space; |
1188 | | |
1189 | 2.12k | if (free_slots < 0 || new_lost_space > static_cast<int>(MAX_LOST_STR_SPACE)) { |
1190 | | // Not enough free slots. We need to force a coalesce of |
1191 | | // string heaps for both old heaps and the inherited from heaps. |
1192 | | // Coalesce can't know the inherited str size so we pass it |
1193 | | // it in so that it can allocate a new read-write string heap |
1194 | | // large enough (INKqa07513). |
1195 | | // INVARIANT: inherit_str_heaps can only be called after |
1196 | | // all the objects the callee wants to inherit strings for |
1197 | | // are put into the heap |
1198 | 97 | coalesce_str_heaps(inherit_str_size); |
1199 | 2.02k | } else { |
1200 | | // Copy over read/write string heap if it exists |
1201 | 2.02k | if (inherit_from->m_read_write_heap) { |
1202 | 2.02k | int str_size = |
1203 | 2.02k | inherit_from->m_read_write_heap->total_size() - sizeof(HdrStrHeap) - inherit_from->m_read_write_heap->space_avail(); |
1204 | 2.02k | ink_release_assert(attach_str_heap(reinterpret_cast<char *>(inherit_from->m_read_write_heap.get() + 1), str_size, |
1205 | 2.02k | inherit_from->m_read_write_heap.get(), &first_free)); |
1206 | 2.02k | } |
1207 | | // Copy over read only string heaps |
1208 | 6.08k | for (const auto &i : inherit_from->m_ronly_heap) { |
1209 | 6.08k | if (i.m_heap_start) { |
1210 | 443 | ink_release_assert(attach_str_heap(i.m_heap_start, i.m_heap_len, i.m_ref_count_ptr.get(), &first_free)); |
1211 | 443 | } |
1212 | 6.08k | } |
1213 | | |
1214 | 2.02k | m_lost_string_space += inherit_from->m_lost_string_space; |
1215 | 2.02k | } |
1216 | | |
1217 | 2.12k | return; |
1218 | 2.12k | } |
1219 | | |
1220 | | // void HdrHeap::dump_heap(int len) |
1221 | | // |
1222 | | // Debugging function to dump the heap in hex |
1223 | | void |
1224 | | HdrHeap::dump_heap(int len) |
1225 | 0 | { |
1226 | 0 | int count = 0; |
1227 | 0 | char *tmp = reinterpret_cast<char *>(this); |
1228 | 0 | char *end; |
1229 | 0 | uint32_t content; |
1230 | |
|
1231 | 0 | if (len < 0) { |
1232 | 0 | len = m_size; |
1233 | 0 | } |
1234 | 0 | end = (reinterpret_cast<char *>(this)) + len; |
1235 | |
|
1236 | 0 | fprintf(stderr, "---- Dumping header heap @ 0x%" PRIx64 " - len %d ------", static_cast<uint64_t>((ptrdiff_t)this), len); |
1237 | |
|
1238 | 0 | while (tmp < end) { |
1239 | 0 | if (count % 4 == 0) { |
1240 | 0 | fprintf(stderr, "\n0x%" PRIx64 ": ", static_cast<uint64_t>((ptrdiff_t)tmp)); |
1241 | 0 | } |
1242 | 0 | count++; |
1243 | | |
1244 | | // Load the content |
1245 | 0 | if (end - tmp > 4) { |
1246 | 0 | content = *(reinterpret_cast<uint32_t *>(tmp)); |
1247 | 0 | } else { |
1248 | | // Less than 4 bytes available so just |
1249 | | // grab the bytes we need |
1250 | 0 | content = 0; |
1251 | 0 | memcpy(&content, tmp, (end - tmp)); |
1252 | 0 | } |
1253 | |
|
1254 | 0 | fprintf(stderr, "0x%x ", content); |
1255 | 0 | tmp += 4; |
1256 | 0 | } |
1257 | |
|
1258 | 0 | fprintf(stderr, "\n-------------- End header heap dump -----------\n"); |
1259 | 0 | } |
1260 | | |
1261 | | uint64_t |
1262 | | HdrHeap::total_used_size() const |
1263 | 0 | { |
1264 | 0 | uint64_t size = 0; |
1265 | 0 | const HdrHeap *h = this; |
1266 | |
|
1267 | 0 | while (h) { |
1268 | 0 | size += (h->m_free_start - h->m_data_start); |
1269 | 0 | h = h->m_next; |
1270 | 0 | } |
1271 | |
|
1272 | 0 | return size; |
1273 | 0 | } |
1274 | | |
1275 | | // |
1276 | | // HdrStrHeap |
1277 | | // |
1278 | | |
1279 | | void |
1280 | | HdrStrHeap::free() |
1281 | 32.6k | { |
1282 | 32.6k | if (_total_size == HdrStrHeap::DEFAULT_SIZE) { |
1283 | 29.9k | THREAD_FREE(this, strHeapAllocator, this_thread()); |
1284 | 29.9k | } else { |
1285 | 2.65k | ats_free(this); |
1286 | 2.65k | } |
1287 | 32.6k | } |
1288 | | |
1289 | | // char* HdrStrHeap::allocate(int nbytes) |
1290 | | // |
1291 | | // Allocates nbytes from the str heap |
1292 | | // Return NULL on allocation failure |
1293 | | // |
1294 | | char * |
1295 | | HdrStrHeap::allocate(int nbytes) |
1296 | 897k | { |
1297 | 897k | if (_avail_size >= static_cast<unsigned>(nbytes)) { |
1298 | 895k | char *new_space = reinterpret_cast<char *>(this) + _total_size - _avail_size; |
1299 | 895k | _avail_size -= nbytes; |
1300 | 895k | return new_space; |
1301 | 895k | } else { |
1302 | 2.00k | return nullptr; |
1303 | 2.00k | } |
1304 | 897k | } |
1305 | | |
1306 | | // char* HdrStrHeap::expand(char* ptr, int old_size, int new_size) |
1307 | | // |
1308 | | // Try to expand str in the heap. If we succeed to |
1309 | | // we return ptr, otherwise we return NULL |
1310 | | // |
1311 | | char * |
1312 | | HdrStrHeap::expand(char *ptr, int old_size, int new_size) |
1313 | 6.24k | { |
1314 | 6.24k | unsigned int expand_size = new_size - old_size; |
1315 | | |
1316 | 6.24k | ink_assert(contains(ptr)); |
1317 | | |
1318 | 6.24k | char *free_start = reinterpret_cast<char *>(this) + _total_size - _avail_size; |
1319 | 6.24k | if (ptr + old_size == free_start && expand_size <= _avail_size) { |
1320 | 6.08k | _avail_size -= expand_size; |
1321 | 6.08k | return ptr; |
1322 | 6.08k | } else { |
1323 | 167 | return nullptr; |
1324 | 167 | } |
1325 | 6.24k | } |