Rizin
unix-like reverse engineering framework and cli tools
liboverride-test.c
Go to the documentation of this file.
1 /*
2  liboverride.c -- override function called by zip_open()
3 
4  Copyright (C) 2017-2022 Dieter Baron and Thomas Klausner
5 
6  This file is part of ckmame, a program to check rom sets for MAME.
7  The authors can be contacted at <ckmame@nih.at>
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12  1. Redistributions of source code must retain the above copyright
13  notice, this list of conditions and the following disclaimer.
14  2. Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions and the following disclaimer in
16  the documentation and/or other materials provided with the
17  distribution.
18  3. The name of the author may not be used to endorse or promote
19  products derived from this software without specific prior
20  written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
23  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
26  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #ifdef _WIN32
40 int main(int argc, const char *argv[]) {
41  /* Symbol override is not supported on Windows. */
42  if (argc > 1 && strcmp(argv[1], "-v") == 0) {
43  printf("not supported on Windows\n");
44  }
45  exit(1);
46 }
47 #else
48 
49 #include <errno.h>
50 #include <unistd.h>
51 
52 #include "zip.h"
53 
54 /*
55  Some systems bind functions called and defined within a shared library, so the override doesn't work. This program calls zip_open and checks whether the override worked.
56  */
57 
58 int
59 main(int argc, const char *argv[]) {
60  int verbose = 0;
61  int error_code;
62 
63  if (argc > 1 && strcmp(argv[1], "-v") == 0) {
64  verbose = 1;
65  }
66 
67  if (getenv("LIBOVERRIDE_SET") == NULL) {
68  char *cwd = getcwd(NULL, 0);
69  char *so = (char *)malloc(strlen(cwd) + 64);
70  if (so == NULL) {
71  if (verbose) {
72  printf("malloc failed\n");
73  }
74  exit(2);
75  }
76  sprintf(so, "%s/libliboverride.so", cwd);
77  setenv("LIBOVERRIDE_SET", "1", 1);
78  setenv("LD_PRELOAD", so, 1);
79  execv(argv[0], (void *)argv);
80  if (verbose) {
81  printf("exec failed: %s\n", strerror(errno));
82  }
83  exit(2);
84  }
85 
86  if (zip_open("nosuchfile", 0, &error_code) != NULL) {
87  /* We expect failure. */
88  if (verbose) {
89  printf("open succeeded\n");
90  }
91  exit(1);
92  }
93  if (error_code != 32000) {
94  /* Override didn't take, we didn't get its magic error code. */
95  if (verbose) {
96  printf("got unexpected error %d\n", error_code);
97  }
98  exit(1);
99  }
100 
101  if (verbose) {
102  printf("success\n");
103  }
104  exit(0);
105 }
106 
107 #endif /* not Windows */
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
sprintf
Definition: kernel.h:365
int main(int argc, const char *argv[])
ZIP_EXTERN zip_t *_Nullable zip_open(const char *_Nonnull, int, int *_Nullable)
Definition: zip_open.c:54
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
char * getenv()
static int verbose
Definition: z80asm.c:73