Skip to content

Commit ac78aee

Browse files
committed
Introduce precompiled binaries for fuzz test
Initially introduced for unit test in commit 1239fd7 (cherry picked from commit 1d32186)
1 parent e0944b9 commit ac78aee

File tree

3 files changed

+125
-107
lines changed

3 files changed

+125
-107
lines changed

.github/workflows/fuzz-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
ls
5151
cd fuzz_testing/script
5252
export AOCL_BOARD_PACKAGE_ROOT="$(git rev-parse --show-toplevel)/test/board/a10_ref"
53+
export ACL_TEST_EXAMPLE_BINARY="$(git rev-parse --show-toplevel)/test/example_binary"
5354
NUM_OF_ITERATIONS=${{ github.event.inputs.num_of_iterations }}
5455
# This if block is only used during testing, because if this workflow is triggered via pull_request, ${{ github.event.inputs.num_of_iterations }} would be empty
5556
if [ -z "${NUM_OF_ITERATIONS}" ]; then

fuzz_testing/test/acl_fuzz_test.cpp

Lines changed: 124 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -218,91 +218,140 @@ const unsigned char *acl_test_get_example_binary(size_t *binary_len) {
218218
}
219219

220220
static void l_load_example_binary(void) {
221-
const char *envvar_offline_device = "CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA";
222-
const char *envvar_program_lib =
223-
"CL_CONTEXT_PROGRAM_EXE_LIBRARY_ROOT_INTELFPGA";
224-
const char *offline_old_value = acl_getenv(envvar_offline_device);
225-
const char *program_lib_old_value = acl_getenv(envvar_program_lib);
226-
int system_ret = -1;
227-
enum { MAX_DEVICES = 100 };
228-
cl_platform_id platform;
229-
cl_device_id device[MAX_DEVICES];
230-
cl_context context;
231-
cl_program program;
232-
cl_int status;
233-
234-
acl_test_setenv(envvar_offline_device, ACLTEST_DEFAULT_BOARD);
235-
acl_test_setenv(envvar_program_lib, ".acltest_builtin_prog");
236-
system_ret = system("rm -rf .acltest_builtin_prog");
237-
assert(system_ret != -1);
238-
239-
ACL_LOCKED(acl_test_setup_generic_system());
240-
241221
// Since this runs before the CppUTest runner is set up, we can't use
242222
// the CHECK* macros.
243223
// Just use asserts.
244224

245-
assert(CL_SUCCESS == clGetPlatformIDs(1, &platform, 0));
246-
assert(CL_SUCCESS == clGetDeviceIDs(platform, CL_DEVICE_TYPE_ACCELERATOR,
247-
MAX_DEVICES, device, 0));
225+
const char *envvar_example_binary = "ACL_TEST_EXAMPLE_BINARY";
226+
const char *example_binary_root = acl_getenv(envvar_example_binary);
227+
if (example_binary_root) {
228+
// Precompiled binaries exist, just read its content
229+
ACL_LOCKED(acl_test_setup_generic_system());
230+
#ifdef _WIN32
231+
std::string bin_file =
232+
std::string(example_binary_root) + "/windows/example.aocr";
233+
#else
234+
std::string bin_file =
235+
std::string(example_binary_root) + "/linux/example.aocr";
236+
#endif
237+
FILE *infile = fopen(bin_file.c_str(), "rb");
238+
assert(infile && "Cannot open example binary example.aocr, make sure "
239+
"ACL_TEST_EXAMPLE_BINARY is set to the correct path");
240+
241+
// Get binary length
242+
assert(fseek(infile, 0, SEEK_END) == 0);
243+
long int position = ftell(infile);
244+
assert(position != -1L);
245+
acl_test_example_binary_len = (size_t)position;
246+
// Return to beginning of file
247+
assert(fseek(infile, 0, SEEK_SET) == 0);
248+
249+
// Read binary
250+
acl_test_example_binary =
251+
(unsigned char *)acl_malloc(acl_test_example_binary_len);
252+
assert(acl_test_example_binary);
253+
assert(fread(acl_test_example_binary, sizeof(char),
254+
acl_test_example_binary_len,
255+
infile) == acl_test_example_binary_len);
256+
assert(fclose(infile) == 0);
257+
} else {
258+
// Precompiled binaries don't exist, do an actual compile with the
259+
// aoc compiler and store the binary as test example binary.
260+
const char *envvar_offline_device = "CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA";
261+
const char *envvar_program_lib =
262+
"CL_CONTEXT_PROGRAM_EXE_LIBRARY_ROOT_INTELFPGA";
263+
const char *offline_old_value = acl_getenv(envvar_offline_device);
264+
const char *program_lib_old_value = acl_getenv(envvar_program_lib);
265+
int system_ret = -1;
266+
enum { MAX_DEVICES = 100 };
267+
cl_platform_id platform;
268+
cl_device_id device[MAX_DEVICES];
269+
cl_context context;
270+
cl_program program;
271+
cl_int status;
272+
273+
acl_test_setenv(envvar_offline_device, ACLTEST_DEFAULT_BOARD);
274+
acl_test_setenv(envvar_program_lib, ".acltest_builtin_prog");
275+
system_ret = system("rm -rf .acltest_builtin_prog");
276+
assert(system_ret != -1);
248277

249-
cl_context_properties props[] = {
250-
CL_CONTEXT_COMPILER_MODE_INTELFPGA,
251-
CL_CONTEXT_COMPILER_MODE_OFFLINE_CREATE_EXE_LIBRARY_INTELFPGA, 0};
252-
context = clCreateContext(props, 1, device, acl_test_notify_print, 0, 0);
253-
assert(context);
254-
255-
const char *src =
256-
"kernel void vecaccum(global int*A, global int*B) {\n"
257-
" size_t gid = get_global_id(0);\n"
258-
" A[gid] += B[gid];\n"
259-
"};\n"
260-
// This one has two constant arguments.
261-
"kernel void vecsum(global int*A, constant int*B, constant int*C) {\n"
262-
" size_t gid = get_global_id(0);\n"
263-
" A[gid] = B[gid] + C[gid];\n"
264-
"};\n"
265-
266-
// This has a printf.
267-
"kernel void printit(global int*A) {\n"
268-
" printf(\"Hello world! %d\\n\", A[0]);\n"
269-
"};\n";
270-
271-
program = clCreateProgramWithSource(context, 1, &src, 0, 0);
272-
assert(program);
273-
274-
status = clBuildProgram(program, 1, device, "-cl-kernel-arg-info", 0, 0);
275-
if (status != CL_SUCCESS) {
276-
printf("Compilation failed. Kernel source is:\n-----\n%s\n----\n", src);
278+
ACL_LOCKED(acl_test_setup_generic_system());
279+
280+
assert(CL_SUCCESS == clGetPlatformIDs(1, &platform, 0));
281+
assert(CL_SUCCESS == clGetDeviceIDs(platform, CL_DEVICE_TYPE_ACCELERATOR,
282+
MAX_DEVICES, device, 0));
283+
284+
cl_context_properties props[] = {
285+
CL_CONTEXT_COMPILER_MODE_INTELFPGA,
286+
CL_CONTEXT_COMPILER_MODE_OFFLINE_CREATE_EXE_LIBRARY_INTELFPGA, 0};
287+
context = clCreateContext(props, 1, device, acl_test_notify_print, 0, 0);
288+
assert(context);
289+
290+
const char *src =
291+
"kernel void vecaccum(global int*A, global int*B) {\n"
292+
" size_t gid = get_global_id(0);\n"
293+
" A[gid] += B[gid];\n"
294+
"};\n"
295+
// This one has two constant arguments.
296+
"kernel void vecsum(global int*A, constant int*B, constant int*C) {\n"
297+
" size_t gid = get_global_id(0);\n"
298+
" A[gid] = B[gid] + C[gid];\n"
299+
"};\n"
300+
301+
// This has a printf.
302+
"kernel void printit(global int*A) {\n"
303+
" printf(\"Hello world! %d\\n\", A[0]);\n"
304+
"};\n";
305+
306+
program = clCreateProgramWithSource(context, 1, &src, 0, 0);
307+
assert(program);
308+
309+
status = clBuildProgram(program, 1, device, "-cl-kernel-arg-info", 0, 0);
310+
if (status != CL_SUCCESS) {
311+
printf("Compilation failed. Kernel source is:\n-----\n%s\n----\n", src);
312+
size_t log_size = 0;
313+
clGetProgramBuildInfo(program, device[0], CL_PROGRAM_BUILD_LOG, 0, 0,
314+
&log_size);
315+
char *log = (char *)acl_malloc(log_size);
316+
clGetProgramBuildInfo(program, device[0], CL_PROGRAM_BUILD_LOG, log_size,
317+
log, 0);
318+
if (log)
319+
printf("Build log is:\n-----\n%s\n----\n", log);
320+
exit(1);
321+
}
322+
323+
// The build log should not be empty
277324
size_t log_size = 0;
325+
size_t empty_log_size = 1;
278326
clGetProgramBuildInfo(program, device[0], CL_PROGRAM_BUILD_LOG, 0, 0,
279327
&log_size);
280-
char *log = (char *)acl_malloc(log_size);
281-
clGetProgramBuildInfo(program, device[0], CL_PROGRAM_BUILD_LOG, log_size,
282-
log, 0);
283-
if (log)
284-
printf("Build log is:\n-----\n%s\n----\n", log);
285-
exit(1);
328+
assert(log_size > empty_log_size);
329+
330+
acl_test_example_binary_len = 0;
331+
assert(CL_SUCCESS == clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES,
332+
sizeof(size_t),
333+
&acl_test_example_binary_len, 0));
334+
acl_test_example_binary =
335+
(unsigned char *)acl_malloc(acl_test_example_binary_len);
336+
assert(acl_test_example_binary);
337+
assert(CL_SUCCESS == clGetProgramInfo(program, CL_PROGRAM_BINARIES,
338+
sizeof(acl_test_example_binary),
339+
&acl_test_example_binary, 0));
340+
341+
// Don't leak
342+
clReleaseProgram(program);
343+
clReleaseContext(context);
344+
345+
acl_test_unsetenv(envvar_offline_device);
346+
if (offline_old_value) {
347+
acl_test_setenv(envvar_offline_device, offline_old_value);
348+
}
349+
acl_test_unsetenv(envvar_program_lib);
350+
if (program_lib_old_value) {
351+
acl_test_setenv(envvar_program_lib, program_lib_old_value);
352+
}
286353
}
287354

288-
// The build log should not be empty
289-
size_t log_size = 0;
290-
size_t empty_log_size = 1;
291-
clGetProgramBuildInfo(program, device[0], CL_PROGRAM_BUILD_LOG, 0, 0,
292-
&log_size);
293-
assert(log_size > empty_log_size);
294-
295-
acl_test_example_binary_len = 0;
296-
assert(CL_SUCCESS == clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES,
297-
sizeof(size_t),
298-
&acl_test_example_binary_len, 0));
299-
acl_test_example_binary =
300-
(unsigned char *)acl_malloc(acl_test_example_binary_len);
301-
assert(acl_test_example_binary);
302-
assert(CL_SUCCESS == clGetProgramInfo(program, CL_PROGRAM_BINARIES,
303-
sizeof(acl_test_example_binary),
304-
&acl_test_example_binary, 0));
305-
306355
// Save the derived sysdef for later tests.
307356
{
308357
acl_pkg_file_t pkg;
@@ -334,19 +383,6 @@ static void l_load_example_binary(void) {
334383
acl_pkg_close_file(pkg);
335384
}
336385

337-
// Don't leak
338-
clReleaseProgram(program);
339-
clReleaseContext(context);
340-
341-
acl_test_unsetenv(envvar_offline_device);
342-
if (offline_old_value) {
343-
acl_test_setenv(envvar_offline_device, offline_old_value);
344-
}
345-
acl_test_unsetenv(envvar_program_lib);
346-
if (program_lib_old_value) {
347-
acl_test_setenv(envvar_program_lib, program_lib_old_value);
348-
}
349-
350386
ACL_LOCKED(acl_test_teardown_generic_system());
351387
}
352388

