This disables various destructive functions and prevents the generated code from interfering with the test (e.g. fork bomb, killing other processes, removing filesystem files, etc.) WARNING This function is NOT a security sandbox. Untrusted code, including, model- generated
(maximum_memory_bytes: Optional[int] = None)
| 657 | |
| 658 | |
| 659 | def reliability_guard(maximum_memory_bytes: Optional[int] = None): |
| 660 | """ |
| 661 | This disables various destructive functions and prevents the generated code |
| 662 | from interfering with the test (e.g. fork bomb, killing other processes, |
| 663 | removing filesystem files, etc.) |
| 664 | |
| 665 | WARNING |
| 666 | This function is NOT a security sandbox. Untrusted code, including, model- |
| 667 | generated code, should not be blindly executed outside of one. See the |
| 668 | Codex paper for more information about OpenAI's code sandbox, and proceed |
| 669 | with caution. |
| 670 | """ |
| 671 | |
| 672 | if maximum_memory_bytes is not None: |
| 673 | import resource |
| 674 | resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes)) |
| 675 | resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes)) |
| 676 | if not platform.uname().system == 'Darwin': |
| 677 | resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes)) |
| 678 | |
| 679 | faulthandler.disable() |
| 680 | |
| 681 | import builtins |
| 682 | builtins.exit = None |
| 683 | builtins.quit = None |
| 684 | |
| 685 | import os |
| 686 | os.environ['OMP_NUM_THREADS'] = '1' |
| 687 | |
| 688 | os.kill = None |
| 689 | os.system = None |
| 690 | os.putenv = None |
| 691 | os.remove = None |
| 692 | os.removedirs = None |
| 693 | os.rmdir = None |
| 694 | os.fchdir = None |
| 695 | os.setuid = None |
| 696 | os.fork = None |
| 697 | os.forkpty = None |
| 698 | os.killpg = None |
| 699 | os.rename = None |
| 700 | os.renames = None |
| 701 | os.truncate = None |
| 702 | os.replace = None |
| 703 | os.unlink = None |
| 704 | os.fchmod = None |
| 705 | os.fchown = None |
| 706 | os.chmod = None |
| 707 | os.chown = None |
| 708 | os.chroot = None |
| 709 | os.fchdir = None |
| 710 | os.lchflags = None |
| 711 | os.lchmod = None |
| 712 | os.lchown = None |
| 713 | os.getcwd = None |
| 714 | os.chdir = None |
| 715 | |
| 716 | import shutil |