sandboxed-api/oss-internship-2020/libuv/README.md

94 lines
3.3 KiB
Markdown
Raw Normal View History

2020-09-22 22:41:06 +08:00
# LibUV Sandbox
2020-09-25 23:06:24 +08:00
This library is a sandboxed version of [LibUV](https://libuv.org/), implemented
using Sandboxed API.
2020-09-22 22:41:06 +08:00
## Setup
The repository can be cloned using:
```
git clone --recursive [URL to this repo]
```
2020-09-25 23:06:24 +08:00
The `--recursive` flag ensures that submodules are also cloned.
2020-09-22 22:41:06 +08:00
2020-09-25 23:06:24 +08:00
Alternatively, if the repository has already been cloned but the submodules have
not, these can be cloned using:
2020-09-22 22:41:06 +08:00
```
git submodule update --init --recursive
2020-09-25 23:06:24 +08:00
```
2020-09-22 22:41:06 +08:00
2020-09-25 23:06:24 +08:00
The full list of Sandboxed API dependencies can be found on
[Sandboxed API Getting Started page](https://developers.google.com/code-sandboxing/sandboxed-api/getting-started).
2020-09-22 22:41:06 +08:00
2020-09-25 23:06:24 +08:00
The following commands, used from the current `libuv/` directory, build the
library:
2020-09-22 22:41:06 +08:00
```
mkdir -p build
cd build
cmake .. -G Ninja -D SAPI_ROOT=[path to sandboxed-api]
cmake --build .
```
## Implementation details
2020-09-25 23:06:24 +08:00
LibUV can't be directly sandboxed by Sandboxed API. Because of the size and
complexity of the library, doing so creates some compilation errors and does not
create the correct APIs for all methods. The solution for this issue is creating
a wrapper library, whose methods (which can be sandboxed properly) internally
call the original LibUV's methods.
2020-09-22 22:41:06 +08:00
2020-09-25 23:06:24 +08:00
Using these wrapper methods is extremely simple, the only relevant difference is
that they have a `sapi_` prefix before their names (e.g. `uv_loop_init` becomes
`sapi_uv_loop_init`). The only exception is the variadic method
`uv_loop_configure`. This has two wrappers, `sapi_uv_loop_configure` (with no
additional parameters) and `sapi_uv_loop_configure_int` (with an additional
`int` parameter). Currently, these are the only valid calls to the method in
LibUV.
2020-09-22 22:41:06 +08:00
#### Wrapper generator
2020-09-25 23:06:24 +08:00
The wrapper is generated automatically by a Python script that is called by
CMake at build time. This script can be found in the `generator` folder.
2020-09-22 22:41:06 +08:00
#### Function pointers
2020-09-25 23:06:24 +08:00
The functions whose pointers will be passed to the library's methods
(*callbacks*) can't be implemented in the files making use of the library, but
must be in other files. These files must be compiled together with the library,
and this is done by adding their absolute path to the CMake variable
`SAPI_UV_CALLBACKS`.
2020-09-22 22:41:06 +08:00
2020-09-25 23:06:24 +08:00
The pointers can then be obtained using an `RPCChannel` object, as shown in the
example `idle-basic.cc`.
2020-09-22 22:41:06 +08:00
## Examples
2020-09-25 23:06:24 +08:00
The `examples` directory contains the sandboxed versions of example source codes
taken from from [LibUV's User Guide](https://docs.libuv.org/en/v1.x/guide.html).
More information about each example can be found in the examples'
[README](examples/README.md).
2020-09-22 22:41:06 +08:00
2020-09-25 23:06:24 +08:00
To build these examples when building the library, the CMake variable
`SAPI_UV_ENABLE_EXAMPLES` must be set to `ON`. This enables Sandboxed API
examples as well.
2020-09-22 22:41:06 +08:00
## Testing
The `tests` folder contains some test cases created using Google Test.
2020-09-25 23:06:24 +08:00
To build these tests when building the library, the CMake variable
`SAPI_UV_ENABLE_TESTS` must be set to `ON`. This enables Sandboxed API tests as
well.
2020-09-22 22:41:06 +08:00
## Policies
2020-09-25 23:06:24 +08:00
Each example and test has an ad-hoc policy implemented on its own file. These
policies can be used as references for pieces of code that perform similar
operations. For example, the `helloworld.cc` example has a policy that only
allows the strictly necessary syscalls for running a loop.
2020-09-22 22:41:06 +08:00
## Callbacks
2020-09-25 23:06:24 +08:00
The `callbacks.h` and `callbacks.cc` files in the `callbacks` folder implement
all the callbacks used by examples and tests.