forked from gramineproject/graphene
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Pal/lib] Add spinlocks to mbedTLS-specific SSL recv/send
Linux-SGX PAL wraps all pipe/UNIX domain socket communication in TLS sessions. Previously, Graphene assumed that only one thread at a time accesses one TLS session (i.e., no multi-threading support). This commit adds spinlocks to `lib_SSL*` functions to support such (rare) multi-threading scenarios. Note that we cannot rely on pthreads and/or mutexes so we use simple spinlocks. This commit adds a corresponding LibOS test.
- Loading branch information
Showing
5 changed files
with
119 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ | |
/multi_pthread_exitless | ||
/openmp | ||
/pipe | ||
/pipe_multithread | ||
/pipe_nonblocking | ||
/pipe_ocloexec | ||
/poll | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* test creates two threads simulteneously writing on the same pipe */ | ||
|
||
#include <err.h> | ||
#include <errno.h> | ||
#include <pthread.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
|
||
#define ITERATIONS 100000 | ||
|
||
int fds[2]; | ||
|
||
static void* thread_run(void* arg) { | ||
char c = (char)(uintptr_t)arg; | ||
for (int i = 0; i < ITERATIONS; i++) { | ||
ssize_t bytes = 0; | ||
while (bytes < sizeof(c)) { | ||
bytes = send(fds[1], &c, sizeof(c), /*flags=*/0); | ||
if (bytes < 0) { | ||
if (errno == EAGAIN || errno == EINTR) | ||
continue; | ||
err(1, "send"); | ||
} | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
int ret; | ||
pthread_t threads[2]; | ||
char thread_ids[2] = {42, 24}; | ||
int thread_bytes[2] = {0, 0}; | ||
|
||
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); | ||
if (ret) { | ||
err(1, "socketpair"); | ||
} | ||
|
||
ret = pthread_create(&threads[0], NULL, &thread_run, (void*)(uintptr_t)thread_ids[0]); | ||
if (ret) { | ||
errno = ret; | ||
err(1, "pthread_create"); | ||
} | ||
|
||
ret = pthread_create(&threads[1], NULL, &thread_run, (void*)(uintptr_t)thread_ids[1]); | ||
if (ret) { | ||
errno = ret; | ||
err(1, "pthread_create"); | ||
} | ||
|
||
for (int i = 0; i < 2 * ITERATIONS; i++) { | ||
char c = 0; | ||
ssize_t bytes = 0; | ||
while (bytes < sizeof(c)) { | ||
bytes = recv(fds[0], &c, sizeof(c), /*flags=*/0); | ||
if (bytes < 0) { | ||
if (errno == EAGAIN || errno == EINTR) | ||
continue; | ||
err(1, "recv"); | ||
} | ||
} | ||
|
||
if (c == thread_ids[0]) | ||
thread_bytes[0] += bytes; | ||
else if (c == thread_ids[1]) | ||
thread_bytes[1] += bytes; | ||
else | ||
errx(1, "received unrecognized thread ID"); | ||
} | ||
|
||
printf("received total bytes from threads: %d and %d\n", thread_bytes[0], thread_bytes[1]); | ||
|
||
if (thread_bytes[0] != ITERATIONS || thread_bytes[1] != ITERATIONS) | ||
errx(1, "received wrong number of bytes from threads"); | ||
|
||
puts("TEST OK"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters