Skip to content

Instantly share code, notes, and snippets.

@wchargin
Created May 30, 2023 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wchargin/2e3ac6f9cbc775578351ce77b05010bf to your computer and use it in GitHub Desktop.
Save wchargin/2e3ac6f9cbc775578351ce77b05010bf to your computer and use it in GitHub Desktop.
cursed code to get the sequence "!||" to be valid
#!/bin/sh
rustc +nightly --crate-type=staticlib forty_two.rs || exit 1
gcc main.c -L. -lforty_two || exit 1
./a.out
# prints: forty_two() = 42
#![no_std]
#![no_core]
#![feature(no_core, lang_items, auto_traits)]
#[no_mangle]
pub extern "C" fn forty_two() -> u32 {
!|| () // evaluates to 42
}
#[lang = "not"]
pub trait Not {
type Output;
fn not(self) -> Self::Output;
}
impl<T> Not for T {
type Output = u32;
fn not(self) -> u32 {
42
}
}
// mandatory lang items since we don't have ::core
#[lang = "sized"]
pub trait Sized {}
#[lang = "copy"]
pub unsafe trait Copy {}
#[lang = "freeze"]
unsafe auto trait Freeze {}
#[lang = "drop_in_place"]
pub unsafe fn drop_in_place<T: ?Sized>(_: *mut T) {
loop {}
}
#include <stdint.h>
#include <stdio.h>
extern uint32_t forty_two();
int main() {
printf("forty_two() = %d\n", forty_two());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment