Menu

Invalid access when accessing parameters from callback.

wmealing
2023-06-24
2023-07-03
  • wmealing

    wmealing - 2023-06-24

    Hello again,

    I'm trying to use libcurl to fetch a page, I've setup my current
    configuration here:

    https://github.com/wmealing/ca/tree/main

    It is based on the libcurl demo from the gnucobol docs. Should be
    able to build it with "make" , assuming you have gnucobol-devel, make
    and libcurl installed.

    I'm having a problem with libcurl's callback from libcurl into the
    cobol CURLOPT_WRITEFUNCTION 'function when accessing the parameters.

    All the parameters passed to curl-writeback seem to be causing an
    "attempt to reference invalid memory address (signal)" error. when
    they are attempted to be accessed.

    My first thought was that this has something to do with the lifetime
    of the items in the linkage section, but after trying many options I
    didn't get any luck. I think (or assume) that libcurl would have
    copied the parameters and that wasn't something that I would have to
    look after.

    What am I doing wrong ? Thanks in advance for any help you can
    provide. I eventually want to end up using this for fetching json, as
    a rest style client, if a project like this exists please let me know,
    as I'd love to learn from it.

    --
    Wade Mealing

     
  • wmealing

    wmealing - 2023-06-24

    I do have a basic example working, where I wrote my own callback ( https://github.com/wmealing/example-cobol-c-callback ) but I'd figure i'd ask before commiting too much time to debugging this if I'm doing something obviously wrong.

     
  • Simon Sobisch

    Simon Sobisch - 2023-06-24

    So - your basic example does work as expected, but the more complex isn't, right?
    Note - the code uploaded there seems to set a callback function that doesn't exist at all ?!?

     
  • wmealing

    wmealing - 2023-06-24

    So - your basic example does work as expected, but the more complex isn't, right?

    Correct.

    Note - the code uploaded there seems to set a callback function that doesn't exist at all ?!?

    Unless i'm misunderstanding how callbacks works, it sets the callback to https://github.com/wmealing/ca/blob/main/src/curl-write-callback.cbl

    I will see checkpoint 1 and 2. The compute element on line 45 fails.

     
    • Simon Sobisch

      Simon Sobisch - 2023-06-30

      Hm, checking the callback function documentation at https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html the function looks right.

      I'm not sure about the magic number for writefunc in the constants - ar you sure that is correct?

      In any case - compiling your code results in :

      $> cobc -free -g curl-write-callback.cbl
      curl-write-callback.cbl:32: warning: handling of parameters passed BY VALUE is unfinished; implementation is likely to be changed [-Wunfinished]
         30 |        procedure division
         31 |            using
         32 >               by value contents
         33 |               by value element-size
         34 |               by value element-count
      curl-write-callback.cbl:33: warning: handling of parameters passed BY VALUE is unfinished; implementation is likely to be changed [-Wunfinished]
         31 |            using
         32 |               by value contents
         33 >               by value element-size
         34 |               by value element-count
         35 |               by value memory-structure
      curl-write-callback.cbl:34: warning: handling of parameters passed BY VALUE is unfinished; implementation is likely to be changed [-Wunfinished]
         32 |               by value contents
         33 |               by value element-size
         34 >               by value element-count
         35 |               by value memory-structure
         36 |           returning real-size.
      curl-write-callback.cbl:35: warning: handling of parameters passed BY VALUE is unfinished; implementation is likely to be changed [-Wunfinished]
         33 |               by value element-size
         34 |               by value element-count
         35 >               by value memory-structure
         36 |           returning real-size.
         37 |
      curl-write-callback.cbl:36: warning: program RETURNING is not implemented [-Wpending]
         34 |               by value element-count
         35 |               by value memory-structure
         36 >           returning real-size.
         37 |
         38 |        display "CHECKPOINT 1."
      

      so those message possibly mean "thin ice".

      Nonetheless, checking the generated C source:

      COB_EXT_EXPORT int      curl__write__callback (cob_u8_t *, cob_s64_t, cob_s64_t, cob_u8_t *);
      

      That looks fine for everything but the return value (int vs. 64bit type).
      To at least better match the type use binary-long unsigned (no change in codegen for the prototype yet).

      Checking further in the code we see that the program expects to have a COBOL caller and therefore gets the number of parameters from the last COBOL call - which obviously was less than you expect.
      To work around that:

             procedure division
      +          with C linkage
                 using
                    by value contents
                    by value element-size
                    by value element-count
                    by value memory-structure
                returning real-size.
      

      But as there a lot of warnings and the return doesn't match I'd personally suggest to have a minimal C wrapper that only takes the parameters, then executes a cob_call() to call COBOL. This has also the benefit that you can directly use curses types and defines in the C wrapper and use whatever you see most useful as types for COBOL.

       
  • wmealing

    wmealing - 2023-06-24

    Example run:

    % ./ca
    Starting..

    CALLBACK HANDLE IS: 0x0000000000404844
    MEMORY ADDRESS:0x0000000000408370
    MEMORY SIZE: +0016777216
    Running total: +0000000001
    CHECKPOINT 1.
    CHECKPOINT 2.

    attempt to reference invalid memory address (signal)

     
  • wmealing

    wmealing - 2023-06-25

    I've modified the trivial example and it seems to have the same behavior. See https://github.com/wmealing/example-cobol-c-callback.git now.

     
  • Anonymous

    Anonymous - 2023-06-26

    After compiling with --debug, the runtime gave additional output:

    libcob: src/curl-write-callback.cbl:45: error: LINKAGE item 'element-count' not passed by caller
    
     Last statement of "curl-write-callback" was COMPUTE at line 45 of src/curl-write-callback.cbl
        ENTRY curl-write-callback at src/curl-write-callback.cbl:38
     Last statement of FUNCTION "fetch-url" was CALL at line 79 of src/fetch-url.cbl
        ENTRY fetch-url at src/fetch-url.cbl:33
     Last statement of "ca" was MOVE
        MAIN-LOGIC at src/ca.cbl:30
        ENTRY ca at src/ca.cbl:21
    

    As far as I can tell libcurl is supposed to call this function with the relevant args. The function gets called, just not as I expect.

     
    😕
    2

    Last edit: Simon Sobisch 2023-06-30
  • wmealing

    wmealing - 2023-06-30

    I now have the trivial example working, I had to explicitly call out that the parameter being passed was 'by value'.

    -        procedure division USING VAL_FROM_C.
    +        procedure division USING BY VALUE VAL_FROM_C.
    

    Same tests on libcurl/more complex version did not yield results, but I will continue to investigate.

     

    Last edit: Simon Sobisch 2023-06-30
  • wmealing

    wmealing - 2023-06-30

    I'd like to know if the current example in the gnucobol programmers guide works works for anyone else, as its not working for me at the moment.

     
    • Simon Sobisch

      Simon Sobisch - 2023-06-30

      Which example in the PG do you refer to?

       
      • wmealing

        wmealing - 2023-07-03

        The example from https://gnucobol.sourceforge.io/faq/index.html "4.1.245 FUNCTION-ID" I for some reason thought I had seen it elsewhere too.

         
  • wmealing

    wmealing - 2023-07-03

    I was able to get my example code working, with the changes you suggested.

     

Anonymous
Anonymous

Add attachments
Cancel