From 825fb23f51f592bf3f430b74c3055083e0714810 Mon Sep 17 00:00:00 2001 From: Alex Wiens Date: Tue, 17 Dec 2024 13:41:35 +0100 Subject: [PATCH 1/3] likwid-bench: Fix use-after-free of testcase string Would segfault with the example parameters "-t clload -l clload", because the "testcase" variable is freed on "-t" processing and used in "-l" processing. --- bench/likwid-bench.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bench/likwid-bench.c b/bench/likwid-bench.c index e27fb04b3..875b597b3 100644 --- a/bench/likwid-bench.c +++ b/bench/likwid-bench.c @@ -355,6 +355,7 @@ int main(int argc, char** argv) } } bdestroy(testcase); + testcase = bfromcstr("none"); if (!builtin) { dynbench_close(test, NULL); @@ -410,6 +411,7 @@ int main(int argc, char** argv) return EXIT_FAILURE; } bdestroy(testcase); + testcase = bfromcstr("none"); break; case 'o': case 'f': From 01d1380122cbdf51161d23044b42daf584db9268 Mon Sep 17 00:00:00 2001 From: Alex Wiens Date: Tue, 17 Dec 2024 16:44:25 +0100 Subject: [PATCH 2/3] likwid-bench: Extend stream size number parsing to 64 bit numbers --- bench/src/strUtil.c | 48 +++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/bench/src/strUtil.c b/bench/src/strUtil.c index 2b1fd7d5b..434978323 100644 --- a/bench/src/strUtil.c +++ b/bench/src/strUtil.c @@ -61,6 +61,38 @@ str2int(const char* str) return (int) val; } +static int +str2uint64_t(const char* str, uint64_t* result) +{ + char* endptr; + errno = 0; + unsigned long long val; + val = strtoull(str, &endptr, 10); + + if ((errno == ERANGE && val == ULLONG_MAX) + || (errno != 0 && val == 0)) + { + fprintf(stderr, "Value in string %s out of range\n", str); + return -EINVAL; + } + + if (endptr == str) + { + fprintf(stderr, "No digits were found in %s\n", str); + return -EINVAL; + } + + // long long could be larger than 64 bits + if (val > 0xFFFFFFFFFFFFFFFF) + { + fprintf(stderr, "Value in string %s out of range for uint64_t\n", str); + return -EINVAL; + } + + *result = val; + return 1; +} + /* ##### FUNCTION DEFINITIONS - EXPORTED FUNCTIONS ################## */ uint64_t @@ -79,21 +111,13 @@ bstr_to_doubleSize(const_bstring str, DataType type) { return 0; } - ret = str2int(bdata(sizeStr)); - if (ret >= 0) - { - sizeU = str2int(bdata(sizeStr)); - } - else + ret = str2uint64_t(bdata(sizeStr), &sizeU); + if (ret < 0) { return 0; } - ret = str2int(bdata(single_sizeStr)); - if (ret >= 0) - { - single_sizeU = str2int(bdata(single_sizeStr)); - } - else + ret = str2uint64_t(bdata(single_sizeStr), &single_sizeU); + if (ret < 0) { return 0; } From cef321d23fa5eecaff13c09ecf90ffa39357852f Mon Sep 17 00:00:00 2001 From: Alex Wiens Date: Tue, 17 Dec 2024 16:46:16 +0100 Subject: [PATCH 3/3] likwid-bench: Add base 2 units to stream size parameter Add kiB, KiB, MiB, GiB as possible units. --- bench/likwid-bench.c | 2 +- bench/src/strUtil.c | 22 ++++++++++++++++++++++ doc/likwid-bench.1 | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/bench/likwid-bench.c b/bench/likwid-bench.c index 875b597b3..0abb7ba26 100644 --- a/bench/likwid-bench.c +++ b/bench/likwid-bench.c @@ -78,7 +78,7 @@ extern void* getIterSingle(void* arg); printf("-t/--test \t type of test \n"); \ printf("-w/--workgroup\t\t :[:[::]-:[:]\n"); \ printf("-W/--Workgroup\t\t :[:[::]]\n"); \ - printf("\t\t\t\t in kB, MB or GB (mandatory)\n"); \ + printf("\t\t\t\t in B, kB, MB, GB, kiB, MiB or GiB (mandatory)\n"); \ printf("For dynamically loaded benchmarks\n"); \ printf("-f/--tempdir \t Specify a folder for the temporary files. default: /tmp\n"); \ printf("-o/--asmout \t Save generated assembly to file\n"); \ diff --git a/bench/src/strUtil.c b/bench/src/strUtil.c index 434978323..a85e49d68 100644 --- a/bench/src/strUtil.c +++ b/bench/src/strUtil.c @@ -101,10 +101,13 @@ bstr_to_doubleSize(const_bstring str, DataType type) int ret; bstring unit = bmidstr(str, blength(str)-2, 2); bstring single_unit = bmidstr(str, blength(str)-1, 1); + bstring triple_unit = bmidstr(str, blength(str)-3, 3); bstring sizeStr = bmidstr(str, 0, blength(str)-2); bstring single_sizeStr = bmidstr(str, 0, blength(str)-1); + bstring triple_sizeStr = bmidstr(str, 0, blength(str)-3); uint64_t sizeU = 0; uint64_t single_sizeU = 0; + uint64_t triple_sizeU = 0; uint64_t junk = 0; uint64_t bytesize = 0; if (blength(sizeStr) == 0) @@ -121,6 +124,11 @@ bstr_to_doubleSize(const_bstring str, DataType type) { return 0; } + ret = str2uint64_t(bdata(triple_sizeStr), &triple_sizeU); + if (ret < 0) + { + return 0; + } bytesize = allocator_dataTypeLength(type); @@ -136,14 +144,28 @@ bstr_to_doubleSize(const_bstring str, DataType type) { junk = (sizeU *1000000000)/bytesize; } + else if ((biseqcstr(triple_unit, "kiB"))||(biseqcstr(triple_unit, "KiB"))) + { + junk = (triple_sizeU *1024)/bytesize; + } + else if (biseqcstr(triple_unit, "MiB")) + { + junk = (triple_sizeU *1024*1024)/bytesize; + } + else if (biseqcstr(triple_unit, "GiB")) + { + junk = (triple_sizeU *1024*1024*1024)/bytesize; + } else if (biseqcstr(single_unit, "B")) { junk = (single_sizeU)/bytesize; } bdestroy(unit); bdestroy(single_unit); + bdestroy(triple_unit); bdestroy(sizeStr); bdestroy(single_sizeStr); + bdestroy(triple_sizeStr); return junk; } diff --git a/doc/likwid-bench.1 b/doc/likwid-bench.1 index a6b78b68b..df7347170 100644 --- a/doc/likwid-bench.1 +++ b/doc/likwid-bench.1 @@ -71,7 +71,7 @@ Filepath for the dynamic generation of benchmarks. Default /tmp/. is alway .SH WORKGROUP SYNTAX .B : [:[::]] [-:] -with size in kB, MB or GB. The +with size in B, kB, MB, GB, kiB, MiB or GiB. The .B defines where the threads are placed. .B