Skip to content

Commit

Permalink
Config: Add assertion to config integer parsing
Browse files Browse the repository at this point in the history
Make sure we allocate enough space for raw string parsing,
and add address santizer mode to set of C tests
  • Loading branch information
Alasdair committed Jan 16, 2025
1 parent 2d6d09b commit 9a396d6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/json/cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu
loop_end:
number_c_string[i] = '\0';

output = (unsigned char*)input_buffer->hooks.allocate(i * sizeof(char));
memcpy(output, number_c_string, i);
output = (unsigned char*)input_buffer->hooks.allocate(i * sizeof(char) + 1);
memcpy(output, number_c_string, i + 1);

number = strtod((const char*)number_c_string, (char**)&after_end);
if (number_c_string == after_end)
Expand Down
4 changes: 3 additions & 1 deletion lib/json/sail_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ void sail_config_unwrap_string(sail_string *str, const sail_config_json config)
void sail_config_unwrap_int(sail_int *n, const sail_config_json config)
{
cJSON *json = (cJSON *)config;
mpz_set_str(*n, json->valuestring, 10);
if (mpz_set_str(*n, json->valuestring, 10) == -1) {
sail_assert(false, "Failed to parse integer from configuration");
}
}

void sail_config_truncate(lbits *rop) {
Expand Down
2 changes: 2 additions & 0 deletions test/c/config_int.sail
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ $else
$option --config ../c/config_int.json
$endif

$option --sv-int-size 256

val main : unit -> unit

function main() = {
Expand Down
8 changes: 6 additions & 2 deletions test/c/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ def test_c(name, c_opts, sail_opts, valgrind, compiler='cc'):
c_opts += ' \'{}\'/lib/json/*.c -I \'{}\'/lib/json'.format(sail_dir, sail_dir)
step('\'{}\' -no_warn -c {} {} 1> {}.c'.format(sail, sail_opts, filename, basename))
step('{} {} {}.c \'{}\'/lib/*.c -lgmp -I \'{}\'/lib -o {}.bin'.format(compiler, c_opts, basename, sail_dir, sail_dir, basename))
step('./{}.bin > {}.result 2> {}.err_result'.format(basename, basename, basename), expected_status = 1 if basename.startswith('fail') else 0)
step('./{}.bin > {}.result 2> {}.err_result'.format(basename, basename, basename),
expected_status = 1 if basename.startswith('fail') else 0,
stderr_file='{}.err_result'.format(basename))
step('diff {}.result {}.expect'.format(basename, basename))
if os.path.exists('{}.err_expect'.format(basename)):
step('diff {}.err_result {}.err_expect'.format(basename, basename))
if valgrind and not basename.startswith('fail'):
step("valgrind --leak-check=full --track-origins=yes --errors-for-leak-kinds=all --error-exitcode=2 ./{}.bin".format(basename), expected_status = 1 if basename.startswith('fail') else 0)
step("valgrind --leak-check=full --track-origins=yes --errors-for-leak-kinds=all --error-exitcode=2 ./{}.bin".format(basename),
expected_status = 1 if basename.startswith('fail') else 0)
step('rm {}.c {}.bin {}.result'.format(basename, basename, basename))
print_ok(filename)
sys.exit()
Expand Down Expand Up @@ -210,6 +213,7 @@ def test_coq(name):
xml += test_c('constant folding', '', '-Oconstant_fold', False)
#xml += test_c('monomorphised C', '-O2', '-O -Oconstant_fold -auto_mono', True)
xml += test_c('undefined behavior sanitised', '-O2 -fsanitize=undefined', '-O', False)
xml += test_c('address sanitised', '-O2 -fsanitize=address', '-O', False)

if 'cpp' in targets:
xml += test_c('unoptimized C with C++ compiler', '-xc++', '', False, compiler='c++')
Expand Down
15 changes: 12 additions & 3 deletions test/sailtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
parser.add_argument("--hide-error-output", help="Hide error information.", action='store_true')
parser.add_argument("--compact", help="Compact output.", action='store_true')
parser.add_argument("--targets", help="Targets to use (where supported).", action='append')
parser.add_argument("--test", help="Run only specified test.", action='append')
args = parser.parse_args()

def is_compact():
Expand Down Expand Up @@ -81,7 +82,8 @@ def chunks(filenames, cores):
ys = []
chunk = []
for filename in filenames:
if re.match('.+\.sail$', filename):
basename = os.path.splitext(os.path.basename(filename))[0]
if re.match('.+\.sail$', filename) and (not args.test or basename in args.test):
chunk.append(filename)
if len(chunk) >= cores:
ys.append(list(chunk))
Expand Down Expand Up @@ -113,7 +115,7 @@ def project_chunks(filenames, cores):
ys.append(list(chunk))
return ys

def step(string, expected_status=0):
def step(string, expected_status=0, stderr_file=''):
p = subprocess.Popen(string, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = p.communicate()
status = p.wait()
Expand All @@ -127,7 +129,14 @@ def step(string, expected_status=0):
print(out.decode('utf-8'))
print('{}stderr{}:'.format(color.NOTICE, color.END))
print(err.decode('utf-8'))
sys.exit(1)
if stderr_file != '':
try:
with open(stderr_file, 'r') as file:
content = stderr_file.read()
print('{}stderr file{}:'.format(color.NOTICE, color.END))
print(content)
except FileNotFoundError:
print('File {} not found'.format(stderr_file))

def banner(string):
print('-' * len(string))
Expand Down

0 comments on commit 9a396d6

Please sign in to comment.