/** * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed 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. */ #include #include #include #include "gwp_asan_test.h" #include "test.h" void invalid_free_right_handler() { find_and_check_file(GWP_ASAN_LOG_DIR, GWP_ASAN_LOG_TAG, "Invalid (Wild) Free"); find_and_check_file(GWP_ASAN_LOG_DIR, GWP_ASAN_LOG_TAG, "1 byte to the right"); clear_log(GWP_ASAN_LOG_DIR, GWP_ASAN_LOG_TAG); cancel_gwp_asan_environment(true); _exit(0); } static void install_sigv_handler() { struct sigaction sigv = { .sa_handler = invalid_free_right_handler, .sa_flags = 0, }; sigaction(SIGSEGV, &sigv, NULL); } void invalid_free_right_test() { config_gwp_asan_environment(true); clear_log(GWP_ASAN_LOG_DIR, GWP_ASAN_LOG_TAG); install_sigv_handler(); char *ptr = (char *)malloc(1); if (!ptr) { t_error("malloc failed."); return; } if (!libc_gwp_asan_ptr_is_mine(ptr)) { t_error("Memory is not allocated by gwp_asan."); return; } free(ptr + 1); } int main() { pid_t pid = fork(); if (pid < 0) { t_error("FAIL fork failed."); } else if (pid == 0) { // child process invalid_free_right_test(); } else { // parent process int status; if (waitpid(pid, &status, 0) != pid) { t_error("gwp_asan_invalid_free_right_test waitpid failed."); } if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { t_error("gwp_asan_invalid_free_right_test failed."); } cancel_gwp_asan_environment(true); } return t_status; }