// use checked-out C list algorithms and the existing allocation-failure harness. module main import nspkg.tests.tooling import os fn main() { run() or { eprintln(err) exit(1) } } fn run() ! { args := tooling.options(true)! base := tooling.temporary('alpm-list-parity')! defer { os.rmdir_all(base) or { eprintln(err) } } for name in ['alpm_list.c', 'alpm_list.h'] { source := os.read_file(os.join_path(args.pacman, 'lib/libalpm', name))!.replace('alpm_list_', 'reference_list_').replace('"alpm_list.h"', '"reference_list.h"') os.write_file(os.join_path(base, name.replace('alpm_list', 'reference_list')), source)! } native := os.read_file(os.join_path(@VMODROOT, 'list.v'))! os.write_file(os.join_path(base, 'list.v'), native)! test_source := os.read_file(os.join_path(@VMODROOT, 'list_test.v'))!.replace('C.alpm_list_', 'C.reference_list_').replace('#flag -lalpm\n', '').replace('#include \n', '').replace('#include ', '#flag ${base}/reference_list.c\n#include "${base}/reference_list.h"') test_file := os.join_path(base, 'list_test.v') os.write_file(test_file, test_source)! mut flags := []string{} if args.sanitize { flags << ['-cc', 'gcc', '-cflags', '-fsanitize=address,undefined -fno-omit-frame-pointer', '-ldflags', '-fsanitize=address,undefined'] // the runtime owns GC allocations; this pass checks invalid memory access. if os.setenv('ASAN_OPTIONS', 'detect_leaks=0', true) != 0 || os.setenv('UBSAN_OPTIONS', 'halt_on_error=1', true) != 0 { return os.last_error() } } list_binary := os.join_path(base, 'list-parity') mut build := flags.clone() build << ['-o', list_binary, test_file] tooling.command(args.compiler, build)! print(tooling.command(list_binary, [])!) tooling.require_no_libalpm(list_binary)! println('Native lists matched checked-out alpm_list.c; no libalpm linked') mut faults := native for name in ['malloc', 'calloc', 'strdup', 'free'] { faults = faults.replace('C.' + name, 'C.list_alloc_' + name) } os.write_file(os.join_path(base, 'list.v'), faults)! header := os.join_path(base, 'list_alloc.h') os.cp(os.join_path(@VMODROOT, 'tests', 'list_alloc.h'), header)! fault_file := os.join_path(base, 'list_alloc_test.v') os.write_file(fault_file, os.read_file(os.join_path(@VMODROOT, 'tests', 'list_alloc_test.v.in'))!.replace('@ALLOC_HEADER@', header))! mut fault_build := flags.clone() fault_binary := os.join_path(base, 'list-faults') fault_build << ['-o', fault_binary, fault_file] tooling.command(args.compiler, fault_build)! print(tooling.command(fault_binary, [])!) println('Allocation-failure tests passed with no tracked leaks or invalid frees') }