Skip to content

Commit 4802afd

Browse files
committed
compilation flow: add release mode
1 parent f35035e commit 4802afd

12 files changed

+119
-93
lines changed

cmake/ConfigSGX.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ endif()
4848
find_package(SGXPSW REQUIRED)
4949

5050
message(STATUS "SGX_SDK: ${SGX_SDK}")
51-
message(STATUS "SGX_COMMON_CFLAGS: ${SGX_COMMON_CFLAGS}")
5251
message(STATUS "SGX_ARCH: ${SGX_ARCH}")
5352
message(STATUS "SGX_MODE: ${SGX_MODE}")
5453
message(STATUS "SGX_BUILD: ${SGX_BUILD}")
@@ -61,3 +60,4 @@ message(STATUS "SGX_TRTS_LIB: ${SGX_TRTS_LIB}")
6160
message(STATUS "SGX_TSVC_LIB: ${SGX_TSVC_LIB}")
6261
message (STATUS "SGX uRTS path: ${SGX_URTS_LIBRARY}")
6362
message (STATUS "SGX uSVC path: ${SGX_USVC_LIBRARY}")
63+
message(STATUS "SGX_COMMON_CFLAGS: ${SGX_COMMON_CFLAGS}")

dockerfiles/deployment/Dockerfile.build

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ COPY . /code
88

99
RUN mkdir /tc && \
1010
cp /code/dockerfiles/deployment/Dockerfile.runtime /tc/Dockerfile && \
11-
cp /code/dockerfiles/deployment/supervisord.conf /tc
12-
11+
mv /code/dockerfiles/deployment/sign_enclave.sh \
12+
/code/dockerfiles/deployment/build_release_docker.sh \
13+
/code/dockerfiles/deployment/supervisord.conf /tc
1314
RUN cd /code && \
1415
make -C src/Enclave/mbedtls-SGX && \
1516
mkdir -p tmp && \

dockerfiles/deployment/Dockerfile.runtime

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ENV LD_LIBRARY_PATH=${SGX_SDK}/sdk_libs
88
COPY . /tc
99

1010
RUN apt-get update && apt-get install --yes supervisor
11+
RUN /tc/sign_enclave.sh
1112

1213
EXPOSE 8123
1314

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
CORE_IMAGE=bl4ck5un/tc-core
4+
BUILD=Release
5+
6+
docker run --rm tc/core-builder | docker build --rm --force-rm -t ${CORE_IMAGE}:${BUILD} .
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
SGX_SDK=${SGX_SDK:-"/opt/intel/sgxsdk"}
4+
SGX_ENCLAVE_SIGNER=${SGX_SDK}/bin/x64/sgx_sign
5+
6+
SIGNATURE_FILE=signature.hex
7+
SIGNING_WORKING_DIR=signing
8+
9+
if [[ -f ${SIGNATURE_FILE} ]]; then
10+
pushd ${SIGNING_WORKING_DIR}
11+
${SGX_ENCLAVE_SIGNER} catsig
12+
-enclave enclave.debug.so \
13+
-config Enclave.config.xml \
14+
-key release_pubkey.pem \
15+
-out enclave.release.so \
16+
-sig signature.hex \
17+
-unsigned enclave.hex
18+
popd
19+
else
20+
echo "nothing to do";
21+
fi

scripts/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ core:
2020
core-debug:
2121
$(MAKE) BUILD=Debug build
2222

23+
release:
24+
cd ${BASE_DIR} && \
25+
docker build \
26+
--build-arg BUILD=Release \
27+
--build-arg MAKE_FLAGS=-j \
28+
--force-rm \
29+
-t tc/core-builder \
30+
-f dockerfiles/deployment/Dockerfile.build . && \
31+
rm -rf build/release && mkdir -p build/release && \
32+
docker run --rm tc/core-builder | tar xvzf - -C build/release
33+
2334
build:
2435
cd ${BASE_DIR} && \
2536
docker build \

src/App/debug.cpp

+13-14
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
#include "App/debug.h"
4545

46-
#include <stdio.h>
47-
#define printf_sgx printf
46+
#include <cstdio>
4847

