1/*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20//
21//  badpointers.m
22//  gctests
23//
24//  Created by Blaine Garst on 11/11/08.
25//  Copyright 2008 Apple. All rights reserved.
26//
27// CONFIG GC -C99 -lauto
28
29#include <auto_zone.h>
30#include <stdio.h>
31
32
33
34void testComplicated(auto_zone_t *the_zone, void *badPointer) {
35/*
36boolean_t auto_zone_atomicCompareAndSwap(auto_zone_t *zone, void *existingValue, void *newValue, void *volatile *location, boolean_t isGlobal, boolean_t issueBarrier);
37    // Atomically update a location with a new GC value.  These use OSAtomicCompareAndSwapPtr{Barrier} with appropriate write-barrier interlocking logic.
38
39boolean_t auto_zone_atomicCompareAndSwapPtr(auto_zone_t *zone, void *existingValue, void *newValue, void *volatile *location, boolean_t issueBarrier);
40    // Atomically update a location with a new GC value.  These use OSAtomicCompareAndSwapPtr{Barrier} with appropriate write-barrier interlocking logic.
41    // This version checks location, and if it points into global storage, registers a root.
42
43extern void *auto_zone_write_barrier_memmove(auto_zone_t *zone, void *dst, const void *src, size_t size);
44*/
45}
46
47void testBadPointer(auto_zone_t *the_zone, void *badPointer) {
48    //printf("auto_zone_retain\n");
49    auto_zone_retain(the_zone, badPointer);
50    //printf("auto_zone_release\n");
51    auto_zone_release(the_zone, badPointer);
52    //printf("auto_zone_retain_count\n");
53    auto_zone_retain_count(the_zone, badPointer);
54    //printf("auto_zone_is_finalized\n");
55    auto_zone_is_finalized(the_zone, badPointer);
56    //printf("auto_zone_set_unscanned\n");
57    auto_zone_set_unscanned(the_zone, badPointer);
58    //printf("auto_zone_create_copy\n");
59    auto_zone_create_copy(the_zone, badPointer);
60    //printf("auto_zone_is_valid_pointer\n");
61    auto_zone_is_valid_pointer(the_zone, badPointer);
62    //printf("auto_zone_size\n");
63    auto_zone_size(the_zone, badPointer);
64    //printf("auto_zone_base_pointer\n");
65    auto_zone_base_pointer(the_zone, badPointer);
66    //printf("auto_zone_set_write_barrier\n");
67    auto_zone_set_write_barrier(the_zone, badPointer, badPointer);
68}
69
70#if defined(__x86_64__)
71#define SUBZONE ((1 << 21)-1)
72#else
73#define SUBZONE ((1 << 20)-1)
74#endif
75
76void construct_and_test() {
77    auto_zone_t *the_zone = auto_zone();
78    // small values
79    long ptr = (long)auto_zone_allocate_object(the_zone, 24, AUTO_MEMORY_UNSCANNED, 0, 0);
80    long badPointer = (ptr & ~(ptr & SUBZONE)) + sizeof(void *);
81    int i;
82    //printf("ptr %lx bad ptr %lx\n", ptr, badPointer);
83    for (i = 0; i < 32; ++i) {
84        testBadPointer(the_zone, (void *)(i*sizeof(void *)+badPointer));
85    }
86
87    // large values
88    long interiors[64];
89    long size = 64*64*1024;  // 64 (32) quantums big
90    ptr = (long)auto_zone_allocate_object(the_zone, 24, AUTO_MEMORY_UNSCANNED, 0, 0);
91    long base = ptr & ~(64*1024-1);
92    long offset = ptr - base;
93    for (int i = 1; i < 64; ++i) {
94        // at every 64K address boundary
95        long bad_address = ptr + i*64*1024;
96        long *fake_Large = (long *)(bad_address - offset);
97        for (int j = 0; j < 10; ++j)
98            fake_Large[j] = -1;
99
100        interiors[i-1] = bad_address;
101    }
102    for (int i = 0; i < 63; ++i)
103        testBadPointer(the_zone, (void *)interiors[i]);
104
105}
106
107int main(int argc, char *argv[]) {
108    construct_and_test();
109    printf("%s: Success\n", argv[0]);
110    return 0;
111}
112
113
114