Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2017-12-05
17:25 • Closed ticket [4f6a1ebd64]: ensemble: segmentation fault when -subcommand and -... artifact: 1435481618 user: dgp
15:59
[4f6a1ebd64] Stop crash when same value passed to the -map and -subcommands options of ensemble conf... check-in: 61096afb17 user: dgp tags: trunk
15:52
[4f6a1ebd64] Stop crash when same value passed to the -map and -subcommands options of ensemble conf... check-in: 866442a1aa user: dgp tags: core-8-branch
15:27
[4f6a1ebd64] Stop crash when same value passed to the -map and -subcommands options of ensemble conf... check-in: 1b11773899 user: dgp tags: core-8-6-branch
14:57
[4f6a1ebd64] Stop crash when same value passed to the -map and -subcommands options of ensemble conf... check-in: 67fc11277f user: dgp tags: core-8-5-branch
14:45 • Ticket [4f6a1ebd64] ensemble: segmentation fault when -subcommand and -map valu... artifact: 8c87e539c2 user: dgp
14:45 • Ticket [4f6a1ebd64]: 3 changes artifact: 2ee78c0cc2 user: dgp
2017-12-04
21:14 • Ticket [4f6a1ebd64]: 3 changes artifact: 72c25ef87b user: sebres
18:37 • Ticket [4f6a1ebd64]: 3 changes artifact: 79960313cc user: dgp
18:36
[4f6a1ebd64] Different fix for the problem. Re-order the filling of the subcommand table so there i... check-in: f8daa294c6 user: dgp tags: bug-4f6a1ebd64
10:18 • Ticket [4f6a1ebd64] ensemble: segmentation fault when -subcommand and -map valu... artifact: 41fbd745dc user: pooryorick
2017-11-30
18:57 • Ticket [4f6a1ebd64]: 3 changes artifact: 7f4f92e7ab user: sebres
18:18 • Ticket [4f6a1ebd64]: 3 changes artifact: 8208078e07 user: dgp
18:14
[4f6a1ebd64] Fix to stop new crashing test namespace-42.9 closed check-in: d3ba3a9f51 user: dgp tags: bug-4f6a1ebd64
2017-11-29
14:18 • Pending ticket [4f6a1ebd64]: ensemble: segmentation fault when -subcommand and ... artifact: 806a80e891 user: sebres
14:07
fix [4f6a1ebd64] bug without duplication, + additional test case to cover the last wins case (on dic... closed check-in: f9e184a041 user: sebres tags: core_8_5_fix_4f6a1ebd64
13:59
Cherry-picked test-cases from [046a5af026]: fix for issue [4f6a1ebd64]: ensemble: segmentation fault... check-in: 2f25d1a37b user: sebres tags: core-8-5-branch
2017-11-28
21:28 • Open ticket [4f6a1ebd64]: ensemble: segmentation fault when -subcommand and -ma... artifact: 744572fd67 user: sebres
18:19 • Ticket [4f6a1ebd64]: 4 changes artifact: 8a3aeaf7d8 user: dgp
18:19 • Ticket [4f6a1ebd64]: 5 changes artifact: 9ca873754f user: dgp
2017-11-25
16:41 • Closed ticket [4f6a1ebd64]. artifact: 2362961bbc user: pooryorick
16:36
Fix for issue [4f6a1ebd64]: ensemble: segmentation fault when -subcommand and -map values are the sa... check-in: 046a5af026 user: pooryorick tags: core-8-branch
15:41 • New ticket [4f6a1ebd64] ensemble: segmentation fault when -subcommand and -map ... artifact: fcd4c610d1 user: pooryorick

ensemble: segmentation fault when -subcommand and -map values are the same object

Closed / Fixed4f6a1ebd64cffa3ef795d83cb15d92c40cfb12bdCreated 9 years agoUpdated 9 years ago
Descriptionwiki2017-11-25 15:41:13
The following example causes a segmentation fault:

namespace eval n {namespace ensemble create}
dict set list one ::two
namespace ensemble configure n -subcommands $list -map $list
n one

The problem is that tclEnsemble.c/BuildEnsembleConfig gets hold of a List internal representation of ensemblePtr->subcommandList, but then converts the internal representation of ensemblePtr->subcommandList to a dictionary. If it so happens that ensemblePtr->subcommandList and ensemblePtr->subcommandDict are the same Tcl_Obj, the storage for the List internal representation is deallocated.

A similar issue is [1670091].

Referenced by9
  • 61096afb17[4f6a1ebd64] Stop crash when same value passed to the -map and -subcommands options of ensemble configuration. check-in2017-12-05 15:59:09
  • 866442a1aa[4f6a1ebd64] Stop crash when same value passed to the -map and -subcommands options of ensemble configuration. check-in2017-12-05 15:52:55
  • 1b11773899[4f6a1ebd64] Stop crash when same value passed to the -map and -subcommands options of ensemble configuration. check-in2017-12-05 15:27:21
  • 67fc11277f[4f6a1ebd64] Stop crash when same value passed to the -map and -subcommands options of ensemble configuration. check-in2017-12-05 14:57:05
  • f8daa294c6[4f6a1ebd64] Different fix for the problem. Re-order the filling of the subcommand table so there is no longer a conflict where multiple intreps of a single value are sought. If the mapDict and exportList are the same, then each key in the mapDict is known to be an element of the exportList without needing to check. check-in2017-12-04 18:36:14
  • d3ba3a9f51[4f6a1ebd64] Fix to stop new crashing test namespace-42.9 check-in2017-11-30 18:14:56
  • f9e184a041fix [4f6a1ebd64] bug without duplication, + additional test case to cover the last wins case (on dict's like {one ::two one ::three}). check-in2017-11-29 14:07:22
  • 2f25d1a37bCherry-picked test-cases from [046a5af026]: fix for issue [4f6a1ebd64]: ensemble: segmentation fault when -subcommand and -map values are the same object. check-in2017-11-29 13:59:42
  • 046a5af026Fix for issue [4f6a1ebd64]: ensemble: segmentation fault when -subcommand and -map values are the same object. check-in2017-11-25 16:36:22
pooryorickwiki2017-11-25 16:41:36

Fixed in commit [046a5af026].

dgpplain2017-11-28 18:19:27
Is there a reason TclListCopy() did not figure in the solution here?
dgpplain2017-11-28 18:19:52
er, TclListObjCopy()
sebresplain2017-11-28 21:28:23
> Is there a reason TclListCopy() did not figure in the solution here?

In my opinion this is anyway too hard to duplicate a list/dict in such obscure case (normally it would be enough to use construct as below to say it is in the dict instead of search in the dict):
```
if (!(i & 1) && i < subcmdc-1) {
    target = subcmdv[i+1];
} else {
    target = NULL;
}
```
But unfortunately there is another obscure case (map used quasi as filtering):
```
proc ::two {} {return 2}
proc ::three {} {return 3}
set list {one ::two one ::three}
namespace ensemble configure n -subcommands $lst -map $lst

% n one
3
```

But I've another own branch where ensembles map/list handling rewritten, and this case with duplication is fixed there. I'll try to rebase it here.

Additionally I reopen this because currently all versions (supported ensembles) are affected.
ATM fixed for core-8-branch only, I'll provide new one for 8.5 and if accepted reintegrate it for all versions (8.5 and higher).
The test case is cherry-picked as is.
sebreswiki2017-11-29 14:18:35

Fixed in [f9e184a041] for 8.5 without duplication, +1 additional test-case to cover the last wins case (on dict's like {one ::two one ::three}, where "one" should be mapped to "::three").

And small code review to make the things simpler.

If it is accepted I'll reintegrate it into 8.6 and higher (consider the move from tclNamesp.c to tclEnsemble.c).

dgpplain2017-11-30 18:18:57
I prefer the fix in [d3ba3a9f51] . It directly addresses the need to protect the pointer returned by Tcl_ListObjGetElements() and thus directly solves the problem instead of handling one particular set of circumstances known to cause the problem.  And it's smaller.

It does create and destroy one additional Tcl_Obj, but that's been engineered to be efficient already, and TclListObjCopy() only pays a large copying cost when it needs to, which is almost never, only for bug conditions like this.
sebresplain2017-11-30 18:57:19
> It does create and destroy one additional Tcl_Obj...

And converts dict to the list (and of course copy this list), then back to the dict, and then again to the list...

> And it's smaller.

Yes, the change as is 2-3 lines shorter... But are we in the save the lines mode now?
pooryorickwiki2017-12-04 10:18:42

I like sebres' fix because I like the idea of avoiding that additional type conversion, and I like the code as "documentation" of a real corner the case where the same value has explicitly been put to use as both a list and a dictionary.

dgpplain2017-12-04 18:37:42
See what you think of the new fix.

https://core.tcl.tk/tcl/info/f8daa294c6a88076
sebreswiki2017-12-04 21:14:07

I like the idea basically, but you've made a large performance regression on creating of each ensemble ("normal" case as well as "strange" case)...

For example in "normal" case (where map != list) by N sub-commands for ensemble: Previously (original):

  - iterate over LIST for N elements;
    - search in target hash-table (also N times).
    - search for replacement in DICT (also N times);
    - set value in target hash-table;
New ([f8daa294c6a88076]):
  - iterate over DICT for N elements;
    - iterate over LIST for N elements (N*N times);
      - search/create in target hash-table (also N*N times).
      - set value in target hash-table;
  - iterate over LIST for N elements (the second "if");
    - search in target hash-table (also N times).

E. g. by 100 sub-commands you've almost 10x overhead there:

-  23.508 µs/# 39808 # 42538.6 #/sec 935.809 nett-ms
+ 268.819 µs/#  3698 #  3720.0 #/sec 994.092 nett-ms
Therefore I don't think it would be better now...

What exactly don't like you in my solution [f9e184a041]? Possibly goto's?

dgpplain2017-12-05 14:45:11
I made another re-working and got a lot closer to what sebres contributed.

I think we're close enough to agreement now to merge the fix forward.  If we need more changes we can always make them.

Thanks!
dgpplain2017-12-05 14:45:27
I made another re-working and got a lot closer to what sebres contributed.

I think we're close enough to agreement now to merge the fix forward.  If we need more changes we can always make them.

Thanks!
dgpplain2017-12-05 17:25:20
Fix merged forward.  In reflection would have been better to fix only 8.6+.
Log in (anonymous is fine) to add a comment.