blob: e48a56354455a00a0fba1691002f5af0c4364ac6 [file]
/*
* Copyright (C) 2026 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 <malloc.h>
#include <unistd.h>
#include <android-base/test_utils.h>
#if defined(__BIONIC__)
static bool g_hook_called = false;
static void* (*g_orig_hook)(size_t, const void*);
#endif
int main() {
write(STDERR_FILENO, "Y", 1);
#if defined(__BIONIC__)
// Verify the hooks are working.
g_orig_hook = __malloc_hook;
__malloc_hook = [](size_t size, const void* arg) -> void* {
g_hook_called = true;
return g_orig_hook(size, arg);
};
void* ptr;
android::base::DoNotOptimize(ptr = malloc(1024));
if (ptr == nullptr) {
_exit(20);
}
_exit(g_hook_called ? 0 : 10);
#else
_exit(0);
#endif
}