HOME  NEWS  FORUM  DOWNLOAD  LINK
OpenCOBOL - an open-source COBOL compiler
Main Menu
Download
Documentation
Development
Who's Online
20 user(s) are online (8 user(s) are browsing Forum)

Members: 0
Guests: 20

more...
Powered by
SourceForge

Xoops

Creative Commons

OpenCOBOL Forum Index
   OpenCOBOL
     Issue between declared variables in linkage section in callee sub and "real" variables in caller sub
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
eraso
Posted on: 2012/2/27 17:06
Not too shy to talk
Joined: 2006/11/22
From: SUTEC - Fontenay sous Bois (France)
Posts: 21
Issue between declared variables in linkage section in callee sub and "real" variables in caller sub
Hello,

I have encountered the code below in the application I'm migrating :

PROGRAM PRG1
       *
        IDENTIFICATION DIVISION.
        PROGRAM-ID. PRG1.

        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 STR-PRG1.
           10  BUFFER PIC X(2000).

        PROCEDURE DIVISION.
        INITIALIZE STR-PRG1.
        CALL "PRG2" USING STR-PRG1.
        EXIT PROGRAM.

SUB PRG2
       *
        IDENTIFICATION DIVISION.
        PROGRAM-ID. PRG2.

        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 STR-PRG2.
           10  BUFFER PIC X(16000).
        LINKAGE SECTION.
        01 STR-PRG1.
           10  BUFFER PIC X(16000).

        PROCEDURE DIVISION.
        MOVE STR-PRG1 TO STR-PRG2.
        EXIT PROGRAM.

As you can read the programs above :
- the caller sub declares a variable of 2000 bytes
- the callee sub declares the same variable of 16000 bytes through the linkage section and a working variable of the same length

The only action made is to copy the value from caller variable to callee variable :
The translation in C language for the line
  MOVE STR-PRG1 TO STR-PRG2.

is
  {
    memcpy (b_5, b_7, 16000);
  }

that is syntactically correct
but the real length of b_7 is 2000 not 16000 so an exception is risen "Attempt to reference unallocated memory (Signal SIGSEGV)" (OC 1.1) / "Memory Fault" (OC 0.33)

