• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

app/25-Apr-2025-7124

README.mdD25-Apr-20255.2 KiB11375

lib.rsD25-Apr-20259.5 KiB243153

manifest-scudo.jsonD25-Apr-2025158 76

manifest.jsonD25-Apr-2025158 76

rules.mkD25-Apr-2025981 3713

README.md

1# Hello World App in Rust
2
3This is a rewrite of Trusty's [Hello World App](https://android.googlesource.com/trusty/app/sample/+/88c0b5406c6615e0945239700fd0b0afbc0de278/hello-world)
4in Rust. It has the functionality of the original `app` and `test-app` TAs written in C, but is structured differently. It's recommended that you're familiar with the original app, as this README will reference the original.
5
6## Download the code
7
8- In the gerrit UI, on the top right corner, select Download Patch, then cherry-pick.
9
10```{admonition} Tip
11You can download the original TA as well, if you want to see the rust TA communicate with the original C `test-app` TA.
12```
13
14You should have a `trusty/user/app/sample/rust-hello-world` directory under your root.
15
16## Directory Structure
17
18The structure of this directory is significantly different. There are two different TAs here, a library(named `rust_hello_world_lib` in its manifest), and the app(named `rust_hello_world_app`). There is only one subdirectory, as the app is contained within the library directory.
19
20
21```
22sample
23|-- rust-hello-world
24|-- |-- app
25|   |-- |-- main.rs
26|   |-- |-- manifest.json
27|   |-- |-- rules.mk
28|-- |-- lib.rs
29|-- |-- README.md
30|-- |-- rules.mk
31|-- usertests-inc.mk
32`
33```
34
35* `rust-hello-world/` - This contains all the source code, and the manifest and makefile for the library TA:
36
37    -   `lib.rs` - This file contains the functionality of the original `app`. It sets up the event loop and exposes the port
38        for other trusted apps to connect onto. In addition, it contains the unit tests that can be run to emulate `test-app`.
39
40    -   `manifest.json` - This file specifies the configuration options for the library app.
41
42    -   `rules.mk` - This is the makefile for the library app. In order to run our unit tests in Rust, the configuration is
43different.
44        * We include `make/library.mk` instead of `make/trusted_app.mk`.
45        * `MODULE_RUST_TESTS := true` allows us to run tests in this TA.
46        * `MODULE_CRATE_NAME := hello_world_in_rust` specifies the crate name, also specifying the port to connect for tests.
47
48* `rust-hello-world/app` - The app directory contains a separated manifest and makefile for the app TA. In addition, the source file for the app TA, `main.rs`, simply calls into `lib.rs` to start the event loop.
49
50    -   `main.rs` - This simply contains the `main` function that runs on app startup, which calls into `lib.rs`.
51    -   `manifest.json` - This manifest specifices the configuration options for the app itself.
52    -   `rules.mk` - The makefile for the app.
53
54## Reading the Source
55
56Like the original, the best idea is to start reading the source code itself.
57
58`main.rs` contains just a simple call to the functions in `lib.rs`, which contains all the functionality.
59
60In `lib.rs`, there are two core traits that are implemented:
61* Serialization/Deserialization - the logic for turning incoming IPC messages into a string to process, and the logic for turning a string into an IPC message to output.
62* Service - The logic that handles an incoming connection, and incoming message. The `on_connect` and `on_message` methods are called whenever connections and messages are sent.
63
64The source code has comments that explain the traits in more detail. Once you have these implementations, you can start up a Manager in `init_and_start_loop()`. The manager automatically dispatches connections and messages to our Service implementation.
65
66In Rust, the unit tests are included in the same file as the source code. This is reflected in `lib.rs`, on the bottom of the file.
67
68## Building and Running the Test
69
70There are two ways to exercise the TA.
711. You can use the unit tests in `lib.rs`.
722. You can actually use the original `test-app` in C to exercise the Rust TA.
73
74Let's start with the unit test approach.
75
76First, you need to build the code. (You don't need to include the rust TA in `TRUSTY_BUILTIN_USER_TASKS` first)
77
78```
79$ ./trusty/vendor/google/aosp/scripts/build.py --jobs=$(nproc) --skip-tests qemu-generic-arm64-test-debug
80```
81
82Next, you run a boot test, with the crate name that was provided in the library TA's `manifest.json` ( `hello_world_in_rust` ). We prepend `com.android.trusty.rust.` and append `.test` to this crate name. Our boot test will look like this:
83
84```
85$ ./build-root/build-qemu-generic-arm64-test-debug/run     --headless --boot-test "com.android.trusty.rust.hello_world_in_rust.test" --verbose
86```
87
88It should run two tests, and pass both!
89
90This approach uses the library TA solely. If you were curious about exercising the app TA, then you can try the second approach. You'll need to have the original CL downloaded.
91
92Once you've done so, don't include the original C `app` in `TRUSTY_BUILTIN_USER_TASKS`. We want to include the rust app TA instead.
93
94```
95TRUSTY_BUILTIN_USER_TASKS := \
96    ...
97	trusty/user/app/sample/rust-hello-world/app \
98```
99
100After including this line, rebuild the QEMU target.
101
102```
103$ ./trusty/vendor/google/aosp/scripts/build.py --jobs=$(nproc) --skip-tests qemu-generic-arm64-test-debug
104```
105
106Now, you should be able to run the boot test. This will use the unit test in the original C `test-app`, which sets up a connection to our rust app TA.
107
108```
109$ ./build-root/build-qemu-generic-arm64-test-debug/run \
110    --headless --boot-test "com.android.trusty.hello.test"
111```
112
113