This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
wasm-micro-runtime/samples/file
YAMAMOTO Takashi 654ac5feca
Use cmake POSITION_INDEPENDENT_CODE instead of hardcoding -pie -fPIE (#1598)
This fixes unused option warnings on -pie for macOS.
(On macOS cmake produces "-fPIE -Xlinker -pie")

Bump required cmake version to 3.14 for CheckPIESupported.

References:
https://cmake.org/cmake/help/latest/prop_tgt/POSITION_INDEPENDENT_CODE.html
https://cmake.org/cmake/help/latest/module/CheckPIESupported.html#module:CheckPIESupported
2022-10-19 01:51:18 +08:00
..
src Use cmake POSITION_INDEPENDENT_CODE instead of hardcoding -pie -fPIE (#1598) 2022-10-19 01:51:18 +08:00
wasm-app linux-sgx: Implement SGX IPFS as POSIX backend for file interaction (#1489) 2022-09-28 13:09:58 +08:00
CMakeLists.txt linux-sgx: Implement SGX IPFS as POSIX backend for file interaction (#1489) 2022-09-28 13:09:58 +08:00
README.md linux-sgx: Implement SGX IPFS as POSIX backend for file interaction (#1489) 2022-09-28 13:09:58 +08:00

"file" sample introduction

This sample demonstrates the supported file interaction API of WASI. This sample can also demonstrate the SGX IPFS (Intel Protected File System), enabling an enclave to seal and unseal data at rest.

Preparation

Please install WASI SDK, download the wasi-sdk release and extract the archive to default path /opt/wasi-sdk. For testing with SGX IPFS, follow the instructions in the documentation of SGX for WAMR.

Build the sample

mkdir build
cd build
cmake ..
make

The WebAssembly application is the file located at wasm-app/file.wasm.

Run workload

Either use iwasm-sample for Linux, or enclave-sample for Intel SGX to run the sample, with the argument to allow the file system interaction with the current folder (--dir=.).

The output with Linux and POSIX is like:

Opening a file..
[Test] File opening passed.
Writing to the file..
[Test] File writing passed.
Moving the cursor to the start of the file..
Reading from the file, up to 1000 characters..
Text read: Hello, world!
[Test] File reading passed.
Determine whether we reach the end of the file..
Is the end of file? 1
[Test] End of file detection passed.
Getting the plaintext size..
The plaintext size is 13.
[Test] Retrieving file offset passed.
Force actual write of all the cached data to the disk..
[Test] Retrieving file offset passed.
Writing 5 characters at offset 7..
File current offset: 13
[Test] Writing at specified offset passed.
Reading 5 characters at offset 7..
Text read: James
File current offset: 13
[Test] Reading at specified offset passed.
Allocate more space to the file..
File current offset: 13
Moving to the end..
File current offset: 23
[Test] Allocation or more space passed.
Extend the file size of 10 bytes using ftruncate..
File current offset: 23
Moving to the end..
File current offset: 33
[Test] Extension of the file size passed.
Closing from the file..
[Test] Closing file passed.
Getting the size of the file on disk..
The file size is 33.
All the tests passed!

The output with SGX and IPFS is like:

Opening a file..
[Test] File opening passed.
Writing to the file..
[Test] File writing passed.
Moving the cursor to the start of the file..
Reading from the file, up to 1000 characters..
Text read: Hello, world!
[Test] File reading passed.
Determine whether we reach the end of the file..
Is the end of file? 1
[Test] End of file detection passed.
Getting the plaintext size..
The plaintext size is 13.
[Test] Retrieving file offset passed.
Force actual write of all the cached data to the disk..
[Test] Retrieving file offset passed.
Writing 5 characters at offset 7..
File current offset: 13
[Test] Writing at specified offset passed.
Reading 5 characters at offset 7..
Text read: James
File current offset: 13
[Test] Reading at specified offset passed.
Allocate more space to the file..
File current offset: 23
Moving to the end..
File current offset: 23
[Test] Allocation or more space passed.
Extend the file size of 10 bytes using ftruncate..
File current offset: 23
Moving to the end..
File current offset: 33
[Test] Extension of the file size passed.
Closing from the file..
[Test] Closing file passed.
Getting the size of the file on disk..
The file size is 4096.
All the tests passed!

For SGX IPFS, refer to SGX Intel Protected File System for more details.