I understand that this issue (maybe someone has already written a post about this problem but I haven't found any) is due to the asset i'm working on but could the problem and the solution may lie between using the cob_module structure and the cob_move function, as when the move action between two variables of different lengths is done by the cob_move function ?

KR,

eraso
btiffin
Posted on: 2012/2/27 21:25
Home away from home
Joined: 2008/6/7
From: CANADA
Posts: 1196
Re: Issue between declared variables in linkage section in callee sub and "real" variables in caller sub
I don't think the memcpy is the problem.
[btiffin@home cobol]$ cobc -x prg1.cob prg2.cob
[btiffin@home cobol]$ ./prg1
Attempt to reference unallocated memory (Signal SIGSEGV)
Abnormal termination - File contents may be incorrect
[btiffin@home cobol]$ cobc -x -W -debug -g prg1.cob prg2.cob
prg1.cob:11: Warning: CALL statement not terminated by END-CALL
prg2.cob:13: Warning: LINKAGE item 'STR-PRG1' is not a PROCEDURE USING parameter
[btiffin@home cobol]$ ./prg1
prg2.cob:13: libcob: BASED/LINKAGE item 'STR-PRG1' has NULL address

Looks more like an error with STR-PRG1 naming on the prg2.cob PROCEDURE DIVISION.

OpenCOBOL is pretty nice about messages with -W, -g and -debug.

Try
       PROGRAM-ID. PRG2.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STR-PRG2.
       10 BUFFER PIC X(16000).
       LINKAGE SECTION.
       01 STR-PRG1.
       10 BUFFER PIC X(16000).

       PROCEDURE DIVISION using str-prg1.
       MOVE STR-PRG1 TO STR-PRG2.
       EXIT PROGRAM.

and then
[btiffin@home cobol]$ cobc -x -W -debug -g prg1.cob prg2.cob
prg1.cob:11: Warning: CALL statement not terminated by END-CALL
[btiffin@home cobol]$ ./prg1
[btiffin@home cobol]$ 

It won't abend, but the last 14000 chars in PRG2 STR-PRG2 is going to be a load of junk.

Cheers,
Brian
eraso
Posted on: 2012/2/28 10:42
Not too shy to talk
Joined: 2006/11/22
From: SUTEC - Fontenay sous Bois (France)
Posts: 21
Re: Issue between declared variables in linkage section in callee sub and "real" variables in caller sub
Hello,

Btiffin, thanks for your reply.

Well I made a mistake when I wrote the program samples for the problem I encountered in the asset I'm working with (I may repeat myselft but, in french, cela va sans dire mais ca va mieux en le disant).
What I wanted to point is around the definition of variables in linkage section and their use in callee sub and the real definition in caller sub.
So I wrote two executables PRG1 and PRG3 calling PRG2 as you can see below :
(I compiled PRG1 and PRG3 as executable - cobc -x -debug - and PRG2 as shared library - cobc -m -debug)

PRG1 :
*
 IDENTIFICATION DIVISION.
 PROGRAM-ID. PRG1.

 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01 STR-PRG1.
   10  BUFFER PIC X(2000).

 PROCEDURE DIVISION.
 INITIALIZE STR-PRG1.
 CALL "PRG2" USING STR-PRG1.
 EXIT PROGRAM.

PRG3 :
*
 IDENTIFICATION DIVISION.
 PROGRAM-ID. PRG3.

 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01 STR-PRG1.
   10  BUFFER PIC X(16000).

 PROCEDURE DIVISION.
 INITIALIZE STR-PRG1.
 CALL "PRG2" USING STR-PRG1.
 EXIT PROGRAM.

PRG2 :
 IDENTIFICATION DIVISION.
 PROGRAM-ID. PRG2.

 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01 STR-PRG2.
   10  BUFFER PIC X(16000).
 LINKAGE SECTION.
 01 STR-PRG1.
   10  BUFFER PIC X(16000).

 PROCEDURE DIVISION USING STR-PRG1.
 MOVE STR-PRG1 TO STR-PRG2.
 DISPLAY "MOVE DONE".
 EXIT PROGRAM.

As you can read, if everything goes right, "MOVE DONE" is displayed on console.

Everything goes right for PRG3 but for PRG1, the output is :
Quote:
PROGRAM-ID: PRG1: ENTRY PRG1
PROGRAM-ID: PRG1: MAIN SECTION
PROGRAM-ID: PRG1: MAIN PARAGRAPH
PROGRAM-ID: PRG1 Line: 11 Statement: INITIALIZE
PROGRAM-ID: PRG1 Line: 12 Statement: CALL
PROGRAM-ID: PRG2 Line: 14 Statement: MOVE
PRG2.cbl:14: Attempt to reference unallocated memory (Signal SIGSEGV)
Abnormal termination - File contents may be incorrect


This because in PRG1 STR-PRG1 is defined of 2000 bytes long but in the linkage section of PRG2, STR-PRG1 is defined of 16000 bytes long, so the move order in PRG2 translated to memcopy instruction try to copy 16000 bytes, that is more than the real length of STR-PRG1 defined in PRG1, so that's why the exception "Attempt to reference unallocated memory (Signal SIGSEGV)" is risen.

So my question is could the problem and the solution may lie between using the cob_module structure and the cob_move function, as when the move action between two variables of different lengths is done by the cob_move function ?

human
Posted on: 2012/2/28 11:45
Home away from home
Joined: 2007/5/15
From: GERMANY
Posts: 1416
Re: Issue between declared variables in linkage section in callee sub and "real" variables in caller sub
As btiffin said: This is a coding error, not a runtime issue. Therefore I believe there will be no change to cob_module/cob_move as it SHOULD crash here (Think of important stuff you pass on CALL and expect to be used).
What I hope is that the size of the using/linkage items will be checked in the future (at least with -debug) and the runtime exits with an error as a segfault is not easy to find.

human
eraso
Posted on: 2012/2/28 13:55
Not too shy to talk
Joined: 2006/11/22
From: SUTEC - Fontenay sous Bois (France)
Posts: 21
Re: Issue between declared variables in linkage section in callee sub and "real" variables in caller sub
Hello,

Thank you for your time Btiffin and Human.

Human, the suggestion you made is The solution.

By now I rewrite manually the parts wrongly coded.

KR,

eraso
Threaded | Newest First Previous Topic | Next Topic | Top

Register To Post
 
Copyright (C) 2005 The OpenCOBOL Project. All rights reserved.
Powered by Xoops2 | PHP | MySQL | Apache
ocean-net