# Building¶

Gramine consists of several components:

• The Library OS itself (a shared library named libsysdb.so)
• The Platform Adaptation Layer, or PAL (a shared library named libpal.so)
• A patched C Library (shared library libc.so and possibly others). Currently there are two options: musl and GNU C Library (glibc).

The build of Gramine implies building at least the first two components. The build of the patched C library is optional but highly recommended for performance reasons. Both patched glibc and patched musl are built by default.

Gramine currently only works on the x86_64 architecture. Gramine is currently tested on Ubuntu 18.04/20.04, along with Linux kernel version 5.x. We recommend building and installing Gramine on Ubuntu with Linux kernel version 5.11 or higher. If you find problems with Gramine on other Linux distributions, please contact us with a detailed bug report.

## Installing dependencies¶

### Common dependencies¶

Run the following command on Ubuntu LTS to install dependencies:

sudo apt-get install -y build-essential \
autoconf bison gawk nasm ninja-build python3 python3-click \
python3-jinja2 python3-pyelftools wget
sudo python3 -m pip install 'meson>=0.56' 'toml>=0.10'


You can also install Meson and python3-toml from apt instead of pip, but only if your distro is new enough to have Meson >= 0.56 and python3-toml >= 0.10 (Debian 11, Ubuntu 20.10).

For GDB support and to run all tests locally you also need to install:

sudo apt-get install -y libunwind8 musl-tools python3-pytest


If you want to build the patched libgomp library, you also need to install GCC’s build dependencies:

sudo apt-get install -y libgmp-dev libmpfr-dev libmpc-dev libisl-dev


### Dependencies for SGX¶

The build of Gramine with SGX support requires the corresponding SGX software infrastructure to be installed on the system. In particular, the FSGSBASE functionality must be enabled in the Linux kernel, the Intel SGX driver must be running, and Intel SGX SDK/PSW/DCAP must be installed.

Note

We recommend to use Linux kernel version 5.11 or higher: starting from this version, Linux has the FSGSBASE functionality as well as the Intel SGX driver built-in. If you have Linux 5.11+, skip steps 2 and 3.

#### 1. Required packages¶

Run the following commands on Ubuntu to install SGX-related dependencies:

sudo apt-get install -y libcurl4-openssl-dev \
libprotobuf-c-dev protobuf-c-compiler protobuf-compiler \
python3-cryptography python3-pip python3-protobuf


#### 2. Install Linux kernel with patched FSGSBASE¶

FSGSBASE is a feature in recent processors which allows direct access to the FS and GS segment base addresses. For more information about FSGSBASE and its benefits, see this discussion. Note that if your kernel version is 5.9 or higher, then the FSGSBASE feature is already supported and you can skip this step. Kernel version can be checked using the following command:

uname -r


If your current kernel version is lower than 5.9, then you have two options:

• Update the Linux kernel to at least 5.9 in your OS distro. If you use Ubuntu, you can follow e.g. this tutorial.
• Use our provided patches to the Linux kernel version 5.4. See section Prepare a signing key for the exact steps.

#### 3. Install the Intel SGX driver¶

This step depends on your hardware and kernel version. Note that if your kernel version is 5.11 or higher, then the Intel SGX driver is already installed and you can skip this step.

If you have an older CPU without FLC support, you need to download and install the the following out-of-tree (OOT) Intel SGX driver:

For this driver, you need to set vm.mmap_min_addr=0 in the system (only required for the legacy SGX driver and not needed for newer DCAP/in-kernel drivers):

sudo sysctl vm.mmap_min_addr=0


Note that this is an inadvisable configuration for production systems.

Alternatively, if your CPU supports FLC, you can choose to install the DCAP version of the Intel SGX driver from:

#### 4. Install Intel SGX SDK/PSW¶

Follow the installation instructions from the “Intel SGX Software Installation Guide” version 1.14 (the latest version as of July 2022):

In general, various documentation for Intel SGX SDK/PSW can be found here:

Additional information, package descriptions, etc. can be found in the official “Intel SGX for Linux” GitHub repo:

#### 5. Install dependencies for DCAP¶

If you plan on enabling -Ddcap option, you need to install libsgx-dcap-quote-verify package (and it’s development counterpart):

