Created
June 23, 2024 23:19
-
-
Save matu3ba/746b623c29f8c505aab726e84970bbd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(__STDC__) | |
#define IS_MAYBE_C89 | |
#if defined(__STDC_VERSION__) | |
#if (__STDC_VERSION__ >= 199409L) | |
#define IS_NOT_C89 | |
#endif | |
#endif | |
#endif | |
#ifndef IS_MAYBE_C89 | |
#error "not C89 compatible" | |
#endif | |
#ifdef IS_NOT_C89 | |
#error "not C89 compatible" | |
#endif | |
/* only for x86_64 */ | |
typedef short unsigned int uint16_t; | |
typedef unsigned int uint32_t; | |
typedef unsigned long uint64_t; | |
typedef unsigned long size_t; | |
typedef unsigned long uintptr_t; | |
#include <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void ptrtointtoptr(void); | |
static void memset_16aligned(void * ptr, char byte, size_t size_bytes, uint16_t alignment) { | |
assert((size_bytes & (alignment-1)) == 0); | |
assert(((uintptr_t)ptr & (alignment-1)) == 0); | |
memset(ptr, byte, size_bytes); | |
} | |
void ptrtointtoptr(void) { | |
const uint16_t alignment = 16; | |
const uint16_t align_min_1 = alignment - 1; | |
void * mem = malloc(1024+align_min_1); | |
void *ptr = (void *)((((uint64_t)mem)+align_min_1) & ~(uint64_t)align_min_1); | |
printf("0x%lu, 0x%lu\n", (unsigned long)mem, (unsigned long)ptr); | |
memset_16aligned(ptr, 0, 1024, alignment); | |
free(mem); | |
} | |
int main(void) { ptrtointtoptr(); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clang -std=c89 -Weverything ptrtoint_inttoptr.c && ./a.out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment