mouse07410 / asn1c

The ASN.1 Compiler
http://lionet.info/asn1c/
BSD 2-Clause "Simplified" License
94 stars 71 forks source link

PER is not compiled, use -gen-PER #33

Closed ovlnmurty closed 6 years ago

ovlnmurty commented 6 years ago

Hi, I just started working on this asn1c. I selected this version because i want Aligned PER support. My purpose is to compile TS25.453 for UTRAN-PCAP. when i started understanding of the generated code, i realized " PER is not compiled, use -gen-PER" comments in generated code. i tried giving that as argument while compiling my asn files using asn1c... still same behavior. i didnt see any compile options to add this in the asn1master makefile. Please guide me. Thanks

mouse07410 commented 6 years ago

I suggest you try vlm_master branch of my fork - it is the current/latest upstream with APER incorporated. That branch enables PER by default, and will replace the current master of this fork fairly soon.

Having said that - usually asn1c -gen-PER your-file.asn1 with the current master should be enough to generate UPER and APER support.

Please post your asn1c invocation line, results of the asn1c run, and your reasons/evidence that suggests that PER support has not been enabled.

ovlnmurty commented 6 years ago

Hi mouse07410,

Thanks for your reply.

I am using following for compiling my asn files... ./asn1c -gen-PER -fcompound-names PCAP-CommonDataTypes.asn PCAP-Constants.asn PCAP-Containers.asn PCAP-IEs.asn PCAP-PDU-Contents.asn PCAP-PDU-Descriptions.asn

And result of the asn1c run is attached. asn1c-out.txt

After this, when i go into details of code, in many places i found "PER is not compiled, use -gen-PER" as comment to some of the structure elements. Ignoring this, i was trying to decode following buffer

uint8_t buffer[128]={0x00, 0x09, 0x20, 0x07, 0xce, 0x2c, 0x00, 0x00, 0x07, 0x00, 0x1c, 0x00, 0x02, 0x45, 0x16, 0x00, 0x1d, 0x00, 0x02, 0x24, 0x20, 0x00, 0x1e, 0x00, 0x05, 0x00, 0x01, 0x2f, 0x55, 0xfb, 0x00, 0x19, 0x40, 0x01, 0xfe, 0x00, 0x1f, 0x40, 0x01, 0x40, 0x00, 0x20, 0x40, 0x01, 0x40, 0x00, 0x21, 0x40, 0x01, 0x30};

and my code is as follows...

printf("\n PCAP Sample decode Started...\n");

asn_dec_rval_t rval;
asn_codec_ctx_t *opt_codec_ctx = 0;
void *structure = 0;
uint8_t i_bptr[] = {0x00, 0x09, 0x20, 0x07, 0xce, 0x2c, 0x00, 0x00, 0x07, 0x00, 0x1c, 0x00, 0x02, 0x45, 0x16, 0x00, 0x1d, 0x00, 0x02, 0x24, 0x20, 0x00, 0x1e, 0x00, 0x05, 0x00, 0x01, 0x2f, 0x55, 0xfb, 0x00, 0x19, 0x40, 0x01, 0xfe, 0x00, 0x1f, 0x40, 0x01, 0x40, 0x00, 0x20, 0x40, 0x01, 0x40, 0x00, 0x21, 0x40, 0x01, 0x30};
size_t   i_size = 50;
InitiatingMessage_t *im = 0;
uint8_t buffer[1280];
size_t buf_size = sizeof(buffer);

im = calloc(1, sizeof(InitiatingMessage_t));
memset(im,0,sizeof(InitiatingMessage_t));

if(!im) {
perror("calloc() failed");
exit(1);
}

TransactionID_t tid;
tid.present = TransactionID_PR_longTID;
tid.choice.longTID = 1998;

im->criticality = Criticality_reject;
im->procedureCode = 9;

im->transactionID = tid;

im->value.buf = 0;
im->value.size = 0;
asn_enc_rval_t er;
printf("\n PCAP Sample Ready to decode...\n");
er = aper_encode_to_buffer(&asn_DEF_InitiatingMessage, &im, buffer, buf_size);
printf("\n PCAP Sample encoded with Result code : %d...\n", er.encoded);
fflush(stdout);

And I am always getting er.encoded as -1. I will use vlm_mster as you suggested. meanwhile, Please suggest if i am going in right direction or not.

Thanks.

mouse07410 commented 6 years ago

As @brchiu suggested here, please try using the flags he recommended, and report the results here.

Also, I expect that since you're building asn1c from the source, it passed all the tests. BTW, out of curiosity, what platform are you running it on?

Also, this output:

. . . . .
Copied ./../skeletons/converter-sample.c        -> converter-sample.c
Generated Makefile.am.sample

indicates that you are not working with the vlm_master branch - that's the messages the old master provides. Although, with -gen-PER even that should work correctly...

in many places i found "PER is not compiled, use -gen-PER" as comment to some of the structure elements.

Please provide specific details - exactly where you saw those comments. In my simple small examples, this is the only kind of "PER is not compiled" message that I see (and I found it only in per_encoder.c and per_decoder.c):

       /*
         * Invoke type-specific decoder.
         */
        if(!td->op->uper_decoder)
                ASN__DECODE_FAILED;     /* PER is not compiled in */

Ignoring this, i was trying to decode following buffer

Offhand, your code does not appear good. First, take a look at asn_application.h file, and review the interfaces. Then (even though you may have done it already), review the examples in the doc file, and make sure you're filling the components of the InitiatingMessage_t using the appropriate interfaces. What you are doing is fine for integers, but most likely won't do for buffers (aka OCTET STRING elements and such). Then, you say you'r trying to decode, but your code is doing encoding...

And *_encode_() does not take a pointer-to-pointer, so it should be , im, buffer, buf_size); not , &im,.

Also, the new way to encode that vlm_master encourages is something like this:

  unsigned char *aper_buf = NULL;
  size_t 
  asn_encode_to_new_buffer_result_t new_er =
    asn_encode_to_new_buffer(NULL, ATS_ALIGNED_BASIC_PER, &asn_DEF_InitiatingMessage, im);
  if (new_er.bufer == NULL || new_er.result.encoded < 0) {
    fprintf(stderr,
            "Failed to APER-encode InitiatingMessage PDU (rc=%ld)\n", new_er.result.encoded);
  } else {
    printf("Basic APER-encoding of InitiatingMessage PDU took %ld bytes\n",
           new_er.result.encoded);
    aper_buf = new_er.buffer;
  }

If those flags won't help, I may need to see the ASN.1 files themselves.

Also, before trying to do complex things with complex structures, why don't you first practice on something simpler - like

Test2b DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

Test2b ::= SEQUENCE {

      count INTEGER (40..20000),
      data  OCTET STRING (SIZE (40..20000))
}

END

and see if you can encode and decode it into APER. With such a small toy example, it would be easy to use the converter-example to test the results of your encoding. Here's a sample in XER that converter-example can convert for you to APER and such:

<Test2b>
    <count>40</count>
    <data>
        F2 68 AC 80 C2 25 49 46 9A 02 93 53 CB A3 EE E2 
        5E F2 45 84 BA 97 55 B6 FE 37 DF DF E2 AF D0 94 
        CD 91 AB 73 66 BE C1 CB
    </data>
</Test2b>
mouse07410 commented 6 years ago

First, I attest that @brchiu's flags work. What really makes the difference is -no-include-deps, though other ones are also needed.

