summaryrefslogtreecommitdiff
path: root/tests/bionic/libc/common/test_executable_destructor.c
blob: e0e1d454f8622db47f51c4b3de7009d5984e56a3 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (C) 2010 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/resource.h>

/* A very simple program used to test constructor and destructor functions
 * in executables (instead of shared libraries).
 */

int x = 0;

/* Initialize x to 1 when the program starts. This will be checked
 * later by the main() function.
 */
static void __attribute__((constructor))
on_load(void)
{
    x = 1;
}

/* Crash intentionally if 'x' is set to 1 */
static void __attribute__((destructor))
on_exit(void)
{
    if (x == 1)
        *(int*)(void*)0 = 10;  /* force a crash */
}

int main(void)
{
    int status;
    pid_t pid;

    /* First, check that the constructor was properly called ! */
    if (x != 1) {
        fprintf(stderr, "Constructor function not called!!\n");
        return 1;
    }

    /* prevent our crashing child process from generating a core file */
    {
        struct rlimit rlim;
        rlim.rlim_cur = 0;
        rlim.rlim_max = RLIM_INFINITY;
        setrlimit(RLIMIT_CORE, &rlim);
    }

    /* Fork the current process, then wait for the child to exit
     * and crash.
     */
    pid = fork();
    if (pid < 0) {
        fprintf(stderr, "Could not fork process: %s\n", strerror(errno));
        return 2;
    }
    /* in the child, simply exit after 1 second. */
    if (pid == 0) {
        sleep(1);
        return 0;
    }
    /* in the parent, wait for the child to terminate */
    if (wait(&status) < 0) {
        fprintf(stderr, "Could not wait for child: %s\n", strerror(errno));
        return 3;
    }
    if (!WIFSIGNALED(status)) {
        fprintf(stderr, "Destructor not called!!\n");
        return 4;
    }

    /* Prevent crashing */
    x = 2;
    printf("ok\n");
    return 0;
}