Skip to content

Commit

Permalink
Add configuration of stack size
Browse files Browse the repository at this point in the history
  • Loading branch information
triomino authored and quark-zju committed Jul 11, 2019
1 parent 6725a25 commit ae02412
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/stack/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2
1 change: 1 addition & 0 deletions examples/stack/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
2 changes: 2 additions & 0 deletions examples/stack/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 -2
3 4
2 changes: 2 additions & 0 deletions examples/stack/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-1
7
20 changes: 20 additions & 0 deletions examples/stack/large_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>
int dfs(int x, int MAX) {
if (x == MAX) return 1;
int tmp[10] = {};
for (int i = 0; i < 10; ++i) {
tmp[i] = x - i;
}
int res = dfs(x+1, MAX), tot = 0;
for (int i = 0; i < 10; ++i) {
tot += i * tmp[i] * res;
}
return tot;
}
int main() {
int a, b;
while(scanf("%d %d",&a, &b) != EOF) {
printf("%d\n", dfs(0, 1000000));
}
return 0;
}
50 changes: 50 additions & 0 deletions examples/stack/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh

echo1() {
# echo in 1 line
echo "$@" | tr -d "\n"
}

DEBUG_LOG=.debug.$$.log
ERROR_LOG=error.log

# Test configuring stack size
src=large_stack.c
# Use default stack size (8M) will cause SEGMENTATION_FAULT
echo -n 'Test limitation of stack size: '
RESULT=`ljudge --debug --keep-stdout --keep-stderr --user-code $src --testcase --input 1.in --output 1.out --testcase --input 2.in --output 2.out 2> $DEBUG_LOG | cat`
EXITCODE=$?
if [ "$EXITCODE" != 0 ] || [ -z "$RESULT" ] || (echo1 "$RESULT" | grep -qi ERROR) || (echo1 "$RESULT" | grep -qv SEGMENTATION_FAULT); then
# Log error
echo `date` 'Error running' $i 'test (exit code ' $EXITCODE ')' >> $ERROR_LOG
echo1 "$RESULT" >> $ERROR_LOG
cat $DEBUG_LOG >> $ERROR_LOG
echo >> $ERROR_LOG
# notify user
echo1 'ERROR' "$RESULT"
echo
echo 'To re-run: ljudge -u '$src' -i 1.in -o 1.out -i 2.in -o 2.out'
echo
else
echo OKAY
fi
# Use larger stack size to eliminate SEGMENTATION_FAULT
echo -n 'Test larger stack size: '
RESULT=`ljudge --debug --keep-stdout --keep-stderr --user-code $src --max-stack 128M --testcase --input 1.in --output 1.out --testcase --input 2.in --output 2.out 2> $DEBUG_LOG | cat`
EXITCODE=$?
if [ "$EXITCODE" != 0 ] || [ -z "$RESULT" ] || (echo1 "$RESULT" | grep -qi ERROR) || (echo1 "$RESULT" | grep -q SEGMENTATION_FAULT); then
# Log error
echo `date` 'Error running' $i 'test (exit code ' $EXITCODE ')' >> $ERROR_LOG
echo1 "$RESULT" >> $ERROR_LOG
cat $DEBUG_LOG >> $ERROR_LOG
echo >> $ERROR_LOG
# notify user
echo1 'ERROR' "$RESULT"
echo
echo 'To re-run: ljudge -u '$src' -i 1.in -o 1.out -i 2.in -o 2.out --max-stack 128M'
echo
else
echo OKAY
fi

[ -e $DEBUG_LOG ] && unlink $DEBUG_LOG
14 changes: 11 additions & 3 deletions src/ljudge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct Limit {
double real_time; // seconds
long long memory; // bytes
long long output; // bytes
long long stack; // bytes
};

struct Testcase {
Expand Down Expand Up @@ -179,6 +180,10 @@ struct LrunArgs : public vector<string> {
push_back("--max-output");
push_back(format("%Ld", limit.output));
}
if (limit.stack > 0) {
push_back("--max-stack");
push_back(format("%Ld", limit.stack));
}
/* [[[end]]] */
}

Expand Down Expand Up @@ -716,7 +721,7 @@ static void print_usage() {
#endif
" [--skip-on-first-failure]\n"
" [--max-cpu-time seconds] [--max-real-time seconds]\n"
" [--max-memory bytes] [--max-output bytes]\n"
" [--max-memory bytes] [--max-output bytes] [--max-stack bytes]\n"
" [--max-checker-cpu-time seconds] [--max-checker-real-time seconds]\n"
" [--max-checker-memory bytes] [--max-checker-output bytes]\n"
" [--max-compiler-cpu-time seconds] [--max-compiler-real-time seconds]\n"
Expand Down Expand Up @@ -1260,8 +1265,8 @@ static Options parse_cli_options(int argc, const char *argv[]) {
options.direct_mode = false;
options.nthread = 0;
options.skip_on_first_failure = false;
current_case.checker_limit = { 5, 10, 1 << 30, 1 << 30 };
current_case.runtime_limit = { 1, 3, 1 << 26 /* 64M mem */, 1 << 25 /* 32M output */ };
current_case.checker_limit = { 5, 10, 1 << 30, 1 << 30, 1 << 30 };
current_case.runtime_limit = { 1, 3, 1 << 26 /* 64M mem */, 1 << 25 /* 32M output */, 1 << 23 /* 8M stack limit */ };
debug_level = getenv("DEBUG") ? 10 : 0;
}

Expand Down Expand Up @@ -1359,6 +1364,9 @@ static Options parse_cli_options(int argc, const char *argv[]) {
} else if (option == "max-memory") {
REQUIRE_NARGV(1);
current_case.runtime_limit.memory = parse_bytes(NEXT_STRING_ARG);
} else if (option == "max-stack") {
REQUIRE_NARGV(1);
current_case.runtime_limit.stack = parse_bytes(NEXT_STRING_ARG);
} else if (option == "max-compiler-cpu-time") {
REQUIRE_NARGV(1);
options.compiler_limit.cpu_time = NEXT_NUMBER_ARG;
Expand Down

0 comments on commit ae02412

Please sign in to comment.