1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/message/copy.h"
29 
30 #include "upb/mem/arena.h"
31 #include "upb/message/accessors.h"
32 #include "upb/message/message.h"
33 
34 // Must be last.
35 #include "upb/mini_table/common.h"
36 #include "upb/port/def.inc"
37 
upb_MessageField_IsMap(const upb_MiniTableField * field)38 static bool upb_MessageField_IsMap(const upb_MiniTableField* field) {
39   return upb_FieldMode_Get(field) == kUpb_FieldMode_Map;
40 }
41 
upb_Clone_StringView(upb_StringView str,upb_Arena * arena)42 static upb_StringView upb_Clone_StringView(upb_StringView str,
43                                            upb_Arena* arena) {
44   if (str.size == 0) {
45     return upb_StringView_FromDataAndSize(NULL, 0);
46   }
47   void* cloned_data = upb_Arena_Malloc(arena, str.size);
48   upb_StringView cloned_str =
49       upb_StringView_FromDataAndSize(cloned_data, str.size);
50   memcpy(cloned_data, str.data, str.size);
51   return cloned_str;
52 }
53 
upb_Clone_MessageValue(void * value,upb_CType value_type,const upb_MiniTable * sub,upb_Arena * arena)54 static bool upb_Clone_MessageValue(void* value, upb_CType value_type,
55                                    const upb_MiniTable* sub, upb_Arena* arena) {
56   switch (value_type) {
57     case kUpb_CType_Bool:
58     case kUpb_CType_Float:
59     case kUpb_CType_Int32:
60     case kUpb_CType_UInt32:
61     case kUpb_CType_Enum:
62     case kUpb_CType_Double:
63     case kUpb_CType_Int64:
64     case kUpb_CType_UInt64:
65       return true;
66     case kUpb_CType_String:
67     case kUpb_CType_Bytes: {
68       upb_StringView source = *(upb_StringView*)value;
69       int size = source.size;
70       void* cloned_data = upb_Arena_Malloc(arena, size);
71       if (cloned_data == NULL) {
72         return false;
73       }
74       *(upb_StringView*)value =
75           upb_StringView_FromDataAndSize(cloned_data, size);
76       memcpy(cloned_data, source.data, size);
77       return true;
78     } break;
79     case kUpb_CType_Message: {
80       UPB_ASSERT(sub);
81       const upb_Message* source = *(upb_Message**)value;
82       UPB_ASSERT(source);
83       upb_Message* clone = upb_Message_DeepClone(source, sub, arena);
84       *(upb_Message**)value = clone;
85       return clone != NULL;
86     } break;
87   }
88   UPB_UNREACHABLE();
89 }
90 
upb_Map_DeepClone(const upb_Map * map,upb_CType key_type,upb_CType value_type,const upb_MiniTable * map_entry_table,upb_Arena * arena)91 upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type,
92                            upb_CType value_type,
93                            const upb_MiniTable* map_entry_table,
94                            upb_Arena* arena) {
95   upb_Map* cloned_map = _upb_Map_New(arena, map->key_size, map->val_size);
96   if (cloned_map == NULL) {
97     return NULL;
98   }
99   upb_MessageValue key, val;
100   size_t iter = kUpb_Map_Begin;
101   while (upb_Map_Next(map, &key, &val, &iter)) {
102     const upb_MiniTableField* value_field = &map_entry_table->fields[1];
103     const upb_MiniTable* value_sub =
104         (value_field->UPB_PRIVATE(submsg_index) != kUpb_NoSub)
105             ? upb_MiniTable_GetSubMessageTable(map_entry_table, value_field)
106             : NULL;
107     upb_CType value_field_type = upb_MiniTableField_CType(value_field);
108     if (!upb_Clone_MessageValue(&val, value_field_type, value_sub, arena)) {
109       return NULL;
110     }
111     if (upb_Map_Insert(cloned_map, key, val, arena) ==
112         kUpb_MapInsertStatus_OutOfMemory) {
113       return NULL;
114     }
115   }
116   return cloned_map;
117 }
118 
upb_Message_Map_DeepClone(const upb_Map * map,const upb_MiniTable * mini_table,const upb_MiniTableField * field,upb_Message * clone,upb_Arena * arena)119 static upb_Map* upb_Message_Map_DeepClone(const upb_Map* map,
120                                           const upb_MiniTable* mini_table,
121                                           const upb_MiniTableField* field,
122                                           upb_Message* clone,
123                                           upb_Arena* arena) {
124   const upb_MiniTable* map_entry_table =
125       mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg;
126   UPB_ASSERT(map_entry_table);
127 
128   const upb_MiniTableField* key_field = &map_entry_table->fields[0];
129   const upb_MiniTableField* value_field = &map_entry_table->fields[1];
130 
131   upb_Map* cloned_map = upb_Map_DeepClone(
132       map, upb_MiniTableField_CType(key_field),
133       upb_MiniTableField_CType(value_field), map_entry_table, arena);
134   if (!cloned_map) {
135     return NULL;
136   }
137   _upb_Message_SetNonExtensionField(clone, field, &cloned_map);
138   return cloned_map;
139 }
140 
upb_Array_DeepClone(const upb_Array * array,upb_CType value_type,const upb_MiniTable * sub,upb_Arena * arena)141 upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type,
142                                const upb_MiniTable* sub, upb_Arena* arena) {
143   size_t size = array->size;
144   upb_Array* cloned_array =
145       _upb_Array_New(arena, size, _upb_Array_CTypeSizeLg2(value_type));
146   if (!cloned_array) {
147     return NULL;
148   }
149   if (!_upb_Array_ResizeUninitialized(cloned_array, size, arena)) {
150     return NULL;
151   }
152   for (size_t i = 0; i < size; ++i) {
153     upb_MessageValue val = upb_Array_Get(array, i);
154     if (!upb_Clone_MessageValue(&val, value_type, sub, arena)) {
155       return false;
156     }
157     upb_Array_Set(cloned_array, i, val);
158   }
159   return cloned_array;
160 }
161 
upb_Message_Array_DeepClone(const upb_Array * array,const upb_MiniTable * mini_table,const upb_MiniTableField * field,upb_Message * clone,upb_Arena * arena)162 static bool upb_Message_Array_DeepClone(const upb_Array* array,
163                                         const upb_MiniTable* mini_table,
164                                         const upb_MiniTableField* field,
165                                         upb_Message* clone, upb_Arena* arena) {
166   _upb_MiniTableField_CheckIsArray(field);
167   upb_Array* cloned_array = upb_Array_DeepClone(
168       array, upb_MiniTableField_CType(field),
169       upb_MiniTableField_CType(field) == kUpb_CType_Message &&
170               field->UPB_PRIVATE(submsg_index) != kUpb_NoSub
171           ? upb_MiniTable_GetSubMessageTable(mini_table, field)
172           : NULL,
173       arena);
174 
175   // Clear out upb_Array* due to parent memcpy.
176   _upb_Message_SetNonExtensionField(clone, field, &cloned_array);
177   return true;
178 }
179 
upb_Clone_ExtensionValue(const upb_MiniTableExtension * mini_table_ext,const upb_Message_Extension * source,upb_Message_Extension * dest,upb_Arena * arena)180 static bool upb_Clone_ExtensionValue(
181     const upb_MiniTableExtension* mini_table_ext,
182     const upb_Message_Extension* source, upb_Message_Extension* dest,
183     upb_Arena* arena) {
184   dest->data = source->data;
185   return upb_Clone_MessageValue(
186       &dest->data, upb_MiniTableField_CType(&mini_table_ext->field),
187       mini_table_ext->sub.submsg, arena);
188 }
189 
190 // Deep clones a message using the provided target arena.
191 //
192 // Returns NULL on failure.
upb_Message_DeepClone(const upb_Message * message,const upb_MiniTable * mini_table,upb_Arena * arena)193 upb_Message* upb_Message_DeepClone(const upb_Message* message,
194                                    const upb_MiniTable* mini_table,
195                                    upb_Arena* arena) {
196   upb_Message* clone = upb_Message_New(mini_table, arena);
197   upb_StringView empty_string = upb_StringView_FromDataAndSize(NULL, 0);
198   // Only copy message area skipping upb_Message_Internal.
199   memcpy(clone, message, mini_table->size);
200   for (size_t i = 0; i < mini_table->field_count; ++i) {
201     const upb_MiniTableField* field = &mini_table->fields[i];
202     if (!upb_IsRepeatedOrMap(field)) {
203       switch (upb_MiniTableField_CType(field)) {
204         case kUpb_CType_Message: {
205           const upb_Message* sub_message =
206               upb_Message_GetMessage(message, field, NULL);
207           if (sub_message != NULL) {
208             const upb_MiniTable* sub_message_table =
209                 upb_MiniTable_GetSubMessageTable(mini_table, field);
210             upb_Message* cloned_sub_message =
211                 upb_Message_DeepClone(sub_message, sub_message_table, arena);
212             if (cloned_sub_message == NULL) {
213               return NULL;
214             }
215             upb_Message_SetMessage(clone, mini_table, field,
216                                    cloned_sub_message);
217           }
218         } break;
219         case kUpb_CType_String:
220         case kUpb_CType_Bytes: {
221           upb_StringView str =
222               upb_Message_GetString(message, field, empty_string);
223           if (str.size != 0) {
224             if (!upb_Message_SetString(
225                     clone, field, upb_Clone_StringView(str, arena), arena)) {
226               return NULL;
227             }
228           }
229         } break;
230         default:
231           // Scalar, already copied.
232           break;
233       }
234     } else {
235       if (upb_MessageField_IsMap(field)) {
236         const upb_Map* map = upb_Message_GetMap(message, field);
237         if (map != NULL) {
238           if (!upb_Message_Map_DeepClone(map, mini_table, field, clone,
239                                          arena)) {
240             return NULL;
241           }
242         }
243       } else {
244         const upb_Array* array = upb_Message_GetArray(message, field);
245         if (array != NULL) {
246           if (!upb_Message_Array_DeepClone(array, mini_table, field, clone,
247                                            arena)) {
248             return NULL;
249           }
250         }
251       }
252     }
253   }
254   // Clone extensions.
255   size_t ext_count;
256   const upb_Message_Extension* ext = _upb_Message_Getexts(message, &ext_count);
257   for (size_t i = 0; i < ext_count; ++i) {
258     const upb_Message_Extension* msg_ext = &ext[i];
259     upb_Message_Extension* cloned_ext =
260         _upb_Message_GetOrCreateExtension(clone, msg_ext->ext, arena);
261     if (!cloned_ext) {
262       return NULL;
263     }
264     if (!upb_Clone_ExtensionValue(msg_ext->ext, msg_ext, cloned_ext, arena)) {
265       return NULL;
266     }
267   }
268 
269   // Clone unknowns.
270   size_t unknown_size = 0;
271   const char* ptr = upb_Message_GetUnknown(message, &unknown_size);
272   if (unknown_size != 0) {
273     UPB_ASSERT(ptr);
274     // Make a copy into destination arena.
275     void* cloned_unknowns = upb_Arena_Malloc(arena, unknown_size);
276     if (cloned_unknowns == NULL) {
277       return NULL;
278     }
279     memcpy(cloned_unknowns, ptr, unknown_size);
280     if (!_upb_Message_AddUnknown(clone, cloned_unknowns, unknown_size, arena)) {
281       return NULL;
282     }
283   }
284   return clone;
285 }
286