execute copyed function code : Segmentation fault

简介:
函数在C里面可以认为是指针, 但是又有特殊的一面.
如 调用 函数test 时, *test, test, &test 可以相互通用. 因为它们都指向同一个地址.
如 : 
[root@db-172-16-3-150 zzz]# cat a.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

typedef unsigned char byte;

int test1() {
  fprintf(stdout, "this is test function.\n");
  return 0;
}
void test2() {}

int main() {
  size_t loadsize;
  loadsize = (size_t) (*test1) - (size_t) (*test2);
  byte a[loadsize];
  fprintf(stdout, "test1:%p, test2:%p\n", test1, test2);
  fprintf(stdout, "test1:%p, *test1:%p, &test1:%p\n", test1, *test1, &test1);
  fprintf(stdout, "loadsize:%lu, a:%lu\n", loadsize, sizeof(a));
  fprintf(stdout, "sizeof(test1):%lu\n", sizeof(test1));
  memset(a, 0, sizeof(a));
  memcpy(a, test1, loadsize);
  int (*pfnFunction)();
  pfnFunction = (int (*)())(a);
  test1();
  // pfnFunction(); // Segmentation fault
  return 0;
}
AI 代码解读

结果 : 
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./a.c -o a && ./a 
test1:0x4005d0, test2:0x4005c0
test1:0x4005d0, *test1:0x4005d0, &test1:0x4005d0  // 这三个值相等.
loadsize:16, a:16
sizeof(test1):1
AI 代码解读


指针的加减运算得到另一个内存地址.  具体结果是加几个字节和指针指向什么类型的数据有关. 如指向int, 那么加1相当于加4字节.
那么能不能用函数指针加1得到下一个地址呢? 从下面的测试来看, 函数指针加1等于加1字节. 可能因为函数代码段是变长的, 编译器没法得到函数代码段的具体大小. sizeof(函数)得到结果1.

上面这个例子是把tese1这个函数的代码段内容拷贝到  pfnFunction 数组, 但是显然有问题, 在调用的时候报错 Segmentation fault .
目录
打赏
0
0
0
1
20691
分享
相关文章
【Azure Function】部署Java Function失败:报错deploy [ERROR] Status code 401和警告 'China North 3' may not be a valid region
1:deploy [ERROR] Status code 401, (empty body). 2: China North 3 may not be a valid region,please refer to https://aka.ms/maven_function_configuration#supported-regions for values. 3:  <azure.functions.maven.plugin.version>1.36.0</azure.functions.maven.plugin.version>
56 11
【Azure Function App】Nodejs Function遇见WorkerProcessExitException : node exited with code -1073740791 (0xC0000409) 错误
【Azure Function App】Nodejs Function遇见WorkerProcessExitException : node exited with code -1073740791 (0xC0000409) 错误
|
11月前
疑难杂症!handleSubmit does not execute onSubmit function
疑难杂症!handleSubmit does not execute onSubmit function
76 0
Serverless 应用引擎操作报错合集之在阿里函数计算中,云函数怎么一直报错Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'php server.php '.如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
229 2
Serverless 应用引擎操作报错合集之阿里函数计算中我打开sd时遇到错误,信息为"Function instance exited unexpectedly(code 1, message:operation not permitted) with start command ' '."如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
358 6
Serverless 应用引擎操作报错合集之阿里函数计算中配置完fc,出现‘Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'npm run start '. 报错如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
293 5
Fail to start function, Code:1
Fail to start function, Code:1
93 2
cloud function service error code -501000, error message 找不到对应的FunctionName.; at cloud.callFunction
cloud function service error code -501000, error message 找不到对应的FunctionName.; at cloud.callFunction
120 0

热门文章

最新文章