You can see that these ASN.1 files are not consistent (at least PCAP-IEs.asn isn't), and use different cases for the DGPSCorrections. That is bad.

$ fgr DGPSCorrection *.asn
PCAP-IEs.asn:640:-- DGPSCorrections
PCAP-IEs.asn:645:DGPSCorrections ::=                    SEQUENCE {
PCAP-IEs.asn:650:   iE-Extensions                       ProtocolExtensionContainer { { DGPSCorrections-ExtIEs } }       OPTIONAL,
PCAP-IEs.asn:654:DGPSCorrections-ExtIEs PCAP-PROTOCOL-EXTENSION ::= {
PCAP-IEs.asn:3389:  dgpsCorrections                     DGPSCorrections                                             OPTIONAL,
$ fgr DgpsCorrection *.asn
PCAP-IEs.asn:2692:  dgpsCorrections                     DgpsCorrections,
PCAP-IEs.asn:2970:DgpsCorrections ::= NULL

Finally, the buffer you gave, fails to decode - and I've no way of knowing whether the buffer is bad, or the code itself fails... Where did you get that buffer, and how do you know it has a valid contents?

brchiu commented 6 years ago

@ovlnmurty, the pcap-dump I built can decode your sample message as following :

sample.source.PCAP$ ./pcap-dump -iaper -per-nopad pcap.sample.aper
<PCAP-PDU>
    <initiatingMessage>
        <procedureCode>9</procedureCode>
        <criticality><reject/></criticality>
        <transactionID>
            <longTID>1998</longTID>
        </transactionID>
        <value>
            <PositionInitiationRequest>
                <protocolIEs>
                    <PositionInitiationRequestIEs>
                        <id>28</id>
                        <criticality><reject/></criticality>
                        <value>
                            <RequestType>
                                <event><direct/></event>
                                <reportArea><geographical-area/></reportArea>
                                <horizontalaccuracyCode>11</horizontalaccuracyCode>
                            </RequestType>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>29</id>
                        <criticality><reject/></criticality>
                        <value>
                            <UE-PositioningCapability>
                                <standAloneLocationMethodsSupported><true/></standAloneLocationMethodsSupported>
                                <ueBasedOTDOASupported><false/></ueBasedOTDOASupported>
                                <networkAssistedGPSSupport><both/></networkAssistedGPSSupport>
                                <supportGPSTimingOfCellFrame><false/></supportGPSTimingOfCellFrame>
                                <supportForIPDL><false/></supportForIPDL>
                                <supportForRxTxTimeDiff><false/></supportForRxTxTimeDiff>
                                <supportForUEAGPSinCellPCH><true/></supportForUEAGPSinCellPCH>
                                <supportForSFNSFNTimeDiff><false/></supportForSFNSFNTimeDiff>
                            </UE-PositioningCapability>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>30</id>
                        <criticality><reject/></criticality>
                        <value>
                            <UC-ID>
                                <rNC-ID>303</rNC-ID>
                                <c-ID>22011</c-ID>
                            </UC-ID>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>25</id>
                        <criticality><ignore/></criticality>
                        <value>
                            <VerticalAccuracyCode>127</VerticalAccuracyCode>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>31</id>
                        <criticality><ignore/></criticality>
                        <value>
                            <ResponseTime><delay-tolerant/></ResponseTime>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>32</id>
                        <criticality><ignore/></criticality>
                        <value>
                            <PositioningPriority><normal-priority/></PositioningPriority>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>33</id>
                        <criticality><ignore/></criticality>
                        <value>
                            <ClientType><lawful-intercept-services/></ClientType>
                        </value>
                    </PositionInitiationRequestIEs>
                </protocolIEs>
            </PositionInitiationRequest>
        </value>
    </initiatingMessage>
</PCAP-PDU>
mouse07410 commented 6 years ago

@brchiu thank you! So that sample message shouldn't be decoded as InitializingMessage but as PCAP-PDU that contains InitializingMessage?

mouse07410 commented 6 years ago

@brchiu one more question. When I tried to work with this sample message, converter-example required that I explicitly tell it what PDU to use (demanded a meaningful "-p" flag). How did you manage to avoid having to specify the PDU in your pcap-dump application?

brchiu commented 6 years ago

@mouse07410 , as shown in attached log, I did not specify the -p command line option to pcap-dump.

ovlnmurty commented 6 years ago

thanks to all. i used brchiu suggested flags and now everything working fine.

mouse07410 commented 6 years ago

as shown in attached log, I did not specify the -p command line option to pcap-dump.

@brchiu that's exactly my question - how did you manage to make pap-dump work correctly without specifying the PDU? Here's what I get:

$ ./pcap-dump -iaper -per-nopad -oxer pcap.sample.aper 
Use '-p <Type>' or '-p list' to select ASN.1 type.
$ ./pcap-dump -p PCAP-PDU -iaper -per-nopad -oxer pcap.sample.aper 
<PCAP-PDU>
    <initiatingMessage>
        <procedureCode>9</procedureCode>
        <criticality><reject/></criticality>
        <transactionID>
            <longTID>1998</longTID>
        </transactionID>
        <value>
            <PositionInitiationRequest>
                <protocolIEs>
                    <PositionInitiationRequestIEs>
                        <id>28</id>
                        <criticality><reject/></criticality>
                        <value>
                            <RequestType>
                                <event><direct/></event>
                                <reportArea><geographical-area/></reportArea>
                                <horizontalaccuracyCode>11</horizontalaccuracyCode>
                            </RequestType>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>29</id>
                        <criticality><reject/></criticality>
                        <value>
                            <UE-PositioningCapability>
                                <standAloneLocationMethodsSupported><true/></standAloneLocationMethodsSupported>
                                <ueBasedOTDOASupported><false/></ueBasedOTDOASupported>
                                <networkAssistedGPSSupport><both/></networkAssistedGPSSupport>
                                <supportGPSTimingOfCellFrame><false/></supportGPSTimingOfCellFrame>
                                <supportForIPDL><false/></supportForIPDL>
                                <supportForRxTxTimeDiff><false/></supportForRxTxTimeDiff>
                                <supportForUEAGPSinCellPCH><true/></supportForUEAGPSinCellPCH>
                                <supportForSFNSFNTimeDiff><false/></supportForSFNSFNTimeDiff>
                            </UE-PositioningCapability>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>30</id>
                        <criticality><reject/></criticality>
                        <value>
                            <UC-ID>
                                <rNC-ID>303</rNC-ID>
                                <c-ID>22011</c-ID>
                            </UC-ID>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>25</id>
                        <criticality><ignore/></criticality>
                        <value>
                            <VerticalAccuracyCode>127</VerticalAccuracyCode>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>31</id>
                        <criticality><ignore/></criticality>
                        <value>
                            <ResponseTime><delay-tolerant/></ResponseTime>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>32</id>
                        <criticality><ignore/></criticality>
                        <value>
                            <PositioningPriority><normal-priority/></PositioningPriority>
                        </value>
                    </PositionInitiationRequestIEs>
                    <PositionInitiationRequestIEs>
                        <id>33</id>
                        <criticality><ignore/></criticality>
                        <value>
                            <ClientType><lawful-intercept-services/></ClientType>
                        </value>
                    </PositionInitiationRequestIEs>
                </protocolIEs>
            </PositionInitiationRequest>
        </value>
    </initiatingMessage>
</PCAP-PDU>
$
velichkov commented 6 years ago

how did you manage to make pap-dump work correctly without specifying the PDU

Recompile with both -pdu=PCAP-PDU -pdu=all

ovlnmurty commented 6 years ago

Thanks mouse07410. i wrongly posted encode lines in my query instead of decode. anyway, now i am able to decode, encode and playing with pcap for many operations. Thanks for the aper support. thanks to all the developers for maintaining this git.

A small suggestion as a user. the documentation is not up to date. in many times, i gone through the source/header files to know the arguments of those functions and possible values. Request you to spare some time on user manual also. As part of give and take, please let me know if you want me to make a user manual from my side. I am happy to share. :)

mouse07410 commented 6 years ago

@velichkov thank you! That magic did the trick.

@ovlnmurty yes please! If you can contribute documentation improvements - they'd be welcome.

But working through your sample (PCAP-PDU), I'm having problems encoding it. Decoding started to work fine as soon as I replaced InitiatingMessage_t with PCAP_PDU_t. But encoding consistently fails for me - I wonder if you could point out what's missing in my clone of your code.

I can re-encode the correctly-decoded PCAP-PDU, but not create an "encodable" PCAP-PDU from scratch (using your code):

. . . . . [decoding part that works fine] . . . . .
  PCAP_PDU_t *tm_o =  calloc(1, sizeof(PCAP_PDU_t));
  if (!tm_o)
    perror("calloc() for PCAP_PDU_t");

  im = calloc(1, sizeof(InitiatingMessage_t));
  if(!im) {
    perror("calloc() for InitiatingMessage_t failed");
    exit(1);
  }

  TransactionID_t tid;
  tid.present = TransactionID_PR_longTID;
  tid.choice.longTID = 1998;

  im->criticality = Criticality_reject;
  im->procedureCode = 9;

  im->transactionID = tid;

  // Now - somehow put InitiatingMessage into the main PCAP-PDU
  tm_o->present = PCAP_PDU_PR_initiatingMessage;
  tm_o->choice = (union PCAP_PDU_u) im;

  //im->value.buf = 0; // does not exist in this structure
  //im->value.size = 0; // does not exist in this structure
  asn_enc_rval_t er;
  printf("\n PCAP Sample Ready to encode...\n");
  er = asn_encode_to_buffer(NULL, ATS_ALIGNED_BASIC_PER,
                &asn_DEF_PCAP_PDU,
                (const void *)tm_o, 
                (void *)buffer, buf_size);
  if (er.encoded >= 0) {
    printf("\n PCAP Sample encoded successfully (%ld bytes)\n", er.encoded);
    fflush(stdout);
  } else {
    fprintf(stderr, "\n Failed to encode PCAP Sample: returned %ld\n", er.encoded);
  }
. . . . .

and the output:

. . . . .
            </PositionInitiationRequest>
        </value>
    </initiatingMessage>
</PCAP-PDU>

 PCAP Sample Ready to encode...

 Failed to encode PCAP Sample: returned -1
ovlnmurty commented 6 years ago

Oops... i too didnt check from scratch... i used following code...

... Some includes

extern asn_TYPE_descriptor_t asn_DEF_InitiatingMessage;

int main() {

printf("\n PCAP Sample decode Started...\n");

asn_dec_rval_t rval;
asn_codec_ctx_t *opt_codec_ctx = 0;
void *structure = 0;
uint8_t i_bptr[] = {0x00, 0x09, 0x20, 0x07, 0xce, 0x2c, 0x00, 0x00, 0x07, 0x00, 0x1c, 0x00, 0x02, 0x45, 0x16, 0x00, 0x1d, 0x00, 0x02, 0x24, 0x20, 0x00, 0x1e, 0x00, 0x05, 0x00, 0x01, 0x2e, 0x55, 0xfb, 0x00, 0x19, 0x40, 0x01, 0xfe, 0x00, 0x1f, 0x40, 0x01, 0x40, 0x00, 0x20, 0x40, 0x01, 0x40, 0x00, 0x21, 0x40, 0x01, 0x30};
size_t   i_size = 50;

uint8_t buffer[1024]={0};
size_t buf_size = 1024;

//////////////////////////////////////

PCAP_PDU_t pcap;

InitiatingMessage_t *im=NULL;
memset(&pcap, 0, sizeof (PCAP_PDU_t));

//////////////////////////////////////
asn_per_constraints_t c;

printf("\n PCAP Sample Ready to decode...\n");
rval = aper_decode_complete(opt_codec_ctx, &asn_DEF_PCAP_PDU,&im, i_bptr, i_size);

//er = aper_encode_to_buffer(&asn_DEF_PCAP_PDU,0, &im, buffer, buf_size);

printf("\n Parsed Tree...\n");
asn_fprint(stdout, &asn_DEF_PCAP_PDU, im);

printf("\n Encoding Starts...\n");

asn_enc_rval_t er;
  printf("\n PCAP Sample Ready to encode...\n");
  er = asn_encode_to_buffer(NULL, ATS_ALIGNED_BASIC_PER,
                &asn_DEF_PCAP_PDU,
                (const void *)im,
                (void *)buffer, buf_size);
  if (er.encoded >= 0) {
    printf("\n PCAP Sample encoded successfully (%ld bytes)\n", er.encoded);
    fflush(stdout);
  } else {
    fprintf(stderr, "\n Failed to encode PCAP Sample: returned %ld\n", er.encoded);
  }

  printf("\n PCAP Sample encoded with Result code : %d...\n", rval.consumed);
    int i;
    for(i=0;i<i_size;i++)
    {
        printf("%02X, ", i_bptr[i]);
    }

fflush(stdout);
return 0;

}

this i separated from my app and checked and confirmed you that encoding works... let me try a fresh encode from new object.... seems there is a problem with protocolIEs. i need to do reverse engineering how asn_fprint is traveling through the structure for protocolIEs and accordingly need to make the structure ready for encode... and post you the results...

ovlnmurty commented 6 years ago

I am trying to add components with following snippet...

// Component-1. ID=28 PositionInitiationRequestIEs_t ie1; RequestType_t rt;

  ie1.criticality = Criticality_reject;
  ie1.id = 28;
  ie1.value.present = PositionInitiationRequestIEs__value_PR_RequestType;
  rt.event = 1;
  rt.reportArea = 1;
  RequestTypeAccuracyCode_t rac = 11;
  rt.horizontalaccuracyCode =&rac;
  ie1.value.choice.RequestType = rt;

  ASN_SEQUENCE_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie1);

Is this the right way to add elements in the ASN Sequence ?

mouse07410 commented 6 years ago

I think this is the right way to add elements to the ASN SEQUENCE OF, but not to ASN SEQUENCE.

I don't have much experience in encoding such constructed types - perhaps @velichkov has examples that he could share?

ovlnmurty commented 6 years ago

Thanks for your reply. @velichkov , can you please share any sample you have. Thanks

ovlnmurty commented 6 years ago

Hi @mouse07410 ,

i used gdb to go step by step... in asn_application.c, it is not printing OO-2... Meaning to say, td->op->aper_encoder is resulting to false. can you suggest something... Thanks.

case ATS_ALIGNED_BASIC_PER: / CANONICAL-APER is a superset of BASIC-APER. / / Fall through. / case ATS_ALIGNED_CANONICAL_PER: printf("\nOO-1"); if(td->op->aper_encoder) { printf("\nOO-2"); er = aper_encode(td, 0, sptr, callback, callback_key); if(er.encoded == -1) { if(er.failed_type && er.failed_type->op->aper_encoder) {

mouse07410 commented 6 years ago

can you suggest something...

It doesn't look like we're filling the structures properly, therefore the encoder complains and refuses to proceed. gdb is of limited use at this point, IMHO. It would probably be easier if we start with a PDU less challenging/complex that the full PCAP-PDU.

I think we need to enhance the documentation, providing encoding example for every supported ASN.1 type, and maybe a couple of combined ones (when one complex type is a member of another one). @velichkov you probably agree?

ovlnmurty commented 6 years ago

Thanks mouse, i am going deep into code and could not find implementation of aper_encoder function. is it a function pointer maintained in the structure of asn_TYPE_operation_t ? where is the implementation of this ? pl guide me

mouse07410 commented 6 years ago

i am going deep into code and could not find implementation of aper_encoder function

Sorry I don't know, and I don't think this is the right direction. You don't want to learn how to drive a car by disassembling and reassembling its engine and transmission.

ovlnmurty commented 6 years ago

:) Okay... Let me learn Formula-1 race and comeback... thanks for the support till now

ovlnmurty commented 6 years ago

Hi @mouse07410 ,

i could able to encode it.... Here is the code ...

ifdef HAVE_CONFIG_H

include

endif

include

include <sys/types.h>

include / for atoi(3) /

include / for getopt(3) /

include / for strerror(3) /

include / for EX_ exit codes */

include / for errno /

include

include

include

include

include

include "ProtocolIE-Container.h"

include "ProtocolIE-Field.h"

include / for ASN__DEFAULT_STACK_MAX /

include

extern asn_TYPE_descriptor_t asn_DEF_InitiatingMessage;

int main() {

printf("\n PCAP Sample encode Started...\n");

uint8_t buffer[1024]={0};
size_t buf_size = 1024;

//////////////////////////////////////

PCAP_PDU_t *pcap;
pcap = calloc(1, sizeof(PCAP_PDU_t));
if(!pcap)
    perror("calloc() for PCAP_PDU_t");

InitiatingMessage_t *im;
im = calloc(1, sizeof(InitiatingMessage_t));
if(!im)
    perror("calloc() for InitiatingMessage_t");

printf("\n Encoding Starts...\n");

im->criticality = Criticality_reject;
im->procedureCode = 9;
im->transactionID.present=TransactionID_PR_longTID;
im->transactionID.choice.longTID = 1234;
im->value.present = InitiatingMessage__value_PR_PositionInitiationRequest;

// Component-1. ID=28
  PositionInitiationRequestIEs_t ie1;
  RequestType_t rt;

  ie1.criticality = Criticality_reject;
  ie1.id = 28;
  ie1.value.present = PositionInitiationRequestIEs__value_PR_RequestType;
  rt.event = 1;
  rt.reportArea = 1;
  RequestTypeAccuracyCode_t rac = 11;
  rt.horizontalaccuracyCode =&rac;
  ie1.value.choice.RequestType = rt;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie1);

  // Component-2. ID=29
  PositionInitiationRequestIEs_t ie2;
  UE_PositioningCapability_t uepc;

  ie2.criticality = Criticality_reject;
  ie2.id = 29;
  ie2.value.present = PositionInitiationRequestIEs__value_PR_UE_PositioningCapability;
  uepc.networkAssistedGPSSupport=2;
  uepc.standAloneLocationMethodsSupported=1;
  uepc.supportForIPDL=0;
  uepc.supportForRxTxTimeDiff=0;
  uepc.supportForSFNSFNTimeDiff = 0;
  uepc.supportForUEAGPSinCellPCH=1;
  uepc.supportGPSTimingOfCellFrame=0;
  uepc.ueBasedOTDOASupported=0;
  uepc.iE_Extensions=NULL;
  ie2.value.choice.UE_PositioningCapability = uepc;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie2);

    // Component-3. ID=30
          PositionInitiationRequestIEs_t ie3;

          ie3.criticality = Criticality_reject;
          ie3.id = 30;
          ie3.value.present = PositionInitiationRequestIEs__value_PR_UC_ID;
          ie3.value.choice.UC_ID.c_ID = 22011;
          ie3.value.choice.UC_ID.rNC_ID = 303;
          ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie3);

          // Component-4. ID=25
              PositionInitiationRequestIEs_t ie4;

              ie4.criticality = Criticality_ignore;
              ie4.id = 25;
              ie4.value.present = PositionInitiationRequestIEs__value_PR_VerticalAccuracyCode;
              ie4.value.choice.VerticalAccuracyCode = 127;
              ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie4);
      // Component-5. ID=31
              PositionInitiationRequestIEs_t ie5;

              ie5.criticality = Criticality_ignore;
              ie5.id = 31;
              ie5.value.present = PositionInitiationRequestIEs__value_PR_ResponseTime;
              ie5.value.choice.ResponseTime=1;
              ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie5);
      // Component-6. ID=32
              PositionInitiationRequestIEs_t ie6;

              ie6.criticality = Criticality_ignore;
              ie6.id = 32;
              ie6.value.present = PositionInitiationRequestIEs__value_PR_PositioningPriority;
              ie6.value.choice.PositioningPriority =1;
              ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie6);
      // Component-7. ID=33
              PositionInitiationRequestIEs_t ie7;

              ie7.criticality = Criticality_ignore;
              ie7.id = 33;
              ie7.value.present = PositionInitiationRequestIEs__value_PR_ClientType;
              ie7.value.choice.ClientType =3;
              ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie7);

  pcap->present = PCAP_PDU_PR_initiatingMessage;
  pcap->choice.initiatingMessage = im;

