浏览代码

* Better error reporting and argument passing from/to the bytecode.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/research@2287 92316355-f0b4-4df1-b90c-862c8a59935f
master
sam 17 年前
父节点
当前提交
36f563a01c
共有 2 个文件被更改,包括 67 次插入37 次删除
  1. +14
    -7
      2008-displacement/main-cpushare
  2. +53
    -30
      2008-displacement/main.c

+ 14
- 7
2008-displacement/main-cpushare 查看文件

@@ -17,6 +17,7 @@ if len(sys.argv) < 4:
print 'Usage: %s <buy_order.cpu> -<mode> [image list]' % (sys.argv[0],) print 'Usage: %s <buy_order.cpu> -<mode> [image list]' % (sys.argv[0],)
sys.exit(-1) sys.exit(-1)
mode = sys.argv[2][1] mode = sys.argv[2][1]
done = 0
source = sys.argv[3:] source = sys.argv[3:]
result = [False] * len(source) result = [False] * len(source)


@@ -34,13 +35,19 @@ class buy_state_machine_class(object):
def start(self): def start(self):
global todo, source global todo, source
if todo >= len(source): if todo >= len(source):
return # We're finished... FIXME: a process is stuck return # We're finished... FIXME: is the transaction kept stuck?
self.index = todo self.index = todo
todo += 1 todo += 1
gd.gdMaxColors = 256 * 256 * 256 gd.gdMaxColors = 256 * 256 * 256
im = gd.image(source[self.index]) im = gd.image(source[self.index])
# Send mode # Send argument count
self.protocol.sendString(PROTO_SECCOMP_FORWARD + mode) self.protocol.sendString(PROTO_SECCOMP_FORWARD + chr(3))
# Send arguments
msg = chr(3)
msg += "bytecode\0"
msg += "-1\0"
msg += source[self.index] + "\0"
self.protocol.sendString(PROTO_SECCOMP_FORWARD + msg)
# Send image size # Send image size
(self.w, self.h) = (w, h) = im.size() (self.w, self.h) = (w, h) = im.size()
print 'sending %s (%dx%d)' % (source[self.index], w, h) print 'sending %s (%dx%d)' % (source[self.index], w, h)
@@ -64,13 +71,13 @@ class buy_state_machine_class(object):
self.answer += data self.answer += data
if ord(self.answer[-1]) != 0: if ord(self.answer[-1]) != 0:
return return
global result global done, result
result[self.index] = self.answer[:-1] result[self.index] = self.answer[:-1]
print self.answer[:-1], print self.answer[:-1],
self.protocol.sendString(PROTO_SECCOMP_SUCCESS) self.protocol.sendString(PROTO_SECCOMP_SUCCESS)
# the PROTO_SECCOMP_SUCCESS probably won't have a chance done += 1
# to go out but it doesn't matter because we'll disconnect if done < len(source):
# not just this but all other seccomp state machines too return
from twisted.internet import reactor from twisted.internet import reactor
reactor.stop() reactor.stop()
elif control == PROTO_SECCOMP_SIGNAL: elif control == PROTO_SECCOMP_SIGNAL:


+ 53
- 30
2008-displacement/main.c 查看文件

@@ -39,14 +39,26 @@ void sighandler(int signal)


void bytecode(unsigned char * mem, int heap_size, int stack_size) void bytecode(unsigned char * mem, int heap_size, int stack_size)
{ {
char mode[] = "-0"; char args[10][1024];
char *argv[] = { "program", mode, "arg" }; char *argv[] = { args[0], args[1], args[2], args[3], args[4],
args[5], args[6], args[7], args[8], args[9] };
int i, argc;
char c; char c;


if(sys_read(0, mode + 1, 1) != 1) if(sys_read(0, &c, 1) != 1)
sys_exit(-5); sys_exit(-5);
argc = (unsigned char)c;

for(i = 0; i < argc; i++)
{
char *p = argv[i];
do
if(sys_read(0, p, 1) != 1)
sys_exit(-5);
while(*p++);
}


main(sizeof(argv)/sizeof(*argv), argv); main(argc, argv);


c = 0; c = 0;
if(sys_write(1, &c, 1) != 1) if(sys_write(1, &c, 1) != 1)
@@ -80,10 +92,8 @@ static int myatoi(const char *str)
sys_write(1, b + 1, (int)(buf + 127 - b)); \ sys_write(1, b + 1, (int)(buf + 127 - b)); \
} while(0) } while(0)


