Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2014 Google, Inc. |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 18 | #include <base/logging.h> |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Etan Cohen | 3e59b5b | 2015-03-31 17:15:53 -0700 | [diff] [blame] | 20 | #include <string.h> |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 21 | |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 22 | #include "osi/include/allocation_tracker.h" |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 23 | #include "osi/include/allocator.h" |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 24 | |
Zach Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 25 | static const allocator_id_t alloc_allocator_id = 42; |
| 26 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 27 | char* osi_strdup(const char* str) { |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame] | 28 | size_t size = strlen(str) + 1; // + 1 for the null terminator |
| 29 | size_t real_size = allocation_tracker_resize_for_canary(size); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 30 | void* ptr = malloc(real_size); |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 31 | CHECK(ptr); |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame] | 32 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 33 | char* new_string = static_cast<char*>( |
| 34 | allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size)); |
| 35 | if (!new_string) return NULL; |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame] | 36 | |
| 37 | memcpy(new_string, str, size); |
| 38 | return new_string; |
Zach Johnson | 0812fe3 | 2014-08-26 20:15:19 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 41 | char* osi_strndup(const char* str, size_t len) { |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 42 | size_t size = strlen(str); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 43 | if (len < size) size = len; |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 44 | |
| 45 | size_t real_size = allocation_tracker_resize_for_canary(size + 1); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 46 | void* ptr = malloc(real_size); |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 47 | CHECK(ptr); |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 48 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 49 | char* new_string = static_cast<char*>( |
| 50 | allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size + 1)); |
| 51 | if (!new_string) return NULL; |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 52 | |
| 53 | memcpy(new_string, str, size); |
| 54 | new_string[size] = '\0'; |
| 55 | return new_string; |
| 56 | } |
| 57 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 58 | void* osi_malloc(size_t size) { |
Zach Johnson | f947fdd | 2014-08-28 13:30:17 -0700 | [diff] [blame] | 59 | size_t real_size = allocation_tracker_resize_for_canary(size); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 60 | void* ptr = malloc(real_size); |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 61 | CHECK(ptr); |
Andre Eisenbach | c02acb7 | 2016-01-11 12:24:14 -0800 | [diff] [blame] | 62 | return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 65 | void* osi_calloc(size_t size) { |
Zach Johnson | f947fdd | 2014-08-28 13:30:17 -0700 | [diff] [blame] | 66 | size_t real_size = allocation_tracker_resize_for_canary(size); |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 67 | void* ptr = calloc(1, real_size); |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 68 | CHECK(ptr); |
Andre Eisenbach | c02acb7 | 2016-01-11 12:24:14 -0800 | [diff] [blame] | 69 | return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 72 | void osi_free(void* ptr) { |
Zach Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 73 | free(allocation_tracker_notify_free(alloc_allocator_id, ptr)); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 74 | } |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 75 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 76 | void osi_free_and_reset(void** p_ptr) { |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 77 | CHECK(p_ptr != NULL); |
Pavlin Radoslavov | 20524d3 | 2016-02-02 18:12:08 -0800 | [diff] [blame] | 78 | osi_free(*p_ptr); |
| 79 | *p_ptr = NULL; |
| 80 | } |
| 81 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 82 | const allocator_t allocator_calloc = {osi_calloc, osi_free}; |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 83 | |
Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 84 | const allocator_t allocator_malloc = {osi_malloc, osi_free}; |