Skip to content

NotLinkingJiraYet fail test if subprocess aborts or segfaults #1112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/alloc_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "config.h"
#include "entity.h"
#include "entity_cache.h"
#include "qd_asan_interface.h"

#include "qpid/dispatch/alloc.h"
#include "qpid/dispatch/ctools.h"
Expand Down Expand Up @@ -346,6 +347,7 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool)
//
qd_alloc_item_t *item = pop_stack(&pool->free_list);
if (item) {
ASAN_UNPOISON_MEMORY_REGION(&item[1], desc->total_size);
#ifdef QD_MEMORY_DEBUG
item->desc = desc;
item->backtrace_size = backtrace(item->backtrace, STACK_DEPTH);
Expand Down Expand Up @@ -403,6 +405,7 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool)
break;
}
item->sequence = 0;
ASAN_POISON_MEMORY_REGION(&item[1], desc->total_size);
#if QD_MEMORY_STATS
desc->stats->held_by_threads++;
desc->stats->total_alloc_from_heap++;
Expand All @@ -413,6 +416,7 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool)

item = pop_stack(&pool->free_list);
if (item) {
ASAN_UNPOISON_MEMORY_REGION(&item[1], desc->total_size);
#ifdef QD_MEMORY_DEBUG
item->desc = desc;
item->backtrace_size = backtrace(item->backtrace, STACK_DEPTH);
Expand Down Expand Up @@ -454,6 +458,7 @@ void qd_dealloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool, char *p)
item->desc = 0;
QD_MEMORY_FILL(p, QD_MEMORY_FREE, desc->total_size);
#endif
ASAN_POISON_MEMORY_REGION(p, desc->total_size);

//
// If this is the thread's first pass through here, allocate the
Expand Down
78 changes: 78 additions & 0 deletions src/qd_asan_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef __qd_asan_interface_h__
#define __qd_asan_interface_h__ 1
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* Defines the ASAN interface file if available. If not, defines stub macros.
*
* See #include <sanitizer/asan_interface.h> in Clang for the source.
*/

#if defined(__clang__)
# define QD_HAS_ADDRESS_SANITIZER __has_feature(address_sanitizer)
#elif defined (__GNUC__) && defined(__SANITIZE_ADDRESS__)
# define QD_HAS_ADDRESS_SANITIZER __SANITIZE_ADDRESS__
#else
# define QD_HAS_ADDRESS_SANITIZER 0
#endif

#if QD_HAS_ADDRESS_SANITIZER

void __asan_poison_memory_region(void const volatile *addr, size_t size);
void __asan_unpoison_memory_region(void const volatile *addr, size_t size);

/// Marks a memory region as unaddressable.
///
/// \note Macro provided for convenience; defined as a no-op if ASan is not
/// enabled.
///
/// \param addr Start of memory region.
/// \param size Size of memory region.
#define ASAN_POISON_MEMORY_REGION(addr, size) \
__asan_poison_memory_region((addr), (size))

/// Marks a memory region as addressable.
///
/// \note Macro provided for convenience; defined as a no-op if ASan is not
/// enabled.
///
/// \param addr Start of memory region.
/// \param size Size of memory region.
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
__asan_unpoison_memory_region((addr), (size))

#else // QD_HAS_ADDRESS_SANITIZER

#define ASAN_POISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))

#endif // QD_HAS_ADDRESS_SANITIZER

// https://github.com/google/sanitizers/wiki/AddressSanitizer#turning-off-instrumentation

#if QD_HAS_ADDRESS_SANITIZER
# define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
#else
# define ATTRIBUTE_NO_SANITIZE_ADDRESS
#endif // QD_HAS_ADDRESS_SANITIZER

#endif // __qd_asan_interface_h__
29 changes: 29 additions & 0 deletions tests/crasher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "Missing argument!\n");
return EXIT_FAILURE;
}

char *arg = argv[1];
if (strcmp(arg, "success") == 0) {
return EXIT_SUCCESS;
} else if (strcmp(arg, "failure") == 0) {
return EXIT_FAILURE;
} else if (strcmp(arg, "segfault") == 0) {
int *a = NULL;
*a = 42;
fprintf(stderr, "Failed to segfault!\n");
return EXIT_FAILURE;
} else if (strcmp(arg, "abort") == 0) {
abort();
fprintf(stderr, "Failed to abort!\n");
return EXIT_FAILURE;
} else {
fprintf(stderr, "Invalid argument!\n");
return EXIT_FAILURE;
}
}
Loading