asn_enc_rval_t er;
  printf("\n PCAP Sample Before encode...\n");

  asn_fprint(stdout, &asn_DEF_PCAP_PDU, pcap);

      printf("\n PCAP Sample Ready to encode...\n");
  er = asn_encode_to_buffer(NULL, ATS_ALIGNED_BASIC_PER,
                &asn_DEF_PCAP_PDU,
                (const void *)pcap,
                (void *)buffer, buf_size);

  if (er.encoded >= 0) {
    printf("\n PCAP Sample encoded successfully (%ld bytes)\n", er.encoded);
    int i;
    for(i=0;i<er.encoded;i++)
    {
        printf("%02X, ", buffer[i]);
    }
    fflush(stdout);
  } else {
    fprintf(stderr, "\n Failed to encode PCAP Sample: returned %ld\n", er.encoded);
  }

fflush(stdout);
return 0;

}

Strange thing is i called asn_fprint before encoding to check all the parameters are in place or not... with that it is able to encode... if i remove that (though its purpose is to print the structure in readable form) it is failing with -1. I need to go in detail why this behavior... I will update my findings...

mouse07410 commented 6 years ago

Strange thing is i called asn_fprint before encoding to check all the parameters are in place or not... with that it is able to encode... if i remove that (though its purpose is to print the structure in readable form) it is failing with -1