4948
void hexdump(const char *title, void const *data, size_t len) {
5049
#if(defined(DEBUG))
@@ -54,38 +53,38 @@ void hexdump(const char *title, void const *data, size_t len) {
5453
if (!data)
5554
return;
5655

57-
printf_sgx("%s\n", title);
56+
std::printf("%s\n", title);
5857

5958
for (r = 0, i = 0; r < (len / 16 + (len % 16 != 0)); r++, i += 16) {
60-
printf_sgx("0x%04X: ", i); /* location of first byte in line */
59+
std::printf("0x%04X: ", i); /* location of first byte in line */
6160

6261
for (c = i; c < i + 8; c++) /* left half of hex dump */
6362
if (c < len)
64-
printf_sgx("%02X ", ((unsigned char const *) data)[c]);
63+
std::printf("%02X ", ((unsigned char const *) data)[c]);
6564
else
66-
printf_sgx(" "); /* pad if short line */
65+
std::printf(" "); /* pad if short line */
6766

68-
printf_sgx(" ");
67+
std::printf(" ");
6968

7069
for (c = i + 8; c < i + 16; c++) /* right half of hex dump */
7170
if (c < len)
72-
printf_sgx("%02X ", ((unsigned char const *) data)[c]);
71+
std::printf("%02X ", ((unsigned char const *) data)[c]);
7372
else
74-
printf_sgx(" "); /* pad if short line */
73+
std::printf(" "); /* pad if short line */
7574

76-
printf_sgx(" ");
75+
std::printf(" ");
7776

7877
for (c = i; c < i + 16; c++) /* ASCII dump */
7978
if (c < len) {
8079
if (((unsigned char const *) data)[c] >= 32 &&
8180
((unsigned char const *) data)[c] < 127)
82-
printf_sgx("%c", ((char const *) data)[c]);
81+
std::printf("%c", ((char const *) data)[c]);
8382
else
84-
printf_sgx("."); /* put this for non-printables */
83+
std::printf("."); /* put this for non-printables */
8584
} else {
86-
printf_sgx(" "); /* pad if short line */
85+
std::printf(" "); /* pad if short line */
8786
}
88-
printf_sgx("\n");
87+
std::printf("\n");
8988
}
9089
#endif
9190
}

src/Enclave/CMakeLists.txt

+6-14
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,9 @@ add_custom_command(TARGET enclave
107107
-out enclave.hex
108108
COMMENT "Generating signing materials. Written to ${CMAKE_CURRENT_BINARY_DIR}/enclave.hex")
109109

110-
add_custom_target(release-enclave
111-
DEPENDS enclave Enclave.config.xml release_pubkey.pem
112-
COMMAND ${SGX_ENCLAVE_SIGNER} catsig
113-
-enclave libenclave.so
114-
-config ${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml
115-
-key ${CMAKE_CURRENT_SOURCE_DIR}/release_pubkey.pem
116-
-out ${RELEASE_ENCLAVE_NAME}
117-
-sig signature.hex
118-
-unsigned enclave.hex
119-
WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
120-
121-
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${DEBUG_ENCLAVE_NAME} DESTINATION enclave
122-
CONFIGURATIONS Debug Prerelease)
123-
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/enclave.hex DESTINATION enclave)
110+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${DEBUG_ENCLAVE_NAME} DESTINATION enclave)
111+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/enclave.hex
112+
${CMAKE_CURRENT_SOURCE_DIR}/release_pubkey.pem
113+
${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml
114+
DESTINATION signing
115+
CONFIGURATIONS Release)

src/Enclave/debug.c

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void hexdump(const char *title, void const *data, size_t len) {
9292
}
9393
}
9494

95+
#ifdef DEBUG
9596
static void bin_to_strhex(const unsigned char *bin, size_t binsz, char *result) {
9697
char hex_str[] = "0123456789abcdef";
9798
unsigned int i;
@@ -106,6 +107,7 @@ static void bin_to_strhex(const unsigned char *bin, size_t binsz, char *result)
106107
result[i * 2 + 1] = hex_str[(bin[i]) & 0x0F];
107108
}
108109
}
110+
#endif
109111

