Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tổng quan
Chúng tôi đã chuẩn bị một số ví dụ để giúp bạn triển khai thư viện Sandboxed API đầu tiên.
Bạn có thể tìm thấy các tệp này trong //sandboxed_api/examples, hãy xem phần giải thích chi tiết bên dưới.
hello_sapi
Đây là một ví dụ rất cơ bản minh hoạ cách hoạt động của SAPI. hello_lib.cc triển khai một hàm, AddTwoIntegers(), sẽ được chạy trong môi trường hộp cát và gọi trong Mã máy chủ.
Một thư viện minh hoạ triển khai một số hàm C và một hàm C++.
Thư viện này dùng protobuf để trao đổi dữ liệu giữa Mã máy chủ và Thư viện SAPI.
Bạn có thể tìm thấy định nghĩa hộp cát trong tệp sandbox.h.
Bạn có thể tìm thấy tệp chú thích hàm (được tạo tự động) (tệp cung cấp nguyên mẫu của các hàm được cách ly) trong bazel-out/genfiles/sandboxed_api/examples/sum/lib/sum-sapi.sapi.h sau khi tạo Bazel.
Bạn có thể tìm thấy logic thực thi thực tế (còn gọi là Mã máy chủ) sử dụng các quy trình được xuất trong hộp cát trong main_sum.cc.
zlib
Đây là một bản triển khai minh hoạ (có chức năng nhưng hiện không được dùng trong quá trình sản xuất) cho thư viện zlib, xuất một số hàm của thư viện này và cung cấp các hàm đó cho Mã máy chủ.
Chức năng được minh hoạ của Mã máy chủ là giải mã các luồng zlib từ stdin sang stdout.
Thư viện SAPI này không sử dụng tệp sandbox.h vì sử dụng chính sách Sandbox2 mặc định và thư viện SAPI được nhúng, nên bạn không cần cung cấp phương thức sapi::Sandbox::GetLibPath() hoặc sapi::Sandbox::GetPolicy().
Ví dụ này minh hoạ việc sử dụng protobuf để cung cấp một chuỗi reverse và hàm duplication. Thư viện này cũng chứa một sandbox.h chuyên dụng để cung cấp Chính sách nghiêm ngặt hơn về Hộp cát.
Mã máy chủ không đại diện cho một chương trình "bình thường", mà thay vào đó, mã này minh hoạ chức năng của thư viện SAPI bằng cách sử dụng các bài kiểm thử đơn vị.
Trong ví dụ này, các hàm không dựa trên protobuf cũng được đưa vào để so sánh giữa hai phương pháp.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\u003cp\u003eThis page provides examples demonstrating how to implement Sandboxed API (SAPI) libraries.\u003c/p\u003e\n"],["\u003cp\u003eExamples include basic SAPI usage (\u003ccode\u003ehello_sapi\u003c/code\u003e), protobuf data exchange (\u003ccode\u003esum\u003c/code\u003e), and sandboxed zlib functionality (\u003ccode\u003ezlib\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eGoogle interns have created sandboxed versions of various open-source libraries, available on GitHub.\u003c/p\u003e\n"],["\u003cp\u003eEach example includes library code, sandbox definitions (if applicable), and host code for interaction.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003estringop\u003c/code\u003e example showcases the use of protobufs for string operations and features a stricter sandbox policy.\u003c/p\u003e\n"]]],[],null,["Overview\n\nWe have prepared some examples to help you implement your first Sandboxed API\nlibrary.\n\nYou can find them in\n[//sandboxed_api/examples](https://github.com/google/sandboxed-api/blob/master/sandboxed_api/examples),\nsee below for detailed explanations.\n| **Note:** In the summer of 2020, Google interns created a number of SAPI sandboxed versions of open-source libraries which are available for download on [GitHub](https://github.com/google/sandboxed-api/tree/main/oss-internship-2020).\n\nhello_sapi\n\nThis is a very basic example which illustrates how SAPI works. The\n[hello_lib.cc](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/examples/hello_sapi/hello_lib.cc)\nimplements one function, `AddTwoIntegers()`, which will be sandboxed and called\nin the [Host\nCode](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/examples/hello_sapi/hello_main.cc).\n\nThe second Host Code file,\n[hello_transacted.cc](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/examples/hello_sapi/hello_transacted.cc),\ndemonstrates an example use of [SAPI\nTransactions](/code-sandboxing/sandboxed-api/transactions).\n\nSum\n\nA demo library implementing a few [C\nfunctions](https://github.com/google/sandboxed-api/blob/master/sandboxed_api/examples/sum/lib/sum.c)\nand a single [C++\nfunction](https://github.com/google/sandboxed-api/blob/master/sandboxed_api/examples/sum/lib/sum_cpp.cc).\nIt uses protobufs to exchange data between Host Code and the SAPI Library.\n\n- The sandbox definition can be found in the [sandbox.h](https://github.com/google/sandboxed-api/blob/master/sandboxed_api/examples/sum/lib/sandbox.h) file.\n- The (automatically generated) function annotation file (a file providing prototypes of sandboxed functions) can be found in `bazel-out/genfiles/sandboxed_api/examples/sum/lib/sum-sapi.sapi.h` after a Bazel build.\n- The actual execution logic (a.k.a. Host Code) which makes use of the exported sandboxed procedures can be found in [main_sum.cc](https://github.com/google/sandboxed-api/blob/master/sandboxed_api/examples/sum/main_sum.cc).\n\nzlib\n\nThis is a demo implementation (functional, but currently not used in production)\nfor the zlib library, exporting some of its functions and making them available\nto the [Host\nCode](https://github.com/google/sandboxed-api/blob/master/sandboxed_api/examples/zlib/main_zlib.cc).\n\nThe demonstrated functionality of the Host Code is decoding of zlib streams from\nstdin to stdout.\n\nThis SAPI library doesn't use the `sandbox.h` file, as it uses the default\nSandbox2 policy, and an embedded SAPI library, so there is no need to provide\nthe `sapi::Sandbox::GetLibPath()` or the `sapi::Sandbox::GetPolicy()` method.\n\nThe zlib SAPI can be found in\n[//sapi_sandbox/examples/zlib](https://github.com/google/sandboxed-api/blob/master/sandboxed_api/examples/zlib),\nalong with its [Host\nCode](https://github.com/google/sandboxed-api/blob/master/sandboxed_api/examples/zlib/main_zlib.cc).\n\nstringop\n\nThis example demonstrates the use of protobufs to provide a string `reverse` and\n`duplication` function. The\n[library](https://github.com/google/sandboxed-api/tree/main/sandboxed_api/examples/stringop/lib)\nalso contains a dedicated\n[sandbox.h](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/examples/stringop/lib/sandbox.h)\nto provide a stricter Sandbox Policy.\n\nThe [Host\nCode](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/examples/stringop/main_stringop.cc)\ndoesn't represent a \"normal\"program, instead it demonstrates the functionality\nof the SAPI library using unit tests.\n\nIn this example, the non-protobuf based functions are also included to provide a\ncomparison between the two approaches."]]