2 #include <sys/resource.h>
5 #include "SkCommandLineFlags.h"
7 #include "SkForceLinking.h"
8 #include "SkGraphics.h"
9 #include "SkImageDecoder.h"
10 #include "SkImageEncoder.h"
11 #include "SkImageInfo.h"
13 #include "SkSurface.h"
15 #include "seccomp_bpf.h"
17 __SK_FORCE_IMAGE_DECODER_LINKING;
19 DEFINE_string(out, "", "Filename of the PNG to write to.");
20 DEFINE_string(source, "", "Filename of the source image.");
22 // Defined in template.cpp.
23 extern SkBitmap source;
25 static bool install_syscall_filter() {
27 #ifndef SK_UNSAFE_BUILD_DESKTOP_ONLY
28 struct sock_filter filter[] = {
29 /* Grab the system call number. */
31 /* List allowed syscalls. */
32 ALLOW_SYSCALL(exit_group),
39 ALLOW_SYSCALL(munmap),
43 struct sock_fprog prog = {
44 SK_ARRAY_COUNT(filter),
48 // Lock down the app so that it can't get new privs, such as setuid.
49 // Calling this is a requirement for an unpriviledged process to use mode
50 // 2 seccomp filters, ala SECCOMP_MODE_FILTER, otherwise we'd have to be
52 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
53 perror("prctl(NO_NEW_PRIVS)");
56 // Now call seccomp and restrict the system calls that can be made to only
57 // the ones in the provided filter list.
58 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
59 perror("prctl(SECCOMP)");
65 if (errno == EINVAL) {
66 fprintf(stderr, "SECCOMP_FILTER is not available. :(\n");
71 #endif /* SK_UNSAFE_BUILD_DESKTOP_ONLY */
74 static void setLimits() {
77 // Limit to 5 seconds of CPU.
80 if (setrlimit(RLIMIT_CPU, &n)) {
81 perror("setrlimit(RLIMIT_CPU)");
84 // Limit to 50M of Address space.
85 n.rlim_cur = 50000000;
86 n.rlim_max = 50000000;
87 if (setrlimit(RLIMIT_AS, &n)) {
88 perror("setrlimit(RLIMIT_CPU)");
92 extern void draw(SkCanvas* canvas);
94 int main(int argc, char** argv) {
95 SkCommandLineFlags::Parse(argc, argv);
98 if (FLAGS_out.count() == 0) {
99 perror("The --out flag must have an argument.");
103 if (FLAGS_source.count() == 1) {
104 if (!SkImageDecoder::DecodeFile(FLAGS_source[0], &source)) {
105 perror("Unable to read the source image.");
109 SkFILEWStream stream(FLAGS_out[0]);
111 SkImageInfo info = SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType);
112 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
113 SkCanvas* canvas = surface->getCanvas();
117 if (!install_syscall_filter()) {
123 // Write out the image as a PNG.
124 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
125 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
126 if (NULL == data.get()) {
127 printf("Failed to encode\n");
130 stream.write(data->data(), data->size());