curl -fsSL https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | sudo apt-key add -
# (if you're on Ubuntu 18.04, write "bionic" instead of "focal" above)

sudo apt-get update
sudo apt-get install libsgx-dcap-quote-verify-dev


## Building¶

In order to build Gramine, you need to first set up the build directory. In the root directory of Gramine repo, run the following command (recall that “direct” means non-SGX version):

meson setup build/ --buildtype=release -Ddirect=enabled -Dsgx=enabled \
-Dsgx_driver=<driver> -Dsgx_driver_include_path=<path-to-sgx-driver-sources>


Note

If you plan to contribute changes to Gramine, then you should always build it with --werror added to the invocation above.

Note

If you invoked meson setup once, the next invocation of this command will not have any effect. Instead, to change the build configuration, use meson configure. For example, if you built with meson setup build/ -Dsgx=disabled first and now want to enable SGX, type meson configure build/ -Dsgx=enabled.

Then, build and install Gramine by running the following:

ninja -C build/
sudo ninja -C build/ install


Set -Ddirect= and -Dsgx= options to enabled or disabled according to whether you built the corresponding PAL (the snippet assumes you built both).

The -Dsgx_driver parameter controls which SGX driver to use:

• upstream (default) for upstreamed in-kernel driver (mainline Linux kernel 5.11+),
• dcap1.6 for Intel DCAP version 1.6 or higher, but below 1.10,
• dcap1.10 for Intel DCAP version 1.10 or higher,
• oot for non-DCAP, out-of-tree version of the driver.

The -Dsgx_driver_include_path parameter must point to the absolute path where the SGX driver was downloaded or installed in the previous step. For example, for the DCAP version 1.41 of the SGX driver, you must specify -Dsgx_driver_include_path="/usr/src/sgx-1.41/include/". If this parameter is omitted, Gramine’s build system will try to determine the right path.

Note

When installing from sources, Gramine executables are placed under /usr/local/bin. Some Linux distributions (notably CentOS) do not search for executables under this path. If your system reports that Gramine programs can not be found, you might need to edit your configuration files so that /usr/local/bin is in your path (in PATH environment variable).

Set -Dglibc= or -Dmusl= options to disabled if you wish not to build the support for any (they are both built by default).

• To build test binaries, run meson -Dtests=enabled. This is necessary if you will be running regression tests. See Contributing to Gramine for details.

• In order to run SGX tools with DCAP version of RA-TLS library (ra_tls_verify_dcap.so), build with meson -Ddcap=enabled option. See RA-TLS example’s README.

Note

EPID version of RA-TLS library (ra_tls_verify_epid.so) is built by default.

• To create a debug build, run meson --buildtype=debug. This adds debug symbols in all Gramine components, builds them without optimizations, and enables detailed debug logs in Gramine.

Warning

Debug builds are not suitable for production.

• To create a debug build that does not disable optimizations, run meson --buildtype=debugoptimized.

Warning

Debug builds are not suitable for production.

Note

This is generally not recommended, because optimized builds lose some debugging information, and may cause GDB to display confusing tracebacks or garbage data. You should use DEBUGOPT=1 only if you have a good reason (e.g. for profiling).

• To compile with undefined behavior sanitization (UBSan), run meson -Dubsan=enabled. This causes Gramine to abort when undefined behavior is detected (and display information about source line). UBSan can be enabled for both debug and non-debug builds.

Warning

UBSan builds (even non-debug) are not suitable for production.

• To compile with address sanitization (ASan), run meson -Dasan=enabled. In this mode, Gramine will attempt to detect invalid memory accesses. ASan can be enabled for both debug and non-debug builds.

ASan is supported only when compiling with Clang (before building, set the appropriate environment variables with export CC=clang CXX=clang++ AS=clang).

Warning

ASan builds (even non-debug) are not suitable for production.

• To build with -Werror, run meson --werror.

• To install into some other place than /usr/local, use meson --prefix=<prefix>. Note that if you chose something else than /usr then for things to work, you probably need to adjust several environment variables:

$PATH <prefix>/bin POSIX.1-2018 8.3 $PYTHONPATH <prefix>/lib/python<version>/site-packages python3(1)