loubnabnl HF staff commited on
Commit
a33001a
1 Parent(s): 942b4fc

add a reliability guard

Browse files
Files changed (1) hide show
  1. testing_util.py +85 -0
testing_util.py CHANGED
@@ -1,6 +1,7 @@
1
  import json
2
  import sys
3
  import faulthandler
 
4
 
5
  # used for debugging to time steps
6
  from datetime import datetime
@@ -52,6 +53,9 @@ def run_test(sample, test=None, debug=False):
52
  if test(generated_code) is not None it'll try to run the code.
53
  otherwise it'll just return an input and output pair.
54
  """
 
 
 
55
  if debug:
56
  print(f"start = {datetime.now().time()}")
57
 
@@ -436,3 +440,84 @@ def call_method(method, inputs):
436
  pass
437
  return _inner_call_method(method)
438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import json
2
  import sys
3
  import faulthandler
4
+ import platform
5
 
6
  # used for debugging to time steps
7
  from datetime import datetime
 
53
  if test(generated_code) is not None it'll try to run the code.
54
  otherwise it'll just return an input and output pair.
55
  """
56
+ # Disable functionalities that can make destructive changes to the test.
57
+ reliability_guard()
58
+
59
  if debug:
60
  print(f"start = {datetime.now().time()}")
61
 
 
440
  pass
441
  return _inner_call_method(method)
442
 
443
+
444
+
445
+
446
+ def reliability_guard(maximum_memory_bytes=None):
447
+ """
448
+ This disables various destructive functions and prevents the generated code
449
+ from interfering with the test (e.g. fork bomb, killing other processes,
450
+ removing filesystem files, etc.)
451
+ WARNING
452
+ This function is NOT a security sandbox. Untrusted code, including, model-
453
+ generated code, should not be blindly executed outside of one. See the
454
+ Codex paper for more information about OpenAI's code sandbox, and proceed
455
+ with caution.
456
+ """
457
+
458
+ if maximum_memory_bytes is not None:
459
+ import resource
460
+
461
+ resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes))
462
+ resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes))
463
+ if not platform.uname().system == "Darwin":
464
+ resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes))
465
+
466
+ faulthandler.disable()
467
+
468
+ import builtins
469
+
470
+ builtins.exit = None
471
+ builtins.quit = None
472
+
473
+ import os
474
+
475
+ os.environ["OMP_NUM_THREADS"] = "1"
476
+
477
+ os.kill = None
478
+ os.system = None
479
+ os.putenv = None
480
+ os.remove = None
481
+ os.removedirs = None
482
+ os.rmdir = None
483
+ os.fchdir = None
484
+ os.setuid = None
485
+ os.fork = None
486
+ os.forkpty = None
487
+ os.killpg = None
488
+ os.rename = None
489
+ os.renames = None
490
+ os.truncate = None
491
+ os.replace = None
492
+ os.unlink = None
493
+ os.fchmod = None
494
+ os.fchown = None
495
+ os.chmod = None
496
+ os.chown = None
497
+ os.chroot = None
498
+ os.fchdir = None
499
+ os.lchflags = None
500
+ os.lchmod = None
501
+ os.lchown = None
502
+ os.getcwd = None
503
+ os.chdir = None
504
+
505
+ import shutil
506
+
507
+ shutil.rmtree = None
508
+ shutil.move = None
509
+ shutil.chown = None
510
+
511
+ import subprocess
512
+
513
+ subprocess.Popen = None # type: ignore
514
+
515
+ __builtins__["help"] = None
516
+
517
+ import sys
518
+
519
+ sys.modules["ipdb"] = None
520
+ sys.modules["joblib"] = None
521
+ sys.modules["resource"] = None
522
+ sys.modules["psutil"] = None
523
+ sys.modules["tkinter"] = None