summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdemangle/common/demangle_int.h
blob: d4c227a87f367b79aeaa81926376e7caffbf2a8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Jason King
 * Copyright 2019 Joyent, Inc.
 */
#ifndef _DEMANGLE_INT_H
#define	_DEMANGLE_INT_H

#include <inttypes.h>
#include <stdio.h>
#include <sys/byteorder.h>
#include <sys/ctype.h> /* Use ASCII ISXXXX() macros */
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/isa_defs.h>
#include "demangle-sys.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __CHECKER__
/*
 * smatch seems to have a bug which chokes on the builtins, so
 * we just have it fallback to the non-builtin definitions
 */
#elif __GNUC__ >= 5 && __GNUC_MINOR__ > 1
#define	USE_BUILTIN_OVERFLOW
#elif defined(__clang__)
#define	USE_BUILTIN_OVERFLOW
#endif

#ifdef USE_BUILTIN_OVERFLOW
static inline boolean_t
mul_overflow(uint64_t a, uint64_t b, uint64_t *v)
{
	return (__builtin_mul_overflow(a, b, v));
}

static inline boolean_t
add_overflow(uint64_t a, uint64_t b, uint64_t *v)
{
	return (__builtin_add_overflow(a, b, v));
}

static inline boolean_t
sub_overflow(uint64_t a, uint64_t b, uint64_t *v)
{
	return (__builtin_sub_overflow(a, b, v));
}
#else
static inline boolean_t
mul_overflow(uint64_t a, uint64_t b, uint64_t *v)
{
	uint64_t val = a * b;

	if (a != 0 && val / a != b)
		return (B_TRUE);
	*v = val;
	return (B_FALSE);
}

static inline boolean_t
add_overflow(uint64_t a, uint64_t b, uint64_t *v)
{
	uint64_t val = a + b;

	if (val < a || val < b)
		return (B_TRUE);
	*v = val;
	return (B_FALSE);
}

static inline boolean_t
sub_overflow(uint64_t a, uint64_t b, uint64_t *v)
{
	uint64_t val = a - b;

	if (val > a)
		return (B_TRUE);
	*v = val;
	return (B_FALSE);
}
#endif

extern sysdem_ops_t *sysdem_ops_default;

char *cpp_demangle(const char *, size_t, sysdem_ops_t *);
char *rust_demangle(const char *, size_t, sysdem_ops_t *);

struct custr_alloc;

void *zalloc(sysdem_ops_t *, size_t);
void *xcalloc(sysdem_ops_t *, size_t, size_t);
void *xrealloc(sysdem_ops_t *, void *, size_t, size_t);
void xfree(sysdem_ops_t *, void *, size_t);
char *xstrdup(sysdem_ops_t *, const char *);

extern volatile boolean_t demangle_debug;

/*
 * gcc seems to get unhappy with the ASSERT() style definition (also borrowed
 * for the DEMDEBUG macro unless demdebug() is returns a non-void value
 * (despite the return value never being used).
 */
int demdebug(const char *, ...);

#define	DEMDEBUG(s, ...) \
	((void)(demangle_debug && demdebug(s, ## __VA_ARGS__)))

#ifdef __cplusplus
}
#endif

#endif /* _DEMANGLE_INT_H */