Bu, SAPI'nin işleyiş şeklini gösteren çok temel bir örnektir. hello_lib.cc, korumalı alana alınacak ve Host
Code'da çağrılacak olan AddTwoIntegers() işlevini uygular.
Birkaç C işlevini
ve tek bir C++ işlevini uygulayan bir demo kitaplık.
Ana makine kodu ile SAPI kitaplığı arasında veri alışverişi yapmak için protobuf'ları kullanır.
Korumalı alan tanımını sandbox.h dosyasında bulabilirsiniz.
(Otomatik olarak oluşturulan) işlev notu dosyası (sandbox'a alınmış işlevlerin prototiplerini sağlayan bir dosya), Bazel derlemesinden sonra bazel-out/genfiles/sandboxed_api/examples/sum/lib/sum-sapi.sapi.h içinde bulunabilir.
Dışa aktarılan korumalı alan prosedürlerinden yararlanan asıl yürütme mantığı (diğer adıyla ana makine kodu), main_sum.cc dosyasında bulunabilir.
zlib
Bu, zlib kitaplığı için bir demo uygulamadır (işlevseldir ancak şu anda üretimde kullanılmamaktadır). Kitaplığın bazı işlevlerini dışa aktarır ve Host
Code'da kullanılabilir hale getirir.
Ana makine kodunun gösterilen işlevi, stdin'den stdout'a zlib akışlarının kodunu çözmektir.
Bu SAPI kitaplığı, varsayılan Sandbox2 politikasını ve yerleştirilmiş bir SAPI kitaplığını kullandığından sandbox.h dosyasını kullanmaz. Bu nedenle, sapi::Sandbox::GetLibPath() veya sapi::Sandbox::GetPolicy() yöntemini sağlamanıza gerek yoktur.
Bu örnekte, reverse dizesi ve duplication işlevi sağlamak için protobuf'ların kullanımı gösterilmektedir. Kitaplık, daha katı bir Özel Korumalı Alan Politikası sağlamak için özel bir sandbox.h dosyası da içerir.
Host
Code
"normal" bir programı temsil etmez. Bunun yerine, birim testlerini kullanarak SAPI kitaplığının işlevselliğini gösterir.
Bu örnekte, iki yaklaşım arasında karşılaştırma yapmak için protobuf tabanlı olmayan işlevler de yer almaktadır.
[[["Anlaması kolay","easyToUnderstand","thumb-up"],["Sorunumu çözdü","solvedMyProblem","thumb-up"],["Diğer","otherUp","thumb-up"]],[["İhtiyacım olan bilgiler yok","missingTheInformationINeed","thumb-down"],["Çok karmaşık / çok fazla adım var","tooComplicatedTooManySteps","thumb-down"],["Güncel değil","outOfDate","thumb-down"],["Çeviri sorunu","translationIssue","thumb-down"],["Örnek veya kod sorunu","samplesCodeIssue","thumb-down"],["Diğer","otherDown","thumb-down"]],["Son güncelleme tarihi: 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."]]