Interesting - I do not observe that problem. Here's my code, based on your code ;)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <memory.h>
#include <errno.h>
#include <sys/types.h>

#include "PCAP-PDU.h"
#include "InitiatingMessage.h"
#include "TransactionID.h"
#include "ProtocolIE-Container.h"
#include "ProtocolIE-Field.h"

int main(int argc, char **argv)
{
  printf("\n PCAP Sample decode Started...\n");

  asn_dec_rval_t rval;

  uint8_t i_bptr[] = {0x00, 0x09, 0x20, 0x07, 0xce, 0x2c, 0x00, 0x00,
              0x07, 0x00, 0x1c, 0x00, 0x02, 0x45, 0x16, 0x00,
              0x1d, 0x00, 0x02, 0x24, 0x20, 0x00, 0x1e, 0x00,
              0x05, 0x00, 0x01, 0x2f, 0x55, 0xfb, 0x00, 0x19,
              0x40, 0x01, 0xfe, 0x00, 0x1f, 0x40, 0x01, 0x40,
              0x00, 0x20, 0x40, 0x01, 0x40, 0x00, 0x21, 0x40,
              0x01, 0x30};
  size_t   i_size = sizeof(i_bptr);

  uint8_t buffer[1280];
  size_t buf_size = sizeof(buffer);

  PCAP_PDU_t *tm_i = NULL;
  InitiatingMessage_t *im = NULL;

  // Test and verify PCAP-PDU decoding
  rval = asn_decode(NULL, ATS_ALIGNED_BASIC_PER,
            &asn_DEF_PCAP_PDU,
            (void **) &tm_i, i_bptr, i_size);
  if (rval.code != RC_OK) {
    fprintf(stderr, " Failed to decode PCAP_PDU: %s (consumed %lu)\n",
        (rval.code == RC_FAIL? "RC_FAIL":"RC_WMORE"), rval.consumed);
    //return -1;
  } else {
    printf(" Successfully decoded PCAP-PDU (consumed %lu bytes)\n",
       rval.consumed);
    xer_fprint(stdout, &asn_DEF_PCAP_PDU, tm_i);
  }
  printf("\n");

  // Try encoding PCAP-PDU from scratch
  printf("\nTrying to encode PCAP-PDU from scratch...\n");
  PCAP_PDU_t *tm_o =  calloc(1, sizeof(PCAP_PDU_t));
  if (!tm_o) {
    perror("calloc() for PCAP_PDU_t");
    exit(-1);
  }
  im = calloc(1, sizeof(InitiatingMessage_t));
  if(!im) {
    perror("calloc() for InitiatingMessage_t failed");
    exit(-2);
  }

  // InitiatingMessage details
  im->criticality = Criticality_reject;
  im->procedureCode = 9;
  im->transactionID.present = TransactionID_PR_longTID;
  im->transactionID.choice.longTID = 1998;
  im->value.present = InitiatingMessage__value_PR_PositionInitiationRequest;

  // Component-1. ID=28
  PositionInitiationRequestIEs_t ie1;
  RequestType_t rt;

  ie1.criticality = Criticality_reject;
  ie1.id = 28;
  ie1.value.present = PositionInitiationRequestIEs__value_PR_RequestType;
  rt.event = 1;
  rt.reportArea = 1;
  RequestTypeAccuracyCode_t rac = 11;
  rt.horizontalaccuracyCode = &rac;
  ie1.value.choice.RequestType = rt;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie1);

  // Component-2. ID=29
  PositionInitiationRequestIEs_t ie2;
  UE_PositioningCapability_t uepc;

  ie2.criticality = Criticality_reject;
  ie2.id = 29;
  ie2.value.present = PositionInitiationRequestIEs__value_PR_UE_PositioningCapability;
  uepc.networkAssistedGPSSupport=2;
  uepc.standAloneLocationMethodsSupported=1;
  uepc.supportForIPDL=0;
  uepc.supportForRxTxTimeDiff=0;
  uepc.supportForSFNSFNTimeDiff = 0;
  uepc.supportForUEAGPSinCellPCH=1;
  uepc.supportGPSTimingOfCellFrame=0;
  uepc.ueBasedOTDOASupported=0;
  uepc.iE_Extensions=NULL;
  ie2.value.choice.UE_PositioningCapability = uepc;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie2);

  printf("setting up PositionInitiationRequest ie3...\n");
  // Component-3. ID=30
  PositionInitiationRequestIEs_t ie3;
  ie3.criticality = Criticality_reject;
  ie3.id = 30;
  ie3.value.present = PositionInitiationRequestIEs__value_PR_UC_ID;
  ie3.value.choice.UC_ID.c_ID = 22011;
  ie3.value.choice.UC_ID.rNC_ID = 303;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie3);

  // Component-4. ID=25
  PositionInitiationRequestIEs_t ie4;

  ie4.criticality = Criticality_ignore;
  ie4.id = 25;
  ie4.value.present = PositionInitiationRequestIEs__value_PR_VerticalAccuracyCode;
  ie4.value.choice.VerticalAccuracyCode = 127;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie4);

  printf("setting up PositionInitiationRequest ie5...\n");
  // Component-5. ID=31
  PositionInitiationRequestIEs_t ie5;
  ie5.criticality = Criticality_ignore;
  ie5.id = 31;
  ie5.value.present = PositionInitiationRequestIEs__value_PR_ResponseTime;
  ie5.value.choice.ResponseTime=1;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie5);

  // Component-6. ID=32
  PositionInitiationRequestIEs_t ie6;

  ie6.criticality = Criticality_ignore;
  ie6.id = 32;
  ie6.value.present = PositionInitiationRequestIEs__value_PR_PositioningPriority;
  ie6.value.choice.PositioningPriority =1;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie6);

  // Component-7. ID=33
  PositionInitiationRequestIEs_t ie7;

  ie7.criticality = Criticality_ignore;
  ie7.id = 33;
  ie7.value.present = PositionInitiationRequestIEs__value_PR_ClientType;
  ie7.value.choice.ClientType =3;
  ASN_SET_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie7);

  if (argc > 1) {
    printf("\nVisual check of InitiatingMessage structure:\n");
    xer_fprint(stdout, &asn_DEF_InitiatingMessage, im);
    printf("\n");
  }

  asn_enc_rval_t er;
  er = asn_encode_to_buffer(NULL, ATS_ALIGNED_BASIC_PER,
                &asn_DEF_InitiatingMessage,
                (const void *)im,
                (void *)buffer, buf_size);
  if (er.encoded >= 0) {
    printf("\n InitiatingMessage encoded successfully (%ld bytes)\n",
       er.encoded);
    fflush(stdout);
  } else {
    fprintf(stderr, "\n Failed to encode InitiatingMessage: returned %ld\n",
        er.encoded);
  }

  // Now - somehow put InitiatingMessage into the main PCAP-PDU
  int rc = CHOICE_variant_set_presence(&asn_DEF_PCAP_PDU,
                   (void *) tm_o,
                   PCAP_PDU_PR_initiatingMessage);
  if (rc < 0) {
    fprintf(stderr, "\n Failed to set CHOICE presence...\n");
  } else {
    printf("\n Set PCAP-PDU CHOICE presence to PCAP_PDU_PR_initiatingMessage\n");
  }

  tm_o->choice.initiatingMessage = im;

  if (argc > 1) {
    printf("\n PCAP Sample Ready to encode:\n");
    xer_fprint(stdout, &asn_DEF_PCAP_PDU, tm_i);
    printf("\n");
  }

  er = asn_encode_to_buffer(NULL, ATS_ALIGNED_BASIC_PER,
                &asn_DEF_PCAP_PDU,
                (const void *)tm_o,
                (void *)buffer, buf_size);
  if (er.encoded >= 0) {
    printf("\n PCAP Sample encoded successfully (%ld bytes)\n\n", er.encoded);

    fflush(stdout);
  } else {
    fprintf(stderr, "\n Failed to encode PCAP Sample: returned %ld\n\n", er.encoded);
  }
}
velichkov commented 6 years ago

Hi @ovlnmurty,

I am trying to add components with following snippet...

// Component-1. ID=28
PositionInitiationRequestIEs_t ie1;
RequestType_t rt;

 ie1.criticality = Criticality_reject;
 ie1.id = 28;
 ie1.value.present = PositionInitiationRequestIEs__value_PR_RequestType;
 rt.event = 1;
 rt.reportArea = 1;
 RequestTypeAccuracyCode_t rac = 11;
 rt.horizontalaccuracyCode =&rac;
 ie1.value.choice.RequestType = rt;

 ASN_SEQUENCE_ADD(&im->value.choice.PositionInitiationRequest.protocolIEs.list, &ie1);

