Test for prebuilt libraries

Simple test that links to a prebuilt .a file and makes sure that the
library can be called into.

This will help us prevent regressions and potentially detect if we break
old prebuilts.

Bug: 172612841
Change-Id: I75413f2fb5aed5e18e9f32cd272be7296c4c9bb7
diff --git a/prebuilts-test/lib/foo.c b/prebuilts-test/lib/foo.c
new file mode 100644
index 0000000..a78c5b6
--- /dev/null
+++ b/prebuilts-test/lib/foo.c
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+
+#include <foo.h>
+#include <string.h>
+#include <uapi/err.h>
+
+const char* foo = "foo";
+
+int get_foo(void* buf, size_t len) {
+    size_t foo_len = strlen(foo);
+    if (len < foo_len) {
+        return ERR_BAD_LEN;
+    }
+
+    strncpy(buf, foo, foo_len);
+
+    return NO_ERROR;
+}
diff --git a/prebuilts-test/lib/include/foo.h b/prebuilts-test/lib/include/foo.h
new file mode 100644
index 0000000..8cde265
--- /dev/null
+++ b/prebuilts-test/lib/include/foo.h
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <lk/compiler.h>
+#include <stddef.h>
+
+__BEGIN_CDECLS
+
+extern const char* foo;
+
+/**
+ * get_foo() - write string @foo into given buffer
+ * @buf: buffer to place the result in
+ * @len: length of @buf
+ *
+ * Return: 0 on success, negative error code on error
+ */
+int get_foo(void* buf, size_t len);
+
+__END_CDECLS
diff --git a/prebuilts-test/lib/rules.mk b/prebuilts-test/lib/rules.mk
new file mode 100644
index 0000000..a852f99
--- /dev/null
+++ b/prebuilts-test/lib/rules.mk
@@ -0,0 +1,25 @@
+# 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)
+
+MODULE_SRCS += \
+	$(LOCAL_DIR)/foo.c \
+
+MODULE_DEPS += \
+	trusty/user/base/lib/libc-trusty \
+
+include make/module.mk
diff --git a/prebuilts-test/main.c b/prebuilts-test/main.c
new file mode 100644
index 0000000..a900aac
--- /dev/null
+++ b/prebuilts-test/main.c
@@ -0,0 +1,36 @@
+/*
+ * 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 "prebuilts-test"
+
+#include <foo.h>
+#include <string.h>
+#include <trusty_unittest.h>
+
+TEST(prebuilts, basic) {
+    int rc;
+    char str[16];
+
+    rc = get_foo(str, sizeof(str));
+    ASSERT_GE(rc, 0);
+
+    rc = strcmp(str, foo);
+    ASSERT_GE(rc, 0);
+
+test_abort:;
+}
+
+PORT_TEST(prebuilts, "com.android.trusty.prebuilts.test")
diff --git a/prebuilts-test/manifest.json b/prebuilts-test/manifest.json
new file mode 100644
index 0000000..c3f384a
--- /dev/null
+++ b/prebuilts-test/manifest.json
@@ -0,0 +1,5 @@
+{
+    "uuid": "e1e77496-cb6a-4a91-b765-eb3e4467c7ae",
+    "min_heap": 4096,
+    "min_stack": 4096
+}
diff --git a/prebuilts-test/prebuilts/arch/arm64/lib.mod.a b/prebuilts-test/prebuilts/arch/arm64/lib.mod.a
new file mode 100644
index 0000000..c536085
--- /dev/null
+++ b/prebuilts-test/prebuilts/arch/arm64/lib.mod.a
Binary files differ
diff --git a/prebuilts-test/prebuilts/include/foo.h b/prebuilts-test/prebuilts/include/foo.h
new file mode 100644
index 0000000..8cde265
--- /dev/null
+++ b/prebuilts-test/prebuilts/include/foo.h
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <lk/compiler.h>
+#include <stddef.h>
+
+__BEGIN_CDECLS
+
+extern const char* foo;
+
+/**
+ * get_foo() - write string @foo into given buffer
+ * @buf: buffer to place the result in
+ * @len: length of @buf
+ *
+ * Return: 0 on success, negative error code on error
+ */
+int get_foo(void* buf, size_t len);
+
+__END_CDECLS
diff --git a/prebuilts-test/rules.mk b/prebuilts-test/rules.mk
new file mode 100644
index 0000000..e67523b
--- /dev/null
+++ b/prebuilts-test/rules.mk
@@ -0,0 +1,56 @@
+# 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.
+
+ifneq ($(TRUSTY_USER_ARCH),arm64)
+$(error Only arm64 is supported.)
+endif
+
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MANIFEST := $(LOCAL_DIR)/manifest.json
+
+MODULE_SRCS += \
+	$(LOCAL_DIR)/main.c \
+
+MODULE_DEPS += \
+	trusty/user/base/lib/unittest \
+
+#BUILD_FROM_SOURCE := true
+
+ifeq (true,$(call TOBOOL,$(BUILD_FROM_SOURCE)))
+# Build library from source.
+MODULE_DEPS += \
+	$(LOCAL_DIR)/lib \
+
+else
+# Include headers needed to call into prebuilt library.
+MODULE_INCLUDES += \
+	$(LOCAL_DIR)/prebuilts/include \
+
+# Link in the prebuilt library. This prebuilt was built from source in lib/.
+# To generate it, set BUILD_FROM_SOURCE variable above. The archive can then be
+# found at:
+# build-root/build-<build_target>/user_tasks/<path_to_app>/<path_to_lib>.mod.a
+MODULE_EXTRA_ARCHIVES += \
+	$(LOCAL_DIR)/prebuilts/arch/$(ARCH)/lib.mod.a \
+
+# Also link in dependencies of the prebuilt.
+MODULE_DEPS += \
+	trusty/user/base/lib/libc-trusty \
+
+endif
+
+include make/module.mk
diff --git a/usertests-inc.mk b/usertests-inc.mk
index 61ffd52..44eaf6d 100644
--- a/usertests-inc.mk
+++ b/usertests-inc.mk
@@ -28,4 +28,10 @@
 	trusty/user/app/sample/spi/swspi-srv \
 	trusty/user/app/sample/spi/swspi-test \
 
+ifeq ($(TRUSTY_USER_ARCH),arm64)
+TRUSTY_ALL_USER_TASKS += \
+	trusty/user/app/sample/prebuilts-test \
+
+endif
+
 TRUSTY_LOADABLE_USER_TASKS += \