File: objc-getclass.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (87 lines) | stat: -rw-r--r-- 2,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// RUN: %empty-directory(%t)
// RUN: %target-clang %s -isysroot %sdk -L%swift-lib-dir/swift/%target-sdk-name -lswiftCore -lobjc -o %t/objc-getclass
// RUN: %target-codesign %t/objc-getclass
// RUN: %target-run %t/objc-getclass %S/Inputs/objc-getclass.txt

// REQUIRES: executable_test
// REQUIRES: objc_interop

// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime

#include <objc/runtime.h>
#include <dlfcn.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>

static BOOL dummyHook(const char * _Nonnull name,
                      Class _Nullable * _Nonnull outClass) {
  return NO;
}

int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "usage: objc-getclass <input.txt>\n"
            "\n"
            "Test demangling class names to get classes.\n");
    return 0;
  }

  // Find the class-by-mangled-name hook
  objc_hook_getClass getObjCClassByMangledName = NULL;

  if (__builtin_available(macOS 10.14.4, iOS 12.2, tvOS 12.2, watchOS 5.2, *)) {
    objc_hook_getClass dummy = NULL;
    objc_setHook_getClass(dummyHook, &getObjCClassByMangledName);
    objc_setHook_getClass(getObjCClassByMangledName, &dummy);
  } else {
    fprintf(stderr, "objc-getclass: OS version is too old\n");
    return 0;
  }

  // Open the input file
  FILE *fp = fopen(argv[1], "rt");
  if (!fp) {
    fprintf(stderr, "objc-getclass: unable to open \"%s\" - %s\n",
            argv[1], strerror(errno));
  }

  // Input file is a list of manglings; we don't really care what classes they
  // resolve to here; this test is about whether or not they actually crash or
  // assert.
  char *line = NULL;
  size_t linecap = 0;
  ssize_t linelen = 0;

  while ((linelen = getline(&line, &linecap, fp)) > 0) {
    char *mangling = line;

    // Trim whitespace
    while (isspace(*mangling))
      ++mangling;

    char *end = line + linelen;
    while (end > line && isspace(end[-1]))
      --end;
    *end = '\0';

    // Skip comments and blank lines
    if (*mangling == '#' || !*mangling)
      continue;

    // Try to get a class
    Class outClass = nil;
    BOOL result = getObjCClassByMangledName(mangling, &outClass);

    if (result)
      printf("%s -> %s\n", mangling, class_getName(outClass));
    else
      printf("%s not found\n", mangling);
  }

  fclose(fp);

  return 0;
}