Running libsandbox

前端 未结 3 1437
梦如初夏
梦如初夏 2021-01-22 12:40

I\'m currently working on an online C/C++/assembly compiler, and I stumbled upon a nice piece of software called libsandbox. This enables me to run the online written code, comp

相关标签:
3条回答
  • 2021-01-22 13:18

    The sandbox is for linux only. You must actually create the sandbox first by using the library functions and then tell the sandbox to run your program.

    This python sample shows how to do it from python. The line "#targeted program" shows you where you will specify the name of your actual application.

    def main(args):
        # sandbox configuration
        cookbook = {
            'args': args[1:],               # targeted program
            'stdin': sys.stdin,             # input to targeted program
            'stdout': sys.stdout,           # output from targeted program
            'stderr': sys.stderr,           # error from targeted program
            'quota': dict(wallclock = 30000,# 30 sec
                          cpu = 2000,       #  2 sec
                          memory = 8388608, #  8 MB
                          disk = 1048576)}  #  1 MB
    
    # create a sandbox instance and execute till end
    msb = MiniSandbox(**cookbook)
    msb.run()
    # verbose statistics
    sys.stderr.write("result: %(result)s\ncpu: %(cpu)dms\nmem: %(mem)dkB\n" % \
        msb.probe())
    return os.EX_OK
    

    I would recommend going to the libsandbox download page and getting the full sample2.py file there and then just running the sandbox using the python script. This will be easier than making a C++ or C program to do it for you.

    So...

    1. Make your C or C++ program. DO NOT LINK IT TO LIBSANDBOX.

    2. Make sure you have python installed.

    3. Run the sample python script from the libsandbox page.

    4. The python script will load the libsandbox for you. Then it will run the program you have built inside the sandbox.

    Simple.

    0 讨论(0)
  • 2021-01-22 13:19

    The short and general answer is: to use libanything, you write a program that utilizes that library - you #include <anything.h> into the source and link with -lanything switch. You're not supposed to find any executable files, unless it's a test suite or an example program for the library.

    I wasn't able to find 'libsandbox' for some reason, so my reply might be grossly inaccurate.

    0 讨论(0)
  • 2021-01-22 13:32

    Totally agree with the answer from @user1401452. Some more tips about libsandbox,

    1. The binary executable to be sandboxed is better linked statically, because loading shared libraries involves system calls, like SYS_open(), that are forbidden by default.
    2. To write a C/C++ program directly invoking the core sandbox library (i.e. libsandbox) is also viable -- though a bit more complex than using the Pythonic wrapper (i.e. pysandbox). An ANSI C equivalent (i.e. sample2.c) of the sample python script is now available at libsandbox's homepage.
    3. The sample programs only demonstrate some essentials of libsandbox. Practical sandboxing solutions typically requires customized sandbox policies with more complex rules.

    DISCLAIMER: I am the author of libsandbox

    0 讨论(0)
提交回复
热议问题