Is this the right way to add elements in the ASN Sequence ?

Generally this is the correct way, you only need to dynamically allocate the ie1 and rac otherwise there will be a segmentation fault when you try to free the memory.

PositionInitiationRequestIEs_t ie1 = calloc(1, sizeof(PositionInitiationRequestIEs_t));

Usually you could find examples how to use something in the tests https://github.com/mouse07410/asn1c/blob/8a29c8e5b09b757e3420473c940f913b9bd90fcc/tests/tests-c-compiler/check-src/check-42.c#L103-L139

Strange thing is i called asn_fprint before encoding to check all the parameters are in place or not... with that it is able to encode... if i remove that (though its purpose is to print the structure in readable form) it is failing with -1. I need to go in detail why this behavior... I will update my findings...

Try running the application through valgrind, it finds some uninitialized variables that could explain the random behavior

$ gcc -g3 -O0 -o test  -I. -L.  test.c  -lasncodec
$ valgrind --tool=memcheck ./test 
==30673== Memcheck, a memory error detector
==30673== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==30673== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==30673== Command: ./test
==30673== 

 PCAP Sample encode Started...

 Encoding Starts...

 PCAP Sample Before encode...

 PCAP Sample Ready to encode...
==30673== Conditional jump or move depends on uninitialised value(s)
==30673==    at 0x40BFB1: SEQUENCE_encode_aper (constr_SEQUENCE.c:1837)
==30673==    by 0x413854: aper_encode (per_encoder.c:251)
==30673==    by 0x4135E5: aper_encode_to_new_buffer (per_encoder.c:189)
==30673==    by 0x414A5E: aper_open_type_put (per_opentype.c:488)
==30673==    by 0x4036A1: OPEN_TYPE_encode_aper (OPEN_TYPE.c:501)
==30673==    by 0x40C1CF: SEQUENCE_encode_aper (constr_SEQUENCE.c:1888)
==30673==    by 0x469B94: SEQUENCE_OF_encode_aper (constr_SEQUENCE_OF.c:289)
==30673==    by 0x40C1CF: SEQUENCE_encode_aper (constr_SEQUENCE.c:1888)
==30673==    by 0x413854: aper_encode (per_encoder.c:251)
==30673==    by 0x4135E5: aper_encode_to_new_buffer (per_encoder.c:189)
==30673==    by 0x414A5E: aper_open_type_put (per_opentype.c:488)
==30673==    by 0x4036A1: OPEN_TYPE_encode_aper (OPEN_TYPE.c:501)
.....

When debugging encode/decode problems add -DASN_EMIT_DEBUG=1 to CFLAGS and clean and recompile everything.

mouse07410 commented 6 years ago

I tried valgrind, but got hit by many false positives.

Here are the complete valgrind output, and the source of the program in question: t.c.txt valgrind.out.txt

BTW, this program does not crash or fail to encode. But valgrind still complains.

velichkov commented 6 years ago

I tried valgrind, but got hit by many false positives. BTW, this program does not crash or fail to encode. But valgrind still complains.

Return non-zero code when encode fails and try running the example in a while loop for several hours. It's a matter of time until it fails.

Try to initialize all structs allocated on the stack

   PositionInitiationRequestIEs_t ie1 = {0};
   RequestType_t rt = {0}

and you will see those are not "false positives"

==40980== LEAK SUMMARY: ==40980== definitely lost: 488 bytes in 10 blocks ==40980== indirectly lost: 8,392 bytes in 19 blocks

Try freeing all the memory and you will observe a crash

ASN_STRUCT_FREE(asn_DEF_PCAP_PDU, tm_o);
Program received signal SIGABRT, Aborted.
0x00007ffff7a389fb in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: dnf debuginfo-install libgcc-7.3.1-2.fc26.x86_64
(gdb) bt
#0  0x00007ffff7a389fb in raise () from /lib64/libc.so.6
#1  0x00007ffff7a3a800 in abort () from /lib64/libc.so.6
#2  0x00007ffff7a7ebb1 in __libc_message () from /lib64/libc.so.6
#3  0x00007ffff7a89a59 in _int_free () from /lib64/libc.so.6
#4  0x00007ffff7a8f3be in free () from /lib64/libc.so.6
#5  0x000000000042ba77 in NativeInteger_free (td=0x790120 <asn_DEF_RequestTypeAccuracyCode>, ptr=0x7fffffffd798, method=ASFM_FREE_EVERYTHING)
    at NativeInteger.c:433
#6  0x000000000040f74b in SEQUENCE_free (td=0x78fe20 <asn_DEF_RequestType>, sptr=0x7fffffffd7f8, method=ASFM_FREE_UNDERLYING)
    at constr_SEQUENCE.c:1002
mouse07410 commented 6 years ago

Thank you very much! I've learned quite a bit from this.

