reverie/tests/disabled/x64-save-return-address.c
Vladimir Makaev 03cbd6044d update hermetic_infra/** files with correct license header - 1/x
Summary:
Followed guide here https://www.internalfb.com/intern/wiki/Linting/License_Lint/ to add fbcode/hermetic_infra/** code to license linter. As we have parts of our code shipped as Open Source it's important to get this automated

This diff is updating existing file's licenses to not get conflict after lint rule enablement

Reviewed By: jasonwhite

Differential Revision: D40674080

fbshipit-source-id: da6ecac036f8964619cf7912058f3a911558e7b1
2022-10-26 12:18:14 -07:00

57 lines
1.3 KiB
C

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
/* must be compiled with -O */
/* demo how we can save return address from previous `callq xxx`
* the byte code could be useful for us to generate temp trampoline
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
__attribute__((noinline)) static void test1(void) {
__asm__(
"callq test2\n\t"
"nop\n\t");
}
__attribute__((noinline, used)) static void test2(void) {
__asm__(
"push %rax\n\t"
"movq 0x8(%rsp), %rax\n\t"
"movq %rax, 0x65001010\n\t"
"addq $0x8, %rsp\n\t"
"nop");
}
__attribute__((noinline)) static void test3(void) {
test1();
}
static void prepare_mmap(void) {
void* addr = mmap(
(void*)0x65000000UL,
0x2000,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0);
if (addr != (void*)0x65000000UL)
abort();
}
int main(int argc, char* argv[]) {
unsigned long* ret = (unsigned long*)0x65001010UL;
prepare_mmap();
test3();
printf("*ret = %lx, expected = %lx\n", *ret, (unsigned long)test1 + 5);
if (*ret != (unsigned long)test1 + 5)
abort();
return 0;
}