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;
}

结果 : 
[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


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

上面这个例子是把tese1这个函数的代码段内容拷贝到  pfnFunction 数组, 但是显然有问题, 在调用的时候报错 Segmentation fault .
目录
相关文章
|
4月前
|
弹性计算 缓存 Serverless
Serverless 应用引擎操作报错合集之阿里函数计算中我打开sd时遇到错误,信息为"Function instance exited unexpectedly(code 1, message:operation not permitted) with start command ' '."如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
243 6
|
25天前
|
JavaScript
【Azure Function App】Nodejs Function遇见WorkerProcessExitException : node exited with code -1073740791 (0xC0000409) 错误
【Azure Function App】Nodejs Function遇见WorkerProcessExitException : node exited with code -1073740791 (0xC0000409) 错误
|
4月前
|
弹性计算 Dubbo Serverless
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过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
147 5
|
4月前
|
Java 中间件 Serverless
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过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
101 2
|
4月前
疑难杂症!handleSubmit does not execute onSubmit function
疑难杂症!handleSubmit does not execute onSubmit function
33 0
|
9月前
|
编译器 Serverless Go
Fail to start function, Code:1
Fail to start function, Code:1
55 2
|
11月前
|
人工智能 自然语言处理 小程序
cloud function service error code -501000, error message 找不到对应的FunctionName.; at cloud.callFunction
cloud function service error code -501000, error message 找不到对应的FunctionName.; at cloud.callFunction
72 0
|
11月前
|
SQL
SAP Table function 执行报错 code CX_SQL_EXCEPTION feature not supported 该如何分析
SAP Table function 执行报错 code CX_SQL_EXCEPTION feature not supported 该如何分析
|
Java Scala
function implemented in Scala - compiled java code - some closure example
function implemented in Scala - compiled java code - some closure example
function implemented in Scala - compiled java code - some closure example

热门文章

最新文章