Skip to content

Instantly share code, notes, and snippets.

View hsqStephenZhang's full-sized avatar
🎯
Focusing

z combinator hsqStephenZhang

🎯
Focusing
View GitHub Profile
@hsqStephenZhang
hsqStephenZhang / type_fun.rs
Last active November 16, 2025 21:55
type fun in rust
#![allow(unused)]
use std::marker::PhantomData;
trait Nat {}
struct Zero;
impl Nat for Zero {}
struct Succ<N: Nat>(PhantomData<N>);
@hsqStephenZhang
hsqStephenZhang / analysis.draft.md
Created November 15, 2025 08:43 — forked from MattPD/analysis.draft.md
Program Analysis Resources (WIP draft)
From c7480bb740e0e04cb5d4c84c8b8851bf855b5111 Mon Sep 17 00:00:00 2001
From: hsqStephenZhang <[email protected]>
Date: Wed, 29 Oct 2025 19:51:18 +0100
Subject: [PATCH] fix grammar
---
fuzzer/fuzzer.py | 34 +++++---
fuzzer/grammar.py | 206 +++++++++++++++++++++++++++++-----------------
2 files changed, 156 insertions(+), 84 deletions(-)
@hsqStephenZhang
hsqStephenZhang / ssh-pty.rs
Created July 22, 2025 08:18
mini ssh implementation without auth and encryption, for fun and nonprofit
/*
[dependencies]
nix = { version = "0.30.0", features = ["term", "process", "poll"] }
*/
use nix::libc;
use nix::pty::{ForkptyResult, Winsize, forkpty};
use nix::sys::time::{TimeVal, TimeValLike};
use nix::sys::{select, termios};
use nix::sys::termios::{Termios, SetArg};
@hsqStephenZhang
hsqStephenZhang / ruscript.rs
Last active July 21, 2025 20:45
rust equivalent of script command
/** Cargo.toml
[dependencies]
nix = { version = "0.30.0", features = ["term", "process", "poll"] }
*/
use nix::libc;
use nix::pty::{ForkptyResult, Winsize, forkpty};
use nix::sys::termios::{SetArg, Termios};
use nix::sys::time::{TimeVal, TimeValLike};
use nix::sys::{select, termios};
use nix::unistd::execvp;
@hsqStephenZhang
hsqStephenZhang / rust_vtable_hack.rs
Last active May 13, 2025 15:23
hacks of rust vtable layout(multi traits)
use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct TraitId(&'static str); // e.g., "A", "B", ...
#[derive(Debug)]
struct TraitDef {
id: TraitId,
supertraits: Vec<TraitId>,
methods: Vec<&'static str>,
@hsqStephenZhang
hsqStephenZhang / main.py
Created May 6, 2025 17:58
regex vs cfg parser on catastrophic backtracking
import time
import regex
from lark import Lark, UnexpectedInput
from tabulate import tabulate
def run_test(size):
test_input = 'a' * size + 'b' # Input that will cause regex backtracking
results = []
# -------------------
@hsqStephenZhang
hsqStephenZhang / memory_ordering_experiment.rs
Created April 12, 2025 12:31
intel TSO memory ordering experiment
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
/// notice:
/// 1. we have to introduce some random delay before the actual store-load operation, or it's very unlikely to observe the reordering.
/// 2. if there is a mfence between the store and load instruction, then it's impossible to observe (0, 0) pair, and chance for observing (1, 1) greatly increased.
/// a. if mfence(fence with SeqCst Ordering) is not enabled, then the output might look like: `pair: (1, 0), count: 5647 pair: (0, 1), count: 94351 pair: (0, 0), count: 2`
/// b. if mfence is enabled, then the output looks like:`pair: (0, 1), count: 94440 pair: (1, 0), count: 5538 pair: (1, 1), count: 22`
@hsqStephenZhang
hsqStephenZhang / boringssl_fingerprint_test.rs
Created February 18, 2025 04:20
test of calculate fingerprint in rust with boringssl
use boring::ssl::{SslConnector, SslCurve, SslMethod};
use bytes::{Bytes, BytesMut};
use futures::StreamExt;
use http_body_util::{BodyStream, Empty};
use hyper_boring::{HttpsLayer, TokioHttpConnector};
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::client::legacy::Client;
use hyper_util::rt::{TokioExecutor, TokioIo};
use tower::util::MapResponse;
use tower::Layer;
@hsqStephenZhang
hsqStephenZhang / echo_client.rs
Created February 11, 2025 12:56
simple tcp echo client
use std::os::fd::AsRawFd;
use tokio::io::{self, AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use std::fmt;
pub struct TcpInfo(pub libc::tcp_info);
impl fmt::Debug for TcpInfo {