From 3e81b4291254e8130c64d129430478ce11e1bb0e Mon Sep 17 00:00:00 2001 From: LI Qing Date: Thu, 13 Feb 2020 03:54:37 +0000 Subject: [PATCH] Add SQLite demo --- demos/README.md | 1 + demos/sqlite/.gitignore | 3 ++ demos/sqlite/README.md | 19 +++++++++ demos/sqlite/download_and_build_sqlite.sh | 18 ++++++++ demos/sqlite/run_sqlite_on_occlum.sh | 31 ++++++++++++++ demos/sqlite/sqlite_demo.c | 50 +++++++++++++++++++++++ 6 files changed, 122 insertions(+) create mode 100644 demos/sqlite/.gitignore create mode 100644 demos/sqlite/README.md create mode 100755 demos/sqlite/download_and_build_sqlite.sh create mode 100755 demos/sqlite/run_sqlite_on_occlum.sh create mode 100644 demos/sqlite/sqlite_demo.c diff --git a/demos/README.md b/demos/README.md index b3da3869..7ae6958c 100644 --- a/demos/README.md +++ b/demos/README.md @@ -18,6 +18,7 @@ This set of demos shows how real-world apps can be easily run inside SGX enclave * `grpc/`: A client and server communicating through [gRPC](https://grpc.io/). * `openvino/` A benchmark of [OpenVINO Inference Engine](https://docs.openvinotoolkit.org/2019_R3/_docs_IE_DG_inference_engine_intro.html). * `python` A demo of [Python](https://www.python.org). +* `sqlite` A demo of [SQLite](https://www.sqlite.org) SQL database engine. * `tensorflow_lite/`: A demo and benchmark of [Tensorflow Lite](https://www.tensorflow.org/lite) inference engine. * `xgboost/`: A demo of [XGBoost](https://xgboost.readthedocs.io/en/latest/). diff --git a/demos/sqlite/.gitignore b/demos/sqlite/.gitignore new file mode 100644 index 00000000..30a1b8a3 --- /dev/null +++ b/demos/sqlite/.gitignore @@ -0,0 +1,3 @@ +sqlite_src/ +occlum_context/ +sqlite_demo diff --git a/demos/sqlite/README.md b/demos/sqlite/README.md new file mode 100644 index 00000000..8c7ba387 --- /dev/null +++ b/demos/sqlite/README.md @@ -0,0 +1,19 @@ +# Use SQLite in SGX with Occlum + +This project demonstrates how Occlum enables [SQLite](https://www.sqlite.org) in SGX enclaves. + +Step 1: Download SQLite and build the demo program +``` +./download_and_build_sqlite.sh +``` +When completed, the demo program (i.e., `sqlite_demo`) is generated. + +Step 2: Run the SQLite demo program inside SGX enclave with Occlum +``` +./run_sqlite_on_occlum.sh +``` + +Step 3 (Optional): Run the SQLite demo program in Linux +``` +./sqlite_demo +``` diff --git a/demos/sqlite/download_and_build_sqlite.sh b/demos/sqlite/download_and_build_sqlite.sh new file mode 100755 index 00000000..21e6d7a2 --- /dev/null +++ b/demos/sqlite/download_and_build_sqlite.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -e + +SQLITE=sqlite-autoconf-3310100 +SQLITE_SRC=sqlite_src +DEMO=sqlite_demo + +# Download SQLite source files +[ ! -d $SQLITE_SRC ] && rm -f $SQLITE.tar.gz && \ + wget http://www3.sqlite.org/2020/$SQLITE.tar.gz \ + && rm -rf $SQLITE && tar xf $SQLITE.tar.gz \ + && mv $SQLITE $SQLITE_SRC \ + && rm -f $SQLITE.tar.gz + +[ -e $DEMO ] && rm -f $DEMO +echo -e "Starting to build $DEMO ..." +occlum-gcc -O2 -I$SQLITE_SRC sqlite_demo.c $SQLITE_SRC/sqlite3.c -lpthread -ldl -o $DEMO +echo -e "Build $DEMO succeed" diff --git a/demos/sqlite/run_sqlite_on_occlum.sh b/demos/sqlite/run_sqlite_on_occlum.sh new file mode 100755 index 00000000..5d4495b1 --- /dev/null +++ b/demos/sqlite/run_sqlite_on_occlum.sh @@ -0,0 +1,31 @@ +#!/bin/bash +set -e + +DEMO=sqlite_demo +SQL_DB=/root/company.db +SQL_STMT="CREATE TABLE COMPANY ( \ + ID INT PRIMARY KEY NOT NULL, \ + NAME TEXT NOT NULL, \ + AGE INT NOT NULL, \ + ADDRESS CHAR(50), \ + SALARY REAL ); \ + INSERT INTO COMPANY VALUES ( 1, 'Kris', 27, 'California', 16000.00 ); \ + SELECT * FROM COMPANY;" + +if [ ! -e $DEMO ];then + echo "Error: cannot stat '$DEMO'" + echo "Please see README and build the $DEMO" + exit 1 +fi + +# 1. Init Occlum Workspace +rm -rf occlum_context && mkdir occlum_context +cd occlum_context +occlum init + +# 2. Copy files into Occlum Workspace and build +cp ../$DEMO image/bin +occlum build + +# 3. Run the demo +occlum run /bin/$DEMO "$SQL_DB" "$SQL_STMT" diff --git a/demos/sqlite/sqlite_demo.c b/demos/sqlite/sqlite_demo.c new file mode 100644 index 00000000..0c891ed3 --- /dev/null +++ b/demos/sqlite/sqlite_demo.c @@ -0,0 +1,50 @@ +/* + * Below is a simple C program that demonstrates how to use the C/C++ interface to SQLite. + * The name of a database is given by the first argument and the second argument is one + * or more SQL statements to execute against the database. + * + * This file is based on the sample code in https://www.sqlite.org/quickstart.html + */ + +#include +#include + +static int callback(void *NotUsed, int argc, char **argv, char **azColName) { + int i; + for (i=0; i < argc; i++) { + printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); + } + printf("\n"); + return 0; +} + +int main(int argc, char **argv) { + sqlite3 *db; + char *db_path, *sql_stmt; + char *zErrMsg = 0; + int rc; + + if (argc != 3) { + fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]); + return -1; + } + db_path = argv[1]; + sql_stmt = argv[2]; + rc = sqlite3_open(db_path, &db); + if (rc) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + return -1; + } + rc = sqlite3_exec(db, sql_stmt, callback, 0, &zErrMsg); + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + sqlite3_close(db); + return -1; + } + sqlite3_close(db); + fprintf(stdout, "Execute sql-statement: \"%s\"\non database: %s OK\n", + sql_stmt, db_path); + return 0; +}