/
test_tls.c
125 lines (104 loc) · 3.17 KB
/
test_tls.c
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
/*
* Copyright (c) 2015-2019 Contributors as noted in the AUTHORS file
*
* This file is part of Solo5, a sandboxed execution environment.
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice appear
* in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "solo5.h"
#include "../../bindings/lib.c"
/*
* XXX TODO: This test contains a bad, incomplete implementation of TLS.
* It's unclear what we should be testing here, and what "TLS support" in
* the context of Solo5 means at all.
*/
/*
* FreeBSD clang toolchain post 8.0.1/12.1-RELEASE fails to build this test
* with:
*
* ld: error: test_tls.o has an STT_TLS symbol but doesn't have an SHF_TLS section
*
* OpenBSD clang toolchains have unspecified non-support for TLS.
*
* In both cases just compile a dummy and disable this test in tests.bats.
*/
/* XXX The above actually applies to any toolchain using lld. Either disable
* this test entirely or fix the linker scripts? */
#if defined(__clang__)
int solo5_app_main(const struct solo5_start_info *si __attribute__((unused)))
{
return SOLO5_EXIT_FAILURE;
}
#else
#if defined(__x86_64__) || defined(__powerpc64__)
/* Variant II */
struct tcb {
volatile uint64_t _data;
void *tp;
};
#define PPC64_TLS_OFFSET 0x7000
#elif defined(__aarch64__)
/* Variant I */
struct tcb {
void *tp;
void *pad;
volatile uint64_t _data;
};
#else
#error Unsupported architecture
#endif
struct tcb tcb1;
struct tcb tcb2;
static void puts(const char *s)
{
solo5_console_write(s, strlen(s));
}
__thread volatile uint64_t _data;
uint64_t __attribute__ ((noinline)) get_data()
{
return _data;
}
void __attribute__ ((noinline)) set_data(uint64_t data)
{
_data = data;
}
int solo5_app_main(const struct solo5_start_info *si __attribute__((unused)))
{
puts("\n**** Solo5 standalone test_tls ****\n\n");
#if defined (__powerpc64__)
tcb1.tp = (void *) (PPC64_TLS_OFFSET + (uintptr_t)&tcb1._data);
tcb2.tp = (void *) (PPC64_TLS_OFFSET + (uintptr_t)&tcb2._data);
#else
tcb1.tp = &tcb1.tp;
tcb2.tp = &tcb2.tp;
#endif
if (solo5_set_tls_base((uintptr_t)tcb1.tp) != SOLO5_R_OK)
return 1;
set_data(1);
if (solo5_set_tls_base((uintptr_t)tcb2.tp) != SOLO5_R_OK)
return 2;
set_data(2);
if (solo5_set_tls_base((uintptr_t)tcb1.tp) != SOLO5_R_OK)
return 3;
if (get_data() != 1)
return 4;
if (solo5_set_tls_base((uintptr_t)tcb2.tp) != SOLO5_R_OK)
return 5;
if (get_data() != 2)
return 6;
puts("SUCCESS\n");
return SOLO5_EXIT_SUCCESS;
}
#endif /* defined(__clang__) */