From d82407951a425f7682b3779afb5cee618f27d29c Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 11 Jul 2021 01:51:49 +0100 Subject: [PATCH] Mapping +unmappign haystacks from cl. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: On mapping failure of haystack, unmap the previous ones before exiting `map_haystacks()` Fortune for naka's current commit: Middle blessing − 中吉 --- include/project.h | 2 ++ src/main.c | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/project.h b/include/project.h index df16259..64ac42d 100644 --- a/include/project.h +++ b/include/project.h @@ -2,6 +2,8 @@ #ifndef _PROJECT_H #define _PROJECT_H +#include + #include "macros.h" #include "ints.h" #include "version.h" diff --git a/src/main.c b/src/main.c index f4e15bd..eba5225 100644 --- a/src/main.c +++ b/src/main.c @@ -45,6 +45,20 @@ noreturn void usage_then_exit(int err, int argc, char** argv) exit(err); } +static int map_haystacks(const char* const * h, map_t maps[pOUT]) +{ + const char* path; + while( (path = *h++) ) + { + map_t *pOUT c = maps++; + TRACE("attempting to map `%s' to map %p", path, c); + debug_assert(c); + if(!map_handle_err(map_file(path, false, 0, 0, c))) + //TODO: Handle unmapping previous haystacks before return + return 0; + } + return 1; +} int main(int argc, char** argv) { @@ -63,7 +77,13 @@ inv_args: if(!map_handle_err(map_file(argv[1], false, 0, 0, &needle))) return PROG_RET_MAP_NEEDLE_FAILED; if(!map_handle_err(map_preload(&needle, false))) WARN("Failed to advise kernel about memory access: needle"); - //TODO: Map argv[2...] + // Remove exe name from argv: now is (needle, haystacks...) + argv+=1; + // Setup haystack maps + map_t haystacks[argc-2]; + TRACE("Attempting to map %d haystacks", argc-2); + if(!map_haystacks((const char**)(argv+1), haystacks)) return PROG_RET_MAP_HAYSTACK_FAILED; + //TODO: Setup thread-pool. //TODO: Dispatch haystack maps to threadpool. //TODO: Use either `cmp_find()` or `cmp_find_many()` to find the `needle` (mapped above) within those haystacks. @@ -71,6 +91,10 @@ inv_args: //TODO: Join the threadpool and consolidate results. //TODO: Iterate through the haystack numbers match results, return the first non-match haystack number through `PROG_RET_MATCH_HAYSTACK_N_FAILED(n)` (haystack numbers are always nonzero). If all were matched, return 0 + for(int i=0;i<(argc-2);i++) { + TRACE("Attempting to unmap haystack %d (%p, `%s')", i, haystacks+i, argv[i+1]); + if(!map_handle_err(map_free(haystacks[i]))) return PROG_RET_UNMAP_HAYSTACK_N_FAILED(i+1); + } if(!map_handle_err(map_free(needle))) return PROG_RET_UNMAP_NEEDLE_FAILED; TRACE("main end"); return 0;