# SandboxCpp **Repository Path**: x_wq3337/sandbox_cpp ## Basic Information - **Project Name**: SandboxCpp - **Description**: 使用cpp完成的代码运行沙箱项目 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-12 - **Last Updated**: 2025-08-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: Cpp ## README # Judge Sandbox A secure sandbox environment for running untrusted code in an isolated environment. This project provides a C++ library for creating and managing sandboxed processes using Linux namespaces, cgroups, and other isolation mechanisms. ## Features - Process isolation using Linux namespaces (PID, mount, network, UTS, IPC) - Resource limits using cgroups (CPU, memory, file size) - Configurable mount points and root directory - Comprehensive logging and error handling - Clean process termination and resource cleanup ## Requirements - Linux kernel 4.6+ - CMake 3.10+ - GTest - spdlog ## Building ### 使用Docker构建 ```shell # 构建Docker镜像 docker build -t judge-sandbox . # 运行Docker容器 docker run -it --rm --privileged judge-sandbox ``` ### 原生Linux编译 ```shell sudo apt update sudo apt install -y \ build-essential cmake git \ libgtest-dev nlohmann-json3-dev \ libspdlog-dev libseccomp-dev mkdir -p build cd build cmake .. -DCMAKE_BUILD_TYPE=Release # 需要 CMake ≥ 3.10 make -j$(nproc) # 多核并行编译 ctest --output-on-failure # (可选)运行单元测试 ``` ## Usage ```shell mv build/libjudge.so* lib ``` - libjudge.so.1.0.0 这是实际的动态库文件,包含了完整的二进制代码 这个文件名表示库的具体版本号:主版本号.次版本号.修订号(1.0.0) 当库有 bug 修复或小更新时,可能会变成 1.0.1, 1.0.2 等 通常不会直接链接到这个文件,因为它的名字会随版本更新而改变 - libjudge.so.1 这是一个符号链接,指向当前主版本号(1)下最新的库文件 目前指向 libjudge.so.1.0.0 当发布新的次版本时(如 1.0.1),这个链接会指向新版本 保证 ABI(二进制接口)兼容性,同一主版本号下的更新不会破坏已有的程序 运行时链接器会使用这个文件 - libjudge.so 这是开发时使用的符号链接,指向 libjudge.so.1 在编译程序时使用这个文件(-ljudge) 不包含版本号,方便开发者使用 ```cpp #include "sandbox_manager.h" int main() { // Create sandbox manager SandboxManager sandbox; // Configure sandbox sandbox.setCommand("echo Hello, World!"); sandbox.setCPUTimeLimit(1); // 1 second sandbox.setMemoryLimit(64); // 64MB sandbox.setOutputLimit(1024); // 1MB // Run command int exit_code = sandbox.run(); // Get output std::string stdout = sandbox.getStdout(); std::string stderr = sandbox.getStderr(); return exit_code; } ``` ```shell # 在开发时(编译阶段) g++ your_program.cpp -I/path/to/include -L/path/to/lib -ljudge -o your_program # 在部署时 ## 1. 将所有三个文件复制到系统库目录(需要 root 权限) sudo cp lib/libjudge.so* /usr/local/lib/ ## 2. 更新系统的动态库缓存 sudo ldconfig ## 3. 确保安装所有依赖 sudo apt-get install libspdlog-dev nlohmann-json3-dev libseccomp-dev # 临时使用 # 设置 LD_LIBRARY_PATH 环境变量,不需要安装到系统目录 export LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH ./your_program ``` ## License MIT License