summaryrefslogtreecommitdiff
path: root/usr/src/test/util-tests/tests/demangle/rust.c
blob: 0b13c9db7ed5613a2b0e690a267d2c1fd1674887 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright (c) 2014 Alex Crichton
 *
 * Permission is hereby granted, free of charge, to any
 * person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the
 * Software without restriction, including without
 * limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software
 * is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice
 * shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
 * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
/*
 * Copyright 2019, Joyent, Inc.
 */

/*
 * Test cases taken from rustc-demangle 0.1.9
 */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <demangle-sys.h>

typedef struct rust_test_case {
	const char *mangled;
	const char *demangled;
} rust_test_case_t;
#define	T(_m, _d) { .mangled = _m, .demangled = _d }
#define	T_ERR(_m) { .mangled = _m }

typedef struct rust_test_grp {
	const char		*name;
	rust_test_case_t	tests[];
} rust_test_grp_t;
#define	GROUP(_n, ...)			\
	static rust_test_grp_t _n = {	\
		.name = #_n,		\
		.tests = {		\
			__VA_ARGS__,	\
			{ NULL, NULL }	\
		}			\
	}

GROUP(demangle,
    T_ERR("test"),
    T("_ZN4testE", "test"),
    T_ERR("_ZN4test"),
    T("_ZN4test1a2bcE", "test::a::bc"));

GROUP(demangle_dollars,
    T("_ZN4$RP$E", ")"),
    T("_ZN8$RF$testE", "&test"),
    T("_ZN8$BP$test4foobE", "*test::foob"),
    T("_ZN9$u20$test4foobE", " test::foob"),
    T("_ZN35Bar$LT$$u5b$u32$u3b$$u20$4$u5d$$GT$E", "Bar<[u32; 4]>"));

GROUP(demangle_many_dollars,
    T("_ZN13test$u20$test4foobE", "test test::foob"),
    T("_ZN12test$BP$test4foobE", "test*test::foob"));

/* BEGIN CSTYLED */
GROUP(demangle_osx,
    T("__ZN5alloc9allocator6Layout9for_value17h02a996811f781011E",
    "alloc::allocator::Layout::for_value::h02a996811f781011"),
    T("__ZN38_$LT$core..option..Option$LT$T$GT$$GT$6unwrap18_MSG_FILE_LINE_COL17haf7cb8d5824ee659E",
    "<core::option::Option<T>>::unwrap::_MSG_FILE_LINE_COL::haf7cb8d5824ee659"),
    T("__ZN4core5slice89_$LT$impl$u20$core..iter..traits..IntoIterator$u20$for$u20$$RF$$u27$a$u20$$u5b$T$u5d$$GT$9into_iter17h450e234d27262170E",
    "core::slice::<impl core::iter::traits::IntoIterator for &'a [T]>::into_iter::h450e234d27262170"));
/* END CSTYLED */

GROUP(demangle_elements_beginning_with_underscore,
    T("_ZN13_$LT$test$GT$E", "<test>"),
    T("_ZN28_$u7b$$u7b$closure$u7d$$u7d$E", "{{closure}}"),
    T("_ZN15__STATIC_FMTSTRE", "__STATIC_FMTSTR"));

/* BEGIN CSTYLED */
GROUP(demangle_trait_impls,
    T("_ZN71_$LT$Test$u20$$u2b$$u20$$u27$static$u20$as$u20$foo..Bar$LT$Test$GT$$GT$3barE",
    "<Test + 'static as foo::Bar<Test>>::bar"));
/* END CSTYLED */

GROUP(invalid_no_chop, T_ERR("_ZNfooE"));

/* BEGIN CSTYLED */
GROUP(handle_assoc_types,
    T("_ZN151_$LT$alloc..boxed..Box$LT$alloc..boxed..FnBox$LT$A$C$$u20$Output$u3d$R$GT$$u20$$u2b$$u20$$u27$a$GT$$u20$as$u20$core..ops..function..FnOnce$LT$A$GT$$GT$9call_once17h69e8f44b3723e1caE",
    "<alloc::boxed::Box<alloc::boxed::FnBox<A, Output=R> + 'a> as core::ops::function::FnOnce<A>>::call_once::h69e8f44b3723e1ca"));
/* END CSTYLED */

static rust_test_grp_t *rust_tests[] = {
	&demangle,
	&demangle_dollars,
	&demangle_many_dollars,
	&demangle_osx,
	&demangle_elements_beginning_with_underscore,
	&demangle_trait_impls,
	&invalid_no_chop,
	&handle_assoc_types
};

static const size_t n_rust_tests = ARRAY_SIZE(rust_tests);

static boolean_t
check_failure(size_t i, rust_test_case_t *tc, const char *dem, boolean_t res)
{
	int savederr = errno;

	if (dem == NULL && savederr == EINVAL)
		return (B_TRUE);

	if (res)
		(void) printf("FAILURE\n");

	if (dem != NULL) {
		(void) printf("  [%zu] Successfully demanged an invalid "
		    "name\n", i);
		(void) printf("         Name: '%s'\n", tc->mangled);
		(void) printf("    Demangled: '%s'\n", dem);
		return (B_FALSE);
	}

	(void) printf("  [%zu] demangle() returned an unexpected error\n", i);
	(void) printf("    Errno: %d\n", savederr);
	return (B_FALSE);
}

static boolean_t
check_success(size_t i, rust_test_case_t *tc, const char *dem, boolean_t res)
{
	if (dem != NULL && strcmp(tc->demangled, dem) == 0)
		return (B_TRUE);

	if (res)
		(void) printf("FAILURE\n");

	if (dem == NULL) {
		(void) printf("  [%zu] Failed to demangle '%s'\n", i,
		    tc->mangled);
		return (B_FALSE);
	}

	(void) printf("  [%zu] Demangled results do not match.\n", i);
	(void) printf("       Mangled: %s\n", tc->mangled);
	(void) printf("      Expected: %s\n", tc->demangled);
	(void) printf("        Actual: %s\n", dem);
	return (B_FALSE);
}

static boolean_t
run_test(rust_test_grp_t *test)
{
	boolean_t res = B_TRUE;

	(void) printf("Test %s: ", test->name);

	for (size_t i = 0; test->tests[i].mangled != NULL; i++) {
		char *dem;

		dem = sysdemangle(test->tests[i].mangled, SYSDEM_LANG_RUST,
		    NULL);
		if (test->tests[i].demangled == NULL)
			res &= check_failure(i, &test->tests[i], dem, res);
		else
			res &= check_success(i, &test->tests[i], dem, res);

		free(dem);
	}

	if (res)
		(void) printf("SUCCESS\n");

	return (res);
}

int
main(int argc, char **argv)
{
	boolean_t ok = B_TRUE;

	for (size_t i = 0; i < n_rust_tests; i++)
		ok &= run_test(rust_tests[i]);

	return (ok ? 0 : 1);
}

const char *
_umem_debug_init(void)
{
	return ("default,verbose");
}

const char *
_umem_logging_init(void)
{
	return ("fail,contents");
}