static void msg(const char *f, ...) static void out(FILE *stream, const char *f, va_list args)
{ {
va_list args;
va_start(args, f);
#ifdef BYTECODE #ifdef BYTECODE
static char const *hex2char = "0123456789abcdef"; static char const *hex2char = "0123456789abcdef";


@@ -141,22 +151,18 @@ static void msg(const char *f, ...)
} }
else if(f[0] == 'g') else if(f[0] == 'g')
{ {
double g = va_arg(args, double), h = 9.9f; double g = va_arg(args, double), h = 0.0000001;
int i; int i = g;
if(g < 0.f) WRITE_INT(i, 10);
{
sys_write(1, "-", 1);
g = -g;
}
for(i = 0; i < 7; i++) for(i = 0; i < 7; i++)
{ {
sys_write(1, hex2char + (int)g, 1);
if(i == 0)
sys_write(1, ".", 1);
g = (g - (int)g) * 10; g = (g - (int)g) * 10;
h = h / 10.f; h *= 10;
if(g < h) if(g < h)
break; break;
if(i == 0)
sys_write(1, ".", 1);
sys_write(1, hex2char + (int)g, 1);
} }
} }
else if(f[0] == 'p') else if(f[0] == 'p')
@@ -196,9 +202,24 @@ static void msg(const char *f, ...)
} }
} }
#else #else
vprintf(f, args); vfprintf(stream, f, args);
fflush(stdout); fflush(stream);
#endif #endif
}

static void err(const char *f, ...)
{
va_list args;
va_start(args, f);
out(stderr, f, args);
va_end(args);
}

static void msg(const char *f, ...)
{
va_list args;
va_start(args, f);
out(stdout, f, args);
va_end(args); va_end(args);
} }


@@ -752,7 +773,7 @@ static double *dbs(double const *src, double const *orig,
changes++; changes++;
} }


fprintf(stderr, "did %i changes\n", changes); err("did %i changes\n", changes);


if(changes == 0) if(changes == 0)
break; break;
@@ -828,13 +849,15 @@ static double *merge(double const *im1, double const *im2, double t)


static void usage(char *argv[]) static void usage(char *argv[])
{ {
fprintf(stderr, "Usage: %s <mode> [ARGS...]\n", argv[0]); msg("Usage: %s <mode> [ARGS...]\n", argv[0]);
fprintf(stderr, "Allowed modes:\n"); msg("Allowed modes:\n");
fprintf(stderr, " -1 <src> raster FS displacement study on src\n"); msg(" -1 <src> raster FS displacement study on src\n");
fprintf(stderr, " -2 <src1> <src2> raster FS displacement study on blends of src1 and src2\n"); msg(" -2 <src1> <src2> raster FS displacement study on blends of src1 and src2\n");
fprintf(stderr, " -3 <src> quick (a,b,c,d) ED kernel analysis on src\n"); msg(" -3 <src> quick (a,b,c,d) ED kernel analysis on src\n");
fprintf(stderr, " -4 <src> exhaustive (a,b,c,d) ED kernel analysis on src\n"); msg(" -4 <src> exhaustive (a,b,c,d) ED kernel analysis on src\n");
fprintf(stderr, " -5 <src> exhaustive displacement study on src\n"); msg(" -5 <src> exhaustive displacement study on src\n");
msg(" -6 <src> restrained (a,b,c,d) ED kernel analysis on src\n");
msg(" -7 <src> restrained displacement study on src\n");
} }


int main(int argc, char *argv[]) int main(int argc, char *argv[])
@@ -844,14 +867,14 @@ int main(int argc, char *argv[])


if(argc < 2) if(argc < 2)
{ {
fprintf(stderr, "%s: too few arguments\n", argv[0]); err("%s: too few arguments\n", argv[0]);
usage(argv); usage(argv);
return EXIT_FAILURE; return EXIT_FAILURE;
} }


if(argv[1][0] != '-' || !(mode = atoi(argv[1] + 1))) if(argv[1][0] != '-' || !(mode = atoi(argv[1] + 1)))
{ {
fprintf(stderr, "%s: invalid mode `%s'\n", argv[0], argv[1]); err("%s: invalid mode `%s'\n", argv[0], argv[1]);
usage(argv); usage(argv);
return EXIT_FAILURE; return EXIT_FAILURE;
} }


||||||
x
 
000:0
正在加载...
取消
保存