/*
 * Copyright (C) 2020 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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 APPLE INC. ``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 APPLE INC. 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.
 */

#pragma once

#include <stdint.h>
#include <wtf/Platform.h>

#if OS(DARWIN) && CPU(ARM64)

#include "JSCConfig.h"

#include <wtf/Platform.h>

#if USE(INLINE_JIT_PERMISSIONS_API)
#include <BrowserEngineCore/BEMemory.h>
#elif USE(PTHREAD_JIT_PERMISSIONS_API)
#include <pthread.h>
#elif USE(APPLE_INTERNAL_SDK)
#include <os/thread_self_restrict.h> 
#endif

static ALWAYS_INLINE void threadSelfRestrictRWXToRW()
{
    ASSERT(g_jscConfig.useFastJITPermissions);

#if USE(INLINE_JIT_PERMISSIONS_API)
    be_memory_inline_jit_restrict_rwx_to_rw_with_witness();
#elif USE(PTHREAD_JIT_PERMISSIONS_API)
    pthread_jit_write_protect_np(false);
#elif USE(APPLE_INTERNAL_SDK)
    os_thread_self_restrict_rwx_to_rw();
#else
    bool tautologyToIgnoreWarning = true;
    if (tautologyToIgnoreWarning)
        RELEASE_ASSERT_NOT_REACHED();
#endif
}

static ALWAYS_INLINE void threadSelfRestrictRWXToRX()
{
    ASSERT(g_jscConfig.useFastJITPermissions);

#if USE(INLINE_JIT_PERMISSIONS_API)
    be_memory_inline_jit_restrict_rwx_to_rx_with_witness();
#elif USE(PTHREAD_JIT_PERMISSIONS_API)
    pthread_jit_write_protect_np(true);
#elif USE(APPLE_INTERNAL_SDK)
    os_thread_self_restrict_rwx_to_rx();
#else
    bool tautologyToIgnoreWarning = true;
    if (tautologyToIgnoreWarning)
        RELEASE_ASSERT_NOT_REACHED();
#endif
}

#else // Not OS(DARWIN) && CPU(ARM64)

NO_RETURN_DUE_TO_CRASH ALWAYS_INLINE void threadSelfRestrictRWXToRW()
{
    CRASH();
}

NO_RETURN_DUE_TO_CRASH ALWAYS_INLINE void threadSelfRestrictRWXToRX()
{
    CRASH();
}

#endif // OS(DARWIN) && CPU(ARM64)
