~ubuntu-branches/ubuntu/saucy/libunwind/saucy

« back to all changes in this revision

Viewing changes to tests/Ltest-nocalloc.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-05-22 09:01:39 UTC
  • mfrom: (4.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130522090139-8x5unm22ox663v9l
Tags: 1.1-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
   -  Enable test suite for amd64 architecture:
     + d/rules: Execute test suite when building for amd64 only.
     + d/patches/20123007_disable_known_test_failures.patch: Disable *-dyn1
       tests as they use unsupported features on amd64.
     + d/patches/20123007_workaround_test-ptrace.patch: Walk default test
       arguments to ensure child process has visibility.
* Dropped changes:
  - d/patches/20122307_fix_setjmp.patch: Included in new upstream release.
  - d/patches/20123007_workaround_ltest-nocalloc.patch: Failing test no 
    longer in codebase.
* d/README.source: Added notes about building libunwind in chroots on
  Ubuntu systems with apport installed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* libunwind - a platform-independent unwind library
 
2
   Copyright (C) 2011 Google, Inc
 
3
        Contributed by Paul Pluzhnikov <ppluzhnikov@google.com>
 
4
 
 
5
Permission is hereby granted, free of charge, to any person obtaining
 
6
a copy of this software and associated documentation files (the
 
7
"Software"), to deal in the Software without restriction, including
 
8
without limitation the rights to use, copy, modify, merge, publish,
 
9
distribute, sublicense, and/or sell copies of the Software, and to
 
10
permit persons to whom the Software is furnished to do so, subject to
 
11
the following conditions:
 
12
 
 
13
The above copyright notice and this permission notice shall be
 
14
included in all copies or substantial portions of the Software.
 
15
 
 
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
18
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
20
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
21
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
22
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
23
 
1
24
#define UNW_LOCAL_ONLY
2
25
#include <libunwind.h>
3
 
#if !defined(UNW_REMOTE_ONLY)
4
 
#include "Gtest-nocalloc.c"
 
26
 
 
27
#include <unistd.h>
 
28
#include <stdio.h>
 
29
#include <stdlib.h>
 
30
#include <dlfcn.h>
 
31
#include <pthread.h>
 
32
 
 
33
#define panic(args...)                          \
 
34
        { fprintf (stderr, args); exit (-1); }
 
35
 
 
36
int num_mallocs;
 
37
int num_callocs;
 
38
int in_unwind;
 
39
 
 
40
void *
 
41
calloc(size_t n, size_t s)
 
42
{
 
43
  static void * (*func)(size_t, size_t);
 
44
 
 
45
#ifdef __GLIBC__
 
46
  /* In glibc, dlsym() calls calloc. Calling dlsym(RTLD_NEXT, "calloc") here
 
47
     causes infinite recursion.  Instead, we simply use it by its other
 
48
     name.  */
 
49
  extern void *__libc_calloc(size_t, size_t);
 
50
  if (!func)
 
51
    func = &__libc_calloc;
 
52
#else
 
53
  if(!func)
 
54
    func = dlsym(RTLD_NEXT, "calloc");
5
55
#endif
 
56
 
 
57
  if (in_unwind) {
 
58
    num_callocs++;
 
59
    return NULL;
 
60
  } else {
 
61
    return func(n, s);
 
62
  }
 
63
}
 
64
 
 
65
void *
 
66
malloc(size_t s)
 
67
{
 
68
  static void * (*func)(size_t);
 
69
 
 
70
  if(!func)
 
71
    func = dlsym(RTLD_NEXT, "malloc");
 
72
 
 
73
  if (in_unwind) {
 
74
    num_mallocs++;
 
75
    return NULL;
 
76
  } else {
 
77
    return func(s);
 
78
  }
 
79
}
 
80
 
 
81
static void
 
82
do_backtrace (void)
 
83
{
 
84
  const int num_levels = 100;
 
85
  void *pc[num_levels];
 
86
 
 
87
  in_unwind = 1;
 
88
  unw_backtrace(pc, num_levels);
 
89
  in_unwind = 0;
 
90
}
 
91
 
 
92
void
 
93
foo3 (void)
 
94
{
 
95
  do_backtrace ();
 
96
}
 
97
 
 
98
void
 
99
foo2 (void)
 
100
{
 
101
  foo3 ();
 
102
}
 
103
 
 
104
void
 
105
foo1 (void)
 
106
{
 
107
  foo2 ();
 
108
}
 
109
 
 
110
int
 
111
main (void)
 
112
{
 
113
  int i, num_errors;
 
114
 
 
115
  /* Create (and leak) 100 TSDs, then call backtrace()
 
116
     and check that it doesn't call malloc()/calloc().  */
 
117
  for (i = 0; i < 100; ++i) {
 
118
    pthread_key_t key;
 
119
    if (pthread_key_create (&key, NULL))
 
120
      panic ("FAILURE: unable to create key %d\n", i);
 
121
  }
 
122
  /* Call backtrace right after thread creation,
 
123
   * where we are sure that we're not inside malloc */
 
124
  do_backtrace();
 
125
  num_mallocs = num_callocs = 0;
 
126
  foo1 ();
 
127
  num_errors = num_mallocs + num_callocs;
 
128
  if (num_errors > 0)
 
129
    {
 
130
      fprintf (stderr,
 
131
               "FAILURE: detected %d error%s (malloc: %d, calloc: %d)\n",
 
132
               num_errors, num_errors > 1 ? "s" : "",
 
133
               num_mallocs, num_callocs);
 
134
      exit (-1);
 
135
    }
 
136
  return 0;
 
137
}