1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <stdint.h>
30 
31 #include "CHECK.h"
32 
33 struct AlignedVar {
34   int field;
35   char buffer[0x1000 - sizeof(int)];
36 } __attribute__((aligned(0x400)));
37 
38 struct SmallVar {
39   int field;
40   char buffer[0xeee - sizeof(int)];
41 };
42 
43 // The single .tdata section should have a size that isn't a multiple of its
44 // alignment.
45 __thread struct AlignedVar var1 = {13};
46 __thread struct AlignedVar var2 = {17};
47 __thread struct SmallVar var3 = {19};
48 
var_addr(void * value)49 static uintptr_t var_addr(void* value) {
50   // Maybe the optimizer would assume that the variable has the alignment it is
51   // declared with.
52   asm volatile("" : "+r,m"(value) : : "memory");
53   return reinterpret_cast<uintptr_t>(value);
54 }
55 
main()56 int main() {
57   CHECK((var_addr(&var1) & 0x3ff) == 0);
58   CHECK((var_addr(&var2) & 0x3ff) == 0);
59   CHECK(var1.field == 13);
60   CHECK(var2.field == 17);
61   CHECK(var3.field == 19);
62   return 0;
63 }
64