xref: /freebsd/contrib/file/src/memtest.c (revision 898496ee)
143a5ec4eSXin LI /*
243a5ec4eSXin LI  * Copyright (c) Christos Zoulas 2021.
343a5ec4eSXin LI  * All Rights Reserved.
443a5ec4eSXin LI  *
543a5ec4eSXin LI  * Redistribution and use in source and binary forms, with or without
643a5ec4eSXin LI  * modification, are permitted provided that the following conditions
743a5ec4eSXin LI  * are met:
843a5ec4eSXin LI  * 1. Redistributions of source code must retain the above copyright
943a5ec4eSXin LI  *    notice immediately at the beginning of the file, without modification,
1043a5ec4eSXin LI  *    this list of conditions, and the following disclaimer.
1143a5ec4eSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
1243a5ec4eSXin LI  *    notice, this list of conditions and the following disclaimer in the
1343a5ec4eSXin LI  *    documentation and/or other materials provided with the distribution.
1443a5ec4eSXin LI  *
1543a5ec4eSXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1643a5ec4eSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1743a5ec4eSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1843a5ec4eSXin LI  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
1943a5ec4eSXin LI  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2043a5ec4eSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2143a5ec4eSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2243a5ec4eSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2343a5ec4eSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2443a5ec4eSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2543a5ec4eSXin LI  * SUCH DAMAGE.
2643a5ec4eSXin LI  */
27*898496eeSXin LI #include "file.h"
2843a5ec4eSXin LI 
29a2dfb722SXin LI #ifndef lint
30*898496eeSXin LI FILE_RCSID("@(#)$File: memtest.c,v 1.6 2022/09/24 20:30:13 christos Exp $")
31a2dfb722SXin LI #endif
32a2dfb722SXin LI 
3343a5ec4eSXin LI #include <sys/types.h>
3443a5ec4eSXin LI #include <sys/stat.h>
3543a5ec4eSXin LI #include <sys/mman.h>
3643a5ec4eSXin LI #include <stdio.h>
3743a5ec4eSXin LI #include <stdbool.h>
3843a5ec4eSXin LI #include <string.h>
3943a5ec4eSXin LI #include <stdlib.h>
4043a5ec4eSXin LI #include <err.h>
4143a5ec4eSXin LI #include <fcntl.h>
4243a5ec4eSXin LI #include <unistd.h>
4343a5ec4eSXin LI #include <dlfcn.h>
4443a5ec4eSXin LI #include <magic.h>
4543a5ec4eSXin LI 
4643a5ec4eSXin LI void *
malloc(size_t len)4743a5ec4eSXin LI malloc(size_t len)
4843a5ec4eSXin LI {
4943a5ec4eSXin LI 	char buf[512];
5043a5ec4eSXin LI 	void *(*orig)(size_t) = dlsym(RTLD_NEXT, "malloc");
5143a5ec4eSXin LI 	void *p = (*orig)(len);
5243a5ec4eSXin LI 	int l = snprintf(buf, sizeof(buf), "malloc %zu %p\n", len, p);
5343a5ec4eSXin LI 	write(2, buf, l);
5443a5ec4eSXin LI 	return p;
5543a5ec4eSXin LI }
5643a5ec4eSXin LI 
5743a5ec4eSXin LI void
free(void * p)5843a5ec4eSXin LI free(void *p)
5943a5ec4eSXin LI {
6043a5ec4eSXin LI 	char buf[512];
6143a5ec4eSXin LI 	void (*orig)(void *) = dlsym(RTLD_NEXT, "free");
6243a5ec4eSXin LI 	(*orig)(p);
6343a5ec4eSXin LI 	int l = snprintf(buf, sizeof(buf), "free %p\n", p);
6443a5ec4eSXin LI 	write(2, buf, l);
6543a5ec4eSXin LI }
6643a5ec4eSXin LI 
6743a5ec4eSXin LI void *
calloc(size_t len,size_t nitems)6843a5ec4eSXin LI calloc(size_t len, size_t nitems)
6943a5ec4eSXin LI {
7043a5ec4eSXin LI 	char buf[512];
7143a5ec4eSXin LI 	void *(*orig)(size_t, size_t) = dlsym(RTLD_NEXT, "calloc");
7243a5ec4eSXin LI 	void *p = (*orig)(len, nitems);
7343a5ec4eSXin LI 	size_t tot = len * nitems;
7443a5ec4eSXin LI 	int l = snprintf(buf, sizeof(buf), "calloc %zu %p\n", tot, p);
7543a5ec4eSXin LI 	write(2, buf, l);
7643a5ec4eSXin LI 	return p;
7743a5ec4eSXin LI }
7843a5ec4eSXin LI void *
realloc(void * q,size_t len)7943a5ec4eSXin LI realloc(void *q, size_t len)
8043a5ec4eSXin LI {
8143a5ec4eSXin LI 	char buf[512];
8243a5ec4eSXin LI 	void *(*orig)(void *, size_t) = dlsym(RTLD_NEXT, "realloc");
8343a5ec4eSXin LI 	void *p = (*orig)(q, len);
8443a5ec4eSXin LI 	int l = snprintf(buf, sizeof(buf), "realloc %zu %p\n", len, p);
8543a5ec4eSXin LI 	write(2, buf, l);
8643a5ec4eSXin LI 	return p;
8743a5ec4eSXin LI }
8843a5ec4eSXin LI 
8943a5ec4eSXin LI static void
usage(void)9043a5ec4eSXin LI usage(void)
9143a5ec4eSXin LI {
9243a5ec4eSXin LI 	fprintf(stderr, "Usage: test [-b] <filename>\n");
9343a5ec4eSXin LI 	exit(EXIT_FAILURE);
9443a5ec4eSXin LI }
9543a5ec4eSXin LI 
9643a5ec4eSXin LI int
main(int argc,char * argv[])9743a5ec4eSXin LI main(int argc, char *argv[])
9843a5ec4eSXin LI {
9943a5ec4eSXin LI 	bool buf = false;
10043a5ec4eSXin LI 	int c;
10143a5ec4eSXin LI 
10243a5ec4eSXin LI 	while ((c = getopt(argc, argv, "b")) != -1)
10343a5ec4eSXin LI 		switch (c) {
10443a5ec4eSXin LI 		case 'b':
10543a5ec4eSXin LI 			buf = true;
10643a5ec4eSXin LI 			break;
10743a5ec4eSXin LI 		default:
10843a5ec4eSXin LI 			usage();
10943a5ec4eSXin LI 		}
11043a5ec4eSXin LI 
11143a5ec4eSXin LI 	argc -= optind;
11243a5ec4eSXin LI 	argv += optind;
11343a5ec4eSXin LI 
11443a5ec4eSXin LI 	if (argc == 0)
11543a5ec4eSXin LI 		usage();
11643a5ec4eSXin LI 
11743a5ec4eSXin LI 	magic_t m = magic_open(0);
11843a5ec4eSXin LI 	if (m == NULL)
11943a5ec4eSXin LI 		err(EXIT_FAILURE, "magic_open");
12043a5ec4eSXin LI 
12143a5ec4eSXin LI 	magic_load(m, NULL);
12243a5ec4eSXin LI 
12343a5ec4eSXin LI 	const char *r;
12443a5ec4eSXin LI 	if (buf) {
12543a5ec4eSXin LI 		int fd = open(argv[0], O_RDONLY);
12643a5ec4eSXin LI 		if (fd == -1)
12743a5ec4eSXin LI 			err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
12843a5ec4eSXin LI 
12943a5ec4eSXin LI 		struct stat st;
13043a5ec4eSXin LI 		if (fstat(fd, &st) == -1)
13143a5ec4eSXin LI 			err(EXIT_FAILURE, "Cannot stat `%s'", argv[0]);
13243a5ec4eSXin LI 		size_t l = (size_t)st.st_size;
13343a5ec4eSXin LI 		void *p = mmap(NULL, l, PROT_READ, MAP_FILE | MAP_PRIVATE, fd,
13443a5ec4eSXin LI 		    (off_t)0);
13543a5ec4eSXin LI 		if (p == MAP_FAILED)
13643a5ec4eSXin LI 			err(EXIT_FAILURE, "Cannot map `%s'", argv[0]);
13743a5ec4eSXin LI 		close(fd);
13843a5ec4eSXin LI 		r = magic_buffer(m, p, l);
13943a5ec4eSXin LI 		munmap(p, l);
14043a5ec4eSXin LI 	} else {
14143a5ec4eSXin LI 		r = magic_file(m, argv[0]);
14243a5ec4eSXin LI 	}
14343a5ec4eSXin LI 	magic_close(m);
14443a5ec4eSXin LI 
14543a5ec4eSXin LI 	printf("%s\n", r ? r : "(null)");
14643a5ec4eSXin LI 
14743a5ec4eSXin LI 	return 0;
14843a5ec4eSXin LI }
149