• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 gRPC authors.
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  */
18 #include <grpc/support/port_platform.h>
19 
20 #include "src/core/lib/iomgr/error.h"
21 
22 #include <inttypes.h>
23 #include <string.h>
24 
25 #include <grpc/status.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29 
30 #ifdef GPR_WINDOWS
31 #include <grpc/support/log_windows.h>
32 #endif
33 
34 #include "src/core/lib/debug/trace.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/lib/iomgr/error_internal.h"
37 #include "src/core/lib/profiling/timers.h"
38 #include "src/core/lib/slice/slice_internal.h"
39 
40 grpc_core::DebugOnlyTraceFlag grpc_trace_error_refcount(false,
41                                                         "error_refcount");
42 grpc_core::DebugOnlyTraceFlag grpc_trace_closure(false, "closure");
43 
error_int_name(grpc_error_ints key)44 static const char* error_int_name(grpc_error_ints key) {
45   switch (key) {
46     case GRPC_ERROR_INT_ERRNO:
47       return "errno";
48     case GRPC_ERROR_INT_FILE_LINE:
49       return "file_line";
50     case GRPC_ERROR_INT_STREAM_ID:
51       return "stream_id";
52     case GRPC_ERROR_INT_GRPC_STATUS:
53       return "grpc_status";
54     case GRPC_ERROR_INT_OFFSET:
55       return "offset";
56     case GRPC_ERROR_INT_INDEX:
57       return "index";
58     case GRPC_ERROR_INT_SIZE:
59       return "size";
60     case GRPC_ERROR_INT_HTTP2_ERROR:
61       return "http2_error";
62     case GRPC_ERROR_INT_TSI_CODE:
63       return "tsi_code";
64     case GRPC_ERROR_INT_SECURITY_STATUS:
65       return "security_status";
66     case GRPC_ERROR_INT_FD:
67       return "fd";
68     case GRPC_ERROR_INT_WSA_ERROR:
69       return "wsa_error";
70     case GRPC_ERROR_INT_HTTP_STATUS:
71       return "http_status";
72     case GRPC_ERROR_INT_LIMIT:
73       return "limit";
74     case GRPC_ERROR_INT_OCCURRED_DURING_WRITE:
75       return "occurred_during_write";
76     case GRPC_ERROR_INT_CHANNEL_CONNECTIVITY_STATE:
77       return "channel_connectivity_state";
78     case GRPC_ERROR_INT_MAX:
79       GPR_UNREACHABLE_CODE(return "unknown");
80   }
81   GPR_UNREACHABLE_CODE(return "unknown");
82 }
83 
error_str_name(grpc_error_strs key)84 static const char* error_str_name(grpc_error_strs key) {
85   switch (key) {
86     case GRPC_ERROR_STR_KEY:
87       return "key";
88     case GRPC_ERROR_STR_VALUE:
89       return "value";
90     case GRPC_ERROR_STR_DESCRIPTION:
91       return "description";
92     case GRPC_ERROR_STR_OS_ERROR:
93       return "os_error";
94     case GRPC_ERROR_STR_TARGET_ADDRESS:
95       return "target_address";
96     case GRPC_ERROR_STR_SYSCALL:
97       return "syscall";
98     case GRPC_ERROR_STR_FILE:
99       return "file";
100     case GRPC_ERROR_STR_GRPC_MESSAGE:
101       return "grpc_message";
102     case GRPC_ERROR_STR_RAW_BYTES:
103       return "raw_bytes";
104     case GRPC_ERROR_STR_TSI_ERROR:
105       return "tsi_error";
106     case GRPC_ERROR_STR_FILENAME:
107       return "filename";
108     case GRPC_ERROR_STR_QUEUED_BUFFERS:
109       return "queued_buffers";
110     case GRPC_ERROR_STR_MAX:
111       GPR_UNREACHABLE_CODE(return "unknown");
112   }
113   GPR_UNREACHABLE_CODE(return "unknown");
114 }
115 
error_time_name(grpc_error_times key)116 static const char* error_time_name(grpc_error_times key) {
117   switch (key) {
118     case GRPC_ERROR_TIME_CREATED:
119       return "created";
120     case GRPC_ERROR_TIME_MAX:
121       GPR_UNREACHABLE_CODE(return "unknown");
122   }
123   GPR_UNREACHABLE_CODE(return "unknown");
124 }
125 
126 #ifndef NDEBUG
grpc_error_do_ref(grpc_error * err,const char * file,int line)127 grpc_error* grpc_error_do_ref(grpc_error* err, const char* file, int line) {
128   if (grpc_trace_error_refcount.enabled()) {
129     gpr_log(GPR_DEBUG, "%p: %" PRIdPTR " -> %" PRIdPTR " [%s:%d]", err,
130             gpr_atm_no_barrier_load(&err->atomics.refs.count),
131             gpr_atm_no_barrier_load(&err->atomics.refs.count) + 1, file, line);
132   }
133   gpr_ref(&err->atomics.refs);
134   return err;
135 }
136 #else
grpc_error_do_ref(grpc_error * err)137 grpc_error* grpc_error_do_ref(grpc_error* err) {
138   gpr_ref(&err->atomics.refs);
139   return err;
140 }
141 #endif
142 
unref_errs(grpc_error * err)143 static void unref_errs(grpc_error* err) {
144   uint8_t slot = err->first_err;
145   while (slot != UINT8_MAX) {
146     grpc_linked_error* lerr =
147         reinterpret_cast<grpc_linked_error*>(err->arena + slot);
148     GRPC_ERROR_UNREF(lerr->err);
149     GPR_ASSERT(err->last_err == slot ? lerr->next == UINT8_MAX
150                                      : lerr->next != UINT8_MAX);
151     slot = lerr->next;
152   }
153 }
154 
unref_strs(grpc_error * err)155 static void unref_strs(grpc_error* err) {
156   for (size_t which = 0; which < GRPC_ERROR_STR_MAX; ++which) {
157     uint8_t slot = err->strs[which];
158     if (slot != UINT8_MAX) {
159       grpc_slice_unref_internal(
160           *reinterpret_cast<grpc_slice*>(err->arena + slot));
161     }
162   }
163 }
164 
error_destroy(grpc_error * err)165 static void error_destroy(grpc_error* err) {
166   GPR_ASSERT(!grpc_error_is_special(err));
167   unref_errs(err);
168   unref_strs(err);
169   gpr_free(
170       reinterpret_cast<void*>(gpr_atm_acq_load(&err->atomics.error_string)));
171   gpr_free(err);
172 }
173 
174 #ifndef NDEBUG
grpc_error_do_unref(grpc_error * err,const char * file,int line)175 void grpc_error_do_unref(grpc_error* err, const char* file, int line) {
176   if (grpc_trace_error_refcount.enabled()) {
177     gpr_log(GPR_DEBUG, "%p: %" PRIdPTR " -> %" PRIdPTR " [%s:%d]", err,
178             gpr_atm_no_barrier_load(&err->atomics.refs.count),
179             gpr_atm_no_barrier_load(&err->atomics.refs.count) - 1, file, line);
180   }
181   if (gpr_unref(&err->atomics.refs)) {
182     error_destroy(err);
183   }
184 }
185 #else
grpc_error_do_unref(grpc_error * err)186 void grpc_error_do_unref(grpc_error* err) {
187   if (gpr_unref(&err->atomics.refs)) {
188     error_destroy(err);
189   }
190 }
191 #endif
192 
get_placement(grpc_error ** err,size_t size)193 static uint8_t get_placement(grpc_error** err, size_t size) {
194   GPR_ASSERT(*err);
195   uint8_t slots = static_cast<uint8_t>(size / sizeof(intptr_t));
196   if ((*err)->arena_size + slots > (*err)->arena_capacity) {
197     (*err)->arena_capacity = static_cast<uint8_t> GPR_MIN(
198         UINT8_MAX - 1, (3 * (*err)->arena_capacity / 2));
199     if ((*err)->arena_size + slots > (*err)->arena_capacity) {
200       return UINT8_MAX;
201     }
202 #ifndef NDEBUG
203     grpc_error* orig = *err;
204 #endif
205     *err = static_cast<grpc_error*>(gpr_realloc(
206         *err, sizeof(grpc_error) + (*err)->arena_capacity * sizeof(intptr_t)));
207 #ifndef NDEBUG
208     if (grpc_trace_error_refcount.enabled()) {
209       if (*err != orig) {
210         gpr_log(GPR_DEBUG, "realloc %p -> %p", orig, *err);
211       }
212     }
213 #endif
214   }
215   uint8_t placement = (*err)->arena_size;
216   (*err)->arena_size = static_cast<uint8_t>((*err)->arena_size + slots);
217   return placement;
218 }
219 
internal_set_int(grpc_error ** err,grpc_error_ints which,intptr_t value)220 static void internal_set_int(grpc_error** err, grpc_error_ints which,
221                              intptr_t value) {
222   uint8_t slot = (*err)->ints[which];
223   if (slot == UINT8_MAX) {
224     slot = get_placement(err, sizeof(value));
225     if (slot == UINT8_MAX) {
226       gpr_log(GPR_ERROR, "Error %p is full, dropping int {\"%s\":%" PRIiPTR "}",
227               *err, error_int_name(which), value);
228       return;
229     }
230   }
231   (*err)->ints[which] = slot;
232   (*err)->arena[slot] = value;
233 }
234 
internal_set_str(grpc_error ** err,grpc_error_strs which,const grpc_slice & value)235 static void internal_set_str(grpc_error** err, grpc_error_strs which,
236                              const grpc_slice& value) {
237   uint8_t slot = (*err)->strs[which];
238   if (slot == UINT8_MAX) {
239     slot = get_placement(err, sizeof(value));
240     if (slot == UINT8_MAX) {
241       char* str = grpc_slice_to_c_string(value);
242       gpr_log(GPR_ERROR, "Error %p is full, dropping string {\"%s\":\"%s\"}",
243               *err, error_str_name(which), str);
244       gpr_free(str);
245       return;
246     }
247   } else {
248     grpc_slice_unref_internal(
249         *reinterpret_cast<grpc_slice*>((*err)->arena + slot));
250   }
251   (*err)->strs[which] = slot;
252   memcpy((*err)->arena + slot, &value, sizeof(value));
253 }
254 
255 static char* fmt_time(gpr_timespec tm);
internal_set_time(grpc_error ** err,grpc_error_times which,gpr_timespec value)256 static void internal_set_time(grpc_error** err, grpc_error_times which,
257                               gpr_timespec value) {
258   uint8_t slot = (*err)->times[which];
259   if (slot == UINT8_MAX) {
260     slot = get_placement(err, sizeof(value));
261     if (slot == UINT8_MAX) {
262       char* time_str = fmt_time(value);
263       gpr_log(GPR_ERROR, "Error %p is full, dropping \"%s\":\"%s\"}", *err,
264               error_time_name(which), time_str);
265       gpr_free(time_str);
266       return;
267     }
268   }
269   (*err)->times[which] = slot;
270   memcpy((*err)->arena + slot, &value, sizeof(value));
271 }
272 
internal_add_error(grpc_error ** err,grpc_error * new_err)273 static void internal_add_error(grpc_error** err, grpc_error* new_err) {
274   grpc_linked_error new_last = {new_err, UINT8_MAX};
275   uint8_t slot = get_placement(err, sizeof(grpc_linked_error));
276   if (slot == UINT8_MAX) {
277     gpr_log(GPR_ERROR, "Error %p is full, dropping error %p = %s", *err,
278             new_err, grpc_error_string(new_err));
279     GRPC_ERROR_UNREF(new_err);
280     return;
281   }
282   if ((*err)->first_err == UINT8_MAX) {
283     GPR_ASSERT((*err)->last_err == UINT8_MAX);
284     (*err)->last_err = slot;
285     (*err)->first_err = slot;
286   } else {
287     GPR_ASSERT((*err)->last_err != UINT8_MAX);
288     grpc_linked_error* old_last =
289         reinterpret_cast<grpc_linked_error*>((*err)->arena + (*err)->last_err);
290     old_last->next = slot;
291     (*err)->last_err = slot;
292   }
293   memcpy((*err)->arena + slot, &new_last, sizeof(grpc_linked_error));
294 }
295 
296 #define SLOTS_PER_INT (1)  // == (sizeof(intptr_t) / sizeof(intptr_t))
297 #define SLOTS_PER_STR (sizeof(grpc_slice) / sizeof(intptr_t))
298 #define SLOTS_PER_TIME (sizeof(gpr_timespec) / sizeof(intptr_t))
299 #define SLOTS_PER_LINKED_ERROR (sizeof(grpc_linked_error) / sizeof(intptr_t))
300 
301 // size of storing one int and two slices and a timespec. For line, desc, file,
302 // and time created
303 #define DEFAULT_ERROR_CAPACITY \
304   (SLOTS_PER_INT + (SLOTS_PER_STR * 2) + SLOTS_PER_TIME)
305 
306 // It is very common to include and extra int and string in an error
307 #define SURPLUS_CAPACITY (2 * SLOTS_PER_INT + SLOTS_PER_TIME)
308 
309 static gpr_atm g_error_creation_allowed = true;
310 
grpc_disable_error_creation()311 void grpc_disable_error_creation() {
312   gpr_atm_no_barrier_store(&g_error_creation_allowed, false);
313 }
314 
grpc_enable_error_creation()315 void grpc_enable_error_creation() {
316   gpr_atm_no_barrier_store(&g_error_creation_allowed, true);
317 }
318 
grpc_error_create(const char * file,int line,const grpc_slice & desc,grpc_error ** referencing,size_t num_referencing)319 grpc_error* grpc_error_create(const char* file, int line,
320                               const grpc_slice& desc, grpc_error** referencing,
321                               size_t num_referencing) {
322   GPR_TIMER_SCOPE("grpc_error_create", 0);
323   uint8_t initial_arena_capacity = static_cast<uint8_t>(
324       DEFAULT_ERROR_CAPACITY +
325       static_cast<uint8_t>(num_referencing * SLOTS_PER_LINKED_ERROR) +
326       SURPLUS_CAPACITY);
327   grpc_error* err = static_cast<grpc_error*>(
328       gpr_malloc(sizeof(*err) + initial_arena_capacity * sizeof(intptr_t)));
329   if (err == nullptr) {  // TODO(ctiller): make gpr_malloc return NULL
330     return GRPC_ERROR_OOM;
331   }
332 #ifndef NDEBUG
333   if (!gpr_atm_no_barrier_load(&g_error_creation_allowed)) {
334     gpr_log(GPR_ERROR,
335             "Error creation occurred when error creation was disabled [%s:%d]",
336             file, line);
337     abort();
338   }
339   if (grpc_trace_error_refcount.enabled()) {
340     gpr_log(GPR_DEBUG, "%p create [%s:%d]", err, file, line);
341   }
342 #endif
343 
344   err->arena_size = 0;
345   err->arena_capacity = initial_arena_capacity;
346   err->first_err = UINT8_MAX;
347   err->last_err = UINT8_MAX;
348 
349   memset(err->ints, UINT8_MAX, GRPC_ERROR_INT_MAX);
350   memset(err->strs, UINT8_MAX, GRPC_ERROR_STR_MAX);
351   memset(err->times, UINT8_MAX, GRPC_ERROR_TIME_MAX);
352 
353   internal_set_int(&err, GRPC_ERROR_INT_FILE_LINE, line);
354   internal_set_str(&err, GRPC_ERROR_STR_FILE,
355                    grpc_slice_from_static_string(file));
356   internal_set_str(&err, GRPC_ERROR_STR_DESCRIPTION, desc);
357 
358   for (size_t i = 0; i < num_referencing; ++i) {
359     if (referencing[i] == GRPC_ERROR_NONE) continue;
360     internal_add_error(
361         &err,
362         GRPC_ERROR_REF(
363             referencing[i]));  // TODO(ncteisen), change ownership semantics
364   }
365 
366   internal_set_time(&err, GRPC_ERROR_TIME_CREATED, gpr_now(GPR_CLOCK_REALTIME));
367 
368   gpr_atm_no_barrier_store(&err->atomics.error_string, 0);
369   gpr_ref_init(&err->atomics.refs, 1);
370   return err;
371 }
372 
ref_strs(grpc_error * err)373 static void ref_strs(grpc_error* err) {
374   for (size_t i = 0; i < GRPC_ERROR_STR_MAX; ++i) {
375     uint8_t slot = err->strs[i];
376     if (slot != UINT8_MAX) {
377       grpc_slice_ref_internal(
378           *reinterpret_cast<grpc_slice*>(err->arena + slot));
379     }
380   }
381 }
382 
ref_errs(grpc_error * err)383 static void ref_errs(grpc_error* err) {
384   uint8_t slot = err->first_err;
385   while (slot != UINT8_MAX) {
386     grpc_linked_error* lerr =
387         reinterpret_cast<grpc_linked_error*>(err->arena + slot);
388     GRPC_ERROR_REF(lerr->err);
389     slot = lerr->next;
390   }
391 }
392 
copy_error_and_unref(grpc_error * in)393 static grpc_error* copy_error_and_unref(grpc_error* in) {
394   GPR_TIMER_SCOPE("copy_error_and_unref", 0);
395   grpc_error* out;
396   if (grpc_error_is_special(in)) {
397     out = GRPC_ERROR_CREATE_FROM_STATIC_STRING("unknown");
398     if (in == GRPC_ERROR_NONE) {
399       internal_set_str(&out, GRPC_ERROR_STR_DESCRIPTION,
400                        grpc_slice_from_static_string("no error"));
401       internal_set_int(&out, GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_OK);
402     } else if (in == GRPC_ERROR_OOM) {
403       internal_set_str(&out, GRPC_ERROR_STR_DESCRIPTION,
404                        grpc_slice_from_static_string("oom"));
405     } else if (in == GRPC_ERROR_CANCELLED) {
406       internal_set_str(&out, GRPC_ERROR_STR_DESCRIPTION,
407                        grpc_slice_from_static_string("cancelled"));
408       internal_set_int(&out, GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_CANCELLED);
409     }
410   } else if (gpr_ref_is_unique(&in->atomics.refs)) {
411     out = in;
412   } else {
413     uint8_t new_arena_capacity = in->arena_capacity;
414     // the returned err will be added to, so we ensure this is room to avoid
415     // unneeded allocations.
416     if (in->arena_capacity - in->arena_size <
417         static_cast<uint8_t> SLOTS_PER_STR) {
418       new_arena_capacity = static_cast<uint8_t>(3 * new_arena_capacity / 2);
419     }
420     out = static_cast<grpc_error*>(
421         gpr_malloc(sizeof(*in) + new_arena_capacity * sizeof(intptr_t)));
422 #ifndef NDEBUG
423     if (grpc_trace_error_refcount.enabled()) {
424       gpr_log(GPR_DEBUG, "%p create copying %p", out, in);
425     }
426 #endif
427     // bulk memcpy of the rest of the struct.
428     // NOLINTNEXTLINE(bugprone-sizeof-expression)
429     size_t skip = sizeof(&out->atomics);
430     memcpy(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(out) + skip),
431            reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(in) + skip),
432            sizeof(*in) + (in->arena_size * sizeof(intptr_t)) - skip);
433     // manually set the atomics and the new capacity
434     gpr_atm_no_barrier_store(&out->atomics.error_string, 0);
435     gpr_ref_init(&out->atomics.refs, 1);
436     out->arena_capacity = new_arena_capacity;
437     ref_strs(out);
438     ref_errs(out);
439     GRPC_ERROR_UNREF(in);
440   }
441   return out;
442 }
443 
grpc_error_set_int(grpc_error * src,grpc_error_ints which,intptr_t value)444 grpc_error* grpc_error_set_int(grpc_error* src, grpc_error_ints which,
445                                intptr_t value) {
446   GPR_TIMER_SCOPE("grpc_error_set_int", 0);
447   grpc_error* new_err = copy_error_and_unref(src);
448   internal_set_int(&new_err, which, value);
449   return new_err;
450 }
451 
452 struct special_error_status_map {
453   grpc_status_code code;
454   const char* msg;
455   size_t len;
456 };
457 const special_error_status_map error_status_map[] = {
458     {GRPC_STATUS_OK, "", 0},                // GRPC_ERROR_NONE
459     {GRPC_STATUS_INVALID_ARGUMENT, "", 0},  // GRPC_ERROR_RESERVED_1
460     {GRPC_STATUS_RESOURCE_EXHAUSTED, "Out of memory",
461      strlen("Out of memory")},              // GRPC_ERROR_OOM
462     {GRPC_STATUS_INVALID_ARGUMENT, "", 0},  // GRPC_ERROR_RESERVED_2
463     {GRPC_STATUS_CANCELLED, "Cancelled",
464      strlen("Cancelled")},  // GRPC_ERROR_CANCELLED
465 };
466 
grpc_error_get_int(grpc_error * err,grpc_error_ints which,intptr_t * p)467 bool grpc_error_get_int(grpc_error* err, grpc_error_ints which, intptr_t* p) {
468   GPR_TIMER_SCOPE("grpc_error_get_int", 0);
469   if (grpc_error_is_special(err)) {
470     if (which != GRPC_ERROR_INT_GRPC_STATUS) return false;
471     *p = error_status_map[reinterpret_cast<size_t>(err)].code;
472     return true;
473   }
474   uint8_t slot = err->ints[which];
475   if (slot != UINT8_MAX) {
476     if (p != nullptr) *p = err->arena[slot];
477     return true;
478   }
479   return false;
480 }
481 
grpc_error_set_str(grpc_error * src,grpc_error_strs which,const grpc_slice & str)482 grpc_error* grpc_error_set_str(grpc_error* src, grpc_error_strs which,
483                                const grpc_slice& str) {
484   GPR_TIMER_SCOPE("grpc_error_set_str", 0);
485   grpc_error* new_err = copy_error_and_unref(src);
486   internal_set_str(&new_err, which, str);
487   return new_err;
488 }
489 
grpc_error_get_str(grpc_error * err,grpc_error_strs which,grpc_slice * str)490 bool grpc_error_get_str(grpc_error* err, grpc_error_strs which,
491                         grpc_slice* str) {
492   if (grpc_error_is_special(err)) {
493     if (which != GRPC_ERROR_STR_GRPC_MESSAGE) return false;
494     const special_error_status_map& msg =
495         error_status_map[reinterpret_cast<size_t>(err)];
496     str->refcount = &grpc_core::kNoopRefcount;
497     str->data.refcounted.bytes =
498         reinterpret_cast<uint8_t*>(const_cast<char*>(msg.msg));
499     str->data.refcounted.length = msg.len;
500     return true;
501   }
502   uint8_t slot = err->strs[which];
503   if (slot != UINT8_MAX) {
504     *str = *reinterpret_cast<grpc_slice*>(err->arena + slot);
505     return true;
506   } else {
507     return false;
508   }
509 }
510 
grpc_error_add_child(grpc_error * src,grpc_error * child)511 grpc_error* grpc_error_add_child(grpc_error* src, grpc_error* child) {
512   GPR_TIMER_SCOPE("grpc_error_add_child", 0);
513   if (src != GRPC_ERROR_NONE) {
514     if (child == GRPC_ERROR_NONE) {
515       /* \a child is empty. Simply return the ref to \a src */
516       return src;
517     } else if (child != src) {
518       grpc_error* new_err = copy_error_and_unref(src);
519       internal_add_error(&new_err, child);
520       return new_err;
521     } else {
522       /* \a src and \a child are the same. Drop one of the references and return
523        * the other */
524       GRPC_ERROR_UNREF(child);
525       return src;
526     }
527   } else {
528     /* \a src is empty. Simply return the ref to \a child */
529     return child;
530   }
531 }
532 
533 static const char* no_error_string = "\"No Error\"";
534 static const char* oom_error_string = "\"Out of memory\"";
535 static const char* cancelled_error_string = "\"Cancelled\"";
536 
537 struct kv_pair {
538   char* key;
539   char* value;
540 };
541 struct kv_pairs {
542   kv_pair* kvs;
543   size_t num_kvs;
544   size_t cap_kvs;
545 };
append_chr(char c,char ** s,size_t * sz,size_t * cap)546 static void append_chr(char c, char** s, size_t* sz, size_t* cap) {
547   if (*sz == *cap) {
548     *cap = GPR_MAX(8, 3 * *cap / 2);
549     *s = static_cast<char*>(gpr_realloc(*s, *cap));
550   }
551   (*s)[(*sz)++] = c;
552 }
553 
append_str(const char * str,char ** s,size_t * sz,size_t * cap)554 static void append_str(const char* str, char** s, size_t* sz, size_t* cap) {
555   for (const char* c = str; *c; c++) {
556     append_chr(*c, s, sz, cap);
557   }
558 }
559 
append_esc_str(const uint8_t * str,size_t len,char ** s,size_t * sz,size_t * cap)560 static void append_esc_str(const uint8_t* str, size_t len, char** s, size_t* sz,
561                            size_t* cap) {
562   static const char* hex = "0123456789abcdef";
563   append_chr('"', s, sz, cap);
564   for (size_t i = 0; i < len; i++, str++) {
565     if (*str < 32 || *str >= 127) {
566       append_chr('\\', s, sz, cap);
567       switch (*str) {
568         case '\b':
569           append_chr('b', s, sz, cap);
570           break;
571         case '\f':
572           append_chr('f', s, sz, cap);
573           break;
574         case '\n':
575           append_chr('n', s, sz, cap);
576           break;
577         case '\r':
578           append_chr('r', s, sz, cap);
579           break;
580         case '\t':
581           append_chr('t', s, sz, cap);
582           break;
583         default:
584           append_chr('u', s, sz, cap);
585           append_chr('0', s, sz, cap);
586           append_chr('0', s, sz, cap);
587           append_chr(hex[*str >> 4], s, sz, cap);
588           append_chr(hex[*str & 0x0f], s, sz, cap);
589           break;
590       }
591     } else {
592       append_chr(static_cast<char>(*str), s, sz, cap);
593     }
594   }
595   append_chr('"', s, sz, cap);
596 }
597 
append_kv(kv_pairs * kvs,char * key,char * value)598 static void append_kv(kv_pairs* kvs, char* key, char* value) {
599   if (kvs->num_kvs == kvs->cap_kvs) {
600     kvs->cap_kvs = GPR_MAX(3 * kvs->cap_kvs / 2, 4);
601     kvs->kvs = static_cast<kv_pair*>(
602         gpr_realloc(kvs->kvs, sizeof(*kvs->kvs) * kvs->cap_kvs));
603   }
604   kvs->kvs[kvs->num_kvs].key = key;
605   kvs->kvs[kvs->num_kvs].value = value;
606   kvs->num_kvs++;
607 }
608 
key_int(grpc_error_ints which)609 static char* key_int(grpc_error_ints which) {
610   return gpr_strdup(error_int_name(which));
611 }
612 
fmt_int(intptr_t p)613 static char* fmt_int(intptr_t p) {
614   char* s;
615   gpr_asprintf(&s, "%" PRIdPTR, p);
616   return s;
617 }
618 
collect_ints_kvs(grpc_error * err,kv_pairs * kvs)619 static void collect_ints_kvs(grpc_error* err, kv_pairs* kvs) {
620   for (size_t which = 0; which < GRPC_ERROR_INT_MAX; ++which) {
621     uint8_t slot = err->ints[which];
622     if (slot != UINT8_MAX) {
623       append_kv(kvs, key_int(static_cast<grpc_error_ints>(which)),
624                 fmt_int(err->arena[slot]));
625     }
626   }
627 }
628 
key_str(grpc_error_strs which)629 static char* key_str(grpc_error_strs which) {
630   return gpr_strdup(error_str_name(which));
631 }
632 
fmt_str(const grpc_slice & slice)633 static char* fmt_str(const grpc_slice& slice) {
634   char* s = nullptr;
635   size_t sz = 0;
636   size_t cap = 0;
637   append_esc_str(GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice), &s, &sz,
638                  &cap);
639   append_chr(0, &s, &sz, &cap);
640   return s;
641 }
642 
collect_strs_kvs(grpc_error * err,kv_pairs * kvs)643 static void collect_strs_kvs(grpc_error* err, kv_pairs* kvs) {
644   for (size_t which = 0; which < GRPC_ERROR_STR_MAX; ++which) {
645     uint8_t slot = err->strs[which];
646     if (slot != UINT8_MAX) {
647       append_kv(kvs, key_str(static_cast<grpc_error_strs>(which)),
648                 fmt_str(*reinterpret_cast<grpc_slice*>(err->arena + slot)));
649     }
650   }
651 }
652 
key_time(grpc_error_times which)653 static char* key_time(grpc_error_times which) {
654   return gpr_strdup(error_time_name(which));
655 }
656 
fmt_time(gpr_timespec tm)657 static char* fmt_time(gpr_timespec tm) {
658   char* out;
659   const char* pfx = "!!";
660   switch (tm.clock_type) {
661     case GPR_CLOCK_MONOTONIC:
662       pfx = "@monotonic:";
663       break;
664     case GPR_CLOCK_REALTIME:
665       pfx = "@";
666       break;
667     case GPR_CLOCK_PRECISE:
668       pfx = "@precise:";
669       break;
670     case GPR_TIMESPAN:
671       pfx = "";
672       break;
673   }
674   gpr_asprintf(&out, "\"%s%" PRId64 ".%09d\"", pfx, tm.tv_sec, tm.tv_nsec);
675   return out;
676 }
677 
collect_times_kvs(grpc_error * err,kv_pairs * kvs)678 static void collect_times_kvs(grpc_error* err, kv_pairs* kvs) {
679   for (size_t which = 0; which < GRPC_ERROR_TIME_MAX; ++which) {
680     uint8_t slot = err->times[which];
681     if (slot != UINT8_MAX) {
682       append_kv(kvs, key_time(static_cast<grpc_error_times>(which)),
683                 fmt_time(*reinterpret_cast<gpr_timespec*>(err->arena + slot)));
684     }
685   }
686 }
687 
add_errs(grpc_error * err,char ** s,size_t * sz,size_t * cap)688 static void add_errs(grpc_error* err, char** s, size_t* sz, size_t* cap) {
689   uint8_t slot = err->first_err;
690   bool first = true;
691   while (slot != UINT8_MAX) {
692     grpc_linked_error* lerr =
693         reinterpret_cast<grpc_linked_error*>(err->arena + slot);
694     if (!first) append_chr(',', s, sz, cap);
695     first = false;
696     const char* e = grpc_error_string(lerr->err);
697     append_str(e, s, sz, cap);
698     GPR_ASSERT(err->last_err == slot ? lerr->next == UINT8_MAX
699                                      : lerr->next != UINT8_MAX);
700     slot = lerr->next;
701   }
702 }
703 
errs_string(grpc_error * err)704 static char* errs_string(grpc_error* err) {
705   char* s = nullptr;
706   size_t sz = 0;
707   size_t cap = 0;
708   append_chr('[', &s, &sz, &cap);
709   add_errs(err, &s, &sz, &cap);
710   append_chr(']', &s, &sz, &cap);
711   append_chr(0, &s, &sz, &cap);
712   return s;
713 }
714 
cmp_kvs(const void * a,const void * b)715 static int cmp_kvs(const void* a, const void* b) {
716   const kv_pair* ka = static_cast<const kv_pair*>(a);
717   const kv_pair* kb = static_cast<const kv_pair*>(b);
718   return strcmp(ka->key, kb->key);
719 }
720 
finish_kvs(kv_pairs * kvs)721 static char* finish_kvs(kv_pairs* kvs) {
722   char* s = nullptr;
723   size_t sz = 0;
724   size_t cap = 0;
725 
726   append_chr('{', &s, &sz, &cap);
727   for (size_t i = 0; i < kvs->num_kvs; i++) {
728     if (i != 0) append_chr(',', &s, &sz, &cap);
729     append_esc_str(reinterpret_cast<const uint8_t*>(kvs->kvs[i].key),
730                    strlen(kvs->kvs[i].key), &s, &sz, &cap);
731     gpr_free(kvs->kvs[i].key);
732     append_chr(':', &s, &sz, &cap);
733     append_str(kvs->kvs[i].value, &s, &sz, &cap);
734     gpr_free(kvs->kvs[i].value);
735   }
736   append_chr('}', &s, &sz, &cap);
737   append_chr(0, &s, &sz, &cap);
738 
739   gpr_free(kvs->kvs);
740   return s;
741 }
742 
grpc_error_string(grpc_error * err)743 const char* grpc_error_string(grpc_error* err) {
744   GPR_TIMER_SCOPE("grpc_error_string", 0);
745   if (err == GRPC_ERROR_NONE) return no_error_string;
746   if (err == GRPC_ERROR_OOM) return oom_error_string;
747   if (err == GRPC_ERROR_CANCELLED) return cancelled_error_string;
748 
749   void* p =
750       reinterpret_cast<void*>(gpr_atm_acq_load(&err->atomics.error_string));
751   if (p != nullptr) {
752     return static_cast<const char*>(p);
753   }
754 
755   kv_pairs kvs;
756   memset(&kvs, 0, sizeof(kvs));
757 
758   collect_ints_kvs(err, &kvs);
759   collect_strs_kvs(err, &kvs);
760   collect_times_kvs(err, &kvs);
761   if (err->first_err != UINT8_MAX) {
762     append_kv(&kvs, gpr_strdup("referenced_errors"), errs_string(err));
763   }
764 
765   qsort(kvs.kvs, kvs.num_kvs, sizeof(kv_pair), cmp_kvs);
766 
767   char* out = finish_kvs(&kvs);
768 
769   if (!gpr_atm_rel_cas(&err->atomics.error_string, 0,
770                        reinterpret_cast<gpr_atm>(out))) {
771     gpr_free(out);
772     out = reinterpret_cast<char*>(gpr_atm_acq_load(&err->atomics.error_string));
773   }
774 
775   return out;
776 }
777 
grpc_os_error(const char * file,int line,int err,const char * call_name)778 grpc_error* grpc_os_error(const char* file, int line, int err,
779                           const char* call_name) {
780   return grpc_error_set_str(
781       grpc_error_set_str(
782           grpc_error_set_int(
783               grpc_error_create(file, line,
784                                 grpc_slice_from_static_string(strerror(err)),
785                                 nullptr, 0),
786               GRPC_ERROR_INT_ERRNO, err),
787           GRPC_ERROR_STR_OS_ERROR,
788           grpc_slice_from_static_string(strerror(err))),
789       GRPC_ERROR_STR_SYSCALL, grpc_slice_from_copied_string(call_name));
790 }
791 
792 #ifdef GPR_WINDOWS
grpc_wsa_error(const char * file,int line,int err,const char * call_name)793 grpc_error* grpc_wsa_error(const char* file, int line, int err,
794                            const char* call_name) {
795   char* utf8_message = gpr_format_message(err);
796   grpc_error* error = grpc_error_set_str(
797       grpc_error_set_str(
798           grpc_error_set_int(
799               grpc_error_create(file, line,
800                                 grpc_slice_from_static_string("OS Error"), NULL,
801                                 0),
802               GRPC_ERROR_INT_WSA_ERROR, err),
803           GRPC_ERROR_STR_OS_ERROR, grpc_slice_from_copied_string(utf8_message)),
804       GRPC_ERROR_STR_SYSCALL, grpc_slice_from_static_string(call_name));
805   gpr_free(utf8_message);
806   return error;
807 }
808 #endif
809 
grpc_log_error(const char * what,grpc_error * error,const char * file,int line)810 bool grpc_log_error(const char* what, grpc_error* error, const char* file,
811                     int line) {
812   GPR_DEBUG_ASSERT(error != GRPC_ERROR_NONE);
813   const char* msg = grpc_error_string(error);
814   gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "%s: %s", what, msg);
815   GRPC_ERROR_UNREF(error);
816   return false;
817 }
818