简体   繁体   English

通过引用修改zval时PHP扩展段错误

[英]PHP extension seg fault when modifying a zval by reference

PHP: PHP:

$publickey = pack('H*', "03ca473d3c0cccbf600d1c89fa33b7f6b1f2b4c66f1f11986701f4b6cc4f54c360");  
$pubkeylen = strlen($publickey);  
$result = secp256k1_ec_pubkey_decompress($publickey, $pubkeylen);  

C extension: C扩展名:

PHP_FUNCTION(secp256k1_ec_pubkey_decompress) {
    secp256k1_start(SECP256K1_START_SIGN);

    zval *pubkey, *pubkeylen;
    unsigned char* newpubkey;
    int newpubkeylen;
    int result;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &pubkey, &pubkeylen) == FAILURE) {
        return;
    }

    newpubkey = Z_STRVAL_P(pubkey);
    newpubkeylen = Z_LVAL_P(pubkeylen);
    result = secp256k1_ec_pubkey_decompress(newpubkey, &newpubkeylen);

    if (result == 1) {
        newpubkey[newpubkeylen] = 0U;
        ZVAL_STRINGL(pubkey, newpubkey, newpubkeylen, 0);
        ZVAL_LONG(pubkeylen, newpubkeylen);
    }

    RETURN_LONG(result);
}

the $publickey is decompressed from a 32 byte to a 65 byte string, for w/e reason when we're doing this we get a Segmentation Fault. $ publickey从32字节解压缩为65字节字符串,出于某些原因,在执行此操作时会遇到Segmentation Fault。
I asume we're doing something structurally wrong ... considering this is our first PHP extension. 我假设我们在结构上做错了...考虑到这是我们的第一个PHP扩展。

full code; 完整代码; https://github.com/afk11/secp256k1-php https://github.com/afk11/secp256k1-php

After looking at your extension code, you haven't linked the actual secp256k1 lib (.so) while building your extension ( #include "secp256k1.h" does not include actual bitcoin/secp256k1 code c library ). 查看扩展代码后,在构建扩展时尚未链接实际的secp256k1库(.so)( #include“ secp256k1.h”不包括实际的bitcoin / secp256k1代码c库)。

You need to change your config.m4 in any of the following ways 您需要通过以下任意一种方式更改config.m4

1) Add "-l/path/to/bitcoin/secp256k1/lib" to the "gcc" options. 1)在“ gcc”选项中添加“ -l / path / to / bitcoin / secp256k1 / lib”。

Help: here, I am talking about once you "make install" on bitcoin/secp256k1, some libraries will be installed to /usr/lib or /usr/lib64 or /usr/lib/secp256k1 etc.... 帮助:在这里,我谈论的是一旦您在bitcoin / secp256k1上进行“安装”,一些库将被安装到/ usr / lib或/ usr / lib64或/ usr / lib / secp256k1等。

-lsecp256k1

// i.e. something like...
PHP_NEW_EXTENSION(secp256k1, secp256k1.c, $ext_shared,, "-lsecp256k1 -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1")

2) Or, include actual *.c files in from actual secp256k1 library 2)或者,从实际的secp256k1库中包含实际的* .c文件

PHP_NEW_EXTENSION(secp256k1, secp256k1.c ../secp256k1/src/secp256k1.c ../secp256k1/src/others.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

I would recommend option-1 我建议选择1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM