Add receiver TA to support send-fd test

The receiver app accepts an fd from nonsecure, and writes "Hello from
Trusty!" into it.

Bug: 117221195
Test: tipc-test -t send-fd
Change-Id: I52fe647b5e429ff5fc32ac220d778a4e71c56dd4
diff --git a/memref-test/receiver/manifest.json b/memref-test/receiver/manifest.json
new file mode 100644
index 0000000..8492efa
--- /dev/null
+++ b/memref-test/receiver/manifest.json
@@ -0,0 +1,14 @@
+{
+    "uuid": "cf31780c-aa93-11ea-8cfd-ab8c0bdee618",
+    "min_heap": 4096,
+    "min_stack": 8192,
+    "start_ports": [
+        {
+            "name": "com.android.trusty.memref.receiver",
+            "flags": {
+                "allow_ta_connect": false,
+                "allow_ns_connect": true
+            }
+        }
+    ]
+}
diff --git a/memref-test/receiver/receiver.c b/memref-test/receiver/receiver.c
new file mode 100644
index 0000000..7ad3bf4
--- /dev/null
+++ b/memref-test/receiver/receiver.c
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define TLOG_TAG "receiver"
+
+#define TLOG_LVL TLOG_LVL_DEBUG
+
+#include <assert.h>
+
+#include <inttypes.h>
+#include <lk/err_ptr.h>
+#include <lk/macros.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/auxv.h>
+
+#include <lib/tipc/tipc.h>
+#include <lib/tipc/tipc_srv.h>
+#include <lib/unittest/unittest.h>
+
+#include <sys/mman.h>
+#include <trusty_unittest.h>
+
+static struct tipc_port_acl receiver_port_acl = {
+        .flags = IPC_PORT_ALLOW_NS_CONNECT,
+        .uuid_num = 0,
+        .uuids = NULL,
+        .extra_data = NULL,
+};
+
+static struct tipc_port receiver_port = {
+        .name = "com.android.trusty.memref.receiver",
+        .msg_max_size = 1,
+        .msg_queue_len = 1,
+        .acl = &receiver_port_acl,
+        .priv = NULL,
+};
+
+static int receiver_on_message(const struct tipc_port* port,
+                               handle_t chan,
+                               void* ctx) {
+    assert(port == &receiver_port);
+    assert(ctx == NULL);
+    handle_t handle;
+    int rc;
+
+    struct ipc_msg msg = {
+            .iov = NULL,
+            .num_iov = 0,
+            .handles = &handle,
+            .num_handles = 1,
+    };
+
+    struct ipc_msg_info msg_inf;
+    rc = get_msg(chan, &msg_inf);
+    if (rc) {
+        return rc;
+    }
+
+    if (msg_inf.num_handles != 1) {
+        TLOGE("Message had no handles\n");
+        return -1;
+    }
+
+    rc = read_msg(chan, msg_inf.id, 0, &msg);
+    put_msg(chan, msg_inf.id);
+    if (rc < 0) {
+        TLOGE("Failed to read message\n");
+        return rc;
+    }
+
+    size_t page_size = getauxval(AT_PAGESZ);
+
+    char* out = mmap(0, page_size, PROT_READ | PROT_WRITE, 0, handle, 0);
+    if (out == MAP_FAILED) {
+        rc = (intptr_t)out;
+        TLOGE("Failed to mmap handle\n");
+        return rc;
+    }
+
+    strcpy(out, "Hello from Trusty!");
+
+    munmap((void*)out, page_size);
+
+    close(handle);
+
+    // Send a message for sync
+    char c = 0;
+    tipc_send1(chan, &c, 1);
+
+    return rc;
+}
+
+static struct tipc_srv_ops receiver_ops = {
+        .on_message = receiver_on_message,
+};
+
+int main(void) {
+    struct tipc_hset* hset = tipc_hset_create();
+
+    if (IS_ERR(hset)) {
+        return PTR_ERR(hset);
+    }
+
+    int rc = tipc_add_service(hset, &receiver_port, 1, 1, &receiver_ops);
+    if (rc < 0) {
+        return rc;
+    }
+
+    rc = tipc_run_event_loop(hset);
+    TLOGE("receiver going down: (%d)\n", rc);
+    return rc;
+}
diff --git a/memref-test/receiver/rules.mk b/memref-test/receiver/rules.mk
new file mode 100644
index 0000000..c25f1fc
--- /dev/null
+++ b/memref-test/receiver/rules.mk
@@ -0,0 +1,30 @@
+# Copyright (C) 2020 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MANIFEST := $(LOCAL_DIR)/manifest.json
+
+MODULE_SRCS += \
+	$(LOCAL_DIR)/receiver.c \
+
+MODULE_DEPS += \
+	trusty/user/base/lib/libc-trusty \
+	trusty/user/base/lib/tipc \
+	trusty/user/base/lib/unittest \
+
+include make/module.mk
diff --git a/usertests-inc.mk b/usertests-inc.mk
index 6622751..61ffd52 100644
--- a/usertests-inc.mk
+++ b/usertests-inc.mk
@@ -23,6 +23,7 @@
 	trusty/user/app/sample/manifest-test \
 	trusty/user/app/sample/memref-test \
 	trusty/user/app/sample/memref-test/lender \
+	trusty/user/app/sample/memref-test/receiver \
 	trusty/user/app/sample/timer \
 	trusty/user/app/sample/spi/swspi-srv \
 	trusty/user/app/sample/spi/swspi-test \