It looks like (a) our demo program did not do the right things allocating and freeing the memory, and (b) asn1c skeletons/*.c files do not do a good job initializing all the needed variables (and maybe worse).

I modified the demo program to address what I could (and it does not crash on freeing any more ;), and indeed the number of errors reported by valgrind dropped from 200+ to 23, and after initializing some stuff in the skeletons/ to zero (as you showed), it dropped further down to 9. But at this point I cannot figure what causes the remaining 9 errors, and suspect it's something within the skeletons files. Attaching the current logs: valgrind.console.txt t.c.txt valgrind.log.txt

velichkov commented 6 years ago

Hi @mouse07410,

I've learned quite a bit from this.

An alternative approach to using valgrind is to compile with -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer -fsanitize=address. Various other options are also available depending on the version, see https://clang.llvm.org/docs/UsersManual.html#controlling-code-generation

(b) asn1c skeletons/*.c files do not do a good job initializing all the needed variables (and maybe worse).

I disagree.

I've tested with both gcc (GCC) 7.3.1 20180130 (Red Hat 7.3.1-2) and clang version 4.0.1 (tags/RELEASE_401/final) on Fedora 26 and could only reproduce one of the errors that you have observed.

==40980== 14 errors in context 9 of 25:
==40980== Conditional jump or move depends on uninitialised value(s)
==40980==    at 0x10008CFD5: SEQUENCE_encode_aper (constr_SEQUENCE.c:1890)
==40980==    by 0x1000B5AC6: aper_encode (per_encoder.c:251)
==40980==    by 0x1000B5D19: aper_encode_to_new_buffer (per_encoder.c:189)
==40980==    by 0x1000B6D58: aper_open_type_put (per_opentype.c:488)
==40980==    by 0x10006471E: OPEN_TYPE_encode_aper (OPEN_TYPE.c:501)
==40980==    by 0x10008CFAE: SEQUENCE_encode_aper (constr_SEQUENCE.c:1888)
==40980==    by 0x10009082A: SEQUENCE_OF_encode_aper (constr_SEQUENCE_OF.c:289)
==40980==    by 0x10008CFAE: SEQUENCE_encode_aper (constr_SEQUENCE.c:1888)
==40980==    by 0x1000B5AC6: aper_encode (per_encoder.c:251)
==40980==    by 0x1000B5D19: aper_encode_to_new_buffer (per_encoder.c:189)
==40980==    by 0x1000B6D58: aper_open_type_put (per_opentype.c:488)
==40980==    by 0x10006471E: OPEN_TYPE_encode_aper (OPEN_TYPE.c:501)
==40980==  Uninitialised value was created by a stack allocation
==40980==    at 0x100068850: BOOLEAN_encode_aper (BOOLEAN.c:358)

And to fix it you need to apply the following fix which basically makes BOOLEAN_encode_aper same as BOOLEAN_encode_uper

diff --git a/skeletons/BOOLEAN.c b/skeletons/BOOLEAN.c
index b6f266c5..114a77c6 100644
--- a/skeletons/BOOLEAN.c
+++ b/skeletons/BOOLEAN.c
@@ -355,14 +355,15 @@ asn_enc_rval_t
 BOOLEAN_encode_aper(const asn_TYPE_descriptor_t *td,
                     const asn_per_constraints_t *constraints,
                     const void *sptr, asn_per_outp_t *po) {
-    const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
-    asn_enc_rval_t er;
+    const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
+    asn_enc_rval_t er = { 0, 0, 0 };

     (void)constraints;

     if(!st) ASN__ENCODE_FAILED;

-    per_put_few_bits(po, *st ? 1 : 0, 1);
+    if(per_put_few_bits(po, *st ? 1 : 0, 1))
+        ASN__ENCODE_FAILED;

     ASN__ENCODED_OK(er);
 }

Here is the output

$valgrind -v --tool=memcheck --leak-check=full --track-origins=yes --show-leak-kinds=all ./t.new
......
Trying to encode PCAP-PDU from scratch...
--31502-- REDIR: 0x4ed7340 (libc.so.6:memcpy@@GLIBC_2.14) redirected to 0x4a286de (_vgnU_ifunc_wrapper)
--31502-- REDIR: 0x4ed20d0 (libc.so.6:memcpy@GLIBC_2.2.5) redirected to 0x4c31ef0 (memcpy@GLIBC_2.2.5)

 InitiatingMessage encoded successfully (49 bytes)

 PCAP Sample encoded successfully (50 bytes)

 Freed allocated PCAP-PDU struct

==31502== 
==31502== HEAP SUMMARY:
==31502==     in use at exit: 0 bytes in 0 blocks
==31502==   total heap usage: 107 allocs, 107 frees, 4,970 bytes allocated
==31502== 
==31502== All heap blocks were freed -- no leaks are possible
==31502== 
==31502== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==31502== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
mouse07410 commented 6 years ago

@velichkov thank you!! Yes, your fix works. Update Except that on Mac I'm still getting one error - see my next post.

But I don't understand - there's a ton of uninitialized in skeletons/*.c:

$ fgrep -n "asn_enc_rval_t er" skeletons/*.c
. . . . .
skeletons/constr_SET_OF_oer.c:267:        asn_enc_rval_t er;
skeletons/constr_SET_OF_oer.c:279:        asn_enc_rval_t erval;
skeletons/converter-example.c:445:        asn_enc_rval_t erv;
skeletons/oer_encoder.c:85:    asn_enc_rval_t er = {0, 0, 0};
skeletons/oer_encoder.c:124:    asn_enc_rval_t er;
skeletons/per_encoder.c:20:    asn_enc_rval_t er;
skeletons/per_encoder.c:117:    asn_enc_rval_t er;
skeletons/per_encoder.c:184:    asn_enc_rval_t er;
skeletons/per_encoder.c:236:    asn_enc_rval_t er;
skeletons/xer_encoder.c:16:    asn_enc_rval_t er = {0, 0, 0};
skeletons/xer_encoder.c:56:    asn_enc_rval_t er;

To be precise, out of 85 occurrences, only 10 are initialized. Why don't those definitions cause valgrind errors, and shouldn't they be initialized the same way as you showed above?

mouse07410 commented 6 years ago

@velichkov even after your fix, I'm still getting one error, which I cannot identify or at least nail to a spot in the code. Here are the files. t.c.txt valgrind.log.txt valgrind.console.txt

mouse07410 commented 6 years ago

@velichkov using Clang sanitizer, I stumbled upon an interesting problem. It looks like awn_bit_data.c has heap buffer overflow in ASN_DEBUG)? When I removed -DASN_EMIT_DEBUG=1, I got a clean run...

. . . . .
clang -maes -mpclmul -msse2 -mssse3 -msse4 -msse4.2 -mrdrnd -mrdseed -Os -Ofast  -g3  -O0  -DASN_EMIT_DEBUG=1 -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer -fsanitize=address -DASN_PDU_COLLECTION -DJUNKTEST -I. -g3  -O0  -DASN_EMIT_DEBUG=1 -fsanitize=undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer -fsanitize=address  -g -o pcap-demo t.c  libasncodec.a -lm
./pcap-demo

 PCAP Sample decode Started...
  [PER got  1<=400 bits => span 1 +0[1..400]:00 (399) => 0x0] (asn_bit_data.c:139)
  [PER got  2<=399 bits => span 3 +0[3..400]:00 (397) => 0x0] (asn_bit_data.c:139)
CHOICE PCAP-PDU got index 0 in range 2 (constr_CHOICE.c:1052)
Discovered CHOICE PCAP-PDU encodes initiatingMessage (constr_CHOICE.c:1080)
Decoding InitiatingMessage as SEQUENCE (APER) (constr_SEQUENCE.c:1509)
Decoding member "procedureCode" in InitiatingMessage (constr_SEQUENCE.c:1599)
Decoding NativeInteger ProcedureCode (APER) (NativeInteger.c:334)
Integer with range 8 bits (INTEGER.c:865)
Aligning 5 bits (per_support.c:301)
  [PER got  5<=397 bits => span 8 +0[8..400]:00 (392) => 0x0] (asn_bit_data.c:139)
  [PER got  8<=392 bits => span 16 +1[8..392]:09 (384) => 0x9] (asn_bit_data.c:139)
Got value 9 + low 0 (INTEGER.c:926)
NativeInteger ProcedureCode got value 9 (NativeInteger.c:351)
Freeing INTEGER as a primitive type (asn_codecs_prim.c:125)
Decoding member "criticality" in InitiatingMessage (constr_SEQUENCE.c:1599)
Decoding Criticality as NativeEnumerated (NativeEnumerated.c:255)
  [PER got  2<=384 bits => span 18 +2[2..384]:20 (382) => 0x0] (asn_bit_data.c:139)
Decoded Criticality = 0 (NativeEnumerated.c:293)
Decoding member "transactionID" in InitiatingMessage (constr_SEQUENCE.c:1599)
  [PER got  1<=382 bits => span 19 +2[3..384]:20 (381) => 0x1] (asn_bit_data.c:139)
CHOICE TransactionID got index 1 in range 1 (constr_CHOICE.c:1052)
Discovered CHOICE TransactionID encodes longTID (constr_CHOICE.c:1080)
Decoding NativeInteger INTEGER (APER) (NativeInteger.c:334)
Integer with range 15 bits (INTEGER.c:865)
Aligning 5 bits (per_support.c:301)
  [PER got  5<=381 bits => span 24 +2[8..384]:20 (376) => 0x0] (asn_bit_data.c:139)
  [PER got 16<=376 bits => span 40 +3[16..376]:07 (360) => 0x7ce] (asn_bit_data.c:139)
Got value 1998 + low 0 (INTEGER.c:926)
NativeInteger INTEGER got value 1998 (NativeInteger.c:351)
Freeing INTEGER as a primitive type (asn_codecs_prim.c:125)
Decoding member "value" in InitiatingMessage (constr_SEQUENCE.c:1599)
Getting open type PositionInitiationRequest... (per_opentype.c:413)
  [PER got  8<=360 bits => span 48 +5[8..360]:2c (352) => 0x2c] (asn_bit_data.c:139)
  [PER got 24<=352 bits => span 72 +6[24..352]:00 (328) => 0x7] (asn_bit_data.c:139)
  [PER got 24<=328 bits => span 96 +9[24..328]:00 (304) => 0x1c00] (asn_bit_data.c:139)
  [PER got 24<=304 bits => span 120 +12[24..304]:02 (280) => 0x24516] (asn_bit_data.c:139)
  [PER got 24<=280 bits => span 144 +15[24..280]:00 (256) => 0x1d00] (asn_bit_data.c:139)
  [PER got 24<=256 bits => span 168 +2[24..256]:02 (232) => 0x22420] (asn_bit_data.c:139)
  [PER got 24<=232 bits => span 192 +5[24..232]:00 (208) => 0x1e00] (asn_bit_data.c:139)
  [PER got 24<=208 bits => span 216 +8[24..208]:05 (184) => 0x50001] (asn_bit_data.c:139)
  [PER got 24<=184 bits => span 240 +11[24..184]:2f (160) => 0x2f55fb] (asn_bit_data.c:139)
  [PER got 24<=160 bits => span 264 +14[24..160]:00 (136) => 0x1940] (asn_bit_data.c:139)
  [PER got 24<=136 bits => span 288 +1[24..136]:01 (112) => 0x1fe00] (asn_bit_data.c:139)
  [PER got 24<=112 bits => span 312 +4[24..112]:1f (88) => 0x1f4001] (asn_bit_data.c:139)
  [PER got 24<=88 bits => span 336 +7[24..88]:40 (64) => 0x400020] (asn_bit_data.c:139)
  [PER got 24<=64 bits => span 360 +10[24..64]:40 (40) => 0x400140] (asn_bit_data.c:139)
  [PER got 24<=40 bits => span 384 +13[24..40]:00 (16) => 0x2140] (asn_bit_data.c:139)
  [PER got 16<=16 bits => span 400 +0[16..16]:01 (0) => 0x130] (asn_bit_data.c:139)
Getting open type PositionInitiationRequest encoded in 44 bytes (per_opentype.c:439)
    Decoding PositionInitiationRequest as SEQUENCE (APER) (constr_SEQUENCE.c:1509)
      [PER got  1<=352 bits => span 1 +0[1..352]:00 (351) => 0x0] (asn_bit_data.c:139)
      [PER got  1<=351 bits => span 2 +0[2..352]:00 (350) => 0x0] (asn_bit_data.c:139)
    Read in presence bitmap for PositionInitiationRequest of 1 bits (0..) (constr_SEQUENCE.c:1532)
    Decoding member "protocolIEs" in PositionInitiationRequest (constr_SEQUENCE.c:1599)
    getting nsnnwn with range 65536 (per_support.c:361)
    Aligning 6 bits (per_support.c:301)
      [PER got  6<=350 bits => span 8 +0[8..352]:00 (344) => 0x0] (asn_bit_data.c:139)
      [PER got 16<=344 bits => span 24 +1[16..344]:00 (328) => 0x7] (asn_bit_data.c:139)
    Preparing to fetch 7+0 elements from ProtocolIE-Container (constr_SET_OF.c:1141)
    SET OF PositionInitiationRequestIEs decoding (constr_SET_OF.c:1160)
    Decoding PositionInitiationRequestIEs as SEQUENCE (APER) (constr_SEQUENCE.c:1509)
    Decoding member "id" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Decoding NativeInteger ProtocolIE-ID (APER) (NativeInteger.c:334)
    Integer with range 16 bits (INTEGER.c:865)
      [PER got 16<=328 bits => span 40 +3[16..328]:00 (312) => 0x1c] (asn_bit_data.c:139)
    Got value 28 + low 0 (INTEGER.c:926)
    NativeInteger ProtocolIE-ID got value 28 (NativeInteger.c:351)
    Freeing INTEGER as a primitive type (asn_codecs_prim.c:125)
    Decoding member "criticality" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Decoding Criticality as NativeEnumerated (NativeEnumerated.c:255)
      [PER got  2<=312 bits => span 42 +5[2..312]:00 (310) => 0x0] (asn_bit_data.c:139)
    Decoded Criticality = 0 (NativeEnumerated.c:293)
    Decoding member "value" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Getting open type RequestType... (per_opentype.c:413)
    Aligning 6 bits (per_support.c:301)
      [PER got  6<=310 bits => span 48 +5[8..312]:00 (304) => 0x0] (asn_bit_data.c:139)
      [PER got  8<=304 bits => span 56 +6[8..304]:02 (296) => 0x2] (asn_bit_data.c:139)
      [PER got 16<=296 bits => span 72 +7[16..296]:45 (280) => 0x4516] (asn_bit_data.c:139)
    Getting open type RequestType encoded in 2 bytes (per_opentype.c:439)
        Decoding RequestType as SEQUENCE (APER) (constr_SEQUENCE.c:1509)
          [PER got  1<=16 bits => span 1 +0[1..16]:45 (15) => 0x0] (asn_bit_data.c:139)
          [PER got  2<=15 bits => span 3 +0[3..16]:45 (13) => 0x2] (asn_bit_data.c:139)
        Read in presence bitmap for RequestType of 2 bits (80..) (constr_SEQUENCE.c:1532)
        Decoding member "event" in RequestType (constr_SEQUENCE.c:1599)
        Decoding RequestTypeEvent as NativeEnumerated (NativeEnumerated.c:255)
          [PER got  1<=13 bits => span 4 +0[4..16]:45 (12) => 0x0] (asn_bit_data.c:139)
          [PER got  2<=12 bits => span 6 +0[6..16]:45 (10) => 0x1] (asn_bit_data.c:139)
        Decoded RequestTypeEvent = 1 (NativeEnumerated.c:293)
        Decoding member "reportArea" in RequestType (constr_SEQUENCE.c:1599)
        Decoding RequestTypeReportArea as NativeEnumerated (NativeEnumerated.c:255)
          [PER got  1<=10 bits => span 7 +0[7..16]:45 (9) => 0x0] (asn_bit_data.c:139)
          [PER got  1<= 9 bits => span 8 +0[8..16]:45 (8) => 0x1] (asn_bit_data.c:139)
        Decoded RequestTypeReportArea = 1 (NativeEnumerated.c:293)
          [PER got  1<= 2 bits => span 1 +0[1..2]:80 (1) => 0x1] (asn_bit_data.c:139)
        Member RequestType->horizontalaccuracyCode is optional, p=1 (1->2) (constr_SEQUENCE.c:1581)
        Decoding member "horizontalaccuracyCode" in RequestType (constr_SEQUENCE.c:1599)
        Decoding NativeInteger RequestTypeAccuracyCode (APER) (NativeInteger.c:334)
        Integer with range 7 bits (INTEGER.c:865)
          [PER got  7<= 8 bits => span 15 +1[7..8]:16 (1) => 0xb] (asn_bit_data.c:139)
        Got value 11 + low 0 (INTEGER.c:926)
        NativeInteger RequestTypeAccuracyCode got value 11 (NativeInteger.c:351)
        Freeing INTEGER as a primitive type (asn_codecs_prim.c:125)
          [PER got  1<= 1 bits => span 2 +0[2..2]:80 (0) => 0x0] (asn_bit_data.c:139)
        Member RequestType->iE-Extensions is optional, p=0 (2->2) (constr_SEQUENCE.c:1581)
      [PER got  1<= 1 bits => span 16 +1[8..8]:16 (0) => 0x0] (asn_bit_data.c:139)
    ProtocolIE-Container SET OF PositionInitiationRequestIEs decoded 0, 0x60d000000040 (constr_SET_OF.c:1164)
    SET OF PositionInitiationRequestIEs decoding (constr_SET_OF.c:1160)
    Decoding PositionInitiationRequestIEs as SEQUENCE (APER) (constr_SEQUENCE.c:1509)
    Decoding member "id" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Decoding NativeInteger ProtocolIE-ID (APER) (NativeInteger.c:334)
    Integer with range 16 bits (INTEGER.c:865)
      [PER got 16<=280 bits => span 88 +9[16..280]:00 (264) => 0x1d] (asn_bit_data.c:139)
    Got value 29 + low 0 (INTEGER.c:926)
    NativeInteger ProtocolIE-ID got value 29 (NativeInteger.c:351)
    Freeing INTEGER as a primitive type (asn_codecs_prim.c:125)
    Decoding member "criticality" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Decoding Criticality as NativeEnumerated (NativeEnumerated.c:255)
      [PER got  2<=264 bits => span 90 +11[2..264]:00 (262) => 0x0] (asn_bit_data.c:139)
    Decoded Criticality = 0 (NativeEnumerated.c:293)
    Decoding member "value" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Getting open type UE-PositioningCapability... (per_opentype.c:413)
    Aligning 6 bits (per_support.c:301)
      [PER got  6<=262 bits => span 96 +11[8..264]:00 (256) => 0x0] (asn_bit_data.c:139)
      [PER got  8<=256 bits => span 104 +12[8..256]:02 (248) => 0x2] (asn_bit_data.c:139)
      [PER got 16<=248 bits => span 120 +13[16..248]:24 (232) => 0x2420] (asn_bit_data.c:139)
    Getting open type UE-PositioningCapability encoded in 2 bytes (per_opentype.c:439)
        Decoding UE-PositioningCapability as SEQUENCE (APER) (constr_SEQUENCE.c:1509)
          [PER got  1<=16 bits => span 1 +0[1..16]:24 (15) => 0x0] (asn_bit_data.c:139)
          [PER got  1<=15 bits => span 2 +0[2..16]:24 (14) => 0x0] (asn_bit_data.c:139)
        Read in presence bitmap for UE-PositioningCapability of 1 bits (0..) (constr_SEQUENCE.c:1532)
        Decoding member "standAloneLocationMethodsSupported" in UE-PositioningCapability (constr_SEQUENCE.c:1599)
          [PER got  1<=14 bits => span 3 +0[3..16]:24 (13) => 0x1] (asn_bit_data.c:139)
        BOOLEAN decoded as TRUE (BOOLEAN.c:347)
        Decoding member "ueBasedOTDOASupported" in UE-PositioningCapability (constr_SEQUENCE.c:1599)
          [PER got  1<=13 bits => span 4 +0[4..16]:24 (12) => 0x0] (asn_bit_data.c:139)
        BOOLEAN decoded as FALSE (BOOLEAN.c:347)
        Decoding member "networkAssistedGPSSupport" in UE-PositioningCapability (constr_SEQUENCE.c:1599)
        Decoding NetworkAssistedGPSSuport as NativeEnumerated (NativeEnumerated.c:255)
          [PER got  1<=12 bits => span 5 +0[5..16]:24 (11) => 0x0] (asn_bit_data.c:139)
          [PER got  2<=11 bits => span 7 +0[7..16]:24 (9) => 0x2] (asn_bit_data.c:139)
        Decoded NetworkAssistedGPSSuport = 2 (NativeEnumerated.c:293)
        Decoding member "supportGPSTimingOfCellFrame" in UE-PositioningCapability (constr_SEQUENCE.c:1599)
          [PER got  1<= 9 bits => span 8 +0[8..16]:24 (8) => 0x0] (asn_bit_data.c:139)
        BOOLEAN decoded as FALSE (BOOLEAN.c:347)
        Decoding member "supportForIPDL" in UE-PositioningCapability (constr_SEQUENCE.c:1599)
          [PER got  1<= 8 bits => span 9 +1[1..8]:20 (7) => 0x0] (asn_bit_data.c:139)
        BOOLEAN decoded as FALSE (BOOLEAN.c:347)
        Decoding member "supportForRxTxTimeDiff" in UE-PositioningCapability (constr_SEQUENCE.c:1599)
          [PER got  1<= 7 bits => span 10 +1[2..8]:20 (6) => 0x0] (asn_bit_data.c:139)
        BOOLEAN decoded as FALSE (BOOLEAN.c:347)
        Decoding member "supportForUEAGPSinCellPCH" in UE-PositioningCapability (constr_SEQUENCE.c:1599)
          [PER got  1<= 6 bits => span 11 +1[3..8]:20 (5) => 0x1] (asn_bit_data.c:139)
        BOOLEAN decoded as TRUE (BOOLEAN.c:347)
        Decoding member "supportForSFNSFNTimeDiff" in UE-PositioningCapability (constr_SEQUENCE.c:1599)
          [PER got  1<= 5 bits => span 12 +1[4..8]:20 (4) => 0x0] (asn_bit_data.c:139)
        BOOLEAN decoded as FALSE (BOOLEAN.c:347)
          [PER got  1<= 1 bits => span 1 +0[1..1]:00 (0) => 0x0] (asn_bit_data.c:139)
        Member UE-PositioningCapability->iE-Extensions is optional, p=0 (1->1) (constr_SEQUENCE.c:1581)
      [PER got  4<= 4 bits => span 16 +1[8..8]:20 (0) => 0x0] (asn_bit_data.c:139)
    ProtocolIE-Container SET OF PositionInitiationRequestIEs decoded 0, 0x60d000000110 (constr_SET_OF.c:1164)
    SET OF PositionInitiationRequestIEs decoding (constr_SET_OF.c:1160)
    Decoding PositionInitiationRequestIEs as SEQUENCE (APER) (constr_SEQUENCE.c:1509)
    Decoding member "id" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Decoding NativeInteger ProtocolIE-ID (APER) (NativeInteger.c:334)
    Integer with range 16 bits (INTEGER.c:865)
      [PER got 16<=232 bits => span 136 +15[16..232]:00 (216) => 0x1e] (asn_bit_data.c:139)
    Got value 30 + low 0 (INTEGER.c:926)
    NativeInteger ProtocolIE-ID got value 30 (NativeInteger.c:351)
    Freeing INTEGER as a primitive type (asn_codecs_prim.c:125)
    Decoding member "criticality" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Decoding Criticality as NativeEnumerated (NativeEnumerated.c:255)
      [PER got  2<=216 bits => span 138 +1[2..216]:00 (214) => 0x0] (asn_bit_data.c:139)
    Decoded Criticality = 0 (NativeEnumerated.c:293)
    Decoding member "value" in PositionInitiationRequestIEs (constr_SEQUENCE.c:1599)
    Getting open type UC-ID... (per_opentype.c:413)
    Aligning 6 bits (per_support.c:301)
      [PER got  6<=214 bits => span 144 +1[8..216]:00 (208) => 0x0] (asn_bit_data.c:139)
      [PER got  8<=208 bits => span 152 +2[8..208]:05 (200) => 0x5] (asn_bit_data.c:139)
      [PER got 24<=200 bits => span 176 +3[24..200]:00 (176) => 0x12f] (asn_bit_data.c:139)
      [PER got 16<=176 bits => span 192 +6[16..176]:55 (160) => 0x55fb] (asn_bit_data.c:139)
    Getting open type UC-ID encoded in 5 bytes (per_opentype.c:439)
        Decoding UC-ID as SEQUENCE (APER) (constr_SEQUENCE.c:1509)
          [PER got  1<=40 bits => span 1 +0[1..40]:00 (39) => 0x0] (asn_bit_data.c:139)
          [PER got  1<=39 bits => span 2 +0[2..40]:00 (38) => 0x0] (asn_bit_data.c:139)
        Read in presence bitmap for UC-ID of 1 bits (0..) (constr_SEQUENCE.c:1532)
        Decoding member "rNC-ID" in UC-ID (constr_SEQUENCE.c:1599)
        Decoding NativeInteger INTEGER (APER) (NativeInteger.c:334)
        Integer with range 12 bits (INTEGER.c:865)
        Aligning 6 bits (per_support.c:301)
          [PER got  6<=38 bits => span 8 +0[8..40]:00 (32) => 0x0] (asn_bit_data.c:139)
          [PER got 16<=32 bits => span 24 +1[16..32]:01 (16) => 0x12f] (asn_bit_data.c:139)
        Got value 303 + low 0 (INTEGER.c:926)
        NativeInteger INTEGER got value 303 (NativeInteger.c:351)
        Freeing INTEGER as a primitive type (asn_codecs_prim.c:125)
        Decoding member "c-ID" in UC-ID (constr_SEQUENCE.c:1599)
        Decoding NativeInteger INTEGER (APER) (NativeInteger.c:334)
        Integer with range 16 bits (INTEGER.c:865)
          [PER got 16<=16 bits => span 40 +3[16..16]:55 (0) => 0x55fb] (asn_bit_data.c:139)
        Got value 22011 + low 0 (INTEGER.c:926)
        NativeInteger INTEGER got value 22011 (NativeInteger.c:351)
        Freeing INTEGER as a primitive type (asn_codecs_prim.c:125)
          [PER got  1<= 1 bits => span 1 +0[1..1]:00 (0) => 0x0] (asn_bit_data.c:139)
        Member UC-ID->iE-Extensions is optional, p=0 (1->1) (constr_SEQUENCE.c:1581)
    =================================================================
==25294==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000315 at pc 0x000102ce9c15 bp 0x7fff5d20a8d0 sp 0x7fff5d20a8c8
READ of size 1 at 0x602000000315 thread T0
    #0 0x102ce9c14 in asn_get_few_bits asn_bit_data.c:132
    #1 0x102d878fd in aper_open_type_get_simple per_opentype.c:455
    #2 0x102d85dcc in aper_open_type_get per_opentype.c:514
    #3 0x102bc03c0 in OPEN_TYPE_aper_get OPEN_TYPE.c:444
    #4 0x102c8eba4 in SEQUENCE_decode_aper constr_SEQUENCE.c:1602
    #5 0x102cd032e in SET_OF_decode_aper constr_SET_OF.c:1161
    #6 0x102c8f129 in SEQUENCE_decode_aper constr_SEQUENCE.c:1604
    #7 0x102d87435 in aper_open_type_get_simple per_opentype.c:446
    #8 0x102d85dcc in aper_open_type_get per_opentype.c:514
    #9 0x102bc03c0 in OPEN_TYPE_aper_get OPEN_TYPE.c:444
    #10 0x102c8eba4 in SEQUENCE_decode_aper constr_SEQUENCE.c:1602
    #11 0x102c4e10e in CHOICE_decode_aper constr_CHOICE.c:1083
    #12 0x102d79bed in aper_decode per_decoder.c:171
    #13 0x102d77e45 in aper_decode_complete per_decoder.c:104
    #14 0x102ce4e46 in asn_decode asn_application.c:473
    #15 0x1029ecfcf in main t.c:39
    #16 0x7fffb6ed2234 in start (libdyld.dylib:x86_64+0x5234)

0x602000000315 is located 0 bytes to the right of 5-byte region [0x602000000310,0x602000000315)
allocated by thread T0 here:
    #0 0x1037a0230 in wrap_realloc (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x59230)
    #1 0x102d86652 in aper_open_type_get_simple per_opentype.c:424
    #2 0x102d85dcc in aper_open_type_get per_opentype.c:514
    #3 0x102bc03c0 in OPEN_TYPE_aper_get OPEN_TYPE.c:444
    #4 0x102c8eba4 in SEQUENCE_decode_aper constr_SEQUENCE.c:1602
    #5 0x102cd032e in SET_OF_decode_aper constr_SET_OF.c:1161
    #6 0x102c8f129 in SEQUENCE_decode_aper constr_SEQUENCE.c:1604
    #7 0x102d87435 in aper_open_type_get_simple per_opentype.c:446
    #8 0x102d85dcc in aper_open_type_get per_opentype.c:514
    #9 0x102bc03c0 in OPEN_TYPE_aper_get OPEN_TYPE.c:444
    #10 0x102c8eba4 in SEQUENCE_decode_aper constr_SEQUENCE.c:1602
    #11 0x102c4e10e in CHOICE_decode_aper constr_CHOICE.c:1083
    #12 0x102d79bed in aper_decode per_decoder.c:171
    #13 0x102d77e45 in aper_decode_complete per_decoder.c:104
    #14 0x102ce4e46 in asn_decode asn_application.c:473
    #15 0x1029ecfcf in main t.c:39
    #16 0x7fffb6ed2234 in start (libdyld.dylib:x86_64+0x5234)

SUMMARY: AddressSanitizer: heap-buffer-overflow asn_bit_data.c:132 in asn_get_few_bits
Shadow bytes around the buggy address:
  0x1c0400000010: fa fa 00 00 fa fa 00 06 fa fa fd fa fa fa fd fa
  0x1c0400000020: fa fa fd fa fa fa fd fa fa fa 02 fa fa fa fd fa
  0x1c0400000030: fa fa fd fa fa fa fd fa fa fa fd fa fa fa 00 fa
  0x1c0400000040: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa
  0x1c0400000050: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa
=>0x1c0400000060: fa fa[05]fa fa fa fd fa fa fa fd fa fa fa fd fa
  0x1c0400000070: fa fa fd fa fa fa fd fa fa fa fa fa fa fa fa fa
  0x1c0400000080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1c0400000090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1c04000000a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1c04000000b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==25294==ABORTING
make: *** [run] Abort trap: 6
velichkov commented 6 years ago

@velichkov even after your fix, I'm still getting one error, which I cannot identify or at least nail to a spot in the code.

==65984== 72 bytes in 3 blocks are possibly lost in loss record 23 of 40
==65984==    at 0x10039809A: calloc (vg_replace_malloc.c:714)
==65984==    by 0x1009AE7E2: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==65984==    by 0x1009C17DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==65984==    by 0x1002F2C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==65984==    by 0x1002F2E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==65984==    by 0x10050E81D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==65984==    by 0x1009AE075: _objc_init (in /usr/lib/libobjc.A.dylib)
==65984==    by 0x100498ACC: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==65984==    by 0x100498AB3: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==65984==    by 0x1003A89C2: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==65984==    by 0x100304A09: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==65984==    by 0x100304C39: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)

I believe these errors are all OSX related and are not problems with the asn1c code so you should be safe to ignore them. There are suppressions for similar errors in default.supp

--5984-- used_suppression:      4 OSX1013:18-Leak /opt/local/lib/valgrind/default.supp:915 suppressed: 120 bytes in 4 blocks
--65984-- used_suppression:      1 OSX1013:dyld-5 /opt/local/lib/valgrind/default.supp:1283

You could also try to reproduce them with an empty main function that just does nothing.

I will take a look at the heap overflow later today or tomorrow.

velichkov commented 6 years ago

Hi @mouse07410,

I just pushed a commit (35fbfa9) that resolve the heap overflow together with 2 other commits in a new branch mouse07410_vlm_master that tracks your vlm_master

mouse07410 commented 6 years ago

@velichkov thank you - instead of cherry-picking that commit, I'm merging your mouse07410_vlm_master branch into my vlm_master.