|
1 | 1 | import sys
|
| 2 | +import time |
2 | 3 |
|
3 | 4 | import unittest
|
4 | 5 | from test import support
|
@@ -623,6 +624,89 @@ def test_max_str_digits(self):
|
623 | 624 | with self.assertRaises(ValueError):
|
624 | 625 | str(i)
|
625 | 626 |
|
| 627 | + @unittest.skip("timing-sensitive tests are evil, especially under JIT + inliner") |
| 628 | + def test_denial_of_service_prevented_int_to_str(self): |
| 629 | + """Regression test: ensure we fail before performing O(N**2) work.""" |
| 630 | + maxdigits = sys.get_int_max_str_digits() |
| 631 | + assert maxdigits < 50_000, maxdigits # A test prerequisite. |
| 632 | + get_time = time.process_time |
| 633 | + if get_time() <= 0: # some platforms like WASM lack process_time() |
| 634 | + get_time = time.monotonic |
| 635 | + |
| 636 | + huge_int = int(f'0x{"c"*65_000}', base=16) # 78268 decimal digits. |
| 637 | + digits = 78_268 |
| 638 | + with support.adjust_int_max_str_digits(digits): |
| 639 | + start = get_time() |
| 640 | + huge_decimal = str(huge_int) |
| 641 | + seconds_to_convert = get_time() - start |
| 642 | + self.assertEqual(len(huge_decimal), digits) |
| 643 | + # Ensuring that we chose a slow enough conversion to measure. |
| 644 | + # It takes 0.1 seconds on a Zen based cloud VM in an opt build. |
| 645 | + if seconds_to_convert < 0.005: |
| 646 | + raise unittest.SkipTest('"slow" conversion took only ' |
| 647 | + f'{seconds_to_convert} seconds.') |
| 648 | + |
| 649 | + # We test with the limit almost at the size needed to check performance. |
| 650 | + # The performant limit check is slightly fuzzy, give it a some room. |
| 651 | + with support.adjust_int_max_str_digits(int(.995 * digits)): |
| 652 | + with self.assertRaises(ValueError) as err: |
| 653 | + start = get_time() |
| 654 | + str(huge_int) |
| 655 | + seconds_to_fail_huge = get_time() - start |
| 656 | + self.assertIn('conversion', str(err.exception)) |
| 657 | + self.assertLess(seconds_to_fail_huge, seconds_to_convert/8) |
| 658 | + |
| 659 | + # Now we test that a conversion that would take 30x as long also fails |
| 660 | + # in a similarly fast fashion. |
| 661 | + extra_huge_int = int(f'0x{"c"*500_000}', base=16) # 602060 digits. |
| 662 | + with self.assertRaises(ValueError) as err: |
| 663 | + start = get_time() |
| 664 | + # If not limited, 8 seconds said Zen based cloud VM. |
| 665 | + str(extra_huge_int) |
| 666 | + seconds_to_fail_extra_huge = get_time() - start |
| 667 | + self.assertIn('conversion', str(err.exception)) |
| 668 | + self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/8) |
| 669 | + |
| 670 | + @unittest.skip("timing-sensitive tests are evil, especially under JIT + inliner") |
| 671 | + def test_denial_of_service_prevented_str_to_int(self): |
| 672 | + """Regression test: ensure we fail before performing O(N**2) work.""" |
| 673 | + maxdigits = sys.get_int_max_str_digits() |
| 674 | + assert maxdigits < 100_000, maxdigits # A test prerequisite. |
| 675 | + get_time = time.process_time |
| 676 | + if get_time() <= 0: # some platforms like WASM lack process_time() |
| 677 | + get_time = time.monotonic |
| 678 | + |
| 679 | + digits = 133700 |
| 680 | + huge = '8'*digits |
| 681 | + with support.adjust_int_max_str_digits(digits): |
| 682 | + start = get_time() |
| 683 | + int(huge) |
| 684 | + seconds_to_convert = get_time() - start |
| 685 | + # Ensuring that we chose a slow enough conversion to measure. |
| 686 | + # It takes 0.1 seconds on a Zen based cloud VM in an opt build. |
| 687 | + if seconds_to_convert < 0.005: |
| 688 | + raise unittest.SkipTest('"slow" conversion took only ' |
| 689 | + f'{seconds_to_convert} seconds.') |
| 690 | + |
| 691 | + with support.adjust_int_max_str_digits(digits - 1): |
| 692 | + with self.assertRaises(ValueError) as err: |
| 693 | + start = get_time() |
| 694 | + int(huge) |
| 695 | + seconds_to_fail_huge = get_time() - start |
| 696 | + self.assertIn('conversion', str(err.exception)) |
| 697 | + self.assertLess(seconds_to_fail_huge, seconds_to_convert/8) |
| 698 | + |
| 699 | + # Now we test that a conversion that would take 30x as long also fails |
| 700 | + # in a similarly fast fashion. |
| 701 | + extra_huge = '7'*1_200_000 |
| 702 | + with self.assertRaises(ValueError) as err: |
| 703 | + start = get_time() |
| 704 | + # If not limited, 8 seconds in the Zen based cloud VM. |
| 705 | + int(extra_huge) |
| 706 | + seconds_to_fail_extra_huge = get_time() - start |
| 707 | + self.assertIn('conversion', str(err.exception)) |
| 708 | + self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/8) |
| 709 | + |
626 | 710 | def test_power_of_two_bases_unlimited(self):
|
627 | 711 | """The limit does not apply to power of 2 bases."""
|
628 | 712 | maxdigits = sys.get_int_max_str_digits()
|
|
0 commit comments