Add SQLite demo

This commit is contained in:
LI Qing 2020-02-13 03:54:37 +00:00
parent 551fb8f9d8
commit 3e81b42912
6 changed files with 122 additions and 0 deletions

@ -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/). * `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). * `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). * `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. * `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/). * `xgboost/`: A demo of [XGBoost](https://xgboost.readthedocs.io/en/latest/).

3
demos/sqlite/.gitignore vendored Normal file

@ -0,0 +1,3 @@
sqlite_src/
occlum_context/
sqlite_demo

19
demos/sqlite/README.md Normal file

@ -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 <database> <sql-statement>
```

@ -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"

@ -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"

@ -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 <stdio.h>
#include <sqlite3.h>
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;
}