変数ガイド
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
はじめに
[概要] ページで説明したように、ホストコードはサンドボックス ライブラリに RPC 呼び出しを行います。サンドボックス化により、プロセス間でメモリが分離されるため、ホストコードはサンドボックス化されたライブラリのメモリに直接アクセスできません。
ホストコードがリモート プロセスの変数とメモリブロックにアクセスできるようにし、メインロジック コードの実装を簡素化するために、SAPI は包括的な C++ クラスのセットを提供します。ただし、多くの場合、ネイティブ C 型も使用できます。
特殊な型(SAPI 型)が必要になるのは、単純な型とメモリブロック(構造体、配列)へのポインタを渡す場合です。
たとえば、ポインタを受け取る関数を呼び出す場合、ポインタはサンドボックス ライブラリのメモリ内の対応するポインタに変換する必要があります。次のコード スニペットは、このシナリオを視覚化したものです。3 つの整数の配列ではなく、::sapi::v::Array<int>
オブジェクトが作成され、サンドボックス化されたライブラリの API 呼び出しで渡すことができます。
int arr[3] = {1, 2, 3};
sapi::v::Array<int> sarr(arr, ABSL_ARRAYSIZE(arr));
利用可能なすべての SAPI タイプの包括的な概要については、SAPI プロジェクトのソースコードの var_*.h
ヘッダー ファイルをご覧ください。これらのヘッダー ファイルは、さまざまな型のデータを表すクラスとテンプレートを提供します。例:
::sapi::v::UChar
は、よく知られている符号なし文字を表します。
::sapi::v::Array<int>
は整数の配列を表します。
SAPI のタイプ
このセクションでは、ホストコードでよく見られる 3 つの SAPI タイプを紹介します。
SAPI ポインタ
サンドボックス化される関数でポインタを渡す必要がある場合、このポインタは以下の PtrXXX()
メソッドのいずれかから取得する必要があります。これらのメソッドは、SAPI 変数クラスによって実装されます。
ポインタ型 |
::PtrNone() |
サンドボックス化された API 関数に渡されたときに、ホストコード プロセスとサンドボックス化されたライブラリ プロセスの間で基盤となるメモリを同期しない。 |
::PtrBefore() |
サンドボックス化された API 関数呼び出しが行われる前に、指しているオブジェクトのメモリを同期します。つまり、呼び出しが開始される前に、ポインタが指す変数のローカル メモリがサンドボックス ライブラリ プロセスに転送されます。 |
::PtrAfter() |
サンドボックス化された API 関数呼び出しの後に、ポイント先のオブジェクトのメモリを同期します。つまり、ポインタが指す変数のリモート メモリは、呼び出しが完了した後にホストコード プロセスのメモリに転送されます。 |
::PtrBoth() |
::PtrBefore() と ::PtrAfter() の機能を組み合わせます。 |
SAPI ポインタのドキュメントはこちらで確認できます。
SAPI 構造体
テンプレート ::sapi::v::Struct
は var_struct.h で説明されています。既存の構造をラップするために使用できるコンストラクタを提供します。SAPI Struct は、SAPI ポインタで説明されているすべてのメソッドを提供し、サンドボックス化されたライブラリ呼び出しに使用できる ::sapi::v::Ptr
オブジェクトを取得します。
次のコード スニペットは、構造が初期化され、zlib の例でサンドボックス化された関数呼び出しに渡される様子を示しています。
sapi::v::Struct<sapi::zlib::z_stream> strm;
…
if (ret = api.deflateInit_(strm.PtrBoth(), Z_DEFAULT_COMPRESSION,
version.PtrBefore(), sizeof(sapi::zlib::z_stream));
…
既存の構造体にポインタが含まれている場合、それらのポインタは Sandboxee のアドレスを指します。そのため、ホストコードからアクセスできるようになる前に、Sandboxee データを転送する必要があります。
SAPI 配列
テンプレート ::sapi::v::Array
は var_array.h で説明されています。このテンプレートには 2 つのコンストラクタがあります。1 つは既存の要素配列をラップするために使用でき、もう 1 つは配列を動的に作成するために使用できます。
次のコード スニペット(sum の例から抜粋)は、このオブジェクトが所有していない配列をラップするコンストラクタの使用方法を示しています。
int arr[10];
sapi::v::Array<int> iarr(arr, ABSL_ARRAYSIZE(arr));
次のコード スニペットは、配列を動的に作成するために使用されるコンストラクタの例を示しています。
sapi::v::Array<uint8_t> buffer(PNG_IMAGE_SIZE(*image.mutable_data()));
SAPI Array は、SAPI ポインタで説明されているすべてのメソッドを提供し、サンドボックス化されたライブラリ呼び出しに使用できる ::sapi::v::Ptr
オブジェクトを取得します。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-26 UTC。"],[[["\u003cp\u003eSandboxed API (SAPI) provides C++ classes (SAPI Types) to enable the Host Code to interact with memory in the Sandboxed Library, particularly for pointers, structures, and arrays.\u003c/p\u003e\n"],["\u003cp\u003eSAPI Pointers (\u003ccode\u003ePtrNone\u003c/code\u003e, \u003ccode\u003ePtrBefore\u003c/code\u003e, \u003ccode\u003ePtrAfter\u003c/code\u003e, \u003ccode\u003ePtrBoth\u003c/code\u003e) control the synchronization of memory between the Host Code and the Sandboxed Library before and after API calls.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003e::sapi::v::Struct\u003c/code\u003e template allows wrapping existing structures for use in sandboxed calls, requiring pointer synchronization for sandboxee data accessibility.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003e::sapi::v::Array\u003c/code\u003e template enables working with arrays in sandboxed environments, offering constructors for wrapping existing arrays and dynamic array creation.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the \u003ccode\u003evar_*.h\u003c/code\u003e header files in the SAPI project source code for a detailed overview of all available SAPI Types and their usage.\u003c/p\u003e\n"]]],[],null,["Introduction\n\nAs explained on the [Overview](/code-sandboxing/sandboxed-api) page, the Host\nCode makes RPC calls to the Sandboxed Library. Sandboxing results in a memory\nseparation between the processes, and thus the Host Code cannot directly access\nmemory in the Sandboxed Library.\n\nIn order to make sure that the Host Code can access variables and memory blocks\nin a remote process and to make the implementation of the main logic code\nsimpler, SAPI provides a comprehensive set of C++ classes. However, in many\ncases you will also be able to use native C-types.\n\nThe need for the special types (SAPI Types) arises when passing pointers to\nsimple types and memory blocks (structures, arrays).\n\nFor example, when calling a function taking a pointer, the pointer must be\nconverted into a corresponding pointer inside the Sandboxed Library's memory.\nThe below code snippet visualises this scenario. Instead of an array of three\nintegers, a `::sapi::v::Array\u003cint\u003e` object is created which can then be passed\nin the Sandboxed Library's API call: \n\n int arr[3] = {1, 2, 3};\n sapi::v::Array\u003cint\u003e sarr(arr, ABSL_ARRAYSIZE(arr));\n\nFor a comprehensive overview of all available SAPI Types, review the `var_*.h`\nheader files in the [SAPI project source\ncode](https://github.com/google/sandboxed-api/tree/master/sandboxed_api). These\nheader files provide classes and templates representing various types of data,\ne.g.:\n\n- `::sapi::v::UChar` represents well-known unsigned chars\n- `::sapi::v::Array\u003cint\u003e` represents an array of integers\n\nSAPI Types\n\nThis section introduces three SAPI Types that are commonly seen in Host Code.\n\nSAPI Pointers\n\nIf a function to be sandboxed requires passing a pointer, this pointer should be\nobtained from one of the `PtrXXX()` methods below. These methods are implemented\nby the SAPI variable classes.\n\n| Pointer Types ||\n|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `::PtrNone()` | Doesn't synchronize the underlying memory between the Host Code process and the Sandboxed Library process when passed to a sandboxed API function. |\n| `::PtrBefore()` | Synchronizes the memory of the object it points to **before** the sandboxed API function call takes place. This means the local memory of the pointed variable will be transferred to the Sandboxed Library process before the call is initiated. |\n| `::PtrAfter()` | Synchronizes the memory of the object it points to **after** the sandboxed API function call takes place. This means the remote memory of the pointed variable will be transferred to the Host Code process memory **after** the call has been completed. |\n| `::PtrBoth()` | Combines the functionality of `::PtrBefore()` and `::PtrAfter()`. |\n\nThe documentation for SAPI pointers can be found\n[here](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/var_pointable.h).\n\nSAPI Struct\n\nThe template `::sapi::v::Struct` is documented in\n[var_struct.h](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/var_struct.h).\nIt provides a constructor that can be used to wrap existing structures. SAPI\nStruct provides all methods outlined in [SAPI Pointers](#sapi_pointers) to\nobtain a `::sapi::v::Ptr` object that can be used for sandboxed library calls.\n\nThe code snippet below shows a structure being initiated and then passed to a\nsandboxed function call in the [zlib\nexample](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/examples/zlib/main_zlib.cc): \n\n sapi::v::Struct\u003csapi::zlib::z_stream\u003e strm;\n ...\n if (ret = api.deflateInit_(strm.PtrBoth(), Z_DEFAULT_COMPRESSION,\n version.PtrBefore(), sizeof(sapi::zlib::z_stream));\n ...\n\nIf your existing struct contains pointers, then those pointers will point to\naddresses in the Sandboxee. In consequence, you will have to transfer Sandboxee\ndata before it becomes accessible to the Host Code.\n\nSAPI Arrays\n\nThe template `::sapi::v::Array` is documented in\n[var_array.h](https://github.com/google/sandboxed-api/blob/main/sandboxed_api/var_array.h).\nIt provides two constructors, one that can be used to wrap existing arrays of\nelements, and another one to dynamically create an array.\n\nThis code snippet (taken from the [sum\nexample](/code-sandboxing/sandboxed-api/examples#sum)) shows the use of the\nconstructor that wraps around an array which is not owned by this object: \n\n int arr[10];\n sapi::v::Array\u003cint\u003e iarr(arr, ABSL_ARRAYSIZE(arr));\n\nThis code snippet shows an example of the constructor used to dynamically create\nan array: \n\n sapi::v::Array\u003cuint8_t\u003e buffer(PNG_IMAGE_SIZE(*image.mutable_data()));\n\nSAPI Array provides all methods outlined in [SAPI Pointers](#sapi_pointers) to\nobtain a `::sapi::v::Ptr` object that can be used for sandboxed library calls."]]