OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
Ubsan.h
Go to the documentation of this file.
1
19#ifndef UBSAN_H
20#define UBSAN_H
21
22#include <Library/DebugLib.h>
23#include <Library/PrintLib.h>
24#include <Library/UefiBootServicesTableLib.h>
25#include <Library/UefiLib.h>
26
27// Pretend UEFI to mean kernel mode.
28#ifndef _KERNEL
29#define _KERNEL 1
30#endif
31
32#if !defined(HAVE_UBSAN_SUPPORT) && defined(__clang__)
33#define HAVE_UBSAN_SUPPORT 1
34#endif
35
36
37// Mark long double as supported (since we may use softfp).
38#ifndef __HAVE_LONG_DOUBLE
39#define __HAVE_LONG_DOUBLE
40#endif
41
42// Non-BSD environments do not support RCSID.
43#ifndef __RCSID
44#define __RCSID(x)
45#endif
46
47#ifndef __KERNEL_RCSID
48#define __KERNEL_RCSID(x, s) __RCSID(s)
49#endif
50
51// Implement builtin via macros, because some toolchains do include stdint.h.
52// This works only because Ubsan.c is not allowed to have any header inclusion.
53#define int8_t INT8
54#define int16_t INT16
55#define int32_t INT32
56#define int64_t INT64
57#define uint8_t UINT8
58#define uint16_t UINT16
59#define uint32_t UINT32
60#define uint64_t UINT64
61#define bool BOOLEAN
62#define intptr_t INTN
63#define uintptr_t UINTN
64
65// Try to provide a somewhat adequate size_t implementation.
66// Have to be careful about -Wformat warnings in Clang.
67#if defined(__GNUC__) || defined(__clang__)
68#if defined(MDE_CPU_X64) || defined(MDE_CPU_IA32)
69#include <stddef.h>
70
71#ifndef __SSIZE_TYPE__
72#define __SSIZE_TYPE__ \
73 __typeof__(_Generic((__SIZE_TYPE__)0, \
74 unsigned long long int : (long long int)0, \
75 unsigned long int : (long int)0, \
76 unsigned int : (int)0, \
77 unsigned short : (short)0, \
78 unsigned char : (signed char)0))
79#endif // __SSIZE_TYPE__
80
81typedef __SSIZE_TYPE__ ssize_t;
82#else
83#error Unknown CPU arch
84#endif
85#else
86#define ssize_t INTN
87#define size_t UINTN
88#endif
89
90// Provide boolean declarations if missing.
91#ifndef false
92#define false FALSE
93#define true TRUE
94#endif
95
96// Provide va_list declarations.
97#define va_list VA_LIST
98#define va_start VA_START
99#define va_end VA_END
100#define va_arg VA_ARG
101
102// Printing macros are not supported in EDK2.
103#ifndef PRIx8
104#define PRIx8 "hhx"
105#define PRIx16 "hx"
106#define PRIx32 "x"
107#define PRId32 "d"
108#define PRIu32 "u"
109#endif
110
111// Hack limits in.
112#ifndef UINT16_MAX
113#define UINT8_MAX 0xffU
114#define UINT16_MAX 0xffffU
115#define UINT32_MAX 0xffffffffU
116#define UINT64_MAX 0xffffffffffffffffULL
117#endif
118
119// Avoid ASSERT/KASSERT conflict from DebugLib.
120#ifdef ASSERT
121#undef ASSERT
122#endif
123
124// Implement KASSERT as the original EDK2 ASSERT.
125#if !defined(MDEPKG_NDEBUG)
126 #define KASSERT(Expression) \
127 do { \
128 if (DebugAssertEnabled ()) { \
129 if (!(Expression)) { \
130 _ASSERT (Expression); \
131 ANALYZER_UNREACHABLE (); \
132 } \
133 } \
134 } while (FALSE)
135#else
136 #define KASSERT(Expression)
137#endif
138
139// PATH_MAX is an unoffical extension.
140#ifndef PATH_MAX
141#define PATH_MAX 1024
142#endif
143
144// CHAR_BIT may not be defined without limits.h.
145#ifndef CHAR_BIT
146#define CHAR_BIT 8
147#endif
148
149// Bit manipulation is not present
150#ifndef __BIT
151#define __BIT(__n) \
152 (((UINTN)(__n) >= CHAR_BIT * sizeof(UINTN)) ? 0 : \
153 ((UINTN)1 << (UINTN)((__n) & (CHAR_BIT * sizeof(UINTN) - 1))))
154#endif
155
156// Extended bit manipulation is also not present
157#ifndef __LOWEST_SET_BIT
158/* find least significant bit that is set */
159#define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
160#define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
161#define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
162#define __SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
163#endif
164
165// BSD-like bit manipulation is not present
166#ifndef CLR
167#define SET(t, f) ((t) |= (f))
168#define ISSET(t, f) ((t) & (f))
169#define CLR(t, f) ((t) &= ~(f))
170#endif
171
172// Note, that we do not use UEFI-like printf.
173#ifndef __printflike
174#if defined(__GNUC__) || defined(__clang__)
175#define __printflike(x, y) __attribute__((format(printf, (x), (y))))
176#else
177#define __printflike(x, y)
178#endif
179#endif
180
181// Route arraycount to ARRAY_SIZE
182#ifndef __arraycount
183#define __arraycount(a) ARRAY_SIZE (a)
184#endif
185
186// Support unreachable where possible
187#ifndef __unreachable
188#if defined(__GNUC__) || defined(__clang__)
189#define __unreachable() __builtin_unreachable()
190#else
191#define __unreachable()
192#endif
193#endif
194
195// C-style printing support.
196#define TINYPRINTF_DEFINE_TFP_SPRINTF 1
197typedef void (*putcf) (void *, char);
198void EFIAPI tfp_format(void *putp, putcf putf, const char *fmt, va_list va);
199int tfp_vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
200int EFIAPI tfp_snprintf(char *str, size_t size, const char *fmt, ...) __printflike(3, 4);
201int EFIAPI tfp_vsprintf(char *str, const char *fmt, va_list ap);
202int EFIAPI tfp_sprintf(char *str, const char *fmt, ...) __printflike(2, 3);
203
204// Redirect log printing to standard output.
205#define snprintf tfp_snprintf
206#define vprintf(f, v) \
207 do { CHAR8 Tmp__[1024]; tfp_vsnprintf (Tmp__, sizeof (Tmp__), f, v); AsciiPrint ("%a", Tmp__); } while (0)
208#define vpanic(f, v) \
209 do { vprintf (f, v); do { } while (1); } while (0)
210
211// Avoid implementing memcpy as a function to avoid LTO conflicts.
212#define memcpy(Dst, Src, Size) do { gBS->CopyMem(Dst, Src, Size); } while (0)
213
214// Forcing VOID for those as the return types actually differ.
215#define strlcpy(Dst, Src, Size) do { AsciiStrnCpyS (Dst, Size, Src, AsciiStrLen (Src)); } while (0)
216#define strlcat(Dst, Src, Size) do { AsciiStrnCatS (Dst, Size, Src, AsciiStrLen (Src)); } while (0)
217
218
219#endif // UBSAN_H
UINT32 size
int tfp_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
#define va_list
Definition Ubsan.h:97
int EFIAPI tfp_sprintf(char *str, const char *fmt,...) __printflike(2
#define __printflike(x, y)
Definition Ubsan.h:177
#define ssize_t
Definition Ubsan.h:86
int EFIAPI int EFIAPI tfp_vsprintf(char *str, const char *fmt, va_list ap)
void EFIAPI tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
void(* putcf)(void *, char)
Definition Ubsan.h:197
int EFIAPI tfp_snprintf(char *str, size_t size, const char *fmt,...) __printflike(3