| 1 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 2 |
* Copyright by The HDF Group. * |
| 3 |
* All rights reserved. * |
| 4 |
* * |
| 5 |
* This file is part of HDF5. The full HDF5 copyright notice, including * |
| 6 |
* terms governing use, modification, and redistribution, is contained in * |
| 7 |
* the COPYING file, which can be found at the root of the source code * |
| 8 |
* distribution tree, or in https://www.hdfgroup.org/licenses. * |
| 9 |
* If you do not have access to either file, you may request a copy from * |
| 10 |
* [email protected]. * |
| 11 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 12 |
|
| 13 |
#if defined(__has_attribute) |
| 14 |
#if __has_attribute(no_sanitize) |
| 15 |
#define HDF_NO_UBSAN __attribute__((no_sanitize("undefined"))) |
| 16 |
#else |
| 17 |
#define HDF_NO_UBSAN |
| 18 |
#endif |
| 19 |
#else |
| 20 |
#define HDF_NO_UBSAN |
| 21 |
#endif |
| 22 |
|
| 23 |
#ifdef H5_LDOUBLE_TO_LONG_SPECIAL_TEST |
| 24 |
|
| 25 |
#include <string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
int HDF_NO_UBSAN main(void) |
| 29 |
{ |
| 30 |
long double ld = 20041683600089727.779961L; |
| 31 |
long ll; |
| 32 |
unsigned long ull; |
| 33 |
unsigned char s[16]; |
| 34 |
unsigned char s2[8]; |
| 35 |
int ret = 1; |
| 36 |
|
| 37 |
if(sizeof(long double) == 16 && sizeof(long) == 8) { |
| 38 |
/*make sure the long double type has 16 bytes in size and |
| 39 |
* 11 bits of exponent. If it is, |
| 40 |
*the bit sequence should be like below. It's not |
| 41 |
*a decent way to check but this info isn't available. */ |
| 42 |
memcpy(s, &ld, 16); |
| 43 |
if(s[0]==0x43 && s[1]==0x51 && s[2]==0xcc && s[3]==0xf3 && |
| 44 |
s[4]==0x85 && s[5]==0xeb && s[6]==0xc8 && s[7]==0xa0 && |
| 45 |
s[8]==0xbf && s[9]==0xcc && s[10]==0x2a && s[11]==0x3c) { |
| 46 |
|
| 47 |
/* Assign the hexadecimal value of long double type. */ |
| 48 |
s[0]=0x43; s[1]=0x51; s[2]=0xcc; s[3]=0xf3; |
| 49 |
s[4]=0x85; s[5]=0xeb; s[6]=0xc8; s[7]=0xa0; |
| 50 |
s[8]=0xbf; s[9]=0xcc; s[10]=0x2a; s[11]=0x3c; |
| 51 |
s[12]=0x3d; s[13]=0x85; s[14]=0x56; s[15]=0x20; |
| 52 |
|
| 53 |
memcpy(&ld, s, 16); |
| 54 |
|
| 55 |
ll = (long)ld; |
| 56 |
memcpy(s2, &ll, 8); |
| 57 |
|
| 58 |
/* The library's algorithm converts it to 0x 00 47 33 ce 17 af 22 82 |
| 59 |
* and gets wrong value 20041683600089730 on the IBM Power6 Linux. |
| 60 |
* But the IBM Power6 Linux converts it to 0x00 47 33 ce 17 af 22 7f |
| 61 |
* and gets the correct value 20041683600089727. It uses some special |
| 62 |
* algorithm. We're going to define the macro and skip the test until |
| 63 |
* we can figure out how they do it. */ |
| 64 |
if(s2[0]==0x00 && s2[1]==0x47 && s2[2]==0x33 && s2[3]==0xce && |
| 65 |
s2[4]==0x17 && s2[5]==0xaf && s2[6]==0x22 && s2[7]==0x7f) |
| 66 |
ret = 0; |
| 67 |
|
| 68 |
ull = (unsigned long)ld; |
| 69 |
memcpy(s2, &ull, 8); |
| 70 |
|
| 71 |
/* The unsigned long is the same as signed long. */ |
| 72 |
if(s2[0]==0x00 && s2[1]==0x47 && s2[2]==0x33 && s2[3]==0xce && |
| 73 |
s2[4]==0x17 && s2[5]==0xaf && s2[6]==0x22 && s2[7]==0x7f) |
| 74 |
ret = 0; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
done: |
| 79 |
exit(ret); |
| 80 |
} |
| 81 |
|
| 82 |
#endif |
| 83 |
|
| 84 |
#ifdef H5_LONG_TO_LDOUBLE_SPECIAL_TEST |
| 85 |
|
| 86 |
#include <string.h> |
| 87 |
#include <stdlib.h> |
| 88 |
|
| 89 |
int HDF_NO_UBSAN main(void) |
| 90 |
{ |
| 91 |
long double ld; |
| 92 |
long ll; |
| 93 |
unsigned long ull; |
| 94 |
unsigned char s[16]; |
| 95 |
int flag=0, ret=1; |
| 96 |
|
| 97 |
/*Determine if long double has 16 byte in size, 11 bit exponent, and |
| 98 |
*the bias is 0x3ff */ |
| 99 |
if(sizeof(long double) == 16) { |
| 100 |
ld = 1.0L; |
| 101 |
memcpy(s, &ld, 16); |
| 102 |
if(s[0]==0x3f && s[1]==0xf0 && s[2]==0x00 && s[3]==0x00 && |
| 103 |
s[4]==0x00 && s[5]==0x00 && s[6]==0x00 && s[7]==0x00) |
| 104 |
flag = 1; |
| 105 |
} |
| 106 |
|
| 107 |
if(flag==1 && sizeof(long)==8) { |
| 108 |
ll = 0x003fffffffffffffL; |
| 109 |
ld = (long double)ll; |
| 110 |
memcpy(s, &ld, 16); |
| 111 |
/* The library converts the value to 0x434fffffffffffff8000000000000000. |
| 112 |
* In decimal it is 18014398509481982.000000, one value short of the original. |
| 113 |
* The IBM Power6 Linux converts it to 0x4350000000000000bff0000000000000. |
| 114 |
* The value is correct in decimal. It uses some special |
| 115 |
* algorithm. We're going to define the macro and skip the test until |
| 116 |
* we can figure out how they do it. */ |
| 117 |
if(s[0]==0x43 && s[1]==0x50 && s[2]==0x00 && s[3]==0x00 && |
| 118 |
s[4]==0x00 && s[5]==0x00 && s[6]==0x00 && s[7]==0x00 && |
| 119 |
s[8]==0xbf && s[9]==0xf0 && s[10]==0x00 && s[11]==0x00 && |
| 120 |
s[12]==0x00 && s[13]==0x00 && s[14]==0x00 && s[15]==0x00) |
| 121 |
ret = 0; |
| 122 |
} |
| 123 |
if(flag==1 && sizeof(unsigned long)==8) { |
| 124 |
ull = 0xffffffffffffffffUL; |
| 125 |
ld = (long double)ull; |
| 126 |
memcpy(s, &ld, 16); |
| 127 |
/* Use a different value from signed long to test. The problem is the same |
| 128 |
* for both long and unsigned long. The value is 18446744073709551615. |
| 129 |
* The library converts the value to 0x43effffffffffffffe000000000000000. |
| 130 |
* In decimal it's 18446744073709548544.000000, very different from the original. |
| 131 |
* The IBM Power6 Linux converts it to 0x43f0000000000000bff0000000000000. |
| 132 |
* The value is correct in decimal. It uses some special |
| 133 |
* algorithm. We're going to define the macro and skip the test until |
| 134 |
* we can figure out how they do it. */ |
| 135 |
if(s[0]==0x43 && s[1]==0xf0 && s[2]==0x00 && s[3]==0x00 && |
| 136 |
s[4]==0x00 && s[5]==0x00 && s[6]==0x00 && s[7]==0x00 && |
| 137 |
s[8]==0xbf && s[9]==0xf0 && s[10]==0x00 && s[11]==0x00 && |
| 138 |
s[12]==0x00 && s[13]==0x00 && s[14]==0x00 && s[15]==0x00) |
| 139 |
ret = 0; |
| 140 |
} |
| 141 |
done: |
| 142 |
exit(ret); |
| 143 |
} |
| 144 |
|
| 145 |
#endif |
| 146 |
|
| 147 |
#ifdef H5_LDOUBLE_TO_LLONG_ACCURATE_TEST |
| 148 |
|
| 149 |
#include <string.h> |
| 150 |
#include <stdlib.h> |
| 151 |
|
| 152 |
int HDF_NO_UBSAN main(void) |
| 153 |
{ |
| 154 |
long double ld = 20041683600089727.779961L; |
| 155 |
long long ll; |
| 156 |
unsigned long long ull; |
| 157 |
unsigned char s[16]; |
| 158 |
int ret = 0; |
| 159 |
|
| 160 |
if(sizeof(long double) == 16) { |
| 161 |
/*make sure the long double type is the same as the failing type |
| 162 |
*which has 16 bytes in size and 11 bits of exponent. If it is, |
| 163 |
*the bit sequence should be like below. It's not |
| 164 |
*a decent way to check but this info isn't available. */ |
| 165 |
memcpy(s, &ld, 16); |
| 166 |
if(s[0]==0x43 && s[1]==0x51 && s[2]==0xcc && s[3]==0xf3 && |
| 167 |
s[4]==0x85 && s[5]==0xeb && s[6]==0xc8 && s[7]==0xa0 && |
| 168 |
s[8]==0xbf && s[9]==0xcc && s[10]==0x2a && s[11]==0x3c) { |
| 169 |
|
| 170 |
/*slightly adjust the bit sequence (s[8]=0xdf). The converted |
| 171 |
*values will go wild on Mac OS 10.4 and IRIX64 6.5.*/ |
| 172 |
s[0]=0x43; s[1]=0x51; s[2]=0xcc; s[3]=0xf3; |
| 173 |
s[4]=0x85; s[5]=0xeb; s[6]=0xc8; s[7]=0xa0; |
| 174 |
s[8]=0xdf; s[9]=0xcc; s[10]=0x2a; s[11]=0x3c; |
| 175 |
s[12]=0x3d; s[13]=0x85; s[14]=0x56; s[15]=0x20; |
| 176 |
|
| 177 |
memcpy(&ld, s, 16); |
| 178 |
ll = (long long)ld; |
| 179 |
ull = (unsigned long long)ld; |
| 180 |
|
| 181 |
if(ll != 20041683600089728 || ull != 20041683600089728) |
| 182 |
ret = 1; |
| 183 |
} |
| 184 |
} |
| 185 |
done: |
| 186 |
exit(ret); |
| 187 |
} |
| 188 |
#endif |
| 189 |
|
| 190 |
#ifdef H5_LLONG_TO_LDOUBLE_CORRECT_TEST |
| 191 |
|
| 192 |
#include <string.h> |
| 193 |
#include <stdlib.h> |
| 194 |
|
| 195 |
int HDF_NO_UBSAN main(void) |
| 196 |
{ |
| 197 |
long double ld; |
| 198 |
long long ll; |
| 199 |
unsigned long long ull; |
| 200 |
unsigned char s[16]; |
| 201 |
int flag=0, ret=0; |
| 202 |
|
| 203 |
/*Determine if long double has 16 byte in size, 11 bit exponent, and |
| 204 |
*the bias is 0x3ff */ |
| 205 |
if(sizeof(long double) == 16) { |
| 206 |
ld = 1.0L; |
| 207 |
memcpy(s, &ld, 16); |
| 208 |
if(s[0]==0x3f && s[1]==0xf0 && s[2]==0x00 && s[3]==0x00 && |
| 209 |
s[4]==0x00 && s[5]==0x00 && s[6]==0x00 && s[7]==0x00) |
| 210 |
flag = 1; |
| 211 |
} |
| 212 |
|
| 213 |
if(flag==1 && sizeof(long long)==8) { |
| 214 |
ll = 0x01ffffffffffffffLL; |
| 215 |
ld = (long double)ll; |
| 216 |
memcpy(s, &ld, 16); |
| 217 |
/*Check if the bit sequence is as supposed to be*/ |
| 218 |
if(s[0]!=0x43 || s[1]!=0x7f || s[2]!=0xff || s[3]!=0xff || |
| 219 |
s[4]!=0xff || s[5]!=0xff || s[6]!=0xff || s[7]!=0xff || |
| 220 |
s[8]!=0xf0 || s[9]!=0x00 || s[10]!=0x00 || s[11]!=0x00) |
| 221 |
ret = 1; |
| 222 |
} |
| 223 |
if(flag==1 && sizeof(unsigned long long)==8) { |
| 224 |
ull = 0x01ffffffffffffffULL; |
| 225 |
ld = (long double)ull; |
| 226 |
memcpy(s, &ld, 16); |
| 227 |
if(s[0]!=0x43 || s[1]!=0x7f || s[2]!=0xff || s[3]!=0xff || |
| 228 |
s[4]!=0xff || s[5]!=0xff || s[6]!=0xff || s[7]!=0xff || |
| 229 |
s[8]!=0xf0 || s[9]!=0x00 || s[10]!=0x00 || s[11]!=0x00) |
| 230 |
ret = 1; |
| 231 |
} |
| 232 |
done: |
| 233 |
exit(ret); |
| 234 |
} |
| 235 |
#endif |
| 236 |
|
| 237 |
#ifdef H5_NO_ALIGNMENT_RESTRICTIONS_TEST |
| 238 |
|
| 239 |
#include <stdlib.h> |
| 240 |
#include <string.h> |
| 241 |
|
| 242 |
typedef struct { |
| 243 |
size_t len; |
| 244 |
void *p; |
| 245 |
} hvl_t; |
| 246 |
|
| 247 |
#ifdef FC_DUMMY_MAIN |
| 248 |
#ifndef FC_DUMMY_MAIN_EQ_F77 |
| 249 |
# ifdef __cplusplus |
| 250 |
extern "C" |
| 251 |
# endif |
| 252 |
int FC_DUMMY_MAIN() |
| 253 |
{ return 1;} |
| 254 |
#endif |
| 255 |
#endif |
| 256 |
int HDF_NO_UBSAN |
| 257 |
main () |
| 258 |
{ |
| 259 |
|
| 260 |
char *chp = "beefs"; |
| 261 |
char **chpp = malloc (2 * sizeof (char *)); |
| 262 |
char **chpp2; |
| 263 |
hvl_t vl = { 12345, (void *) chp }; |
| 264 |
hvl_t *vlp; |
| 265 |
hvl_t *vlp2; |
| 266 |
|
| 267 |
memcpy ((void *) ((char *) chpp + 1), &chp, sizeof (char *)); |
| 268 |
chpp2 = (char **) ((char *) chpp + 1); |
| 269 |
if (strcmp (*chpp2, chp)) { |
| 270 |
free (chpp); |
| 271 |
return 1; |
| 272 |
} |
| 273 |
free (chpp); |
| 274 |
|
| 275 |
vlp = malloc (2 * sizeof (hvl_t)); |
| 276 |
memcpy ((void *) ((char *) vlp + 1), &vl, sizeof (hvl_t)); |
| 277 |
vlp2 = (hvl_t *) ((char *) vlp + 1); |
| 278 |
if (vlp2->len != vl.len || vlp2->p != vl.p) { |
| 279 |
free (vlp); |
| 280 |
return 1; |
| 281 |
} |
| 282 |
free (vlp); |
| 283 |
|
| 284 |
; |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
#endif |
| 289 |
|
| 290 |
#ifdef H5_DISABLE_SOME_LDOUBLE_CONV_TEST |
| 291 |
|
| 292 |
#include <stdio.h> |
| 293 |
#include <stdlib.h> |
| 294 |
#include <string.h> |
| 295 |
|
| 296 |
int HDF_NO_UBSAN main(void) |
| 297 |
{ |
| 298 |
FILE *fp; |
| 299 |
char cpu[64]; |
| 300 |
|
| 301 |
fp = popen("uname -m", "r"); |
| 302 |
|
| 303 |
fgets(cpu, sizeof(cpu)-1, fp); |
| 304 |
|
| 305 |
pclose(fp); |
| 306 |
|
| 307 |
if(strncmp(cpu, "ppc64le", 7) == 0) |
| 308 |
return 0; |
| 309 |
|
| 310 |
return 1; |
| 311 |
} |
| 312 |
|
| 313 |
#endif |