Index: experimental/webtry/main.cpp |
diff --git a/experimental/webtry/main.cpp b/experimental/webtry/main.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6f2b1cbb3b565e716c5ff3acbba660fb36f6add0 |
--- /dev/null |
+++ b/experimental/webtry/main.cpp |
@@ -0,0 +1,109 @@ |
+#include <sys/time.h> |
+#include <sys/resource.h> |
+ |
+#include "SkCanvas.h" |
+#include "SkCommandLineFlags.h" |
+#include "SkData.h" |
+#include "SkForceLinking.h" |
+#include "SkGraphics.h" |
+#include "SkImageEncoder.h" |
+#include "SkImageInfo.h" |
+#include "SkStream.h" |
+#include "SkSurface.h" |
+ |
+#include "seccomp_bpf.h" |
+#include "syscall_reporter.h" |
+ |
+DEFINE_string(out, "", "Filename of the PNG to write to."); |
+DEFINE_bool(logSyscall, false, "Dump the name of the blocked syscall that was attempted. Use only for debugging."); |
+ |
+static int installSyscallFilter(void) { |
mtklein
2014/04/08 19:00:23
Usually static methods are named_like_this
jcgregorio
2014/04/09 14:10:31
Done.
|
+ struct sock_filter filter[] = { |
+ /* Validate architecture. */ |
+ VALIDATE_ARCHITECTURE, |
+ /* Grab the system call number. */ |
+ EXAMINE_SYSCALL, |
+ /* List allowed syscalls. */ |
+ ALLOW_SYSCALL(exit_group), |
+ ALLOW_SYSCALL(exit), |
+ ALLOW_SYSCALL(fstat), |
+ ALLOW_SYSCALL(read), |
+ ALLOW_SYSCALL(write), |
+ ALLOW_SYSCALL(close), |
+ ALLOW_SYSCALL(mmap), |
+ ALLOW_SYSCALL(munmap), |
+ ALLOW_SYSCALL(brk), |
+ KILL_PROCESS, |
+ }; |
+ struct sock_fprog prog = { |
+ (unsigned short)(sizeof(filter)/sizeof(filter[0])), |
mtklein
2014/04/08 19:00:23
SK_ARRAY_COUNT?
jcgregorio
2014/04/09 14:10:31
Done.
|
+ filter, |
+ }; |
+ |
+ if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
mtklein
2014/04/08 19:00:23
This function could use a bit of comment narration
jcgregorio
2014/04/09 14:10:31
Done.
|
+ perror("prctl(NO_NEW_PRIVS)"); |
+ goto failed; |
+ } |
+ if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { |
+ perror("prctl(SECCOMP)"); |
+ goto failed; |
+ } |
+ return 0; |
+ |
+failed: |
+ if (errno == EINVAL) |
+ fprintf(stderr, "SECCOMP_FILTER is not available. :(\n"); |
+ return 1; |
+} |
+ |
+void setLimits() { |
mtklein
2014/04/08 19:00:23
static void set_limits?
jcgregorio
2014/04/09 14:10:31
Done.
|
+ struct rlimit n; |
+ |
+ // Limit to 5 seconds of CPU. |
+ n.rlim_cur = 5; |
+ n.rlim_max = 5; |
+ if (setrlimit(RLIMIT_CPU, &n)) { |
+ perror("setrlimit(RLIMIT_CPU)"); |
+ } |
+ |
+ // Limit to 50M of Address space. |
+ n.rlim_cur = 50000000; |
+ n.rlim_max = 50000000; |
+ if (setrlimit(RLIMIT_AS, &n)) { |
+ perror("setrlimit(RLIMIT_CPU)"); |
+ } |
+} |
+ |
+void draw(SkCanvas* canvas); |
mtklein
2014/04/08 19:00:23
Can't hurt to remind us that this comes from outsi
jcgregorio
2014/04/09 14:10:31
Done.
|
+ |
+int main(int argc, char** argv) { |
+ SkCommandLineFlags::Parse(argc, argv); |
+ SkForceLinking(false); |
mtklein
2014/04/08 19:00:23
Typically I use the __SK_FORCE_IMAGE_DECODER_LINKI
jcgregorio
2014/04/09 14:10:31
Done.
|
+ SkAutoGraphics init; |
+ |
+ SkFILEWStream stream(FLAGS_out[0]); |
mtklein
2014/04/08 19:00:23
Bail if FLAGS_out.count() == 0?
jcgregorio
2014/04/09 14:10:31
Done.
|
+ |
+ SkImageInfo info = SkImageInfo::MakeN32(300, 300, kPremul_SkAlphaType); |
+ SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
+ SkCanvas* canvas = surface->getCanvas(); |
+ |
+ setLimits(); |
+ |
+ if (FLAGS_logSyscall && install_syscall_reporter()) { |
mtklein
2014/04/08 19:00:23
Ordinarily I'd prefer true, false over 0, 1. I gu
jcgregorio
2014/04/09 14:10:31
Done.
|
+ return 1; |
+ } |
+ if (installSyscallFilter()) { |
+ return 1; |
+ } |
+ |
+ draw(canvas); |
+ |
+ // Write out the image as a PNG. |
+ SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
mtklein
2014/04/08 19:00:23
I know it's sort of notional, but I can't help but
jcgregorio
2014/04/09 14:10:31
Yeah, I'm torn between making the code look exactl
|
+ SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100)); |
+ if (NULL == data.get()) { |
+ printf("Failed to encode\n"); |
+ exit(1); |
+ } |
+ stream.write(data->data(), data->size()); |
+} |