xref: /netbsd-src/external/bsd/file/dist/src/memtest.c (revision e15daa8be9575f7ad2ca804c7c7c2d7f8e182d98)
1 /*	$NetBSD: memtest.c,v 1.1.1.2 2023/08/18 18:36:50 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Christos Zoulas 2021.
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include "file.h"
30 
31 #ifndef lint
32 #if 0
33 FILE_RCSID("@(#)$File: memtest.c,v 1.6 2022/09/24 20:30:13 christos Exp $")
34 #else
35 __RCSID("$NetBSD: memtest.c,v 1.1.1.2 2023/08/18 18:36:50 christos Exp $");
36 #endif
37 #endif
38 
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/mman.h>
42 #include <stdio.h>
43 #include <stdbool.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <err.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <dlfcn.h>
50 #include <magic.h>
51 
52 void *
malloc(size_t len)53 malloc(size_t len)
54 {
55 	char buf[512];
56 	void *(*orig)(size_t) = dlsym(RTLD_NEXT, "malloc");
57 	void *p = (*orig)(len);
58 	int l = snprintf(buf, sizeof(buf), "malloc %zu %p\n", len, p);
59 	write(2, buf, l);
60 	return p;
61 }
62 
63 void
free(void * p)64 free(void *p)
65 {
66 	char buf[512];
67 	void (*orig)(void *) = dlsym(RTLD_NEXT, "free");
68 	(*orig)(p);
69 	int l = snprintf(buf, sizeof(buf), "free %p\n", p);
70 	write(2, buf, l);
71 }
72 
73 void *
calloc(size_t len,size_t nitems)74 calloc(size_t len, size_t nitems)
75 {
76 	char buf[512];
77 	void *(*orig)(size_t, size_t) = dlsym(RTLD_NEXT, "calloc");
78 	void *p = (*orig)(len, nitems);
79 	size_t tot = len * nitems;
80 	int l = snprintf(buf, sizeof(buf), "calloc %zu %p\n", tot, p);
81 	write(2, buf, l);
82 	return p;
83 }
84 void *
realloc(void * q,size_t len)85 realloc(void *q, size_t len)
86 {
87 	char buf[512];
88 	void *(*orig)(void *, size_t) = dlsym(RTLD_NEXT, "realloc");
89 	void *p = (*orig)(q, len);
90 	int l = snprintf(buf, sizeof(buf), "realloc %zu %p\n", len, p);
91 	write(2, buf, l);
92 	return p;
93 }
94 
95 static void
usage(void)96 usage(void)
97 {
98 	fprintf(stderr, "Usage: test [-b] <filename>\n");
99 	exit(EXIT_FAILURE);
100 }
101 
102 int
main(int argc,char * argv[])103 main(int argc, char *argv[])
104 {
105 	bool buf = false;
106 	int c;
107 
108 	while ((c = getopt(argc, argv, "b")) != -1)
109 		switch (c) {
110 		case 'b':
111 			buf = true;
112 			break;
113 		default:
114 			usage();
115 		}
116 
117 	argc -= optind;
118 	argv += optind;
119 
120 	if (argc == 0)
121 		usage();
122 
123 	magic_t m = magic_open(0);
124 	if (m == NULL)
125 		err(EXIT_FAILURE, "magic_open");
126 
127 	magic_load(m, NULL);
128 
129 	const char *r;
130 	if (buf) {
131 		int fd = open(argv[0], O_RDONLY);
132 		if (fd == -1)
133 			err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
134 
135 		struct stat st;
136 		if (fstat(fd, &st) == -1)
137 			err(EXIT_FAILURE, "Cannot stat `%s'", argv[0]);
138 		size_t l = (size_t)st.st_size;
139 		void *p = mmap(NULL, l, PROT_READ, MAP_FILE | MAP_PRIVATE, fd,
140 		    (off_t)0);
141 		if (p == MAP_FAILED)
142 			err(EXIT_FAILURE, "Cannot map `%s'", argv[0]);
143 		close(fd);
144 		r = magic_buffer(m, p, l);
145 		munmap(p, l);
146 	} else {
147 		r = magic_file(m, argv[0]);
148 	}
149 	magic_close(m);
150 
151 	printf("%s\n", r ? r : "(null)");
152 
153 	return 0;
154 }
155