Skip to content
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

Fixed Darwin-specific undefined behavior in get_executable_directory() #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ink-splatters
Copy link

What

SIGABRT is received by the process on Darwin, due to

readlink("/proc/self/exe", filepath, 256);

failing to fill in the buffer (procfs does not exist on Darwin).

This causes undefined behavior in the subsequent code: null pointer dereference and attempt to write to invalid memory location (0x0000000000000001)

NOTE: though SIGABRT is received somewhat reproducibly,

man 2 readlink

says nothing about buffer contents in case of an error, which potentially may delay the failure beyond get_executable_directory(), causing unknown side effects.

Fix

using proc_pidpath() was implemented for Unix (primarily Darwin and other BSD systems).

Tests

the project was build using clang 17 from nixpkgs, on macOS running on Apple MacBook Air M1. The problem could not be reproduced anymore.

Click here for the details
$ arch
arm64

$ sw_vers

ProductName:		macOS
ProductVersion:		14.3.1
BuildVersion:		23D60

build.sh:

#!/usr/bin/env bash

set -e
set -o pipefail

CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME:-Darwin}

CMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS:--fuse-ld=lld}
OpenMP_C_FLAGS=${OpenMP_C_FLAGS:--Xclang -fopenmp}
OpenMP_CXX_FLAGS=${OpenMP_CXX_FLAGS:--Xclang -fopenmp}
USE_STATIC_MOLTENVK=${USE_STATIC_MOLTENVK:-ON}
CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES:-arm64}
CMAKE_CROSSCOMPILING=${CMAKE_CROSSCOMPILING:-ON}
CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR:-arm64}

if [ "$1" = "--debug" ]; then
    # this is how I caught it with lldb
    CMAKE_C_FLAGS="-fno-limit-debug-info -g ${CMAKE_C_FLAGS}"
    CMAKE_CXX_FLAGS="-fno-limit-debug-info -g ${CMAKE_CXX_FLAGS}"
else
    CMAKE_C_FLAGS=${CMAKE_C_FLAGS:--O3}
    CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS:--O3}
fi

mkdir -p build
cmake \
    -DCMAKE_SYSTEM_NAME="$CMAKE_SYSTEM_NAME" \
    -DCMAKE_C_FLAGS="$CMAKE_C_FLAGS" \
    -DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" \
    -DCMAKE_EXE_LINKER_FLAGS="$CMAKE_EXE_LINKER_FLAGS" \
    -DOpenMP_C_FLAGS="$OpenMP_C_FLAGS" \
    -DOpenMP_CXX_FLAGS="$OpenMP_CXX_FLAGS" \
    -DUSE_STATIC_MOLTENVK="$USE_STATIC_MOLTENVK" \
    -DCMAKE_OSX_ARCHITECTURES="$CMAKE_OSX_ARCHITECTURES" \
    -DCMAKE_CROSSCOMPILING="$CMAKE_CROSSCOMPILING" \
    -DCMAKE_SYSTEM_PROCESSOR="$CMAKE_SYSTEM_PROCESSOR" \
    -G Ninja \
    -S src \
    -B build

cmake --build build

shell.nix:

with import <nixpkgs> { };
mkShell.override { inherit (llvmPackages_17) stdenv; } {
  buildInputs = with darwin.apple_sdk.frameworks;
    with llvmPackages_17; [
      openmp
      vulkan-headers
      vulkan-loader
      libcxx
      libcxxabi
      Metal
      QuartzCore
      CoreGraphics
      Cocoa
      IOKit
      IOSurface
      Foundation
    ];
  nativeBuildInputs = [ cmake ninja lld_17 glslang llvmPackages_17.bintools ];
}
$ nix-shell shell.nix --run './build.sh'

The resulted build unfortunately could not find valid GPU ( which is not related to this PR).

using proc_pidpath() instead of accessing non-existent /proc/self/exe:

procfs does not exist on Darwin, so strrchr() returned zero, failing
to find '/' in the unfilled buffer.

In the absense of checks, this resulted in null-pointer dereference and
attempt to write to invalid memory location ( 0x0000000000000001 )
@NayamAmarshe
Copy link
Member

NayamAmarshe commented Apr 6, 2024

We actually applied this fix a long time ago but never pushed the changes because my github was down for a whole month. Could you please check the changes and see if they're correct or need any correction?

Thank you very much for the PR and sorry for the delayed response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants