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)
| 475 | |
| 476 | |
| 477 | def reliability_guard(maximum_memory_bytes: Optional[int] = None): |
| 478 | """ |
| 479 | This disables various destructive functions and prevents the generated code |
| 480 | from interfering with the test (e.g. fork bomb, killing other processes, |
| 481 | removing filesystem files, etc.) |
| 482 | |
| 483 | WARNING |
| 484 | This function is NOT a security sandbox. Untrusted code, including, model- |
| 485 | generated code, should not be blindly executed outside of one. See the |
| 486 | Codex paper for more information about OpenAI's code sandbox, and proceed |
| 487 | with caution. |
| 488 | """ |
| 489 | |
| 490 | if maximum_memory_bytes is not None: |
| 491 | import resource |
| 492 | resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes)) |
| 493 | resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes)) |
| 494 | if not platform.uname().system == 'Darwin': |
| 495 | resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes)) |
| 496 | |
| 497 | faulthandler.disable() |
| 498 | |
| 499 | import builtins |
| 500 | builtins.exit = None |
| 501 | builtins.quit = None |
| 502 | |
| 503 | import os |
| 504 | os.environ['OMP_NUM_THREADS'] = '1' |
| 505 | |
| 506 | os.kill = None |
| 507 | os.system = None |
| 508 | os.putenv = None |
| 509 | os.remove = None |
| 510 | os.removedirs = None |
| 511 | os.rmdir = None |
| 512 | os.fchdir = None |
| 513 | os.setuid = None |
| 514 | os.fork = None |
| 515 | os.forkpty = None |
| 516 | os.killpg = None |
| 517 | os.rename = None |
| 518 | os.renames = None |
| 519 | os.truncate = None |
| 520 | os.replace = None |
| 521 | os.unlink = None |
| 522 | os.fchmod = None |
| 523 | os.fchown = None |
| 524 | os.chmod = None |
| 525 | os.chown = None |
| 526 | os.chroot = None |
| 527 | os.fchdir = None |
| 528 | os.lchflags = None |
| 529 | os.lchmod = None |
| 530 | os.lchown = None |
| 531 | os.getcwd = None |
| 532 | os.chdir = None |
| 533 | |
| 534 | import shutil |