1 // Helper functions used only in tests
8 // These functions are used in the unit tests for C ABI calls.
11 rust_dbg_extern_identity_u32(uint32_t u) {
16 rust_dbg_extern_identity_u64(uint64_t u) {
21 rust_dbg_extern_identity_double(double u) {
26 rust_dbg_extern_identity_u8(char u) {
30 typedef void *(*dbg_callback)(void*);
33 rust_dbg_call(dbg_callback cb, void *data) {
37 void rust_dbg_do_nothing() { }
45 rust_dbg_extern_return_TwoU8s() {
53 rust_dbg_extern_identity_TwoU8s(struct TwoU8s u) {
63 rust_dbg_extern_return_TwoU16s() {
71 rust_dbg_extern_identity_TwoU16s(struct TwoU16s u) {
81 rust_dbg_extern_return_TwoU32s() {
89 rust_dbg_extern_identity_TwoU32s(struct TwoU32s u) {
99 rust_dbg_extern_return_TwoU64s() {
107 rust_dbg_extern_identity_TwoU64s(struct TwoU64s u) {
117 rust_dbg_extern_identity_TwoDoubles(struct TwoDoubles u) {
130 // MSVC doesn't allow empty structs or unions
136 rust_dbg_extern_empty_struct(struct ManyInts v1, struct Empty e, struct ManyInts v2) {
137 assert(v1.arg1 == v2.arg1 + 1);
138 assert(v1.arg2 == v2.arg2 + 1);
139 assert(v1.arg3 == v2.arg3 + 1);
140 assert(v1.arg4 == v2.arg4 + 1);
141 assert(v1.arg5 == v2.arg5 + 1);
142 assert(v1.arg6.one == v2.arg6.one + 1);
143 assert(v1.arg6.two == v2.arg6.two + 1);
148 rust_get_test_int() {
153 rust_get_null_ptr() {
157 // Debug helpers strictly to verify ABI conformance.
172 struct char_char_double {
178 struct char_char_float {
185 rust_dbg_abi_1(struct quad q) {
186 struct quad qq = { q.c + 1,
194 rust_dbg_abi_2(struct floats f) {
195 struct floats ff = { f.c + 1.0,
201 struct char_char_double
202 rust_dbg_abi_3(struct char_char_double a) {
203 struct char_char_double ccd = { a.a + 1,
209 struct char_char_float
210 rust_dbg_abi_4(struct char_char_float a) {
211 struct char_char_float ccd = { a.a + 1,
219 rust_dbg_static_mut = 3;
222 rust_dbg_static_mut_check_four() {
223 assert(rust_dbg_static_mut == 4);
232 uint64_t get_x(struct S s) {
236 uint64_t get_y(struct S s) {
240 uint64_t get_z(struct S s) {
244 uint64_t get_c_many_params(void *a, void *b, void *c, void *d, struct quad f) {
255 float get_c_exhaust_sysv64_ints(
262 // `f` used the last integer register, so `g` goes on the stack.
263 // It also used to bring the "count of available integer registers" down to
264 // `-1` which broke the next SSE-only aggregate argument (`h`) - see #62350.
271 // Calculates the average of `(x + y) / n` where x: i64, y: f64. There must be exactly n pairs
272 // passed as variadic arguments. There are two versions of this function: the
273 // variadic one, and the one that takes a `va_list`.
274 double rust_valist_interesting_average(uint64_t n, va_list pairs) {
277 for(i = 0; i < n; i += 1) {
278 sum += (double)va_arg(pairs, int64_t);
279 sum += va_arg(pairs, double);
284 double rust_interesting_average(uint64_t n, ...) {
288 sum = rust_valist_interesting_average(n, pairs);
293 int32_t rust_int8_to_int32(int8_t x) {
297 typedef union LARGE_INTEGER {
309 LARGE_INTEGER increment_all_parts(LARGE_INTEGER li) {
318 #if __SIZEOF_INT128__ == 16
320 unsigned __int128 identity(unsigned __int128 a) {
324 __int128 square(__int128 a) {
328 __int128 sub(__int128 a, __int128 b) {
334 #define OPTION_TAG_NONE (0)
335 #define OPTION_TAG_SOME (1)
337 struct U8TaggedEnumOptionU64 {
344 struct U8TaggedEnumOptionU64
345 rust_dbg_new_some_u64(uint64_t some) {
346 struct U8TaggedEnumOptionU64 r = {
347 .tag = OPTION_TAG_SOME,
353 struct U8TaggedEnumOptionU64
354 rust_dbg_new_none_u64(void) {
355 struct U8TaggedEnumOptionU64 r = {
356 .tag = OPTION_TAG_NONE,
362 rust_dbg_unpack_option_u64(struct U8TaggedEnumOptionU64 o, uint64_t *into) {
365 case OPTION_TAG_SOME:
368 case OPTION_TAG_NONE:
371 assert(0 && "unexpected tag");
376 struct U8TaggedEnumOptionU64U64 {
386 struct U8TaggedEnumOptionU64U64
387 rust_dbg_new_some_u64u64(uint64_t a, uint64_t b) {
388 struct U8TaggedEnumOptionU64U64 r = {
389 .tag = OPTION_TAG_SOME,
390 .some = { .a = a, .b = b },
395 struct U8TaggedEnumOptionU64U64
396 rust_dbg_new_none_u64u64(void) {
397 struct U8TaggedEnumOptionU64U64 r = {
398 .tag = OPTION_TAG_NONE,
404 rust_dbg_unpack_option_u64u64(struct U8TaggedEnumOptionU64U64 o, uint64_t *a, uint64_t *b) {
408 case OPTION_TAG_SOME:
412 case OPTION_TAG_NONE:
415 assert(0 && "unexpected tag");
420 uint16_t issue_97463_leak_uninit_data(uint32_t a, uint32_t b, uint32_t c) {
421 struct bloc { uint16_t a; uint16_t b; uint16_t c; };
422 struct bloc *data = malloc(sizeof(struct bloc));
424 data->a = a & 0xFFFF;
425 data->b = b & 0xFFFF;
426 data->c = c & 0xFFFF;
428 return data->b; /* leak data */