Skip to content

Instantly share code, notes, and snippets.

View saagarjha's full-sized avatar

Saagar Jha saagarjha

View GitHub Profile
@saagarjha
saagarjha / a14.plist.txt
Created January 16, 2024 04:08
plutil -p /usr/share/kpep/a14.plist
{
"internal" => 0
"name" => "a14"
"system" => {
"cpu" => {
"aliases" => {
"Cycles" => "FIXED_CYCLES"
"Instructions" => "FIXED_INSTRUCTIONS"
}
"architecture" => "arm64"
@saagarjha
saagarjha / file_drain.c
Created November 11, 2023 10:01
"Drain" files while they are processed to reduce free disk space requirements
// Sometimes you have a large file on a small disk and would like to "transform"
// it in some way: for example, by decompressing it. However, you might not have
// enough space on disk to keep both the the compressed file and the
// decompressed results. If the process can be done in a streaming fashion, it
// would be nice if the file could be "drained"; that is, the file would be
// sequentially deleted as it is consumed. At the start you'd have 100% of the
// original file, somewhere in the middle you'd have about half of the original
// file and half of your output, and by the end the original file will be gone
// and you'll be left with just the results. If you do it this way, you might
// be able to do the entire operation without extra space!
@saagarjha
saagarjha / mmap_vs_read.c
Created September 29, 2023 10:42
Test whether mmap or read is faster on your computer
// As seen on:
// https://federated.saagarjha.com/notice/AaEMQpJBSbxhLyxYzg
// https://twitter.com/_saagarjha/status/1707423903969341949
// Compiling: gcc mmap_vs_read.c -O3 -o mmap_vs_read
// Usage: ./mmap_vs_read <bigfile> <mmap|read>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
@saagarjha
saagarjha / MetadataExtractor.js
Created February 17, 2023 09:36
Apple's metadata extraction code for link previews in Messages, taken from macOS Ventura 13.3 Beta (22E5219e)
//
// LinkPresentation
// Copyright © 2015-2020 Apple Inc. All rights reserved.
//
// FIXME: Twitter equivalents?
(function () {
var MetadataExtractor = {
@saagarjha
saagarjha / FB11988552.html
Created February 12, 2023 16:44
Leak memory in Safari from a website
<!DOCTYPE html>
<html>
<head>
<script>
var database;
async function foo() {
let key = await window.crypto.subtle.generateKey(
{
name: "HMAC",
// Usage should be fairly self-explanatory, just paste this in a header and use
// CRASH_WITH_MESSAGE("foobar") in your function.
// Example backtrace:
// Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
// 0 ??? 0x1022d8000 CRASHING IN test.c:20 (foobar) + 0
// 1 a.out 0x1022d7f60 main + 24
// 2 dyld 0x195f07e50 start + 2544
#define STRINGIFY(a) #a
#define CRASH_FUNCTION_NAME(file, line, message) "CRASHING IN " file ":" STRINGIFY(line) " (" message ")"
@saagarjha
saagarjha / fix_FB11645580.mm
Last active January 1, 2024 04:09
Fix an Xcode hang caused by FB11645580 due to IDERunDestination registering thousands of duplicate KVO observers
// https://gist.github.com/saagarjha/ed701e3369639410b5d5303612964557
#import "swizzler.h"
#import <Foundation/Foundation.h>
#import <cstddef>
#import <cstdlib>
#import <dlfcn.h>
#import <mach-o/dyld.h>
#import <mutex>
#import <string>
#import <tuple>
@saagarjha
saagarjha / binja_stripped_selector_stub_fixup.py
Last active October 22, 2023 17:33
Fix up objc_msgSend selector stubs in Binary Ninja
for function in filter(lambda f: f.name == "_objc_msgSend", bv.functions):
selector = function.lowest_address + 4
selector = list(bv.get_code_refs_from(selector))[0]
selector = list(bv.get_data_refs(selector))[0]
selector = list(bv.get_data_refs_from(selector))[0]
selector = bv.get_data_var_at(selector).value
# objc_msgSend itself, probably
if not selector:
continue
selector = selector[:-1].decode()
@saagarjha
saagarjha / path_hook.mm
Created October 17, 2022 18:57
Some code I used to help write FB11698739. Very rough and posted as-is: don't copy things blindly from the internet, but that applies doubly so here!
// clang path_hook.mm -shared -ldl -g -framework Foundation path_hook.o -L/usr/lib/swift
#include <cassert>
#include <cstdint>
#include <dlfcn.h>
#include <mach/arm/vm_param.h>
#include <mach/kern_return.h>
#include <mach/mach_init.h>
#include <mach/vm_map.h>
#include <mach/vm_prot.h>
@saagarjha
saagarjha / watchOSURLSchemes.txt
Created June 19, 2022 00:18
watchOS app URL schemes
// Generated via
// grep -r -l CFBundleURLSchemes /Volumes/JupiterB19R570.N121bOS/ | xargs -n1 sh -c '/usr/libexec/PlistBuddy -c "Print" "$0" > /dev/null 2>&1 && echo && echo "$0" && /usr/libexec/PlistBuddy -c "Print :CFBundleURLTypes" "$0"'
// -[LSApplicationWorkspace openURL:configuration:completionHandler] is a good way to open these
/Volumes/JupiterB19R570.N121bOS/System/Library/CoreServices/Carousel.app/Info.plist
Array {
Dict {
CFBundleURLName = com.apple.carousel
CFBundleURLSchemes = Array {