1 /*
2  * Copyright (c) 2019 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 /*
25  * For reference, see LLVM's ubsan_value.h
26  * These structures and functions are used to access LLVM encoded UBSan
27  * values.
28  */
29 #pragma once
30 
31 #include <stdbool.h>
32 #include <stddef.h>
33 #include <stdint.h>
34 
35 struct source_location {
36     const char* filename;
37     uint32_t line;
38     uint32_t column;
39 };
40 
location_is_valid(struct source_location loc)41 static inline bool location_is_valid(struct source_location loc) {
42     return !!loc.filename;
43 }
44 
45 struct type_descriptor {
46     uint16_t kind;
47     uint16_t info;
48     char name[];
49 };
50 
51 enum type_kind {
52     TYPE_KIND_INTEGER = 0x0,
53     TYPE_KIND_FLOAT = 0x1,
54     TYPE_KIND_UNKNOWN = 0xFFFF,
55 };
56 
57 typedef uintptr_t value_handle_t;
58 //#define value_handle_t uintptr_t
59 
60 struct value_t {
61     const struct type_descriptor* type;
62     value_handle_t val;
63 };
64 
int_typeinfo_is_signed(uint16_t info)65 static inline bool int_typeinfo_is_signed(uint16_t info) {
66     return info & 1;
67 }
68 
int_typeinfo_width_bits(uint16_t info)69 static inline size_t int_typeinfo_width_bits(uint16_t info) {
70     size_t shift = info >> 1;
71     return 1U << shift;
72 }
73 
float_typeinfo_width_bits(uint16_t info)74 static inline size_t float_typeinfo_width_bits(uint16_t info) {
75     return info;
76 }
77 
type_is_integer(const struct type_descriptor * type)78 static inline bool type_is_integer(const struct type_descriptor* type) {
79     return (enum type_kind)type->kind == TYPE_KIND_INTEGER;
80 }
81 
type_is_float(const struct type_descriptor * type)82 static inline bool type_is_float(const struct type_descriptor* type) {
83     return (enum type_kind)type->kind == TYPE_KIND_FLOAT;
84 }
85 
type_width_bits(const struct type_descriptor * type)86 static inline size_t type_width_bits(const struct type_descriptor* type) {
87     if (type_is_integer(type)) {
88         return int_typeinfo_width_bits(type->info);
89     } else {
90         return float_typeinfo_width_bits(type->info);
91     }
92 }
93 
type_is_inline(const struct type_descriptor * type)94 static inline bool type_is_inline(const struct type_descriptor* type) {
95     return type_width_bits(type) <= sizeof(value_handle_t) * 8;
96 }
97 
type_is_signed_integer(const struct type_descriptor * type)98 static inline bool type_is_signed_integer(const struct type_descriptor* type) {
99     return type_is_integer(type) && int_typeinfo_is_signed(type->info);
100 }
101 
type_is_unsigned_integer(const struct type_descriptor * type)102 static inline bool type_is_unsigned_integer(
103         const struct type_descriptor* type) {
104     return type_is_integer(type) && !int_typeinfo_is_signed(type->info);
105 }
106