110112
void print_str_dbg(const char *title, const unsigned char *data, size_t len) {
111113
#ifdef DEBUG

src/Enclave/encoding.cpp

+1-41
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "encoding.h"
4545

4646
#include <string>
47+
#include <iterator>
4748

4849
#include "commons.h"
4950
#include "debug.h"
@@ -78,14 +79,6 @@ int append_as_uint256(bytes &out, uint64_t in, int len) {
7879
return 0;
7980
}
8081

81-
// compute how many (non-zero) bytes there are in _i
82-
template<typename T>
83-
static uint8_t byte_length(T _i) {
84-
uint8_t i = 0;
85-
for (; _i != 0; ++i, _i >>= 8) {}
86-
return i;
87-
}
88-
8982
uint8_t bytesRequired(uint64_t _i) { return byte_length<uint64_t>(_i); }
9083

9184
void bytes::replace(const bytes &in) {
@@ -99,39 +92,6 @@ void bytes::from_hex(const char *src) {
9992
this->insert(this->begin(), b.begin(), b.end());
10093
}
10194

102-
#include <iterator>
103-
104-
template <typename Iter>
105-
void rlp_string(Iter begin, Iter end, std::vector<uint8_t>& out) {
106-
static_assert(std::is_same<typename std::iterator_traits<Iter>::value_type, uint8_t>::value, "Iter must point to uint8_t");
107-
108-
long len = std::distance(begin, end);
109-
if (len < 0)
110-
throw std::invalid_argument("String too long to be encoded.");
111-
112-
int32_t len_len;
113-
if (len == 1 && (*begin) < 0x80) {
114-
out.push_back(*begin);
115-
return;
116-
}
117-
118-
// longer than 1
119-
if (len < 56) {
120-
out.push_back(0x80 + static_cast<uint8_t>(len));
121-
out.insert(out.end(), begin, end);
122-
} else {
123-
len_len = byte_length<size_t>(len);
124-
if (len_len > 8) {
125-
throw std::invalid_argument("String too long to be encoded.");
126-
}
127-
128-
out.push_back(0xb7 + static_cast<uint8_t>(len_len));
129-
130-
std::vector<uint8_t> b_len = itob(len);
131-
out.insert(out.end(), b_len.begin(), b_len.end());
132-
out.insert(out.end(), begin, end);
133-
}
134-
}
13595

13696
void bytes::to_rlp(bytes &out) {
13797
rlp_string(this->begin(), this->end(), out);

src/Enclave/encoding.h

+53-19
Original file line numberDiff line numberDiff line change
@@ -44,43 +44,80 @@
4444
#define TOWN_CRIER_ENCODING_H
4545

4646
#include "commons.h"
47+
#include "debug.h"
4748
#include "log.h"
4849

4950
#include <vector>
5051
#include <array>
5152

52-
5353
using std::vector;
5454
using std::invalid_argument;
5555
using std::string;
5656
using std::array;
5757
typedef std::vector<uint8_t> BYTE;
5858

59-
6059
//! split an integer @code{num} to an byte array
6160
//! prepend 0 until reaching @code{width}
6261
std::vector<uint8_t> itob(uint64_t num, size_t width = 0);
6362

63+
// compute how many (non-zero) bytes there are in _i
64+
template<typename T>
65+
static uint8_t byte_length(T _i) {
66+
uint8_t i = 0;
67+
for (; _i != 0; ++i, _i >>= 8) {}
68+
return i;
69+
}
70+
71+
template<typename Iter>
72+
void rlp_string(Iter begin, Iter end, std::vector<uint8_t> &out) {
73+
static_assert(std::is_same<typename std::iterator_traits<Iter>::value_type, uint8_t>::value,
74+
"Iter must point to uint8_t");
75+
76+
long len = std::distance(begin, end);
77+
if (len < 0)
78+
throw std::invalid_argument("String too long to be encoded.");
79+
80+
int32_t len_len;
81+
if (len == 1 && (*begin) < 0x80) {
82+
out.push_back(*begin);
83+
return;
84+
}
85+
86+
// longer than 1
87+
if (len < 56) {
88+
out.push_back(0x80 + static_cast<uint8_t>(len));
89+
out.insert(out.end(), begin, end);
90+
} else {
91+
len_len = byte_length<size_t>(len);
92+
if (len_len > 8) {
93+
throw std::invalid_argument("String too long to be encoded.");
94+
}
95+
96+
out.push_back(0xb7 + static_cast<uint8_t>(len_len));
97+
98+
std::vector<uint8_t> b_len = itob(len);
99+
out.insert(out.end(), b_len.begin(), b_len.end());
100+
out.insert(out.end(), begin, end);
101+
}
102+
}
103+
64104
class RLPSerializable {
65105
public:
66-
virtual void to_rlp(std::vector<uint8_t>& out) = 0;
106+
virtual void to_rlp(std::vector<uint8_t> &out) = 0;
67107
};
68108

69-
template <typename Iter>
70-
void rlp_string(Iter begin, Iter end, std::vector<uint8_t>& out);
71-
72109
class bytes : public std::vector<uint8_t> {
73110
public:
74111
bytes() = default;
75-
bytes(const bytes& a, const bytes& b) {
112+
bytes(const bytes &a, const bytes &b) {
76113
std::vector<uint8_t>::insert(std::vector<uint8_t>::end(), a.begin(), a.end());
77114
std::vector<uint8_t>::insert(std::vector<uint8_t>::end(), b.begin(), b.end());
78115
}
79116
explicit bytes(size_t len) : std::vector<uint8_t>(len, static_cast<uint8_t>(0)) {}
80-
explicit bytes(const std::vector<uint8_t>& data) : std::vector<uint8_t>(data) {}
117+
explicit bytes(const std::vector<uint8_t> &data) : std::vector<uint8_t>(data) {}
81118
void replace(const bytes &);
82119
virtual void from_hex(const char *src);
83-
virtual void parseUInt64(uint64_t i){
120+
virtual void parseUInt64(uint64_t i) {
84121
this->clear();
85122
vector<uint8_t> b = itob(i);
86123
this->insert(this->begin(), b.begin(), b.end());
@@ -90,20 +127,18 @@ class bytes : public std::vector<uint8_t> {
90127
void toString();
91128
};
92129

93-
#include "debug.h"
94-
95-
class bytes20: public RLPSerializable {
130+
class bytes20 : public RLPSerializable {
96131
private:
97-
static const size_t SIZE=20;
132+
static const size_t SIZE = 20;
98133
array<uint8_t, SIZE> _b;
99134

100135
public:
101136
bytes20() = default;
102-
explicit bytes20(const char* hex);
103-
void to_rlp(std::vector<uint8_t>& out) override {
137+
explicit bytes20(const char *hex);
138+
void to_rlp(std::vector<uint8_t> &out) override {
104139
rlp_string(_b.begin(), _b.end(), out);
105140
}
106-
void dump(const char* title) {
141+
void dump(const char *title) {
107142
#ifndef NDEBUG
108143
hexdump(title, _b.data(), _b.size());
109144
#endif
@@ -112,7 +147,7 @@ class bytes20: public RLPSerializable {
112147

113148
class bytes32 : public bytes {
114149
private:
115-
const size_t SIZE=32;
150+
const size_t SIZE = 32;
116151
public:
117152
bytes32() {
118153
vector<uint8_t>::resize(SIZE);
@@ -132,8 +167,7 @@ class bytes32 : public bytes {
132167
void replace(const bytes32 &in) { bytes::replace(in); }
133168
void replace(const BYTE &in);
134169

135-
136-
void reset(){}
170+
void reset() {}
137171
};
138172

139173
uint8_t get_n_th_byte(uint64_t in, int n);

src/Enclave/eth_transaction.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,8 @@ int form_transaction(int nonce,
234234
tx.m_data = encoded_delivery_call;
235235

236236
try {
237-
LL_DEBUG("before rlpEncode");
238237
tx.rlpEncode(out, false);
239-
LL_DEBUG("after rlpEncode");
238+
LL_DEBUG("rlpEncode done");
240239
}
241240
catch (const std::invalid_argument &e) {
242241
LL_CRITICAL("%s", e.what());

0 commit comments

Comments
 (0)