fuzz_testing/test/acl_fuzz_test.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,10 @@ typedef struct {
8686
void *status_user_data;
8787
} acl_hal_device_test;
8888

89-
// This must match the define in acl_kernel_if.c
90-
#define KERNEL_VERSION_ID (0xa0c00001)
91-
92-
// These must match the defines in acl_kernel_if.c
93-
#define OFFSET_VERSION_ID ((dev_addr_t)0x0000)
94-
#define OFFSET_KERNEL_CRA_SEGMENT ((dev_addr_t)0x0020)
95-
#define OFFSET_SW_RESET ((dev_addr_t)0x0030)
9689
// Default mem_org address.
9790
// Runtime is now using one loaded from autodiscovery,
9891
// rather than hard coded value.
9992
// For tests, autodiscovery will still have the default value.
10093
#define OFFSET_MEM_ORG ((dev_addr_t)0x0018)
101-
#define OFFSET_KERNEL_CRA ((dev_addr_t)0x1000)
102-
#define OFFSET_CONFIGURATION_ROM ((dev_addr_t)0x2000)
103-
104-
// These must match the defines in acl_pll.c
105-
#define OFFSET_ROM ((dev_addr_t)0x400)
106-
#define OFFSET_RECONFIG_CTRL ((dev_addr_t)0x200)
107-
#define OFFSET_COUNTER ((dev_addr_t)0x100)
108-
#define OFFSET_RESET ((dev_addr_t)0x110)
109-
#define OFFSET_LOCK ((dev_addr_t)0x120)
110-
111-
// This must match the define in acl_pll.c
112-
#define MAX_KNOWN_SETTINGS 100
11394

11495
#endif

0 commit comments

Comments
 (0)