DF-4883 / fixpt_harness.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 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | /* * Behavioral harness for DF-4883 (dc_fixpt_log divergence / ASSERT-only * validation family in sys/dev/drm/amd/display/dc/basics/fixpt31_32.c). * * Function bodies below are copied VERBATIM from: * sys/dev/drm/amd/display/dc/basics/fixpt31_32.c (from_fraction, exp, * taylor series, log) * sys/dev/drm/amd/display/include/fixed31_32.h (add/sub/neg/abs/lt/le, * mul, div, div_int, mul_int, shl, round, constants) * with the following documented substitutions for userspace: * - ASSERT(x) -> no-op, exactly as in a production kernel build * (os_types.h:76 `#define ASSERT(expr) WARN_ON(!(expr))`, * WARN_ON is non-fatal in the DragonFly DRM compat layer). * - div64_u64_rem/div64_s64 -> plain 64-bit / and %. * - an iteration counter is added to dc_fixpt_log (HARNESS-ONLY, marked). * * Build: cc -O2 -o fixpt_harness fixpt_harness.c * Run: ./fixpt_harness * * Scenarios: * [sanity] dc_fixpt_log(2.0) must converge to ~0.693 quickly. * [DF-4883] dc_fixpt_log(-1.0): child is killed after TIMEOUT seconds or * dies on SIGFPE (integer divide by zero, the userspace mirror * of the kernel #DE panic through complete_integer_division_u64, * the same sink as DF-1949). Either outcome is a halt-class * failure (unbounded loop or divide exception). */ #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <limits.h> #include <unistd.h> #include <signal.h> #include <sys/types.h> #include <sys/wait.h> #define ASSERT(x) do { } while (0) /* production semantics */ struct fixed31_32 { long long value; }; #define FIXED31_32_BITS_PER_FRACTIONAL_PART 32 static const struct fixed31_32 dc_fixpt_zero = { 0 }; static const struct fixed31_32 dc_fixpt_half = { 0x80000000LL }; static const struct fixed31_32 dc_fixpt_one = { 0x100000000LL }; static const struct fixed31_32 dc_fixpt_two_pi = { 26986075409LL }; static const struct fixed31_32 dc_fixpt_ln2 = { 2977044471LL }; static const struct fixed31_32 dc_fixpt_ln2_div_2 = { 1488522236LL }; static unsigned long long div64_u64_rem(unsigned long long dividend, unsigned long long divisor, unsigned long long *remainder) { *remainder = dividend % divisor; return dividend / divisor; } static long long div64_s64(long long n, long long d) { return n / d; } static inline unsigned long long abs_i64(long long arg) /* fixpt31_32.c:29 */ { if (arg > 0) return (unsigned long long)arg; else return (unsigned long long)(-arg); } /* ---- begin verbatim library code ------------------------------------ */ static inline unsigned long long complete_integer_division_u64( /* :43 */ unsigned long long dividend, unsigned long long divisor, unsigned long long *remainder) { unsigned long long result; ASSERT(divisor); result = div64_u64_rem(dividend, divisor, remainder); return result; } #define FRACTIONAL_PART_MASK \ ((1ULL << FIXED31_32_BITS_PER_FRACTIONAL_PART) - 1) #define GET_INTEGER_PART(x) \ ((x) >> FIXED31_32_BITS_PER_FRACTIONAL_PART) #define GET_FRACTIONAL_PART(x) \ (FRACTIONAL_PART_MASK & (x)) struct fixed31_32 dc_fixpt_from_fraction(long long numerator, long long denominator) /* :67 */ { struct fixed31_32 res; int arg1_negative = numerator < 0; int arg2_negative = denominator < 0; unsigned long long arg1_value = arg1_negative ? -numerator : numerator; unsigned long long arg2_value = arg2_negative ? -denominator : denominator; unsigned long long remainder; unsigned long long res_value = complete_integer_division_u64( arg1_value, arg2_value, &remainder); ASSERT(res_value <= LONG_MAX); { unsigned int i = FIXED31_32_BITS_PER_FRACTIONAL_PART; do { remainder <<= 1; res_value <<= 1; if (remainder >= arg2_value) { res_value |= 1; remainder -= arg2_value; } } while (--i != 0); } { unsigned long long summand = (remainder << 1) >= arg2_value; ASSERT(res_value <= LLONG_MAX - summand); res_value += summand; } res.value = (long long)res_value; if (arg1_negative ^ arg2_negative) res.value = -res.value; return res; } /* header inlines needed by exp/log (verbatim) */ static inline struct fixed31_32 dc_fixpt_from_int(long long value) { struct fixed31_32 res; res.value = value << FIXED31_32_BITS_PER_FRACTIONAL_PART; return res; } static inline struct fixed31_32 dc_fixpt_neg(struct fixed31_32 arg) { struct fixed31_32 res; res.value = -arg.value; return res; } static inline struct fixed31_32 dc_fixpt_abs(struct fixed31_32 arg) { if (arg.value < 0) return dc_fixpt_neg(arg); else return arg; } static inline int dc_fixpt_lt(struct fixed31_32 a, struct fixed31_32 b) { return a.value < b.value; } static inline int dc_fixpt_le(struct fixed31_32 a, struct fixed31_32 b) { return a.value <= b.value; } static inline struct fixed31_32 dc_fixpt_add(struct fixed31_32 arg1, struct fixed31_32 arg2) { struct fixed31_32 res; res.value = arg1.value + arg2.value; return res; } static inline struct fixed31_32 dc_fixpt_sub(struct fixed31_32 arg1, struct fixed31_32 arg2) { struct fixed31_32 res; res.value = arg1.value - arg2.value; return res; } static inline struct fixed31_32 dc_fixpt_div_int(struct fixed31_32 arg1, long long arg2) { return dc_fixpt_from_fraction(arg1.value, dc_fixpt_from_int(arg2).value); } static inline struct fixed31_32 dc_fixpt_div(struct fixed31_32 arg1, struct fixed31_32 arg2) { return dc_fixpt_from_fraction(arg1.value, arg2.value); } static inline struct fixed31_32 dc_fixpt_shl(struct fixed31_32 arg, unsigned char shift) { arg.value = arg.value << shift; return arg; } static inline int dc_fixpt_round(struct fixed31_32 arg) { unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value; const long long summand = dc_fixpt_half.value; arg_value += summand; if (arg.value >= 0) return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART); else return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART); } struct fixed31_32 dc_fixpt_mul(struct fixed31_32 arg1, struct fixed31_32 arg2) /* :119 */ { struct fixed31_32 res; int arg1_negative = arg1.value < 0; int arg2_negative = arg2.value < 0; unsigned long long arg1_value = arg1_negative ? -arg1.value : arg1.value; unsigned long long arg2_value = arg2_negative ? -arg2.value : arg2.value; unsigned long long arg1_int = GET_INTEGER_PART(arg1_value); unsigned long long arg2_int = GET_INTEGER_PART(arg2_value); unsigned long long arg1_fra = GET_FRACTIONAL_PART(arg1_value); unsigned long long arg2_fra = GET_FRACTIONAL_PART(arg2_value); unsigned long long tmp; res.value = arg1_int * arg2_int; res.value <<= FIXED31_32_BITS_PER_FRACTIONAL_PART; tmp = arg1_int * arg2_fra; res.value += tmp; tmp = arg2_int * arg1_fra; res.value += tmp; tmp = arg1_fra * arg2_fra; tmp = (tmp >> FIXED31_32_BITS_PER_FRACTIONAL_PART) + (tmp >= (unsigned long long)dc_fixpt_half.value); res.value += tmp; if (arg1_negative ^ arg2_negative) res.value = -res.value; return res; } static inline struct fixed31_32 dc_fixpt_mul_int(struct fixed31_32 arg1, int arg2) { return dc_fixpt_mul(arg1, dc_fixpt_from_int(arg2)); } static struct fixed31_32 fixed31_32_exp_from_taylor_series(struct fixed31_32 arg) /* :307 */ { unsigned int n = 9; struct fixed31_32 res = dc_fixpt_from_fraction( n + 2, n + 1); ASSERT(dc_fixpt_lt(arg, dc_fixpt_one)); do res = dc_fixpt_add( dc_fixpt_one, dc_fixpt_div_int( dc_fixpt_mul( arg, res), n)); while (--n != 1); return dc_fixpt_add( dc_fixpt_one, dc_fixpt_mul( arg, res)); } struct fixed31_32 dc_fixpt_exp(struct fixed31_32 arg) /* :335 */ { if (dc_fixpt_le( dc_fixpt_ln2_div_2, dc_fixpt_abs(arg))) { int m = dc_fixpt_round( dc_fixpt_div( arg, dc_fixpt_ln2)); struct fixed31_32 r = dc_fixpt_sub( arg, dc_fixpt_mul_int( dc_fixpt_ln2, m)); ASSERT(m != 0); ASSERT(dc_fixpt_lt(dc_fixpt_abs(r), dc_fixpt_one)); if (m > 0) return dc_fixpt_shl( fixed31_32_exp_from_taylor_series(r), (unsigned char)m); else return dc_fixpt_div_int( fixed31_32_exp_from_taylor_series(r), 1LL << -m); } else if (arg.value != 0) return fixed31_32_exp_from_taylor_series(arg); else return dc_fixpt_one; } static unsigned long long g_log_iterations; /* HARNESS-ONLY instrumentation */ static void sigfpe_report(int sig) /* HARNESS-ONLY */ { char buf[128]; int n = snprintf(buf, sizeof(buf), "[child] SIGFPE (integer 0/0) trapped after ~%llu log iterations\n", g_log_iterations); write(2, buf, n); _exit(128 + sig); } struct fixed31_32 dc_fixpt_log(struct fixed31_32 arg) /* :378 */ { struct fixed31_32 res = dc_fixpt_neg(dc_fixpt_one); struct fixed31_32 error; ASSERT(arg.value > 0); g_log_iterations = 0; do { struct fixed31_32 res1 = dc_fixpt_add( dc_fixpt_sub( res, dc_fixpt_one), dc_fixpt_div( arg, dc_fixpt_exp(res))); error = dc_fixpt_sub( res, res1); res = res1; g_log_iterations++; /* HARNESS-ONLY */ } while (abs_i64(error.value) > 100ULL); return res; } /* ---- end verbatim library code -------------------------------------- */ int main(void) { struct fixed31_32 r; setvbuf(stdout, NULL, _IONBF, 0); /* [1] sanity: log(2) converges */ r = dc_fixpt_log(dc_fixpt_from_int(2)); printf("[sanity] dc_fixpt_log(2.0) = %.9f (expect ~0.693147181) after %llu iterations\n", (double)r.value / 4294967296.0, g_log_iterations); /* [2] DF-4883: log(0) and [3] DF-4883: log(-1) — each in a forked * child; a SIGFPE handler prints the iteration count at trap time. */ { static const long long cases[] = { 0, -1 }; for (unsigned ci = 0; ci < sizeof(cases)/sizeof(cases[0]); ci++) { fflush(stdout); pid_t p = fork(); if (p == 0) { signal(SIGFPE, sigfpe_report); struct fixed31_32 a; a.value = cases[ci] << FIXED31_32_BITS_PER_FRACTIONAL_PART; r = dc_fixpt_log(a); printf("[DF-4883] dc_fixpt_log(%lld.0) RETURNED %.9f after %llu iterations\n", cases[ci], (double)r.value / 4294967296.0, g_log_iterations); _exit(0); } int st = 0, done = 0; for (int i = 0; i < 60 && !done; i++) { /* wait ~60s */ if (waitpid(p, &st, WNOHANG) == p) done = 1; else usleep(1000000); } if (done && WIFSIGNALED(st)) { printf("[DF-4883] log(%lld.0): child killed by signal %d " "(SIGFPE=%d => integer-divide-by-zero; kernel equivalent " "= #DE panic via complete_integer_division_u64, the " "DF-1949 sink)\n", cases[ci], WTERMSIG(st), SIGFPE); } else if (done && WIFEXITED(st)) { printf("[DF-4883] log(%lld.0): child exited status=%d\n", cases[ci], WEXITSTATUS(st)); } else { kill(p, SIGKILL); waitpid(p, &st, 0); printf("[DF-4883] log(%lld.0): child still looping after 60s " "=> UNBOUNDED LOOP in dc_fixpt_log (kernel: wedged thread, " "100%% CPU holding display state)\n", cases[ci]); } } } return 0; } |