From bddb7ca6b0c4ffed44132e971b953b929b263583 Mon Sep 17 00:00:00 2001 From: Xaver K Date: Fri, 27 Mar 2020 12:27:14 +0100 Subject: [PATCH] Static server (#2) * Wrap input and file output functions for a read only static server. * Added -y to docker apt-get in docker file. * wError/wAll as options in Kernel, defaults only to wAll. --- Dockerfile | 4 ++ jupyter_c_kernel/kernel.py | 18 ++++++- jupyter_c_kernel/resources/stdio_wrap.h | 65 ++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 38acb16..3480d3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,10 @@ MAINTAINER Xaver Klemenschits USER root +# Install vim and ssh +RUN apt-get update +RUN apt-get install -y vim openssh-client + WORKDIR /tmp COPY ./ jupyter_c_kernel/ diff --git a/jupyter_c_kernel/kernel.py b/jupyter_c_kernel/kernel.py index 81f97a2..502ac9c 100644 --- a/jupyter_c_kernel/kernel.py +++ b/jupyter_c_kernel/kernel.py @@ -102,6 +102,9 @@ class CKernel(Kernel): def __init__(self, *args, **kwargs): super(CKernel, self).__init__(*args, **kwargs) self._allow_stdin = True + self.readOnlyFileSystem = False + self.wAll = True # show all warnings by default + self.wError = False # but keep comipiling for warnings self.files = [] mastertemp = tempfile.mkstemp(suffix='.out') os.close(mastertemp[0]) @@ -145,6 +148,12 @@ class CKernel(Kernel): def compile_with_gcc(self, source_filename, binary_filename, cflags=None, ldflags=None): cflags = ['-std=c11', '-fPIC', '-shared', '-rdynamic'] + cflags + if self.wError: + cflags = cflags + ['-Werror'] + if self.wAll: + cflags = cflags + ['-Wall'] + if self.readOnlyFileSystem: + cflags = ['-DREAD_ONLY_FILE_SYSTEM'] + cflags args = ['gcc', source_filename] + cflags + ['-o', binary_filename] + ldflags return self.create_jupyter_subprocess(args) @@ -178,8 +187,13 @@ class CKernel(Kernel): # check whether int main() is specified, if not add it around the code # also add common magics like -lm def _add_main(self, magics, code): - x = re.search("int\s+main\s*\(", code) - if x is None: + # remove comments + tmpCode = re.sub("//.*", "", code) + tmpCode = re.sub("/\*.*?\*/", "", tmpCode, flags=re.M|re.S) + + x = re.search("int\s+main\s*\(", tmpCode) + + if not x: code = self.main_head + code + self.main_foot magics['cflags'] += ['-lm'] diff --git a/jupyter_c_kernel/resources/stdio_wrap.h b/jupyter_c_kernel/resources/stdio_wrap.h index 0374711..e7a1ebf 100644 --- a/jupyter_c_kernel/resources/stdio_wrap.h +++ b/jupyter_c_kernel/resources/stdio_wrap.h @@ -1,10 +1,63 @@ +#ifndef STDIO_WRAP_H +#define STDIO_WRAP_H + #include +#include + +/* Define the functions to overload the old ones */ +int scanf_wrap(const char *format, ...) { + printf(""); + fflush(stdout); + va_list arglist; + va_start( arglist, format ); + int result = vscanf( format, arglist ); + va_end( arglist ); + return result; +} + +int getchar_wrap(){ + printf(""); + fflush(stdout); + return getchar(); +} + +/* Replace all the necessary input functions + Need double hashes in case there are no __VA_ARGS__*/ +#define scanf(format, ...) scanf_wrap(format, ##__VA_ARGS__) + +#define getchar() getchar_wrap() + +/* Replace FILE write operations for read-only systems */ +#ifdef READ_ONLY_FILE_SYSTEM + +/* Define wrapping functions */ +/* Output that the fopen succeeded and return some valid pointer */ +FILE *fopen_wrap(const char *filename, const char *modes) { + static long stream = 0x1FFFF0000; +#ifdef SHOW_FILE_IO_VERBOSE + printf("\x01b[42m"); + printf("\"%s\" opened in mode \"%s\"\n", filename, modes); + printf("\x01b[0m"); +#endif /* SHOW_FILE_IO_VERBOSE */ + return (FILE*)stream++; +} + +int fprintf_wrap(FILE* stream, const char* format, ...) { + printf("\x01b[42m"); + printf("%p:", stream); + printf("\x01b[0m"); + va_list arglist; + va_start( arglist, format ); + int result = vprintf(format, arglist); + va_end( arglist ); + return result; +} /* Replace all the necessary input functions */ -#define scanf(...) printf("");\ -fflush(stdout);\ -scanf(__VA_ARGS__); +#define fopen(file, mode) fopen_wrap(file, mode) -#define getchar() printf("");\ -fflush(stdout);\ -getchar(); +#define fprintf(stream, format, ...) fprintf_wrap(stream, format, ##__VA_ARGS__) + +#endif /* READ_ONLY_FILE_SYSTEM */ + +#endif /* STDIO_WRAP_H */